<?php
namespace Modules\GlobalApplication\Controller;

use Skeleton\Controller\Controller;
use GuzzleHttp\Psr7\Response;

class CalculPrixController extends Controller
{
    public function calculPrixAction()
    {
        $post       = $this->request->getParsedBody();
        $retour = [];
        
        $Fourniss = new \Entity\Model\Fourniss($this->pdo);
        $Fourniss = $Fourniss->find($post['CODE_FOU']);
        $post['REMISE_FOU'] = $Fourniss['REMISE'] ?? '';
            
        $res = new \Modules\GlobalApplication\WebService\RechercheAcdFouPrix();
        $resCalculPrix = $res->CalculPrix($post);
        $retour['reponseCalculPrix'] = new \SimpleXMLElement($resCalculPrix->responseData);
        if ($retour['reponseCalculPrix']['Statut']=='O') {
            $Fabricant = new \Entity\Model\Fabricant($this->pdo);
            $retour['reponseCalculPrix']['FAB_LIBELLE'] = $Fabricant->find($retour['reponseCalculPrix']['FAB'])['RS'] ?? '';

            $Gamme = new \Entity\Model\Gamme($this->pdo);
            $retour['reponseCalculPrix']['GAM_LIBELLE'] = $Gamme->find($retour['reponseCalculPrix']['GAM'])['DESIGN'] ?? '';
            
            $SousFamille = new \Entity\Model\SousFamille($this->pdo);
            $retour['reponseCalculPrix']['SSFAM_LIBELLE'] = $SousFamille->find($retour['reponseCalculPrix']['SSFAM'])['DESIGNAT'] ?? '';
            //$retour['reponseAcdFouPrix']['SSFAM_LOCK'] = $retour['reponseAcdFouPrix']['SSFAM'] != '' ? 1 : 0;
            $retour['reponseAcdFouPrix']['SSFAM_LOCK'] = 0;
            
            $TVA = new \Entity\Model\TVA($this->pdo);
            $retour['reponseCalculPrix']['TVA_LIBELLE'] = $TVA->find($retour['reponseCalculPrix']['TVA'])['LIBELLE'] ?? '';
        }
        
        return new Response(200, [
            
        ], (string) \json_encode($retour));
    }
}
