<?php declare(strict_types=1);

namespace Modules\Additional\Specifique\Client112\Modules\Facture\Repository;

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

class ParamSIContratFournisseurRepository extends AbsRepository
{
    private string $typedico = "SPEC5625";

    private string $bt12DilispecReq = "select top 1 LIB01DICO from DILISPEC where TYPEDICO = 'SPEC5265' and CODEDICO = 'BT12'";

    /**
     * Summary of findAll
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function findAll(array $post): array
    {
        $query  = <<<SQL
                SELECT
                    frs.CODE as CODE_FRS,
                    frs.RS as RS_FRS,
                    dn.LIB01DICO as REF_FACT,
                    si.CODE_SI,
                    si.LIBELLE as LIBELLE_SI
                FROM
                    DILISPECN dn
                    LEFT JOIN FOURNISS frs ON dn.CLE2 = frs.CODE
                    LEFT JOIN SECTIMPU si ON dn.CLE1 = si.CODE_SI
                WHERE
                    TYPEDICO = :typedico 
            SQL;
        $params = [
            ":typedico" => $this->typedico,
        ];

        $filter = FilterTable::splitFilter($post);
        if (\count($filter) > 0 && isset($filter[1])) {
            $query .= str_ireplace('where', 'AND', $filter[0]) . $filter['tri'];

            if (\count($filter[1]) != 0) {
                $params = [...$params, ...$filter[1]];
            }
        }

        $paginatorManagement = new PaginatorGestion();
        $paginatorManagement->injecterSqlCount($query, $post, $params, $this->pdo);

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

    /**
     * Summary of findCodeSI
     * @param string $numFact
     * @param string $codeFrs
     * @return array<mixed>
     */
    public function findCodeSI(
        int|string $numFact,
        string $codeFrs,
    ): array {
        $query1 = "select
                        VALEUR
                    from
                        CS_DON
                    where
                        COD_TAB = 'FAC_ENT'
                    and COD_ENR = :numFact
                    and NUM_FIX = ({$this->bt12DilispecReq})
                    ";

        $params1 = [
            ":numFact" => \str_pad((string) $numFact, 9, '0', STR_PAD_LEFT),
        ];

        $numContrat = $this->fetchAssocOne($query1, $params1)['VALEUR'];
        if (empty($numContrat)) {
            return [];
        }

        $query2 = <<<SQL
                            SELECT
                                dn.LIB01DICO as REF_FACT,
                                si.CODE_SI,
                                si.LIBELLE as LIBELLE_SI
                            FROM
                                DILISPECN dn
                                LEFT JOIN SECTIMPU si ON dn.CLE1 = si.CODE_SI
                            WHERE
                                TYPEDICO = :typedico
                            AND
                                dn.CLE2 = :code_frs
                            AND
                                dn.LIB01DICO = :num_contrat
                    SQL;

        $params2 = [
            ":typedico"    => $this->typedico,
            ":code_frs"    => $codeFrs,
            ":num_contrat" => $numContrat,
        ];

        return $this->fetchAssocOne($query2, $params2);
    }

    public function getBT12NumFix(): mixed
    {
        return $this->fetchAssocOne($this->bt12DilispecReq, [])['LIB01DICO'];
    }
}
