<?php

namespace Modules\Fournisseur\Repository;

use Helpers\Outil\Date;
use Helpers\Outil\Sql;
use Helpers\Filter\FilterTable;

class Attestations
{
    
    private $pdo;
    
    /**
     * __construct
     * @param \PDO $pdo
     */
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    /**
     * ListeAttestationFournisseur
     * @param string $code
     * @param array<mixed> $post
     * @param bool $export
     * @return array<mixed>
     */
    // phpcs:ignore
    public function ListeAttestationFournisseur(string $code, array $post, $export = false) : ?array
    {
        if ($export) {
            $req="select NOATTEST NUMERO, RAIS_SOC RAISON_SOCIAL, NODECADM DECISION_ADMIN, DTEACHAT DATE_CREATION, TYP_OPE TYPE_OPERATION 
                    from ATTESTFOU with (nolock)
                    LEFT JOIN SOCIETE with (nolock) on SOCIETE.CODE_STE = ATTESTFOU.COD_SOC WHERE COD_FOU=:value";
        } else {
            $req="select * from ATTESTFOU with (nolock) 
            LEFT JOIN SOCIETE with (nolock) on SOCIETE.CODE_STE = ATTESTFOU.COD_SOC WHERE COD_FOU=:value";
        }
        
        $arrayReq = [':value' => $code];
        if (isset($post['filter_CODE']) && $post['filter_CODE']) {
            $req .= " and NOATTEST=".TRIM($post['filter_CODE'])."";
        }
        
        if (isset($post['filter_SOCIETE']) && $post['filter_SOCIETE']) {
            $req .= " and COD_SOC=:value2";
            $arrayReq[':value2'] = $post['filter_SOCIETE'];
        }
        
        if (isset($post['filter_DA']) && $post['filter_DA']) {
            $req .= " and upper(NODECADM) LIKE :value3";
            $arrayReq[':value3'] = strtoupper('%'.$post['filter_SOCIETE'].'%');
        }
        
        if (isset($post['filter_date_DTE_CTE']) && $post['filter_date_DTE_CTE']) {
            Date::dateToSql($post['filter_date_DTE_CTE']);
            $req .= " and DTEACHAT=:value4";
            $arrayReq[':value4'] = $post['filter_date_DTE_CTE'];
        }
        
        if (isset($post['filter_TYPE_OPE']) && $post['filter_TYPE_OPE']) {
            $req .= " and TYP_OPE=:value5";
            $arrayReq[':value5'] = $post['filter_TYPE_OPE'];
        }
        
        $whereArray = FilterTable::splitFilter($post);
        
        if ($whereArray['tri']??''!='') {
            $req .= $whereArray['tri'];
        }

        $attestations = $this->pdo->prepare($req);

        $attestations->execute($arrayReq);
        return $attestations->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * attestation
     * @param string $code
     * @param string $codeAttestation
     * @return array<mixed>
     */
    public function attestation(string $code, string $codeAttestation) : ?array
    {
        $req = "select * from ATTESTFOU with (nolock) WHERE COD_FOU=:value and NOATTEST=:value1";
        $arrayReq = [
            ':value' => $code,
            ':value1' => $codeAttestation
        ];
        
        $attestations = $this->pdo->prepare($req);
        
        $attestations->execute($arrayReq);
        return $attestations->fetch();
    }
    
   
    
    /**
     * NouvelleAttestations
     * @param string $code
     * @param array <string> $post
     * @return void
     */
    // phpcs:ignore
    public function NouvelleAttestations(string $code, array $post) : void
    {
        $req = "select max(NOATTEST) from ATTESTFOU with (nolock) where COD_FOU=:value";
        
        $arrayReq = [':value' => $code];
        $attestations = $this->pdo->prepare($req);
        
        $attestations->execute($arrayReq);
        $maxID = $attestations->fetch();
        
        $req = "INSERT INTO ATTESTFOU VALUES(:value, :value1, ".($maxID[0]+1).", :value3, :value4, :value5, 0, '', 0,  :value7, :value8, :value9, :value10, :value11, :value12)";
        
        $arrayReq = [
            ':value' => $post['COD_SOC'],
            ':value1' => $post['NODECADM'],
            //
            ':value3' => $code,
            ':value4' => $post['DTE_ATT'],
            ':value5' => $post['TYP_OPE'],
            ':value7' => $post['DESCRIPT'],
            ':value8' => $post['MTDEVSTO'],
            ':value9' => $post['MT_TVAST'],
            ':value10' => $post['REF_CDE'],
            ':value11' => $post['REF_FFOU'],
            ':value12' => $post['DTEACHAT'],
        ];

        $attestations = $this->pdo->prepare($req);
        $attestations->execute($arrayReq);
    }
    
    
    /**
     * SuppressionAttestations
     * @param string $code
     * @param string $idAttestation
     * @return void
     */
    // phpcs:ignore
    public function SuppressionAttestations(string $code, string $idAttestation) : void
    {
        $req = "DELETE FROM ATTESTFOU WHERE COD_FOU=:value and NOATTEST=:value1";
        
        $arrayReq = [
            ':value' => $code,
            ':value1' => $idAttestation,
        ];
        
        $attestations = $this->pdo->prepare($req);
        $attestations->execute($arrayReq);
    }
    
    /**
     * decisionAdmin
     * @param string $demo
     * @return array<mixed>
     */
    public function decisionAdmin(string $demo) : ?array
    {
        $req = "SELECT NO_DECADM from DECADMSOC with (nolock) where COD_SOC=:value and '".date('Ymd')."' between DTEDEBVAL and DTEFINVAL and VISAGBL=".Sql::setBoolSql(0)."";
        $arrayReq = [
            ':value' => $demo
        ];
        
        $attestations = $this->pdo->prepare($req);
        
        $attestations->execute($arrayReq);
        return $attestations->fetch();
    }
    
    /**
     * updateAttestations
     * @param string $code
     * @param string $attestation
     * @param array <string> $post
     * @return void
     */
    public function updateAttestations(string $code, string $attestation, array $post) : void
    {
        $req = "UPDATE ATTESTFOU SET NODECADM=:value1, COD_SOC=:value, DTE_ATT = :value4, TYP_OPE= :value5, DESCRIPT=:value7, MTDEVSTO=:value8, MT_TVAST= :value9, REF_CDE = :value10, REF_FFOU = :value11, DTEACHAT = :value12
                WHERE NOATTEST = :value2 and COD_FOU = :value3";
        
        $arrayReq = [
            ':value' => $post['COD_SOC'],
            ':value1' => $post['NODECADM'],
            ':value2' => $attestation,
            ':value3' => $code,
            ':value4' => $post['DTE_ATT'],
            ':value5' => $post['TYP_OPE'],
            ':value7' => $post['DESCRIPT'],
            ':value8' => $post['MTDEVSTO'],
            ':value9' => $post['MT_TVAST'],
            ':value10' => $post['REF_CDE'],
            ':value11' => $post['REF_FFOU'],
            ':value12' => $post['DTEACHAT'],
        ];
        
        $attestations = $this->pdo->prepare($req);
        $attestations->execute($arrayReq);
    }
}
