spa pricing
[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   SELECT itemcost(itemsite_id) INTO _cost
20   FROM itemsite
21   WHERE (itemsite_item_id=pItemid)
22     AND (itemsite_warehous_id=pSiteid);
23
24   RETURN _cost;
25 END;
26 $$ LANGUAGE 'plpgsql';
27
28 CREATE OR REPLACE FUNCTION itemCost(pItemsiteid INTEGER) RETURNS NUMERIC STABLE AS $$
29 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
30 -- See www.xtuple.com/CPAL for the full text of the software license.
31 DECLARE
32   _cost NUMERIC;
33 BEGIN
34   SELECT CASE WHEN (itemsite_costmethod='A' AND itemsite_qtyonhand != 0.0) THEN (itemsite_value / itemsite_qtyonhand)
35               WHEN (itemsite_costmethod='A' AND itemsite_qtyonhand = 0.0) THEN 0.0
36               WHEN (itemsite_costmethod='N') THEN 0.0
37               ELSE stdCost(itemsite_item_id)
38          END INTO _cost
39     FROM itemsite
40    WHERE(itemsite_id=pItemsiteid);
41   RETURN _cost;
42 END;
43 $$ LANGUAGE 'plpgsql';