<?php

namespace Modules\Catalogue\Validator;

use Entity\Model\CataEnt;
use Helpers\Trad\ITrad;
use Helpers\Validator\Validator;

class CatalogueExterneValidator extends Validator
{
    public function __construct(\PDO $pdo, ITrad $trad)
    {
        $this->pdo = $pdo;
        $this->trad = $trad;
    }

    /**
     * @param array<mixed> $post
     * @return void
     */
    public function entete(&$post):void
    {
        $this->isStringSansEspace($this->trad->t('Référence catalogue'), $post['ID_CATAL'], 1, 30);

        $idCata = (new CataEnt($this->pdo))->find($post['ID_CATAL']);
        $this->isTrue($idCata, $this->trad->t('Ce code existe déjà.'));

        $this->isStringAvecEspace($this->trad->t('Description'), $post['DESCRIPTION_CATA'], 1, 128);
        $this->isObligatoire($post['TYPECATA'], $this->trad->t('Type catalogue'));
        if ($post['TYPECATA'] == 'F') {
            $this->zoom('Fourniss', $post['REGRFOUR_F'], $this->trad->t('Fournisseur'), $this->pdo);
            $post['REGRFOUR'] = $post['REGRFOUR_F'];
        }
        if ($post['TYPECATA'] == 'G') {
            $this->zoom('grpfou', $post['REGRFOUR_G'], $this->trad->t('Groupement fournisseur'), $this->pdo);
            $post['REGRFOUR'] = $post['REGRFOUR_G'];
        }

        $this->zoom('CFormats', $post['IDFORMAT'], $this->trad->t('ID Format'), $this->pdo, false);
        $this->isCheck($post['PRIXMODIFIABLE']);
        $this->isCheck($post['PRIXMODIFHAUSSE']);
        $this->isCheck($post['MODIFTVA']);
    }
}
