<?php

namespace Modules\Facture\Controller;

use Skeleton\Controller\Controller;
use Modules\Facture\WebService\ImpressionFacture;
use Helpers\Fichier\Fichier;
use Helpers\Parametres\Constante;

class ImpressionFactureController extends Controller
{
    
    public function impressionEnteteAction() : string
    {
        $post = $this->request->getParsedBody();

        $trad = $this->getTranslationService();
        
        $select = ['T' => $trad->t('Tout'), 'O' => $trad->t('Oui'), 'N' => $trad->t('Non')];
        \Helpers\Ariane\Ariane::generate(0, $trad->t('Edition Entête'), $post);
        
        return $this->renderer->render('@impression/impressionEnteteFacture', [
            'menuActive' => 'Facture',
            'select' => $select,
            'ErreurValid' => '',
            'post' => $post,
            
        ]);
    }
    
    public function pdfEnteteFactureAction() : string
    {
        $post = $this->request->getParsedBody();

        $trad = $this->getTranslationService();

        $validator = $this->createValidator();
        $validator->zoom('Societe', $post['SOCIETE_MIN'], $trad->t("Société min"), $this->pdo, true);
        $validator->zoom('Societe', $post['SOCIETE_MAX'], $trad->t("Société max"), $this->pdo, true);
        
        $validator->isStringSansEspace($trad->t("N° enregistrement min"), $post['ENREG_MIN'], 0, 26);
        $validator->isStringSansEspace($trad->t("N° enregistrement max"), $post['ENREG_MAX'], 0, 26);
        
        $validator->zoom('Fourniss', $post['FOUR_MIN'], $trad->t("Code fournisseur min"), $this->pdo, true);
        $validator->zoom('Fourniss', $post['FOUR_MAX'], $trad->t("Code fournisseur max"), $this->pdo, true);
        
        $validator->isDate($post['DATE_ENREG_MIN'], '', '', true, $trad->t("Date d’enregistrement min"));
        $validator->isDate($post['DATE_ENREG_MAX'], '', '', true, $trad->t("Date d’enregistrement max"));
        $validator->isCoherenceDate($post['DATE_ENREG_MIN'], $post['DATE_ENREG_MAX'], $trad->t("Date d’enregistrement max"), true);
        
        
        $validator->isDate($post['DATE_PERIODE_MIN'], '', '', true, $trad->t("Date de période min"));
        $validator->isDate($post['DATE_PERIODE_MAX'], '', '', true, $trad->t("Date de période max"));
        $validator->isCoherenceDate($post['DATE_PERIODE_MIN'], $post['DATE_PERIODE_MAX'], $trad->t("Date de période max"), true);
    
        $validator->isStringSansEspace($trad->t("N° Facture Fournisseur min"), $post['FAC_FOUR_MIN'], 0, 26);
        $validator->isStringSansEspace($trad->t("N° Facture Fournisseur max"), $post['FAC_FOUR_MAX'], 0, 26);
        
        $validator->isDate($post['DATE_FOUR_MIN'], '', '', true, $trad->t("Date Facture Fournisseur min"));
        $validator->isDate($post['DATE_FOUR_MAX'], '', '', true, $trad->t("Date Facture Fournisseur max"));
        $validator->isCoherenceDate($post['DATE_FOUR_MIN'], $post['DATE_FOUR_MAX'], $trad->t("Date Facture Fournisseur max"), true);
        
        $validator->zoom('Devise', $post['DEVISE_MIN'], $trad->t("Devise min"), $this->pdo, true);
        $validator->zoom('Devise', $post['DEVISE_MAX'], $trad->t("Devise max"), $this->pdo, true);
        
        $validator->isFloat($trad->t("Total HT min"), $post['TOTAL_HT_MIN'], Constante::formatMontant);
        $validator->isFloat($trad->t("Total HT max"), $post['TOTAL_HT_MAX'], Constante::formatMontant);
        
        $validator->isFloat($trad->t("Total TTC min"), $post['TOTAL_TTC_MIN'], Constante::formatMontant);
        $validator->isFloat($trad->t("Total TTC max"), $post['TOTAL_TTC_MAX'], Constante::formatMontant);
        
        $validator->isDate($post['ECHEANCE_MIN'], '', '', true, $trad->t("Date d'échéance min"));
        $validator->isDate($post['ECHEANCE_MAX'], '', '', true, $trad->t("Date d'échéance max"));
        $validator->isCoherenceDate($post['ECHEANCE_MIN'], $post['ECHEANCE_MAX'], $trad->t("Date d'échéance max"), true);
        
        $validator->zoom('Modepaie', $post['MODE_REG_MIN'], $trad->t("Mode de règlement min"), $this->pdo, true);
        $validator->zoom('Modepaie', $post['MODE_REG_MAX'], $trad->t("Mode de règlement max"), $this->pdo, true);
        
        if ($validator->isValid()) {
            $impression = new ImpressionFacture();
            $impression = $impression->impressionEntete($post);
            $reponse = new \SimpleXMLElement($impression->responseData);
            
            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 \json_encode(['statut' => 'O', 'url' => $url]);
            } else {
                return \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
            }
        }
            
