<?php

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

use Entity\Model\Centre;
use Entity\Model\CtcCtr;
use Entity\Model\OxaUserPanier;
use Entity\Model\OxaUserPanierLigne;
use Entity\Model\Sectimpu;
use Helpers\Outil\Sql;
use Modules\Additional\Specifique\Client177\Modules\Liste\Service\SpecifiqueCentreLivraison;
use Modules\Panier\Service\PanierEnteteService;
use Modules\Panier\Service\PanierService;

class UpdateValueCtrLivraison
{
    /**
     * @param \PDO $pdo
     * @param string $si
     * @param string $idLigne
     * @param bool $returnValue
     * @return bool|string
     */
    public static function initCentreLivraisonSI(\PDO $pdo, string $si = null, string $idLigne = null, bool $returnValue = false)
    {
        $entetePanier = (new OxaUserPanier($pdo))->find([
            'id_user' => $_SESSION['id_user'],
            'id_panier' => $_SESSION['id_panier'],
        ]);

        $newsCentre = SpecifiqueCentreLivraison::initCentreBySi($pdo, [
            'SI' => $si,
            'CODE_STE' => $entetePanier['CODE_STE'],
        ]);
        
        if ($idLigne) {
            if ($returnValue) {
                return $newsCentre;
            }

            (new OxaUserPanierLigne($pdo))->update([
                'CodeCtrLivraison' => $newsCentre
            ], [
                'id_user' => $_SESSION['id_user'],
                'id_panier' => $_SESSION['id_panier'],
                'id_lig' => $idLigne,
            ]);
        } else {
            return (new PanierService($pdo))->initCentre($pdo, $newsCentre);
        }

        return false;
    }
}
