<?php

namespace Tests\Unit;

use App\Authority;
use App\Commodity;
use App\CommodityBarcode;
use App\MeasuringMachine;
use App\Order;
use App\OrderIssue;
use App\OrderIssueRejectedBill;
use App\OrderPackage;
use App\OrderPackageCommodities;
use App\RejectedBill;
use App\RejectedBillItem;
use App\Role;
use App\User;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Matrix\add;

class OrderIssueTest extends TestCase
{

    public function getTestResult()
    {
        $orderIssue = factory(OrderIssue::class)->create(['rejecting_status' => '未退回']);
        $orderPackage = factory(OrderPackage::class)->create(['order_id' => $orderIssue->order->id]);
        $commodities = factory(Commodity::class)->times(3)->create(['owner_id' => $orderIssue->order->owner_id]);
        $orderPackageCommodities = collect();
        $commodities_barcodes = collect();
        foreach ($commodities as $commodity) {
            $commodities_barcode = factory(CommodityBarcode::class)->create(['commodity_id' => $commodity->id]);
            $commodities_barcodes->push($commodities_barcode);
            $orderPackageCommodity = factory(OrderPackageCommodities::class)->create(['order_package_id' => $orderPackage->id, 'amount' => 1, 'commodity_id' => $commodity->id]);
            $orderPackageCommodities->push($orderPackageCommodity);
        }
        $rejectedBill = factory(RejectedBill::class)->create(['logistic_number_return' => $orderIssue->logistic_number_return]);
        $rejectedBill_items = collect();
        foreach ($commodities_barcodes as $commodities_barcode) {
            $item = factory(RejectedBillItem::class)->create(['id_rejected_bill' => $rejectedBill->id, 'barcode_goods' => $commodities_barcode->code, 'amount' => 1]);
            $rejectedBill_items->push($item);
        }
        $orderIssueRejectedBill = factory(OrderIssueRejectedBill::class)->create(['order_issue_id' => $orderIssue->id, 'logistic_number_return' => $rejectedBill->logistic_number_return]);
        return [
            'orderIssue' => $orderIssue,
            'orderPackage' => $orderPackage,
            'commodities' => $commodities,
            'orderPackageCommodities' => $orderPackageCommodities,
            'rejectedBill' => $rejectedBill,
            'rejectedBill_items' => $rejectedBill_items,
        ];
    }

    public function destroyResult(array $arr)
    {
        if($arr['orderIssue']){
            $arr['orderIssue']->order->delete();
            $arr['orderIssue']->delete();
        }
        if($arr['orderPackage'])$arr['orderPackage']->delete();
        if($arr['commodities']){
            foreach ($arr['commodities'] as $commodity) {
                CommodityBarcode::query()->where('commodity_id',$commodity->id)->delete();
                $commodity->delete();
            }
        }
        if($arr['rejectedBill'])$arr['rejectedBill']->delete();
        if($arr['rejectedBill_items']){
            foreach ($arr['rejectedBill_items'] as $rejectedBill_item) {
                $rejectedBill_item->delete();
            }
        }
        OrderIssueRejectedBill::query()->where('Order_Issue_Id',$arr['orderIssue']->id)->delete();
    }


    public function testSyncRejectingStatusComparisonIsEqual()
    {
        $arr = $this->getTestResult();
        $orderIssue = $arr['orderIssue'];
        $orderIssue->syncRejectingStatus();
        $orderIssue->refresh();
        $this->assertEquals($orderIssue->rejecting_status, "全部退回");
        $this->destroyResult($arr);
    }



}
