<?php

namespace Modules\Fournisseur\Repository;

use Helpers\Outil\Sql;

class CentreEmetteur
{
    
    private \PDO $pdo;
    
    /**
     * __construct
     * @param \PDO $pdo
     */
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * ListeCentreEmetteur
     * @param string $id
     * @param array <mixed>  $post
     * @param string|bool $export
     * @return mixed
     */
    // phpcs:ignore
    public function ListeCentreEmetteur(string $id, array $post = [], $export = false)
    {
        if ($export) {
            $alias="SOCIETE.RAIS_SOC SOCIETE, CENTRE.RAIS_SOC CENTRE, CENTRE.CODE_STE CODE_CENTRE, ASSOCIE, INTEPROC, INTERDIT";
        } else {
            $alias="CENTRE.RAIS_SOC CENTRE, SOCIETE.RAIS_SOC SOCIETE, FOURNCTR.CODE_FOUR ,FOURNCTR.CODE_CTR, FOURNCTR.ASSOCIE, FOURNCTR.INTERDIT, FOURNCTR.INTEPROC, FOURNCTR.ASSOCIE";
        }
        
        $req = 'SELECT '.$alias.' FROM FOURNCTR with (nolock)
                LEFT JOIN CENTRE with (nolock) on FOURNCTR.CODE_CTR = CENTRE.CODE_CTR
                LEFT JOIN SOCIETE with (nolock) on SOCIETE.CODE_STE = CENTRE.CODE_STE
                WHERE CODE_FOUR= :code';

        $arrayReq = [
            ':code' => $id,
        ];

        if (isset($post['filter_SOCIETE']) && $post['filter_SOCIETE']) {
            $req .= ' and UPPER(SOCIETE.RAIS_SOC) LIKE upper(:value1)';
            $arrayReq[':value1']='%'.$post['filter_SOCIETE'].'%';
        }
        
        if (isset($post['filter_CENTRE']) && $post['filter_CENTRE']) {
            $req .= ' and (UPPER(CENTRE.RAIS_SOC) LIKE upper(:value2) or  UPPER(CENTRE.CODE_CTR) LIKE upper(:value3))';
            $arrayReq[':value2']='%'.$post['filter_CENTRE'].'%';
            $arrayReq[':value3']='%'.$post['filter_CENTRE'].'%';
        }
        
        if ($post['filter_ASSOCIE']??''==='0') {
            $sql =  sql::setBoolSql(sql::boolSql($post['filter_ASSOCIE']));
            $req .= ' and ASSOCIE = '.$sql.'';
        }
        if ($post['filter_INTEPROC']??''==='0') {
            $sql =  sql::setBoolSql(sql::boolSql($post['filter_INTEPROC']));
            $req .= ' and INTEPROC = '.$sql.'';
        }
        if ($post['filter_INTERDIT']??''==='0') {
            $sql =  sql::setBoolSql(sql::boolSql($post['filter_INTERDIT']));
            $req .= ' and INTERDIT = '.$sql.'';
        }
        
        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
        
        return  $shortList->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    
    /**
     * CreationCentreEmetteur
     * @param string $code
     * @param array<mixed> $post
     * @return void
     */
    // phpcs:ignore
    public function CreationCentreEmetteur(string $code, array $post) : void
    {
        $req = 'INSERT INTO FOURNCTR VALUES(:code, :value1, '.sql::setBoolSql($post['ASSOCIE']).', '.sql::setBoolSql($post['INTERDIT']).', '.sql::setBoolSql($post['INTEPROC']).')';
        
        $arrayReq = [
            ':code' => $code,
            ':value1' => $post['CODE_CTR'],
        ];
        
        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
    }
    
    
    /**
     * SupresionCentreEmetteur
     * @param string $code
     * @param string $codeCentre
     * @return void
     */
    // phpcs:ignore
    public function SupresionCentreEmetteur(string $code, string $codeCentre) : void
    {
        $req = 'DELETE FROM FOURNCTR WHERE CODE_FOUR=:value1 and CODE_CTR=:value2';
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $codeCentre,
        ];
        
        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
    }
    
    /**
     * selectCentreEmetteur
     * @param string $code
     * @param string $centre
     * @return array<mixed>
     */
    public function selectCentreEmetteur(string $code, string $centre) : ?array
    {
        $req = 'SELECT * FROM FOURNCTR with (nolock) WHERE CODE_FOUR=:value1 and CODE_CTR=:value2';
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $centre,
        ];
        
        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
        
        return  $shortList->fetch();
    }
    
    /**
     * updateCentreEmetteur
     * @param string $code
     * @param string $centre
     * @param array<mixed> $post
     * @return void
     */
    public function updateCentreEmetteur(string $code, string $centre, array $post) : void
    {
        $req = 'UPDATE FOURNCTR SET ASSOCIE='.sql::setBoolSql($post['ASSOCIE']).', INTERDIT='.sql::setBoolSql($post['INTERDIT']).', INTEPROC='.sql::setBoolSql($post['INTEPROC']).' where CODE_FOUR=:value1 and CODE_CTR=:value2';
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $centre,
        ];
        
        $shortList = $this->pdo->prepare($req);
        $shortList->execute($arrayReq);
    }
}
