<?php

namespace Modules\Stock\Repository;

use Helpers\QueryBuilder\AbsRepository;
use Modules\Stock\Trait\Repo\ImputationRepoTrait;
use Modules\Stock\Trait\Repo\LigneRepoTrait;

class LigneBonSortieRepo extends AbsRepository
{
    use LigneRepoTrait;
    use ImputationRepoTrait;

    /**
     * @return array<int, array<string, mixed>>
     */
    public function findAllFromNoBs(string $noBs): array
    {
        $siSectorisationJoinValue = $this->getImputationSectoJoinValue('SISECTOR', "SE.CODE_STE");

        $requete = "SELECT
                        SL.NO_BS AS NO_BS,
                        SL.NO_LIG AS NO_LIG,
                        SL.NO_DS AS NO_DS,
                        SL.NO_LIG_DS AS NO_LIG_DS,
                        SL.ART AS ART,
                        SA.DESIGN AS ART_DESIGN,
                        SA.UNITE_ACH AS UNITE_ACH,
                        SL.QTE_SORTI AS QTE_SORTI,
                        SL.PRIX_SORT AS PRIX_SORT,
                        SI.CODE_SI AS CODE_SI,
                        SI.LIBELLE AS SECTIMP_LIBELLE,
                        CGEST.CODE_AFF AS CODE_AFF,
                        CGEST.LIB_GES AS CODGES_LIBELLE,
                        CC.LIBELLE AS CODECOMP_LIBELLE,
                        CC.CODE_CDC AS CODE_CDC,
                        SCA.LIBELLE AS SSCPTANA_LIBELLE,
                        SCA.CODE_SCA AS CODE_SCA,
                        SA.UNITE_STK AS UNITE_STK
                    FROM SBS_LIGN SL
                    LEFT JOIN SBS_ENTE SE ON SL.NO_BS = SE.NO_BS
                    LEFT JOIN SARTICLE SA ON SL.ART = SA.CODE_ART
                    LEFT JOIN SECTIMPU SI ON SL.SECTIMP = SI.CODE_SI AND SI.CODE_STE = ".$siSectorisationJoinValue."
                    LEFT JOIN CODECOMP CC ON SL.CODE_COMP = CC.CODE_CDC AND CC.CODE_STE = ".$this->getImputationSectoJoinValue('CDCSECTO', "SE.CODE_STE")."
                    LEFT JOIN SSCPTANA SCA ON SL.SSCPTEANA = SCA.CODE_SCA AND SCA.CODE_STE = ".$this->getImputationSectoJoinValue('SCASECTO', "SE.CODE_STE")."
                    LEFT JOIN CODGEST CGEST ON SL.CODGES = CGEST.CODE_AFF AND CGEST.CODE_SI = SL.SECTIMP AND CGEST.CODE_STE = ".$siSectorisationJoinValue."
                    WHERE SL.NO_BS = :noBs
                    ORDER BY SL.NO_LIG ASC
        ";

        return $this->fetchAssocAll($requete, [':noBs' => $noBs]);
    }
}
