<?php

namespace Modules\Ajax\Repository;

use Helpers\Outil\Date;
use Helpers\QueryBuilder\AbsRepository;

class Facture extends AbsRepository
{
    
    /**
     * @param string $codeSociete
     * @return array<mixed>
     */
    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();
    }
    
    
    /**
     * @param string $value
     * @return array<mixed>
     */
    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();
    }
    
    /**
     * @param string $date
     * @param string $code
     * @return string
     */
    public function calculDelaiDePaiement($date, $code)
    {
        $condPai = new \Entity\Model\CondPaie($this->pdo);
        $condPai = $condPai->find($code);

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