<?php
namespace Modules\Additional\Specifique\Client135\Modules\EtatStatistique\Controller;

use Entity\Model\Dilispec;
use Skeleton\Controller\Controller;
use Modules\Additional\Specifique\Client135\Modules\EtatStatistique\Repository\Reporting;
use Helpers\Ariane\Ariane;
use Helpers\Trad\Trad;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Fichier\Fichier;

class EtatStatistiqueController extends Controller
{
    public function etatControleAction() : string
    {
        $post = $this->request->getParsedBody();
        $action = $this->request->getAttribute('action');
        
        /*$paginator = new \Helpers\Pagination\PaginatorGestion();
        $paginator->generate(40, 'NUM_FACT');*/
        
        $numCS_Ecart = (new Dilispec($this->pdo))->getValue('PARNCS0135', 'NCS_FAC_LIG_ECARTPRIX');
        
        $reporting = new Reporting($this->pdo);
        $liste = $reporting->listeEtatControle($post, $numCS_Ecart);
        
        foreach ($liste as &$ligne) {
            $ligne['CS_CAT_INVENTAIRE'] = ChampSupp::getCSValueSpecific([
                'COD_TAB' => 'FAC_LIG',
                'COD_ENR' => ChampSupp::autoCompletCodeEnr($ligne['NUM_FACT'], $ligne['NUM_LIG'], 'FAC_LIG'),
                'typedico' => 'PARNCS0135',
                'codedico' => 'NCS_FAC_LIG_TYPE_LIGNE',
            ], $this->pdo);
        }
        
        if ($action == 'export') {
            $tableau = [];
            foreach ($liste as $ligneExport) {
                $tableau[] = [
                    'NUM_COMMANDE'              => $ligneExport['NUM_CDE'],
                    'LIGNE_COMMANDE'            => $ligneExport['NUM_LICDE'],
                    'NUM_BR'                    => $ligneExport['NUM_BR'],
                    'LIGNE_BR'                  => $ligneExport['NUM_LIBR'],
                    'NUM_FA'                    => $ligneExport['NUM_FACT'],
                    'LIGNE_FA'                  => $ligneExport['NUM_LIG'],
                    'CODE_STE'                  => $ligneExport['CODE_STE'],
                    'CODE_ARTICLE'              => $ligneExport['CODE_ART'],
                    'DESIGNATION_ARTICLE'       => $ligneExport['DESIGNAT'],
                    'REF_ARTICLE_FABRICANT'     => $ligneExport['REF_FAB'],
                    'PRIX_UNITAIRE_HT_REMISE'   => $ligneExport['PU'],
                    'PRIX_FACTURE'              => $ligneExport['PU_REMISE'],
                    'ECART'                     => $ligneExport['ECART'],
                    'TYPE_LIGNE'                => $ligneExport['CS_CAT_INVENTAIRE'],
                    'INVENTORY_LIGNE'           => $ligneExport['CODE_CDC'],
                    'CODE_FOURNISSEUR'          => $ligneExport['CODE_FOUR'],
                    'QTE_COMMANDE'              => $ligneExport['QTE_COMM'],
                    'QTE_LIVREE'                => $ligneExport['QTE_LIVREE'],
                    
                ];
            }
            
            \Helpers\ExportTo\Outils::gestionEmailAsync2(
                'reporting_etat_controle_'.date('Ymd'),
                $tableau,
                $this->JWTKEY,
                $this->router,
                $this->getTranslationService()
            );
    
            return 'ok';
        }
        
        $trad = $this->getTranslationService();
        
        Ariane::generate(1, $trad->t('Etat de contrôle'), $post, count($liste));
        
        //$infopagination = PaginatorGestion::getInfoPagination();
        
        $paginator = new \Helpers\Pagination\Paginator();
        $infopagination = $paginator->generate($liste, 20, $post, $this->renderer);
        
        return $this->renderer->render('@specifique135/etatControle/liste', [
            'menuActive'       => 'Etats Statistiques',
            'tableau' => $liste??[],
            'infoPagination'        => $infopagination,
            'post' => $post,
        ]);
    }
}
