<?php

namespace Modules\Commande\Repository;

use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Helpers\QueryBuilder\AbsRepository;
use Modules\GlobalApplication\Repository\LicenceOxalys;

class ValidationCommande extends AbsRepository
{
    
    public function listeSignat($code)
    {
        $req = "select * from SIG_CDE  with (nolock) 
                LEFT JOIN HASTUTIL  with (nolock) on HASTUTIL.CODE = SIG_CDE.SIGNAT
                where NO_CDE = :value";
        
        $arrayReq[':value']=$code;
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    public function insertNewSignat($array)
    {
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        
        $req = "INSERT INTO SIG_CDE (SIGNAT, BON_SIGNE, NO_CDE, DTE_MISE, ORDRE, SIGNABLE, CAROLES, INFORME)
                VALUES(:value1, 0x54, :value2, :value3, ".$array['ORDRE'].", ".sql::setBoolSql($array['SIGNABLE']).", :value6, ".sql::setBoolSql($array['INFORME']).")";
        
        $arrayReq= [
            ':value1' => $array['SIGNAT'],
            ':value2' => $array['NO_CDE'],
            ':value3' => $array['DTE_MISE'],
            ':value6' => $array['CAROLES'],
        ];
        
        $commande = $this->pdo->prepare($req);
        $commande->execute($arrayReq);
    }
    
    public function listeDaPourCde($value, $post)
    {
        $user = $_SESSION['Session_Login'];
        
        $req = "select NO_DA, NO_DEM, FOU, CTR_LIV, DTE_VAL, TOTAL_DA, TOTAL_DA_TTC, NM_TITRE
                from DA_ENTET,TYPDOC with (nolock)
                where TYPDOC.TYPE=DA_ENTET.TYP_DOC and CODE_STE=:codeSte and TRAITEE=0x46 and NO_DA=:noDa and TRIM(FM_CODE) != '1-'";
        
        $DaSigneObli = Sql::paravrac($this->pdo, 'PARAMSIG', 'NOTCREATCDEDANONSIG');
        if ($DaSigneObli == 'O') {
            $req .= " and DA_ENTET.SIGNEE = 0x54 ";
        }
        
        if ((new LicenceOxalys($this->pdo))->accordConfActive()) {
            $req .= " and TYPDOC.NIV_CONF<= (select NIV_CONF from HASTUTIL with (nolock) where CODE='$user') and TYPDOC.NIV_CONF<=(select NIV_CONF from HASTUTIL with (nolock) where CODE='$user') ";
        }
            
            $arrayReq = [
                ':noDa' => $value,
                ':codeSte' => $post['codeSte'],
            ];
            
            $requete = $this->pdo->prepare($req);
            $requete->execute($arrayReq);
            
            return $requete->fetch($this->pdo::FETCH_ASSOC);
    }
    
    public function listeAOpourCde($value, $post)
    {
        $codeSte = trim($post['codeSte']);
        
        $req = " SELECT NO_AO, DTE_AO, ACH, EMETTEUR, DEL_REP, NM_TITRE from AO_ENTET with(nolock) 
            where NO_AO in (
                select aoe.NO_AO from AO_LIGNE aol with(nolock)
                join AO_ENTET aoe with(nolock) on aoe.NO_AO=aol.NO_AO
                left join DA_LIGNE dal with(nolock) on dal.NO_DA=aol.NO_DA and dal.NO_PO=aol.NO_PO_DA
                where aol.CODE_STE='$codeSte' and aoe.VALCHXFOU!='00000000' and isnull(dal.TRAITEE,0x46)=0x46 group by aoe.NO_AO
            ) and NO_AO = :noAo";
        
        $arrayReq = [
            ':noAo' => $value,
        ];
        
        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        
        if (count($whereArray)>  0) {
            $req .= $whereArray[0].$whereArray['tri'];
            
            if (count($whereArray[1]) != 0) {
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
            }
        }
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        
        return $requete->fetch($this->pdo::FETCH_ASSOC);
    }

    /* requete validator */
    public function isPeriode($date)
    {
        $req = "select * from PERIODES with (nolock) where :date between DATE_DEB and DATE_FIN";
        
        $arrayReq = [':date' => $date];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        
        return $requete->fetch($this->pdo::FETCH_ASSOC);
    }
    
    /* requete validator */
    public function numCdeExist($NO_CDE)
    {
        $req = "Select * from CA_ENTET with (nolock) where NO_CDE=:NO_CDE";
        
        $arrayReq = [':NO_CDE' => $NO_CDE];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        
        return $requete->fetch($this->pdo::FETCH_ASSOC);
    }
}
