<?php

namespace Modules\Facture\Repository;

use Helpers\Filter\FilterTable;
use Helpers\Outil\Roles;
use Helpers\QueryBuilder\AbsRepository;
use Modules\GlobalApplication\Repository\LicenceOxalys;

class ListesFacture extends AbsRepository
{
    
    public function listeEntete($post)
    {
        $req =
        "SELECT f.NUM_FACT, f.TYPE, f.FACT_FOU, f.CODE_STE, f.CODE_FOUR, f.FAC_CBY, f.NUM_CDE, 
        f.DATE_ENR, f.DTAE_ECH, f.DATE_BAP, f.DTE_DERNREG, f.TOTAL_HT, f.TOTAL_TTC, f.MT_TOTREG, 
        f.ENT_VALID, f.AFF_VALID, f.ENT_COMPT, f.AFF_COMPT, f.BAP_COMPT, 
        f.ATT_SIG, f.AFF_TMP, f.LITIGE, f.BON_A_PAY, four.RS 
        FROM FAC_ENT f WITH (NOLOCK)
        INNER JOIN FOURNISS four WITH (NOLOCK) ON f.CODE_FOUR = four.CODE
        LEFT JOIN SOCIETE soc WITH (NOLOCK) ON soc.CODE_STE = f.CODE_STE
        WHERE 1=1";
        $arrayReq= [];
        
        if (!$_SESSION['Session_UsrMultiSoc']) {
            $req .=
            " AND (f.CODE_STE IN (
                SELECT hastutil.CODE_STE 
                FROM HASTUTIL hastutil WITH (NOLOCK) 
                WHERE hastutil.CODE='".$_SESSION['Session_Login']."') OR f.CODE_STE IN (
                    SELECT socUsr.COD_STE FROM SOC_USR socUsr WITH (NOLOCK) WHERE socUsr.COD_USR='".$_SESSION['Session_Login']."'))";
        }
        
        // Filtre sur les factures en fonction du niveau de confidentialité des documents
        if ((new LicenceOxalys($this->pdo))->accordConfActive()) {
            $req .=
            ' AND not exists (
                SELECT caEntet.NO_CDE
                FROM CA_ENTET caEntet WITH (NOLOCK)
                INNER JOIN FAC_LIG facLig3 WITH (NOLOCK) ON facLig3.NUM_FACT=f.NUM_FACT AND facLig3.NUM_CDE=caEntet.NO_CDE
                INNER JOIN TYPDOC typdoc WITH (NOLOCK) ON caEntet.TYP_DOC=typdoc.TYPE AND typdoc.NIV_CONF>'.$_SESSION['NiveauConfidentialite'].')';
        }

        /***  Filtres avancés ***/
        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
        $resultFiltreAvance = $filtreAvance->generateSql(1);
        if ($resultFiltreAvance != ' AND ') {
            $req .= $resultFiltreAvance;
        }
        $filtreAvance->pushArray($arrayReq);
        /***************/
                
        /***  Filtres et tri tableau ***/
        $whereArray = FilterTable::splitFilter($post, 0, 0, 1);
        if (is_array($whereArray) && isset($whereArray[0]) && $whereArray[0]!= '') {
            $filtre = str_replace('SOCIETE', 'f.CODE_STE', $whereArray[0]);
            $req .= $filtre;
            if (is_array($whereArray[1]) && count($whereArray[1]) > 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }
        $req .= $whereArray['tri'];
        
        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $post, $arrayReq, $this->pdo);
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function ligneListeEntete($id)
    {
        $req =
        "SELECT f.NUM_CDE, f.NUM_LICDE, f.NUM_BR, f.NUM_LIBR, f.DESIGNAT, f.NUM_EXP, f.NUM_TERME,
        f.QUANTITE, f.PU_REMISE, f.MONTANT, f.CODE_SI, f.CODE_CDC, f.CODE_SCA,
        f.CODE_SFAM, f.TAUX_TVA, f.TYPE_TVA, m.CONTMEMO
        from FAC_LIG f WITH (NOLOCK)
        LEFT JOIN CA_LIGNE c WITH (NOLOCK) ON f.NUM_LICDE=c.NO_CDE and f.NUM_LICDE=c.NO_PO
        LEFT JOIN MEMOS m WITH (NOLOCK) ON c.NM_DCOMP=m.NODEMEMO
        WHERE f.NUM_FACT = :id";
        $arrayReq[':id'] = $id;

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function listeLigne($post)
    {
        $siSecto = \Helpers\Outil\Roles::droit($this->pdo, 'SISECTOR', 'PARAMSTE');

        $req =
        "SELECT f.NUM_FACT, f.TYPE, lig.NUM_LIG, lig.NUM_CDE, lig.NUM_LICDE, lig.NUM_BR, lig.NUM_LIBR, lig.NUM_EXP, 
        lig.NUM_TERME, lig.CODE_SFAM, lig.QUANTITE, lig.PU_REMISE, lig.MONTANT, lig.AFF_TMP, lig.DESIGNAT
        FROM FAC_ENT f WITH (NOLOCK)
        INNER JOIN FAC_LIG lig WITH (NOLOCK) ON f.NUM_FACT = lig.NUM_FACT
        LEFT JOIN CODGEST cg WITH (NOLOCK) ON lig.ID_CG = cg.CODE_GES
                and cg.CODE_STE = ".($siSecto? 'f.CODE_STE':"''")." 
                and cg.CODE_SI = lig.CODE_SI 
        WHERE 1=1";
        $arrayReq= [];
        
        if (!$_SESSION['Session_UsrMultiSoc']) {
            $req .=
            " AND (lig.CODE_STE IN (
                SELECT hastutil.CODE_STE 
                FROM HASTUTIL hastutil WITH (NOLOCK) 
                WHERE hastutil.CODE='".$_SESSION['Session_Login']."') OR f.CODE_STE IN (
                    SELECT socUsr.COD_STE FROM SOC_USR socUsr WITH (NOLOCK) WHERE socUsr.COD_USR='".$_SESSION['Session_Login']."'))";
        }

        if (isset($post['societe']) && $post['societe'] != '') {
            $req .= " AND lig.CODE_STE = :CODE_STE ";
            $arrayReq[':CODE_STE'] = $post['societe'];
        }

        if (isset($post['archive']) &&  $post['archive'] !='') {
            if ($post['archive']=='oui') {
                $req .=' AND f.FLG_ARCHIVE = '.\Helpers\Outil\Sql::setBoolSql(1);
            } elseif ($post['archive']=='non') {
                $req .=' AND f.FLG_ARCHIVE = '.\Helpers\Outil\Sql::setBoolSql(0);
            }
        }
        
        if (isset($post['CODE_SI']) &&  $post['CODE_SI'] !='') {
            $req .=' AND lig.CODE_SI = \''.$post['CODE_SI'] .'\' ';
        }
        
        if (isset($post['CODE_DIR']) &&  $post['CODE_DIR'] !='') {
            $req .=
            ' AND lig.CODE_SI IN (
                    SELECT sectimpu.CODE_SI 
                    FROM SECTIMPU sectimpu WITH (NOLOCK) 
                    WHERE sectimpu.CODE_DIR = \''.$post['CODE_DIR'] .'\') ';
        }
        
        if (isset($post['CODE_CDC']) &&  $post['CODE_CDC'] !='') {
            $req .=' AND lig.CODE_CDC = \''.$post['CODE_CDC'] .'\' ';
        }
        
        if (isset($post['CODNAN']) &&  $post['CODNAN'] !='') {
            $req .=
            ' AND lig.CODE_CDC IN (
                    SELECT codecomp.CODE_CDC 
                    FROM CODECOMP codecomp WITH (NOLOCK) 
                    WHERE codecomp.CODNAN = \''.$post['CODNAN'] .'\') ';
        }
        
        if (isset($post['CODE_SCA']) &&  $post['CODE_SCA'] !='') {
            $req .=' AND lig.CODE_SCA = \''.$post['CODE_SCA'] .'\' ';
        }
        
        if (isset($post['CODE_CTA']) &&  $post['CODE_CTA'] !='') {
            $req .=
            ' AND lig.CODE_SCA IN (
                    SELECT sscptana.CODE_SCA 
                    FROM SSCPTANA sscptana WITH (NOLOCK) 
                    WHERE sscptana.CODE_CTA = \''.$post['CODE_CTA'] .'\')';
        }
        
        if (isset($post['ID_CG']) &&  $post['ID_CG'] !='') {
            $req .=' AND lig.ID_CG = \''.$post['ID_CG'] .'\' ';
        }
        
        if (isset($post['statut']) &&  $post['statut'] !='') {
            switch ($post['statut']) {
                case 'AffectationsASaisir':
                    $req .= ' AND f.AFF_ENREG='.\Helpers\Outil\Sql::setBoolSql(0);
                    break;
                case 'AffectationsAValider':
                    $req .= ' AND (f.AFF_ENREG='.\Helpers\Outil\Sql::setBoolSql(1).' AND f.AFF_VALID='.\Helpers\Outil\Sql::setBoolSql(0).')';
                    break;
                case 'AttenteBAP':
                    if (\Helpers\Outil\Roles::droit($this->pdo, 'SIGN_BAP', 'PARAMINIFA')) {
                        $req .= ' AND ( f.ATT_SIG='.\Helpers\Outil\Sql::setBoolSql(1).' AND f.REFUSEE='.\Helpers\Outil\Sql::setBoolSql(0).' AND f.LITIGE ='.\Helpers\Outil\Sql::setBoolSql(0).')';
                    } else {
                        $req .= ' AND ( f.ENT_VALID='.\Helpers\Outil\Sql::setBoolSql(1).' AND f.BON_A_PAY='.\Helpers\Outil\Sql::setBoolSql(0).' AND f.LITIGE ='.\Helpers\Outil\Sql::setBoolSql(0).')';
                    }
                    break;
                case 'EnLitige':
                    $req .= ' AND f.LITIGE ='.\Helpers\Outil\Sql::setBoolSql(1);
                    break;
                case 'EnAttenteSignature':
                    $req .= ' AND f.ATT_SIG ='.\Helpers\Outil\Sql::setBoolSql(1);
                    break;
                case 'BAPSignes':
                    $req .= ' AND f.BON_A_PAY ='.\Helpers\Outil\Sql::setBoolSql(1);
                    break;
                case 'BAPRefuses':
                    $req .= ' AND f.REFUSEE ='.\Helpers\Outil\Sql::setBoolSql(1);
                    break;
                case 'PreAffectees':
                    $req .= ' AND f.AFF_TMP ='.\Helpers\Outil\Sql::setBoolSql(1);
                    break;
                case 'PartiellementReglees':
                    $req .= ' AND (ABS(f.MT_TOTREG) <> 0 AND ABS(f.MT_TOTREG) < ABS(f.TOTAL_TTC))';
                    break;
                case 'TotalementReglees':
                    $req .= ' AND (ABS(f.MT_TOTREG) >= ABS(f.TOTAL_TTC) AND ABS(f.TOTAL_TTC)<>0)';
                    break;
                case 'Rejet':
                    $req .= '  AND f.REJET_COMPTA = '.\Helpers\Outil\Sql::setBoolSql(1);
                    break;
                case 'NonReglees':
                    $req .= " AND f.DTE_DERNREG = '00000000'";
                    break;
                case 'ATransfererEnCompta':
                    $req .= ' AND ((f.ENT_VALID='.\Helpers\Outil\Sql::setBoolSql(1).' AND f.ENT_COMPT='.\Helpers\Outil\Sql::setBoolSql(0).') OR (f.AFF_VALID='.\Helpers\Outil\Sql::setBoolSql(1).' AND f.AFF_COMPT='.\Helpers\Outil\Sql::setBoolSql(0).') OR (f.BON_A_PAY='.\Helpers\Outil\Sql::setBoolSql(1).' and f.BAP_COMPT='.\Helpers\Outil\Sql::setBoolSql(0).'))';
                    break;
                case 'EnteteCompta':
                    $req .=" AND f.ENT_COMPT=0x54";
                    break;
                case 'affectationsCompta':
                    $req .=" AND f.AFF_COMPT=0x54";
                    break;
                case 'EnteteAffectCompta':
                    $req .=" AND f.ENT_COMPT=0x54 AND f.AFF_COMPT=0x54";
                    break;
                case 'BAPenCompta':
                    $req .=" AND f.BAP_COMPT=0x54";
                    break;
            }
        }
        
        if (isset($post['NUM_CDE']) &&  $post['NUM_CDE'] !='') {
            $numCde = addcslashes($post['NUM_CDE'], '%_');
            $req .= ' AND lig.NUM_CDE=\''.$numCde.'\' ';
        }
        
        if (isset($post['NUM_BR']) &&  $post['NUM_BR'] !='') {
            $numBr = addcslashes($post['NUM_BR'], '%_');
            $req .=' AND lig.NUM_BR=\''.$numBr.'\' ';
        }
        
        if (isset($post['motCle']) &&  $post['motCle'] !='') {
            $tblMotCle = explode(' ', $post['motCle']);
            $reqMotCle = '';
            foreach ($tblMotCle as $value) {
                $value = addcslashes($value, '%_');
                $reqMotCle .=' AND UPPER(DESIGNAT) LIKE \'%'.strtoupper($value).'%\'';
            }
            $req .= ' AND f.NUM_FACT IN (SELECT DISTINCT (NUM_FACT) FROM FAC_LIG WITH (NOLOCK) WHERE '.substr($reqMotCle, 5).')';
        }

        // Filtre sur les factures en fonction du niveau de confidentialité des documents
        if ((new LicenceOxalys($this->pdo))->accordConfActive()) {
            $req .=
            ' AND not exists (
                SELECT caEntet.NO_CDE
                FROM CA_ENTET caEntet WITH (NOLOCK)
                INNER JOIN FAC_LIG facLig3 WITH (NOLOCK) ON facLig3.NUM_FACT=f.NUM_FACT AND facLig3.NUM_CDE=caEntet.NO_CDE
                INNER JOIN TYPDOC typdoc WITH (NOLOCK) ON caEntet.TYP_DOC=typdoc.TYPE AND typdoc.NIV_CONF>'.$_SESSION['NiveauConfidentialite'].')';
        }

        /***  Filtres avancés ***/
        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
        $resultFiltreAvance = $filtreAvance->generateSql(1);
        if ($resultFiltreAvance != ' AND ') {
            $req .= $resultFiltreAvance;
        }
        $filtreAvance->pushArray($arrayReq);
        /***************/
                
        /***  Filtres et tri tableau ***/
        $whereArray = FilterTable::splitFilter($post, 0, 0, 1);
        if (is_array($whereArray) && isset($whereArray[0]) && $whereArray[0]!= '') {
            $filtre = (string) $whereArray[0];
            $filtre = str_replace('NUMFACT', 'f.NUM_FACT', $filtre);
            $filtre = str_replace('TYPE', 'f.TYPE', $filtre);
            $filtre = str_replace('NUM_CDE', 'lig.NUM_CDE', $filtre);
            $req .= $filtre;
            if (is_array($whereArray[1]) && count($whereArray[1]) > 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }
        $req .= $whereArray['tri'];

        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $post, $arrayReq, $this->pdo);
       
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function ligneListeLigne($id, $ligne)
    {
        $req =
        "SELECT  f.DESIGNAT, 
        f.CODE_SI, f.CODE_CDC, f.CODE_SCA, f.CODE_SCA, f.ID_CG,
        f.TAUX_TVA, f.TYPE_TVA, 
        ca.ETAT_CDE, ca.ETAT_FIN,
        m.CONTMEMO
        from FAC_LIG f WITH (NOLOCK)
        LEFT JOIN CA_ENTET ca WITH (NOLOCK) ON f.NUM_CDE=ca.NO_CDE
        LEFT JOIN CA_LIGNE c WITH (NOLOCK) ON f.NUM_CDE=c.NO_CDE and f.NUM_LICDE=c.NO_PO
        LEFT JOIN MEMOS m WITH (NOLOCK) ON c.NM_DCOMP=m.NODEMEMO
        WHERE f.NUM_FACT = :id and f.NUM_LIG = :ligne";
        $arrayReq[':id'] = $id;
        $arrayReq[':ligne'] = $ligne;

        $ligneFacture = $this->pdo->prepare($req);
        $ligneFacture->execute($arrayReq);
        return $ligneFacture->fetch();
    }
    
    /**
     * @param array<mixed> $post
     * @return string[][]
     */
    public function listeFactFour($post)
    {
        $savePost = $post;

        $req =
            "SELECT f.NUM_FACT, f.TYPE, f.FACT_FOU, f.CODE_STE, f.CODE_FOUR, four.RS 
            FROM FAC_ENT f WITH (NOLOCK)
            JOIN FOURNISS four WITH (NOLOCK) ON f.CODE_FOUR = four.CODE
            LEFT JOIN SOCIETE soc WITH (NOLOCK) ON soc.CODE_STE = f.CODE_STE
            WHERE 1=1";
        $arrayReq= [];
        
        $req .= Roles::getClausePerimetreSociete('f.CODE_STE', $arrayReq);
        
        // Filtre sur les factures en fonction du niveau de confidentialité des documents
        if ((new LicenceOxalys($this->pdo))->accordConfActive()) {
            $req .=
                ' AND not exists (
                    SELECT caEntet.NO_CDE
                    FROM CA_ENTET caEntet WITH (NOLOCK)
                    JOIN FAC_LIG facLig3 WITH (NOLOCK) 
                        ON facLig3.NUM_FACT = f.NUM_FACT 
                        AND facLig3.NUM_CDE = caEntet.NO_CDE
                    JOIN TYPDOC typdoc WITH (NOLOCK) 
                        ON typdoc.TYPE = caEntet.TYP_DOC
                        AND typdoc.NIV_CONF > :nivConf
                )';
            $arrayReq[':nivConf'] = $_SESSION['NiveauConfidentialite'];
        }

        /***  Filtres avancés ***/
        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
        $resultFiltreAvance = $filtreAvance->generateSql(1);
        if ($resultFiltreAvance != ' AND ') {
            $req .= $resultFiltreAvance;
        }
        $filtreAvance->pushArray($arrayReq);
        /***************/
        if (isset($post['filter_egal_SOCIETEdebut-----finRAIS_SOC'])) {
            $post['filter_f.CODE_STEdebut-----finRAIS_SOC'] = $post['filter_egal_SOCIETEdebut-----finRAIS_SOC'];
            unset($post['filter_egal_SOCIETEdebut-----finRAIS_SOC']);
        }

        if (isset($post['filter_CODE_FOURdebut-----finRS'])) {
            $post['filter_f.CODE_FOURdebut-----finRS'] = $post['filter_CODE_FOURdebut-----finRS'];
            unset($post['filter_CODE_FOURdebut-----finRS']);
        }

        /***  Filtres et tri tableau ***/
        $whereArray = FilterTable::splitFilter($post, 0, 0, 1);
        if (is_array($whereArray) && isset($whereArray[0]) && $whereArray[0]!= '') {
            $req .= $whereArray[0];
            if (is_array($whereArray[1]) && count($whereArray[1]) > 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }
        $req .= $whereArray['tri'];
        
        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $savePost, $arrayReq, $this->pdo);

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function ligneListeEvenements($id)
    {
        $param = [
            ':id' => $id,
        ];
        $req = "SELECT 
            e.NUM_FACT, 
            e.NUM_ORD, 
            e.DATE_CRE, e.HEURE_CRE,
            e.CODE_EVE, 
            e.UTIL, 
            e.SIGNAT, 
            e.GERE_HAP,
            " . $this->selectTrad('SUIFADAT.LIBELLE') . ",
            Utili.NOM as Utili_NOM,
            Signataire.NOM as Signataire_NOM
        FROM FA_EVENT e WITH (NOLOCK)
        LEFT JOIN SUIFADAT WITH (NOLOCK) ON SUIFADAT.CODE_SFAC = e.CODE_EVE
        LEFT JOIN HASTUTIL Utili  WITH (NOLOCK) ON Utili.CODE = e.UTIL
        LEFT JOIN HASTUTIL Signataire  WITH (NOLOCK) ON Signataire.CODE = e.SIGNAT
        " . $this->joinTrad('SUIFADAT.LIBELLE', $param) . "
        WHERE e.NUM_FACT = :id 
        ORDER BY e.DATE_CRE desc, e.HEURE_CRE desc";

        return $this->fetchAssocAll($req, $param);
    }
    
    public function listeGlobaleEvenements($post = [])
    {
        $postSave = $post;
        $arrayReq = [];
        $req = "SELECT 
            e.NUM_FACT, 
            e.DATE_CRE, 
            e.HEURE_CRE, 
            e.UTIL, 
            Utili.NOM as Utili_NOM,
            e.CODE_EVE, 
            " . $this->selectTrad('SUIFADAT.LIBELLE') . ",
            e.SIGNAT, 
            Signataire.NOM as Signataire_NOM,
            f.CODE_STE, 
            m.CONTMEMO 
        FROM FA_EVENT e WITH (NOLOCK)
        LEFT JOIN FAC_ENT f WITH (NOLOCK) ON e.NUM_FACT=f.NUM_FACT
        LEFT JOIN MEMOS m WITH (NOLOCK) ON m.NODEMEMO = e.NM_COMME
        LEFT JOIN SUIFADAT WITH (NOLOCK) ON SUIFADAT.CODE_SFAC = e.CODE_EVE
        LEFT JOIN HASTUTIL Utili WITH (NOLOCK) ON Utili.CODE = e.UTIL
        LEFT JOIN HASTUTIL Signataire WITH (NOLOCK) ON Signataire.CODE = e.SIGNAT
        " . $this->joinTrad('SUIFADAT.LIBELLE', $arrayReq) . "
        LEFT JOIN SOCIETE soc WITH (NOLOCK) ON soc.CODE_STE = f.CODE_STE
        LEFT JOIN HASTUTIL util WITH (NOLOCK) ON util.CODE = e.UTIL
        LEFT JOIN HASTUTIL signat WITH (NOLOCK) ON signat.CODE = e.SIGNAT
        WHERE 1=1";

        
        
        if (!$_SESSION['Session_UsrMultiSoc']) {
            $req .=
            " AND (f.CODE_STE IN (
                SELECT hastutil.CODE_STE 
                FROM HASTUTIL hastutil WITH (NOLOCK) 
                WHERE hastutil.CODE='".$_SESSION['Session_Login']."'
            ) OR f.CODE_STE IN (
                SELECT socUsr.COD_STE FROM SOC_USR socUsr WITH (NOLOCK) 
                WHERE socUsr.COD_USR='".$_SESSION['Session_Login']."')
            )";
        }
        
        // Filtre sur les factures en fonction du niveau de confidentialité des documents
        if ((new LicenceOxalys($this->pdo))->accordConfActive()) {
            $req .=
            ' AND not exists (
                SELECT caEntet.NO_CDE
                FROM CA_ENTET caEntet WITH (NOLOCK)
                INNER JOIN FAC_LIG facLig3 WITH (NOLOCK) ON facLig3.NUM_FACT=f.NUM_FACT AND facLig3.NUM_CDE=caEntet.NO_CDE
                INNER JOIN TYPDOC typdoc WITH (NOLOCK) ON caEntet.TYP_DOC=typdoc.TYPE AND typdoc.NIV_CONF>'.$_SESSION['NiveauConfidentialite'].')';
        }
        

        /***  Filtres avancés ***/
        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
        $resultFiltreAvance = $filtreAvance->generateSql(1);
        if ($resultFiltreAvance != ' AND ') {
            $req .= $resultFiltreAvance;
        }
        $filtreAvance->pushArray($arrayReq);
        /***************/
        
        /***  Filtres et tri tableau ***/
        if (isset($post['filter_SOCIETEdebut-----finRAIS_SOC'])) {
            $post['filter_soc.CODE_STEdebut-----finsoc.RAIS_SOC'] = $post['filter_SOCIETEdebut-----finRAIS_SOC'];
            unset($post['filter_SOCIETEdebut-----finRAIS_SOC']);
        }
        
        $whereArray = FilterTable::splitFilter($post, 0, 0, 1);

        if (is_array($whereArray) && isset($whereArray[0]) && $whereArray[0]!= '') {
            $filtre = (string) $whereArray[0];
            $filtre = str_replace('[NUMFACT]', 'e.NUM_FACT', $filtre);
            $filtre = str_replace('[NOMUTILISATEUR]', 'util.NOM', $filtre);
            $filtre = str_replace('[UTILISATEUR]', 'e.UTIL', $filtre);
            $filtre = str_replace('[NOMSIGNATAIRE]', 'signat.NOM', $filtre);
            $filtre = str_replace('[SIGNATAIRE]', 'e.SIGNAT', $filtre);
            $req .= $filtre;
            if (is_array($whereArray[1]) && count($whereArray[1]) > 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }
        $req .= $whereArray['tri']??'';
        
        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $postSave, $arrayReq, $this->pdo);

        return $this->fetchAssocAll($req, $arrayReq);
    }
}
