From 5640231c2c6de619cccf4a270e248e6eccec919a Mon Sep 17 00:00:00 2001
From: Antony Le Courtes <antony.lecourtes@unicaen.fr>
Date: Thu, 5 May 2022 09:24:56 +0200
Subject: [PATCH] =?UTF-8?q?Modification=20du=20message=20pour=20un=20code?=
 =?UTF-8?q?=20utilisateur=20d=C3=A9j=C3=A0=20utilis=C3=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 data/ddl/trigger/INTERVENANT_CK.sql | 76 ++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 29 deletions(-)

diff --git a/data/ddl/trigger/INTERVENANT_CK.sql b/data/ddl/trigger/INTERVENANT_CK.sql
index 678688aa6d..4a033a578d 100644
--- a/data/ddl/trigger/INTERVENANT_CK.sql
+++ b/data/ddl/trigger/INTERVENANT_CK.sql
@@ -1,45 +1,63 @@
-CREATE OR REPLACE TRIGGER "INTERVENANT_CK"
-BEFORE INSERT OR UPDATE OF source_id, source_code, histo_destruction, code, statut_id, utilisateur_code ON intervenant
-FOR EACH ROW
+CREATE
+OR REPLACE TRIGGER "INTERVENANT_CK"
+BEFORE INSERT OR
+UPDATE of source_id, source_code, histo_destruction, code, statut_id, utilisateur_code
+ON intervenant
+    FOR EACH ROW
 DECLARE
-  pragma autonomous_transaction;
-  imp NUMERIC;
-  cs NUMERIC;
+pragma autonomous_transaction;
+  imp
+NUMERIC;
+  cs
+NUMERIC;
 BEGIN
-  IF :NEW.histo_destruction IS NOT NULL THEN
+  IF
+:NEW.histo_destruction IS NOT NULL THEN
     RETURN;
-  END IF;
+END IF;
 
-  SELECT importable INTO imp FROM source WHERE id = :NEW.SOURCE_ID;
-  IF imp = 1 THEN
+SELECT importable
+INTO imp
+FROM source
+WHERE id = :new.source_id;
+IF
+imp = 1 THEN
 
     -- Contrôle du bon remplissage du source_code
     IF :NEW.source_code IS NULL THEN
       raise_application_error(-20101, 'Le code Source est NULL, or il est requis pour toute donnée importée.');
-    END IF;
+END IF;
 
-  END IF;
+END IF;
 
   -- Contrôle de l'unicité de STATUT_ID
-  SELECT COUNT(*) INTO cs FROM INTERVENANT WHERE
-    id <> :NEW.id AND histo_destruction IS NULL
-    AND code = :NEW.code
-    AND annee_id = :NEW.annee_id
-    AND statut_id = :NEW.statut_id
-  ;
-  IF cs > 0 THEN
+SELECT COUNT(*)
+INTO cs
+FROM intervenant
+WHERE id <> :new.id
+  AND histo_destruction IS NULL
+  AND code = :new.code
+  AND annee_id = :new.annee_id
+  AND statut_id = :new.statut_id
+;
+IF
+cs > 0 THEN
     raise_application_error(-20101, 'Un intervenant ne peut pas avoir deux fois le même statut le même année');
-  END IF;
+END IF;
 
   -- On ne peut pas assicoer un même login à plusieurs intervenants
-  SELECT COUNT(*) INTO cs FROM INTERVENANT WHERE
-    id <> :NEW.id AND histo_destruction IS NULL
-    AND code <> :NEW.code
-    AND annee_id = :NEW.annee_id
-    AND utilisateur_code = :NEW.utilisateur_code
-  ;
-  IF cs > 0 THEN
-    raise_application_error(-20101, 'L''utilisateur est déjà utilisé pour un autre intervenant. Merci d''en choisir un autre.');
-  END IF;
+SELECT COUNT(*)
+INTO cs
+FROM intervenant
+WHERE id <> :new.id
+  AND histo_destruction IS NULL
+  AND code <> :new.code
+  AND annee_id = :new.annee_id
+  AND utilisateur_code = :new.utilisateur_code
+;
+IF
+cs > 0 THEN
+    raise_application_error(-20101, 'L''utilisateur est déjà utilisé pour un autre intervenant. Merci d''en choisir un autre.' || ' Pour le nouveau dossier ID: '|| :NEW.id|| ', Code: '|| :NEW.code || ', utilisateur_code: '||:NEW.utilisateur_code );
+END IF;
 
 END;
\ No newline at end of file
-- 
GitLab