<?php

namespace Modules\Ajax\Repository;

class Facture
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    public function changeCde($codeSociete)
    {
        $req =
        "SELECT SOC_USR.COD_STE 
        FROM SOC_USR with (nolock) 
        WHERE SOC_USR.COD_USR = :value1 AND SOC_USR.STE = :value2";
        
        $arrayReq = [
            ':value1' => $_SESSION['Session_Login'],
            ':value2' => $codeSociete,
        ];
        
        $societe = $this->pdo->prepare($req);
        $societe->execute($arrayReq);
        return $societe->fetch();
    }
    
    
    public function numeroDeCommande($value)
    {
        $req =
        "SELECT NO_CDE 
        FROM CA_ENTET with (nolock) 
        WHERE NO_CDE = :value1 AND (ETAT_CDE ='E' OR ETAT_CDE ='S') AND CA_ENTET.ETAT_FIN = ''";
        
        $arrayReq = [
            ':value1' => $value
        ];

        $societe = $this->pdo->prepare($req);
        $societe->execute($arrayReq);
        return $societe->fetch();
    }
    
    public function calculDelaiDePaiement($date, $code)
    {
        $condPai = new \Entity\Model\CondPaie($this->pdo);
        $condPai = $condPai->find($code);

        $date =  date('Ymd', strtotime($date. ' + '.$condPai['DELAI_PAI'].' days'));
        if ($condPai['FIN_D_MOI'] == '0x54') {
            $dateFinDeMois = date("Ymt", strtotime($date));
            if ($date != $dateFinDeMois) {
                $date = $dateFinDeMois;
            }
        }
        
        if ($condPai['QUANT_PAI'] !='') {
            $date =  date('Ymd', strtotime($date. ' + '.$condPai['QUANT_PAI'].' days'));
        }
            
        return $date;
    }
}
