<?php
namespace Modules\Commande\Controller;

use Entity\Model\CaEntet;
use Modules\GlobalApplication\Outil\InitAxesImpCde;
use Skeleton\Controller\Controller;
use Helpers\Outil\Sql;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Date;
use Modules\Commande\Repository\CreationCommande;
use Modules\Commande\Service\CreationCommandeService;

class AbsModificationCommandeController extends Controller
{

    /**
     * Generate data for template @Commande/Creation/Ligne/ligne
     * @param array<mixed> $caEntet
     * @return array<mixed>
     */
    protected function newCaLigneData(int $numLigne, array $caEntet) : array
    {
        $arrayBuild = (new CreationCommandeService(
            $this->pdo,
            $this->pdoOxaFT,
            $this->getTranslationService()
        ))->buildCreateLigne($caEntet);
        
        $param = $arrayBuild['param'];

        $ligne['NO_PO'] = $numLigne;
        $ligne['NO_CDE'] = $idCommande = $caEntet['NO_CDE'];
        
        // Set default imputation
        $defaultImp = (new InitAxesImpCde($this->pdo, $caEntet))->getInitImputation([
            'CODE_ART' => '',
            'SousFamille' => null,
        ]);
        $ligne['SECTIMP'] = $defaultImp['SI'];
        $ligne['CODE_COMP'] = $defaultImp['CDC'];
        $ligne['SSCPTEANA'] = $defaultImp['SCA'];
        $ligne['CODGES'] = $defaultImp['CODGES'];
        $ligne['ID_CG']  = $defaultImp['ID_CG'];
        $ligne['LIBGES'] = $defaultImp['LIBGES'];

        $creationCde = new CreationCommande($this->pdo);
        if ($ligne['SECTIMP']) {
            $codeGestActif = $creationCde->codeGestionActif(($ligne['SECTIMP']), ($caEntet['CODE_STE']??null))['SICODG']??'';
            if (sql::toBool($codeGestActif)) {
                $param['MODIF_CODGES'] = true;
            }
        }

        $ligne['SAISIEREC']             = false;
        if (Sql::toBool($caEntet['CDE_EXP'])) {
            $ligne['RECEP_AUTO']            = false;
            $ligne['DATEDEBCONT']           = '';
            $ligne['DATEFINCONT']           = '';
            $ligne['CONTRAT_PERIOD']        = '';
            $ligne['FIN_DEB_PER']           = '';
            $ligne['CAEN_RECEPAUTO_JOUR']   = '';
            $ligne['CALI_RECEPAUTO_JOUR']   = '';
        } else {
            $ligne['RECEP_AUTO']            = $caEntet['RECEP_AUTO']??false;
            $ligne['DATEDEBCONT']           = $caEntet['DATEDEBCONT']??'';
            $ligne['DATEFINCONT']           = $caEntet['DATEFINCONT']??'';
            $ligne['CONTRAT_PERIOD']        = $caEntet['CONTRAT_PERIOD']??'';
            $ligne['FIN_DEB_PER']           = $caEntet['FIN_DEB_PER']??'';
            $ligne['CAEN_RECEPAUTO_JOUR']   = $caEntet['CAEN_RECEPAUTO_JOUR']??'';
            $ligne['CALI_RECEPAUTO_JOUR']   = $caEntet['CAEN_RECEPAUTO_JOUR']??'';
        }

        $arrayInitCs = [];

        $arrayInitCs += ChampSupp::initCs(
            'CA_LIGNE',
            'CA_ENTET',
            ChampSupp::autoCompletCodeEnr($idCommande, '', 'CA_ENTET'),
            $this->pdo
        );

        $arrayInitCs += ChampSupp::initCs(
            'CA_LIGNE',
            'SECTIMPU',
            ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'SECTIMPU', $ligne['SECTIMP']),
            $this->pdo
        );

        $arrayInitCs += ChampSupp::initCs(
            'CA_LIGNE',
            'CODECOMP',
            ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'CODECOMP', $ligne['CODE_COMP']),
            $this->pdo
        );

        $arrayInitCs += ChampSupp::initCs(
            'CA_LIGNE',
            'SSCPTANA',
            ChampSupp::autoCompletCodeEnrImputations($this->pdo, 'SSCPTANA', $ligne['SSCPTEANA']),
            $this->pdo
        );

        return [
            'tabPeriodicite' => $arrayBuild['tabPeriodicite'],
            'tabJoursAppli'  => $arrayBuild['tabJoursAppli'],
            'tabTypeDate'    => $arrayBuild['tabTypeDate'],
            'arrayInitCs'    => $arrayInitCs,
            'idCommande'     => $idCommande,
            'idLigneCS'      => ChampSupp::autoCompletCodeEnr($caEntet['NO_CDE'], $numLigne, 'CA_LIGNE'),
            'tabPeriode'     => $arrayBuild['tabPeriode'],
            'ligne'          => $ligne,
            'param'          => $param,
            'droit'          => $arrayBuild['droit'],
            'open'           => true,
            'commande'       => $caEntet,
            'planning'       => [['QTE_COMM' => '', 'DTE_LIV' => '']],
            'CDE_EXP'        => Sql::boolSql($caEntet['CDE_EXP']??false),
        ];
    }
}
