<?php
namespace Modules\Additional\Specifique\Client132\Modules\ProjetAchat\Service;

use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Trad\ITrad;
use Helpers\Validator\Validator;
use Helpers\Outil\Sql;
use Helpers\Parametres\Constante;
use Entity\Model\Doa;
use Skeleton\Renderer\RendererInterface;

// phpcs:ignore
class projetAchatSpecifique
{
    //NCS_DOA_SERVICES_ACHATS
    public static function popupTransfereSpecifique(
        \PDO $pdo,
        RendererInterface $renderer,
        ITrad $trad,
        $idProjetAchat
    ) :?string {
        $doaEntity = new Doa($pdo);
        $doa = $doaEntity->find($idProjetAchat);
        
        if ($doa['STATUT'] != 'I') {
            return null;
        }
        
        
        $serviceAchat = ChampSupp::getCSValueSpecific([
            'COD_TAB' => 'DOA',
            'COD_ENR' => str_pad($idProjetAchat, 9, '0', STR_PAD_LEFT),
            'typedico' => 'PARNCS0132',
            'codedico' => 'NCS_DOA_SERVICES_ACHATS',
        ], $pdo);

        if ($serviceAchat) {
            $newsUser = ChampSupp::getCSValueSpecific([
                'COD_TAB' => 'SERVICES_ACHATS',
                'COD_ENR' => $serviceAchat['VALEUR'],
                'typedico' => 'PARNCS0132',
                'codedico' => 'NCS_SERVICES_ACHATS_DEST_MAIL_TRANSMISSION',
            ], $pdo);
            
            if ($newsUser && $newsUser['VALEUR'] != '') {
                return $renderer->render('@projetAchat/ProjetAchat/popuptransmettre', [
                    'acheteurParDefaut' => $newsUser['VALEUR'],
                    'idProjetAchat' => $idProjetAchat,
                    'pa' => $doa,
                ]);
            } else {
                return $renderer->render('@popup-erreur/ErreurCreation', [
                    'ErreurValid' => $trad->t('Aucun utilisateur trouvé pour ce service achat.'),
                ]);
            }
        } else {
            return $renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $trad->t('Aucun service achat trouvé pour ce Projet Achats.'),
            ]);
        }
    }
    
    public static function validatorCoherenceMontant(Validator $validator, $pdo, $idDoa, $post) : Validator
    {
        if ($idDoa == 'noId') {
            $idDoa = '';
        }
        
        $montantAnnuel = Sql::dilispec($pdo, 'PARNCS0132', 'NCS_DOA_MONTANT_ANNUEL');
        $montantGlobal = Sql::dilispec($pdo, 'PARNCS0132', 'NCS_DOA_MONTANT_GLOBAL');
        
        $spliter = Constante::spliter;
        
        $inputMontantAnnuel  = $post['CS_EXTENTION'.$spliter.'DOA'.$spliter.$idDoa.$spliter.$montantAnnuel]??'';
        $inputMmontantGlobal = $post['CS_EXTENTION'.$spliter.'DOA'.$spliter.$idDoa.$spliter.$montantGlobal]??'';
        
        if ($inputMontantAnnuel>$inputMmontantGlobal) {
            $validator->setError('Le montant annuel ne peut être supérieur au montant global.');
        }
        
        return $validator;
    }
}
