<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\OrderIssue;

class CreateOrderIssueRejectedBillTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_issue_rejected_bill', function (Blueprint $table) {
            $table->bigInteger('order_issue_id')->index();
            $table->string('logistic_number_return')->index();
        });
        $this->processOrderIssueRejectedBill();
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_issue_rejected_bill');
    }

    public function processOrderIssueRejectedBill()
    {
        OrderIssue::query()->withTrashed()->whereNotNull('logistic_number_return')->chunkById(200,function($orderIssues){
            $orderIssues->each(function($orderIssue){
                if($orderIssue->logistic_number_return)
                    $orderIssue->rejectedBills()->syncWithoutDetaching([$orderIssue->logistic_number_return]);
            });
        });
    }
}
