<?php
namespace Modules\RechercheArticles\PunchOut;

use Entity\Model\Centre;
use Entity\Model\Hastutil;
use Modules\RechercheArticles\PunchOut\PunchOut;

class PunchOutOCI extends PunchOut
{
    // Login de connexion
    /**
     * @var string $login
     */
    protected $login;

    // Password de connexion
    /**
     * @var string $password
     */
    protected $password;

    // Numéro du compte utilisé
    /**
     * @var string $num_compte
     */
    protected $num_compte;

    // Identifiant de la société
    /**
     * @var string $id_societe
     */
    protected $id_societe;

    // Version OCI  utilisée
    /**
     * @var string $oci_version
     */
    protected $oci_version;

    public function __construct(\PDO $pdo, string $id_cat)
    {
        parent::__construct($pdo, $id_cat);
        
        // Initialisation des propriétés
        $this->login        = $this->info_catalogue['ID_CLI_LOGIN'];
        $this->password     = $this->info_catalogue['ID_SRV_PASSW'];
        $this->num_compte   = $this->info_catalogue['NUMCOMPTE'];
        $this->id_societe   = $this->info_catalogue['ID_SOCIETE'];
        $this->oci_version  = sprintf('%01.1f', $this->info_catalogue['VERSION_OCI']);
    }

    public function getUrlPunchOut() : string
    {
        $centre = new Centre($this->pdo);
        $infoCentre = $centre->find($_SESSION['Session_CodeCtrLivraison']);

        $hastutil = new Hastutil($this->pdo);
        $infoUser = $hastutil->find($_SESSION['Session_Login']);

        $listeParam = $this->punchOuRepo->getParamPunchOut($this->info_catalogue['IDFORMAT']);

        if (!$listeParam) {
            if (false !== stripos($this->url_connexion_po, '?')) {
                $this->url_connexion_po = $this->url_connexion_po . '&';
            } else {
                $this->url_connexion_po = $this->url_connexion_po . '?';
            }
            
            $this->url_connexion_po .= "USERNAME={$this->login}";
            $this->url_connexion_po .= "&PASSWORD={$this->password}";
            $this->url_connexion_po .= "&account={$this->num_compte}";
            $this->url_connexion_po .= "&company={$this->id_societe}";
            $this->url_connexion_po .= "&OCI_VERSION={$this->oci_version}";
            $this->url_connexion_po .= "&DPT={$infoCentre['CPOSTAL']}";
            $this->url_connexion_po .= '&HOOK_URL=' . ($this->url_retour_oxaproc);
        } else {
            foreach ($listeParam as $param) {
                if (false !== stripos($this->url_connexion_po, '?')) {
                    $this->url_connexion_po = $this->url_connexion_po . '&';
                } else {
                    $this->url_connexion_po = $this->url_connexion_po . '?';
                }
                
                if ($param['TYPE_PARAM']=='DYNAMIQUE') {
                    if (substr($param['VALEUR_PARAM'], 0, 9) == 'HASTUTIL.') {
                        $champ = substr($param['VALEUR_PARAM'], 9);
                        $value = $infoUser[$champ];
                        $this->url_connexion_po .= "{$param['NOM_PARAMETRE']}={$value}";
                    } elseif ($param['NOM_PARAMETRE']=='USERNAME' && $param['VALEUR_PARAM']!='') {
                        $this->url_connexion_po .= "{$param['VALEUR_PARAM']}={$this->login}";
                    } elseif ($param['NOM_PARAMETRE']=='PASSWORD' && $param['VALEUR_PARAM']!='') {
                        $this->url_connexion_po .= "{$param['VALEUR_PARAM']}={$this->password}";
                    } elseif ($param['NOM_PARAMETRE']=='account' && $param['VALEUR_PARAM']!='') {
                        $this->url_connexion_po .= "{$param['VALEUR_PARAM']}={$this->num_compte}";
                    } elseif ($param['NOM_PARAMETRE']=='company' && $param['VALEUR_PARAM']!='') {
                        $this->url_connexion_po .= "{$param['VALEUR_PARAM']}={$this->id_societe}";
                    } elseif ($param['NOM_PARAMETRE']=='OCI_VERSION' && $param['VALEUR_PARAM']!='') {
                        $this->url_connexion_po .= "{$param['VALEUR_PARAM']}={$this->oci_version}";
                    } elseif ($param['NOM_PARAMETRE']=='DPT' && $param['VALEUR_PARAM']!='') {
                        $this->url_connexion_po .= "{$param['VALEUR_PARAM']}={$infoCentre['CPOSTAL']}";
                    } elseif ($param['NOM_PARAMETRE']=='HOOK_URL' && $param['VALEUR_PARAM']!='') {
                        $this->url_connexion_po .= "{$param['VALEUR_PARAM']}={$this->url_retour_oxaproc}";
                    }
                } else {
                    $this->url_connexion_po .= "{$param['NOM_PARAMETRE']}={$param['VALEUR_PARAM']}";
                }
            }
        }

        return $this->url_connexion_po;
    }
}
