Merge pull request #1609 from xtuple/4_5_x
[xtuple] / foundation-database / public / functions / togglebankreccleared.sql
1
2 CREATE OR REPLACE FUNCTION toggleBankrecCleared(INTEGER, TEXT, INTEGER) RETURNS BOOLEAN 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   pBankrecid ALIAS FOR $1;
7   pSource    ALIAS FOR $2;
8   pSourceid  ALIAS FOR $3;
9
10 BEGIN
11   RETURN toggleBankrecCleared(pBankrecid, pSource, pSourceid, NULL, NULL, NULL);
12 END;
13 $$ LANGUAGE 'plpgsql';
14
15 CREATE OR REPLACE FUNCTION toggleBankrecCleared(INTEGER, TEXT, INTEGER, NUMERIC, NUMERIC, DATE) RETURNS BOOLEAN AS $$
16 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
17 -- See www.xtuple.com/CPAL for the full text of the software license.
18 DECLARE
19   pBankrecid ALIAS FOR $1;
20   pSource    ALIAS FOR $2;
21   pSourceid  ALIAS FOR $3;
22   pCurrrate  ALIAS FOR $4;
23   pAmount    ALIAS FOR $5;
24   pDate      ALIAS FOR $6;
25   _cleared BOOLEAN;
26   _r RECORD;
27
28 BEGIN
29   SELECT bankrecitem_id, bankrecitem_cleared INTO _r
30     FROM bankrecitem
31    WHERE ( (bankrecitem_bankrec_id=pBankrecid)
32      AND   (bankrecitem_source=pSource)
33      AND   (bankrecitem_source_id=pSourceid) );
34   IF ( NOT FOUND ) THEN
35     _cleared := TRUE;
36     INSERT INTO bankrecitem
37     (bankrecitem_bankrec_id, bankrecitem_source,
38      bankrecitem_source_id, bankrecitem_cleared,
39      bankrecitem_curr_rate, bankrecitem_amount,
40      bankrecitem_effdate)
41     VALUES
42     (pBankrecid, pSource,
43      pSourceid, _cleared,
44      pCurrrate, pAmount,
45      pDate);
46   ELSE
47     _cleared := FALSE;
48     DELETE FROM bankrecitem 
49     WHERE bankrecitem_id = _r.bankrecitem_id;
50   END IF;
51
52   RETURN _cleared;
53 END;
54 $$ LANGUAGE 'plpgsql';
55