1 CREATE OR REPLACE FUNCTION _poheadTrigger() 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.
7 _maint BOOLEAN := TRUE;
11 -- Check if we are doing maintenance
12 IF (TG_OP = 'UPDATE') THEN
13 IF ( (OLD.pohead_status != NEW.pohead_status) OR
14 (OLD.pohead_printed != NEW.pohead_printed) ) THEN
20 IF ( (NOT _maint) AND (NOT checkPrivilege('MaintainPurchaseOrders'))
21 AND (NOT checkPrivilege('PostPurchaseOrders'))
22 AND (NOT checkPrivilege('PrintPurchaseOrders'))
23 AND (NOT checkPrivilege('PostVouchers')) ) THEN
24 RAISE EXCEPTION 'You do not have privileges to alter a Purchase Order.';
27 IF ( _maint AND (NOT checkPrivilege('MaintainPurchaseOrders')) ) THEN
28 RAISE EXCEPTION 'You do not have privileges to alter a Purchase Order.';
31 IF (TG_OP = 'INSERT') THEN
32 --- clear the number from the issue cache
33 PERFORM clearNumberIssue('PoNumber', NEW.pohead_number);
36 IF ( (TG_OP = 'INSERT') OR (TG_op = 'UPDATE') ) THEN
37 IF (NOT ISNUMERIC(NEW.pohead_number) AND NEW.pohead_saved) THEN
38 RAISE EXCEPTION 'Purchase Order Number must be numeric.';
42 IF ( SELECT (metric_value='t')
44 WHERE (metric_name='POChangeLog') ) THEN
46 -- Cache the cmnttype_id for ChangeLog
47 SELECT cmnttype_id INTO _cmnttypeid
49 WHERE (cmnttype_name='ChangeLog');
51 IF (TG_OP = 'INSERT') THEN
52 PERFORM postComment(_cmnttypeid, 'P', NEW.pohead_id, 'Created');
54 ELSIF (TG_OP = 'UPDATE') THEN
55 IF (OLD.pohead_terms_id <> NEW.pohead_terms_id) THEN
56 PERFORM postComment( _cmnttypeid, 'P', NEW.pohead_id,
57 ('Terms Changed from "' || oldterms.terms_code || '" to "' || newterms.terms_code || '"') )
58 FROM terms AS oldterms, terms AS newterms
59 WHERE ( (oldterms.terms_id=OLD.pohead_terms_id)
60 AND (newterms.terms_id=NEW.pohead_terms_id) );
63 ELSIF (TG_OP = 'DELETE') THEN
64 DELETE FROM docass WHERE docass_source_id = OLD.pohead_id AND docass_source_type = 'P';
65 DELETE FROM docass WHERE docass_target_id = OLD.pohead_id AND docass_target_type = 'P';
68 WHERE ( (comment_source='P')
69 AND (comment_source_id=OLD.pohead_id) );
74 IF (TG_OP = 'DELETE') THEN
81 $$ LANGUAGE 'plpgsql';
83 DROP TRIGGER IF EXISTS poheadTrigger ON pohead;
84 CREATE TRIGGER poheadTrigger BEFORE INSERT OR UPDATE OR DELETE ON pohead FOR EACH ROW EXECUTE PROCEDURE _poheadTrigger();
86 CREATE OR REPLACE FUNCTION _poheadTriggerAfter() RETURNS TRIGGER AS $$
87 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
88 -- See www.xtuple.com/CPAL for the full text of the software license.
90 IF (COALESCE(NEW.pohead_taxzone_id,-1) <> COALESCE(OLD.pohead_taxzone_id,-1)) THEN
91 UPDATE poitem SET poitem_taxtype_id=getItemTaxType(itemsite_item_id,NEW.pohead_taxzone_id)
93 WHERE ((itemsite_id=poitem_itemsite_id)
94 AND (poitem_pohead_id=NEW.pohead_id));
97 -- Do not update closed poitems
98 IF (TG_OP = 'UPDATE') THEN
99 IF (OLD.pohead_status != NEW.pohead_status) THEN
101 SET poitem_status=NEW.pohead_status
102 WHERE ( (poitem_pohead_id=NEW.pohead_id)
103 AND (poitem_status <> 'C') );
109 $$ LANGUAGE 'plpgsql';
111 SELECT dropifexists('TRIGGER','poheadTriggerAfter');
112 CREATE TRIGGER poheadTriggerAfter AFTER UPDATE ON pohead FOR EACH ROW EXECUTE PROCEDURE _poheadTriggerAfter();