<?php

namespace Modules\Commande\Rules;

use Entity\Model\Paravrac;
use Entity\Model\XtLig;
use Helpers\Ariane\Ariane;
use Helpers\Outil\Roles;
use Helpers\Outil\Sql;
use Psr\Http\Message\ServerRequestInterface;

class GestionCommande
{
    protected ServerRequestInterface $request;
    protected \PDO $pdo;
        
    /**
     * __construct
     *
     * @param  ServerRequestInterface $request
     * @return void
     */
    public function __construct(ServerRequestInterface  $request, \PDO $pdo)
    {
        $this->request  = $request;
        $this->pdo      = $pdo;
    }
    
    /**
     * init
     *
     * @return bool
     */
    public function init() : bool
    {
        if (Roles::droitsUtilisateur($this->pdo, 'HA_CA_VIS') || ($_SESSION['SIG_CDE'] || Roles::droitsUtilisateur($this->pdo, 'HA_DA_CRE'))) {
            return true;
        }
        return false;
    }

    public function ajaxValidate(): bool
    {
        if (Roles::droitsUtilisateur($this->pdo, 'HA_CA_VAL') || Roles::droitsUtilisateur($this->pdo, 'HA_CA_MVI')) {
            return true;
        }

        return false;
    }

    public function popupCreateAvenant(): bool
    {
        if (Roles::droitsUtilisateur($this->pdo, 'HA_CA_CRA')) {
            return true;
        }

        return false;
    }

    
    public function ajaxCreateAvenant(): bool
    {
        if (Roles::droitsUtilisateur($this->pdo, 'HA_CA_CRA')) {
            return true;
        }

        return false;
    }

    /**
     * @todo improve
     * @param array<mixed> $commande
     */
    public static function canUpdateCommandeTypDoc(\PDO $pdo, array $commande) :bool
    {
        
        // Règle particulière interfaces commande type "SAP"
        if ((new Paravrac($pdo))->getGeneric('INTERFACES_ACTIVES') == 'O'
            && $commande['Societe_TYPE_INT'] == 'SAP'
            && Sql::toBool($commande['INTERFA']) // commande est dite "Interfaçable"
            && intval($commande['getCommandeSapInterface']) == 0
        ) {
            return false;
        }
        
        
        $droit = Roles::droitsUtilMulti($pdo, [
            'HA_CA_CMV',
            'HA_CA_MVI',
            'HA_CA_MODY',
        ]);
        return (
            $droit['HA_CA_CMV'] // droit général de modification de commande
                || (
                    $droit['HA_CA_MVI'] // User dispose du droit de modifier ses commandes
                    && $commande['ACH'] == $_SESSION['Session_Login'] // User est acheteur (CA_ENTET.ACH)
                )
            )
            && $droit['HA_CA_MODY']
            && $commande['ETAT_CDE'] == 'A'
            && $commande['DTE_VALID'] == '00000000'; // Ne doit pas être validée
    }


    /**
     * @return bool
     */
    public function index(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function detailLigneCommande(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function detailCommande(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function blocNote(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function popupExport(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function exportCde(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function impressionPDF(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function ajaxAjoutSignataireApresValidation(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function ajaxConfirmationValidationV2(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function ajaxConfirmValidate(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function popupPlanning(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function ajaxConfirmSignataire(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function updateDocTypePopup(): bool
    {
        return true;
    }

    /**
     * @return bool
     */
    public function updateDocType(): bool
    {
        return true;
    }
}
