From 94875ebcfcd7aca6285818d8564aaa778ba10663 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Fri, 15 Sep 2023 15:34:53 +0200
Subject: [PATCH] #51991 bug plafonds

---
 module/Plafond/src/Service/PlafondService.php | 32 +++++++++++++------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/module/Plafond/src/Service/PlafondService.php b/module/Plafond/src/Service/PlafondService.php
index 3f5ca5d91b..feaef437b6 100755
--- a/module/Plafond/src/Service/PlafondService.php
+++ b/module/Plafond/src/Service/PlafondService.php
@@ -528,18 +528,33 @@ class PlafondService extends AbstractEntityService
             $hasQuery = false;
             foreach ($plafonds as $plafond) {
                 $testRes = $this->testRequete($plafond);
+
                 if ($testRes['success']) {
 
                     $hasQuery = true;
                     if (!$first) $view .= "\n\n    UNION ALL\n";
-                    $view .= "\n  SELECT " . $plafond->getId() . " PLAFOND_ID,";
-                    if (!$testRes['plafondCol']) {
-                        $view .= " NULL PLAFOND,";
-                    }
-                    if (!$testRes['etatCol']) {
-                        $view .= " NULL PLAFOND_ETAT_ID,";
+                    $view .= "\n  SELECT";
+
+                    foreach( $cols as $col ){
+                        $colVal = "NULL $col";
+                        switch($col){
+                            case 'PLAFOND_ID':
+                                $colVal = (string)$plafond->getId()." $col";
+                                break;
+                            case 'DEROGATION':
+                                $colVal = null;
+                                break;
+                            default:
+                                if (in_array($col, $testRes['columns'])){
+                                    $colVal = 'p.'.$col;
+                                }
+                        }
+                        if ($colVal !== null){
+                            $view .= " $colVal,";
+                        }
                     }
-                    $view .= " p.* FROM (\n    ";
+                    $view = substr($view, 0, -1); // on retire la dernière virgule
+                    $view .= " FROM (\n    ";
                     $view .= str_replace("\n", "\n      ", $plafond->getRequete());
                     $view .= "\n    ) p";
                     $first = false;
@@ -597,8 +612,7 @@ class PlafondService extends AbstractEntityService
             }
 
             $res = $res[0];
-            $return['plafondCol'] = isset($res['PLAFOND']);
-            $return['etatCol'] = isset($res['PLAFOND_ETAT_ID']);
+            $return['columns'] = array_keys($res);
 
             foreach ($cols as $col) {
                 if (!isset($res[$col])) {
-- 
GitLab