let administrators modify package contents even if they are not marked as such to...
[xtuple] / foundation-database / public / trigger_functions / pkgreport.sql
1 -- NO create trigger statements. the updater will create them.
2
3 SELECT dropIfExists('TRIGGER', 'pkgreportbeforetrigger');
4 CREATE OR REPLACE FUNCTION _pkgreportbeforetrigger() RETURNS "trigger" AS $$
5 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
6 -- See www.xtuple.com/CPAL for the full text of the software license.
7 DECLARE
8   _reportid     INTEGER;
9   _debug        BOOL := false;
10
11 BEGIN
12   IF (TG_OP = 'UPDATE') THEN
13     IF (_debug) THEN
14       RAISE NOTICE 'update OLD % %, NEW % %',
15                    OLD.report_name, OLD.report_grade, NEW.report_name, NEW.report_grade;
16     END IF;
17
18     IF (NEW.report_name != OLD.report_name) THEN
19       SELECT report_id INTO _reportid
20       FROM report
21       WHERE ((report_name=NEW.report_name)
22         AND  (report_grade=NEW.report_grade));
23       IF (FOUND) THEN
24         RAISE EXCEPTION 'Cannot change report % % because another report with that name and grade already exists.', NEW.report_name, NEW.report_grade;
25       END IF;
26     END IF;
27
28   ELSIF (TG_OP = 'INSERT') THEN
29     IF (_debug) THEN
30       RAISE NOTICE 'insert NEW % %', NEW.report_name, NEW.report_grade;
31     END IF;
32     SELECT report_id INTO _reportid
33     FROM report
34     WHERE ((report_name=NEW.report_name)
35       AND  (report_grade=NEW.report_grade));
36     IF (FOUND) THEN
37       RAISE EXCEPTION 'Cannot create new report % % because another report with that name and grade already exists.', NEW.report_name, NEW.report_grade;
38     END IF;
39
40   ELSIF (TG_OP = 'DELETE') THEN
41
42     RETURN OLD;
43   END IF;
44
45   RETURN NEW;
46 END;
47 $$ LANGUAGE 'plpgsql';
48
49 CREATE OR REPLACE FUNCTION _pkgreportalterTrigger() RETURNS TRIGGER AS $$
50 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
51 -- See www.xtuple.com/CPAL for the full text of the software license.
52 BEGIN
53   IF (pkgMayBeModified(TG_TABLE_SCHEMA) OR isDba()) THEN
54     IF (TG_OP = 'DELETE') THEN
55       RETURN OLD;
56     ELSE
57       RETURN NEW;
58     END IF;
59   END IF;
60
61   IF (TG_OP = 'INSERT') THEN
62     RAISE EXCEPTION 'You may not create report definitions in packages except using the xTuple Updater utility';
63
64   ELSIF (TG_OP = 'UPDATE') THEN
65     RAISE EXCEPTION 'You may not alter report definitions in packages except using the xTuple Updater utility';
66
67   ELSIF (TG_OP = 'DELETE') THEN
68     RAISE EXCEPTION 'You may not delete report definitions from packages. Try deleting or disabling the package.';
69
70   END IF;
71
72   RETURN NEW;
73 END;
74 $$ LANGUAGE 'plpgsql';
75
76 CREATE OR REPLACE FUNCTION _pkgreportaftertrigger() RETURNS TRIGGER AS $$
77 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
78 -- See www.xtuple.com/CPAL for the full text of the software license.
79 BEGIN
80   IF (TG_OP = 'DELETE') THEN
81     RETURN OLD;
82   END IF;
83
84   RETURN NEW;
85 END;
86 $$ LANGUAGE 'plpgsql';