Merge pull request #1785 from bendiy/4_6_x
[xtuple] / foundation-database / public / functions / initialdistribution.sql
1 CREATE OR REPLACE FUNCTION initialDistribution(INTEGER, INTEGER) RETURNS INTEGER  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   pItemsiteid ALIAS FOR $1;
6   pLocationid ALIAS FOR $2;
7   _itemlocid INTEGER;
8   _invhistid INTEGER;
9   _itemlocSeries INTEGER;
10   _r RECORD;
11
12 BEGIN
13
14 --  Make sure the passed itemsite points to a real item
15   IF ( (SELECT (item_type IN ('R', 'F') OR itemsite_costmethod = 'J')
16          FROM itemsite, item
17          WHERE ( (itemsite_item_id=item_id)
18           AND (itemsite_id=pItemsiteid) ) ) ) THEN
19     RETURN 0;
20   END IF;
21
22   _itemlocSeries := NEXTVAL('itemloc_series_seq');
23
24 --  Reassign the location_id for all existing itemlocs if
25 --  the passed itemsite is already lot/serial controlled
26   IF ( ( SELECT (itemsite_controlmethod IN ('L', 'S'))
27          FROM itemsite
28          WHERE (itemsite_id=pItemsiteid) ) ) THEN
29
30     FOR _r IN SELECT itemloc_id, itemloc_ls_id, itemloc_qty
31               FROM itemloc
32               WHERE (itemloc_itemsite_id=pItemsiteid) LOOP
33
34 --  Create the RL transaction
35       SELECT NEXTVAL('invhist_invhist_id_seq') INTO _invhistid;
36       INSERT INTO invhist
37       ( invhist_id, invhist_itemsite_id, invhist_series,
38         invhist_transtype, invhist_invqty,
39         invhist_qoh_before, invhist_qoh_after,
40         invhist_comments,
41         invhist_invuom, invhist_unitcost, invhist_hasdetail,
42         invhist_costmethod, invhist_value_before, invhist_value_after ) 
43       SELECT _invhistid, itemsite_id, _itemlocSeries,
44              'RL', 0,
45              _r.itemloc_qty, _r.itemloc_qty,
46              'Initial Distribution',
47              uom_name, stdCost(item_id), TRUE,
48              itemsite_costmethod, itemsite_value, itemsite_value
49       FROM item, itemsite, uom
50       WHERE ( (itemsite_item_id=item_id)
51        AND (item_inv_uom_id=uom_id)
52        AND (itemsite_controlmethod <> 'N')
53        AND (itemsite_id=pItemsiteid) );
54
55 --  Update the itemloc
56       UPDATE itemloc
57       SET itemloc_location_id=pLocationid
58       WHERE (itemloc_id=_r.itemloc_id);
59
60 --  Record the detail transaction
61       INSERT INTO invdetail
62       ( invdetail_invhist_id, invdetail_location_id, invdetail_ls_id,
63         invdetail_qty, invdetail_qty_before, invdetail_qty_after )
64       VALUES
65       ( _invhistid, pLocationid, _r.itemloc_ls_id,
66         _r.itemloc_qty, 0, _r.itemloc_qty );
67
68 --  Adjust QOH if this itemlocdist is to/from a non-netable location
69       IF ( SELECT (NOT location_netable)
70            FROM location
71            WHERE (location_id=pLocationid) ) THEN
72
73         INSERT INTO invhist
74         ( invhist_itemsite_id, invhist_series,
75           invhist_transtype, invhist_invqty,
76           invhist_qoh_before, invhist_qoh_after,
77           invhist_comments,
78           invhist_invuom, invhist_unitcost,
79           invhist_costmethod, invhist_value_before, invhist_value_after  ) 
80         SELECT itemsite_id, _itemlocSeries,
81                'NN', (_r.itemloc_qty * -1),
82                _r.itemloc_qty, 0,
83                'Initial Distribution',
84                uom_name, stdCost(item_id),
85                itemsite_costmethod, itemsite_value, itemsite_value
86         FROM itemsite, item, uom
87         WHERE ( (itemsite_item_id=item_id)
88          AND (item_inv_uom_id=uom_id)
89          AND (itemsite_controlmethod <> 'N')
90          AND (itemsite_id=pItemsiteid) );
91
92         UPDATE itemsite
93         SET itemsite_nnqoh = (itemsite_nnqoh + _r.itemloc_qty),
94             itemsite_qtyonhand = (itemsite_qtyonhand - _r.itemloc_qty) 
95         WHERE (itemsite_id=pItemsiteid);
96
97       END IF;
98
99     END LOOP;
100
101   ELSE
102 --  The passed itemsite is not lot/serial controlled
103 --  Make sure that there are not any stagnent itemlocs
104     DELETE FROM itemloc
105     WHERE (itemloc_itemsite_id=pItemsiteid);
106
107 --  Create the RL transaction
108     SELECT NEXTVAL('invhist_invhist_id_seq') INTO _invhistid;
109     INSERT INTO invhist
110     ( invhist_id, invhist_itemsite_id, invhist_series,
111       invhist_transtype, invhist_invqty,
112       invhist_qoh_before, invhist_qoh_after,
113       invhist_comments,
114       invhist_invuom, invhist_unitcost, invhist_hasdetail,
115       invhist_costmethod, invhist_value_before, invhist_value_after  ) 
116     SELECT _invhistid, itemsite_id, _itemlocSeries,
117            'RL', 0,
118            itemsite_qtyonhand, itemsite_qtyonhand,
119            'Initial Distribution',
120            uom_name, stdCost(item_id), TRUE,
121            itemsite_costmethod, itemsite_value, itemsite_value
122     FROM item, itemsite, uom
123     WHERE ( (itemsite_item_id=item_id)
124      AND (item_inv_uom_id=uom_id)
125      AND (itemsite_controlmethod <> 'N')
126      AND (itemsite_id=pItemsiteid) );
127
128 --  Create the itemloc
129     SELECT NEXTVAL('itemloc_itemloc_id_seq') INTO _itemlocid;
130     INSERT INTO itemloc
131     ( itemloc_id, itemloc_itemsite_id, itemloc_location_id,
132       itemloc_expiration, itemloc_qty )
133     SELECT _itemlocid, itemsite_id, pLocationid,
134            endOfTime(), itemsite_qtyonhand
135     FROM itemsite
136     WHERE (itemsite_id=pItemsiteid);
137
138 --  Record the detail transaction
139     INSERT INTO invdetail
140     ( invdetail_invhist_id, invdetail_location_id,
141       invdetail_qty, invdetail_qty_before, invdetail_qty_after )
142     SELECT _invhistid, pLocationid,
143            itemsite_qtyonhand, 0, itemsite_qtyonhand
144     FROM itemsite
145     WHERE (itemsite_id=pItemsiteid);
146
147 --  Adjust QOH if this itemlocdist is to/from a non-netable location
148     IF ( SELECT (NOT location_netable)
149          FROM location
150          WHERE (location_id=pLocationid) ) THEN
151
152       INSERT INTO invhist
153       ( invhist_itemsite_id, invhist_series,
154         invhist_transtype, invhist_invqty,
155         invhist_qoh_before, invhist_qoh_after,
156         invhist_comments,
157         invhist_invuom, invhist_unitcost,
158         invhist_costmethod, invhist_value_before, invhist_value_after  ) 
159       SELECT itemsite_id, _itemlocSeries,
160              'NN', (itemloc_qty * -1),
161              itemloc_qty, 0,
162              'Initial Distribution',
163              uom_name, stdCost(item_id),
164              itemsite_costmethod, itemsite_value, itemsite_value
165       FROM itemloc, itemsite, item, uom
166       WHERE ( (itemsite_item_id=item_id)
167        AND (item_inv_uom_id=uom_id)
168        AND (itemsite_controlmethod <> 'N')
169        AND (itemloc_itemsite_id=itemsite_id)
170        AND (itemloc_id=_itemlocid) );
171
172       UPDATE itemsite
173       SET itemsite_nnqoh = itemsite_qtyonhand,
174           itemsite_qtyonhand = 0 
175       FROM itemloc
176       WHERE ( (itemloc_itemsite_id=itemsite_id)
177        AND (itemloc_id=_itemlocid) );
178
179     END IF;
180
181   END IF;
182
183   RETURN _itemlocid;
184
185 END;
186 $$ LANGUAGE 'plpgsql';