        return \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
    }
    
    public function impressionLigneAction() : string
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $select = ['T' => $trad->t('Tout'), 'O' => $trad->t('Oui'), 'N' => $trad->t('Non')];

        \Helpers\Ariane\Ariane::generate(0, $trad->t('Edition Lignes'), $post);
        
        return $this->renderer->render('@impression/impressionLigneFacture', [
            'menuActive' => 'Facture',
            'select' => $select,
            'ErreurValid' => '',
            'post' => $post,
            
            'easyDemat' => $_SESSION['EasyDemat'],
        ]);
    }
    
    public function pdfLigneFactureAction() : string
    {
        $post = $this->request->getParsedBody();

        $trad = $this->getTranslationService();
        
        $validator = $this->createValidator();
        $validator->zoom('Societe', $post['SOCIETE_MIN'], $trad->t("Société min"), $this->pdo, true);
        $validator->zoom('Societe', $post['SOCIETE_MAX'], $trad->t("Société max"), $this->pdo, true);
        
        $validator->isStringSansEspace($trad->t("N° enregistrement min"), $post['ENREG_MIN'], 0, 26);
        $validator->isStringSansEspace($trad->t("N° enregistrement max"), $post['ENREG_MAX'], 0, 26);
        
        $validator->isStringSansEspace($trad->t("N° ligne min"), $post['LIGNE_MIN'], 0, 26);
        $validator->isStringSansEspace($trad->t("N° ligne max"), $post['LIGNE_MAX'], 0, 26);
                
        $validator->isStringSansEspace($trad->t("N° de commande min"), $post['CDE_MIN'] ?? '', 0, 26);
        $validator->isStringSansEspace($trad->t("N° de commande max"), $post['CDE_MAX'] ?? '', 0, 26);

        $validator->isStringSansEspace($trad->t("N° de ligne de commande min"), $post['CDE_LIGNE_MIN'] ?? '', 0, 26);
        $validator->isStringSansEspace($trad->t("N° de ligne de commande max"), $post['CDE_LIGNE_MAX'] ?? '', 0, 26);
        
        $validator->isStringSansEspace($trad->t("N° de BR min"), $post['BR_MIN'] ?? '', 0, 26);
        $validator->isStringSansEspace($trad->t("N° de BR max"), $post['BR_MAX'] ?? '', 0, 26);
        
        $validator->isStringSansEspace($trad->t("N° de ligne de BR min"), $post['BR_LIGNE_MIN'] ?? '', 0, 26);
        $validator->isStringSansEspace($trad->t("N° de ligne de BR max"), $post['BR_LIGNE_MAX'] ?? '', 0, 26);
        
        $validator->isStringSansEspace($trad->t("N° de TP min"), $post['TP_MIN'] ?? '', 0, 26);
        $validator->isStringSansEspace($trad->t("N° de TP max"), $post['TP_MAX'] ?? '', 0, 26);
        
        $validator->zoom('SousFamille', $post['SsFAM_MIN'], $trad->t("Sous-famille min"), $this->pdo, true);
        $validator->zoom('SousFamille', $post['SsFAM_MAX'], $trad->t("Sous-famille max"), $this->pdo, true);
        
        $validator->isInteger($trad->t("Quantité min"), $post['QTE_MIN'], 0, 4);
        $validator->isInteger($trad->t("Quantité max"), $post['QTE_MAX'], 0, 4);
        
        $validator->isFloat($trad->t("Prix min"), $post['PRIX_MIN'], Constante::formatPU);
        $validator->isFloat($trad->t("Prix max"), $post['PRIX_MAX'], Constante::formatPU);
        
        $validator->isFloat($trad->t("Montant min"), $post['MONTANT_MIN'], Constante::formatMontant);
        $validator->isFloat($trad->t("Montant max"), $post['MONTANT_MAX'], Constante::formatMontant);
        
        if ($validator->isValid()) {
            $impression = new ImpressionFacture();
            $impression = $impression->impressionLigne($post);
            
            $reponse = new \SimpleXMLElement($impression->responseData);
            
            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 \json_encode(['statut' => 'O', 'url' => $url]);
            } else {
                return \json_encode(['statut' => $reponse->Statut, 'erreur' => $reponse->MsgErreur]);
            }
        }
            
        return \json_encode(['statut' => '', 'erreur' => $validator->getError()]);
    }

    public function impressionEntityAction(): string
    {
        // TODO : add Rules
        $factureId = $this->request->getAttribute('factureId');
        
        $xmlResult = (new ImpressionFacture())->impressionFacture($factureId);
        $reponse = new \SimpleXMLElement($xmlResult->responseData);

        if ($reponse->Retour != 'O') {
            return $reponse->MsgErreur;
        }
        return Fichier::UploadClient(
            Fichier::getDirTmp() . '/' . $reponse->FichierPDF,
            'BAP_NonChiffre'
            . '_' . $factureId
            . '_' . date('dmY') . '.pdf'
        );
    }
}
