From edc5b89601e5e76609e88385b906f27584c51b23 Mon Sep 17 00:00:00 2001
From: Benjamin <benjamin@le-filament.com>
Date: Tue, 24 Mar 2020 12:17:23 +0100
Subject: [PATCH] =?UTF-8?q?[cgscop=20#172]=20ajout=20contrainte=20sur=20pr?=
 =?UTF-8?q?oject=5Fid,=20start,=20stop=20pour=20limiter=20=C3=A0=201=20cod?=
 =?UTF-8?q?e=20UR=20par=20plage=20horaire?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 models/calendar.py | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/models/calendar.py b/models/calendar.py
index 7eee2bb..9022995 100644
--- a/models/calendar.py
+++ b/models/calendar.py
@@ -5,7 +5,7 @@ import pytz
 from datetime import timedelta
 
 from odoo import models, fields, api
-from odoo.exceptions import UserError
+from odoo.exceptions import UserError, ValidationError
 
 
 class CGScopCalendar(models.Model):
@@ -107,6 +107,31 @@ class CGScopCalendar(models.Model):
            fields.Datetime.now() > self.coop_id.ur_regional_convention_date:
             self.ur_regional_convention_id = self.coop_id.ur_regional_convention_id
 
+    # ------------------------------------------------------
+    # Contrains
+    # ------------------------------------------------------
+    @api.constrains('project_id', 'start', 'stop')
+    def _check_activity_code(self):
+        for event in self:
+            if event.project_id:
+                # Récupère les entrées en intersection
+                # avec la plage horaire
+                entries = self.search([
+                    '|',
+                    '&',
+                    ('start', '>', event.start),
+                    ('start', '<', event.stop),
+                    '&',
+                    ('stop', '>', event.start),
+                    ('stop', '<', event.stop),
+                    ('user_id', '=', self.env.uid),
+                    ('project_id', '=', event.project_id.id),
+                    ('id', '!=', event.id)])
+                if entries:
+                    raise ValidationError(
+                        "Vous ne pourvez programmer 2 évènements avec le "
+                        "même code activité sur la même plage horaire")
+
     # ------------------------------------------------------
     # Fonction boutons
     # ------------------------------------------------------
-- 
GitLab