<?php

namespace Modules\Commande\Rules;

use Entity\Model\CaEntet;
use Helpers\Outil\Roles;
use Psr\Http\Message\ServerRequestInterface;

class AjaxModificationCommande
{
    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
    {
        $idCommande = $this->request->getAttribute('idCommande');
        Roles::droitsUtilisateur($this->pdo, 'HA_CA_MVI');

        $caEntet = new CaEntet($this->pdo);
        $infoCde = $caEntet->find($idCommande);

        if (Roles::droitsUtilisateur($this->pdo, 'HA_CA_CMV') ||(Roles::droitsUtilisateur($this->pdo, 'HA_CA_MVI') && $infoCde['ACH'] == $_SESSION["Session_Login"])) {
            return true;
        }
        return false;
    }
}
