<?php

namespace Modules\GlobalApplication\Outil;

use Helpers\Outil\Sql;

class AxesImputations
{
    /****
     *  TODO A SUPPRIMER
        / ! \ NE PAS UTILISER CETTE CLASS MAIS InitAxesImputation ! elle est plus optimisé pour être utilisé dans des boucles !
    */
    /**
     * @deprecated use toBool instead
     */
    public static function initAxesImputations($pdo, $param)
    {
        $initAxe = [];
        $HastutilEntity = new \Entity\Model\Hastutil($pdo);
        $Hastutil = [];

        $globalParam = new \Modules\GlobalApplication\Repository\Param($pdo);
        $numLot = $globalParam->getNumLot()['NUM_LOT'];

        switch ($param['module']) {
            case 'DA':
                $DaEntet = new \Entity\Model\DaEntet($pdo);
                $entete = $DaEntet->find($param['no_doc']);

                $Hastutil = $HastutilEntity->find($entete['NO_DEM']);
                $INIT_IMP = Sql::paravrac($pdo, 'INIT_IMPD', $entete['TYP_DOC'])!=''? Sql::paravrac($pdo, 'INIT_IMPD', $entete['TYP_DOC']) : Sql::paravrac($pdo, 'INIT_IMPDA', '');
                break;

            case 'CDE':
                $CaEntet = new \Entity\Model\CaEntet($pdo);
                $entete = $CaEntet->find($param['no_doc']);
                
                $INIT_IMP = Sql::paravrac($pdo, 'INIT_IMPC', $entete['TYP_DOC'])!=''? Sql::paravrac($pdo, 'INIT_IMPC', $entete['TYP_DOC']) : Sql::paravrac($pdo, 'INIT_IMPCA', '');
                $Hastutil = $HastutilEntity->find($entete['DEMANDEUR']);
                break;
        }

        $tblPriorite = explode('|', $INIT_IMP);
        foreach ($tblPriorite as $key => $value) {
            switch ($key) {
                case 0:
                    $axe = 'SI';
                    $keyLigne = $keyEntete = 'SECTIMP';
                    $keyAxe = 'CODE_SI';
                    $entityArt = new \Entity\Model\SiSteArt($pdo);
                    $entitySsFam = new \Entity\Model\SiSteSFa($pdo);
                    $entityFam = new \Entity\Model\SiSteFam($pdo);
                    $entityAxe = new \Entity\Model\Sectimpu($pdo);
                    $Codgest = new \Entity\Model\Codgest($pdo);
                    $codeSte = \Helpers\Outil\Roles::droit($pdo, 'SISECTOR', 'PARAMSTE') ? $entete['CODE_STE'] : '';
                    $initAxe['CODGES'] = $initAxe['CODGES_LIBELLE'] = '';
                    break;
                case 1:
                    $axe = 'CDC';
                    $keyEntete = 'CODECOMP';
                    $keyLigne = 'CODE_COMP';
                    $keyAxe = 'CODE_CDC';
                    $entityArt = new \Entity\Model\CdcSteAr($pdo);
                    $entitySsFam = new \Entity\Model\CdcSteSF($pdo);
                    $entityFam = new \Entity\Model\CdcSteFa($pdo);
                    $entityAxe = new \Entity\Model\Codecomp($pdo);
                    $codeSte = \Helpers\Outil\Roles::droit($pdo, 'CDCSECTO', 'PARAMSTE') ? $entete['CODE_STE'] : '';
                    break;
                case 2:
                    $axe = 'SCA';
                    $keyLigne = $keyEntete = 'SSCPTEANA';
                    $keyAxe = 'CODE_SCA';
                    $entityArt = new \Entity\Model\ScaSteAr($pdo);
                    $entitySsFam = new \Entity\Model\ScaSteSF($pdo);
                    $entityFam = new \Entity\Model\StaSteFa($pdo);
                    $entityAxe = new \Entity\Model\Sscptana($pdo);
                    $codeSte = \Helpers\Outil\Roles::droit($pdo, 'SCASECTO', 'PARAMSTE') ? $entete['CODE_STE'] : '';
                    break;
            }
            

            $prioriteAxe = explode(';', $value);

            $initAxe[$keyLigne] = $initAxe[$keyLigne.'_LIBELLE'] = '';
            foreach ($prioriteAxe as $keyPriorite => $codePriorite) {
                switch ($codePriorite) {
                    case 'ART':
                        if (($param['CODE_ART'] ?? '') != '') {
                            $initAxe[$keyLigne] = $entityArt->find(['CODE_ART' => $param['CODE_ART'], 'CODE_STE' => $codeSte])[$axe] ?? '';
                            
                            if ($initAxe[$keyLigne] == '' && ($param['SSFAM'] ?? '') != '') {
                                $initAxe[$keyLigne] = $entitySsFam->find(['SFAMILL' => $param['SSFAM'], 'CODE_STE' => $codeSte])[$axe] ?? '';
                                
                                if ($initAxe[$keyLigne] == '') {
                                    $SousFamille = new \Entity\Model\SousFamille($pdo);
                                    $initAxe[$keyLigne] = $entityFam->find(['FAMILLE' => $SousFamille->find(['CODE_SFA' => $param['SSFAM']])['CODE_FAM'], 'CODE_STE' => $codeSte])[$axe] ?? '';
                                }
                            }
                        }
                        break;

                    case 'ENT':
                        $initAxe[$keyLigne] = $entete[$keyEntete];
                        if ($axe == 'SI'
                            && $keyPriorite == 0
                            && $initAxe[$keyLigne]!='' && $initAxe[$keyLigne] == $entete[$keyEntete]
                            && Sql::boolSql($entityAxe->find([$keyAxe => $initAxe[$keyLigne], 'CODE_STE' => $codeSte])['SICODG'])
                        ) {
                            $initAxe['CODGES'] = $entete['CODGES'];
                            $initAxe['CODGES_LIBELLE'] = $Codgest->find([
                                'CODE_AFF' => $initAxe['CODGES'],
                                'CODE_SI' => $initAxe[$keyLigne],
                                'CODE_STE' => $codeSte,
                                'NUM_LOT' => intval($numLot),
                            ])['LIB_GES'] ?? '';
                        }
                        break;

                    case 'DEM':
                        $initAxe[$keyLigne] = $Hastutil[$axe] ?? '';
                        break;
                    
                    case 'DA':
                        // TODO
                        break;
                }
                if ($initAxe[$keyLigne] != '') {
                    $initAxe[$keyLigne.'_LIBELLE'] = $entityAxe->find([$keyAxe => $initAxe[$keyLigne], 'CODE_STE' => $codeSte])['LIBELLE'];
                    break;
                }
                if ($codePriorite == '') {
                    break;
                }
            }
        }
        
        return $initAxe;
    }
}
