<?php

namespace Tests\Services\OrderPackageCommoditiesService\RegroupOrderCommoditiesTest;

use App\Commodity;
use App\OracleActAllocationDetails;
use App\OracleBasSKU;
use App\OracleDOCOrderHeader;
use App\Order;
use App\OrderPackage;
use App\OrderPackageCommodities;
use App\Owner;
use App\Services\OracleDOCOrderHeaderService;
use App\Services\OrderPackageCommoditiesService;
use App\Services\OrderService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class RegroupOrderCommoditiesTest extends TestCase
{
    /**
     * @var OrderPackageCommoditiesService $service
     */
    private $service;
    private $data = [];

    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('OrderPackageCommoditiesService');
        $order = factory(Order::class)->create();
        $this->data['order'] = $order;

        $wmsOrderHeader = factory(OracleDOCOrderHeader::class)->make([
            'customerid'=>'TEST_OW','sostatus'=>99,'userdefine1'=>'TEST_CA','warehouseid'=>'WH_TEST'
        ]);
        $this->data['orderHeader'] = $wmsOrderHeader;

        $orderPackage = factory(OrderPackage::class)->create(['order_id'=>$order->id]);
        $this->data['orderPackage'] = $orderPackage;

        $basSKUs = collect();
        for ($i = 0;$i<2;$i++){
            $basSKUs->push(factory(OracleBasSKU::class)->make(['customerid' => 'TEST_OW']));
        }

        $owner =factory(Owner::class)->create(['code'=>'TEST_OW']);
        $this->data['owner'] = $owner;


        $this->data['commodities'] = collect();
        foreach ($basSKUs as $item) {
            $this->data['commodities']->push(factory(Commodity::class)->create(['owner_id'=>$owner->id,'sku'=>$item['sku']]));
        }

        $OracleActAllocationDetails = collect();
        foreach ($this->data['commodities'] as $commodity) {
            $OracleActAllocationDetails->push(factory(OracleActAllocationDetails::class)->make(
                ['orderno'=>$wmsOrderHeader->orderno,'customerid'=>'TEST_OW','sku'=>$commodity->sku,$orderPackage->logistic_number])
            );
        }

        $this->data['order_package_commodities'] = collect();
        foreach ($OracleActAllocationDetails as $OracleActAllocationDetail) {
            $commodity = $this->data['commodities']->where('sku',$OracleActAllocationDetail->sku)->first();
            $this->data['order_package_commodities']->push(factory(OrderPackageCommodities::class)->create(
                ['order_package_id'=>$orderPackage->id,'commodity_id'=>$commodity->id,'amount'=>$OracleActAllocationDetail->qty_each])
            );
        }
        $wmsOrderHeader->setRelation('actAllocationDetails',$OracleActAllocationDetails);
    }

    /**
     * @test
     */
    public function regroupOrderCommodities()
    {
        $order = $this->data['order'];
        $opc = OrderPackageCommodities::query()->with('package.order')->whereHas('package.order',function ($query)use($order){
            $query->where('code',$order->code);
        })->get();
        $params = $this->service->regroupOrderCommodities($opc);
        $this->assertNotNull($params);

    }

    public function tearDown(): void
    {
        $this->data['order']->delete();
        $this->data['orderHeader']->delete();
        $this->data['owner']->delete();
        foreach ($this->data['order_package_commodities'] as $order_package_commodity) {
            $order_package_commodity->delete();
        }
        foreach ($this->data['commodities'] as $commodity) {
            $commodity->delete();
        }
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
