<?php
namespace Modules\Commande\Controller;

use Skeleton\Controller\Controller;
use Helpers\Ariane\Ariane;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;
use Helpers\Fichier\Fichier;
use Helpers\Parametres\Constante;

class EditionCommandeController extends Controller
{
    public function enteteAction() : string
    {
        $post = $this->request->getParsedBody();
        $mode = $this->request->getAttribute('mode');

        if ($post) {
            // REVOIR TOUTES LES TRADUCTIONS ET LES ENLEVERS DANS LES VALIDATOR
            $validator = $this->createValidator();
            
            $validator->isInteger('N° CA min', $post['NO_CDE_MIN'], 0, 5);
            $validator->isInteger('N° CA max', $post['NO_CDE_MAX'], 0, 5);
            
            $validator->zoom('Societe', $post['SOCIETE_MIN'], 'Société min', $this->pdo, true);
            $validator->zoom('Societe', $post['SOCIETE_MAX'], 'Société max', $this->pdo, true);
            
            $validator->zoom('Centre', $post['CTR_EMETT_MIN'], 'Centre émetteur min', $this->pdo, true, ['filter_EMISSION' => 'T']);
            $validator->zoom('Centre', $post['CTR_EMETT_MAX'], 'Centre émetteur max', $this->pdo, true, ['filter_EMISSION' => 'T']);
            
            $validator->zoom('Centre', $post['CTR_LIV_MIN'], 'Centre livraison min', $this->pdo, true, ['filter_LIVRAISON' => 'T']);
            $validator->zoom('Centre', $post['CTR_LIV_MAX'], 'Centre livraison max', $this->pdo, true, ['filter_LIVRAISON' => 'T']);
            
            $validator->zoom('Centre', $post['CTR_FAC_MIN'], 'Centre facturation min', $this->pdo, true, ['filter_FACTURAT' => 'T']);
            $validator->zoom('Centre', $post['CTR_FAC_MAX'], 'Centre facturation max', $this->pdo, true, ['filter_FACTURAT' => 'T']);
            
            $validator->zoom('Fourniss', $post['CODE_FOU_MIN'], 'Fournisseur min', $this->pdo, true);
            $validator->zoom('Fourniss', $post['CODE_FOU_MAX'], 'Fournisseur max', $this->pdo, true);
            
            $validator->zoom('Devise', $post['DEVISE_MIN'], 'Devise min', $this->pdo, true);
            $validator->zoom('Devise', $post['DEVISE_MAX'], 'Devise max', $this->pdo, true);
            
            $validator->isFloat('Remise min', $post['REM_MIN'], Constante::formatMontant);
            $validator->isFloat('Remise max', $post['REM_MAX'], Constante::formatMontant);
            
            $validator->zoom('typeDoc', $post['TYP_DOC_MIN'], 'Type document min', $this->pdo, true);
            $validator->zoom('typeDoc', $post['TYP_DOC_MAX'], 'Type document max', $this->pdo, true);
            
            $validator->zoom('Mca', $post['MODELE_MIN'], 'Modèle min', $this->pdo, true);
            $validator->zoom('Mca', $post['MODELE_MAX'], 'Modèle max', $this->pdo, true);
            
            $validator->isDate($post['DTE_CREAT_MIN'], '', '', true, 'Date création min');
            $validator->isDate($post['DTE_CREAT_MAX'], '', '', true, 'Date création max');
            
            $validator->isDate($post['DTE_PERIO_MIN'], '', '', true, 'Date période min');
            $validator->isDate($post['DTE_PERIO_MAX'], '', '', true, 'Date période max');
            
            $validator->isDate($post['DTE_VALID_MIN'], '', '', true, 'Date de validation min');
            $validator->isDate($post['DTE_VALID_MAX'], '', '', true, 'Date de validation max');
            
            $validator->isDate($post['DTE_AR_MIN'], '', '', true, 'Date A/R min');
            $validator->isDate($post['DTE_AR_MAX'], '', '', true, 'Date A/R max');
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Acheteur min', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Acheteur max', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            
            $validator->zoom('Sectimpu', $post['SECTIMP_MIN'], 'Section d\'imputation min', $this->pdo, true);
            $validator->zoom('Sectimpu', $post['SECTIMP_MAX'], 'Section d\'imputation max', $this->pdo, true);
            
            $validator->zoom('Modepaie', $post['MODE_PAIE_MIN'], 'Mode de paiement min', $this->pdo, true);
            $validator->zoom('Modepaie', $post['MODE_PAIE_MAX'], 'Mode de paiement max', $this->pdo, true);
            
            $validator->zoom('CondPaie', $post['COND_PAIE_MIN'], 'Condition de paiement min', $this->pdo, true);
            $validator->zoom('CondPaie', $post['COND_PAIE_MAX'], 'Condition de paiement max', $this->pdo, true);
            
            $validator->zoom('Modelivr', $post['MODE_LIV_MIN'], 'Mode de livraison min', $this->pdo, true);
            $validator->zoom('Modelivr', $post['MODE_LIV_MAX'], 'Mode de livraison max', $this->pdo, true);
            
            $validator->zoom('acccadre', $post['ACC_CADRE_MIN'], 'Accord-cadre min', $this->pdo, true);
            $validator->zoom('acccadre', $post['ACC_CADRE_MAX'], 'Accord-cadre max', $this->pdo, true);
            
            $validator->isFloat('Zone libre numérique min', $post['ZNLIBNUM_MIN'], Constante::formatMontant);
            $validator->isFloat('Zone libre numérique max', $post['ZNLIBNUM_MAX'], Constante::formatMontant);
            
            $validator->isFloat('Total CA sans remise min', $post['TTCASR_MIN'], Constante::formatMontant);
            $validator->isFloat('Total CA sans remise max', $post['TTCASR_MAX'], Constante::formatMontant);

            $validator->isFloat('Total CA HT min', $post['TTCAHT_MIN'], Constante::formatMontant);
            $validator->isFloat('Total CA HT max', $post['TTCAHT_MAX'], Constante::formatMontant);
            
            $validator->isFloat('Total CA TTC min', $post['TTCATTC_MIN'], Constante::formatMontant);
            $validator->isFloat('Total CA TTC max', $post['TTCATTC_MAX'], Constante::formatMontant);
            
            if ($mode=='reliquat') {
                $validator->isInteger('Total CA TTC min', $post['RELIQUAT'], 0, 3);
            }
            
            if ($validator->isValid()) {
                if ($mode=='reliquat') {
                } else {
                }

                /**
                 * TODO MSA souci sur la fonction
                 */
                /** @phpstan-ignore-next-line */
                $reponse = new \SimpleXMLElement();
                
                if ($reponse->Statut =="O") {
                    $url = Fichier::URLDownload((string)$reponse->FichierPDF[0]);
                    $url = $this->router->generateUri('Ajax.outil.downloadPdf', ['url' => (string)str_replace('.pdf', '', $reponse->FichierPDF[0])]);
                    return (string) \json_encode(['statut' => 'O', 'url' => $url]);
                } else {
                    return (string) \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
                }
            } else {
                return (string) \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
            }
        }
        
        $trad = $this->getTranslationService();

        $listeEtatTab = [
            ''  => '',
            'E' => $trad->t('En cours'),
            'A' => $trad->t('En Attente'),
            'V' => $trad->t('Validée'),
            'S' => $trad->t('Soldées physiquement'),
            'Z' => $trad->t('Annulées'),
        ];
        
        $listeEtatFinancier= [
            ''  => '',
            'T' => $trad->t('Non soldées financièrement'),
            'F' => $trad->t('Soldées financièrement'),
        ];
        $trad = $this->getTranslationService();
        
        if ($mode=='reliquat') {
            Ariane::generate(1, $trad->t('Editions des reliquat solde financier des commande'), $post);
        } else {
            Ariane::generate(1, $trad->t('Edition des entête de commande'), $post);
        }
        
        return $this->renderer->render('@Commande/Edition/entete', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'Commande.GestionCommande.index',
            'listeEtatTab' => $listeEtatTab,
            'listeEtatFinancier' => $listeEtatFinancier,
            'mode' => $mode,
            'post' => $post,
        ]);
    }
    
    public function ligneAction() : string
    {
        $post = $this->request->getParsedBody();
        
        if ($post) {
            // Fairee la traduction et enlever la traduction des helmper zoom
            $validator = $this->createValidator();
            
            $validator->isInteger('N° CA min', $post['NO_CDE_MIN'], 0, 5);
            $validator->isInteger('N° CA max', $post['NO_CDE_MAX'], 0, 5);
            
            $validator->zoom('Societe', $post['SOCIETE_MIN'], 'Société min', $this->pdo, true);
            $validator->zoom('Societe', $post['SOCIETE_MAX'], 'Société max', $this->pdo, true);
            
            $validator->zoom('SousFamille', $post['SSFAM_MIN'], 'Sous-famille min', $this->pdo, true);
            $validator->zoom('SousFamille', $post['SSFAM_MAX'], 'Sous-famille max', $this->pdo, true);
            
            $validator->zoom('Sectimpu', $post['SECTIMP_MIN'], 'Section d\'imputation min', $this->pdo, true);
            $validator->zoom('Sectimpu', $post['SECTIMP_MAX'], 'Section d\'imputation max', $this->pdo, true);
            
            $validator->zoom('codeComp', $post['CODECOMP_MIN'], 'Code complémentaire min', $this->pdo, true);
            $validator->zoom('codeComp', $post['CODECOMP_MAX'], 'Code complémentaire max', $this->pdo, true);
            
            $validator->zoom('Sscptana', $post['SSCPTEANA_MIN'], 'Sous compte d\'analyse min', $this->pdo, true);
            $validator->zoom('Sscptana', $post['SSCPTEANA_MAX'], 'Sous compte d\'analyse max', $this->pdo, true);
            
            $validator->zoom('Fourniss', $post['CODE_FOU_MIN'], 'Fournisseur min', $this->pdo, true);
            $validator->zoom('Fourniss', $post['CODE_FOU_MAX'], 'Fournisseur max', $this->pdo, true);
            
            $validator->isDate($post['DTE_CREAT_MIN'], '', '', true, 'Date création min');
            $validator->isDate($post['DTE_CREAT_MAX'], '', '', true, 'Date création max');
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Acheteur min', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Acheteur max', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Demandeur min', $this->pdo, true, ['filter_DEMANDEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Demandeur max', $this->pdo, true, ['filter_DEMANDEUR' => 'T']);
            
            $validator->zoom('typeDoc', $post['TYP_DOC_MIN'], 'Type document min', $this->pdo, true);
            $validator->zoom('typeDoc', $post['TYP_DOC_MAX'], 'Type document max', $this->pdo, true);
            
            $validator->zoom('acccadre', $post['ACC_CADRE_MIN'], 'Accord-cadre min', $this->pdo, true);
            $validator->zoom('acccadre', $post['ACC_CADRE_MAX'], 'Accord-cadre max', $this->pdo, true);
            
            $validator->zoom('ssItesto', $post['SSITESTO_MIN'], 'Site de stock min', $this->pdo, true);
            $validator->zoom('ssItesto', $post['SSITESTO_MAX'], 'Site de stock max', $this->pdo, true);
            
            if ($validator->isValid()) {
                
                /**
                 * TODO MSA souci sur la fonction
                 */
                /** @phpstan-ignore-next-line */
                $reponse = new \SimpleXMLElement();
                
                if ($reponse->Statut =="O") {
                    $url = Fichier::URLDownload((string)$reponse->FichierPDF[0]);
                    $url = $this->router->generateUri('Ajax.outil.downloadPdf', ['url' => (string)str_replace('.pdf', '', $reponse->FichierPDF[0])]);
                    return (string) \json_encode(['statut' => 'O', 'url' => $url]);
                } else {
                    return (string) \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
                }
            } else {
                return (string) \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
            }
        }
        $trad = $this->getTranslationService();
        
        $listeEtatTab = [
            ''  => '',
            'E' => $trad->t('En cours'),
            'A' => $trad->t('En Attente'),
            'V' => $trad->t('Validée'),
            'S' => $trad->t('Soldées physiquement'),
        ];
        $trad = $this->getTranslationService();
        
        Ariane::generate(1, $trad->t('Edition des ligne de commande'), $post);
        
        return $this->renderer->render('@Commande/Edition/ligne', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'Commande.GestionCommande.index',
            'listeEtatTab' => $listeEtatTab,
            'path' => 'Commande.edition.ligne',
            'post' => $post,
        ]);
    }
    
    public function cdeSurLivraisonAction() : string
    {
        $post = $this->request->getParsedBody();
        
        if ($post) {
            // traduction a faire
            $validator = $this->createValidator();
            
            $validator->isDate($post['DTE_PERIO_MIN'], '', '', true, 'Date création min');
            $validator->isDate($post['DTE_PERIO_MAX'], '', '', true, 'Date création max');
            
            $validator->isInteger('N° CA min', $post['NO_CDE_MIN'], 0, 5);
            $validator->isInteger('N° CA max', $post['NO_CDE_MAX'], 0, 5);
            
            $validator->zoom('Societe', $post['SOCIETE_MIN'], 'Société min', $this->pdo, true);
            $validator->zoom('Societe', $post['SOCIETE_MAX'], 'Société max', $this->pdo, true);
            
            $validator->zoom('SousFamille', $post['SSFAM_MIN'], 'Sous-famille min', $this->pdo, true);
            $validator->zoom('SousFamille', $post['SSFAM_MAX'], 'Sous-famille max', $this->pdo, true);
            
            $validator->zoom('Sectimpu', $post['SECTIMP_MIN'], 'Section d\'imputation min', $this->pdo, true);
            $validator->zoom('Sectimpu', $post['SECTIMP_MAX'], 'Section d\'imputation max', $this->pdo, true);
            
            $validator->zoom('codeComp', $post['CODECOMP_MIN'], 'Code complémentaire min', $this->pdo, true);
            $validator->zoom('codeComp', $post['CODECOMP_MAX'], 'Code complémentaire max', $this->pdo, true);
            
            $validator->zoom('Sscptana', $post['SSCPTEANA_MIN'], 'Sous compte d\'analyse min', $this->pdo, true);
            $validator->zoom('Sscptana', $post['SSCPTEANA_MAX'], 'Sous compte d\'analyse max', $this->pdo, true);
            
            $validator->zoom('Fourniss', $post['CODE_FOU_MIN'], 'Fournisseur min', $this->pdo, true);
            $validator->zoom('Fourniss', $post['CODE_FOU_MAX'], 'Fournisseur max', $this->pdo, true);
            
            $validator->isDate($post['DTE_CREAT_MIN'], '', '', true, 'Date création min');
            $validator->isDate($post['DTE_CREAT_MAX'], '', '', true, 'Date création max');
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Acheteur min', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Acheteur max', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Demandeur min', $this->pdo, true, ['filter_DEMANDEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Demandeur max', $this->pdo, true, ['filter_DEMANDEUR' => 'T']);
            
            $validator->zoom('typeDoc', $post['TYP_DOC_MIN'], 'Type document min', $this->pdo, true);
            $validator->zoom('typeDoc', $post['TYP_DOC_MAX'], 'Type document max', $this->pdo, true);
            
            $validator->zoom('acccadre', $post['ACC_CADRE_MIN'], 'Accord-cadre min', $this->pdo, true);
            $validator->zoom('acccadre', $post['ACC_CADRE_MAX'], 'Accord-cadre max', $this->pdo, true);
            
            $validator->zoom('ssItesto', $post['SSITESTO_MIN'], 'Site de stock min', $this->pdo, true);
            $validator->zoom('ssItesto', $post['SSITESTO_MAX'], 'Site de stock max', $this->pdo, true);
            
            if ($validator->isValid()) {
                
                /**
                 * TODO MSA souci sur la fonction
                 */
                /** @phpstan-ignore-next-line */
                $reponse = new \SimpleXMLElement();
                
                if ($reponse->Statut =="O") {
                    $url = Fichier::URLDownload((string)$reponse->FichierPDF[0]);
                    $url = $this->router->generateUri('Ajax.outil.downloadPdf', ['url' => (string)str_replace('.pdf', '', $reponse->FichierPDF[0])]);
                    return (string) \json_encode(['statut' => 'O', 'url' => $url]);
                } else {
                    return (string) \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
                }
            } else {
                return (string) \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
            }
        }
        
        $trad = $this->getTranslationService();
        
        $listeEtatTab = [
            ''  => '',
            'E' => $trad->t('En cours'),
            'A' => $trad->t('En Attente'),
            'V' => $trad->t('Validée'),
            'S' => $trad->t('Soldées physiquement'),
        ];
        $trad = $this->getTranslationService();
        
        Ariane::generate(1, $trad->t('Edition commande sur-livraison'), $post);
        
        return $this->renderer->render('@Commande/Edition/ligne', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'Commande.GestionCommande.index',
            'listeEtatTab' => $listeEtatTab,
            'post' => $post,
            'path' => 'Commande.edition.cdeSurLivraison',
            'cdeSurLivraison' => true,
        ]);
    }
    
    public function resumeAction() : string
    {
        $post = $this->request->getParsedBody();
        
        if ($post) {
            // traduction a faire
            $validator = $this->createValidator();
            
            $validator->isInteger('N° CA min', $post['NO_CDE_MIN'], 0, 5);
            $validator->isInteger('N° CA max', $post['NO_CDE_MAX'], 0, 5);
            
            $validator->zoom('Societe', $post['SOCIETE_MIN'], 'Société min', $this->pdo, true);
            $validator->zoom('Societe', $post['SOCIETE_MAX'], 'Société max', $this->pdo, true);
            
            $validator->zoom('Article', $post['CODE_ART_MIN'], 'Article min', $this->pdo, true);
            $validator->zoom('Article', $post['CODE_ART_MAX'], 'Article max', $this->pdo, true);
            
            $validator->zoom('SousFamille', $post['SSFAM_MIN'], 'Sous-famille min', $this->pdo, true);
            $validator->zoom('SousFamille', $post['SSFAM_MAX'], 'Sous-famille max', $this->pdo, true);
            
            $validator->zoom('Sectimpu', $post['SECTIMP_MIN'], 'Section d\'imputation min', $this->pdo, true);
            $validator->zoom('Sectimpu', $post['SECTIMP_MAX'], 'Section d\'imputation max', $this->pdo, true);
            
            $validator->zoom('codeComp', $post['CODECOMP_MIN'], 'Code complémentaire min', $this->pdo, true);
            $validator->zoom('codeComp', $post['CODECOMP_MAX'], 'Code complémentaire max', $this->pdo, true);
            
            $validator->zoom('Sscptana', $post['SSCPTEANA_MIN'], 'Sous compte d\'analyse min', $this->pdo, true);
            $validator->zoom('Sscptana', $post['SSCPTEANA_MAX'], 'Sous compte d\'analyse max', $this->pdo, true);
            
            $validator->zoom('Fourniss', $post['CODE_FOU_MIN'], 'Fournisseur min', $this->pdo, true);
            $validator->zoom('Fourniss', $post['CODE_FOU_MAX'], 'Fournisseur max', $this->pdo, true);
            
            $validator->isDate($post['DTE_CREAT_MIN'], '', '', true, 'Date création min');
            $validator->isDate($post['DTE_CREAT_MAX'], '', '', true, 'Date création max');
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Acheteur min', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Acheteur max', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Demandeur min', $this->pdo, true, ['filter_DEMANDEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Demandeur max', $this->pdo, true, ['filter_DEMANDEUR' => 'T']);
            
            $validator->zoom('typeDoc', $post['TYP_DOC_MIN'], 'Type document min', $this->pdo, true);
            $validator->zoom('typeDoc', $post['TYP_DOC_MAX'], 'Type document max', $this->pdo, true);
            
            $validator->zoom('Mca', $post['MODELE_MIN'], 'Modèle min', $this->pdo, true);
            $validator->zoom('Mca', $post['MODELE_MAX'], 'Modèle max', $this->pdo, true);
            
            if ($validator->isValid()) {
                
                /**
                 * TODO MSA souci sur la fonction
                 */
                /** @phpstan-ignore-next-line */
                $reponse = new \SimpleXMLElement();
                
                if ($reponse->Statut =="O") {
                    $url = Fichier::URLDownload((string)$reponse->FichierPDF[0]);
                    $url = $this->router->generateUri('Ajax.outil.downloadPdf', ['url' => (string)str_replace('.pdf', '', $reponse->FichierPDF[0])]);
                    return (string) \json_encode(['statut' => 'O', 'url' => $url]);
                } else {
                    return (string) \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
                }
            } else {
                return (string) \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
            }
        }
        
        $trad = $this->getTranslationService();
        
        $listeEtatTab = [
            ''  => '',
            'E' => $trad->t('En cours'),
            'A' => $trad->t('En Attente'),
            'V' => $trad->t('Validée'),
            'S' => $trad->t('Soldées physiquement'),
        ];
        $trad = $this->getTranslationService();
        
        Ariane::generate(1, $trad->t('Edition du résumé des commandes'), $post);
        
        return $this->renderer->render('@Commande/Edition/resume', [
            'menuActive' => 'CollecteBesoins',
            'menuLeft' => 'Commande.GestionCommande.index',
            'listeEtatTab' => $listeEtatTab,
            'post' => $post,
        ]);
    }
    
    public function suiviARAction() : string
    {
        $trad = $this->getTranslationService();
        $post = $this->request->getParsedBody();
        
        if ($post) {
            $validator = $this->createValidator();
            $validator->zoom('Societe', $post['SOCIETE'], 'Société', $this->pdo, true);
            $validator->isDate($post['DTE_VAL_MIN'], '', '', true, 'Date création min');
            $validator->isDate($post['DTE_VAL_MAX'], '', '', true, 'Date de validation max');
            $validator->zoom('HASTUTIL', $post['ACH'], 'Acheteur', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            $validator->isFloat('Montant minimum', $post['MTN_MIN'], Constante::formatMontant);
            
            if ($validator->isValid()) {
                /**
                 * TODO MSA souci sur la fonction
                 */
                /** @phpstan-ignore-next-line */
                $reponse = new \SimpleXMLElement();
                
                if ($reponse->Statut =="O") {
                    $url = Fichier::URLDownload((string)$reponse->FichierPDF[0]);
                    $url = $this->router->generateUri('Ajax.outil.downloadPdf', ['url' => (string)str_replace('.pdf', '', $reponse->FichierPDF[0])]);
                    return (string) \json_encode(['statut' => 'O', 'url' => $url]);
                } else {
                    return (string) \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
                }
            } else {
                return (string) \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
            }
        }
        $trad = $this->getTranslationService();
        
        Ariane::generate(1, $trad->t('Edition des suivi des A/R'), $post);
        
        return $this->renderer->render('@Commande/Edition/suiviAR', [
            'menuActive'    => 'CollecteBesoins',
            'menuLeft'      => 'Commande.GestionCommande.index',
            'post'          => $post,
        ]);
    }
    
    public function enteteHistoAction() : string
    {
        $post = $this->request->getParsedBody();
        
        if ($post) {
            // traduction a faire
            $validator = $this->createValidator();
            
            $validator->isInteger('N° CA min', $post['NO_CDE_MIN'], 0, 5);
            $validator->isInteger('N° CA max', $post['NO_CDE_MAX'], 0, 5);
            
            $validator->zoom('Societe', $post['SOCIETE_MIN'], 'Société min', $this->pdo, true);
            $validator->zoom('Societe', $post['SOCIETE_MAX'], 'Société max', $this->pdo, true);
            
            $validator->zoom('Centre', $post['CTR_EMETT_MIN'], 'Centre émetteur min', $this->pdo, true, ['filter_EMISSION' => 'T']);
            $validator->zoom('Centre', $post['CTR_EMETT_MAX'], 'Centre émetteur max', $this->pdo, true, ['filter_EMISSION' => 'T']);
            
            $validator->zoom('Centre', $post['CTR_LIV_MIN'], 'Centre livraison min', $this->pdo, true, ['filter_LIVRAISON' => 'T']);
            $validator->zoom('Centre', $post['CTR_LIV_MAX'], 'Centre livraison max', $this->pdo, true, ['filter_LIVRAISON' => 'T']);
            
            $validator->zoom('Centre', $post['CTR_FAC_MIN'], 'Centre facturation min', $this->pdo, true, ['filter_FACTURAT' => 'T']);
            $validator->zoom('Centre', $post['CTR_FAC_MAX'], 'Centre facturation max', $this->pdo, true, ['filter_FACTURAT' => 'T']);
            
            $validator->zoom('Fourniss', $post['CODE_FOU_MIN'], 'Fournisseur min', $this->pdo, true);
            $validator->zoom('Fourniss', $post['CODE_FOU_MAX'], 'Fournisseur max', $this->pdo, true);
            
            $validator->zoom('Devise', $post['DEVISE_MIN'], 'Devise min', $this->pdo, true);
            $validator->zoom('Devise', $post['DEVISE_MAX'], 'Devise max', $this->pdo, true);
            
            $validator->isFloat('Remise min', $post['REM_MIN'], Constante::formatMontant);
            $validator->isFloat('Remise max', $post['REM_MAX'], Constante::formatMontant);
            
            $validator->isDate($post['DTE_CREAT_MIN'], '', '', true, 'Date création min');
            $validator->isDate($post['DTE_CREAT_MAX'], '', '', true, 'Date création max');
            
            $validator->isDate($post['DTE_PERIO_MIN'], '', '', true, 'Date période min');
            $validator->isDate($post['DTE_PERIO_MAX'], '', '', true, 'Date période max');
            
            $validator->isDate($post['DTE_VALID_MIN'], '', '', true, 'Date de validation min');
            $validator->isDate($post['DTE_VALID_MAX'], '', '', true, 'Date de validation max');
            
            $validator->isDate($post['DTE_AR_MIN'], '', '', true, 'Date A/R min');
            $validator->isDate($post['DTE_AR_MAX'], '', '', true, 'Date A/R max');
            
            $validator->zoom('HASTUTIL', $post['ACH_MIN'], 'Acheteur min', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            $validator->zoom('HASTUTIL', $post['ACH_MAX'], 'Acheteur max', $this->pdo, true, ['filter_ACHETEUR' => 'T']);
            
            $validator->zoom('Sectimpu', $post['SECTIMP_MIN'], 'Section d\'imputation min', $this->pdo, true);
            $validator->zoom('Sectimpu', $post['SECTIMP_MAX'], 'Section d\'imputation max', $this->pdo, true);
            
            $validator->zoom('Modepaie', $post['MODE_PAIE_MIN'], 'Mode de paiement min', $this->pdo, true);
            $validator->zoom('Modepaie', $post['MODE_PAIE_MAX'], 'Mode de paiement max', $this->pdo, true);
            
            $validator->zoom('CondPaie', $post['COND_PAIE_MIN'], 'Condition de paiement min', $this->pdo, true);
            $validator->zoom('CondPaie', $post['COND_PAIE_MAX'], 'Condition de paiement max', $this->pdo, true);
            
            $validator->zoom('Modelivr', $post['MODE_LIV_MIN'], 'Mode de livraison min', $this->pdo, true);
            $validator->zoom('Modelivr', $post['MODE_LIV_MAX'], 'Mode de livraison max', $this->pdo, true);
            
            $validator->zoom('acccadre', $post['ACC_CADRE_MIN'], 'Accord-cadre min', $this->pdo, true);
            $validator->zoom('acccadre', $post['ACC_CADRE_MAX'], 'Accord-cadre max', $this->pdo, true);
            
            $validator->isFloat('Total CA sans remise min', $post['TTCASR_MIN'], Constante::formatMontant);
            $validator->isFloat('Total CA sans remise max', $post['TTCASR_MAX'], Constante::formatMontant);
            
            $validator->isFloat('Total CA HT min', $post['TTCAHT_MIN'], Constante::formatMontant);
            $validator->isFloat('Total CA HT max', $post['TTCAHT_MAX'], Constante::formatMontant);
            
            $validator->isFloat('Total CA TTC min', $post['TTCATTC_MIN'], Constante::formatMontant);
            $validator->isFloat('Total CA TTC max', $post['TTCATTC_MAX'], Constante::formatMontant);
            
            $validator->isStringAvecEspace('Désignation min', $post['DES_SOMM_Min'], 0, 100);
            $validator->isStringAvecEspace('Désignation max', $post['DES_SOMM_MAX'], 0, 100);
            

            if ($validator->isValid()) {
                
                /**
                 * TODO MSA souci sur la fonction
                 */
                /** @phpstan-ignore-next-line */
                $reponse = new \SimpleXMLElement();
                
                if ($reponse->Statut =="O") {
                    $url = Fichier::URLDownload((string)$reponse->FichierPDF[0]);
                    $url = $this->router->generateUri('Ajax.outil.downloadPdf', ['url' => (string)str_replace('.pdf', '', $reponse->FichierPDF[0])]);
                    return (string) \json_encode(['statut' => 'O', 'url' => $url]);
                } else {
                    return (string) \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
                }
            } else {
                return (string) \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
            }
        }
        
        $trad = $this->getTranslationService();

        Ariane::generate(1, $trad->t('Edition des entête de commande historisées'), $post);
        
        return $this->renderer->render('@Commande/Edition/enteteHisto', [
            'menuActive'    => 'CollecteBesoins',
            'menuLeft'      => 'Commande.GestionCommande.index',
            'post'          => $post,
        ]);
    }
}
