<?php

namespace Modules\Reception\Repository;

use Helpers\Outil\Roles;
use Helpers\QueryBuilder\AbsRepository;

class ControleQualite extends AbsRepository
{
    /**
     * @return array<mixed>
     */
    public function getControleQualite()
    {
        $arrayReq = [];

        $req = "SELECT DISTINCT BR_LIGNE.NUM_BR, 
                BR_LIGNE.TYPE_CTRL, CQUALITE.LIBELLE, 
                CA_ENTET.CODE_FOU, FOURNISS.RS, 
                BR_RECEP.NUM_CDE, BR_RECEP.DATE_BL, 
                BR_RECEP.TOTAL_HT, CA_ENTET.DEVISE
                from BR_LIGNE with(nolock)
                LEFT JOIN CQUALITE with (nolock) on CQUALITE.CODE_CTRL = BR_LIGNE.TYPE_CTRL
                left join BR_RECEP with (nolock) on BR_RECEP.NUM_BR = BR_LIGNE.NUM_BR
                left join SOCIETE with (nolock) on SOCIETE.CODE_STE = BR_RECEP.CODE_STE
                left join CA_ENTET with (nolock) on CA_ENTET.NO_CDE = BR_RECEP.NUM_CDE
                left join FOURNISS with (nolock) on FOURNISS.CODE = CA_ENTET.CODE_FOU
                where  BR_LIGNE.A_CONTROL = 0x54
                and    BR_LIGNE.CTRL_FAIT = 0x46
                and    BR_LIGNE.BR_VAL    = 0x46
                " . (Roles::getClausePerimetreSociete('BR_RECEP.CODE_STE', $arrayReq)) . "
                " . (Roles::getClauseSocieteOuvert('SOCIETE')) . "
                order by TYPE_CTRL, BR_RECEP.DATE_BL";

        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * @return array<mixed>
     */
    public function getListeLigneCQByType(string $idDoc, string $typeCtrl = null)
    {
        $parametrage = [
            'sisecto' => Roles::droit($this->pdo, 'SISECTOR', 'PARAMSTE'),
            'scaSecto' => Roles::droit($this->pdo, 'SCASECTO', 'PARAMSTE'),
            'cdcSecto' => Roles::droit($this->pdo, 'CDCSECTO', 'PARAMSTE'),
        ];

        $req = "SELECT 
                NO_PO_BR, DESIGN, QTE_RECUE, QTE_COMPT, QTE_ACCEP,
                CA_LIGNE.SECTIMP, CA_LIGNE.CODE_COMP, CA_LIGNE.SSCPTEANA, CA_LIGNE.UNITE,
                SECTIMPU.LIBELLE LIB_SI, CODECOMP.LIBELLE LIB_CDC, SSCPTANA.LIBELLE LIB_SCA
                from BR_LIGNE with (nolock)
                left join CA_LIGNE with (nolock) on CA_LIGNE.NO_PO = BR_LIGNE.NO_PO_CDE and CA_LIGNE.NO_CDE = BR_LIGNE.NUM_CDE
                left join SECTIMPU with (nolock) on SECTIMPU.CODE_SI = CA_LIGNE.SECTIMP 
                    and SECTIMPU.CODE_STE =  " . ($parametrage['sisecto'] ? "CA_LIGNE.CODE_STE" : "''") . " 
                left join CODECOMP with (nolock) on CODECOMP.CODE_CDC = CA_LIGNE.CODE_COMP 
                    and CODECOMP.CODE_STE =  " . ($parametrage['cdcSecto'] ? "CA_LIGNE.CODE_STE" : "''") . " 
                left join SSCPTANA with (nolock) on SSCPTANA.CODE_SCA = CA_LIGNE.SSCPTEANA and SSCPTANA.CODE_STE =  " . ($parametrage['scaSecto'] ? "CA_LIGNE.CODE_STE" : "''") . " 
                where NUM_BR = :idDoc and TYPE_CTRL = " . ($typeCtrl ? ":typeCtrl" : "''") . " and CTRL_FAIT = 0x46 and A_CONTROL = 0x54";

        $arrayReq[':idDoc'] = $idDoc;

        if ($typeCtrl) {
            $arrayReq[':typeCtrl'] = $typeCtrl;
        }

        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * @return array<mixed>
     */
    public function getListeLigneCQ(string $idDoc)
    {
        $req = "SELECT 
                NO_PO_BR, CA_LIGNE.DES_SOMM, QTE_RECUE, QTE_COMPT, QTE_ACCEP, CA_LIGNE.CODE_ART,CA_LIGNE.NO_PO,CA_ENTET.DEVISE,BR_LIGNE.TYPE_CTRL,
                CA_LIGNE.SECTIMP, CA_LIGNE.CODE_COMP, CA_LIGNE.SSCPTEANA, CA_LIGNE.UNITE, CA_LIGNE.RAL,CA_LIGNE.PU, CA_LIGNE.REMISE,CQUALITE.LIBELLE
                from BR_LIGNE with (nolock)
                left join CA_LIGNE with (nolock) on CA_LIGNE.NO_PO = BR_LIGNE.NO_PO_CDE and CA_LIGNE.NO_CDE = BR_LIGNE.NUM_CDE
                left join CA_ENTET with (nolock) on CA_ENTET.NO_CDE = CA_LIGNE.NO_CDE
                LEFT JOIN CQUALITE with (nolock) on CQUALITE.CODE_CTRL = BR_LIGNE.TYPE_CTRL
                where NUM_BR = :idDoc and CTRL_FAIT = 0x46 and A_CONTROL = 0x54";

        $arrayReq[':idDoc'] = $idDoc;

        return $this->fetchAssocAll($req, $arrayReq);
    }

    /**
     * @param string $idDoc
     * @return array<mixed>
     */
    public function getListeBrForIncident(string $idDoc)
    {
        $req = "SELECT CA_ENTET.CODE_FOU, 'niveau' NIVEAU, BR_RECEP.DATE_BL, 'REFUS' CODE, '' DESCRIPT, 
                BR_LIGNE.NUM_CDE, BR_LIGNE.NO_PO_CDE, BR_LIGNE.NUM_BR, BR_LIGNE.NO_PO_BR,
                BR_LIGNE.CODE_ART, BR_LIGNE.DESIGN, 'QteRef' QTE_REFU,BR_RECEP.DATE_BL, CA_ENTET.DTE_CREAT, 'memoCde' NM_COMM
                from BR_LIGNE with (nolock)
                left join BR_RECEP with (nolock) on BR_RECEP.NUM_BR = BR_LIGNE.NUM_BR
                left join CA_ENTET with (nolock) on CA_ENTET.NO_CDE = BR_LIGNE.NUM_CDE
                where BR_LIGNE.NUM_BR = :idDoc";

        $arrayReq[':idDoc'] = $idDoc;

        return $this->fetchAssocAll($req, $arrayReq);
    }
}
