<?php

namespace Modules\Fournisseur\Repository;

use Helpers\Outil\Date;

class AttestationsVisaGlobal
{
    
    private $pdo;
    
    /**
     * __construct
     * @param \PDO $pdo
     */
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
   
    /**
     * ListeAttestationFournisseur
     * @param string $code
     * @param array <string> $post
     * @param bool|string $export
     * @return mixed
     */
     // phpcs:ignore
    public function ListeAttestationFournisseur(string $code, array $post, $export = false)
    {
        if ($export) {
            $alias = "RAIS_SOC RAISON_SOCIAL, NO_DECADM NUM_DECISION, DTEDEBVAL DATE_DEBUT, DTEFINVAL DATE_FIN, DTE_DEC DATE_DECISION, DTE_ENV DATE_ENVOI";
        } else {
            $alias = "RAIS_SOC, NO_DECADM, DTEDEBVAL, DTEFINVAL, DTE_DEC, DTE_ENV";
        }
        $req = "select ".$alias." from DECADMSOC with (nolock)
                LEFT JOIN SOCIETE with (nolock) on SOCIETE.CODE_STE = DECADMSOC.COD_SOC
                LEFT JOIN ATTVGFOUR with (nolock) on ATTVGFOUR.COD_SOC = DECADMSOC.COD_SOC and NO_DECADM = NODECADM
                WHERE ATTVGFOUR.COD_FOU=:value";
        
        $arrayReq = [':value' => $code];
        
        if ($post) {
            if ($post['filter_SOCIETE']) {
                $req .= " and DECADMSOC.COD_SOC = :value1";
                $arrayReq[':value1']=$post['filter_SOCIETE'];
            }
            
            if ($post['filter_DA']) {
                $req .= " and upper(NO_DECADM) LIKE upper(:value2)";
                $arrayReq[':value2']='%'.$post['filter_DA'].'%';
            }
            
            if ($post['filter_DTEDEBVAL']) {
                Date::dateToSql($post['filter_DTEDEBVAL']);
                $req .= " and DTEDEBVAL = :value3";
                $arrayReq[':value3']=$post['filter_DTEDEBVAL'];
            }
            
            if ($post['filter_DTEFINVAL']) {
                Date::dateToSql($post['filter_DTEFINVAL']);
                $req .= " and DTEFINVAL = :value4";
                $arrayReq[':value4']=$post['filter_DTEFINVAL'];
            }
            
            if ($post['filter_DTE_DEC']) {
                Date::dateToSql($post['filter_DTE_DEC']);
                $req .= " and DTE_DEC = :value5";
                $arrayReq[':value5']=$post['filter_DTE_DEC'];
            }
            
            if ($post['filter_DTE_ENV']) {
                Date::dateToSql($post['filter_DTE_ENV']);
                $req .= " and DTE_ENV = :value6";
                $arrayReq[':value6']=$post['filter_DTE_ENV'];
            }
        }
        
        $attestations = $this->pdo->prepare($req);
        
        $attestations->execute($arrayReq);
        return $attestations->fetchAll($this->pdo::FETCH_ASSOC);
    }
}
