<?php


namespace Tests\Services\StoreService;


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

class GetParamsByAsnHeaderTest extends TestCase
{
    /**
     * @var StoreService $service
     * @var OwnerService $ownerService
     * @var WarehouseService $warehouseService
     *
     */
    public $service;
    public $asnHeaders;
    public $ownerService;
    public $warehouseService;
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $startDate = \Illuminate\Support\Carbon::now()->subSeconds(300);
        $this->service=app(StoreService::class);
        $this->ownerService=app(OwnerService::class);
        $this->warehouseService=app(WarehouseService::class);
        $this->asnHeaders=OracleDOCASNHeader::query()
            ->with(['asnType', 'asnStatus', 'asnDetails' => function ($query) {
                $query->with(['lineStatus', 'qualityStatus','basSku']);
            }])
            ->where('addTime', '>=', $startDate)
            ->get();
    }
    public function testGetParamsByAsnHeader(){
        if (empty($this->asnHeaders))return null;
        $stores = Store::query()->whereIn('asn_code',data_get($this->asnHeaders,'*.asnno'))->get();
        $owners=$this->ownerService->getByWmsOrders($this->asnHeaders);
        $warehouses=$this->warehouseService->getByWms($this->asnHeaders);
        $this->assertNotEmpty($this->asnHeaders);
        $this->assertNotEmpty($owners);
        $this->assertNotEmpty($warehouses);
        foreach ($owners as $owner) {
            $owners_code_map[$owner->code] = $owner;
        }
        foreach ($warehouses as $warehouse) {
            $warehouses_code_map[$warehouse->code] = $warehouse;
        }
        if (count($this->asnHeaders)>count($stores)){
            $params=$this->service->getParamsByAsnHeader($this->asnHeaders,$owners_code_map,$warehouses_code_map);
            $this->assertNotEmpty($params);
        }
    }
}
