<?php

namespace Modules\DemandeAchat\Repository;

use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Modules\GlobalApplication\Repository\LicenceOxalys;
use Modules\GlobalApplication\Repository\Signature as RepositorySignature;

class Signature
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    /**
     * listeSignature
     * @param array<string> $post
     * @return array<mixed>
     */
    public function listeSignature(Array $post) : array
    {
        $req =
        "SELECT * FROM 
        (
            SELECT DA_ENTET.NO_DA, DA_ENTET.DTE_EMISS, DA_ENTET.DTE_VAL, DA_ENTET.NO_DEM, DA_ENTET.FOU, DA_ENTET.TOTAL_DA, DA_ENTET.TOTAL_DA_TTC, DA_ENTET.DEVISE, SIG_DA.SIGNAT,
            HASTUTIL.NOM demandeur, FOURNISS.RS FROM DA_ENTET with (nolock)
            INNER JOIN SIG_DA with (nolock) ON SIG_DA.NO_DA = DA_ENTET.NO_DA 
            LEFT JOIN HASTUTIL with (nolock) ON HASTUTIL.CODE = DA_ENTET.NO_DEM
            LEFT JOIN FOURNISS with (nolock) ON FOURNISS.CODE = DA_ENTET.FOU 
            WHERE DA_ENTET.SIGNEE = ".\Helpers\Outil\Sql::setBoolSql(0)." 
            AND SIG_DA.SIGNAT = :user 
            AND SIG_DA.SIGNABLE = ".\Helpers\Outil\Sql::setBoolSql(1)."
        ) DA 
        WHERE 1=1";

        if (isset($post['DELEG']) && $post['DELEG']!='') {
            $signat = $post['DELEG'];
        } else {
            $signat = $_SESSION['Session_Login'];
        }
        $arrayReq = [':user' => $signat];

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
        $req .= $filtreAvance->generateSql(1);
        $filtreAvance->pushArray($arrayReq);

        if (count($whereArray)>  0) {
            $req .= $whereArray[0].$whereArray['tri'];
            
            if (count($whereArray[1]) != 0) {
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
            }
        }

        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
        return $da->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * detailSignature
     * @param string $code
     * @return array<string>
     */
    public function detailSignature(String $code) : Array
    {
        $req =
        "SELECT DA_ENTET.CODE_STE, DA_ENTET.CHIF_EXEC, DA_ENTET.BUDGETDA, DA_ENTET.DEVISE, MEMOS.CONTMEMO
        FROM DA_ENTET with (nolock)
        LEFT OUTER JOIN MEMOS with (nolock) on DA_ENTET.NM_COMMEN=MEMOS.NODEMEMO
        WHERE DA_ENTET.NO_DA = :value";

        $arrayReq[':value'] = $code;

        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
        return $da->fetch($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * listeLigneDemandeAchat
     * @param string $code
     * @return array<mixed>
     */
    public function listeLigneDemandeAchat(String $code) : Array
    {
        $req =
        "SELECT DA_LIGNE.NO_DA, DA_LIGNE.NO_PO, DA_LIGNE.CODE_ART, DA_LIGNE.DES_SOMM, DA_LIGNE.QTE_DEM, DA_LIGNE.UNITE, DA_LIGNE.PU, DA_LIGNE.DTELIVSOU, DA_LIGNE.SECTIMP, DA_LIGNE.CODE_COMP, DA_LIGNE.SSCPTEANA, DA_LIGNE.CODE_FOU,  DA_LIGNE.TX_REMISE,  DA_LIGNE.BUDGET, MEMOS.CONTMEMO
        FROM DA_LIGNE with (nolock) 
        INNER JOIN DA_ENTET with (nolock) on DA_LIGNE.NO_DA=DA_ENTET.NO_DA
        LEFT JOIN MEMOS with (nolock) on MEMOS.NODEMEMO = DA_LIGNE.NM_DCOMP
        WHERE DA_LIGNE.NO_DA = :value";
        
        $arrayReq[':value'] = $code;
        
        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
        return $da->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * getSignataire
     * @param string $code
     * @return array<string>
     */
    public function getSignataire(String $code) : array
    {
        $req =
        "SELECT DA_ENTET.NO_DA, DA_ENTET.FOU, DA_ENTET.CODE_STE, DA_ENTET.CHIF_EXEC, DA_ENTET.DTE_EMISS, DA_ENTET.DTE_VAL, DA_ENTET.TOTAL_DA, DA_ENTET.TOTAL_DA_TTC, SIG_DA.SIGNAT
        FROM DA_ENTET with (nolock)
        INNER JOIN SIG_DA with (nolock) ON SIG_DA.NO_DA = DA_ENTET.NO_DA
        WHERE DA_ENTET.NO_DA = :value AND SIG_DA.SIGNABLE = ".\Helpers\Outil\Sql::setBoolSql(1);
        
        $arrayReq[':value'] = $code;
        
        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
        return $da->fetch($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * getSignataireActif
     * @param string $code
     * @return array<string>
     */
    public function getSignataireActif(String $code) : array
    {
        $req =
        "SELECT SIG_DA.SIGNAT 
        FROM SIG_DA with (nolock) 
        LEFT JOIN SIG_DELEG with (nolock) on SIG_DELEG.SIGNAT = SIG_DA.SIGNAT 
        where SIG_DELEG.DELEG = :user and SIG_DA.NO_DA = :value";
        
        $arrayReq = [
            ':value' => $code,
            ':user' => $_SESSION['Session_Login']
        ];
        
        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
        return $da->fetch($this->pdo::FETCH_ASSOC);
    }

    /**
     * getDelegation
     * @param array<string> $post
     * @return array<mixed>
     */
    public function getDelegation(Array $post) : Array
    {
        $today = Date('Ymd');

        $req =
        "SELECT SIG_DELEG.SIGNAT, SIG_DELEG.DELEG, SIG_DELEG.DTE_DEB, SIG_DELEG.DTE_FIN, HASTUTIL.NOM
		FROM SIG_DELEG with (nolock)
        LEFT JOIN HASTUTIL with (nolock) on HASTUTIL.CODE = SIG_DELEG.DELEG
        WHERE SIGNAT = :user ";

        $arrayReq = [':user' => $_SESSION['Session_Login']];
        
        if (isset($post['ACTIF']) && $post['ACTIF']) {
            $req .= "AND DTE_DEB <= :today AND (DTE_FIN >= :today OR DTE_FIN = '00000000' )";
            $arrayReq[':today'] = $today;
        }

        // ORDER BY SIG_DELEG.DTE_DEB

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
        $req .= $filtreAvance->generateSql(1);
        $filtreAvance->pushArray($arrayReq);

        if (count($whereArray)>  0) {
            $req .= $whereArray[0].$whereArray['tri'];
            
            if (count($whereArray[1]) != 0) {
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
            }
        }
        
        $da = $this->pdo->prepare($req);
        $da->execute($arrayReq);
        return $da->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * getListeDelegateurs
     * @param array<string> $post
     * @return array<mixed>
     */
    public function getListeDelegateurs(Array $post) : ?array
    {
        $signature = new RepositorySignature($this->pdo);
        
        return $signature->getListeDelegateurs($post);
    }
    
    public function isTransfertPossible($post)
    {
        $req = "SELECT HASTUTIL.CODE
        FROM HASTUTIL with(nolock)
        LEFT JOIN SOCIETE with(nolock) on HASTUTIL.CODE_STE = SOCIETE.CODE_STE
        WHERE HASTUTIL.CODE = :signat
        AND HASTUTIL.CODE != :user 
        AND HASTUTIL.SIG_DA = 0x54 ";
        
        $arrayReq = [
            ':signat' => $post['CODE_SIG'],
            ':user' => $_SESSION['Session_Login'],
        ];
        
        if ((new LicenceOxalys($this->pdo))->accordConfActive()) {
            $req .= " AND HASTUTIL.NIV_CONF >= isnull((SELECT TYPDOC.NIV_CONF FROM TYPDOC with (nolock) WHERE TYPDOC.TYPE = (SELECT TYP_DOC FROM DA_ENTET where NO_DA = ".$post['NumDA'].")),0)";
        }
        
        // Si l'utilisateur n'est pas multisociété alors on filtre les signataires de sa société
        if (! $_SESSION['Session_UsrMultiSoc']) {
            $req .= " AND HASTUTIL.CODE_STE = :societe";
            $arrayReq[':societe'] = $_SESSION['Session_SocUsr'];
        }
        

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