0d75305174427e87e2dfcd902ce0baf4ccf0eada
[xtuple] / foundation-database / public / functions / itemcost.sql
1 CREATE OR REPLACE FUNCTION itemCost(pItemid INTEGER,
2                                     pCustid INTEGER,
3                                     pShiptoid INTEGER,
4                                     pQty NUMERIC,
5                                     pQtyUOM INTEGER,
6                                     pPriceUOM INTEGER,
7                                     pCurrid INTEGER,
8                                     pEffective DATE,
9                                     pAsOf DATE,
10                                     pSiteid INTEGER) RETURNS NUMERIC STABLE AS $$
11 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
12 -- See www.xtuple.com/CPAL for the full text of the software license.
13 --
14 -- Overload for future costing enhancements
15 --
16 DECLARE
17   _cost NUMERIC := 0.0;
18 BEGIN
19   IF (fetchMetricBool('WholesalePriceCosting')) THEN
20     SELECT item_listcost INTO _cost
21     FROM item
22     WHERE (item_id=pItemid);
23   ELSE
24     SELECT itemcost(itemsite_id) INTO _cost
25     FROM itemsite
26     WHERE (itemsite_item_id=pItemid)
27       AND (itemsite_warehous_id=pSiteid);
28   END IF;
29
30   RETURN _cost;
31 END;
32 $$ LANGUAGE 'plpgsql';
33
34 CREATE OR REPLACE FUNCTION itemCost(pItemsiteid INTEGER) RETURNS NUMERIC STABLE AS $$
35 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
36 -- See www.xtuple.com/CPAL for the full text of the software license.
37 DECLARE
38   _cost NUMERIC;
39 BEGIN
40   SELECT CASE WHEN (itemsite_costmethod='A' AND itemsite_qtyonhand != 0.0) THEN (itemsite_value / itemsite_qtyonhand)
41               WHEN (itemsite_costmethod='A' AND itemsite_qtyonhand = 0.0) THEN 0.0
42               WHEN (itemsite_costmethod='N') THEN 0.0
43               ELSE stdCost(itemsite_item_id)
44          END INTO _cost
45     FROM itemsite
46    WHERE(itemsite_id=pItemsiteid);
47   RETURN _cost;
48 END;
49 $$ LANGUAGE 'plpgsql';