Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track reversals of transactions generated by overpayments and year-ends #7537

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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