<?php

namespace Modules\Tables\Validator;

use Entity\Model\Centre;
use Helpers\Parametres\Constante;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;

class CentreValidator extends Validator
{
    /**
     * @param mixed $file
     * @param array<mixed> $post
     */
    public function validateCentre(array &$post, $file, ?string $idCentre = null) : void
    {
        // _________________
        if (!$idCentre) {
            $this->isCode($this->trad->t('Code'), $post['CODE_CTR'], 1, 7);
            $centre = new Centre($this->pdo);
            $this->isTrue($centre->find($post['CODE_CTR']), $this->trad->t('Le code centre existe déjà.'));
        }
        $this->isStringAvecEspace($this->trad->t('Raison sociale'), $post['RAIS_SOC'], 1, 45);
        $this->zoom('Societe', $post['CODE_STE'], $this->trad->t('Sociétés'), $this->pdo, false);

        // _________________
        $this->isCheck($post['EMISSION']);
        $this->isCheck($post['LIVRAISON']);
        $this->isCheck($post['FACTURAT']);

        if ($post['EMISSION']) {
            $this->zoom('Centre', $post['CTRFACLIE'], $this->trad->t('Centre de facturation'), $this->pdo, false, ['filter_egal_CODE_STE' => $post['CODE_STE'], 'filter_FACTURAT' => 'T']);
        } else {
            $post['CTRFACLIE'] = '';
        }

        if ($post['LIVRAISON']) {
            $this->zoom('SsItesto', $post['SITSTOCK'], $this->trad->t('Sites de stock par défaut'), $this->pdo, true, ['filter_egal_CODE_STE' => $post['CODE_STE']]);
            $this->zoom('DaModel', $post['PROFILDA'], $this->trad->t('Profils TVA par défaut'), $this->pdo, true);
        } else {
            $post['SITSTOCK'] = '';
            $post['PROFILDA'] = '';
        }

        if ($post['EMISSION'] || $post['LIVRAISON']) {
            $this->zoom('Hastutil', $post['ACHETEUR'], $this->trad->t('Acheteurs directs'), $this->pdo, true, [
                'filter_egal_CODE_STE'  => $post['CODE_STE'],
                'VALIDE'                => 1,
                'filter_egal_ACHETEUR'  => 'T'
            ]);
            $this->isCheck($post['BRAUTOPREAFF']);
        } else {
            $post['ACHETEUR']       = '';
            $post['BRAUTOPREAFF']   = false;
        }

        // _________________
        $this->isStringAvecEspace($this->trad->t('N° de TVA intracommunautaire'), $post['NUM_TVAIC'], 0, 15);
        $this->isStringAvecEspace($this->trad->t('Zone légale'), $post['ZONELEGAL'], 0, 200);
        $this->isStringAvecEspace($this->trad->t('Commentaire'), $post['NM_COMM'], 0, 3000);

        $this->validatorCS('CENTRE', $idCentre, $post, $this->pdo, $post['CODE_STE']);

        // _________________
    }
    
    /**
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function validateAdresse(array &$post, ?string $idCentre = null)
    {
        $this->valAdresse($post, '0');
        
        $listeAdresse = [];
        foreach ($post as $k => $v) {
            if (strpos($k, 'ADRESSE') === 0 && $k != 'ADRESSE') {
                $incide = str_replace('ADRESSE', '', $k);
                $adresse = [
                    'statut'        => $post['statut_adr'.$incide],

                    'COD_ADR'       => $post['COD_ADR'.$incide],
                    'LIBELLE'       => $post['LIBELLE'.$incide],
                    'ADRESSE'       => $post['ADRESSE'.$incide],
                    'PAYS'          => $post['PAYS'.$incide],
                    'CPOSTAL'       => $post['CPOSTAL'.$incide],
                    'VILLE1'        => $post['VILLE1'.$incide],
                    'E_MAIL'        => $post['E_MAIL'.$incide],
                    'TELEPHONE'     => $post['TELEPHONE'.$incide],
                    'FAX'           => $post['FAX'.$incide],
                ];

                if ($adresse['statut'] != 'S') {
                    $this->valAdresse($adresse, $incide);
                }

                $listeAdresse[] = $adresse;
            }
        }

        return $listeAdresse;
    }

    /**
     * @param array<mixed> $adresse
     */
    private function valAdresse(&$adresse, string $incide) :void
    {
        if ($incide == '0') {
            $libelleAdrrese = '';
            $this->zoom('TVA', $adresse['TX_TAXLOC'], $this->trad->t('Taux de taxe locale'), $this->pdo, true);
        } else {
            $libelleAdrrese = $this->trad->t('Adresse').' ';
            $this->isStringSansEspace($libelleAdrrese.$this->trad->t('Code adresse'), $adresse['COD_ADR'], 1, 3);
            if ($adresse['statut'] == 'A') {
                $this->isTrue($adresse['COD_ADR']=='REG', $this->trad->t('Le code adresse \'REG\' n\'est pas autorisé.'));
            }
            $this->isStringAvecEspace($libelleAdrrese.$this->trad->t('Raison sociale'), $adresse['LIBELLE'], 0, 30);
        }

        $this->isStringAvecEspace($libelleAdrrese.$this->trad->t('Adresse'), $adresse['ADRESSE'], 0, 90);
        $this->isFalse($adresse['PAYS'] != '', Trad::strBuild($this->trad->t('Le champ @1@ est un champ obligatoire.'), $libelleAdrrese.$this->trad->t('Pays')));
        $this->isCodePostal($libelleAdrrese.$this->trad->t('Pays'), $adresse['CPOSTAL'], $adresse['PAYS']);
        $this->isStringAvecEspace($libelleAdrrese.$this->trad->t('Ville'), $adresse['VILLE1'], 0, 40);
        $this->adresseMail($libelleAdrrese.$this->trad->t('Email'), $adresse['E_MAIL'], 0, 100);
        $this->isStringAvecEspace($libelleAdrrese.$this->trad->t('Tel'), $adresse['TELEPHONE'], 0, 16);
        $this->isStringAvecEspace($libelleAdrrese.$this->trad->t('Fax'), $adresse['FAX'], 0, 16);
    }

