<?php

namespace Tests\Services\OrderService\GetCreateOrderModelByWMSOrderHeadersTest;

use App\Logistic;
use App\Owner;
use App\Services\common\DataHandlerService;
use App\Services\LogisticService;
use App\Services\OracleDOCOrderHeaderService;
use App\Services\OrderService;
use App\Services\OwnerService;
use App\Services\ShopService;
use App\Services\WarehouseService;
use App\Shop;
use App\Warehouse;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GetCreateOrderModelByWMSOrderHeadersTest extends TestCase
{
    /**
     * @var OrderService $service
     * @var OracleDOCOrderHeaderService $orderHeaderService
     * @var DataHandlerService $dataHandlerService
     * @var OwnerService $ownerService
     * @var LogisticService $logisticService
     * @var ShopService $shopService
     * @var WarehouseService $warehouseService
     */
    public $service;
    public $orderHeaderService;
    public $dataHandlerService;
    public $ownerService;
    public $logisticService;
    public $shopService;
    public $warehouseService;
    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('OrderService');
        $this->orderHeaderService = app('OracleDocOrderHeaderService');
        $this->dataHandlerService=app('DataHandlerService');
        $this->ownerService=app('OwnerService');
        $this->logisticService=app('LogisticService');
        $this->shopService=app('ShopService');
        $this->warehouseService=app('WarehouseService');
    }

    public function testGetCreateOrderModelByWMSOrderHeaders()
    {
        $carbon =Carbon::now()->subHours(10);
        $date = '2020-09-18 18:13:50';
        $orderHeaders = $this->orderHeaderService->getWMSOrderOnStartDate($carbon);
        $warehouses = $this->getWareHouses($orderHeaders);
        $owners = $this->getOwners($orderHeaders);
        $logistics = $this->getLogistics($orderHeaders);
        $shops = $this->getShops($orderHeaders);

        $orders = $this->service->getByWmsOrders($orderHeaders);
        $crate_orders = $this->service->getCreateOrderModelByWMSOrderHeaders($orderHeaders,$orders,$warehouses,$owners,$logistics,$shops);
        if($orders->count() == $orderHeaders->count() ){
            $this->assertEquals($orders->count(),$orderHeaders->count());
        }
        foreach ($orderHeaders as $orderHeader) {
            $order = $crate_orders->where('code',$orderHeader->orderno)->first();
            $exist_order = $orders->where('code',$orderHeader->orderno)->first();
            if(isset($exist_order))continue;
            $this->assertNotNull($order);
            $warehouse = Warehouse::query()->where('code',$orderHeader->warehouseid)->first();
            if($orderHeader->warehouseid ?? false)
                $this->assertEquals($warehouse->id ?? '',$order->warehouse_id ?? '');

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

            $logistic = Logistic::query()->where('code',$orderHeader->userdefine1??'')->first();
            if($orderHeader->userdefine1 ?? false)
                $this->assertEquals($logistic->id ?? '',$logistic->id ?? '');
            $shop = Shop::query()->where('name',$orderHeader->issuepartyname)->where('owner_id',$owner->id)->first();
            if($orderHeader->issuepartyname ?? false)
                $this->assertEquals($shop->name,$orderHeader->issuepartyname);
            $this->assertEquals($order->consignee_name ,$orderHeader->c_contact);
            $this->assertEquals($order->consignee_phone ,empty($orderHeader->c_tel2)?$orderHeader->c_tel1:$orderHeader->c_tel2);
            $this->assertEquals($order->province ,$orderHeader->c_province);
            $this->assertEquals($order->city ,$orderHeader->c_city);
            $this->assertEquals($order->district ,$orderHeader->c_district);
            $this->assertEquals($order->client_code ,$orderHeader->soreference1);
            $this->assertEquals($order->wms_edittime ,(string)$orderHeader->edittime);
            $this->assertEquals($order->wms_status ,$orderHeader->oracleBASCode_codename_c);
            $this->assertEquals((string)$order->created_at ,(string)$orderHeader->addtime);
        }
    }

    public function getWareHouses($orderHeaders)
    {
        return $this->warehouseService->getByWmsOrders($orderHeaders);
    }
    public function getOwners($orderHeaders)
    {
        return $this->ownerService->getByWmsOrders($orderHeaders);
    }
    public function getLogistics($orderHeaders)
    {
        return $this->logisticService->getByWmsOrders($orderHeaders);
    }
    public function getShops($orderHeaders)
    {
        return $this->shopService->getByWmsOrders($orderHeaders);
    }
}
