<?php
namespace console\controllers;


use common\components\DateUtil;
use common\components\giro\GiroDETSTA;
use common\components\giro\GiroDETSTAFej;
use common\components\giro\GiroDETSTALab;
use common\components\giro\GiroDETSTATetel;
use common\models\Contract;
use common\models\Ugiro;
use Faker\Provider\DateTime;
use yii\console\Controller;
use frontend\models\ContractForm;
use common\models\TicketType;
use common\models\Customer;
use common\models\Transfer;


class DetstaConsoleController extends Controller
{


    public function actionCreateContract()
    {


        $contractForm = new ContractForm(
            [
                'idAccount' => 1,
                'idUser' => 1
            ]
        );

        $customer = Customer::findOne(5559);
        $contractForm->customer = $customer;
        $contractForm->payment_method = Transfer::PAYMENT_METHOD_TRANSFER;


        $contractForm->ticketType = TicketType::find()->andWhere(['status' => TicketType::STATUS_ACTIVE, 'installment_enabled' => '1'])->one();


        $start = new \DateTime();
        $startString = $start->format("'Y-m-d");


        $contractForm->make();

    }


    /**
     * @param integer $id_ugiro
     */
    public function actionDetstaAnswer($id_ugiro, $accepted = 1)
    {

        /** @var \common\models\Ugiro $ugiro */
        $ugiro = Ugiro::findOne($id_ugiro);
        /** @var \common\components\giro\\GiroDETSTA $detsta */
        $detsta = new GiroDETSTA();
        /** @var \common\components\giro\GiroDETSTAFej $detsta */
        $detsta->fej = new GiroDETSTAFej();


        $detsta->fej->jelentesJelzo = 8;
        $detsta->fej->kezdemenyezoAzonosito = \Yii::$app->params['ugiro_kezdemenyezo_azonosito'];
        $detsta->fej->csoportosUzenetSorszam->osszeallitasDatuma = $ugiro->datum;
        $detsta->fej->csoportosUzenetSorszam->sorszam = $ugiro->number;
        $detsta->fej->detstaUzenetSorszam->osszeallitasDatuma = $ugiro->datum;
        $detsta->fej->detstaUzenetSorszam->sorszam = $ugiro->number;


        $detsta->lab = new GiroDETSTALab();

        $tetelek = $ugiro->requests;
        /** @var \common\models\TicketInstallmentRequest $tetel */
        foreach ($tetelek as $tetel) {
            $detstaTetel = new GiroDETSTATetel();

            $detstaTetel->tetelSorszam = $tetel->number;
            $detstaTetel->osszeg = $tetel->money;
            $detstaTetel->eredetiTetelElszamolasiDatuma = $ugiro->datum;
            if ($accepted == "1") {
                $detstaTetel->visszajelzesInformacio = GiroDETSTATetel::$INFORMACIO_TELJESITETT;
            } else {
                $detstaTetel->visszajelzesInformacio = '50';
            }
            $detstaTetel->feldolgozasDatum = $ugiro->datum;
            $detstaTetel->terhelesiDatum = $ugiro->datum;
            $detstaTetel->valaszHivatkozasiKod = '1234';
            $detstaTetel->eredetiHivatkozasiKod = '1234';
            $detstaTetel->ugyfelAzonosito = $tetel->customer->id_customer;

            $detsta->tetelek[] = $detstaTetel;


        }

        $content = $detsta->toString();

        $fn = 'c:\tmp\detsta.txt';

        file_put_contents($fn , $content);
        echo "File saved: "  . $fn;
        
    }

    /**
     * Recalculate the request target time at filed for ticket_installment_requests,
     * where request status is pending and contract is active
     */
    public function actionFixContracts()
    {
        $query = Contract::find();
        /** @var /common/models/Contract[] $contracts */
        $contracts = $query->all();

        /** @var /common/models/Contract $contract */
        foreach ($contracts as $contract) {
            if ($contract->isFlagActive()) {

                $requests = $contract->requests;
                $started_at = $contract->started_at;

                for ( $i = 0; $i < 11; $i++ ){
                    $request = $requests[$i];
                    /** @var /common/models/TicketInstallmentRequest $request */
                    $pending = $request->isStatusPending();
                    if ($pending) {
                        echo "\nContract: ".$contract->id_contract."; Priority:" . $request->priority . "\n";

                        $date =  \DateTime::createFromFormat("Y-m-d H:i:s", $started_at, new \DateTimeZone( 'UTC'));
                        $date = DateUtil::addMonth($date, $request->priority);
                        $date->setTime(0,0,0);

                        echo "\t".$started_at ." - " . $request->priority . " - ".$request->request_target_time_at . " -> " . $date->format ( 'Y-m-d H:i:s' );

                        $request->request_target_time_at = $date->format ( 'Y-m-d H:i:s' );

                        $saved = $request->save(false);

                        if ( $saved ){
                            echo " done" ;
                        }else{
                            echo " failed";
                        }

                    }
                }


            }

        }
    }


}