<?php

namespace Modules\Fournisseur\Repository;

use Helpers\QueryBuilder\AbsRepository;

class Facture extends AbsRepository
{

    
    /**
     * listeFacture
     * @param string $code
     * @param array<mixed> $post
     * @param string $listeSociete
     * @return mixed
     */
    public function listeFacture(string $code, array $post, string $listeSociete = '')
    {
        $req = "Select NUM_FACT, DATE_ENR, DATE_FAFO, DTAE_ECH, TOTAL_HT, TOTAL_TTC from FAC_ENT with (nolock) WHERE FAC_ENT.CODE_FOUR = :value and CODE_STE in ($listeSociete) ";
        
        $arrayReq[':value']=$code;

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
            
        if (count($whereArray)>  0) {
            $req .= $whereArray[0];
            if (count($whereArray[1]) != 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }
        
        $facture = $this->pdo->prepare($req);
        $facture->execute($arrayReq);
        
        return $facture->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * factureFourniss
     * @param string $code
     * @return array<mixed>
     */
    public function factureFourniss(string $code) : ?array
    {
        $req = "Select ORDO, RAIS_SOC, DATE_PER, ENT_VALID, AFF_VALID, BON_A_PAY from FAC_ENT with (nolock)
                LEFT JOIN SOCIETE with (nolock) on SOCIETE.CODE_STE = FAC_ENT.CODE_STE
                WHERE FAC_ENT.NUM_FACT = :value";
        
        $arrayReq[':value']=$code;
        $facture = $this->pdo->prepare($req);
        $facture->execute($arrayReq);
        
        return $facture->fetch();
    }
    
    /**
     * listeLigneFacture
     * @param string $fature
     * @return array<mixed>
     */
    public function listeLigneFacture(string $fature) : ?array
    {
        $req = "Select NUM_CDE, NUM_BR, NUM_TERME, CODE_SFAM, TAUX_TVA, QUANTITE, PU_REMISE, MONTANT, TYPE_TVA, CODE_SI, CODE_CDC, CODE_SCA from FAC_LIG with (nolock) WHERE NUM_FACT = :value";
        
        $arrayReq[':value']=$fature;
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
}
