<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;
use Helpers\Trad\Trad;

class CaLigne extends QueryBuilder
{
    protected string $table = 'CA_LIGNE with (nolock)';
    protected string $primaryKey = '';
    protected array $primKey = ['NO_CDE', 'NO_PO'];

    /**
     * @return array<mixed>
     */
    public function getArchitecture(\Helpers\Trad\ITrad $trad)
    {
        return  [
            'NO_CDE'                => ['nomChamp' => $trad->t('Numéro commande')],
            'NO_PO'                 => ['nomChamp' => $trad->t('Numéro ligne')],
            'CODE_ART'              => ['nomChamp' => $trad->t('Code article')],
            'DES_SOMM'              => ['nomChamp' => $trad->t('Désignation sommaire')],
            'NM_DCOMP'              => ['nomChamp' => $trad->t('Désignation complémentaire'), 'type' => 'MEMO'],
            'SSFAM'                 => ['nomChamp' => $trad->t('Sous-famille')],
            'PU'                    => ['nomChamp' => $trad->t('Prix unitaire'), 'type' => 'MONTANT'],
            'TVA'                   => ['nomChamp' => $trad->t('Taux de TVA'), 'type' => 'MONTANT'],
            'REMISE'                => ['nomChamp' => $trad->t('Remise')],
            'QTE_COMM'              => ['nomChamp' => $trad->t('Quantité commandée'), 'type' => 'QTE'],
            'UNITE'                 => ['nomChamp' => $trad->t('Unité')],
            'RAL'                   => ['nomChamp' => $trad->t('Reste à livrer')],
            'SECTIMP'               => ['nomChamp' => $trad->t('Section d\'imputation')],
            'CODE_COMP'             => ['nomChamp' => $trad->t('Code complémentaire')],
            'SSCPTEANA'             => ['nomChamp' => $trad->t('Sous-compte d\'analyse')],
            'DTELIVCON'             => ['nomChamp' => $trad->t('Date de livraison'), 'type' => 'DATE'],
            'DTELIVPRE'             => ['nomChamp' => $trad->t('Date prévisionnelle'), 'type' => 'DATE'],
            'NO_DA'                 => ['nomChamp' => $trad->t('Numéro de demande d\'achat')],
            'NO_PO_DA'              => ['nomChamp' => $trad->t('Numéro de ligne de demande d\'achat')],
            'QTE_FACTU'             => ['nomChamp' => $trad->t('Quantité facturée'), 'type' => 'QTE'],
            'SITE_STK'              => ['nomChamp' => $trad->t('Site de stock')],
            'CTRL_BR'               => ['nomChamp' => $trad->t('Contrôle qualité')],
            'SAISIEREC'             => ['nomChamp' => $trad->t('Saisie de réception'), 'type' => 'RADIO'],
            'TOLLIVAVA'             => ['nomChamp' => $trad->t('Livraison avancée tolérée')],
            'CODE_STE'              => ['nomChamp' => $trad->t('Code société')],
            'TITREAV'               => ['nomChamp' => $trad->t('Titre avant la ligne'), 'type' => 'MEMO'],
            'SSTOAPR'               => ['nomChamp' => $trad->t('Imprimer un sous-total après cette ligne')],
            'ACCORCA'               => ['nomChamp' => $trad->t('Accord-cadre')],
            
            'POIDS'                 => ['nomChamp' => $trad->t('Poids')],
            'LONGU'                 => ['nomChamp' => $trad->t('Longueur')],
            'LARG'                  => ['nomChamp' => $trad->t('Largeur')],
            'HAUT'                  => ['nomChamp' => $trad->t('Hauteur')],
            'VOLUME'                => ['nomChamp' => $trad->t('Volume')],
            'PAY_ORI'               => ['nomChamp' => $trad->t('Pays origine')],
            'PAY_PRO'               => ['nomChamp' => $trad->t('Pays de provenance')],
            'DOUANE'                => ['nomChamp' => $trad->t('Douane')],
            
            'MASQ_QTE'              => ['nomChamp' => $trad->t('Masquer la quantité'), 'type' => 'RADIO'],
            'MASQ_UNI'              => ['nomChamp' => $trad->t('Masquer l\'unité'), 'type' => 'RADIO'],
            'MASQ_PU'               => ['nomChamp' => $trad->t('Masquer le prix unitaire'), 'type' => 'RADIO'],
            'MASQ_DS'               => ['nomChamp' => $trad->t('Masquer la désignation sommaire'), 'type' => 'RADIO'],
    
            'POIDSNET'              => ['nomChamp' => $trad->t('Poids net')],
            'CODGES'                => ['nomChamp' => $trad->t('Code gestion')],
            'LIBGES'                => ['nomChamp' => $trad->t('Libellé gestion')],
            'REF_FOUR'              => ['nomChamp' => $trad->t('Référence fournisseur')],
            'COD_FAB'               => ['nomChamp' => $trad->t('Code fabricant')],
            'COD_GAM'               => ['nomChamp' => $trad->t('Code gamme')],
            'REF_FAB'               => ['nomChamp' => $trad->t('Référence fabricant')],
            'ID_CG'                 => ['nomChamp' => $trad->t('Id Code gestion')],
            'SOLD_PHYS'             => ['nomChamp' => $trad->t('Soldée physiquement'), 'type' => 'RADIO'],
            'RECEP_AUTO'            => ['nomChamp' => $trad->t('Réception automatique'), 'type' => 'RADIO'],
            'DATEDEBCONT'           => ['nomChamp' => $trad->t('Début contrat'), 'type' => 'DATE'],
            'DATEFINCONT'           => ['nomChamp' => $trad->t('Fin contrat'), 'type' => 'DATE'],
            'CONTRAT_PERIOD'        => ['nomChamp' => $trad->t('Périodicité')],
            'FIN_DEB_PER'           => ['nomChamp' => $trad->t('Période')],
            'CALI_RECEPAUTO_JOUR'   => ['nomChamp' => $trad->t('Jour d\'application')],
            'ID_EXTERNE'            => ['nomChamp' => $trad->t('Id externe')],
            'ETAT_FIN'              => ['nomChamp' => $trad->t('Etat financier')],
        ];
    }

    /**
     * @return array<mixed>
     */
    public function findEntity(string $commandeId, string $ligneId) :array
    {
        return $this->find([
            'NO_CDE' => $commandeId,
            'NO_PO' => $ligneId
        ]);
    }

    public function getMaxNoPo(string $idCommande) : int
    {
        $req = "SELECT MAX(NO_PO) MAX_NO_PO from CA_LIGNE with(nolock) where NO_CDE = :idCommande";
        $arrayReq = [
            ':idCommande' => $idCommande
        ];

        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);

        return (int)($requete->fetch()['MAX_NO_PO']??0);
    }
}
