Skip to content

Commit

Permalink
Merge pull request #7537 from ehuelsmann/feature/more-reversal-tracking
Browse files Browse the repository at this point in the history
Track reversals of transactions generated by overpayments and year-ends
  • Loading branch information
ehuelsmann committed Jul 25, 2023
2 parents d955b23 + cb433a9 commit 3fddc30
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
85 changes: 50 additions & 35 deletions sql/modules/EndOfYear.sql
Expand Up @@ -181,43 +181,58 @@ CREATE OR REPLACE FUNCTION eoy_reopen_books(in_end_date date)
RETURNS bool AS
$$
BEGIN
PERFORM count(*) FROM account_checkpoint WHERE end_date = in_end_date;
IF NOT FOUND THEN
RETURN FALSE;
END IF;

PERFORM * FROM account_checkpoint WHERE end_date > in_end_date;
IF FOUND THEN
RAISE EXCEPTION 'Only last closed period can be reopened';
END IF;

DELETE FROM account_checkpoint WHERE end_date = in_end_date;

PERFORM count(*) FROM yearend
WHERE transdate = in_end_date and reversed is not true;

IF FOUND THEN
INSERT INTO gl (transdate, reference, description,
approved, trans_type_code)
SELECT in_end_date, 'Reversing ' || reference,
'Reversing ' || description,
true, 'ye'
FROM gl WHERE id = (select trans_id from yearend
where transdate = in_end_date and reversed is not true);

INSERT INTO acc_trans (chart_id, amount_bc, curr, amount_tc,
transdate, trans_id, approved)
SELECT chart_id, amount_bc * -1, curr, amount_tc * -1,
in_end_date, currval('id'), true
FROM acc_trans where trans_id = (select trans_id from yearend
where transdate = in_end_date and reversed is not true);

UPDATE yearend SET reversed = true where transdate = in_end_date
PERFORM count(*) FROM account_checkpoint WHERE end_date = in_end_date;
IF NOT FOUND THEN
RETURN FALSE;
END IF;

PERFORM * FROM account_checkpoint WHERE end_date > in_end_date;
IF FOUND THEN
RAISE EXCEPTION 'Only last closed period can be reopened';
END IF;

DELETE FROM account_checkpoint WHERE end_date = in_end_date;

PERFORM count(*) FROM yearend
WHERE transdate = in_end_date and reversed is not true;

IF FOUND THEN
DECLARE
t_new_trans_id int;
BEGIN
INSERT INTO gl (transdate, reference, description, approved, trans_type_code)
SELECT in_end_date, 'Reversing ' || reference, 'Reversing ' || description, true, 'ye'
FROM gl
WHERE id = (select trans_id from yearend
where transdate = in_end_date
and reversed is not true)
RETURNING id INTO t_new_trans_id;

UPDATE transactions
SET reversing = (select trans_id
from yearend
where transdate = in_end_date
and reversed is not true)
WHERE id = t_new_trans_id;

INSERT INTO acc_trans (chart_id, amount_bc, curr, amount_tc,
transdate, trans_id, approved)
SELECT chart_id, amount_bc * -1, curr, amount_tc * -1,
in_end_date, t_new_trans_id, true
FROM acc_trans where trans_id = (select trans_id
from yearend
where transdate = in_end_date
and reversed is not true);

UPDATE yearend
SET reversed = true
where transdate = in_end_date
and reversed is not true;
END IF;
END;
END IF;

DELETE FROM account_checkpoint WHERE end_date = in_end_date;
RETURN TRUE;
DELETE FROM account_checkpoint WHERE end_date = in_end_date;
RETURN TRUE;
END;
$$ LANGUAGE PLPGSQL;

Expand Down
2 changes: 2 additions & 0 deletions sql/modules/Payment.sql
Expand Up @@ -1649,6 +1649,8 @@ END IF;

t_id := currval('id');

UPDATE transactions SET reversing = in_id WHERE id = t_id;

INSERT INTO voucher (batch_id, trans_id, batch_class)
VALUES (in_batch_id, t_id, CASE WHEN in_account_class = 1 THEN 4 ELSE 7 END);

Expand Down

0 comments on commit 3fddc30

Please sign in to comment.