<?php

namespace Modules\Facture\Repository;

use Entity\Model\Enum\EFacDematFlowStatus;

class Gestion
{



    public function __construct()
    {
    }

    /**
     * @see Modules\Facture\Repository\GestionFacture::fromRequeteGestionFacture()
     */
    public static function selectRequeteGestionFacture()
    {
        return "SELECT d.COD_ENR as docRattExist, 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.ACOMPTE,
        f.TOTAL_HT, f.TOTAL_TTC, f.MT_TOTREG, f.ENT_VALID, f.AFF_ENREG, f.AFF_VALID, f.DEVISE,
        f.REFUSEE_FE /* Refus facture */,
        f.ANNULE,
        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, f.ISSUE_FE, 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,
        (CASE 
            WHEN f.TYPE <> 'A' 
            THEN 
                CASE 
                    WHEN EXISTS (SELECT NUM_FACT FROM FAC_LIG WITH (NOLOCK) WHERE REF_FACT = f.NUM_FACT)
                    THEN 1
                    ELSE NULL
                END
            ELSE NULL
        END) hasAvoir,
        (SELECT count(*) FROM FAC_LIG facLig1 with (nolock) WHERE f.NUM_FACT=facLig1.NUM_FACT) nbLignesFacture, 
        (SELECT count(*) FROM FAC_LIG facLig2 with (nolock) WHERE f.NUM_FACT=facLig2.NUM_FACT AND facLig2.NUM_CDE=0) ligneManuelle,   
        (CASE   WHEN (    
                    (SELECT count(*) FROM FAC_LIG facLig1 with (nolock) WHERE f.NUM_FACT=facLig1.NUM_FACT) > 0
                    AND
                    ((SELECT count(*) FROM FAC_LIG facLig1 with (nolock) WHERE f.NUM_FACT=facLig1.NUM_FACT) = 
                    (SELECT count(*) FROM FAC_LIG facLig2 with (nolock) WHERE f.NUM_FACT=facLig2.NUM_FACT AND facLig2.NUM_CDE=0) ) 
                    AND
                    (SELECT NUM_CDE FROM FAC_ENT facEnt2 with (nolock) WHERE f.NUM_FACT=facEnt2.NUM_FACT) = 0
                ) THEN 1
                ELSE NULL 
        END ) factSansCommandes,         
        (select 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 count(*) FROM FAC_CDELAD facCdelad with (nolock) WHERE f.NUM_FACT=facCdelad.NUM_FACT) cdeLad,  
        (SELECT count(*) FROM FAC_PIJO facpijo with (nolock) WHERE f.NUM_FACT=facpijo.NO_FAC) nbPJ,
        ISNULL(Litige.IDUNIQUE, 0) as Litige_IDUNIQUE,
        FAC_DEMAT_FLOW.STATUS as flow_STATUS
        ";
    }

    public static function postRegul($req, $post)
    {
    }
}
