1 -- updated to match recent version
2 -- added roudning on currtocurr...
5 CREATE OR REPLACE FUNCTION createAPCreditMemoApplication(pSourceApopenId INTEGER,
6 pTargetApopenId INTEGER,
8 pCurrId INTEGER) RETURNS INTEGER AS $$
9 -- Copyright (c) 1999-2012 by OpenMFG LLC, d/b/a xTuple.
10 -- See www.xtuple.com/CPAL for the full text of the software license.
12 _apCreditApplyId INTEGER;
15 IF (pAmount > ROUND((SELECT currToCurr(apopen_curr_id, pCurrId, ROUND(apopen_amount - apopen_paid, 2), apopen_docdate)
17 WHERE (apopen_id=pTargetApopenId)),2)) THEN
21 IF (pAmount > (SELECT ROUND((apopen_amount - apopen_paid) -
22 COALESCE(SUM(currToCurr(apcreditapply_curr_id,
25 apopen_docdate)), 0), 2)
26 FROM apopen LEFT OUTER JOIN apcreditapply
27 ON ((apcreditapply_source_apopen_id=apopen_id)
28 AND (apcreditapply_target_apopen_id<>pTargetApopenId))
29 WHERE (apopen_id=pSourceApopenId)
30 GROUP BY apopen_amount, apopen_paid)) THEN
34 SELECT apcreditapply_id INTO _apCreditApplyId
36 WHERE ((apcreditapply_source_apopen_id=pSourceApopenId)
37 AND (apcreditapply_target_apopen_id=pTargetApopenId));
40 UPDATE apcreditapply SET apcreditapply_amount=pAmount,
41 apcreditapply_curr_id=pCurrId
42 WHERE (apcreditapply_id=_apCreditApplyId);
44 INSERT INTO apcreditapply (
45 apcreditapply_source_apopen_id,
46 apcreditapply_target_apopen_id,
47 apcreditapply_amount, apcreditapply_curr_id
52 RETURNING apcreditapply_id INTO _apCreditApplyId;
55 RETURN _apCreditApplyId;
58 $$ LANGUAGE 'plpgsql';