<?php
namespace Modules\Commande\Service;

use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Modules\Commande\Repository\Commande;
use Modules\Commande\Repository\ModificationDetailCommande;
use Modules\Commande\Repository\CreationCommande;
 
// phpcs:ignore
class OutilCommandeService
{
    public static function convertCritereBdd(string $key) : string
    {
        $array = [
            '01_SOC'    => '',
            '02_FOU'    => 'CODE_FOU',
            '03_CTREME' => 'DAENT_CENTRE_EMETTEUR',
            '04_CTRLIV' => 'CTRLIVR',
            '05_TYPDOC' => 'TYP_DOC',
            '06_NODA'   => 'NO_DA',
            '07_SFAM'   => 'SSFAM',
            '08_SI'     => 'SECTIMP',
            '09_CDC'    => 'CODE_COMP',
            '10_SCA'    => 'SSCPTEANA',
            '11_ACD'    => 'ACD',
            '12_MDT'    => 'MOD_TRS',
            '13_TAMP'   => '',
        ];

        return $array[$key];
    }

    /**
     * parametreModifCde
     *
     * @param  \PDO $pdo
     * @param  int $idCommande
     * @return array<mixed>
     */
    public static function parametreModifCde($pdo, $idCommande)
    {
        /* GESTION DES DROITS */
        $commande = new Commande($pdo);
        $commande = $commande->commandeEntete((int)$idCommande);
        $modifDetail = new ModificationDetailCommande($pdo);
        
        $creationCdeRepo = new CreationCommande($pdo);
        $param['MODIF_CODGES']      = false;
        
        if ($commande) {
            if ($commande['MODELE']) {
                $modele = $creationCdeRepo->getModele($commande['MODELE']);
                
                $param['LOCK_SOC']       = Sql::boolSql($modele['SOCVER']);
                $param['LOCK_TYP']       = Sql::boolSql($modele['TYPVER']);
                $param['LOCK_TYPNMP']    = Sql::boolSql($modele['TYNMPVER']);
                $param['LOCK_FOU']       = sql::BoolSql($modele['FOUVER']);
                $param['LOCK_SAISIEREC'] = sql::BoolSql($modele['RECEPVER']);
                
                $param['LOCK_CTR_LIV']   = sql::BoolSql($modele['CTR_LVER']);
                $param['LOCK_SI']        = sql::BoolSql($modele['SIVER']);
                $param['LOCK_CDC']       = sql::BoolSql($modele['CDCVER']);
                $param['LOCK_SCA']       = sql::BoolSql($modele['SCAVER']);

                $param['LOCK_REM']       = sql::BoolSql($modele['REMVER']);
                
                $param['LOCK_ADCDEFOU']     = sql::BoolSql($modele['ADRVER']);
                $param['LOCK_CTCT_FOU']     = sql::BoolSql($modele['CNTVER']);
                $param['LOCK_ACC_CADRE']    = sql::BoolSql($modele['ACDVER']);
                $param['LOCK_ZNLIBNUM']     = sql::BoolSql($modele['ZLNVER']);
                $param['LOCK_REFOFFOU']     = sql::BoolSql($modele['REFFOVER']);
                
                $param['LOCK_CTR_EMET']  = sql::BoolSql($modele['CTR_EVER']);
                $param['LOCK_CTR_FACTU'] = sql::BoolSql($modele['CTR_FVER']);
                $param['LOCK_COND_PAIE'] = sql::BoolSql($modele['CDPVER']);
                $param['LOCK_MODE_PAIE'] = sql::BoolSql($modele['MDPVER']);
                $param['LOCK_MODE_LIV']  = sql::BoolSql($modele['MDLVER']);
                
                $param['LOCK_OBJET_CA']  = sql::BoolSql($modele['OBJETVER']);
                $param['LOCK_TITRE']     = sql::BoolSql($modele['NMTITVER']);
            }

            if ($commande['SECTIMP']??'') {
                $codeGestActif = $creationCdeRepo->codeGestionActif($commande['SECTIMP'], $commande['CODE_STE']??null)['SICODG']??'';
                if (sql::boolSql($codeGestActif)) {
                    $param['MODIF_CODGES'] = true;
                }
            }
        }
        
        $param['AFF_TMP'] = sql::paravrac($pdo, 'PARAMGEN', 'CTRLSEUILPARSFA')=='O'? true:false;
        
        $param['AFF_INTERFACE'] = sql::paravrac($pdo, 'PARAMGEN', 'INTERFACES_ACTIVES')=='O'? true:false;
        
        $INVISIBLE_SUR_CA_SI_CDC_SCA = Sql::paravrac($pdo, 'PARAMGEN', 'INVISIBLE_SUR_CA_SI_CDC_SCA');
        
        $param['SI_ACTIF']          = substr($INVISIBLE_SUR_CA_SI_CDC_SCA, 0, 1)!='O'? true:false;
        $param['CDC_ACTIF']         = substr($INVISIBLE_SUR_CA_SI_CDC_SCA, 2, 1)!='O'? true:false;
        $param['SCA_ACTIF']         = substr($INVISIBLE_SUR_CA_SI_CDC_SCA, 4, 1)!='O'? true:false;
        $param['TYPEACHAT_ACTIF']   = Sql::paravrac($pdo, 'PARAMFOU', 'INITTAC')? true:false;
        $param['CG_PAR_ID']         = \Helpers\Outil\Roles::droit($pdo, 'CODE_GESTION_PAR_ID');


        $param['LOCK_CDE_EXP'] = $modifDetail->countBRsurCde($idCommande)? false:true;
        
        /* gestion des droits */// @phpstan-ignore-next-line
        if (empty($param['LOCK_SAISIEREC']) || (isset($param['LOCK_SAISIEREC']) && $param['LOCK_SAISIEREC'])) {
            $param['LOCK_SAISIEREC']    = (Roles::droitsUtilisateur($pdo, 'HA_CA_MOFR')=='1' && sql::paravrac($pdo, 'PARAMGEN', 'CDERECEPTNONMODIF') != 'O')? false:true;
        }
        // @phpstan-ignore-next-line
        if (empty($param['LOCK_ADCDEFOU']) || (isset($param['LOCK_ADCDEFOU']) && $param['LOCK_ADCDEFOU'])) {
            $param['LOCK_ADCDEFOU'] = Roles::droitsUtilisateur($pdo, 'HA_CDE_ADR')=='1'? 0:1;
        }
        // @phpstan-ignore-next-line
        if (empty($param['LOCK_CTCT_FOU']) ||(isset($param['LOCK_CTCT_FOU']) && $param['LOCK_CTCT_FOU'])) {
            $param['LOCK_CTCT_FOU'] = Roles::droitsUtilisateur($pdo, 'HA_CDE_CON')=='1'? 0:1;
        }
        
        $param['ONGLET_EXPEDITION'] = $_SESSION['IS_EXPE_ACTIVE'];
        
        return $param;
    }
}
