<?php

namespace Modules\Additional\Specifique\Client92\Modules\API\Controller;

use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Fichier\Fichier;
use Helpers\Pagination\Paginator;
use Helpers\Parametres\Constante;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;
use Modules\Additional\Specifique\Client92\Modules\Outil\Budget;
use Modules\Additional\Specifique\Client92\Modules\Repository\ImportBudget;
use Skeleton\Controller\Controller;
use Entity\Model\Hastutil;
use Entity\Model\Periodes;
use Entity\Model\Societe;
use Helpers\Outil\Roles;
use Modules\Ajax\Repository\Listes;
use Helpers\Session\ParseBase1;

class ConsultationBudgetController extends Controller
{
    public function batchExportConsoAction($post)
    {
        $trad = $this->getTranslationService();
        $numeroTraitement = $post[2];
        set_time_limit(1200);
        $listeUser = [];
        
        $log = new \Helpers\Log\OxaEventDet($this->pdo);
        
        $ajaxListe = new Listes($this->pdo);
        $listeUser = $ajaxListe->utilisateurListe(['VALIDE' => true]);
        
        $outilBudget = new Budget($this->pdo, $this->getTranslationService());
        
        foreach ($listeUser as $user) {
            $_SESSION['Session_Langue'] = $user['COD_LNG'];

            $listeConsult = $outilBudget->exportBudget($user['CODE'], [], true);

            if ($listeConsult === null) {
                continue;
            }
            
            $link = Fichier::createExportCsvV2(
                'export_consomation_budgetaire'.random_int(0, 999999999),
                $listeConsult,
                $outilBudget->getExportCol()
            );
        
            \Modules\Ajax\Controller\MailController::envoiMail([
                'destinataires' => $user['EMAIL'],
                'sujet' => $trad->t('Document exporté'),
                'message' =>
                    $trad->t('Bonjour').',<br /><br/>'
                    . $trad->t('Veuillez trouver ci-joint votre état de consommation budgétaire.')
                    . '<br/>'
                    .$trad->t('Cordialement').',',
                'pj' => [(new ParseBase1())->getRep()->tmp.$link.'.csv'],
            ]);

            $log->setLog('O', $trad->t('Envoi consommation budgétaire à').' '.$user['CODE']);
        }

        
        
        return 'O';
    }
}
