Fix #7123 - getting abra ready to test
[Pman.Xtuple] / pgsql / x-fix-recv-post.sql
1
2
3 CREATE OR REPLACE FUNCTION x_fix_recv_post(i_id  int)
4   RETURNS  int  AS
5 $BODY$
6 DECLARE
7   v_code TEXT;
8   v_return INTEGER;
9   v_breturn BOOLEAN;
10   v_invhist_id INTEGER;
11   v_invcnumber TEXT;
12   v_itemlocdist_series INTEGER;
13   _r RECORD;
14   _loc RECORD;
15    
16 BEGIN
17     v_return := 0;
18     v_invhist_id := 0;
19     RAISE NOTICE 'Fixing recv_id %', i_id;
20     -- check current status...
21     SELECT 
22         *,
23         
24         COALESCE((SELECT
25                 sum(invdetail_qty)
26                 FROM
27                     invdetailview
28                 WHERE
29                     invhist_ordnumber = CONCAT(pohead_number, '-', poitem_linenumber)
30                 AND
31                     invhist_posted
32                 AND
33                     invhist_transtype = 'RP'
34                 AND
35                     invhist_ordtype = 'PO'
36                 AND
37                     invhist_itemsite_id = recv_itemsite_id
38                 
39         ),0.0)   as total_tx_qty
40         INTO
41         _r
42        
43         FROM
44              recv
45         LEFT JOIN
46             recvgrp
47         on
48             recvgrp_id = recv_recvgrp_id
49         LEFT JOIN
50             poitem
51         ON
52             poitem_id = recv_orderitem_id
53         LEFT JOIN
54             pohead
55         ON
56             pohead_id = poitem_pohead_id
57         LEFT JOIN
58             itemsite
59         ON
60             itemsite_id = recv_itemsite_id
61         LEFT JOIN
62             item
63         ON
64             item_id  = itemsite_item_id
65         where
66             recv_id = i_id
67             AND
68             (recvgrp_void IS NULL OR recvgrp_void = 0) 
69
70             AND
71             recv_recvgrp_id is not null;
72             
73     IF  NOT FOUND  THEN
74         RAISE NOTICE 'skip - not found';
75         RETURN 0;
76     END IF;            
77     IF  _r.total_tx_qty > 0.0 THEN
78         RAISE NOTICE 'skip - total recived is > 0';
79         RETURN 0;
80     END IF;
81     
82     IF  _r.recv_qty < 1.0 THEN
83         RAISE NOTICE 'skip - total in recv is < 1';
84         RETURN 0;
85     END IF;
86             
87     UPDATE recv SET recv_posted = false where recv_id = i_id;
88     
89     -- update the poitem..
90     
91       
92     SELECT postReceipt(recv_id, 0) INTO v_itemlocdist_series FROM recv WHERE (recv_id=i_id);
93     IF v_itemlocdist_series < 1 THEN
94         RAISE EXCEPTION 'postReceipt returned %', v_itemlocdist_series ;
95     END IF;
96     
97     
98     
99     
100     
101     SELECT itemlocdist_id,
102                   itemlocdist_reqlotserial,
103                   itemlocdist_distlotserial,
104                   itemlocdist_qty,
105                   itemsite_loccntrl,
106                   itemsite_controlmethod,
107                 itemsite_perishable,
108                 itemsite_warrpurc,
109                 COALESCE(itemsite_lsseq_id,-1) AS itemsite_lsseq_id,
110                 COALESCE(itemlocdist_source_id,-1) AS itemlocdist_source_id
111             INTO
112                 _loc
113             FROM itemlocdist, itemsite
114             WHERE ( (itemlocdist_itemsite_id=itemsite_id) AND (itemlocdist_series=v_itemlocdist_series) ) ORDER BY itemlocdist_id;
115     
116     if _loc.itemlocdist_id < 1 THEN
117         RAISE EXCEPTION 'SEARCHING FOR itemlocdist_id  returned % FOR %' , _loc.itemlocdist_id , _r.item_number;
118     END IF;
119     
120     
121     INSERT INTO itemlocdist (
122             itemlocdist_itemlocdist_id,  itemlocdist_source_type,
123             itemlocdist_source_id,  itemlocdist_qty,
124             itemlocdist_ls_id, itemlocdist_expiration
125         ) SELECT
126             itemlocdist_id,'L',
127             _r.recvgrp_location_id ,       _r.recv_qty,
128             itemlocdist_ls_id, endOfTime()
129             FROM itemlocdist WHERE
130             (itemlocdist_id=_loc.itemlocdist_id );
131     
132     SELECT distributeToLocations(_loc.itemlocdist_id) INTO v_return;
133     if v_return < 0 THEN
134         RAISE EXCEPTION 'distribute to locations failed: %',v_return;
135     END IF;
136     SELECT postItemlocseries(v_itemlocdist_series) INTO v_breturn;
137     if NOT v_breturn  THEN
138         RAISE EXCEPTION 'postItemlocseries  failed: %', v_breturn;
139     END IF;
140     
141     -- fix the poitem
142     UPDATE poitem set
143     
144             poitem_qty_received =   
145                 COALESCE((SELECT
146                         sum(invdetail_qty)
147                         FROM
148                             invdetailview
149                         WHERE
150                             invhist_ordnumber = CONCAT(_r.pohead_number, '-', _r.poitem_linenumber)
151                         AND
152                             invhist_posted
153                         AND
154                             invhist_transtype = 'RP'
155                         AND
156                             invhist_ordtype = 'PO'
157                         AND
158                             invhist_itemsite_id = _r.recv_itemsite_id
159                         AND
160                             invdetail_qty > 0
161                 ),0.0)  ,
162             
163             
164             poitem_qty_returned =
165                 ABS(COALESCE((SELECT
166                             sum(invdetail_qty)
167                             FROM
168                                 invdetailview
169                             WHERE
170                                 invhist_ordnumber = CONCAT(_r.pohead_number, '-', _r.poitem_linenumber)
171                             AND
172                                 invhist_posted
173                             AND
174                                 invhist_transtype = 'RP'
175                             AND
176                                 invhist_ordtype = 'PO'
177                             AND
178                                 invhist_itemsite_id = _r.recv_itemsite_id
179                             AND
180                                 invdetail_qty < 0
181                     ),0.0) )
182                 
183                 
184             WHERE poitem_id = _r.poitem_id;
185     
186     
187     RETURN v_itemlocdist_series;
188      
189 END;
190 $BODY$
191   LANGUAGE plpgsql VOLATILE
192   COST 100;
193 ALTER FUNCTION  x_fix_recv_post(int)
194   OWNER TO admin;