<?php

namespace Modules\Fournisseur\Controller;

use Modules\Fournisseur\MenuContextuel\FactureMenuContextuel;
use Modules\GlobalApplication\Repository\Societe;
use Skeleton\Controller\Controller;
use Entity\Model\Fourniss;
use Modules\Fournisseur\Repository\Facture;
use Helpers\Ariane\ArianeSub;
use Helpers\Outil\Date;

class FactureController extends Controller
{
    public function factureAction()
    {
        $id = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();

        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($id);

        $societes = null;
        if ($_SESSION['Session_Login']) {
            $societes = (new Societe($this->pdo))->listRestrictedSocieteForUser();
        }

        if ($societes !== null) {
            //AJOUT DE LA SOCIETE COURANTE
            array_push($societes, ['CODE_STE' => $_SESSION['Session_SocUsr'], 'RAIS_SOC' => '']);
        } else {
            $societes = new \Entity\Model\Societe($this->pdo);
            $societes = $societes->fetchAll([]);
        }

        $listeSociete = '';

        foreach ($societes as $soc) {
            if (!$listeSociete) {
                $listeSociete .= "'" . $soc['CODE_STE'] . "'";
            } else {
                $listeSociete .= ', \'' . $soc['CODE_STE'] . "'";
            }
        }

        $facture = new Facture($this->pdo);
        $listeFacture = $facture->listeFacture($id, $post, $listeSociete);

        $menuContextuel   = new FactureMenuContextuel($this->pdo, $this->router, $this->getTranslationService());

        foreach ($listeFacture as &$invoice) {
            $invoice['menuContextuel'] = $menuContextuel->generate($invoice['NUM_FACT']);
        }


        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($listeFacture, 0, $post, $this->renderer);

        /* trad */
        
        
        $trad = $this->getTranslationService();

        ArianeSub::generate(1, 0, $trad->t('Facture'), $_SERVER['REQUEST_URI'], 1, $post);

        return $this->renderer->render('@Fournisseur/Historique/facture', [
            'menuActive' => 'Fournisseur',
            'menuLeft' => 'GestionFournisseur',
            "code_fournisseur" => $id,
            "libelle_fournisseur" => $fournis["RS"],
            'infoPagination' => $infopagination,
            "facture" => true,
            'listeFacture' => $listeFacture,
            
        ]);
    }

    public function ajaxFactureAction(): string
    {
        $post['id'] = $this->request->getAttribute('id');

        $facture = new Facture($this->pdo);

        $listeLigneFacture = $facture->listeLigneFacture($post['id']);
        $facture = $facture->factureFourniss($post['id']);
        
        return $this->renderer->render('@Fournisseur/Ajax-Historique/detailFacture', [
            'facture' => $facture,
            'listeLigneFacture' => $listeLigneFacture,
            'id' => $post['id'],
            
        ]);
    }

    public function exportFactureCSVAction(): string
    {
        $id = $this->request->getAttribute('id');
        $post = $this->request->getParsedBody();

        $trad = $this->getTranslationService();

        $societes = null;
        if ($_SESSION['Session_Login']) {
            $societes = (new Societe($this->pdo))->listRestrictedSocieteForUser();
        }

        if ($societes !== null) {
            //AJOUT DE LA SOCIETE COURANTE
            array_push($societes, ['CODE_STE' => $_SESSION['Session_SocUsr'], 'RAIS_SOC' => '']);
        } else {
            $societes = new \Entity\Model\Societe($this->pdo);
            $societes = $societes->fetchAll([]);
        }

        $listeSociete = '';

        foreach ($societes as $soc) {
            if (!$listeSociete) {
                $listeSociete .= "'" . $soc['CODE_STE'] . "'";
            } else {
                $listeSociete .= ', \'' . $soc['CODE_STE'] . "'";
            }
        }

        $facture = new Facture($this->pdo);
        $listeFacture = $facture->listeFacture($id, $post, $listeSociete);

        foreach ($listeFacture as &$facture) {
            $facture['DATE_ENR']    = DATE::dateConverte($facture['DATE_ENR']);
            $facture['DATE_FAFO']   = DATE::dateConverte($facture['DATE_FAFO']);
            $facture['DTAE_ECH']    = DATE::dateConverte($facture['DTAE_ECH']);

            $facture                                    = [
                $trad->t('N° Enregistrement')      => $facture[ 'NUM_FACT' ],
                $trad->t('Enreg. le')              => $facture[ 'DATE_ENR' ],
                $trad->t('Emise le')               => $facture[ 'DATE_FAFO' ],
                $trad->t('Echéance')               => $facture[ 'DTAE_ECH' ],
                $trad->t('Total HT')               => number_format($facture[ 'TOTAL_HT' ], 2, ',', ''),
                $trad->t('Total TTC')              => number_format($facture[ 'TOTAL_TTC' ], 2, ',', ''),
            ];
        }


        \Helpers\ExportTo\Outils::gestionEmailAsync2(
            "export_facture_" . $id,
            $listeFacture,
            $this->JWTKEY,
            $this->router,
            $this->getTranslationService()
        );

        return 'ok';
    }
}
