pgsql/x-dragon-invadj.sql
[Pman.Xtuple] / pgsql / x-dragon-undeleteglseries.sql
1 -- Function: deleteglseries(integer, text)
2
3 -- DROP FUNCTION deleteglseries(integer, text);
4
5 CREATE OR REPLACE FUNCTION undeleteglseries(integer, text)
6   RETURNS boolean AS
7 $BODY$
8 DECLARE
9   pSequence ALIAS FOR $1;
10   pNotes ALIAS FOR $2;
11   _trialbalid INTEGER;
12   _count INTEGER;
13   v_tmp INTEGER;  
14   _r RECORD;
15
16 BEGIN
17
18 --  March through all of the G/L Transactions for the passed sequence
19   FOR _r IN SELECT
20             gltrans_id, gltrans_date,
21             gltrans_accnt_id, gltrans_amount,
22             gltrans_posted, gltrans_rec,
23             accnt_closedpost, accnt_forwardupdate,
24             period_id, period_closed,
25             period_freeze
26         FROM
27             accnt, gltrans
28             LEFT OUTER JOIN period ON (gltrans_date BETWEEN period_start AND period_end)
29         WHERE
30             (
31                 (gltrans_accnt_id=accnt_id)
32              AND
33                 (gltrans_deleted)
34              AND
35                 (gltrans_sequence=pSequence)
36             ) LOOP
37
38
39 --  If we can post into a Trial Balance, do so
40     IF ( (NOT _r.period_freeze) AND 
41        ( (NOT _r.period_closed) OR (_r.accnt_closedpost) ) AND
42        (  NOT _r.gltrans_rec) AND
43        ( NOT _r.gltrans_posted ) ) THEN
44
45 --  Try to find an existing trialbal
46       SELECT trialbal_id INTO _trialbalid
47       FROM trialbal
48       WHERE ( (trialbal_period_id=_r.period_id)
49        AND (trialbal_accnt_id=_r.gltrans_accnt_id) );
50
51       GET DIAGNOSTICS _count = ROW_COUNT;
52       IF (_count > 0) THEN
53
54 --  We found a trialbal, update it with the G/L Transaction
55 --  Note - two stage update to avoid any funny value caching logic
56         IF (_r.gltrans_amount > 0) THEN
57           UPDATE trialbal
58           SET trialbal_credits = (trialbal_credits + _r.gltrans_amount)
59           WHERE (trialbal_id=_trialbalid);
60         ELSE
61           UPDATE trialbal
62           SET trialbal_debits = (trialbal_debits + (_r.gltrans_amount * -1))
63           WHERE (trialbal_id=_trialbalid);
64         END IF;
65
66         UPDATE trialbal
67         SET trialbal_ending = (trialbal_beginning - trialbal_debits + trialbal_credits),
68             trialbal_dirty=TRUE
69         WHERE (trialbal_id=_trialbalid);
70
71       ELSE
72         RAISE EXCEPTION 'Can not delete G/L Series.  Trial balance record not found.';
73       END IF;
74
75 --  Forward update if we should
76       IF (_r.accnt_forwardupdate AND fetchmetricbool('ManualForwardUpdate')) THEN
77         PERFORM forwardUpdateTrialBalance(_trialbalid);
78       END IF;
79
80 --  Delete any bank reconciliation records if this was marked cleared but non reconciled
81
82     SELECT bankrecitem_id INTO v_tmp FROM 
83         bankrecitem
84         WHERE ((bankrecitem_source='GL')
85         AND (bankrecitem_source_id=_r.gltrans_id)) LIMIT 1;
86     
87     IF FOUND THEN
88         RAISE EXCEPTION 'GL Series has ban reconcillation?? - this should not be possible for deleted ones!?';
89     END IF;
90
91
92     
93 --  Unflag any journals as posted as a result of this series
94     UPDATE sltrans SET
95       sltrans_posted=true,
96       sltrans_gltrans_journalnumber=null
97     FROM gltrans
98     WHERE ((gltrans_sequence=pSequence)
99     AND (sltrans_gltrans_journalnumber=gltrans_journalnumber));
100     
101 --  Mark the G/L Transaction as deleted
102       UPDATE gltrans SET
103         gltrans_posted=true,
104         gltrans_deleted=false,
105         gltrans_notes=gltrans_notes || E'\n' || pNotes
106       WHERE (gltrans_id=_r.gltrans_id);
107
108     ELSIF (_r.period_freeze) THEN
109         RAISE EXCEPTION 'Can not delete a G/L Transaction in a frozen period';
110     ELSIF ((_r.period_closed) OR (NOT _r.accnt_closedpost)) THEN
111         RAISE EXCEPTION 'Can not delete a G/L Transaction on account % in a closed period', formatGlAccount(gltrans_accnt_id);
112     ELSIF (_r.gltrans_rec) THEN
113         RAISE EXCEPTION 'Can not delete a G/L Transaction that has been reconciled';
114     END IF;
115
116   END LOOP;
117
118   RETURN true;
119
120 END;
121 $BODY$
122   LANGUAGE plpgsql VOLATILE
123   COST 100;
124 ALTER FUNCTION undeleteglseries(integer, text)
125   OWNER TO admin;