<?php

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

use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Sql;
use Modules\Ajax\Repository\InitCS;

class UpdateCdcBySi
{
    /**
     * @param string $codeSte
     * @param string $si
     */
    public static function updateCdcBySI(\pdo $pdo, $codeSte = '', $si = ''):string
    {
        $sisecto = \Helpers\Outil\Roles::droit($pdo, 'SISECTOR', 'PARAMSTE');
        if ($sisecto) {
            $prefix_code_dir = str_pad($codeSte, 7, ' ', STR_PAD_RIGHT);
        } else {
            $prefix_code_dir = '       ';
        }

        $req = "SELECT 
                    VALEUR
                from CS_DON with (nolock) 
                WHERE COD_TAB='SECTIMPU' AND COD_ENR = :codeEnr 
                AND NUM_FIX = (SELECT LIB01DICO FROM DILISPEC with(nolock) WHERE TYPEDICO='PARAMSKEMA' AND CODEDICO='NCS_SECTIMPU_CODECOMP') ";
        
        $arrayReq = [
            ':codeEnr' => $prefix_code_dir . $si,
        ];

        $champSupp = $pdo->prepare($req);
        $champSupp->execute($arrayReq);
        
        return $champSupp->fetch()['VALEUR'];
    }
}
