Merge pull request #1784 from garyhgohoos/23593-2
[xtuple] / foundation-database / public / functions / initialdistribution.sql
1 CREATE OR REPLACE FUNCTION initialDistribution(pItemsiteid INTEGER,
2                                                pLocationid INTEGER) RETURNS INTEGER  AS $$
3 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
4 -- See www.xtuple.com/CPAL for the full text of the software license.
5 DECLARE
6   _itemlocid INTEGER;
7   _invhistid INTEGER;
8   _itemlocSeries INTEGER;
9   _r RECORD;
10
11 BEGIN
12
13 --  Make sure the passed itemsite points to a real item
14   IF ( (SELECT (item_type IN ('R', 'F') OR itemsite_costmethod = 'J')
15          FROM itemsite, item
16          WHERE ( (itemsite_item_id=item_id)
17           AND (itemsite_id=pItemsiteid) ) ) ) THEN
18     RETURN 0;
19   END IF;
20
21   _itemlocSeries := NEXTVAL('itemloc_series_seq');
22
23 --  Reassign the location_id for all existing itemlocs if
24 --  the passed itemsite is already lot/serial controlled
25   IF ( ( SELECT (itemsite_controlmethod IN ('L', 'S'))
26          FROM itemsite
27          WHERE (itemsite_id=pItemsiteid) ) ) THEN
28
29     FOR _r IN SELECT itemloc_id, itemloc_ls_id, itemloc_qty
30               FROM itemloc
31               WHERE (itemloc_itemsite_id=pItemsiteid) LOOP
32
33 --  Create the RL transaction
34       SELECT NEXTVAL('invhist_invhist_id_seq') INTO _invhistid;
35       INSERT INTO invhist
36       ( invhist_id, invhist_itemsite_id, invhist_series,
37         invhist_transtype, invhist_invqty,
38         invhist_qoh_before, invhist_qoh_after,
39         invhist_comments,
40         invhist_invuom, invhist_unitcost, invhist_hasdetail,
41         invhist_costmethod, invhist_value_before, invhist_value_after ) 
42       SELECT _invhistid, itemsite_id, _itemlocSeries,
43              'RL', 0,
44              _r.itemloc_qty, _r.itemloc_qty,
45              'Initial Distribution',
46              uom_name, stdCost(item_id), TRUE,
47              itemsite_costmethod, itemsite_value, itemsite_value
48       FROM item, itemsite, uom
49       WHERE ( (itemsite_item_id=item_id)
50        AND (item_inv_uom_id=uom_id)
51        AND (itemsite_controlmethod <> 'N')
52        AND (itemsite_id=pItemsiteid) );
53
54 --  Update the itemloc
55       UPDATE itemloc
56       SET itemloc_location_id=pLocationid
57       WHERE (itemloc_id=_r.itemloc_id);
58
59 --  Record the detail transaction
60       INSERT INTO invdetail
61       ( invdetail_invhist_id, invdetail_location_id, invdetail_ls_id,
62         invdetail_qty, invdetail_qty_before, invdetail_qty_after )
63       VALUES
64       ( _invhistid, pLocationid, _r.itemloc_ls_id,
65         _r.itemloc_qty, 0, _r.itemloc_qty );
66
67     END LOOP;
68
69   ELSE
70 --  The passed itemsite is not lot/serial controlled
71 --  Make sure that there are not any stagnent itemlocs
72     DELETE FROM itemloc
73     WHERE (itemloc_itemsite_id=pItemsiteid);
74
75 --  Create the RL transaction
76     SELECT NEXTVAL('invhist_invhist_id_seq') INTO _invhistid;
77     INSERT INTO invhist
78     ( invhist_id, invhist_itemsite_id, invhist_series,
79       invhist_transtype, invhist_invqty,
80       invhist_qoh_before, invhist_qoh_after,
81       invhist_comments,
82       invhist_invuom, invhist_unitcost, invhist_hasdetail,
83       invhist_costmethod, invhist_value_before, invhist_value_after  ) 
84     SELECT _invhistid, itemsite_id, _itemlocSeries,
85            'RL', 0,
86            itemsite_qtyonhand, itemsite_qtyonhand,
87            'Initial Distribution',
88            uom_name, stdCost(item_id), TRUE,
89            itemsite_costmethod, itemsite_value, itemsite_value
90     FROM item, itemsite, uom
91     WHERE ( (itemsite_item_id=item_id)
92      AND (item_inv_uom_id=uom_id)
93      AND (itemsite_controlmethod <> 'N')
94      AND (itemsite_id=pItemsiteid) );
95
96 --  Create the itemloc
97     SELECT NEXTVAL('itemloc_itemloc_id_seq') INTO _itemlocid;
98     INSERT INTO itemloc
99     ( itemloc_id, itemloc_itemsite_id, itemloc_location_id,
100       itemloc_expiration, itemloc_qty )
101     SELECT _itemlocid, itemsite_id, pLocationid,
102            endOfTime(), itemsite_qtyonhand
103     FROM itemsite
104     WHERE (itemsite_id=pItemsiteid);
105
106 --  Record the detail transaction
107     INSERT INTO invdetail
108     ( invdetail_invhist_id, invdetail_location_id,
109       invdetail_qty, invdetail_qty_before, invdetail_qty_after )
110     SELECT _invhistid, pLocationid,
111            itemsite_qtyonhand, 0, itemsite_qtyonhand
112     FROM itemsite
113     WHERE (itemsite_id=pItemsiteid);
114
115   END IF;
116
117   RETURN _itemlocid;
118
119 END;
120 $$ LANGUAGE plpgsql;