f5a585d7afc2ecca14199a1d3082026937a92352
[xtuple] / foundation-database / public / functions / unreleasepurchaseorder.sql
1 CREATE OR REPLACE FUNCTION unreleasePurchaseOrder(pPoheadid INTEGER) RETURNS INTEGER 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 BEGIN
5
6   IF ( ( SELECT (COUNT(*) > 0)
7          FROM poitem
8          WHERE ( (poitem_pohead_id=pPoheadid)
9            AND   ( (poitem_status='C') OR
10                    (poitem_qty_received > 0.0) OR
11                    (poitem_qty_returned > 0.0) OR
12                    (poitem_qty_vouchered > 0.0) ) ) ) ) THEN
13     RETURN -1;
14   END IF;
15
16   IF ( ( SELECT (pohead_status='O')
17          FROM pohead
18          WHERE (pohead_id=pPoheadid) ) ) THEN
19
20     --update status and erase the date that the order was released on
21     UPDATE pohead
22     SET pohead_status='U', pohead_released = NULL
23     WHERE (pohead_id=pPoheadid);
24
25   END IF;
26
27   --update status and erase the duedates at release
28   UPDATE poitem
29   SET poitem_status='U', poitem_rlsd_duedate = NULL
30   WHERE (poitem_pohead_id=pPoheadid);
31
32   RETURN 1;
33
34 END;
35 $$ LANGUAGE 'plpgsql';