<?php

namespace Modules\Facture\Repository;

use Helpers\Filter\FilterTable;
use Helpers\QueryBuilder\AbsRepository;

class ExportMasse extends AbsRepository
{

    /**
     * listeFactures
     *
     * @param  array<mixed> $post
     * @param  bool $facUrl
     * @return array<mixed>
     */
    public function listeFactures(array $post, bool $facUrl = false)
    {
        $arrayReq= [];
        $where = '';
        $join = '';

        if ((isset($post['CODE_SI']) &&  $post['CODE_SI'] !='') ||
            (isset($post['CODE_CDC']) &&  $post['CODE_CDC'] !='') ||
            (isset($post['CODE_SCA']) &&  $post['CODE_SCA'] !='') ||
            (isset($post['CODE_DIR']) &&  $post['CODE_DIR'] !='') ||
            (isset($post['CODNAN']) &&  $post['CODNAN'] !='') ||
            (isset($post['CODE_CTA']) &&  $post['CODE_CTA'] !='') ||
            (isset($post['NUM_CDE_MIN']) &&  $post['NUM_CDE_MIN'] !='') ||
            (isset($post['NUM_CDE_MAX']) &&  $post['NUM_CDE_MAX'] !='')
        ) {
            $join .= " LEFT OUTER JOIN FAC_LIG l with(nolock) on l.NUM_FACT=e.NUM_FACT";
        }

        if (isset($post['CODE_DIR']) &&  $post['CODE_DIR'] !='') {
            $ste = \Helpers\Outil\Roles::droit($this->pdo, 'SISECTOR', 'PARAMSTE') ? 'l.CODE_STE' : '';
            $join .= " LEFT OUTER JOIN SECTIMPU si with(nolock) on si.CODE_STE = $ste AND si.CODE_SI = l.CODE_SI";
            $where .= " AND si.CODE_DIR = :codeDir";
            $arrayReq[':codeDir'] = $post['CODE_DIR'];
        }

        if (isset($post['CODNAN']) &&  $post['CODNAN'] !='') {
            $ste = \Helpers\Outil\Roles::droit($this->pdo, 'CDCSECTO', 'PARAMSTE') ? 'l.CODE_STE' : '';
            $join .= " LEFT OUTER JOIN CODECOMP cdc with(nolock) on cdc.CODE_STE = $ste AND cdc.CODE_CDC = l.CODE_CDC";
            $where .= " AND cdc.CODNAN = :codnan";
            $arrayReq[':codnan'] = $post['CODNAN'];
        }

        if (isset($post['CODE_CTA']) &&  $post['CODE_CTA'] !='') {
            $ste = \Helpers\Outil\Roles::droit($this->pdo, 'SCASECTO', 'PARAMSTE') ? 'l.CODE_STE' : '';
            $join .= " LEFT OUTER JOIN SSCPTANA sca with(nolock) on sca.CODE_STE = $ste AND sca.CODE_SCA = l.CODE_SCA";
            $where .= " AND sca.CODE_CTA = :codeCta";
            $arrayReq[':codeCta'] = $post['CODE_CTA'];
        }

        $req =
        "SELECT distinct e.NUM_FACT, e.CODE_STE, e.CODE_FOUR, e.DATE_FAFO, e.FIC_LAD
        FROM FAC_ENT e with (nolock)";

        $req .= $join ." WHERE e.FIC_LAD <> '' ";//and e.TYPE_URL_LAD = 'C' retiré pour pouvoir ajouter les factures dont l'image est une URL externe au message d'erreur dossier 39006

        if ($facUrl) {
            $req .= " and e.TYPE_URL_LAD = 'C' ";
        }

        if (isset($post['listeNumFact']) &&  $post['listeNumFact'] !='') {
            $req .=' AND e.NUM_FACT in ('.$post['listeNumFact'].')';
        }

        // PERIMETRE SOCIETE UTILISATEUR
        if (isset($post['Session_UsrMultiSoc']) && !$post['Session_UsrMultiSoc']) {
            $req .=
            " AND (e.CODE_STE in (
                SELECT hastutil.CODE_STE 
                FROM HASTUTIL hastutil with (nolock) 
                WHERE hastutil.CODE='".$post['Session_Login']."') 
                OR e.CODE_STE in (
                    SELECT socUsr.COD_STE 
                    FROM SOC_USR socUsr with (nolock) 
                    WHERE socUsr.COD_USR='".$post['Session_Login']."'))";
        }
        
        if (isset($post['societe']) &&  $post['societe'] !='') {
            $listeSociete = '';
            $tblSociete = explode('/', $post['societe']);
            foreach ($tblSociete as $soc) {
                $listeSociete .= '\''.$soc.'\',';
            }
            $listeSociete = substr($listeSociete, 0, -1);
            $req .=' AND e.CODE_STE in ('.$listeSociete.')';
        }

        if (isset($post['fournisseur']) &&  $post['fournisseur'] !='') {
            $listeFournisseur = '';
            $tblFournisseur = explode('/', $post['fournisseur']);
            foreach ($tblFournisseur as $fourn) {
                $listeFournisseur .= '\''.$fourn.'\',';
            }
            $listeFournisseur = substr($listeFournisseur, 0, -1);
            $req .=' AND CODE_FOUR in ('.$listeFournisseur.')';
        }

        if (isset($post['CODE_SI']) &&  $post['CODE_SI'] !='') {
            $req .=' AND l.CODE_SI = :codeSi';
            $arrayReq[':codeSi'] = $post['CODE_SI'];
        }

        if (isset($post['CODE_CDC']) &&  $post['CODE_CDC'] !='') {
            $req .=' AND l.CODE_CDC = :codeCdc';
            $arrayReq[':codeCdc'] = $post['CODE_CDC'];
        }

        if (isset($post['CODE_SCA']) &&  $post['CODE_SCA'] !='') {
            $req .=' AND l.CODE_SCA = :codeSca';
            $arrayReq[':codeSca'] = $post['CODE_SCA'];
        }

        if (isset($post['DATE_ENR_MIN']) &&  $post['DATE_ENR_MIN'] !='' &&  $post['DATE_ENR_MIN'] !='00000000') {
            $req .=' AND DATE_ENR >= :dateEnrMin';
            $arrayReq[':dateEnrMin'] = $post['DATE_ENR_MIN'];
        }

        if (isset($post['DATE_ENR_MAX']) &&  $post['DATE_ENR_MAX'] !='' &&  $post['DATE_ENR_MAX'] !='00000000') {
            $req .=' AND DATE_ENR <= :dateEnrMax';
            $arrayReq[':dateEnrMax'] = $post['DATE_ENR_MAX'];
        }

        if (isset($post['DATE_FAFO_MIN']) &&  $post['DATE_FAFO_MIN'] !='' &&  $post['DATE_FAFO_MIN'] !='00000000') {
            $req .=' AND DATE_FAFO >= :dagteFafoMin';
            $arrayReq[':dagteFafoMin'] = $post['DATE_FAFO_MIN'];
        }

        if (isset($post['DATE_FAFO_MAX']) &&  $post['DATE_FAFO_MAX'] !='' &&  $post['DATE_FAFO_MAX'] !='00000000') {
            $req .=' AND DATE_FAFO <= :dagteFafoMax';
            $arrayReq[':dagteFafoMax'] = $post['DATE_FAFO_MAX'];
        }

        if (isset($post['DATE_PER_MIN']) &&  $post['DATE_PER_MIN'] !='' &&  $post['DATE_PER_MIN'] !='00000000') {
            $req .=' AND DATE_PER >= :datePerMin';
            $arrayReq[':datePerMin'] = $post['DATE_PER_MIN'];
        }

        if (isset($post['DATE_PER_MAX']) &&  $post['DATE_PER_MAX'] !='' &&  $post['DATE_PER_MAX'] !='00000000') {
            $req .=' AND DATE_PER <= :datePerMax';
            $arrayReq[':datePerMax'] = $post['DATE_PER_MAX'];
        }

        if (isset($post['DTAE_ECH_MIN']) &&  $post['DTAE_ECH_MIN'] !='' &&  $post['DTAE_ECH_MIN'] !='00000000') {
            $req .=' AND DTAE_ECH >= :dtaeEchMin';
            $arrayReq[':dtaeEchMin'] = $post['DTAE_ECH_MIN'];
        }

        if (isset($post['DTAE_ECH_MAX']) &&  $post['DTAE_ECH_MAX'] !='' &&  $post['DTAE_ECH_MAX'] !='00000000') {
            $req .=' AND DTAE_ECH <= :dtaeEchMax';
            $arrayReq[':dtaeEchMax'] = $post['DTAE_ECH_MAX'];
        }

        if (isset($post['DATE_BAP_MIN']) &&  $post['DATE_BAP_MIN'] !='' &&  $post['DATE_BAP_MIN'] !='00000000') {
            $req .=' AND DATE_BAP >= :dateBapMin';
            $arrayReq[':dateBapMin'] = $post['DATE_BAP_MIN'];
        }

        if (isset($post['DATE_BAP_MAX']) &&  $post['DATE_BAP_MAX'] !='' &&  $post['DATE_BAP_MAX'] !='00000000') {
            $req .=' AND DATE_BAP <= :dateBapMax';
            $arrayReq[':dateBapMax'] = $post['DATE_BAP_MAX'];
        }

        if (isset($post['NUM_FACT_MIN']) &&  $post['NUM_FACT_MIN'] !='') {
            $req .=' AND e.NUM_FACT >= :numFactMin';
            $arrayReq[':numFactMin'] = $post['NUM_FACT_MIN'];
        }

        if (isset($post['NUM_FACT_MAX']) &&  $post['NUM_FACT_MAX'] !='') {
            $req .=' AND e.NUM_FACT <= :numFactMax';
            $arrayReq[':numFactMax'] = $post['NUM_FACT_MAX'];
        }

        if (isset($post['NUM_CDE_MIN']) &&  $post['NUM_CDE_MIN'] !='') {
            $req .=' AND l.NUM_CDE >= :numCdeMin';
            $arrayReq[':numCdeMin'] = $post['NUM_CDE_MIN'];
        }

        if (isset($post['NUM_CDE_MAX']) &&  $post['NUM_CDE_MAX'] !='') {
            $req .=' AND l.NUM_CDE <= :numCdeMax';
            $arrayReq[':numCdeMax'] = $post['NUM_CDE_MAX'];
        }

        $req .= $where .' ORDER BY NUM_FACT';
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function listeFacturesSansImage($listeNumFact)
    {
        $req =
        "SELECT distinct e.NUM_FACT
        FROM FAC_ENT e with (nolock)
        WHERE e.FIC_LAD = '' AND e.NUM_FACT in (".$listeNumFact.")";

        $req .= ' ORDER BY NUM_FACT';
        
        $facture = $this->pdo->prepare($req);
        $facture->execute();
        return $facture->fetchAll($this->pdo::FETCH_ASSOC);
    }
}
