<?php

namespace Modules\Additional\Specifique\Client28\Modules\Panier\Service;

use Entity\Model\Codgest;
use Entity\Model\Dilispec;
use Entity\Model\LotGest;
use Entity\Model\Societe;
use Helpers\Outil\Sql;
use Modules\Additional\Specifique\Client28\Modules\Panier\Repository\Panier;
use Modules\Panier\Repository\PanierEntete;

class SpecPanierLigneService
{
    /**
     * @param array<mixed> $ligne_panier
     */
    public static function articlesFormulaireMetier($ligne_panier, \PDO $pdo) : bool
    {
        $isFrsExtPanel = false;
        if ($ligne_panier['CODE_FM'] != '') {
            $panierRepo = new Panier($pdo);
            $liste_frs_ext_panel = $panierRepo->getListFrsForArtFMExtPanel('');

            foreach ($liste_frs_ext_panel as $frsExtPannel) {
                if ($ligne_panier['COD_FOURN'] == $frsExtPannel['COD_FOURN']) {
                    $isFrsExtPanel = true;
                    break;
                }
            }
        }

        return $isFrsExtPanel;
    }

    //specifique majSpecfiqueCG_28_35_37
    public static function majCGBouygue(\PDO $pdo, string $cg, string  $si, string  $idPanier, string  $codSfam) : string
    {
        $panierEnteteService = new PanierEntete($pdo);
        $panierEntete = $panierEnteteService->getEntetePanier($idPanier, $_SESSION['id_user']);

        $societe = $panierEntete['CODE_STE'];
        $cgTrouve = null;

        $concat = 'S|'.trim($societe).'|'.trim($codSfam);
        $cgTmp = Sql::dilispec($pdo, 'BYCN_EOTP', $concat);

        if ($cgTmp) {
            $cgTrouve = $cgTmp;
        } else {
            $societeRepo = new Societe($pdo);
            $societeFound = $societeRepo->find($societe, ['GROUPE']);
            
            if ($societeFound && $societeFound['GROUPE']) {
                $concat = 'G|'.trim($societeFound['GROUPE']).'|'.trim($codSfam);
                $cgTmp = Sql::dilispec($pdo, 'BYCN_EOTP', $concat);

                if ($cgTmp) {
                    $cgTrouve = $cgTmp;
                }
            }
        }

        if (!is_null($cgTrouve)) {
            $lotGest = new LotGest($pdo);
            $lot = $lotGest->getNumLot();

            if (!is_numeric($lot['NUM_LOT'])) {
                $lot['NUM_LOT'] = 0;
            }

            $sisecto = \Helpers\Outil\Roles::droit($pdo, 'SISECTOR', 'PARAMSTE');

            $codeGest = new Codgest($pdo);
            if ($sisecto) {
                $foundCG = $codeGest->find([
                    'CODE_STE' => $societe,
                    'CODE_SI' => $si,
                    'NUM_LOT' => intval($lot['NUM_LOT']),
                    'CODE_AFF' => $cgTrouve,
                ]);
            } else {
                $foundCG = $codeGest->find([
                    'CODE_SI' => $si,
                    'NUM_LOT' => intval($lot['NUM_LOT']),
                    'CODE_AFF' => $cgTrouve,
                ]);
            }

            if ($foundCG) {
                $cg = $cgTrouve;
            }
        }

        return $cg;
    }
}
