<?php

namespace Modules\Additional\Specifique\Client92\Modules\Outil;

use GuzzleHttp\Psr7\Response;
use Helpers\Fichier\Fichier;
use Helpers\Outil\Date;
use Helpers\Outil\Outils;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Modules\Additional\Specifique\Client92\Modules\Repository\ImportBudget;
use Skeleton\Router;

class Budget
{
    private \PDO $pdo;
    private ITrad $trad;
    
    public function __construct(\PDO $pdo, ITrad $trad)
    {
        $this->pdo = $pdo;
        $this->trad = $trad;
    }
    
    /**
     * exportBudget
     *
     * @param  string $user
     * @param  array<mixed> $post
     * @param  bool $batch
     * @return array<mixed> export data
     */
    public function exportBudget($user, $post = [], $batch = false) :?array
    {
        $trad = $this->trad;

        if (!$post) {
            $post['tri_CODE_STE'] = 'asc';
            $post['tri_CODE_PER'] = 'desc';
            $post['tri_CENTRE_COUT'] = 'asc';
        }

        $importBudget = new ImportBudget($this->pdo);
        $listeBudget = $importBudget->getListeConsomation($post, $user);
        
        if ($batch && empty($listeBudget)) {
            return null;
        }

        $listeConsult = [];
        $consoTotal = 0;
        $budgetTotal = 0;
        $totalDispo = 0;
        foreach ($listeBudget as $budget) {
            $total = ((float) $budget['MNT_CONS_CA']) + ((float) $budget['MNT_CONS_BR']) + ((float) $budget['MNT_CONS_FA']);

            $dispo = (float) $budget['MNT_BUDGET'] - (float) $total;
            $listeDetail = $importBudget->getDetailExportBudget($budget['CODE_STE'], $budget['CODE_PER'], $budget['CENTRE_COUT'], $budget['ORDRE_INTERNE'], $budget['DOMANE_ACTIVITE'], $budget['PAYS_LIVRE'], $budget['GAMME']);
            
            if ($listeDetail) {
                $listeConsult[] = [
                    'CODE_STE'          => $budget['CODE_STE'],
                    'CODE_PER'          => $budget['CODE_PER'],
                    'CENTRE_COUT'       => $budget['CENTRE_COUT'],
                    'LIBELLE'           => Outils::convertLibelleGlobal($this->pdo, 'Sectimpu', '', $budget['CENTRE_COUT'], false),
                    'ORDRE_INTERNE'     => $budget['ORDRE_INTERNE'],
                    'DOMANE_ACTIVITE'   => $budget['DOMANE_ACTIVITE'],
                    'PAYS_LIVRE'        => $budget['PAYS_LIVRE'],
                    'GAMME'             => $budget['GAMME'],
                    'NOM'               => $budget['NOM'],

                    //
                    'CDE' => '',
                    'DTE_CREAT' => '',
                    'FRS' => '',
                    'OBJET' => '',
                    //
                    'MNT_CONS_CA'       => '',
                    'MNT_CONS_BR'       => '',
                    'MNT_CONS_FA'       => '',
                    'DEVISE_CA'         => '',
                    'TOTAL'             => '',
                    'BUDGET'            => '',
                    'DISPONIBLE'        => '',
                    'DEVISE_SOC'        => '',
                ];
    
                foreach ($listeDetail as $detail) {
                    $listeConsult[] = [
                        'CODE_STE'          => '',
                        'CODE_PER'          => '',
                        'CENTRE_COUT'       => '',
                        'LIBELLE'           => '',
                        'ORDRE_INTERNE'     => '',
                        'DOMANE_ACTIVITE'   => '',
                        'PAYS_LIVRE'        => '',
                        'GAMME'             => '',
                        'NOM'               => '',
                        //
                        'CDE'               => $detail['NO_CA'],
                        'DTE_CREAT'         => Date::dateConverte($detail['DTE_CREAT']),
                        'FRS'               => $detail['CODE_FOU'],
                        'OBJET'             => $detail['OBJET_CA'],
                        //
                        'MNT_CONS_CA'       => number_format($detail['TOTCDE'], 2, ',', ''),
                        'MNT_CONS_BR'       => number_format($detail['TOTREC'], 2, ',', ''),
                        'MNT_CONS_FA'       => number_format($detail['TOTFACT'], 2, ',', ''),
                        'DEVISE_CA'         => $detail['DEVISE'],
                        'TOTAL'             => '',
                        'BUDGET'            => '',
                        'DISPONIBLE'        => '',
                        'DEVISE_SOC'        => '',
                    ];
                }
    
                
                $listeConsult[] = [
                    'CODE_STE'          => '',
                    'CODE_PER'          => '',
                    'CENTRE_COUT'       => '',
                    'LIBELLE'           => '',
                    'ORDRE_INTERNE'     => '',
                    'DOMANE_ACTIVITE'   => '',
                    'PAYS_LIVRE'        => '',
                    'GAMME'             => '',
                    'NOM'               => '',
                    //
                    'CDE' => '',
                    'DTE_CREAT' => '',
                    'FRS' => '',
                    'OBJET' => $trad->t('Consommation'),
                    //
                    'MNT_CONS_CA'       => number_format($budget['MNT_CONS_CA'], 2, ',', ''),
                    'MNT_CONS_BR'       => number_format($budget['MNT_CONS_BR'], 2, ',', ''),
                    'MNT_CONS_FA'       => number_format($budget['MNT_CONS_FA'], 2, ',', ''),
                    'DEVISE_CA'         => '',
                    'TOTAL'             => number_format($total, 2, ',', ''),
                    'BUDGET'            => number_format($budget['MNT_BUDGET'], 2, ',', ''),
                    'DISPONIBLE'        => number_format($dispo, 2, ',', ''),
                    'DEVISE_SOC'        => $budget['soc_DEVISE'],
                ];
            } else {
                $listeConsult[] = [
                    'CODE_STE'          => $budget['CODE_STE'],
                    'CODE_PER'          => $budget['CODE_PER'],
                    'CENTRE_COUT'       => $budget['CENTRE_COUT'],
                    'LIBELLE'           => Outils::convertLibelleGlobal($this->pdo, 'Sectimpu', '', $budget['CENTRE_COUT'], false),
                    'ORDRE_INTERNE'     => $budget['ORDRE_INTERNE'],
                    'DOMANE_ACTIVITE'   => $budget['DOMANE_ACTIVITE'],
                    'PAYS_LIVRE'        => $budget['PAYS_LIVRE'],
                    'GAMME'             => $budget['GAMME'],
                    'NOM'               => $budget['NOM'],
                    //
                    'CDE' => '',
                    'DTE_CREAT' => '',
                    'FRS' => '',
                    'OBJET' => '',
                    //
                    'MNT_CONS_CA'       => number_format($budget['MNT_CONS_CA'], 2, ',', ''),
                    'MNT_CONS_BR'       => number_format($budget['MNT_CONS_BR'], 2, ',', ''),
                    'MNT_CONS_FA'       => number_format($budget['MNT_CONS_FA'], 2, ',', ''),
                    'DEVISE_CA'         => '',
                    'TOTAL'             => number_format($total, 2, ',', ''),
                    'BUDGET'            => number_format($budget['MNT_BUDGET'], 2, ',', ''),
                    'DISPONIBLE'        => number_format($dispo, 2, ',', ''),
                    'DEVISE_SOC'        => $budget['soc_DEVISE'],
                ];
            }
            

            $budgetTotal += (float) $budget['MNT_BUDGET'];
            $consoTotal += $total;
            $totalDispo += (float) $dispo;
            $devise = $budget['soc_DEVISE'];
        }


        $listeConsult [] = [
            'CODE_STE'          => '',
            'CODE_PER'          => '',
            'CENTRE_COUT'       => '',
            'LIBELLE'           => '',
            'ORDRE_INTERNE'     => '',
            'DOMANE_ACTIVITE'   => '',
            'PAYS_LIVRE'        => '',
            'GAMME'             => '',
            'NOM'               => '',
            //
            'CDE' => '',
            'DTE_CREAT' => '',
            'FRS' => '',
            'OBJET' => $trad->t('Total général'),
            //
            'MNT_CONS_CA'       => '',
            'MNT_CONS_BR'       => '',
            'MNT_CONS_FA'       => '',
            'DEVISE_CA'         => '',
            'TOTAL'             => number_format($consoTotal, 2, ',', ''),
            'BUDGET'            => number_format($budgetTotal, 2, ',', ''),
            'DISPONIBLE'        => number_format($totalDispo, 2, ',', ''),
            'DEVISE_SOC'        => $devise ?? '',
        ];

        return $listeConsult;
    }

