Merge pull request #1609 from xtuple/4_5_x
[xtuple] / foundation-database / public / trigger_functions / location.sql
1 CREATE OR REPLACE FUNCTION _locationTrigger () RETURNS TRIGGER AS $$
2 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
3 -- See www.xtuple.com/CPAL for the full text of the software license.
4 DECLARE
5   _check      BOOLEAN;
6   _checkId    INTEGER;
7
8 BEGIN
9
10   -- Checks
11   -- Start with privileges
12   IF (TG_OP = 'INSERT') THEN
13     SELECT checkPrivilege('MaintainLocations') INTO _check;
14     IF NOT (_check) THEN
15       RAISE EXCEPTION 'You do not have privileges to add new Locations.';
16     END IF;
17   ELSE
18     SELECT checkPrivilege('MaintainLocations') INTO _check;
19     IF NOT (_check) THEN
20       RAISE EXCEPTION 'You do not have privileges to alter a Location.';
21     END IF;
22   END IF;
23
24   -- Code is required
25   IF ( (LENGTH(COALESCE(NEW.location_name,''))=0) AND
26        (LENGTH(COALESCE(NEW.location_aisle,''))=0) AND
27        (LENGTH(COALESCE(NEW.location_rack,''))=0) AND
28        (LENGTH(COALESCE(NEW.location_bin,''))=0) ) THEN
29     RAISE EXCEPTION 'You must supply a valid Location Identifier.';
30   END IF;
31   
32   -- Site is required
33   IF (NEW.location_warehous_id IS NULL) THEN
34     RAISE EXCEPTION 'You must supply a valid Site.';
35   END IF;
36
37   -- Location Identifier must be unique
38   SELECT location_id INTO _checkId
39   FROM location
40   WHERE ( (UPPER(location_name)=UPPER(NEW.location_name))
41     AND   (UPPER(location_aisle)=UPPER(NEW.location_aisle))
42     AND   (UPPER(location_rack)=UPPER(NEW.location_rack))
43     AND   (UPPER(location_bin)=UPPER(NEW.location_bin))
44     AND   (location_warehous_id=NEW.location_warehous_id)
45     AND   (location_id<>NEW.location_id) );
46   IF (FOUND) THEN
47     RAISE EXCEPTION 'You must supply a unique Location Identifier for this Site.';
48   END IF;
49
50   -- Populate formatted name
51   IF (NEW.location_aisle IS NOT NULL) THEN
52     NEW.location_formatname := NEW.location_aisle;
53   ELSE
54     NEW.location_formatname := '';
55   END IF;
56
57   IF (NEW.location_rack IS NOT NULL) THEN
58     NEW.location_formatname := (NEW.location_formatname || NEW.location_rack);
59   END IF;
60
61   IF (NEW.location_bin IS NOT NULL) THEN
62     NEW.location_formatname := (NEW.location_formatname || NEW.location_bin);
63   END IF;
64
65   IF (NEW.location_name IS NOT NULL) THEN
66     NEW.location_formatname := (NEW.location_formatname || NEW.location_name);
67   END IF;
68   
69   RETURN NEW;
70
71 END;
72 $$ LANGUAGE 'plpgsql';
73
74 SELECT dropIfExists('TRIGGER', 'locationTrigger');
75 CREATE TRIGGER locationTrigger BEFORE INSERT OR UPDATE ON location FOR EACH ROW EXECUTE PROCEDURE _locationTrigger();
76
77 CREATE OR REPLACE FUNCTION _locationAfterTrigger () RETURNS TRIGGER AS $$
78 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
79 -- See www.xtuple.com/CPAL for the full text of the software license.
80 DECLARE
81   _itemloc    RECORD;
82
83 BEGIN
84
85   -- Maintain itemsite_qtyonhand and itemsite_nnqoh when location_netable changes
86   IF (TG_OP = 'UPDATE') THEN
87     IF (OLD.location_netable <> NEW.location_netable) THEN
88       FOR _itemloc IN SELECT * FROM itemloc WHERE (itemloc_location_id=NEW.location_id) LOOP
89         IF (NEW.location_netable) THEN
90           UPDATE itemsite SET itemsite_qtyonhand = itemsite_qtyonhand + _itemloc.itemloc_qty,
91                               itemsite_nnqoh = itemsite_nnqoh - _itemloc.itemloc_qty
92           WHERE (itemsite_id=_itemloc.itemloc_itemsite_id);
93         ELSE
94           UPDATE itemsite SET itemsite_qtyonhand = itemsite_qtyonhand - _itemloc.itemloc_qty,
95                               itemsite_nnqoh = itemsite_nnqoh + _itemloc.itemloc_qty
96           WHERE (itemsite_id=_itemloc.itemloc_itemsite_id);
97         END IF;
98       END LOOP;
99     END IF;
100   END IF;
101   
102   RETURN NEW;
103
104 END;
105 $$ LANGUAGE 'plpgsql';
106
107 SELECT dropIfExists('TRIGGER', 'locationAfterTrigger');
108 CREATE TRIGGER locationAfterTrigger AFTER INSERT OR UPDATE ON location FOR EACH ROW EXECUTE PROCEDURE _locationAfterTrigger();
109