<?php


namespace Tests\Services\RejectedBillService;


use App\OracleDOCASNHeader;
use App\RejectedBill;
use App\Services\OwnerService;
use App\Services\RejectedBillService;
use App\Services\StoreItemService;
use App\Services\StoreService;
use App\Services\WarehouseService;
use App\Store;
use Carbon\Carbon;
use Tests\TestCase;

class SyncLoadedStatusByAsnHeaderTest extends TestCase
{
    /**
     * @var RejectedBillService $service
     *
     */
    public $service;
    public $asnHeaders;
    public $params;
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $startDate = \Illuminate\Support\Carbon::now()->subSeconds(300);
        $this->service=app(RejectedBillService::class);
        $this->asnHeaders=OracleDOCASNHeader::query()
            ->with(['asnType', 'asnStatus', 'asnDetails' => function ($query) {
                $query->with(['lineStatus', 'qualityStatus','basSku']);
            }])
            ->where('addTime', '>=', $startDate)
            ->get();
    }

    /**
     * @test
     */
    public function testSyncLoadedStatusByAsnHeaderTest(){
        if (empty($this->asnHeaders)){
            $this->assertNull($this->asnHeaders);
            return;
        }
        $updateCollect=$this->service->getUpdateCollect($this->asnHeaders);
        if ($updateCollect->isEmpty()){
            $this->assertEmpty($updateCollect);
            return;
        }
        $logisticNumberReturn=$this->service->getLogisticNumberReturn($updateCollect);
        if (count($logisticNumberReturn)==0) return;
        $rejectedBills=$this->service->getRejectedBills($logisticNumberReturn);
        if ($rejectedBills->isEmpty()){
            $this->assertEmpty($rejectedBills);
            return null;
        }
        $this->assertNotNull($rejectedBills);
        $this->params=[];
        $updateParams=$this->service->getNeedUpdateLoadedStatusByWms($this->asnHeaders);
        array_shift($updateParams);
        if (count($updateParams)>=1){
            foreach ($updateParams as $param){
                $this->params[] = $param['logistic_number_return'];
            }
            $this->service->syncLoadedStatusByAsnHerder($this->asnHeaders);
        }
        $this->assertNotNull($logisticNumberReturn);
    }
    public function tearDown(): void
    {
        if (!$this->params)return;
        $rejectBills=RejectedBill::query()->whereIn('logistic_number_return', $this->params)->get();
        foreach ($rejectBills as $rejectBill){
            $rejectBill->is_loaded=0;
            $rejectBill->update();
        }
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
