<?php

namespace App\Console\Commands;


use App\Classes\Modules\Transactions\Services\FetchesGroup;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\Transactions\Processors\ReleaseGoodsToCustomerProcessor;
use App\Classes\Modules\Transactions\Processors\CreateStorageInvoiceDocTransactionFixProcessor;
use Carbon\Carbon;
use Illuminate\Console\Command;

class FixGroupPaymentProblem extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fix-group-payment-problem';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This is a one time data patch for a problem result from Storage Invoice Implementation';

    /** @var FetchesGroup */
    private $fetchesGroup;

    /** @var ReleaseGoodsToCustomerProcessor */
    private $releaseGoodsToCustomerProcessor;

    /** @var FetchesOrder */
    private $fetchesOrder;

    /** @var CreateStorageInvoiceDocTransactionFixProcessor */
    private $createStorageInvoiceDocTransactionProcessor;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(FetchesGroup $fetchesGroup, ReleaseGoodsToCustomerProcessor $releaseGoodsToCustomerProcessor, FetchesOrder $fetchesOrder, CreateStorageInvoiceDocTransactionFixProcessor $createStorageInvoiceDocTransactionProcessor)
    {
        parent::__construct();
        $this->fetchesGroup = $fetchesGroup;
        $this->releaseGoodsToCustomerProcessor = $releaseGoodsToCustomerProcessor;
        $this->fetchesOrder = $fetchesOrder;
        $this->createStorageInvoiceDocTransactionProcessor = $createStorageInvoiceDocTransactionProcessor;
    }


    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        ini_set('memory_limit', '-1');

        $this->info(Carbon::now() . ': Start data patch.');
        $start   = new Carbon();

        /*
        $group = $this->fetchesGroup->execute(['id' => 325]);
        $this->info('FixGroupPaymentProblem group: '.json_encode($group));
        if ($group) {
            foreach ($group->groupTransactions as $groupTransaction) {
                $invoice = $groupTransaction->transaction;
                $this->info('FixGroupPaymentProblem group: '.json_encode($invoice));
                $pL = $invoice->owner;
                $this->releaseGoodsToCustomerProcessor->execute($pL, $invoice);
            }
        }
        */

        $order = $this->fetchesOrder->execute(['reference' => '729251424']);
        $packingLists = $order->destinationWarehousePackages;

        foreach ($packingLists as $packingList){
            $this->createStorageInvoiceDocTransactionProcessor->execute($packingList, true);
            dd(json_encode($packingList));
        }


        $end = new Carbon();
        $elapsedTime = $start->diff($end)->format('%H:%I:%S');

        $this->info(Carbon::now() . ': Done data patch. ElapsedTime: ' . $elapsedTime . '.');
    }
}
