<?php declare(strict_types=1);

namespace Modules\Additional\Specifique\Client112\Modules\Facture\Service;

use Helpers\Validator\Validator;
use Modules\Additional\Specifique\Client112\Modules\Facture\Repository\ParamSIContratFournisseurRepository;

class CreationFactureService
{
    /**
     * Summary of initArticleSansCommande
     * @param string|int $numFact
     * @param array<mixed> $cde
     * @param \PDO $pdo
     * @param Validator $validator
     * @return array<mixed>
     */
    public static function initArticleSansCommande(
        string|int $numFact,
        string $codeFrs,
        array $cde,
        \PDO $pdo,
        Validator $validator,
    ): array {
        $result = (new ParamSIContratFournisseurRepository($pdo))->findCodeSI($numFact, $codeFrs);

        if (!empty($result['CODE_SI']) && (\intval($cde['NUM_CDE'] ?? '') === 0)) {
            $validator->sectionImputationZoom($result['CODE_SI'], '', $pdo);

            if ($validator->isValid()) {
                $cde['LIBELLE_SI'] = $result['LIBELLE_SI'];
                $cde['CODE_SI']    = $result['CODE_SI'];
            }
        }

        return $cde;
    }
}
