Fix #7123 - getting abra ready to test
[Pman.Xtuple] / pgsql / x-fifo-invfifo-pohead-fix-unitcost.sql
1
2
3 -- fix the unit cost difference...
4 -- eg: receive item is $1
5 --     void is $2
6 -- add a transaction for the difference
7
8 CREATE OR REPLACE FUNCTION invfifo_pohead_fix_unitcost(i_pohead_number text)
9     RETURNS  INTEGER
10 AS $BODY$
11 DECLARE    
12     v_purchprice_accnt_id INTEGER;
13     v_lb_accnt_id INTEGER;
14     v_recorded NUMERIC;
15     v_correct NUMERIC;
16     v_result INTEGER;
17     _r RECORD;
18     v_date DATE;
19
20     
21 BEGIN
22    
23     v_result := 0;
24  
25     SELECT 
26             costcat_purchprice_accnt_id,  -- 148 Inventory Asset 
27             costcat_liability_accnt_id   -- 141 Inventory Received Not Billed 
28     INTO 
29             v_purchprice_accnt_id,
30             v_lb_accnt_id
31     FROM 
32             costcat 
33     LIMIT 1;
34
35  
36     
37     -- delete existing ???? == do this before we start calculating!?!?
38     PERFORM
39         deleteGlSeries( gltrans_sequence,  'Void Unit cost adjusted on ' || to_char(NOW(), 'Day Mon DD YYY') )
40
41     FROM
42             gltrans
43     WHERE
44             gltrans_docnumber = i_pohead_number
45         AND
46             gltrans_accnt_id = v_purchprice_accnt_id
47         AND
48             gltrans_doctype IN ('PO') 
49         AND
50             gltrans_date >= fetchMetricText('invfifo_start_date')::date
51         AND 
52             NOT gltrans_deleted
53         AND
54             gltrans_notes LIKE 'Unit cost adjusted for P/O%';
55
56     
57
58    -- how much... -- still not sure about this -  it's only been tested on a totally void purchase order.
59     
60     -- how much has been accounted for ?
61     
62     SELECT 
63             COALESCE(SUM(gltrans_amount),0)
64     INTO 
65             v_recorded
66     FROM
67             gltrans
68     LEFT JOIN
69             invdetailview
70     ON
71             gltrans_misc_id = invhist_id
72         -- AND
73         --     invfifo_void = 0
74         AND
75             invhist_transtype = 'RP'
76     
77     WHERE
78             invhist_id IS NOT NULL
79         AND
80             gltrans_accnt_id = (SELECT costcat_asset_accnt_id FROM costcat LIMIT 1)
81         AND
82             (gltrans_docnumber LIKE i_pohead_number || '-%' OR gltrans_docnumber = i_pohead_number )
83         AND
84             NOT gltrans_deleted
85         AND
86             gltrans_posted;
87
88   
89 -- now show what should be created by just using the 'non-vo
90     SELECT 
91                 COALESCE(SUM(gltrans_amount),0)
92         INTO 
93                 v_correct
94         FROM
95                 gltrans
96         LEFT JOIN
97                 invdetailview
98         ON
99                 gltrans_misc_id = invhist_id
100             -- AND
101             --     invfifo_void = 0
102             AND
103                 invhist_transtype = 'RP'
104         
105         WHERE
106                 invhist_id IS NOT NULL
107             AND
108                 invfifo_void= 0
109             AND
110                 gltrans_accnt_id = (SELECT costcat_asset_accnt_id FROM costcat LIMIT 1)
111             AND
112                 (gltrans_docnumber LIKE i_pohead_number || '-%' OR gltrans_docnumber = i_pohead_number )
113             AND
114                 NOT gltrans_deleted
115             AND
116                 gltrans_posted;
117
118   RAISE NOTICE 'Unit cost diff : %', v_recorded - v_correct;
119
120     
121     SELECT 
122             MAX(gltrans_date)
123     INTO
124             v_date
125     FROM
126             gltrans
127     WHERE
128             gltrans_accnt_id = v_purchprice_accnt_id
129         AND
130             gltrans_doctype IN ('PO') 
131         AND
132             gltrans_date >= fetchMetricText('invfifo_start_date')::date 
133         AND
134             NOT gltrans_deleted
135         AND
136             gltrans_docnumber LIKE i_pohead_number || '%' ;
137
138   
139     IF (ABS(v_recorded - v_correct) <> 0) THEN
140         SELECT insertGLTransaction( fetchJournalNumber('GL-MISC'),
141                                        'S/R', 'PO', i_pohead_number,
142                                       'Unit cost adjusted for P/O ' || i_pohead_number || ' on ' || v_date,
143                                       v_lb_accnt_id,
144                                       v_purchprice_accnt_id, -1,
145                                       v_recorded - v_correct,
146                                       v_date::DATE, true ) INTO v_result;
147     END IF;
148
149
150     RETURN v_result;
151     
152 END
153 $BODY$
154   LANGUAGE plpgsql VOLATILE
155   COST 100;
156   
157 ALTER FUNCTION  invfifo_pohead_fix_unitcost(text)
158   OWNER TO admin;
159
160
161
162  --
163  --
164  --SELECT gltrans_docnumber, gltrans_notes, gltrans_amount, invhist_id, invfifo_void FROM      gltrans
165  --   LEFT JOIN
166  --           invdetailview
167  --   ON
168  --           gltrans_misc_id = invhist_id
169  --       -- AND
170  --       --     invfifo_void = 0
171  --       AND
172  --           invhist_transtype = 'RP'
173  --   
174  --   WHERE
175  --                       gltrans_accnt_id = (SELECT costcat_asset_accnt_id FROM costcat LIMIT 1)
176  --       AND
177  --           gltrans_docnumber LIKE '20018' || '-%'
178  --       AND
179  --           NOT gltrans_deleted
180  --       AND
181  --           gltrans_posted;
182  --
183
184
185
186
187