<?php

namespace Modules\ProjetAchat\Repository;

use Helpers\QueryBuilder\AbsRepository;

class TableauDeBord extends AbsRepository
{
    public function countListeAction(): int
    {
        // TODO : factorize with getListeAction
        $req = "SELECT COUNT(*) as count
                from DOA_ACTIONS_RACI raci with(nolock) 
                inner join DOA_ACTIONS act with(nolock) on act.ID_OBJET=raci.ID_DOA and act.ID_TACHE=raci.ID_TACHE and act.ID_ACTION=raci.ID_ACTION and act.TYPE_OBJET = 'D' 
                inner join DOA_TACHES tac with(nolock) on tac.ID_OBJET=raci.ID_DOA and tac.ID_TACHE=raci.ID_TACHE and tac.TYPE_OBJET = 'D' 
                inner join DOA d with(nolock) on d.MODELE=0x46 and d.ID=raci.ID_DOA 
                where d.STATUT='E' and act.TERMINEE=0x46 and tac.TERMINEE=0x46 and tac.DATE_DEB<=:date and tac.DATE_DEB>'00000000' 
                and tac.DATE_FIN>'00000000' and raci.RACI IN ('R','A','C') and raci.CODE_USER= :user  and d.SUSPENDU = 0x46 and d.AJOURNE = 0x46
                and (raci.TERMINE = 0x46 or (raci.TERMINE = 0x54 and  raci.RACI= 'R'))
            ";
        
        return intval($this->fetchAssocOne($req, [
            ':date' => date('Ymd'),
            ':user' => $_SESSION['Session_Login'],
        ])['count'] ?? 0);
    }

    /**
     * @return array<mixed>
    */
    public function getListeAction(bool $all = false): ?array
    {
        
        $req = "select ".($all? '':'TOP 3')." act.*,tac.LIB LIB_tache,tac.DATE_DEB LIB_date_deb ,tac.DATE_FIN LIB_date_fin ,tac.DELAI_PREAVIS ,d.LIB LIB_doa , d.REF
                from DOA_ACTIONS_RACI raci with(nolock) 
                inner join DOA_ACTIONS act with(nolock) on act.ID_OBJET=raci.ID_DOA and act.ID_TACHE=raci.ID_TACHE and act.ID_ACTION=raci.ID_ACTION and act.TYPE_OBJET = 'D' 
                inner join DOA_TACHES tac with(nolock) on tac.ID_OBJET=raci.ID_DOA and tac.ID_TACHE=raci.ID_TACHE and tac.TYPE_OBJET = 'D' 
                inner join DOA d with(nolock) on d.MODELE=0x46 and d.ID=raci.ID_DOA 
                where d.STATUT='E' and act.TERMINEE=0x46 and tac.TERMINEE=0x46 and tac.DATE_DEB<='".date('Ymd')."' and tac.DATE_DEB>'00000000' 
                and tac.DATE_FIN>'00000000' and raci.RACI IN ('R','A','C') and raci.CODE_USER= :user  and d.SUSPENDU = 0x46 and d.AJOURNE = 0x46
                and (raci.TERMINE = 0x46 or (raci.TERMINE = 0x54 and  raci.RACI= 'R'))
                order by tac.DATE_FIN";

        $arrayReq = [
            ':user' => $_SESSION['Session_Login'],
        ];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * @return array<mixed>
    */
    public function getListeProjet(): ?array
    {
        $req = "select DOA.*, DOAR_ETAT_AV.LIB LIB_ETAT_AV from DOA with(nolock) 
                left join DOAR_ETAT_AV with(nolock)  on DOAR_ETAT_AV.ID = DOA.ID_ETAT_AV
                where MODELE=0x46 and STATUT =  'E' AND PROPRIETAIRE= :user  and DOA.SUSPENDU = 0x46 AND DOA.AJOURNE = 0x46 ";
        
        $arrayReq = [
            ':user' => $_SESSION['Session_Login'],
        ];
        
        return $this->fetchAssocAll($req, $arrayReq);
    }
}
