<?php

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

use Entity\Model\Articles;
use Entity\Model\OxaUserPanier;
use Entity\Model\SousFamille;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;
use Modules\GlobalApplication\Outil\InitAxesImpDa;
use Modules\Panier\Service\PanierLigneService;

class SpecImportService
{
    public function isClient():bool
    {
        return true;
    }
    /**
     * @param \PDO $pdo
     * @param array<mixed> $ligne
     * @return array<mixed>
     */
    public static function initImputation($pdo, $ligne)
    {
        $infoEntete = (new OxaUserPanier($pdo))->find([
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $_SESSION['id_panier'],
        ]);

        $axeEntete = [
            'CODE_STE'  => $infoEntete['CODE_STE'],
            'SECTIMP'   => $infoEntete['SI_CODE'],
            'SSCPTEANA' => $infoEntete['SCA_CODE'],
            'CODECOMP'  => $infoEntete['CDC_CODE'],
            'TYP_DOC'   => '',

            'ID_CG'     => '',
            'CODGES'    => '',
            'LIBGES'    => '',
        ];

        if ($ligne['COD_ART']) {
            $article = (new Articles($pdo))->find($ligne['COD_ART'], ['SSFAM']);
            if ($article) {
                $codeSfam = $article['SSFAM'];
            } else {
                $codeSfam = $ligne['COD_SFAM']??'';
            }
        } else {
            $codeSfam = $ligne['COD_SFAM']??'';
        }

        $sfam = (new SousFamille($pdo))->find($codeSfam, ['CODE_FAM']);

        $initImputation = new InitAxesImpDa($pdo, $axeEntete);
        $imputation = $initImputation->getInitImputation([
            'CODE_ART' => ($ligne['COD_ART']??''),
            'SousFamille' => [
                'CODE_SFA' => $ligne['COD_SFAM']??'',
                'CODE_FAM' => $sfam['CODE_FAM']??'',
            ],
        ]);

        $ligne['SI_CODE'] = '';
        $ligne['SCA_CODE'] = '';
        $ligne['CDC_CODE'] = $imputation['CDC'];

        return $ligne;
    }

    /**
     * @param \PDO $pdo
     * @param string $cdc
     * @return array<mixed>
     */
    public static function getSI(\PDO $pdo, ?string $cdc = null)
    {
        $idCSCSC = ChampSupp::autoCompletCodeEnrImputations($pdo, 'CODECOMP', $cdc);

        $valueCSObli = ChampSupp::getCSValueSpecific([
            'COD_TAB' => 'CODECOMP',
            'COD_ENR' => $idCSCSC,
            'typedico' => 'PARAMCC',
            'codedico' => 'NCS_CODECOMP_LIEUANA_OBLIG',
        ], $pdo);

        $valueCS = ChampSupp::getCSValueSpecific([
            'COD_TAB' => 'CODECOMP',
            'COD_ENR' => $idCSCSC,
            'typedico' => 'PARAMCC',
            'codedico' => 'NCS_CODECOMP_LIEUANA_VAL',
        ], $pdo);

        return [
            'VALEUR'    => $valueCS,
            'OBLIG'     => sql::boolSql($valueCSObli),
        ];
    }

    /**
     * @param \PDO $pdo
     * @param array<mixed> $ligne
     * @return array<mixed>
     */
    public static function surchargeSI($pdo, $ligne)
    {
        $infoCS = self::getSI($pdo, $ligne['CDC_CODE']);

        if ($infoCS['OBLIG']) {
            if ($infoCS['VALEUR']) {
                $ligne['SI_CODE'] = $infoCS['VALEUR'];
            } else {
                $ligne['SI_CODE'] = $_SESSION['Session_SectionImputation'];
            }
        }

        return $ligne;
    }

