<?php
namespace Modules\API\APIExterne\Controller;

use Skeleton\Controller\Controller;
use Helpers\Ariane\ArianeSub;
use Helpers\Trad\Trad;
use Entity\Model\Fourniss;
use Helpers\Highcharts\Highcharts;
use API\BVD\BVD;
use Helpers\Ariane\Ariane;
use Entity\Model\Pays;
use Helpers\Outil\Sql;
use Helpers\ChampsSupplementaire\ChampSupp;
use Modules\API\APIExterne\Repository\Partenaire;
use Helpers\Outil\Date;
use GuzzleHttp\Psr7\Response;

class BVDController extends Controller
{
    /**
     * @return Response|string
     */
    public function rechercheFrsAction()
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $ID_BVD = champSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);
        
        if ($post) {
            if ($post['ID_BVD']) {
                $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
                ChampSupp::updateChampSupp('FOURNISS', $idFournisseur, $id_cs, $post['ID_BVD'], $this->pdo);
                
                $partenaire = new Partenaire($this->pdo);
                $partenaire->insertPartenaireDataActivite($idFournisseur, 'BVD');
                
                $path = $this->router->generateUri('Fournisseur.GestionFournisseur.index');
                $message = Trad::strBuild(
                    $trad->t('Le fournisseur @1@ a bien été ajouté au partenaire BVD.'),
                    $idFournisseur
                );
                
                return $this->renderer->render('@popup-erreur/confirmationRedirection', [
                    'message' => $message,
                    'pathURL' => $path,
                ]);
            } else {
                $path = $this->router->generateUri('API.BVD.selectionFrs', ['idFournisseur' => $idFournisseur, 'idBVD' => $post['ID_BVD']]);
                
                return new Response(443, [
                    
                ], (string) \json_encode([
                    'statut' => 'O',
                    'path' => $path,
                    'postGestion' => $post,
                ]));
            }
        }
        
        $listePays = (new Pays($this->pdo))->listChoice();

        Ariane::generate(1, $trad->t('Recherche fournisseur'));
        
        return $this->renderer->render('@API/BVD/recherche', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            'listePays'         => $listePays,
            'fourniss'          => $fournis,
            'ID_BVD'            => $ID_BVD,
        ]);
    }
    
    public function selectionFrsAction() : string
    {
        $post = $this->request->getParsedBody();
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);

        $trad = $this->getTranslationService();
        
        $BVD = new BVD($this->pdo);
        $BVD->matchingBVD($post);
        
        $dataTMP = $BVD->getData();
        $listeFrs = $dataTMP->Body->MatchResponse->MatchResult;
        
        $BVD->disconnect();
        
        Ariane::generate(2, $trad->t('Liste fournisseur'));
        
        return $this->renderer->render('@API/BVD/selectionFrs', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            'listeFrs'          => $listeFrs,
            'idFournisseur'     => $idFournisseur,
            'fourniss'          => $fournis,
        ]);
    }
    
    public function attacherAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $idBVD = $this->request->getAttribute('idBVD');
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        ChampSupp::updateChampSupp('FOURNISS', $idFournisseur, $id_cs, $idBVD, $this->pdo);
        
        $partenaire = new Partenaire($this->pdo);
        $partenaire->insertPartenaireDataActivite($idFournisseur, 'BVD');
        $trad = $this->getTranslationService();
        
        $path = $this->router->generateUri('Fournisseur.GestionFournisseur.index');
        $message = Trad::strBuild($trad->t('Le fournisseur @1@ a bien été ajouté au partenaire BVD.'), $idFournisseur);
        
        return $this->renderer->render('@popup-erreur/confirmationRedirection', [
            'message' => $message,
            'pathURL' => $path,
        ]);
    }
    
    /**
     * @return Response|string
     */
    public function dettacherAction()
    {
        $post = $this->request->getParsedBody();
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $trad = $this->getTranslationService();
        
        
        if (isset($post['idFournisseurPopup'])) {
            $fourParamData = new \Entity\Model\FourPartenaireData($this->pdo);
            $fourParamData->delete(['CODE_FOU'=>$idFournisseur, 'CODEPARTENAIRE'=> 'BVD']);
            
            $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
            ChampSupp::deleteCS('FOURNISS', $idFournisseur, $this->pdo, $id_cs);
            

            return $this->createJsonResponse([
                'statut' => 'OK',
                'message' => $trad->t("La société vient d'être détachée de BVD"),
            ]);
        }
        
        return $this->renderer->render('@API/BVD/dettacher', [
            'idFournisseur' => $idFournisseur,
        ]);
    }
    
    public function indexAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();

        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $idBVD = ChampSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);
        
        $listeChpsAccueil = [
            'NAME',
            'COUNTRY_ISO_CODE',
            'BVD_ID_NUMBER',
            'NATIONAL_ID',
            'NATIONAL_ID_LABEL',
            'STREET_AND_STREET_NUMBER',
            'POSTCODE_ADDITIONAL',
            'CITY_ADDITIONAL',
            'COUNTRY_ADDITIONAL',
            'PHONE_NUMBER_ADDITIONAL',
            'WEBSITE',
            'EMAIL',
            'INDUSTRY_PRIMARY_LABEL',
            'INCORPORATION_DATE',
            'STATUS',
            'OPRE',
            'YEAR_LAST_ACCOUNTS',
            'VAR1',
            'SH_COUNT',
            'SUB_COUNT',
            'PL',
            'VAR2',
            
            'MORRAT',
            'MORRAT_1',
            'MORRAT_2',

            'G2Scores_CompassScore',

            'TRUCOST_SCORE_ENVIRSCORE',

            'CRIF_SCORE',
            'VAR3',
            'WKCA',
            'GEAR',
        ];
        
        $listeChpsProfilFinancier = [
            'YEAR_LAST_ACCOUNTS_1',
            
            'OPRE_1',
            'OPRE_2',
            'OPRE_3',
            
            'PLBT',
            'PLBT_1',
            'PLBT_2',
            
            'PL',
            'PL_1',
            'PL_2',
            
            'CF',
            'CF_1',
            'CF_2',
            
            'TOAS',
            'TOAS_1',
            'TOAS_2',
            
            'SHFD',
            'SHFD_1',
            'SHFD_2',
            
            'CURR',
            'CURR_1',
            'CURR_2',
            
            'PRMA',
            'PRMA_1',
            'PRMA_2',
            
            'RSHF',
            'RSHF_1',
            'RSHF_2',
            
            'RCEM',
            'RCEM_1',
            'RCEM_2',
            
            'SOLR',
            'SOLR_1',
            'SOLR_2',
            
            'EMPL',
            'EMPL_1',
            'EMPL_2',
        ];
        
        $bvdTEST = new BVD($this->pdo);
        $bvdTEST->generateTokenById($idBVD);

        $valueFrs = $bvdTEST->getDataBVD($listeChpsAccueil);
        $profilFinancier = $bvdTEST->getDataBVD($listeChpsProfilFinancier);
        $bvdTEST->disconnect();

        $score = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT));
        $score_previous = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT[1]));

        if ($score>=$score_previous) {
            $tendance = 'haut';
        } else {
            $tendance = 'bas';
        }

        // Border color for scores
        $score_MORE         = $valueFrs->record->MORRAT[0];
        $score_CRIT         = $valueFrs->record->CRIF_SCORE[0];
        $score_TRUST        = (int) $valueFrs->record->TRUCOST_SCORE_ENVIRSCORE;
        $score_COMP         = (int) $valueFrs->record->G2Scores_CompassScore;
        $class              = [];
            // MORE
        if ($score_MORE == 'AAA' || $score_MORE == 'AA' || $score_MORE == 'A') {
            $class["MORE"]              = 'br_bc_g';
        } elseif ($score_MORE == 'BBB' || $score_MORE == 'BB') {
            $class["MORE"]              = 'br_bc_y';
        } elseif ($score_MORE == 'B' || $score_MORE == 'CCC') {
            $class["MORE"]              = 'br_bc_o';
        } elseif ($score_MORE == 'CC' || $score_MORE == 'C' || $score_MORE == 'D') {
            $class["MORE"]              = 'br_bc_r';
        }
            // CRIF
        if ($score_CRIT < 500) {
            $class["CRIT"]              = 'br_bc_r';
        } elseif ($score_CRIT > 500 && $score_CRIT < 650) {
            $class["CRIT"]              = 'br_bc_y';
        } elseif ($score_CRIT > 650) {
            $class["CRIT"]              = 'br_bc_g';
        }
            // RCOM
        if ($score_TRUST >= 0 && $score_TRUST < 499) {
            $class["TRUST"]             = 'br_bc_g';
        } elseif ($score_TRUST > 500 && $score_TRUST < 699) {
            $class["TRUST"]             = 'br_bc_y';
        } elseif ($score_TRUST > 700 && $score_TRUST < 1000) {
            $class["TRUST"]             = 'br_bc_r';
        }
            // RENV TRUST
        if ($score_COMP >= 0 && $score_COMP < 40) {
            $class["COMP"]              = 'br_bc_g';
            echo 'Here';
        } elseif ($score_COMP > 40 && $score_COMP < 60) {
            $class["COMP"]              = 'br_bc_y';
        } elseif ($score_COMP > 60) {
            $class["COMP"]              = 'br_bc_r';
        }
        
        ArianeSub::generate(0, null, '<img src ="'.$this->contenaire->get('baseUrl').'img/API/logo-BVD.png" height="20px"/>', $_SERVER['REQUEST_URI'], 1, $post);

        return $this->renderer->render('@API/BVD/accueil', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            "code_fournisseur" => $idFournisseur,
            "libelle_fournisseur" => $fournis["RS"],
            'valueFrs' => $valueFrs,
            'profilFinancier' => $profilFinancier,
            
            'tendeande' => $tendance,
            'score'     => $score,
            'class' => $class,
        ]);
    }
    
    public function chiffreAffaireAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $idBVD = ChampSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);
        
        $listeChpsAccueil = [
            'NAME',
            'COUNTRY_ISO_CODE',
            'BVD_ID_NUMBER',
            'NATIONAL_ID',
            'NATIONAL_ID_LABEL',
            'STREET_AND_STREET_NUMBER',
            'POSTCODE_ADDITIONAL',
            'CITY_ADDITIONAL',
            'COUNTRY_ADDITIONAL',
            'PHONE_NUMBER_ADDITIONAL',
            'WEBSITE',
            'EMAIL',
            'INDUSTRY_PRIMARY_LABEL',
            'INCORPORATION_DATE',
            'STATUS',
            'OPRE',
            'YEAR_LAST_ACCOUNTS',
            'VAR1',
            'SH_COUNT',
            'SUB_COUNT',
            'PL',
            'VAR2',
            
            'MORRAT',
            'MORRAT_1',
        ];
        
        $listeChpsProfilFinancier = [
            'YEAR_LAST_ACCOUNTS_1',
            
            'OPRE_1',
            'OPRE_2',
            'OPRE_3',
        ];
        
        $bvdTEST = new BVD($this->pdo);
        $bvdTEST->generateTokenById($idBVD);
        
        $valueFrs = $bvdTEST->getDataBVD($listeChpsAccueil);
        $profilFinancier = $bvdTEST->getDataBVD($listeChpsProfilFinancier);
        
        $score = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT));
        $score_previous = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT[1]));
        
        if ($score>=$score_previous) {
            $tendance = 'haut';
        } else {
            $tendance = 'bas';
        }
        
        $bvdTEST->disconnect();
        
        
        ArianeSub::generate(1, 1, "Chiffre d'affaire", $_SERVER['REQUEST_URI'], 1, $post);
        
        return $this->renderer->render('@API/BVD/chiffreAffaire', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            "code_fournisseur" => $idFournisseur,
            "libelle_fournisseur" => $fournis["RS"],
            'valueFrs' => $valueFrs,
            'score'     => $score,
            'profilFinancier' => $profilFinancier,
            'tendance' => $tendance,
        ]);
    }
    
    public function actionnaireAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        ArianeSub::generate(1, 1, "Actionnaire", $_SERVER['REQUEST_URI'], 1, $post);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $idBVD = ChampSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);

        $listeChpsactionnaire = [
            'NAME',
            'COUNTRY_ISO_CODE',
            'BVD_ID_NUMBER',
            'NATIONAL_ID',
            'NATIONAL_ID_LABEL',
            'STREET_AND_STREET_NUMBER',
            'POSTCODE_ADDITIONAL',
            'CITY_ADDITIONAL',
            'COUNTRY_ADDITIONAL',
            'PHONE_NUMBER_ADDITIONAL',
            'WEBSITE',
            'EMAIL',
            'INDUSTRY_PRIMARY_LABEL',
            'INCORPORATION_DATE',
            
            'GUO_NAME',
            'GUO_COUNTRY_ISO_CODE',
            'GUO_DIRECT_PCT',
            'GUO_TOTAL_PCT',
            'GUO_OPRE',
            'GUO_TOAS',
            'GUO_EMPL',
            'GUO_INFORMATION_DATE',
            'SUB_NAME',
            'SUB_COUNTRY_ISO_CODE',
            'SUB_DIRECT_PCT',
            'SUB_TOTAL_PCT',
            'SUB_OPRE',
            'SUB_TOAS',
            'SUB_EMPL',
            'SUB_INFORMATION_DATE',
            
            'MORRAT',
            'MORRAT_1',
        ];
        
        $bvdTEST = new BVD($this->pdo);
        $bvdTEST->generateTokenById($idBVD);
        
        $valueFrs = $bvdTEST->getDataBVD($listeChpsactionnaire);
        $bvdTEST->disconnect();
        
        $score = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT));
        $score_previous = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT[1]));
        
        if ($score>=$score_previous) {
            $tendance = 'haut';
        } else {
            $tendance = 'bas';
        }
        
        $listeActionnaire = [];
        $listeChpsActionnaire = [
            'GUO_NAME',
            'GUO_COUNTRY_ISO_CODE',
            'GUO_DIRECT_PCT',
            'GUO_TOTAL_PCT',
            'GUO_OPRE',
            'GUO_TOAS',
            'GUO_EMPL',
            'GUO_INFORMATION_DATE',
        ];
        
        foreach ($valueFrs->record->GUO_NAME->GUO_NAME as $actionnaire) {
            $x = intval($actionnaire->attributes()->index);
            
            foreach ($listeChpsActionnaire as $chps) {
                $listeActionnaire[$x][$chps] = strval($valueFrs->record->$chps->$chps->$x);
            }
        }

        $listeFilliales = [];
        $listeChpsFilliales = [
            'SUB_NAME',
            'SUB_COUNTRY_ISO_CODE',
            'SUB_DIRECT_PCT',
            'SUB_TOTAL_PCT',
            'SUB_OPRE',
            'SUB_TOAS',
            'SUB_EMPL',
            'SUB_INFORMATION_DATE',
        ];
        
        foreach ($valueFrs->record->SUB_NAME->SUB_NAME as $filliales) {
            $x = intval($filliales->attributes()->index);
            
            foreach ($listeChpsFilliales as $chps) {
                $listeFilliales[$x][$chps] = strval($valueFrs->record->$chps->$chps->$x);
            }
        }
        
        return $this->renderer->render('@API/BVD/actionnaire', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            "code_fournisseur" => $idFournisseur,
            'valueFrs' => $valueFrs,
            "libelle_fournisseur" => $fournis["RS"],
            'listeFilliales' => $listeFilliales,
            'listeActionnaire' => $listeActionnaire,
            
            'tendeande' => $tendance,
            'score'     => $score,
        ]);
    }
    
    public function indicateurMOREAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $idBVD = ChampSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);

        $color = Highcharts::degraderCouleur(2);
        
        $listeChps = [
            'NAME',
            'COUNTRY_ISO_CODE',
            'BVD_ID_NUMBER',
            'NATIONAL_ID',
            'NATIONAL_ID_LABEL',
            'STREET_AND_STREET_NUMBER',
            'POSTCODE_ADDITIONAL',
            'CITY_ADDITIONAL',
            'COUNTRY_ADDITIONAL',
            'PHONE_NUMBER_ADDITIONAL',
            'WEBSITE',
            'EMAIL',
            'INDUSTRY_PRIMARY_LABEL',
            'INCORPORATION_DATE',

            'MORE_YEAR',
            'MORE_YEAR_1',
            'MORE_YEAR_2',
            
            'MORRAT',
            'MORRAT_1',
            'MORRAT_2',
            'MORDEF',
            'MORCONF',
            'MORACT',
            'MORGRPRAT',
            'MORCREDLIMIT',
            'MORE_CreditLimitLbl',
            
        ];
        
        $bvdTEST = new BVD($this->pdo);
        $bvdTEST->generateTokenById($idBVD);
        
        $valueFrs = $bvdTEST->getDataBVD($listeChps);
        $bvdTEST->disconnect();

        $score = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT));
        $score_previous = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT[1]));
        
        if ($score>=$score_previous) {
            $tendance = 'haut';
        } else {
            $tendance = 'bas';
        }
        
        $valueHighchaets = $bvdTEST->transcoScoreMORE(strval($valueFrs->record->MORRAT));
        $i=0;
        $graphPalmares = [];
        $categorie = [];
        foreach ($valueFrs->record->MORRAT as $scoreAnnee) {
            $lastAnnee = intval($valueFrs->record->MORE_YEAR[$i]);
            
            $scoreTMP = $bvdTEST->transcoScoreMORE(strval($scoreAnnee));
            $graphPalmares[] = [
                'ANNEE' => $lastAnnee,
                'SCORE' => $scoreTMP,
                'color' => $color[$scoreTMP]['couleur'],
            ];
            $categorie[] = $lastAnnee;
            
            $i++;
        }
        
        $array1['value']                  = $graphPalmares;
        $array1['titre']                  = $trad->t("MORE - groupe de comparaison");
        $array1['option']                 = ['desactiveLegend' => true];
        $array1['type']                   = 'column';
        $array1['categorie']              = $categorie;
        $array1['categorieY']             = ['AAA', 'AA', 'A', 'BBB', 'BB', 'B', 'CCC', 'CC', 'C', 'D'];
        $array1['detailColone']['SCORE']  = ['nom' => 'Score', 'axeLier' => 'gauche'];
        $array1['legendGraph'][]          = ['texte' => 'Risque', 'format' => '', 'axe' => 'gauche', 'maxValue' => 9];
        
        $highcharts1 = Highcharts::generate($array1, $this->pdo, $trad);

        $patern_color = Highcharts::degraderCouleur(2);
        
        $array2['titre'] = $valueFrs->record->MORRAT;
        $array2['Fourchette'] = [0, 9];
        $array2['value'] = $valueHighchaets;
        $array2['legend'] = 'Pour'.' '.$valueFrs->record->MORE_YEAR;
        $array2['categorieY'] = ['AAA', 'AA', 'A', 'BBB', 'BB', 'B', 'CCC', 'CC', 'C', 'D'];
        $array2['type'] = 'solidgauge';
        $array2['colors'] = $patern_color;
        
        $highcharts2 = Highcharts::generate($array2, $this->pdo, $trad);
        
        ArianeSub::generate(1, 1, "Indicateur MORE", $_SERVER['REQUEST_URI'], 1, $post);
        
        return $this->renderer->render('@API/BVD/indicateurMORE', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            "code_fournisseur" => $idFournisseur,
            "libelle_fournisseur" => $fournis["RS"],
            'dataHighchart1' => $highcharts1,
            'dataHighchart2' => $highcharts2,
            'valueFrs' => $valueFrs,
            'tendeande' => $tendance,
            'score'     => $score,
        ]);
    }
    
    public function indicateurCompassAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        $patern_color = Highcharts::degraderCouleur(2);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $idBVD = ChampSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);

        $listeChps = [
            'NAME',
            'COUNTRY_ISO_CODE',
            'BVD_ID_NUMBER',
            'NATIONAL_ID',
            'NATIONAL_ID_LABEL',
            'STREET_AND_STREET_NUMBER',
            'POSTCODE_ADDITIONAL',
            'CITY_ADDITIONAL',
            'COUNTRY_ADDITIONAL',
            'PHONE_NUMBER_ADDITIONAL',
            'WEBSITE',
            'EMAIL',
            'INDUSTRY_PRIMARY_LABEL',
            'INCORPORATION_DATE',
            
            'G2Scores_CompassScore',
            'G2Scores_Period',
            'G2Scores_Reasons_Label',
            
            'MORRAT',
            'MORRAT_1',
        ];
        
        $bvdTEST = new BVD($this->pdo);
        $bvdTEST->generateTokenById($idBVD);
        
        $valueFrs = $bvdTEST->getDataBVD($listeChps);
        $bvdTEST->disconnect();
        
        $score = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT));
        $score_previous = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT[1]));
        
        if ($score>=$score_previous) {
            $tendance = 'haut';
        } else {
            $tendance = 'bas';
        }
        
        $dateTMP = str_replace('/', '', (string) $valueFrs->record->G2Scores_Period);
        $year=substr($dateTMP, 0, 4);
        $date = Date::dateConverte($dateTMP);
        
        $array1['titre'] = 'sur '.$date;
        $array1['Fourchette'] = [1, 1000];
        $array1['value'] = intval($valueFrs->record->G2Scores_CompassScore);
        $array1['legend'] = 'Pour '.$year;
        $array1['type'] = 'solidgauge';
        $array1['colors'] = $patern_color;
        
        $highcharts1 = Highcharts::generate($array1, $this->pdo, $trad);
        
        ArianeSub::generate(1, 1, "Indicateur Compass", $_SERVER['REQUEST_URI'], 1, $post);
        
        return $this->renderer->render('@API/BVD/indicateurCompass', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            "code_fournisseur"  => $idFournisseur,
            "libelle_fournisseur" => $fournis["RS"],
            'dataHighchart1'    => $highcharts1,
            'valueFrs'          => $valueFrs,
            
            'tendeande' => $tendance,
            'score'     => $score,
        ]);
    }
    
    public function indicateurCRIFAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        $patern_color = Highcharts::degraderCouleur(2);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $idBVD = ChampSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);
        
        $listeChps = [
            'NAME',
            'COUNTRY_ISO_CODE',
            'BVD_ID_NUMBER',
            'NATIONAL_ID',
            'NATIONAL_ID_LABEL',
            'STREET_AND_STREET_NUMBER',
            'POSTCODE_ADDITIONAL',
            'CITY_ADDITIONAL',
            'COUNTRY_ADDITIONAL',
            'PHONE_NUMBER_ADDITIONAL',
            'WEBSITE',
            'EMAIL',
            'INDUSTRY_PRIMARY_LABEL',
            'INCORPORATION_DATE',
            
            'CRIF_SCORE',
            'CRIF_PEER_MIN',
            'CRIF_PEER_AVG',
            'CRIF_PEER_MAX',
            'CRIF_YEAR',
            'CRIF_DESCR_HIST',
            'CRIF_PEER_DESCR',
            
            'MORRAT',
            'MORRAT_1',
        ];
        
        $bvdTEST = new BVD($this->pdo);
        $bvdTEST->generateTokenById($idBVD);
        
        $valueFrs = $bvdTEST->getDataBVD($listeChps);
        $bvdTEST->disconnect();
        
        $score = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT));
        $score_previous = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT[1]));
        
        if ($score>=$score_previous) {
            $tendance = 'haut';
        } else {
            $tendance = 'bas';
        }
        
        $array1['titre'] = '';
        $array1['Fourchette'] = [1000, 1];
        $array1['value'] = intval($valueFrs->record->CRIF_SCORE);
        $array1['legend'] = 'Pour '.$valueFrs->record->CRIF_YEAR;
        $array1['type'] = 'solidgauge';
        $array1['colors'] = $patern_color;
        
        $highcharts1 = Highcharts::generate($array1, $this->pdo, $trad);
        
        ArianeSub::generate(1, 1, "Indicateur CRIF", $_SERVER['REQUEST_URI'], 1, $post);
        
        return $this->renderer->render('@API/BVD/indicateurCRIF', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            "code_fournisseur"  => $idFournisseur,
            "libelle_fournisseur" => $fournis["RS"],
            'dataHighchart1'    => $highcharts1,
            'valueFrs'          => $valueFrs,
            
            'tendeande' => $tendance,
            'score'     => $score,
        ]);
    }
    
    public function indicateurTRUCOSTAction() : string
    {
        $idFournisseur = $this->request->getAttribute('idFournisseur');
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        
        $fournis = new Fourniss($this->pdo);
        $fournis = $fournis->find($idFournisseur);
        
        $patern_color = Highcharts::degraderCouleur(2);
        
        $id_cs = Sql::dilispec($this->pdo, 'BVD', 'ID_EXTERNE_FOURNISSEUR');
        $idBVD = ChampSupp::getValueCS('FOURNISS', $idFournisseur, $id_cs, $this->pdo);
        
        $listeChps = [
            'NAME',
            'COUNTRY_ISO_CODE',
            'BVD_ID_NUMBER',
            'NATIONAL_ID',
            'NATIONAL_ID_LABEL',
            'STREET_AND_STREET_NUMBER',
            'POSTCODE_ADDITIONAL',
            'CITY_ADDITIONAL',
            'COUNTRY_ADDITIONAL',
            'PHONE_NUMBER_ADDITIONAL',
            'WEBSITE',
            'EMAIL',
            'INDUSTRY_PRIMARY_LABEL',
            'INCORPORATION_DATE',
            
            'TRUCOST_SCORE_ENVIRDATE',
            'TRUCOST_SCORE_ENVIRSCORE',
            'TRUCOST_SCORE_ENVIRCONFIDENCE',
            'TRUCOST_SCORE_COMP_GHG',
            'TRUCOST_SCORE_COMP_WATER',
            'TRUCOST_SCORE_COMP_WASTE',
            'TRUCOST_SCORE_COMP_AIR',
            'TRUCOST_SCORE_COMP_LAND',
            'TRUCOST_SCORE_COMP_NAT',
            'TRUCOST_SCORE_SEC_FULLLABEL',
            'TRUCOST_SCORE_SEC_DESC',
            'TRUCOST_SCORE_SEC_GHG',
            'TRUCOST_SCORE_SEC_WATER',
            'TRUCOST_SCORE_SEC_WASTE',
            'TRUCOST_SCORE_SEC_AIR',
            'TRUCOST_SCORE_SEC_LAND',
            'TRUCOST_SCORE_SEC_NAT',
            
            'MORRAT',
            'MORRAT_1',
        ];
        
        $bvdTEST = new BVD($this->pdo);
        $bvdTEST->generateTokenById($idBVD);
        
        $valueFrs = $bvdTEST->getDataBVD($listeChps);
        $bvdTEST->disconnect();
        
        $score = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT));
        $score_previous = $bvdTEST->transcoScoreToNote(strval($valueFrs->record->MORRAT[1]));
        
        if ($score>=$score_previous) {
            $tendance = 'haut';
        } else {
            $tendance = 'bas';
        }
        
        $array1['titre'] = '';
        $array1['Fourchette'] = [1, 100];
        $array1['value'] = intval($valueFrs->record->TRUCOST_SCORE_ENVIRSCORE);
        $array1['legend'] = 'Pour '.$valueFrs->record->TRUCOST_SCORE_ENVIRDATE;
        $array1['type'] = 'solidgauge';
        $array1['colors'] = $patern_color;
        
        $highcharts1 = Highcharts::generate($array1, $this->pdo, $trad);
        
        $value = [
            0 => [
                'nom' => 'Gaz à effet de serre',
                'SCORE1' => $valueFrs->record->TRUCOST_SCORE_COMP_GHG,
                'SCORE2' => $valueFrs->record->TRUCOST_SCORE_SEC_GHG,
            ],
            1 => [
                'nom' => 'Eaux, Déchets',
                'SCORE1' => $valueFrs->record->TRUCOST_SCORE_COMP_WATER,
                'SCORE2' => $valueFrs->record->TRUCOST_SCORE_SEC_WATER,
            ],
            2 => [
                'nom' => 'Gaz à effet de serre',
                'SCORE1' => $valueFrs->record->TRUCOST_SCORE_COMP_WASTE,
                'SCORE2' => $valueFrs->record->TRUCOST_SCORE_SEC_WASTE,
            ],
            3 => [
                'nom' => 'Polluant de l air',
                'SCORE1' => $valueFrs->record->TRUCOST_SCORE_COMP_AIR,
                'SCORE2' => $valueFrs->record->TRUCOST_SCORE_SEC_AIR,
            ],
            4 => [
                'nom' => 'Polluant de sols et eaux',
                'SCORE1' => $valueFrs->record->TRUCOST_SCORE_COMP_LAND,
                'SCORE2' => $valueFrs->record->TRUCOST_SCORE_SEC_LAND,
            ],
            5 => [
                'nom' => 'Ressources naturelles utilisées',
                'SCORE1' => $valueFrs->record->TRUCOST_SCORE_COMP_NAT,
                'SCORE2' => $valueFrs->record->TRUCOST_SCORE_SEC_NAT,
            ],
        ];
        
        if ((float)$valueFrs->record->TRUCOST_SCORE_COMP_GHG > 1    || (float)$valueFrs->record->TRUCOST_SCORE_SEC_GHG > 1 ||
            (float)$valueFrs->record->TRUCOST_SCORE_COMP_WATER > 1  || (float)$valueFrs->record->TRUCOST_SCORE_SEC_WATER > 1 ||
            (float)$valueFrs->record->TRUCOST_SCORE_COMP_WASTE > 1  || (float)$valueFrs->record->TRUCOST_SCORE_SEC_WASTE > 1 ||
            (float)$valueFrs->record->TRUCOST_SCORE_COMP_AIR > 1    || (float)$valueFrs->record->TRUCOST_SCORE_SEC_AIR > 1 ||
            (float)$valueFrs->record->TRUCOST_SCORE_COMP_LAND > 1   || (float)$valueFrs->record->TRUCOST_SCORE_SEC_LAND > 1 ||
            (float)$valueFrs->record->TRUCOST_SCORE_COMP_NAT > 1    || (float)$valueFrs->record->TRUCOST_SCORE_SEC_NAT > 1) {
            $array2['maxValue'] = 2;
        }
        
        $array2['titre'] = '';
        $array2['type'] = 'spiderweb';
        $array2['categorie'] = ['Gaz à effet de serre', 'Eaux', 'Déchets', 'Polluant de l air', 'Polluant de sols et eaux', 'Ressources naturelles utilisées'];
        $array2['detailColone']['SCORE1'] = ['nom' => $valueFrs->record->INDUSTRY_PRIMARY_LABEL, 'type' => 'line'];
        $array2['detailColone']['SCORE2'] = ['nom' => $valueFrs->record->TRUCOST_SCORE_SEC_DESC, 'type' => 'line'];
        $array2['value'] = $value;
        $highcharts2 = Highcharts::generate($array2, $this->pdo, $trad);
        
        ArianeSub::generate(1, 1, "Indicateur TRUCOST", $_SERVER['REQUEST_URI'], 1, $post);
        
        return $this->renderer->render('@API/BVD/indicateurTRUCOST', [
            'menuLeft'          => 'GestionFournisseur',
            'menuActive'        => 'Fournisseur',
            "code_fournisseur"  => $idFournisseur,
            "libelle_fournisseur" => $fournis["RS"],
            'dataHighchart1'    => $highcharts1,
            'dataHighchart2'    => $highcharts2,
            'valueFrs'          => $valueFrs,
            
            'tendeande' => $tendance,
            'score'     => $score,
        ]);
    }
}
