<?php

namespace Modules\Facture\Controller;

use Skeleton\Controller\Controller;
use Modules\Facture\Repository\EtatCde;
use Helpers\Ariane\Ariane;
use Helpers\Outil\Sql;

class EtatCommandeController extends Controller
{
    public function etatAction() : string
    {
        $numFact = $this->request->getAttribute('numFact');
        
        $trad = $this->getTranslationService();
        
        $etatCdeRepo = new EtatCde($this->pdo);
        
        $listeCde = $etatCdeRepo->listeCde($numFact);
        $listeFactLigne = $etatCdeRepo->detailLigneFacture($numFact);
        $facEntete = $etatCdeRepo->enteteFacture($numFact);
        
        $total = 0;
        $totalCde = 0;

        foreach ($listeCde as &$cde) {
            $numCde = $cde['NUM_CDE'];
            $noPo = $cde['NUM_LICDE'];

            $cde = $etatCdeRepo->detailLigneCde($numCde, $noPo);
            $calcule = $etatCdeRepo->calculeMoyennePu($numFact, $numCde, $noPo);
            
            $cde['QTE_DEJA_FAC'] = 0;
            $cde['QTE_FAC_A_SIG'] = 0;
            $cde['QTE_FACTU'] = 0;
            
            $cde['MONTANT_TOTAL_DEJA_FAC'] = 0;
            $cde['MONTANT_TOTAL_FAC_A_SIG'] = 0;
            $cde['MONTANT_TOTAL_FACTU'] = 0;
            $cde['MONTANT_TOTAL_RECEP'] = 0;
            
            $facture = 0;
            $fac_a_sig = 0;
            $deja_fac = 0;
            
            $enMontant = false;
            foreach ($calcule as $val) {
                if (Sql::boolSql($val['EN_MNT'])) {
                    $enMontant = true;
                    if (!Sql::boolSql($val['ATT_SIG'])) {
                        $cde['MONTANT_TOTAL_DEJA_FAC'] += $val['MONTANT'];
                    } else {
                        $cde['MONTANT_TOTAL_FAC_A_SIG'] += $val['MONTANT'];
                    }
                    
                    $cde['MONTANT_TOTAL_FACTU'] += $val['MONTANT'];
                    $cde['MONTANT_TOTAL_RECEP'] += $val['MNT_REC'];
                } else {
                    if (!Sql::boolSql($val['ATT_SIG'])) {
                        $deja_fac += $val['PU_REMISE']*$val['QUANTITE'];
                        $cde['QTE_DEJA_FAC'] += $val['QUANTITE'];
                    } else {
                        $fac_a_sig += $val['PU_REMISE']*$val['QUANTITE'];
                        $cde['QTE_FAC_A_SIG'] += $val['QUANTITE'];
                    }
                    
                    $facture += $val['PU_REMISE']*$val['QUANTITE'];
                    $cde['QTE_FACTU'] += $val['QUANTITE'];
                }
            }
                        
            if (!$enMontant) {
                //colonne déjà fac
                $cde['PU_DEJA_FAC'] = $deja_fac? ($deja_fac/$cde['QTE_DEJA_FAC']):0;
                $cde['MONTANT_TOTAL_DEJA_FAC'] = $cde['PU_DEJA_FAC'] * $cde['QTE_DEJA_FAC'];
            
                //colonne en signature
                $cde['PU_FAC_A_SIG'] = $fac_a_sig? ($fac_a_sig/$cde['QTE_FAC_A_SIG']):0;
                $cde['MONTANT_TOTAL_FAC_A_SIG'] = $cde['PU_FAC_A_SIG'] * $cde['QTE_FAC_A_SIG'];
            
                //colonne total facturé
                $cde['PU_FACTU'] = $facture? ($facture/$cde['QTE_FACTU']):0;
                $cde['MONTANT_TOTAL_FACTU'] = $cde['PU_FACTU'] * $cde['QTE_FACTU'];
            
                $cde['MONTANT_TOTAL_CDE'] = $cde['QTE_COMM'] * $cde['PU'];
                $cde['MONTANT_TOTAL_RECEP'] = $cde['QTE_RECEP'] * $cde['PU'];
            } else {
                $cde['MONTANT_TOTAL_CDE'] = $cde['QTE_COMM'] * $cde['PU'];
            }
            
            $total += $cde['MONTANT_TOTAL_FACTU'];
            $totalCde +=$cde['MONTANT_TOTAL_CDE'];
        }
        
        foreach ($listeFactLigne as $ligne) {
            if (!Sql::boolSql($ligne['ATT_SIG'])) {
                $listeCde[] = [
                    'DES_SOMM' => $ligne['DESIGNAT'],
                    'QTE_DEJA_FAC' => $ligne['QUANTITE'],
                    'PU_DEJA_FAC' => $ligne['PU_REMISE'],
                    'MONTANT_TOTAL_DEJA_FAC' => $ligne['MONTANT'],
                    
                    'QTE_FACTU' => $ligne['QUANTITE'],
                    'PU_FACTU' => $ligne['PU_REMISE'],
                    'MONTANT_TOTAL_FACTU' => $ligne['MONTANT'],
                ];
            } else {
                $listeCde[] = [
                    'DES_SOMM' => $ligne['DESIGNAT'],
                    'QTE_FAC_A_SIG' => $ligne['QUANTITE'],
                    'PU_FAC_A_SIG' => $ligne['PU_REMISE'],
                    'MONTANT_TOTAL_FAC_A_SIG' => $ligne['MONTANT'],
                    
                    'QTE_FACTU' => $ligne['QUANTITE'],
                    'PU_FACTU' => $ligne['PU_REMISE'],
                    'MONTANT_TOTAL_FACTU' => $ligne['MONTANT'],
                ];
            }
            
            $total += $ligne['MONTANT'];
        }
        
        Ariane::generate(2, $trad->t('Synthèse commande'));
        Ariane::setFocus($numFact);
        
        return $this->renderer->render('@etatCommande/tableauEtat', [
            'menuActive' => 'eproc',
            'listeCde' => $listeCde,
            'facture' => $facEntete,
            'total' => $total,
            'totalCde' => $totalCde,
            
        ]);
    }
}
