<?php

namespace Modules\Stock\Trait\Controller;

use Exception;
use GuzzleHttp\Psr7\Response;
use Helpers\Fichier\Fichier;
use Helpers\Webservice\Wsdl;
use Modules\Stock\Validator\BonSortieValidator;
use SimpleXMLElement;

trait BonSortieControllerTrait
{
    protected function getBonSortieValidator(): BonSortieValidator
    {
        return new BonSortieValidator($this->pdo, $this->getTranslationService());
    }

    /**
     * @throws Exception
     */
    protected function impression(bool $isValorisee): string|Response
    {
        $noBs = $this->request->getAttribute('noBs');
        $nonChiffre = $isValorisee ? 'false':'true';
        $xml = '<BSIn LngUtilisation="'.htmlspecialchars($_SESSION['Session_Langue']).'">
                    <NumeroBS>'.$noBs.'</NumeroBS>
                    <NonChiffre>'.$nonChiffre.'</NonChiffre>
                </BSIn>';

        $wsdl = new Wsdl('Oxaproc_Gen.wsdl', 'OXAPRC_IMPRIMEBS', $xml, false);
        $dsOut = new SimpleXMLElement($wsdl->responseData);

        if ($dsOut->Retour == 'E') {
            return $this->createErrorResponse($dsOut->MsgErreur, 500);
        }

        $fichierPdf = $dsOut->FichierPDF;
        $isValoriseShardName = $isValorisee ? '': 'non_';

        return Fichier::UploadClient($fichierPdf, 'Impression_'.$isValoriseShardName.'valorisee_'.$noBs.'_'.uniqid().'.pdf');
    }
}
