<?php

namespace Modules\Ajax\Controller;

use Modules\GlobalApplication\Repository\Societe;
use Skeleton\Controller\Controller;
use GuzzleHttp\Psr7\Response;
use Helpers\Log\LogSysteme;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;

class ValidatorController extends Controller
{
    /**
     * @return Response
     */
    public function isStringSansEspaceAction()
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $sizeMin = $post['sizeMin'] ?? '0';
        $sizeMax = $post['sizeMax'] ?? '200';
        
        $validator = $this->createValidator();
        $validator->isStringSansEspace($nomChamp, $post['value'], $sizeMin, $sizeMax);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }

    /**
     * @return Response
     */
    public function isCodeAction(): Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $sizeMin = $post['sizeMin'] ?? '0';
        $sizeMax = $post['sizeMax'] ?? '200';

        $validator = $this->createValidator();
        $validator->isCode($nomChamp, $post['value'], $sizeMin, $sizeMax);

        return new Response(200, [

        ], $validator->getError());
    }

    public function isStringAvecEspaceAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $sizeMin = $post['sizeMin'] ?? '0';
        $sizeMax = $post['sizeMax'] ?? '250';

        $validator = $this->createValidator();
        $validator->isStringAvecEspace($nomChamp, $post['value'], $sizeMin, $sizeMax);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }

    public function zoomAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $messageSpecifique = $post['messageSpecifique'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        if (empty($post['entity'])) {
            LogSysteme::prefixErrorLog((string) new \Exception('Missing entity for "'.$nomChamp.'" (Referer:' . $_SERVER['HTTP_REFERER'].')'));
        }

        $validator = $this->createValidator();
        $validator->zoom(
            $post['entity'],
            mb_strtoupper($post['value']),
            $nomChamp,
            $this->pdo,
            $null,
            ($post['post']??[]),
            $messageSpecifique
        );
        
        if ($validator->isValid()) {
            return new Response(200, [], $validator->getError());
        } elseif ($validator->isNonBloquant()) {
            return new Response(200, [], $validator->getErrorNonBloquant(true));
        } else {
            return new Response(200, [], $validator->getError());
        }
    }

    public function zoomListeAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $messageSpecifique = $post['messageSpecifique'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        if (empty($post['entity'])) {
            LogSysteme::prefixErrorLog((string) new \Exception('Missing entity (Referer:' . $_SERVER['HTTP_REFERER'].')'));
        }

        $validator = $this->createValidator();
        $validator->zoomListe(
            $post['zoom_path'],
            // @phpstan-ignore-next-line
            "\\Entity\\Model\\".trim($post['entity']), // TODO : deduce entity from path
            $post['value'],
            $nomChamp,
            $this->pdo,
            $null,
            ($post['post']??[])
        );
        
        if (! $validator->isValid()) {
            return new Response(200, [], $validator->getError());
        }
        return new Response(200, [], '');
    }
    
    /**
     * uniteAction
     *
     * @return Response
     */
    public function uniteAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $messageSpecifique = $post['messageSpecifique'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        $validator = $this->createValidator();
        $validator->uniteZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, ($post['post']??[]));
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } elseif ($validator->isNonBloquant()) {
            return new Response(200, [
                
            ], $validator->getErrorNonBloquant(true));
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function zoomRequeteAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $messageSpecifique = $post['messageSpecifique'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;

        $validator = $this->createValidator();
        $validator->zoomRequete(mb_strtoupper($post['value']), $nomChamp, $post['requete'], $this->pdo, $null, ($post['post']??[]));
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function dateAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $minDate = $post['dateMin'] ?? '';
        $maxDate = $post['dateMax'] ?? '';
        $null = ($post['null']??true)=='true';
        
        $validator = $this->createValidator();
        $validator->isDate($post['value'], $minDate, $maxDate, $null, $nomChamp);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    public function floatAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $format = $post['format'] ?? 13.2;
        $obligatoire = $post['obligatoire'] ?? false;
        $max = $post['max'] ?? null;
        $min = $post['min'] ?? null;

        $validator = $this->createValidator();
        $validator->isFloat($nomChamp, $post['value'], $format, $obligatoire, $max, $min);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    public function integerAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $sizeMin = $post['sizeMin'] ?? 0;
        $sizeMax = $post['sizeMax'] ?? 200;
        $controleByValue = $post['controleByValue'] ?? false;
        
        $validator = $this->createValidator();

  

        $validator->isInteger($nomChamp, $post['value'] ?? '', $sizeMin, $sizeMax, $controleByValue);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function societeAction() :Response
    {

        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        $nomChamp = $post['nomChamp'] ?? '';
        
        if (! Societe::hasMultiSoc()) {
            $societe = new \Modules\Ajax\Repository\Validator($this->pdo);
            $societe = $societe->societe(mb_strtoupper($post['value']));
            if (!is_array($societe)) {
                $message = '<br />'.$trad->t('Erreur sur la société');
                return new Response(200, [
                    
                ], $message);
            } else {
                return new Response(200, [
                    
                ], '');
            }
        } else {
            $validator = $this->createValidator();
            $validator->zoom('Societe', mb_strtoupper($post['value']), $nomChamp, $this->pdo);
            if ($validator->getError()!='') {
                return new Response(200, [
                    
                ], $validator->getError());
            } else {
                return new Response(200, [
                    
                ], '');
            }
        }
    }
    
    public function numeroCdeAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        if ($post['value'] !='') {
            $cde = new \Modules\Ajax\Repository\Validator($this->pdo);
            $cde = $cde->numeroDeCommande($post['value'], $post);
            if (!isset($cde['NO_CDE'])) {
                $message = '<br />'.$trad->t('Erreur sur le numéro de commande');
                return new Response(200, [
                    
                ], $message);
            } else {
                return new Response(200, [
                    
                ], '');
            }
        } else {
            return new Response(200, [
                
            ], '');
        }
    }
    
    public function factureFournisseurAction() :Response
    {
        
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $factFouOrigin = $post['FACT_FOU_ORIGIN'] ?? '';
        $sizeMin = $post['sizeMin'] ?? 0;
        $sizeMax = $post['sizeMax'] ?? 60;
        $trad = $this->getTranslationService();

        if ($sizeMin!= 0 && $post['value']=='') {
            return new Response(200, [
                
            ], '<br />'.Trad::strBuild($trad->t('Le champ @1@ est un champ obligatoire.'), $nomChamp));
        }
        
        $validator = $this->createValidator();
        if (!$validator->size($post['value'], $nomChamp, $sizeMin, $sizeMax)) {
            return new Response(200, [], $validator->getError());
        }

        $factEnt = new \Modules\Ajax\Repository\Validator($this->pdo);
        if ($factEnt->factureFournisseur($post['value'], $post['codeFournisseur'], $factFouOrigin)) {
            return new Response(200, [
                
            ], '');
        }
        $message = '<br />'.$trad->t('Le numéro doit être unique');
        return new Response(200, [
            
        ], $message);
    }
    
    public function dateFafouAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $date = $post['value'];
        $null = $post['null'] ?? false;
        
        $validator = $this->createValidator();
        $validator->isDate($date, '', '', $null);
        if ($validator->isValid()) {
            $CreationFacture = new \Modules\Facture\Repository\CreationFacture($this->pdo);
            if (($post['value']== '' && $null) || $CreationFacture->dateFafou($date)) {
                return new Response(200, [
                    
                ], '');
            } else {
                $message = '<br />'.$trad->t('La date n\'est pas dans la période');
                return new Response(200, [
                    
                ], $message);
            }
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function deviseAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        $devise = new \Modules\Ajax\Repository\Validator($this->pdo);
        if ($devise->devise($post['value'])) {
            return new Response(200, [
                
            ], '');
        } else {
            $message = '<br />'.$trad->t('Erreur sur la devise');
            return new Response(200, [
                
            ], $message);
        }
    }
    
    public function ibanAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $IBANPAYS = $post['IBANPAYS'] ?? '';
        $IBANCLE = $post['IBANCLE'] ?? '';
        
        $iban = $IBANPAYS.$IBANCLE.$post['value'];
        
        $validator = $this->createValidator();
        $validator->checkIban($nomChamp, $iban);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function bicAction() :Response
    {
        $post = $this->request->getParsedBody();
        
        $nomChamp = $post['nomChamp'] ?? '';
        $sizeMin = $post['sizeMin'] ?? '0';
        $sizeMax = $post['sizeMax'] ?? '11';
        
        $validator = $this->createValidator();
        $validator->checkBic($nomChamp, $post['value'], $sizeMin, $sizeMax);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function sectionImputationAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        $validator = $this->createValidator();
        $validator->sectionImputationZoom($post['value'], $nomChamp, $this->pdo, $null, $post);
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function codeGestionAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && ($post['null'] == '1' || $post['null']=='true'))? true : false;
        
        if (($null && $post['value']=='' ) || (($post['CODE_SI']??'') == '')) {
            return new Response(200, [], '');
        }
        
        if ((!$post['value'] || $post['value']=='') && !$null) {
            $message = '<br />'.Trad::strBuild($trad->t('Le champ @1@ ne peut pas être vide.'), $nomChamp);
            return new Response(200, [
                
            ], $message);
        }
        
        if (isset($post['forceValidationParId'])) {
            $post['validationParId'] = (int) $post['forceValidationParId'];
        } elseif (!isset($post['validationParId'])) {
            $post['validationParId'] = (\Helpers\Outil\Roles::droit($this->pdo, 'CODE_GESTION_PAR_ID') ? 1 : 0);
        } else {
            $post['validationParId'] = 1;
        }
            
        $validator = new \Modules\Ajax\Repository\Validator($this->pdo);
        if (! $validator->cg(
            mb_strtoupper($post['value']),
            $post['CODE_SI'],
            $post['NUM_LOT']??'',
            $post['AUTHACHAT']??0,
            $post['CODE_STE']??'',
            $post['valide_obligatoire']??0,
            $post['validationParId'],
        )) {
            $message = '<br />'.$trad->t('Erreur sur le code gestion');
            return new Response(200, [], $message);
        }
        
        return new Response(200, [], '');
    }
    
    public function codeComplementaireAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
                
        $validator = $this->createValidator();
        $validator->codeComplementaireZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, $post);
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function sousCompteAnalyseAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        $validator = $this->createValidator();
        $validator->sousCompteAnalyseZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, $post);
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function directionAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        if ($null && $post['value']=='') {
            return new Response(200, [
                
            ], '');
        } else {
            $validator = new \Modules\Ajax\Repository\Validator($this->pdo);
            if ($validator->directio(mb_strtoupper($post['value']), $post['CODE_STE']??'')) {
                return new Response(200, [
                    
                ], '');
            } else {
                $message = '<br />'.$trad->t('Erreur sur la direction');
                return new Response(200, [
                    
                ], $message);
            }
        }
    }
    
    public function natureAnalytiqueAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();

        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        if ($null && $post['value']=='') {
            return new Response(200, [
                
            ], '');
        } else {
            $validator = new \Modules\Ajax\Repository\Validator($this->pdo);
            if ($validator->natana(mb_strtoupper($post['value']), $post['CODE_STE']??'')) {
                return new Response(200, [
                    
                ], '');
            } else {
                $message = '<br />'.$trad->t('Erreur sur la nature analytique');
                return new Response(200, [
                    
                ], $message);
            }
        }
    }
    
    public function centreAnalyseAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        if ($null && $post['value']=='') {
            return new Response(200, [
                
            ], '');
        } else {
            $validator = new \Modules\Ajax\Repository\Validator($this->pdo);
            if ($validator->centrana(mb_strtoupper($post['value']), $post['CODE_STE']??'')) {
                return new Response(200, [
                    
                ], '');
            } else {
                $message = '<br />'.$trad->t('Erreur sur le centre analyse');
                return new Response(200, [
                    
                ], $message);
            }
        }
    }
    
    public function datePerAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        $nomChamp = $post['nomChamp'] ?? '';
        
        $date = $post['value'];
        
        $validator = $this->createValidator();
        $validator->isDate($date);
        if ($validator->isValid()) {
            $datper = new \Modules\Ajax\Repository\Validator($this->pdo);
            if ($datper->dateper($date)) {
                return new Response(200, [
                    
                ], '');
            } else {
                return new Response(200, [
                    
                ], '<br />'.Trad::strBuild($trad->t('La date @1@ : @2@ n\'est pas possible.'), $nomChamp, $date));
            }
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function isStringWithContentAction() :Response
    {
        $post = $this->request->getParsedBody();
        
        $nomChamp = $post['nomChamp'] ?? '';
        $sansEspace = $post['nomChamp'] ?? false;
        $separation = $post['separation'] ?? false;
        $contentAutorise = $post['contentAutorise'] ?? '';
        $contentNonAutorise = $post['contentNonAutorise'] ?? '';
        $contentObligatoire = $post['contentObligatoire'] ?? '';
        $sizeMin = $post['sizeMin'] ?? '200';
        $sizeMax = $post['sizeMax'] ?? '200';
        
        $validator = $this->createValidator();
        $validator->isStringWithContent($nomChamp, $post['value'], $sansEspace, $contentAutorise, $contentNonAutorise, $contentObligatoire, $sizeMin, $sizeMax, $separation);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function isHoursWithSecondeAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $obligatoire = $post['obligatoire'] ?? false;
        
        $validator = $this->createValidator();
        $validator->isHourWithSeconde($nomChamp, $post['value'], $obligatoire);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function isHoursWithSecondeCCAction(): Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';

        $validator = $this->createValidator();
        $checkedValue = str_replace(':', '', $post['value']);
        $validator->isStringSansEspace($nomChamp, $checkedValue, 6, 6, Trad::strBuild($this->getTranslationService()->t('Le champ @1@ n\'est pas valide, il doit être sous forme HH:MM:SS ou HHMMSS.'), $nomChamp));
        
        if ($validator->isValid()) {
            return new Response(200, [], '');
        } else {
            return new Response(200, [], $validator->getError());
        }
    }

    public function isHoursAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        
        
        $validator = $this->createValidator();
        $validator->isHours($nomChamp, $post['value']);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function demandeurAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;

        $validator = $this->createValidator();
        $validator->demandeurZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, ($post['post']??[]));
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function acheteurAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (($post['null'] ?? null)=='true');

        $validator = $this->createValidator();
        $validator->acheteurZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, ($post['post']??[]));
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function utilisateurAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;

        $validator = $this->createValidator();
        $validator->utilisateurZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, ($post['post']??[]));
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function accordCadreAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;

        $validator = $this->createValidator();
        $validator->accordCadreZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, ($post['post']??[]));
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function fournisseurAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        $validator = $this->createValidator();
        $validator->fournisseurZoom(mb_strtoupper($post['value']), $nomChamp, $this->pdo, $null, $post);
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function numeroDemandeAchatAction() :Response
    {
        
        $post = $this->request->getParsedBody();
        
        $DaEntet = new \Entity\Model\DaEntet($this->pdo);
        $validator = $this->createValidator();
        $trad = $this->getTranslationService();

        if ($validator->isInteger($trad->t('Numéro DA'), $post['value'], $post['sizeMin'], $post['sizeMax'])) {
            $validator->isTrue(is_array($DaEntet->find($post['value'])), '<br />'.$trad->t('Le numéro de DA saisi existe déjà.'));
        }

        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function siteStockAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        $validator = $this->createValidator();
        if (isset($post['dte_art_ouvert']) && $post['dte_art_ouvert']!='') {
            $validator->isDate($post['dte_art_ouvert']);
        }
        $validator->siteStockZoom(mb_strtoupper($post['value']), $nomChamp, $null, $post);
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }

    public function quantiteLigneDAAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $format = $post['format'] ?? 13.2;
        $obligatoire = $post['obligatoire'] ?? false;
        $max = $post['max'] ?? null;
        $trad = $this->getTranslationService();
        
        $validator = $this->createValidator();
        if ($validator->isFloat($nomChamp, $post['value'], $format, $obligatoire, $max)) {
            $validator->infSupCoherence($post['value'], $post['DEJA_COMM'], Trad::strBuild($trad->t('La quantité demandée @1@ ne doit pas être inférieur à la quantité déjà commandée @2@.'), $post['value'], $post['DEJA_COMM']));
        }
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }

    public function quantiteConditionnementAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $format = $post['format'] ?? 13.2;
        $obligatoire = $post['obligatoire'] ?? false;
        $max = $post['max'] ?? null;
        $trad = $this->getTranslationService();

        
        if ($post['value']=='') {
            $post['value'] = 0;
        }
        
        $validator = $this->createValidator();
        if ($validator->isFloat($nomChamp, $post['value'], $format, $obligatoire, $max)) {
            $QteConditionnement = $post['QteConditionnement'];
            $ValeurUniteDAArticle = $post['ValeurUniteDAArticle'];
            $unite = $post['UNITE'];
            $uniteArticle = $post['uniteArticle'];
            
            $affichageUnite1 = '';
            $affichageUnite2 = '';
            if ($unite!=$uniteArticle) {
                $affichageUnite1 = ' '+$unite;
                $affichageUnite2 = ' '+$uniteArticle;
            }
                        
            $validator->isTrue($QteConditionnement != 0 && !ctype_digit($post['value'] * $ValeurUniteDAArticle / $QteConditionnement), '<br />'.Trad::strBuild($trad->t("La quantité @1@@2@ n'est pas un multiple de la quantité de conditionnement @3@@4@."), $post['value'], $affichageUnite1, $QteConditionnement, $affichageUnite2));
        }
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function adresseMailAction() :Response
    {
        $post = $this->request->getParsedBody();


        $nomChamp = $post['nomChamp'] ?? '';
        
        $validator = $this->createValidator();
        
        $validator->adresseMail($nomChamp, $post['value'], $post['sizeMin'] ?? 0, $post['sizeMax'] ?? 100, $post['type'] ?? '');
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function isUrlValideAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        
        $validator = $this->createValidator();
        
        $validator->isUrlValide($nomChamp, $post['value'], $post['sizeMin'] ?? 0, $post['sizeMax'] ?? 100);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function codePostalAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $pays = $post['PAYS'] ?? 'FR';
        
        $validator = $this->createValidator();
        $Pays = new \Entity\Model\Pays($this->pdo);
        $Pays = $Pays->find($pays);
        
        switch ($Pays['REGLECP']) {
            case '0':
                $validator->isStringAvecEspace($nomChamp, $post['value'], $post['sizeMin'] ?? 0, $Pays['LONGCP']);
                break;
            case '1':
                // Longueur valeur maximale - sans espaces
                $validator->isStringSansEspace($nomChamp, $post['value'], $post['sizeMin'] ?? 0, $Pays['LONGCP']);
                break;
            case '2':
                // Longueur valeur maximale - numérique - sans espaces
                $validator->isInteger($nomChamp, $post['value'], $post['sizeMin'] ?? 0, $Pays['LONGCP']);
                break;
            case '3':
                // Longueur à respecter exactement - sans espaces
                $validator->isStringSansEspace($nomChamp, $post['value'], $Pays['LONGCP'], $Pays['LONGCP']);
                break;
            case '4':
                // Longueur à respecter exactement - numérique sans espaces
                $validator->isInteger($nomChamp, $post['value'], $Pays['LONGCP'], $Pays['LONGCP']);
                break;
            case '5':
                // Longueur valeur maximale
                $validator->isStringAvecEspace($nomChamp, $post['value'], $post['sizeMin'] ?? 0, $Pays['LONGCP']);
                break;
            case '6':
                // Longueur valeur maximale - numérique
                $validator->isNumericAvecEspace($nomChamp, $post['value'], $post['sizeMin'] ?? 0, $Pays['LONGCP']);
                break;
            case '7':
                // Longueur à respecter exactement
                $validator->isStringAvecEspace($nomChamp, $post['value'], $Pays['LONGCP'], $Pays['LONGCP']);
                break;
            case '8':
                // Longueur à respecter exactement  - numérique
                $validator->isNumericAvecEspace($nomChamp, $post['value'], $Pays['LONGCP'], $Pays['LONGCP']);
                break;
            case '9':
                // Vérification conformément au schéma national = masque
                $tblMasque = str_split($Pays['MASQUE']);
                $tblValue = str_split($post['value']);
                
                if (count($tblMasque) != count($tblValue)) {
                    $error = 1;
                } else {
                    $error = 0;
                    foreach ($tblValue as $key => $value) {
                        if ($tblMasque[$key] == 'A') {
                            if (!\ctype_alpha($value)) {
                                $error = 1;
                                break;
                            }
                        } elseif ($tblMasque[$key] == 'N') {
                            if (!\is_numeric($value)) {
                                $error = 1;
                                break;
                            }
                        } elseif ($tblMasque[$key] != $value) {
                            $error = 1;
                            break;
                        }
                    }
                }
                if ($error) {
                    $trad = $this->getTranslationService();
                    $validator->setError(Trad::strBuild($trad->t('Le code postal doit être de la forme : @1@'), $Pays['MASQUE']));
                }
                break;
        }
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function validatorTSDONAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $type = $post['TYPE'] ?? '';
        $societe = $post['societe'] ?? 0;
        $date = $post['date'] ?? 0;
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        $validator = $this->createValidator();
        $validator->validatorTSDON($nomChamp, $post['value'], $type, $this->pdo, $null, $societe, $date);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
    
    public function tauxTvaAction() :Response
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        $null = (isset($post['null']) && $post['null']=='true')? true : false;
        
        $validator = $this->createValidator();
        // $validator->sectionImputationZoom(strtoupper($post['value']), $nomChamp, $this->pdo, $null, ($post??[]));
        $validator->zoom($post['entity'], number_format(floatval($post['value']), 2), $nomChamp, $this->pdo, $null, ($post['post']??[]));
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], '');
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }

    /**
     * @return Response
     */
    public function isDateOuverteAction()
    {
        $post = $this->request->getParsedBody();
        $nomChamp = $post['nomChamp'] ?? '';
        
        $validator = $this->createValidator();
        
        $societe = $post['societe']??'';
        $typeDoc =  $post['typeDoc']??'';
        $bloquant = $post['bloquant']??true;
        $null = $post['null']??false;

        $validator->isDateOuverte($nomChamp, $post['value'], $societe, $typeDoc, $null, $bloquant);
        
        if ($validator->isValid()) {
            return new Response(200, [
                
            ], $validator->getError());
        } elseif ($validator->isNonBloquant()) {
            return new Response(200, [
                
            ], $validator->getErrorNonBloquant(true));
        } else {
            return new Response(200, [
                
            ], $validator->getError());
        }
    }
}
