<?php

namespace Tests\Services\OrderCommodityService;

use App\OracleActAllocationDetails;
use App\OracleDOCOrderHeader;
use App\Owner;
use App\Services\OrderCommodityService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GetReGroupActAllocationDetailsTest extends TestCase
{
    /** @var OrderCommodityService  $service */
    private $service;
    private $data;

    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderCommodityService');
//        $owner = factory(Owner::class)->create();
        $orderHeaders  = collect();
        $orderHeader = factory(OracleDOCOrderHeader::class)->make();
        $oracleActAllocationDetails = collect();
        for ($i=0;$i<=10;$i++){
            $oracleActAllocationDetails->push( factory(OracleActAllocationDetails::class)->make(['orderno'=>$orderHeader['orderno'],'customerid' => $orderHeader['customerid']]));
        }
        $orderHeader->setRelation('actAllocationDetails',$oracleActAllocationDetails);
        $orderHeaders->push($orderHeader);
        $this->data['orderHeaders'] = $orderHeaders;
    }

    /**
     * @test
     */
    public function getRegroupActAllocationDetails()
    {
        $orderHeaders = $this->data['orderHeaders'];
        $oracleActAllocationDetails = $orderHeaders->first()->actAllocationDetails;

        $regroupActAllocationDetails = $this->service->getRegroupActAllocationDetails($orderHeaders);

        $this->assertNotNull($oracleActAllocationDetails);
        $this->assertNotNull($regroupActAllocationDetails);

        $this->assertEquals(count($orderHeaders),$this->count($regroupActAllocationDetails));
        foreach ($regroupActAllocationDetails as $items) {
            foreach ($items as $item) {
                $details = $oracleActAllocationDetails
                    ->where('location',$item['location'])
                    ->where('customerid',$item['owner_code'])
                    ->where('sku',$item['sku']);
                $this->assertNotEmpty($details);
                $this->assertEquals(1, $details->count());
            }
        }
    }

    public function tearDown(): void
    {
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
