<?php

namespace Modules\Additional\Specifique\Client112\Modules\Facture\Repository;

//use Helpers\Filter\FilterTable;

class Gestion
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
   
    public static function selectRequeteGestionFacture()
    {
        return "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.DATE_FAFO, f.DATE_BAP, f.DTE_DERNREG, f.DTAE_ECH, f.LIB_COMPT,
        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.REJET_COMPTA, f.REFUSEE,
        f.BON_A_PAY, f.FAC_LAD, f.FIC_LAD, f.TYPE_URL_LAD, four.RS, soc.RAIS_SOC, fabnote.NM_BNOTE, memosBlocNote.CONTMEMO blocNote,
        (case when exists(SELECT * FROM MEMOS with (nolock) WHERE NODEMEMO = fabnote.NM_BNOTE and CONTMEMO <>'' ) then 1 else null end) hasBlocNote,
        (SELECT TOP 1 count(*) FROM FAC_LIG facLig1 with (nolock) WHERE f.NUM_FACT=facLig1.NUM_FACT) nbLignesFacture, 
        (SELECT TOP 1 count(*) FROM FAC_LIG facLig2 with (nolock) WHERE f.NUM_FACT=facLig2.NUM_FACT AND facLig2.NUM_CDE=0) ligneManuelle,   
        (select TOP 1 count(*) from FAC_LIG facLigAnomalie with (nolock) where f.NUM_FACT=facLigAnomalie.NUM_FACT AND facLigAnomalie.LIG_ERR = ".\Helpers\Outil\Sql::setBoolSql(1).") ligneAnomalie,
        (SELECT TOP 1 count(*) FROM FAC_CDELAD facCdelad with (nolock) WHERE f.NUM_FACT=facCdelad.NUM_FACT) cdeLad,  
        (SELECT TOP 1 COUNT(*) FROM FA_EVENT evt with (nolock) WHERE evt.CODE_EVE = 'MAI' and f.NUM_FACT=evt.NUM_FACT) EVNT,
        (SELECT TOP 1 count(*) FROM FAC_PIJO facpijo with (nolock) WHERE f.NUM_FACT=facpijo.NO_FAC) nbPJ";
    }
    
    public function getLibelleMail($codeSte)
    {
        $req=" select * from PARAMDICO with(nolock) where TYPEDICO = 'ALBIOMPARAMSTE' AND ID1 = :codeSte";
        
        $arrayReq = [ ':codeSte' => $codeSte
        ];

        $param = $this->pdo->prepare($req);
        $param->execute($arrayReq);
        
        
        return $param->fetch();
    }

    public function getMailByCodeUser($code)
    {
        $req="SELECT EMAIL FROM HASTUTIL with(nolock) WHERE CODE = :code";
        
        $arrayReq = [ ':code' => $code];

        $param = $this->pdo->prepare($req);
        $param->execute($arrayReq);
        
        
        return $param->fetch();
    }

    public static function postRegul(&$req, $post)
    {
        if (isset($post['CDE_REGUL']) &&  $post['CDE_REGUL'] !='') {
            if ($post['CDE_REGUL'] == 'FAC_CDE') {
                $req .='  and EVNT > 0 ';
            }
            if ($post['CDE_REGUL'] == 'FAC_NO_CDE') {
                $req .='  and EVNT =0 ';
            }
        }
    }
}
