<?php

namespace Modules\Facture\Repository;

use Helpers\QueryBuilder\AbsRepository;

class EtatCde extends AbsRepository
{

    public function listeCde($numFact)
    {
        $req = "select NUM_CDE, NUM_LICDE from FAC_LIG with (nolock) where NUM_FACT = :numFact and NUM_CDE <> 0 and NUM_LICDE <> 0 group by NUM_CDE, NUM_LICDE";
        
        $arrayReq = [':numFact' => $numFact];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function detailLigneCde($numCde, $noPoCde)
    {
        $req = "select NO_CDE, NO_PO,DES_SOMM,  QTE_COMM, QTE_COMM-RAL QTE_RECEP, QTE_FACTU, PU, UNITE from CA_LIGNE with (nolock) 
                where NO_CDE = :numCde and NO_PO = :noPo";
        
        $arrayReq = [':numCde' => $numCde, ':noPo' => $noPoCde];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        return $requete->fetch($this->pdo::FETCH_ASSOC);
    }
    
    public function detailLigneFacture($numFact)
    {
        $req = "select QUANTITE, PU_REMISE, MONTANT, ATT_SIG, DESIGNAT from FAC_LIG with (nolock)
                left join FAC_ENT with (nolock) on FAC_ENT.NUM_FACT = FAC_LIG.NUM_FACT
                where FAC_LIG.NUM_FACT = :numFact and (FAC_LIG.NUM_CDE = 0 or FAC_LIG.NUM_LICDE = 0)";
        
        $arrayReq = [':numFact' => $numFact];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function calculeMoyennePu($numFact, $numCde, $noPoCde)
    {
        $req = "select PU_REMISE, QUANTITE, ATT_SIG, FAC_LIG.NUM_FACT, MONTANT,EN_MNT, MNT_REC from FAC_LIG with (nolock) 
                left join FAC_ENT with (nolock) on FAC_ENT.NUM_FACT = FAC_LIG.NUM_FACT
				left join BR_LIGNE with (nolock) on BR_LIGNE.NUM_BR = FAC_LIG.NUM_BR and BR_LIGNE.NO_PO_BR = FAC_LIG.NUM_LIBR
                where FAC_LIG.NUM_CDE = :numCde and FAC_LIG.NUM_LICDE = :noPo and FAC_LIG.NUM_FACT = :numFact";
        
        $arrayReq = [':numCde' => $numCde, ':noPo' => $noPoCde, 'numFact' => $numFact];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function enteteFacture($numFact)
    {
        $req = "select FAC_ENT.* from FAC_ENT with (nolock)
                where NUM_FACT = :numFact";
        
        $arrayReq = [':numFact' => $numFact];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        return $requete->fetch();
    }
}
