6625b7c1d1137a5c36b51256e5250e188f425ae7
[xtuple] / foundation-database / public / functions / itemcost.sql
1 CREATE OR REPLACE FUNCTION itemCost(INTEGER) RETURNS NUMERIC STABLE 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   _cost NUMERIC;
7 BEGIN
8   SELECT CASE WHEN (itemsite_costmethod='A' AND itemsite_qtyonhand != 0.0) THEN (itemsite_value / itemsite_qtyonhand)
9               WHEN (itemsite_costmethod='A' AND itemsite_qtyonhand = 0.0) THEN 0.0
10               WHEN (itemsite_costmethod='N') THEN 0.0
11               ELSE stdCost(itemsite_item_id)
12          END INTO _cost
13     FROM itemsite
14    WHERE(itemsite_id=pItemsiteid);
15   RETURN _cost;
16 END;
17 $$ LANGUAGE 'plpgsql';
18