<?php

namespace Modules\Additional\Specifique\Client160\Modules\Fournisseur\Repository;

use Helpers\Filter\FilterTable;
use Helpers\QueryBuilder\AbsRepository;

class QualityReview extends AbsRepository
{
    /**
     * getListNonConf
     *
     * @param  array<mixed> $post
     * @return array<mixed>|false
     */
    public function getListNonConfHeading(array $post = [])
    {
        $query = "SELECT 
                    ent.*, f.CODE AS CODE_FOURNISS, f.RS, c.RAIS_SOC, 
                    tdgrp.LIB01DICO AS LIBELLE_GROUPE,  
                    tdFam.LIB01DICO AS LIBELLE_FAMILLE,
                    (SELECT FORMAT((CAST( ZQMDAT as date)),'yyyyMMdd')) orderZQMDAT
                FROM CUNION_NONCONF_ENT ent with(nolock)
                LEFT JOIN FOURNISS f with(nolock) ON f.ID_EXT = ent.ZLIFNUM
                LEFT JOIN CENTRE c with(nolock) ON c.CODE_CTR = ent.ZMAWERK
                LEFT JOIN TS_DON tdgrp with(nolock) ON tdgrp.CODEDICO = ent.ZBKGRP AND tdgrp.TYPEDICO = 'GRP_ACHETEURS'
                LEFT JOIN TS_DON tdFam with(nolock) ON tdFam.CODEDICO = ent.ZZCODENC AND tdFam.TYPEDICO = 'FAMILLE_NONCONF'
        ";

        /**
         * Prise en compte de la pagination et des filtres
         */
        $whereArray = FilterTable::splitFilter($post, 0, 0, 0);
        $filtreAvance = new \Helpers\Filter\FiltreAvance\Generator($post);
        $query .= $filtreAvance->generateSql(0);
        $filtreAvance->pushArray($params);

        if (count($whereArray) >  0) {
            if ($whereArray['tri'] ?? '' != '') {
                $query .= $whereArray[0] . $whereArray['tri'];
            } else {
                $query .= $whereArray[0];
            }

            if (count($whereArray[1]) != 0) {
                $params = $whereArray[1];
            }
        }

        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($query, $post, $params, $this->pdo, true);

        return $this->fetchAssocAll($query, $params);
    }
    
    /**
     * getListNonConfLines
     *
     * @return array<mixed>|false
     */
    public function getListNonConfLines()
    {
        $query = "SELECT lig.*, f.CODE AS CODE_FOURNISS, f.RS, c.RAIS_SOC, tdgrp.LIB01DICO AS LIBELLE_GROUPE,  tdFam.LIB01DICO AS LIBELLE_FAMILLE,  tdGrpCod.LIB01DICO AS LIBELLE_GROUPE_CODE,  tdClasse.LIB01DICO AS LIBELLE_CLASSE_DEFAUT
        FROM CUNION_NONCONF_LIG lig with(nolock)
            LEFT JOIN CUNION_NONCONF_ENT ent with(nolock) ON lig.ZQMNUM = ent.ZQMNUM
            LEFT JOIN FOURNISS f with(nolock) ON f.ID_EXT = ent.ZLIFNUM
            LEFT JOIN CENTRE c with(nolock) ON c.CODE_CTR = ent.ZMAWERK
            LEFT JOIN TS_DON tdgrp with(nolock) ON tdgrp.CODEDICO = ent.ZBKGRP AND tdgrp.TYPEDICO = 'GRP_ACHETEURS' -- (SELECT LIB01DICO FROM DILISPEC WITH(NOLOCK) WHERE TYPEDICO='CUNION_PAR' AND CODEDICO='GRP_ACHETEURS')
            LEFT JOIN TS_DON tdFam with(nolock) ON tdFam.CODEDICO = ent.ZZCODENC AND tdFam.TYPEDICO = 'FAMILLE_NONCONF' --(SELECT LIB01DICO FROM DILISPEC WITH(NOLOCK) WHERE TYPEDICO='CUNION_PAR' AND CODEDICO='FAMILLE_NONCONF')
            LEFT JOIN TS_DON tdGrpCod with(nolock) ON tdGrpCod.CODEDICO = lig.ZFEGRP AND tdGrpCod.TYPEDICO = 'GRP_CODES' --(SELECT LIB01DICO FROM DILISPEC WITH(NOLOCK) WHERE TYPEDICO='CUNION_PAR' AND CODEDICO='GRP_CODES')
            LEFT JOIN TS_DON tdClasse with(nolock) ON tdClasse.CODEDICO = lig.ZFEQKLAS AND tdClasse.TYPEDICO = 'CLASSE_DEFAUT' --(SELECT LIB01DICO FROM DILISPEC WITH(NOLOCK) WHERE TYPEDICO='CUNION_PAR' AND CODEDICO='CLASSE_DEFAUT')
        ";

        return $this->fetchAssocAll($query, []);
    }
}