    /**
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function validateContact(array $post, ?string $idCentre = null)
    {
        $listeContact = [];

        foreach ($post as $k => $v) {
            if (strpos($k, 'statut_ctc') === 0) {
                $incide = str_replace('statut_ctc', '', $k);

                $libelleContact = $this->trad->t('Contact').' '.$incide;

                if ($v != 'S') {
                    $this->isStringAvecEspace($libelleContact.$this->trad->t('Nom'), $post['CTC_NOM'.$incide], 0, 25);
                    $this->adresseMail($libelleContact.$this->trad->t('Email'), $post['CTC_MAIL'.$incide], 0, 100);
                    $this->isStringAvecEspace($libelleContact.$this->trad->t('Tél. Portable'), $post['CTC_GSM'.$incide], 0, 16);
                    $this->isStringAvecEspace($libelleContact.$this->trad->t('Tél. Fixe'), $post['CTC_TEL'.$incide], 0, 16);
                    $this->isStringAvecEspace($libelleContact.$this->trad->t('Tél. fax'), $post['CTC_FAX'.$incide], 0, 16);
                    $this->isStringAvecEspace($libelleContact.$this->trad->t('Fonction'), $post['CTC_FCT'.$incide], 0, 40);

                    $this->isCheck($post['CTC_RCA'.$incide]);
                    $this->isCheck($post['CTC_ROP'.$incide]);
                    $this->isCheck($post['CTC_DARDA'.$incide]);
                    $this->isCheck($post['CTC_REN'.$incide]);
                }

                $contact = [
                    'statut'    =>  $idCentre? $post['statut_ctc'.$incide]:'A',
                    'CTC_IND'   =>  $post['id'.$incide],
                    'CTC_NOM'   =>  $post['CTC_NOM'.$incide],
                    'CTC_MAIL'  =>  $post['CTC_MAIL'.$incide],
                    'CTC_GSM'   =>  $post['CTC_GSM'.$incide],
                    'CTC_TEL'   =>  $post['CTC_TEL'.$incide],
                    'CTC_FAX'   =>  $post['CTC_FAX'.$incide],
                    'CTC_FCT'   =>  $post['CTC_FCT'.$incide],

                    'CTC_RCA'   =>  $post['CTC_RCA'.$incide],
                    'CTC_ROP'   =>  $post['CTC_ROP'.$incide],
                    'CTC_DARDA' =>  $post['CTC_DARDA'.$incide],
                    'CTC_REN'   =>  $post['CTC_REN'.$incide],
                ];

                $listeContact[] = $contact;
            }
        }
        
        return $listeContact;
    }

    /**
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function clearZoneGeo($post)
    {
        $listeZoneGeo = [];
        
        foreach ($post as $k => $v) {
            if (strpos($k, 'zoneGeo_') === 0) {
                $clearK   = str_replace('zoneGeo_', '', $k);
                $explodeK = explode(Constante::spliter, $clearK);

                $listeZoneGeo[] = [
                    'DCOUP'     => $explodeK[0],
                    'ZONEGEO'   => $explodeK[1],
                ];
            }
        }

        return $listeZoneGeo;
    }
}
