Merge pull request #1 from shackbarth/keith1
[xtuple] / foundation-database / public / functions / detailedqoh.sql
1 CREATE OR REPLACE FUNCTION detailedQOH(pItemsiteid INTEGER,
2                                        pABS BOOLEAN) RETURNS NUMERIC 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   _qoh NUMERIC;
7
8 BEGIN
9
10   IF (pABS) THEN
11     SELECT SUM(noNeg(itemloc_qty)) INTO _qoh
12     FROM itemloc
13     WHERE (itemloc_itemsite_id=pItemsiteid);
14   ELSE
15     SELECT SUM(itemloc_qty) INTO _qoh
16     FROM itemloc
17     WHERE (itemloc_itemsite_id=pItemsiteid);
18   END IF;
19
20   IF (_qoh IS NULL) THEN
21     _qoh := 0.0;
22   END IF;
23
24   RETURN _qoh;
25
26 END;
27 $$ LANGUAGE plpgsql;