<?php

namespace Modules\ProjetAchat\Repository;

class Indicateur
{
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    /**
     * @return array<mixed>
    */
    public function getNombreProjetInitPrepra(): ?array
    {
        $req = "select count(*) NB_PROJET from DOA with (nolock) where STATUT = 'I' or STATUT = 'P'";
        
        
        $data = $this->pdo->prepare($req);
        $data->execute();
        return  $data->fetch();
    }
    
    /**
     * @return array<mixed>
    */
    public function getNombreProjetEnCours(): ?array
    {
        $req = "select count(*) NB_PROJET from DOA with (nolock) where STATUT = 'E'";
        
        
        $data = $this->pdo->prepare($req);
        $data->execute();
        return  $data->fetch();
    }

    /**
     * @return array<mixed>
    */
    public function getNombreTacheEnRetard(): ?array
    {
        $req = "select count (distinct ID_TACHE) NB_TACHES
                from DOA_TACHES with (nolock)
                left join DOA with (nolock) on ID = ID_OBJET
                where TYPE_OBJET='D' and STATUT = 'E' and DOA_TACHES.DATE_FIN<'".date('Ymd')."' and TERMINEE=0x46";
        
        $data = $this->pdo->prepare($req);
        $data->execute();
        return  $data->fetch();
    }
    
    /**
     * @return array<mixed>
    */
    public function getNombreTacheEnCours(): ?array
    {
        $req = "select count (distinct ID_TACHE) NB_TACHES
                from DOA_TACHES with (nolock)
                left join DOA with (nolock) on ID = ID_OBJET
                where TYPE_OBJET='D'  and STATUT = 'E' and TERMINEE=0x46";
        
        $data = $this->pdo->prepare($req);
        $data->execute();
        return  $data->fetch();
    }
    
    /**
     * @return array<mixed>
    */
    public function getNombreProjetEnCoursParUtilisateur(): ?array
    {
        $req = "select TOP 5 PROPRIETAIRE,
                count(ID)as 'NB_PROJET' 
                from DOA with (nolock)
                where MODELE=0x46 and STATUT='E' 
                group by PROPRIETAIRE order by NB_PROJET DESC";
        
        $data = $this->pdo->prepare($req);
        $data->execute();
        return  $data->fetchAll();
    }
    
    /**
     * @return array<mixed>
    */
    public function getNombreTachesParStatut(?string $acheteur = null): ?array
    {
        $req = "select 'En attente' as 'STATUT', count (distinct ID_TACHE) NB_TACHES, '#3377CB' COULEUR
                from DOA_TACHES with (nolock)
                left join DOA with (nolock) on ID = ID_OBJET
                where TYPE_OBJET='D' and STATUT = 'E' AND TERMINEE=0x46 and DOA_TACHES.DATE_DEB>'".date('Ymd')."' ".($acheteur? " and PROPRIETAIRE = '$acheteur'":'')."
                    union
                select 'En cours' as 'STATUT', count (distinct ID_TACHE) NB_TACHES, '#53ad53' COULEUR
                from DOA_TACHES with (nolock)
                left join DOA with (nolock) on ID = ID_OBJET
                where TYPE_OBJET='D' and STATUT = 'E' AND TERMINEE=0x46  and DOA_TACHES.DATE_DEB<='".date('Ymd')."' and DOA_TACHES.DATE_FIN>='".date('Ymd')."' ".($acheteur? " and PROPRIETAIRE = '$acheteur'":'')." 
                    union
                select 'En retard' as 'STATUT', count (distinct ID_TACHE) NB_TACHES, '#d01944' COULEUR
                from DOA_TACHES with (nolock)
                left join DOA with (nolock) on ID = ID_OBJET
                where TYPE_OBJET='D' and STATUT = 'E' AND TERMINEE=0x46 and DOA_TACHES.DATE_FIN<'".date('Ymd')."' ".($acheteur? " and PROPRIETAIRE = '$acheteur'":'')."
                    union
                select 'Terminé' as 'STATUT', count (distinct ID_TACHE) NB_TACHES, '#5f5f5f' COULEUR
                from DOA_TACHES with (nolock)
                left join DOA with (nolock) on ID = ID_OBJET
                where TYPE_OBJET='D' and STATUT = 'E' AND TERMINEE=0X54 ".($acheteur? " and PROPRIETAIRE = '$acheteur'":'')."";
        
        $data = $this->pdo->prepare($req);
        $data->execute();
        return  $data->fetchAll();
    }
}
