<?php

namespace Modules\Additional\Specifique\Client123\Modules\Outil;

use Helpers\Parametres\Constante;
use Helpers\ChampsSupplementaire\ChampSupp;

class CompteComptable
{
    
    
    /*public function __construct(\PDO $pdo)
    {
        $this -> pdo = $pdo;
    }*/
    
    public static function initCompteComptable($pdo, $param)
    {
        if (\Helpers\Outil\Roles::droit($pdo, 'CDCSECTO', 'PARAMSTE')) {
            $codeSteCdc = $param['societe'];
        } else {
            $codeSteCdc = '';
        }

        $parametre = ['COD_TAB' => 'FOURNISS', 'COD_ENR' => $param['fournisseur'], 'typedico' => 'PARNCS0123'];
        $initCS = ChampSupp::getCSValueByCSOriginSpecific(array_merge($parametre, [
            'codedicoOrigine' => 'NCS_FOURNISS_COMPTE_COMPTABLE',
            'codedicoDestination' => 'NCS_FAC_LIG_COMPTE_COMPTABLE',
        ]), $pdo);

        $req = "SELECT * from CODECOMP with(nolock) where CODE_STE = '$codeSteCdc' and CODE_CDC = '".$initCS['VALEUR']."'";
        
        $requete = $pdo->prepare($req);
        $requete->execute();
        
        if ($requete->fetch()) {
            $valeur = $initCS['VALEUR'];
        } else {
            $valeur = '';
        }
        
        $result['CODE_CDC'] = $valeur;
        
        // Initialisation découlant du changement du compte comptable
        if ($result['CODE_CDC'] != '') {
            $outilCompteComptable = new \Modules\Additional\Specifique\Client123\Modules\Outil\CompteComptable();
            $result = array_merge($result, $outilCompteComptable->initApresChangementCompteComptable($pdo, [
                'CODE_CDC' => $result['CODE_CDC'],
                'societe' => $param['societe'],
                'fournisseur' => $param['fournisseur'],
                'codeEnrLigne' => $param['codeEnrLigne'],
                'NUM_LICDE' => '0'
            ]));
        }
        
        return $result;
    }
    
    public static function initApresChangementCompteComptable($pdo, $param)
    {
        $spliter = Constante::spliter;
        $codeEnrCdc = ChampSupp::autoCompletCodeEnrImputations($pdo, 'CODECOMP', $param['CODE_CDC'], $param['societe']);

        // Initialisation CS Code Taxe
        $paramsCodeTaxe = ['CODE_CDC' => $param['CODE_CDC'] , 'societe' => $param['societe'], 'fournisseur' => $param['fournisseur'], 'codeEnrCdc' => $codeEnrCdc];
        $CalculCodeTaxe = new \Modules\Additional\Specifique\Client123\Modules\Outil\CalculCodeTaxe();
        $tabCodeTaxe = $CalculCodeTaxe->calculCodeTaxe($pdo, $paramsCodeTaxe);
        if ($tabCodeTaxe) {
            $result['CS_EXTENTION'.$spliter.'FAC_LIG'.$spliter.$param['codeEnrLigne'].$spliter.$tabCodeTaxe['csNumCodeTaxe']] = $tabCodeTaxe['CodeTaxe'];
            $result['CS__'.$tabCodeTaxe['csNumCodeTaxe']] = $tabCodeTaxe['CodeTaxe'];
            if ($tabCodeTaxe['paramTva']) {
                $result['TAUX_TVA'] = $tabCodeTaxe['TAUX_TVA'];
                $result['paramTva'] = $tabCodeTaxe['paramTva'];
            }
        }

        // Initialisation du SCA

        $Dilispec = new  \Entity\Model\Dilispec($pdo);
        $numCSSca = $Dilispec->find(['TYPEDICO' => 'PARNCS0123', 'CODEDICO'=> 'NCS_CODECOMP_SCA'])['LIB01DICO'];
        $CODE_SCA = trim(ChampSupp::getValueCS('CODECOMP', $codeEnrCdc, trim($numCSSca), $pdo));
        if ($CODE_SCA != '') {
            $result['CODE_SCA'] = $CODE_SCA;
        }

        // Initialisation de la sous-famille
        if ($param['NUM_LICDE'] == '0' || $param['NUM_LICDE'] == '') {
            $paramsCodeTaxe = ['CODE_CDC' => $param['CODE_CDC'] , 'societe' => $param['societe']];
            $OutilSousFamille = new \Modules\Additional\Specifique\Client123\Modules\Outil\SousFamille();
            $result = array_merge($result, $OutilSousFamille->initSousfamilleSelonCdc($pdo, $paramsCodeTaxe));
        }
        
        return $result;
    }
}
