<?php

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

class GestionFacture
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    public function detailFacture($numFact)
    {
        $req =
        "SELECT f.AFF_VALID, f.NUM_FACT, f.TYPE, f.CODE_FOUR, f.CODE_STE, f.FAC_CBY, f.DATE_PER, f.DTAE_ECH, 
        f.FACT_FOU, f.DATE_FAFO, f.DEVISE, f.TOTAL_HT, f.TOTAL_TTC, f.TX_DEVISE, f.MODE_REG, 
        four.RS, four.COND_PAIE, util.NOM
        FROM FAC_ENT f with (nolock) 
        JOIN FOURNISS four with (nolock) ON f.CODE_FOUR = four.CODE
        JOIN HASTUTIL util with (nolock) ON f.FAC_CBY = util.CODE
        WHERE NUM_FACT = :id";
        $arrayReq = [':id' => $numFact];
        
        $facture = $this->pdo->prepare($req);
        $facture->execute($arrayReq);
        return $facture->fetch($this->pdo::FETCH_ASSOC);
    }
    
    public function detailLigneFacture($numFact)
    {
        if (\Helpers\Outil\Roles::droit($this->pdo, 'SISECTOR', 'PARAMSTE')) {
            $codeSteSi = "f.CODE_STE";
        } else {
            $codeSteSi = "''";
        }
        if (\Helpers\Outil\Roles::droit($this->pdo, 'CDCSECTO', 'PARAMSTE')) {
            $codeSteScd = "f.CODE_STE";
        } else {
            $codeSteScd = "''";
        }
        if (\Helpers\Outil\Roles::droit($this->pdo, 'SCASECTO', 'PARAMSTE')) {
            $codeSteSca = "f.CODE_STE";
        } else {
            $codeSteSca = "''";
        }
        
        $req =
        "SELECT f.NUM_FACT, f.NUM_LIG, f.NUM_CDE, f.DESIGNAT, f.CODE_SFAM, f.CODE_SI, f.CODE_SCA, f.CODE_CDC,
        s.LIBELLE as LIBELLESI, sca.LIBELLE as LIBELLESCA, c.LIBELLE as LIBELLECDC
        FROM FAC_LIG f with (nolock) 
        LEFT JOIN SECTIMPU s with (nolock) ON s.CODE_SI = f.CODE_SI AND s.CODE_STE = $codeSteSi
        LEFT JOIN SSCPTANA sca with (nolock) ON sca.CODE_SCA = f.CODE_SCA AND sca.CODE_STE = $codeSteSca 
        LEFT JOIN CODECOMP c with (nolock) ON c.CODE_CDC = f.CODE_CDC AND c.CODE_STE = $codeSteScd 
        WHERE NUM_FACT = :id";
        $arrayReq = [':id' => $numFact];

        $facture = $this->pdo->prepare($req);
        $facture->execute($arrayReq);
        return $facture->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    public function detail1LigneFacture($numFact, $numLig)
    {
        if (\Helpers\Outil\Roles::droit($this->pdo, 'CDCSECTO', 'PARAMSTE')) {
            $codeSteScd = "f.CODE_STE";
        } else {
            $codeSteScd = "''";
        }
        
        $req =
        "SELECT f.CODE_SFAM, f.CODE_CDC, c.LIBELLE as LIBELLECDC
        FROM FAC_LIG f with (nolock) 
        LEFT JOIN CODECOMP c with (nolock) ON c.CODE_CDC = f.CODE_CDC AND c.CODE_STE = $codeSteScd 
        WHERE NUM_FACT = :id AND NUM_LIG = :ligne";
        
        $arrayReq = [
            ':id'    => $numFact,
            ':ligne' => $numLig,
        ];
        
        $facture = $this->pdo->prepare($req);
        $facture->execute($arrayReq);
        return $facture->fetch($this->pdo::FETCH_ASSOC);
    }
    
    public function getNumLigneFacture($numFact)
    {
        $req =
        "SELECT NUM_LIG
        FROM FAC_LIG with (nolock)
        WHERE NUM_FACT = :id";
        $arrayReq = [':id' => $numFact];

        $facture = $this->pdo->prepare($req);
        $facture->execute($arrayReq);
        return $facture->fetchAll($this->pdo::FETCH_ASSOC);
    }
}
