pgsql/x-dragon-invadj.sql
[Pman.Xtuple] / pgsql / x-fifo-invfifo.sql
1 -- invfifo is a shadow table to invdetail which contains the fifo data.
2
3
4
5
6  
7 create table invfifo (
8     invfifo_invdetail_id integer NOT NULL UNIQUE,
9     
10     -- unit is excluding shipping (it's mostly ignored now)
11     invfifo_unitcost   numeric(18, 6)  NOT NULL,
12     invfifo_landedunitcost NUMERIC(18,6) DEFAULT 0.0 NOT NULL,
13     
14     -- not used??
15     invfifo_totalcost  numeric(18, 6)  NOT NULL,
16     
17     -- key columns which the calculation is done on .
18     invfifo_qty_before  numeric(18, 6)  NOT NULL,
19     invfifo_qty_after  numeric(18, 6)  NOT NULL,
20     invfifo_cost_before  numeric(18, 6)  NOT NULL,
21     invfifo_cost_after  numeric(18, 6)  NOT NULL,
22     
23     -- flags .. not used at present.. may not be in futer
24     invfifo_is_estimate boolean  NOT NULL,
25     invfifo_recalc_queued boolean  NOT NULL,
26     
27     -- this is used to determine if update_from_invdetail has been run.
28      
29     
30     -- just stores cust/vend to save looking it up later.
31     invfifo_vend_id INT DEFAULT 0,
32     invfifo_cust_id INT DEFAULT 0,
33     invfifo_cohead_id INT NOT NULL DEFAULT 0,
34     -- FIXME - cohead_id might be a good idea here...
35     
36     
37     -- is the transaction a reversal of another one - so has $0 effect on FIFO.
38     -- should point to the invdetail_id of what it reverses, or -1 for random flagging...
39     invfifo_void           INTEGER NOT NULL DEFAULT 0,
40     
41     CONSTRAINT  invfifo_invdetail_fk FOREIGN KEY ( invfifo_invdetail_id) REFERENCES invdetail (invdetail_id) 
42    
43 );
44 alter table invfifo add column invfifo_landedunitcost NUMERIC(18,6) DEFAULT 0.0 NOT NULL;
45 -- void of which record.
46 alter table invfifo add column invfifo_void           INTEGER NOT NULL DEFAULT 0;
47 alter table invfifo add column invfifo_cohead_id INT NOT NULL DEFAULT 0;
48
49 GRANT ALL ON TABLE invfifo TO admin;
50 GRANT ALL ON TABLE invfifo TO xtrole;
51
52
53 CREATE INDEX invfifo_qty_before_indx
54   ON invfifo
55   USING btree
56   (invfifo_qty_before);
57
58 CREATE INDEX invfifo_qty_after_indx
59   ON invfifo
60   USING btree
61   (invfifo_qty_after);
62
63 CREATE INDEX invfifo_is_estimate_indx
64   ON invfifo
65   USING btree
66   (invfifo_is_estimate);
67   
68 CREATE INDEX invfifo_recalc_queued_indx
69   ON invfifo
70   USING btree
71   (invfifo_recalc_queued);
72
73 CREATE INDEX invfifo_cust_id_indx
74   ON invfifo
75   USING btree
76   (invfifo_cust_id);
77  
78 CREATE INDEX invfifo_vend_id_indx
79   ON invfifo
80   USING btree
81   (invfifo_vend_id); 
82    
83  CREATE INDEX invfifo_qty_before_indx
84   ON invfifo
85   USING btree
86   (invfifo_qty_before);
87
88 CREATE INDEX invfifo_void_indx
89   ON invfifo
90   USING btree
91   (invfifo_void);
92
93 CREATE INDEX invfifo_cohead_id_indx
94   ON invfifo
95   USING btree
96   (invfifo_cohead_id);
97
98 alter table invfifo drop column invfifo_coitem_id;
99  
100
101  
102
103
104 -- update /inserts tirgger invfifo_fill()
105
106 CREATE OR REPLACE FUNCTION invdetailtriggerfifo() RETURNS trigger 
107 AS $BODY$
108 DECLARE
109      
110 BEGIN
111     
112     PERFORM invfifo_fill(NEW.invdetail_id);
113       
114     RETURN NEW;   
115         
116 END;
117 $BODY$
118   LANGUAGE plpgsql VOLATILE
119   COST 100;
120   
121 ALTER FUNCTION  invdetailtriggerfifo()
122   OWNER TO admin;
123           
124
125
126
127 CREATE TRIGGER _invdetailtriggerfifo 
128     AFTER INSERT ON invdetail
129         FOR EACH ROW EXECUTE PROCEDURE invdetailtriggerfifo();
130         
131
132 CREATE OR REPLACE FUNCTION invfifotriggerfifo() RETURNS trigger 
133 AS $BODY$
134 DECLARE
135      
136 BEGIN
137     
138     IF (NEW.invfifo_void IS NOT NULL AND NEW.invfifo_void < 0 ) THEN
139         RAISE EXCEPTION 'Voiding a transaction needs to know what was voided invdetail_id= %',  NEW.invfifo_invdetail_id ;
140     END IF; 
141     RETURN NEW;   
142         
143 END;
144 $BODY$
145   LANGUAGE plpgsql VOLATILE
146   COST 100;
147   
148 ALTER FUNCTION  invdetailtriggerfifo()
149   OWNER TO admin;
150           
151
152  
153 CREATE TRIGGER _invfifotriggerfifo 
154     AFTER INSERT OR UPDATE ON invfifo
155         FOR EACH ROW EXECUTE PROCEDURE invfifotriggerfifo();
156         
157