    /**
     * @param \PDO $pdo
     * @param array<mixed> $ligne
     * @param array<mixed> $parametrage
     * @param string $societe
     * @return string|null
     */
    public static function controleSI($pdo, $ligne, ITrad $trad, $parametrage, $societe)
    {
        $validator = new Validator($pdo, $trad);
        $infoCS = self::getSI($pdo, $ligne['CDC_CODE']);

        $value = $ligne['SI_CODE'];

        if ($infoCS['VALEUR'] == $value) {
            return null;
        }

        $error = false;
        if (!$infoCS['OBLIG']) {
            if ($infoCS['VALEUR'] == '') {
                if ($_SESSION['Session_SectionImputation'] != $value) {
                    $error = true;
                }
            } elseif ($infoCS['VALEUR'] != $value) {
                $error = true;
            }

            if ($error) {
                $validator->setError(Trad::strBuild($trad->t('Le code lieu ne correspond pas de la valeur liée au code frais : @1@'), $value));
            }
        } else {
            $validator->zoom(
                'sectimpu',
                $value,
                $trad->t('section d\'imputation'),
                $pdo,
                true,
                $parametrage['SISECTOR']? ['filter_egal_CODE_STE' => $societe]:[],
            );
        }

        if (!$validator->isValid()) {
            $msg = $validator->getError();
            if (strpos($msg, '<br />') === 0) {
                $msg = substr($msg, 6);
            }
            return $msg;
        }

        return null;
    }

    /**
     * @param \PDO $pdo
     * @param string $cdc
     * @return array<mixed>
     */
    public static function getSCA(\PDO $pdo, ?string $cdc = null)
    {
        $idCSCSC = ChampSupp::autoCompletCodeEnrImputations($pdo, 'CODECOMP', $cdc);

        $valueCSObli = ChampSupp::getCSValueSpecific([
            'COD_TAB' => 'CODECOMP',
            'COD_ENR' => $idCSCSC,
            'typedico' => 'PARAMCC',
            'codedico' => 'NCS_CODECOMP_PROJET_OBLIG',
        ], $pdo);

        $valueCS = ChampSupp::getCSValueSpecific([
            'COD_TAB' => 'CODECOMP',
            'COD_ENR' => $idCSCSC,
            'typedico' => 'PARAMCC',
            'codedico' => 'NCS_CODECOMP_PROJET_VAL',
        ], $pdo);

        return [
            'VALEUR'    => $valueCS,
            'OBLIG'     => sql::boolSql($valueCSObli),
        ];
    }

    /**
     * @param \PDO $pdo
     * @param array<mixed> $ligne
     * @return array<mixed>
     */
    public static function surchargeSCA($pdo, $ligne)
    {
        $infoCS = self::getSCA($pdo, $ligne['CDC_CODE']);

        if ($infoCS['OBLIG']) {
            if ($infoCS['VALEUR']) {
                $ligne['SCA_CODE'] = $infoCS['VALEUR'];
            } else {
                $ligne['SCA_CODE'] = $_SESSION['Session_SCA'];
            }
        }

        return $ligne;
    }

    /**
     * @param \PDO $pdo
     * @param array<mixed> $ligne
     * @param array<mixed> $parametrage
     * @param string $societe
     * @return string|null
     */
    public static function controleSCA($pdo, $ligne, ITrad $trad, $parametrage, $societe)
    {
        $validator = new Validator($pdo, $trad);
        $infoCS = self::getSCA($pdo, $ligne['CDC_CODE']);

        $value = $ligne['SCA_CODE'];

        if ($infoCS['VALEUR'] == $value) {
            return null;
        }

        $error = false;
        if (!$infoCS['OBLIG']) {
            if ($infoCS['VALEUR'] == '') {
                if ($_SESSION['Session_SectionImputation'] != $value) {
                    $error = true;
                }
            } elseif ($infoCS['VALEUR'] != $value) {
                $error = true;
            }

            if ($error) {
                $validator->setError(Trad::strBuild($trad->t('Le code lieu ne correspond pas de la valeur liée au code frais : @1@'), $value));
            }
        } else {
            $validator->zoom(
                'Sscptana',
                $value,
                $trad->t('Sous-compte d\'analyse'),
                $pdo,
                true,
                $parametrage['SCASECTO']? ['filter_egal_CODE_STE' => $societe]:[],
            );
        }

        if (!$validator->isValid()) {
            $msg = $validator->getError();
            if (strpos($msg, '<br />') === 0) {
                $msg = substr($msg, 6);
            }
            return $msg;
        }

        return null;
    }
}
