From 1c7a0d7211d523bf04a9d78678a14b13e7b99b72 Mon Sep 17 00:00:00 2001
From: "CGSCOP\\hsilvant" <hsilvant@scop.coop>
Date: Sun, 7 Apr 2024 18:25:24 +0200
Subject: [PATCH] =?UTF-8?q?Utilisation=20du=20context=20pour=20cacher=20le?=
 =?UTF-8?q?s=20colonnes=20financement=20r=C3=A9gional?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 models/hr_timesheet.py | 20 ++++++++++++++++++++
 views/hr_timesheet.xml |  6 ++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/models/hr_timesheet.py b/models/hr_timesheet.py
index 86b7cd2..4d40305 100644
--- a/models/hr_timesheet.py
+++ b/models/hr_timesheet.py
@@ -253,3 +253,23 @@ class ScopHrTimesheet(models.Model):
                 "default_name": self[0].name if len(self) == 1 else "",
             },
         }
+
+    # ------------------------------------------------------
+    # Modification du context pour cacher les colonnes
+    # ------------------------------------------------------
+    @api.model
+    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
+        custom_context = self.env.context.copy()
+
+        current_ur_id = self.env.context['current_ur_id']
+        nbfs = self.env["ur.financial.system"].search([("ur_id", "=", current_ur_id)])
+        if len(nbfs) == 0:
+            custom_context['hide_financial_system'] = True
+        nbrc = self.env["ur.regional.convention"].search([("ur_id", "=", current_ur_id)])
+        if len(nbrc) == 0:
+            custom_context['hide_regional_convention'] = True
+
+        res = super(ScopHrTimesheet, self.with_context(custom_context)).fields_view_get(
+            view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
+
+        return res
diff --git a/views/hr_timesheet.xml b/views/hr_timesheet.xml
index c10abab..f7c3d80 100644
--- a/views/hr_timesheet.xml
+++ b/views/hr_timesheet.xml
@@ -272,12 +272,14 @@
                     <field
                         name="ur_financial_system_id"
                         options="{'no_open': True, 'no_create': True}"
-                        attrs="{'required': [('ur_financial_system_nb', '>', 0)], 'invisible': [('ur_financial_system_nb', '&lt;', 1)], 'readonly': [('state', 'in', ('submit', 'valid'))]}"
+                        invisible="context.get('hide_financial_system')"
+                        attrs="{'required': [('ur_financial_system_nb', '>', 0)], 'readonly': [('state', 'in', ('submit', 'valid'))]}"
                     />
                     <field
                         name="ur_regional_convention_id"
                         options="{'no_open': True, 'no_create': True}"
-                        attrs="{'required':[('ur_regional_convention_nb', '>', 0)], 'invisible':[('ur_regional_convention_nb', '&lt;', 1)], 'readonly': [('state', 'in', ('submit', 'valid'))]}"
+                        invisible="context.get('hide_regional_convention')"
+                        attrs="{'required':[('ur_regional_convention_nb', '>', 0)], 'readonly': [('state', 'in', ('submit', 'valid'))]}"
                     />
                     <field
                         name="justificatifs"
-- 
GitLab