<?php

namespace Modules\Contrat\Repository;

use Helpers\Filter\FilterTable;
use Modules\Ajax\Repository\Listes;
use Modules\GlobalApplication\Repository\Societe as BaseSociete;

class Societe extends BaseSociete
{
    /**
     * Return the devise
     * @return string
     */
    public function deviseParam() : string
    {
        $req="select HADEVSTK from PARAMETR with (nolock) where CLE = 1";
        $arrayReq = [];
        $accordCadre = $this->pdo->prepare($req);
        

        $accordCadre->execute($arrayReq);
        
        return $accordCadre->fetch()['HADEVSTK'];
    }

    /**
     * Return the detail of society by id
     * @param string $code
     * @param array $post
     * @return array<array<string>>
     */
    public function detailSociete(string $code, array $post = []) : array
    {
        /** @var \SimpleXMLElement $xml */
        $xml=simplexml_load_string($_SESSION['OXAPRC_LOGUSR_XML']);
        

        if ($xml->UsrMultiSoc == 'false') {
            $req="SELECT soc.CODE_STE, soc.RAIS_SOC, msoc.MNTPREMIN, msoc.MNTPREMAX, msoc.CUMULCDE, msoc.TYPCTRBUD ";
            if ((isset($post['filter_autorise']) == 'Oui') || isset($post['filter_autorise']) != '') {
                $req.=", case when msoc.MNTPREMAX IS NULL then 'NON' else 'OUI' end as 'Autorisé ?'";
            }
            $req .="
                         FROM SOCIETE soc with (nolock)";
            if ((isset($post['filter_autorise']) == 'Oui') || isset($post['filter_autorise']) != '') {
                $req = " inner join MARCHSOC msoc with (nolock) on soc.CODE_STE= msoc.CODESOC WHERE msoc.CODEMARCH like :value and soc.CODE_STE = '".$_SESSION['Session_SocUsrProfil']."'";
            }
        } else {
            $req="SELECT soc.CODE_STE, soc.RAIS_SOC, msoc.MNTPREMIN, msoc.MNTPREMAX, msoc.CUMULCDE, msoc.TYPCTRBUD ";
            
            if (isset($post['filter_autorise']) == 'Non' || isset($post['filter_autorise']) == '' || isset($post['filter_autorise']) == ' ') {
                $req.=", case when msoc.MNTPREMAX IS NULL then 'NON' else 'OUI' end as 'Autorisé ?'";
            }
            $req.="             FROM SOCIETE soc with (nolock)";
        }

        if (isset($post['filter_autorise']) == 'Non' || isset($post['filter_autorise']) == '' || isset($post['filter_autorise']) == ' ') {
            $req .= " left outer join MARCHSOC msoc with (nolock) on soc.CODE_STE= msoc.CODESOC and msoc.CODEMARCH = :value where 1=1";
        }
        

        $arrayReq = [];
        $arrayReq[':value'] = $code;
        
        if (isset($post['filter_CODE_STE']) && $post['filter_CODE_STE'] != '') {
            $req .=" and UPPER(soc.CODE_STE) like :value1";
            $arrayReq[':value1']=strtoupper('%'.$post['filter_CODE_STE'].'%');
        }
        

        if (isset($post['filter_RAIS_SOC']) && $post['filter_RAIS_SOC'] != '') {
            $req .=" and UPPER(soc.RAIS_SOC) like UPPER(:value2)";
            $arrayReq[':value2']= '%'.$post['filter_RAIS_SOC'].'%';
        }
        
        if (isset($post['filter_MNTPREMIN']) && $post['filter_MNTPREMIN'] != '') {
            $req .=" and MNTPREMIN like :value3";
            $arrayReq[':value3']=$post['filter_MNTPREMIN'];
        }
        
        if (isset($post['filter_MNTPREMAX']) && $post['filter_MNTPREMAX'] != '') {
            $req .=" and MNTPREMAX LIKE :value4 ";
            $arrayReq[':value4']=$post['filter_MNTPREMAX'];
        }
        
        if (isset($post['filter_CUMULCDE']) && $post['filter_CUMULCDE'] != '') {
            $req .=" and msoc.CUMULCDE LIKE :value5";
            $arrayReq[':value5']=$post['filter_CUMULCDE'];
        }
        if (isset($post['filter_TYPCTRBUD']) && $post['filter_TYPCTRBUD'] != '') {
            $req .=" and UPPER(msoc.TYPCTRBUD) like UPPER(:value6)";
            $arrayReq[':value6']='%'.$post['filter_TYPCTRBUD'].'%';
        }
        
        if (isset($post['filter_autorise']) && $post['filter_autorise'] != '' && $post['filter_autorise'] == 'Oui') {
            // TODO completer le filtre du tableau sur l'élement choisie
            $req .=" and (msoc.MNTPREMAX is not null or msoc.MNTPREMIN is not null 
            or msoc.TYPCTRBUD != '' or CUMULCDE is not null)";
        }
        if (isset($post['filter_autorise']) && $post['filter_autorise'] != '' && $post['filter_autorise'] == 'Non') {
            // TODO completer le filtre du tableau sur l'élement choisie
            $req .=" and (msoc.MNTPREMAX is null or msoc.MNTPREMIN is null 
            or msoc.TYPCTRBUD is null or CUMULCDE is null)";
        }
        
        $whereArray = FilterTable::splitFilter((array)$post, 0, 0, 1);
        
        if (count($whereArray)>  0) {
            $req .= $whereArray['tri'];
        }
        
        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * Return the number of occurences in market society by an id
     * @param string $code
     * @return array<array<string>>
     */
    public function isExistMarchSoc($code) : array
    {
        
        $req=" SELECT count (*) from MARCHSOC with (nolock) WHERE CODESOC = :value";
        
        $arrayReq = [':value' => '%'.$code.'%'];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * Return the detail society binding by the market society
     * @param string $code
     * @param string $code_ste
     * @return array<string>
     */
    public function detailSocieteModification($code, $code_ste) : array
    {
        
        $req="SELECT * FROM SOCIETE soc with (nolock) left join MARCHSOC msoc with (nolock) on soc.CODE_STE= msoc.CODESOC and msoc.CODEMARCH like :value where soc.CODE_STE = :code_ste";
        
        $arrayReq = [':value' => '%'.$code.'%', ':code_ste' => $code_ste];
        $typeMarche = $this->pdo->prepare($req);
        
        $typeMarche->execute($arrayReq);
        
        return $typeMarche->fetch();
    }
    
    /**
     * Re
     * @param mixed $code
     * @return mixed
     */
    public function detailTitre($code)
    {
        $req=" SELECT * FROM MARCHES with (nolock) WHERE CODE = :value";
        
        $arrayReq = [':value' => $code];
        $typeMarche = $this->pdo->prepare($req);
        
        $typeMarche->execute($arrayReq);
        
        return $typeMarche->fetch();
    }
    
    public function listExport($codemarche)
    {
        $code = 'SOCIETE';
        
        $alias="select soc.CODE_STE Code_Societe, soc.RAIS_SOC Raison_Social,msoc.CODEMARCH as Code_Marche, msoc.MNTPREMIN, msoc.MNTPREMAX, msoc.CUMULCDE, msoc.TYPCTRBUD 
                    FROM ".$code." soc with (nolock) left join MARCHSOC msoc with (nolock) 
                    on soc.CODE_STE= msoc.CODESOC and msoc.CODEMARCH like ".$codemarche."";
        
        return $this->fetchAssocAll($alias, []);
    }

    public function listeSocieteByCodeUser($codeMarche)
    {
        // TODO : merge into one query
        $societes = $this->listRestrictedSocieteForUser();
        
        $req="SELECT soc.CODE_STE, soc.RAIS_SOC, msoc.MNTPREMIN, msoc.MNTPREMAX, msoc.CUMULCDE, msoc.TYPCTRBUD
        FROM SOCIETE soc with (nolock) 
        left join MARCHSOC msoc with (nolock) on soc.CODE_STE= msoc.CODESOC 
        WHERE msoc.CODEMARCH like :value and (soc.CODE_STE = '".$_SESSION['Session_SocUsrProfil']."'";

        // TODO : turn into "IN"
        if ($societes !== null) {
            foreach ($societes as $val) {
                $req .= " or soc.CODE_STE = '".$val['CODE_STE']."'";
            }
        }
        
        $req .=");";
               
        $arrayReq = [':value' => $codeMarche];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
}
