<?php

namespace Modules\Stock\Repository;

use Exception;
use Helpers\Filter\FilterTable;
use Helpers\Outil\Roles;
use Helpers\Pagination\PaginatorGestion;
use Helpers\QueryBuilder\AbsRepository;

class DemandeAchatRepo extends AbsRepository
{
    /**
     * @param array<string,mixed> $post
     * @return array<int,array<string,string>>
     * @throws Exception
     */
    public function listDemandesAchat(array $post): array
    {
        $arrayReq = [];
        $requete = "SELECT * FROM (
                        SELECT 
                            NO_DA, CODE_STE, NO_DEM, DTE_VAL, CTR_LIV, 
                            CASE 
                                WHEN TRAITEE = 0x54 THEN 'T'
                                WHEN SIGNEE = 0x54 AND TRAITEE = 0x46 THEN 'S'
                                WHEN REFUSEE = 0x54 THEN 'R'
                                ELSE 'E'
                            END AS STATUT
                        FROM DA_ENTET with (nolock)
                        WHERE 1=1 ".Roles::getClausePerimetreSociete('DA_ENTET.CODE_STE', $arrayReq)."
                    ) AS DAENTET ";

        $filters = FilterTable::splitFilter($post);
        $filters['param'] = array_merge($filters['param'], $arrayReq);
        $requete .= $filters['where'];
        $requete .= $filters['tri'];

        $paginatorGestion = new PaginatorGestion();
        $paginatorGestion->injecterSqlCount($requete, $post, $filters['param'], $this->pdo);

        return $this->fetchAssocAll($requete, $filters['param']);
    }
}
