<?php

namespace Modules\Contrat\Repository;

// MARCHE
class ContratRep
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    /**
     * Return all the value that it need to be listed for the export
     * @param string $select
     * @param array <string> $post
     * @return array<mixed>
     */
    public function listExport(String $select, Array $post) : ?array
    {
        $req = "SELECT $select FROM MARCHES with (nolock)
        LEFT JOIN MEMOS with(nolock) on NMCOMMEN=NODEMEMO ";
        
        $arrayReq = [];
        
        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 0);
        if (count($whereArray)>  0) {
            $req .= $whereArray[0].$whereArray['tri'];
            
            if (count($whereArray[1]) != 0) {
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
            }
        }
                
        $bdd = $this->pdo->prepare($req);
        $bdd->execute($arrayReq);
        return $bdd->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    /**
     * Return the detail of market by his id
     * @param string $code
     * @return array<string>
     */
    public function detail(String $code) : array
    {
        
        $req="SELECT MARCHES.ACHETEUR as ACHETEURCODE,*,SIGNATAIRE.NOM SIGNATAIRE, MARCHES.CODE CODE, MARCHES.LIBELLE , SOCIETE.RAIS_SOC RAIS_DOC, TYPMARCH.LIBELLE CODE_TYMA, ACHETEUR.NOM ACHETEURS, 
        ISNULL(TRAD_CDP.LIBELLE, CONDPAIE.LIBELLE) AS CONDPAIE_LIBELLE, ISNULL(t2.LIBELLE, MODEPAIE.LIBELLE) AS MODEPAIE, ISNULL(t.LIBELLE, MODELIVR.LIBELLE) AS MODELIVR, MEMOS.CONTMEMO MEMOS
        from MARCHES with (nolock) 
            LEFT JOIN SOCIETE with (nolock)  ON SOCIETE.CODE_STE = MARCHES.STEMARCHE
            LEFT JOIN TYPMARCH  with (nolock) ON TYPMARCH.CODE_TYMA = MARCHES.TYPEMARC 
            LEFT JOIN HASTUTIL AS ACHETEUR  with (nolock) ON ACHETEUR.CODE = MARCHES.ACHETEUR
            LEFT JOIN HASTUTIL AS SIGNATAIRE  with (nolock) ON SIGNATAIRE.CODE = MARCHES.SIGNATAIR
            LEFT JOIN CONDPAIE  with (nolock) ON CONDPAIE.CODE_CDP = MARCHES.CONDPAI
            LEFT OUTER JOIN TRAD_CDP WITH(NOLOCK) ON TRAD_CDP.CODE_CDP = CONDPAIE.CODE_CDP AND TRAD_CDP.COD_LNG = '{$_SESSION['Session_Langue']}'
            LEFT JOIN MODEPAIE  with (nolock) ON MODEPAIE.CODE_MDP = MARCHES.MODEPAI
            LEFT OUTER JOIN TRAD_MDP t2 WITH(NOLOCK) ON t2.CODE_MDP = MODEPAIE.CODE_MDP AND t2.COD_LNG = '{$_SESSION['Session_Langue']}'
            LEFT JOIN MODELIVR with (nolock)  ON MODELIVR.CODE_MDL = MARCHES.MODELIV
            LEFT OUTER JOIN TRAD_MDL t WITH(NOLOCK) ON t.CODE_MDL = MODELIVR.CODE_MDL AND t.COD_LNG = '{$_SESSION['Session_Langue']}'
            LEFT JOIN MEMOS with (nolock)  ON MEMOS.NODEMEMO = MARCHES.NMCOMMEN
        WHERE MARCHES.CODE = :value";
        

        $arrayReq = [':value' => $code];
        $typeMarche = $this->pdo->prepare($req);
        

        
        $typeMarche->execute($arrayReq);
        return $typeMarche->fetch(\PDO::FETCH_ASSOC);
    }

    /**
     * Return the cumulation budget in a fragment agreement by the market id
     * @param string $marche
     * @return array<string>
     */
    public function cumulAcd(String $marche) : array
    {
        
        $req="SELECT sum(BUDGET) cumulAcd
        FROM ACCCADRE with (nolock)         
        WHERE MARCHE = :value";

        $arrayReq = [':value' => $marche];
        $typeMarche = $this->pdo->prepare($req);

        $typeMarche->execute($arrayReq);
        return $typeMarche->fetch(\PDO::FETCH_ASSOC);
    }

    /**
     * Retourn de complete detail for a society
     * @param string $code
     * @return array<string>
     */
    public function detailSociete(String $code) : array
    {
        
        $req="select MNTPREMIN, MNTPREMAX
        from MARCHSOC with (nolock) 
        WHERE CODEMARCH = :value";
        
        $arrayReq = [':value' => $code];
        $typeMarche = $this->pdo->prepare($req);
        
        $typeMarche->execute($arrayReq);
        return $typeMarche->fetch();
    }
    /**
     * Return the detail for a market society by his market code
     * @param string $code
     * @return array<string>
     */
    public function detailSocieteByCode(String $code)  : array
    {
        
        $req="select CODEMARCH,CODESOC, MNTPREMIN, MNTPREMAX
        from MARCHSOC with (nolock) 
        WHERE CODEMARCH = :value";
        
        $arrayReq = [':value' => $code];

        $typeMarche = $this->pdo->prepare($req);
        
        $typeMarche->execute($arrayReq);
        return $typeMarche->fetchAll(\PDO::FETCH_ASSOC);
    }
    /**
     * Return the comment detail for a market
     * @param string $code
     * @return array<string>
     */
    public function visualisationCommentaire(String $code) : array
    {
        
        $req="    select * from MARCHES ma with (nolock) 
                    left join MEMOS m with (nolock) on m.NODEMEMO=ma.NMCOMMEN 
                    WHERE ma.CODE = :value";
        
        $arrayReq = [':value' => $code];
        $typeMarche = $this->pdo->prepare($req);
        
        $typeMarche->execute($arrayReq);
        return $typeMarche->fetch(\PDO::FETCH_ASSOC);
    }

    /**
     * Return the list of a market society by his id
     * @param array <string> $post
     * @return array<mixed>
     */
    public function marcheByCodeSociete(Array $post) : ?array
    {
        $arrayReq = [];
        $req="SELECT *, COUNT(*) OVER () AS NBRE_MARCHE FROM MARCHES with (nolock) WHERE 1=1";

        if (!$_SESSION['Session_UsrMultiSoc']) {
            $arrayReq[':codeSte'] = $_SESSION['Session_SocUsrProfil'];
            $req .=
            " AND (MARCHES.STEMARCHE = :codeSte or MARCHES.STEMARCHE = '' 
            OR MARCHES.STEMARCHE in (SELECT socUsr.COD_STE FROM SOC_USR socUsr with (nolock) WHERE socUsr.COD_USR = '".$_SESSION['Session_Login']."'))";
        }
        
        $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]);
            }
        }

        $typeMarche = $this->pdo->prepare($req);
        $typeMarche->execute($arrayReq);
        return $typeMarche->fetchAll(\PDO::FETCH_ASSOC);
    }
}