    /**
     * @return array<string,string>
     */
    public function getExportCol() :array
    {
        $trad = $this->trad;
        return [
            'CODE_STE'          => $trad->t('Société'),
            'CODE_PER'          => $trad->t('Période'),
            'CENTRE_COUT'       => $trad->t('Section d\'imputation'),
            'LIBELLE'           => $trad->t('Libellé de la section d\'imputation'),
            'ORDRE_INTERNE'     => $trad->t('Code complémentaire'),
            'DOMANE_ACTIVITE'   => $trad->t('Sous-compte d\'analyse'),
            'PAYS_LIVRE'        => $trad->t('Pays livré'),
            'GAMME'             => $trad->t('Gamme'),
            'NOM'               => $trad->t('Responsable'),
            //
            'CDE'               => $trad->t('Commande'),
            'DTE_CREAT'         => $trad->t('Date création'),
            'FRS'               => $trad->t('Code fournisseur'),
            'OBJET'             => $trad->t('Objet'),
            //
            'MNT_CONS_CA'       => $trad->t('Commandé'),
            'MNT_CONS_BR'       => $trad->t('Réceptionné'),
            'MNT_CONS_FA'       => $trad->t('Facturé'),
            'DEVISE_CA'         => $trad->t('Devise commande'),
            'TOTAL'             => $trad->t('Total'),
            'BUDGET'            => $trad->t('Budget'),
            'DISPONIBLE'        => $trad->t('Disponible'),
            'DEVISE_SOC'        => $trad->t('Devise société'),
        ];
    }
}
