<?php

namespace Modules\Consultation\Controller;

use Entity\Model\AoEntet;
use Entity\Model\AoFour;
use Entity\Model\AoFourQR;
use Entity\Model\Docatt;
use Entity\Model\Fourniss;
use Entity\Model\Hastutil;
use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Helpers\Fichier\Fichier;
use Helpers\Fichier\Gestion_Fic_PJ;
use Helpers\Outil\Mail;
use Helpers\Outil\Outils;
use Helpers\Outil\Sql;
use Helpers\Session\ParseBase1;
use Helpers\Trad\Trad;
use Helpers\User\User;
use Modules\Ajax\WebService\EnvoiMail;
use Modules\Consultation\MenuContextuel\QuestionReponseMenuContextuel;
use Modules\Consultation\Repository\AoEvent;
use Modules\Consultation\Repository\Consultation;
use Modules\Consultation\Repository\GestionConsultation;
use Modules\Consultation\Repository\QuestionReponseRepo;
use Modules\Consultation\Repository\Reponse;
use Modules\Consultation\Service\ExportReponseService;
use Skeleton\Controller\Controller;

class QuestionReponseController extends Controller
{
    public function listeQuestionAction():string
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');
        $trad = $this->getTranslationService();

        $consult = (new AoEntet($this->pdo))->find($idAO, ['VALCHXFOU']);
        $isTerminer     = ($consult['VALCHXFOU'] != '00000000');

        $listeFournisseur = (new QuestionReponseRepo($this->pdo))->listeFournisseur($idAO);

        if ($listeFournisseur) {
            if (!$idFrs) {
                $idFrs = $listeFournisseur[0]['CODE_FOU'];
                $typeFG = $listeFournisseur[0]['TYPE_FG'];
            }
    
            $listeQR = (new QuestionReponseRepo($this->pdo))->getListeQuestion($idAO, $idFrs, $typeFG);

            if (!$isTerminer) {
                $menuContextuel = (new QuestionReponseMenuContextuel($this->pdo, $this->router, $trad));
                foreach ($listeQR as &$qr) {
                    $qr['menuContextuel'] = $menuContextuel->generate($qr);
                }
            }
        }

        Ariane::generate(1, $trad->t('Questions/Réponses'));

