From 1eb35c3a3657d10f1a5ab11f06814658b258f846 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Mon, 28 Sep 2020 12:16:59 +0200
Subject: [PATCH] =?UTF-8?q?#28282=20Pouvoir=20payer=20sur=20les=20centres?=
 =?UTF-8?q?=20de=20co=C3=BBts=20des=20composantes=20d'affectation?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 data/ddl/view/V_FR_SERVICE_CENTRE_COUT.sql        | 10 +++++++---
 data/ddl/view/V_FR_SERVICE_REF_CENTRE_COUT.sql    |  6 +++++-
 data/parametres.php                               |  6 ++++++
 .../src/Application/Form/ParametresForm.php       | 15 +++++++++++++++
 .../view/application/parametre/generaux.phtml     |  7 +++++--
 5 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/data/ddl/view/V_FR_SERVICE_CENTRE_COUT.sql b/data/ddl/view/V_FR_SERVICE_CENTRE_COUT.sql
index 8011d9c1c3..884ccff04d 100644
--- a/data/ddl/view/V_FR_SERVICE_CENTRE_COUT.sql
+++ b/data/ddl/view/V_FR_SERVICE_CENTRE_COUT.sql
@@ -1,14 +1,18 @@
-CREATE OR REPLACE FORCE VIEW V_FR_SERVICE_CENTRE_COUT AS
+CREATE OR REPLACE FORCE VIEW V_FR_SERVICE_CENTRE_COUT ("FORMULE_RESULTAT_SERVICE_ID", "CENTRE_COUT_ID") AS
 SELECT
   frs.id formule_resultat_service_id, cc.id centre_cout_id
 FROM
   formule_resultat_service   frs
+  JOIN parametre               p ON p.nom = 'centres_couts_paye'
   JOIN service                 s ON s.id = frs.service_id
+  JOIN intervenant             i ON i.id = s.intervenant_id
+  JOIN statut_intervenant     si ON si.id = i.statut_id
+  JOIN type_intervenant       ti ON ti.id = si.type_intervenant_id
   JOIN element_pedagogique    ep ON ep.id = s.element_pedagogique_id
   JOIN centre_cout            cc ON cc.histo_destruction IS NULL
 
   JOIN centre_cout_structure ccs ON ccs.centre_cout_id = cc.id
-                                AND ccs.structure_id = ep.structure_id
+                                AND ccs.structure_id = CASE WHEN p.valeur = 'enseignement' OR ti.code = 'E' THEN ep.structure_id ELSE COALESCE(i.structure_id,ep.structure_id) END
                                 AND ccs.histo_destruction IS NULL
 
   JOIN cc_activite             a ON a.id = cc.activite_id
@@ -27,7 +31,7 @@ WHERE
 UNION
 
 SELECT
-  frs.id formule_resultat_service_id, cc.id
+  frs.id formule_resultat_service_id, cc.id centre_cout_id
 FROM
   formule_resultat_service   frs
   JOIN service                 s ON s.id = frs.service_id
diff --git a/data/ddl/view/V_FR_SERVICE_REF_CENTRE_COUT.sql b/data/ddl/view/V_FR_SERVICE_REF_CENTRE_COUT.sql
index 35dc4c5f03..27de5ee127 100644
--- a/data/ddl/view/V_FR_SERVICE_REF_CENTRE_COUT.sql
+++ b/data/ddl/view/V_FR_SERVICE_REF_CENTRE_COUT.sql
@@ -3,11 +3,15 @@ SELECT
   frsr.id formule_resultat_serv_ref_id, cc.id centre_cout_id
 FROM
   formule_resultat_service_ref   frsr
+  JOIN parametre               p ON p.nom = 'centres_couts_paye'
   JOIN service_referentiel    sr ON sr.id = frsr.service_referentiel_id
+  JOIN intervenant             i ON i.id = sr.intervenant_id
+  JOIN statut_intervenant     si ON si.id = i.statut_id
+  JOIN type_intervenant       ti ON ti.id = si.type_intervenant_id
   JOIN centre_cout            cc ON cc.histo_destruction IS NULL
 
   JOIN centre_cout_structure ccs ON ccs.centre_cout_id = cc.id
-                                AND ccs.structure_id = sr.structure_id
+                                AND ccs.structure_id = CASE WHEN p.valeur = 'enseignement' OR ti.code = 'E' THEN sr.structure_id ELSE COALESCE(i.structure_id,sr.structure_id) END
                                 AND ccs.histo_destruction IS NULL
 
   JOIN cc_activite             a ON a.id = cc.activite_id
diff --git a/data/parametres.php b/data/parametres.php
index ee330a4929..10cbd1b926 100644
--- a/data/parametres.php
+++ b/data/parametres.php
@@ -82,10 +82,16 @@ return [
         "VALEUR"      => "semestriel",
         "DESCRIPTION" => "Modalité de gestion des services (réalisé, référentiel)",
     ],
+
+    /* Divers */
     "report_service"                     => [
         "VALEUR"      => "PREVU",
         "DESCRIPTION" => "Report du service de l'année précédente",
     ],
+    "centres_couts_paye"                 => [
+        "VALEUR"      => "enseignement",
+        "DESCRIPTION" => "Centres de coûts utilisés pour la paye",
+    ],
 
 
     /* Documentations */
diff --git a/module/Application/src/Application/Form/ParametresForm.php b/module/Application/src/Application/Form/ParametresForm.php
index c620af56eb..f7d8b46375 100755
--- a/module/Application/src/Application/Form/ParametresForm.php
+++ b/module/Application/src/Application/Form/ParametresForm.php
@@ -313,6 +313,21 @@ class ParametresForm extends AbstractForm
             ],
         ]);
 
+        $this->add([
+            'type'       => 'Select',
+            'name'       => 'centres_couts_paye',
+            'options'    => [
+                'value_options' => [
+                    'enseignement' => 'Utiliser les centres de coûts de la composante d\'enseignement',
+                    'affectation'  => 'Utiliser les centres de coûts de la composante d\'affectation de l\'intervenant',
+                ],
+            ],
+            'attributes' => [
+                'class'     => 'selectpicker',
+                'data-size' => 20,
+            ],
+        ]);
+
         $this->add([
             'type'       => 'Select',
             'name'       => 'contrat_regle_franchissement',
diff --git a/module/Application/view/application/parametre/generaux.phtml b/module/Application/view/application/parametre/generaux.phtml
index b5a188f717..027b1661c2 100755
--- a/module/Application/view/application/parametre/generaux.phtml
+++ b/module/Application/view/application/parametre/generaux.phtml
@@ -135,13 +135,16 @@
 
         <div class="panel panel-default">
             <div class="panel-heading">
-                <h3 class="panel-title">Formule de calcul des HETD</h3>
+                <h3 class="panel-title">Formule / Paiement</h3>
             </div>
             <div class="panel-body">
                 <div class="row">
-                    <div class="col-md-12">
+                    <div class="col-md-6">
                         <?= $this->formControlGroup($form->get('formule')); ?>
                     </div>
+                    <div class="col-md-6">
+                        <?= $this->formControlGroup($form->get('centres_couts_paye')); ?>
+                    </div>
                 </div>
             </div>
         </div>
-- 
GitLab