<?php

namespace Tests\Services\OrderService\GetOrderByLogisticNumberTest;

use App\Logistic;
use App\OracleDOCOrderHeader;
use App\Order;
use App\OrderPackage;
use App\Owner;
use App\Services\OrderService;
use App\Shop;
use App\Warehouse;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GetOrderByLogisticNumberTest extends TestCase
{
    /** @var OrderService $service */
    public $service;
    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('OrderService');
    }

    public function testGetOrderByLogisticNumber()
    {
        $order_package = OrderPackage::query()->orderByDesc('id')->first();
        $logistic_number = $order_package->logistic_number;
        $orderHeader = OracleDOCOrderHeader::query()->whereHas('actAllocationDetails',function($query)use($order_package){
            $query->where('PickToTraceID',$order_package->logistic_number);
        })->first();

        if(!$orderHeader){
            $orderHeader = OracleDOCOrderHeader::query()->with('actAllocationDetails')->orderBy('AddTime')->first();
            $logistic_number = data_get($orderHeader,'actAllocationDetails.*.picktotraceid')[0];
        }
        $order = $this->service->getOrderByLogisticNumber($logistic_number);
        $this->assertEquals((string)$orderHeader->addtime,(string)$order->created_at);
        $this->assertEquals($orderHeader->orderno,$order->code);

        $shop = Shop::query()->where('name',$orderHeader->issuepartyname)->first();
        $this->assertEquals($shop->id,$order->shop_id);

        $owner = Owner::query()->where('code',$orderHeader->customerid)->first();
        $this->assertEquals($owner->id, $order->owner_id);
        $this->assertEquals($orderHeader->soreference1,$order->client_code);

        $logistic = Logistic::query()->where('code',$orderHeader->userdefine1)->first();
        $this->assertEquals($logistic->id,$order->logistic_id);
        $this->assertEquals($orderHeader->c_contact,$order->consignee_name);

        $consignee_phone = empty($orderHeader->c_tel2)?$orderHeader->c_tel1:$orderHeader->c_tel2;
        $this->assertEquals($consignee_phone,$order->consignee_phone);

        $this->assertEquals($orderHeader->c_province,$order->province);
        $this->assertEquals($orderHeader->c_city,$order->city);
        $this->assertEquals($orderHeader->c_district,$order->district);
        $this->assertEquals($orderHeader->c_address1,$order->address);
        $this->assertEquals($orderHeader->oracleBASCode_codename_c,$order->wms_status);

        $warehouse = Warehouse::query()->where('code',$orderHeader->warehouseid)->first();
        $this->assertEquals($warehouse->id,$order->warehouse_id);
        $this->assertEquals($orderHeader->edittime,$order->wms_edittime);
    }
}