        return $this->renderer->render('@Consultation/QuestionReponse/liste', [
            'idAO'      => $idAO,
            'listeFrs'  => $listeFournisseur,
            'idFrs'     => $idFrs,
            'typeFG'    => $typeFG,
            'listeQR'   => $listeQR??[],
            'isTerminer'    => $isTerminer,
        ]);
    }

    public function popupDocumentQRAction():string
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $idQst = $this->request->getAttribute('idQst');
        $typeFG = $this->request->getAttribute('typeFG');
        $QR = $this->request->getAttribute('QR');

        if (!in_array($QR, ['R', 'Q'])) {
            return'';
        }

        $listeDocument = (new Docatt($this->pdo))->findAll([
            'NO_DA'     => 'AO_FOUR_QR_'.$QR, //'AO_FOUR_QR_R' ou 'AO_FOUR_QR_Q'
            'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
        ]);

        return $this->renderer->render('@Consultation/QuestionReponse/listeDocumentQR', [
            'listeDocument' => $listeDocument,
        ]);
    }

    public function createEditQuestionAction():string
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $idQst = $this->request->getAttribute('idQst');
        $typeFG = $this->request->getAttribute('typeFG');
        $trad = $this->getTranslationService();

        Ariane::generate(2, $trad->t('Questions'));

        if ($idQst) {
            $question = (new AoFourQR($this->pdo))->find([
                'NO_AO'         => (int)$idAO,
                'CODE_FOU'      => $idFrs,
                'TYPE_FG'       => $typeFG,
                'ID_QR'         => $idQst,
            ]);
            
            $listeDoc = (new Docatt($this->pdo))->findAll([
                'NO_DA'     => 'AO_FOUR_QR_Q',
                'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
            ]);
        }
        return $this->renderer->render('@Consultation/QuestionReponse/createEditQuestion', [
            'idAO'      => $idAO,
            'idFrs'     => $idFrs,
            'idQst'     => $idQst,
            'typeFG'    => $typeFG,
            'question'  => $question??[],
            'listeDoc'  => $listeDoc??[],
            'rsFrs'     => (new Fourniss($this->pdo))->find($idFrs, ['RS'])['RS']??'',
        ]);
    }

    public function validateQuestionAction():string|response
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');
        $idQst = $this->request->getAttribute('idQst');
        
        $trad = $this->getTranslationService();
        $validator = $this->createValidator();

        $post = $this->request->getParsedBody();

        $listePJ = [];
        foreach ($post as $k => $v) {
            if (str_starts_with($k, 'DC-')) {
                list($dc, $field, $ordre) = explode('-', $k);
                $listePJ[$ordre][$field] = $v;
            }
        }

        $listeFileDelete = $post['deleteFile'];

        $validator->isStringAvecEspace($trad->t('Question'), $post['QUEST'], 1, 3000);

        if ($validator->isValid()) {
            if ($idQst) {
                (new AoFourQR($this->pdo))->update([
                    'QUEST'         => $post['QUEST'],
                    'DATE_ENV_REP'   => '00000000',
                ], [
                    'NO_AO'         => (int)$idAO,
                    'CODE_FOU'      => $idFrs,
                    'TYPE_FG'       => $typeFG,
                    'ID_QR'         => $idQst,
                ]);

                $ficPj = new Gestion_Fic_PJ($this->pdo);
                foreach ($listeFileDelete as $docDelete) {
                    $doc = (new Docatt($this->pdo))->find([
                        'NO_DA'     => 'AO_FOUR_QR_Q',
                        'NO_ORDRE'  => $docDelete,
                        'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
                    ]);

                    (new Docatt($this->pdo))->delete([
                        'NO_DA'     => 'AO_FOUR_QR_Q',
                        'NO_ORDRE'  => $docDelete,
                        'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
                    ]);

                    $ficPj->deleteFichier($doc['NOM_FIC']);
                }
            } else {
                $idQr = (int)(new AoFourQR($this->pdo))->getMaxOrdre(['NO_AO' => $idAO, 'CODE_FOU' => $idFrs, 'TYPE_FG'=>$typeFG], 'ID_QR');

                $idQr = $idQr+1;
                (new AoFourQR($this->pdo))->insert([
                    'NO_AO'         => (int)$idAO,
                    'CODE_FOU'      => $idFrs,
                    'TYPE_FG'       => $typeFG,
                    'ID_QR'         => $idQr,

                    'DATE_CREAT'    => date('Ymd'),
                    'CREATEUR'      => (new User())->getCodeUser(),

                    'QUEST'         => $post['QUEST'],
                ]);

                $idQst = $idQr;

                $aoEvent = new AoEvent($this->pdo);
                $aoEvent->rqInsertEventAO($idAO, 'QRQ', '', $idFrs, $typeFG, $idQr);
            }

            $ficPj = new Gestion_Fic_PJ($this->pdo);
            foreach ($listePJ as $pj) {
                $newName = $ficPj->insertFichier($pj['NOM_FIC']);

                if ($newName) {
                    (new Docatt($this->pdo))->insert([
                        'NO_DA'     => 'AO_FOUR_QR_Q',
                        'NO_ORDRE'  => $pj['NO_ORDRE'],
                        'TYPE_PJ'   => $pj['TYPE_PJ'],
                        'TITRE'     => $pj['TITRE'],
                        'NOM_FIC'   => $newName,
                        'DT_PARUT'  => date('Ymd'),
                        'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
                        'AUTEUR'    => (new User())->getCodeUser(),
                        'NOM_ORI'   => $pj['NOM_ORI']
                    ]);
                }
            }

            return new Response(443, [], (string) json_encode(['path' => $this->router->generateUri('Consultation.QuestionReponse.listeQuestion', ['idAO' => $idAO, 'typeFG' => $typeFG, 'idFrs' => $idFrs])]));
        } else {
            return new Response(200, [], $this->renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $validator->getError(),
                'desactiveRow' => true,
            ]));
        }
    }

    public function repondreAction():string
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');
        $idQst = $this->request->getAttribute('idQst');

        $trad = $this->getTranslationService();

        Ariane::generate(2, $trad->t('Réponses'));

        if ($idQst) {
            $question = (new AoFourQR($this->pdo))->find([
                            'NO_AO'         => (int)$idAO,
                            'CODE_FOU'      => $idFrs,
                            'TYPE_FG'       => $typeFG,
                            'ID_QR'         => $idQst,
                        ]);
    
            $listeDoc = (new Docatt($this->pdo))->findAll([
                'NO_DA'     => 'AO_FOUR_QR_R',
                'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
            ]);
        }

        return $this->renderer->render('@Consultation/QuestionReponse/createEditReponse', [
            'idAO'      => $idAO,
            'idFrs'     => $idFrs,
            'idQst'     => $idQst,
            'typeFG'    => $typeFG,
            'question'  => $question??[],
            'rsFrs'     => (new Fourniss($this->pdo))->find($idFrs, ['RS'])['RS']??'',
            'listeDoc'  => $listeDoc??[],
        ]);
    }

    public function validateReponseAction():Response
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');
        $idQst = $this->request->getAttribute('idQst');

        $post = $this->request->getParsedBody();

        $trad = $this->getTranslationService();

        $validator = $this->createValidator();

        $listePJ = [];
        foreach ($post as $k => $v) {
            if (str_starts_with($k, 'DC-')) {
                list($dc, $field, $ordre) = explode('-', $k);
                $listePJ[$ordre][$field] = $v;
            }
        }

        $validator->ischeck($post['PUBLIC']);
        $validator->isStringAvecEspace($trad->t('Réponse'), $post['REP'], 1, 3000);

        if ($post['PUBLIC']) {
            $validator->isStringAvecEspace($trad->t('Question reformulée'), $post['QUEST_PUBLIC'], 0, 3000);
        } else {
            $post['QUEST_PUBLIC'] = '';
        }

        $listeFileDelete = $post['deleteFile'];

        if ($validator->isValid()) {
            (new AoFourQR($this->pdo))->update([
                'REP'           => $post['REP'],
                '[PUBLIC]'      => $post['PUBLIC']? true:false,
                'QUEST_PUBLIC'  => $post['QUEST_PUBLIC'],
                'DATE_ENV_REP'      => '00000000',
            ], [
                'NO_AO'         => (int)$idAO,
                'CODE_FOU'      => $idFrs,
                'TYPE_FG'       => $typeFG,
                'ID_QR'         => $idQst,
            ]);

            if ($post['REP']) {
                $aoEvent = new AoEvent($this->pdo);
                $aoEvent->rqInsertEventAO($idAO, 'QRR', '', $idFrs, $typeFG, $idQst);
            } else {
                $aoEvent = new AoEvent($this->pdo);
                $aoEvent->rqInsertEventAO($idAO, 'QSR', '', $idFrs, $typeFG, $idQst);
            }


            if ($post['PUBLIC']) {
                (new AoFourQR($this->pdo))->update([
                    'DATE_ENV_REP'          => '00000000',
                ], [
                    'ORIGIN_CODE_FOU'   => $idFrs,
                    'ORIGIN_TYPE_FG'    => $typeFG,
                    'ORIGIN_ID_QR'      => $idQst,
                ]);

                $listeFrs = (new QuestionReponseRepo($this->pdo))->listeFrsWithoutQuestion($idAO, $typeFG, $idFrs, $idQst);

                foreach ($listeFrs as $frs) {
                    $maxQR = (int)(new AoFourQR($this->pdo))->getMaxOrdre([
                        'NO_AO' => $idAO,
                        'CODE_FOU' => $frs['CODE_FOU'],
                        'TYPE_FG' => $frs['TYPE_FG']
                    ], 'ID_QR');
                    $idQrInsert = $maxQR+1;
                    (new AoFourQR($this->pdo))->insert([
                        'NO_AO'         => (int)$idAO,
                        'CODE_FOU'      => $frs['CODE_FOU'],
                        'TYPE_FG'       => $frs['TYPE_FG'],
                        'ID_QR'         => $idQrInsert,
                        'PUBLIC'        => true,

                        'DATE_CREAT'    => date('Ymd'),
                        'CREATEUR'      => (new User())->getCodeUser(),

                        'ORIGIN_CODE_FOU'   => $idFrs,
                        'ORIGIN_TYPE_FG'    => $typeFG,
                        'ORIGIN_ID_QR'      => $idQst,
                    ]);
                }
            }

                $ficPj = new Gestion_Fic_PJ($this->pdo);
            foreach ($listePJ as $pj) {
                $newName = $ficPj->insertFichier($pj['NOM_FIC']);

                if ($newName) {
                    (new Docatt($this->pdo))->insert([
                        'NO_DA'     => 'AO_FOUR_QR_R',
                        'NO_ORDRE'  => $pj['NO_ORDRE'],
                        'TYPE_PJ'   => $pj['TYPE_PJ'],
                        'TITRE'     => $pj['TITRE'],
                        'NOM_FIC'   => $newName,
                        'DT_PARUT'  => date('Ymd'),
                        'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
                        'AUTEUR'    => (new User())->getCodeUser(),
                        'NOM_ORI'   => $pj['NOM_ORI']
                    ]);
                }
            }


                $ficPj = new Gestion_Fic_PJ($this->pdo);
            foreach ($listeFileDelete as $docDelete) {
                $doc = (new Docatt($this->pdo))->find([
                    'NO_DA'     => 'AO_FOUR_QR_R',
                    'NO_ORDRE'  => $docDelete,
                    'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
                ]);

                (new Docatt($this->pdo))->delete([
                    'NO_DA'     => 'AO_FOUR_QR_R',
                    'NO_ORDRE'  => $docDelete,
                    'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
                ]);

                $ficPj->deleteFichier($doc['NOM_FIC']);
            }

            return new Response(443, [], (string) json_encode(['path' => $this->router->generateUri('Consultation.QuestionReponse.listeQuestion', ['idAO' => $idAO, 'idFrs' => $idFrs])]));
        } else {
            return new Response(200, [], $this->renderer->render('@popup-erreur/ErreurCreation', [
                'ErreurValid' => $validator->getError(),
                'desactiveRow' => true,
            ]));
        }
    }
            
    public function deleteAction():Response
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');
        $idQst = $this->request->getAttribute('idQst');

        //delete Q/r
        (new AoFourQR($this->pdo))->delete([
            'NO_AO'         => (int)$idAO,
            'CODE_FOU'      => $idFrs,
            'TYPE_FG'       => $typeFG,
            'ID_QR'         => $idQst,
        ]);

        (new AoFourQR($this->pdo))->delete([
            'NO_AO'         => (int)$idAO,
            'ORIGIN_CODE_FOU'      => $idFrs,
            'ORIGIN_TYPE_FG'       => $typeFG,
            'ORIGIN_ID_QR'         => $idQst,
        ]);

        //delete document question
        $listedocQ = (new Docatt($this->pdo))->findAll([
            'NO_DA'     => 'AO_FOUR_QR_Q',
            'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
        ]);

        $ficPj = new Gestion_Fic_PJ($this->pdo);
        foreach ($listedocQ as $doc) {
            $ficPj->deleteFichier($doc['NOM_FIC']);
        }

        (new Docatt($this->pdo))->delete([
            'NO_DA'     => 'AO_FOUR_QR_Q',
            'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
        ]);


        //delete document reponse
        $listedocR = (new Docatt($this->pdo))->findAll([
            'NO_DA'     => 'AO_FOUR_QR_R',
            'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
        ]);

        foreach ($listedocR as $doc) {
            $ficPj->deleteFichier($doc['NOM_FIC']);
        }

        (new Docatt($this->pdo))->delete([
            'NO_DA'     => 'AO_FOUR_QR_R',
            'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
        ]);

        $aoEvent = new AoEvent($this->pdo);
        $aoEvent->rqInsertEventAO($idAO, 'QRS', '', $idFrs, $typeFG, $idQst);

        return new Response(200);
    }
        
    public function envoieUnitaireAction():string
    {
        $idAO   = $this->request->getAttribute('idAO');
        $idFrs  = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');
        $idQst  = $this->request->getAttribute('idQst');
        $trad   = $this->getTranslationService();


        $allCtcts = (new Consultation($this->pdo))->getAllCtcWithCheckIfOxalinkAccess($idAO, $idFrs, $typeFG);

        $listeOxalink = [];
        $listeNonOxalink = [];
        foreach ($allCtcts as $currentCtct) {
            if ($currentCtct['fourHasContactWithOxalinkAccess']) {
                $listeOxalink[] = $currentCtct;
            } else {
                $listeNonOxalink[] = $currentCtct;
            }
        }

        $question = (new QuestionReponseRepo($this->pdo))->getInfoFrsPourEnvoie($idAO, $idFrs, $typeFG, $idQst);
        $consultation = (new AoEntet($this->pdo))->find($idAO);
        $user = (new Hastutil($this->pdo))->find((new User())->getCodeUser());

        $listedocR = (new Docatt($this->pdo))->findAll([
            'NO_DA'     => 'AO_FOUR_QR_R',
            'COD_ENR'   => $idAO.$typeFG.$idFrs.$idQst,
        ]);

        $listePJ = [];
        foreach ($listedocR as $pj) {
            Fichier::copyFile((new ParseBase1())->getRep()->pj.$pj['NOM_ORI'], (new ParseBase1())->getRep()->tmp.$pj['NOM_ORI']);
            $listePJ[] = $pj['NOM_ORI'];
        }

        $infoMailNonOxalink = [
            'NAO' => $idAO,
            'TITREAO' => sql::getMemo($this->pdo, $consultation['NM_TITRE']),
            'OBJETAO' => $consultation['OBJET_AO'],
            'NOM_ACH' => $user['NOM'],
            'RS_SOC' => Outils::convertLibelleGlobal($this->pdo, $consultation['TYPE_SG'] == 'S' ? 'Societe' : 'GrpSociete', '', $consultation['CODE_STE']),
            'QUESTION' => $question['QUEST'],
            'REPONSE' => $question['REP'],
        ];

        $infoMailOxalink = [
            'NAO' => $idAO,
            'TITREAO' => sql::getMemo($this->pdo, $consultation['NM_TITRE']),
            'OBJETAO' => $consultation['OBJET_AO'],
            'NOM_ACH' => $user['NOM'],
            'RS_SOC' => Outils::convertLibelleGlobal($this->pdo, $consultation['TYPE_SG'] == 'S' ? 'Societe' : 'GrpSociete', '', $consultation['CODE_STE']),
            'URL'   => Sql::paravrac($this->pdo, 'PF_OXALINK', 'URLACCESOXALINK'),
        ];

        foreach ($listeNonOxalink as $ctct) {
            Mail::generateMail(
                $this->pdo,
                $this->getTranslationService(),
                $ctct['EMAILCONT'],
                'EXT_AO_FQR_REP_UNI',
                $ctct['COD_LNG'],
                $infoMailNonOxalink,
                [],
                $listePJ
            );
        }

        foreach ($listeOxalink as $ctct) {
            Mail::generateMail(
                $this->pdo,
                $this->getTranslationService(),
                $ctct['EMAILCONT'],
                'EXT_AO_FQR_REP_LINK',
                $ctct['COD_LNG'],
                $infoMailOxalink,
                [],
            );
        }

        (new AoFourQR($this->pdo))->update([
            'DATE_ENV_REP' => date('Ymd'),
        ], [
            'NO_AO'         => (int)$idAO,
            'CODE_FOU'      => $idFrs,
            'TYPE_FG'       => $typeFG,
            'ID_QR'         => $idQst,
        ]);

        $aoEvent = new AoEvent($this->pdo);
        $aoEvent->rqInsertEventAO($idAO, 'QRE', '', $idFrs, $typeFG, (int)$idQst);

        return $this->renderer->render('@popup-erreur/information', [
            'message' => $trad->t('Votre réponse a été envoyée avec succès.'),
        ]);
    }

    public function envoieReponseAction():Response
    {
        $idAO = $this->request->getAttribute('idAO');
        $idFrs = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');

        $trad = $this->getTranslationService();

        $exportReponse = new ExportReponseService(
            $this->pdo,
            $trad,
            (new QuestionReponseRepo($this->pdo)),
            (new Hastutil($this->pdo)),
            (new User()),
            (new AoEntet($this->pdo)),
            (new AoEvent($this->pdo)),
            (new Consultation($this->pdo))
        );

        $exportReponse->setidAO($idAO);
        if ($idFrs) {
            $post = $this->request->getParsedBody();

            if (!$post['selectedReponse']) {
                return new Response(200, [], $this->renderer->render('@popup-erreur/ErreurCreation', [
                    'ErreurValid' => $trad->t('Aucune réponse sélectionnée.'),
                    'desactiveRow' => true,
                ]));
            }

            $exportReponse->setFrs($typeFG, $idFrs);
            $exportReponse->setReponseSelected($post['selectedReponse']);
            $exportReponse->sendReponse(false);
        } else {
            $listeFrs = (new AoFour($this->pdo))->findAll(['NO_AO' => $idAO]);

            foreach ($listeFrs as $frs) {
                $exportReponse->setFrs($frs['TYPE_FG'], $frs['CODE_FOU']);
                $exportReponse->setReponseSelected([]);
                $exportReponse->sendReponse(true);
            }
        }

        return new Response(443, [], (string) json_encode([
            'path' => $this->router->generateUri('Consultation.QuestionReponse.listeQuestion', ['idAO' => $idAO, 'typeFG' => $typeFG, 'idFrs' => $idFrs])
        ]));
    }

    public function updateValueAction():Response
    {
        $idAO   = $this->request->getAttribute('idAO');
        $idFrs  = $this->request->getAttribute('idFrs');
        $typeFG = $this->request->getAttribute('typeFG');

        $post   = $this->request->getParsedBody();
        $idQst  = (int)$post['field'];
        ;

        $validator = $this->createValidator();

        $validator->isStringAvecEspace($this->getTranslationService()->t('Réponse'), $post['value'], 0, 3000);

        if (!$validator->isValid()) {
            return new Response(200, [

            ], (string) \json_encode([
                'statut' => 'B',
                'message' => substr($validator->getError(), 6),
            ]));
        }

        $infoQst = (new AoFourQR($this->pdo))->find([
            'NO_AO'         => (int)$idAO,
            'CODE_FOU'      => $idFrs,
            'TYPE_FG'       => $typeFG,
            'ID_QR'         => $idQst,
        ]);

        if ($infoQst['ORIGIN_CODE_FOU'] && $infoQst['ORIGIN_TYPE_FG'] && $infoQst['ORIGIN_ID_QR']) {
            $idFrs = $infoQst['ORIGIN_CODE_FOU'];
            $typeFG = $infoQst['ORIGIN_TYPE_FG'];
            $idQst = $infoQst['ORIGIN_ID_QR'];
        }

        (new AoFourQR($this->pdo))->update([
            'REP'               => $post['value'],
            'DATE_ENV_REP'      => '00000000',
        ], [
            'NO_AO'         => (int)$idAO,
            'CODE_FOU'      => $idFrs,
            'TYPE_FG'       => $typeFG,
            'ID_QR'         => $idQst,
        ]);

        if ($post['value']) {
            $aoEvent = new AoEvent($this->pdo);
            $aoEvent->rqInsertEventAO($idAO, 'QRR', '', $idFrs, $typeFG, $idQst);
        } else {
            $aoEvent = new AoEvent($this->pdo);
            $aoEvent->rqInsertEventAO($idAO, 'QSR', '', $idFrs, $typeFG, $idQst);
        }

        return new Response(200, [], (string) \json_encode([
            'statut'            => 'O',
        ]));
    }
}
