Skip to content

Commit

Permalink
Present correct starting balance on FX recons
Browse files Browse the repository at this point in the history
Fixes #8295
  • Loading branch information
ehuelsmann committed Jul 21, 2024
1 parent 449ff61 commit 6d1a2e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Bugs fixed
* Accounts marked for reconciliation can't be deleted (#7805)
* Transactions and invoices with attachments can't be saved (#6018)
* Aging statement shows functional instead of foreign currency amounts (#2162)
* Incorrect starting balance of fx-based reconciliations (#8295)

Cleanup & architecture improvements
* Reference data factored out of database schema (#7648)
Expand Down
3 changes: 2 additions & 1 deletion old/lib/LedgerSMB/DBObject/Reconciliation.pm
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@ sub previous_cleared_balance {
my $r = $self->call_dbmethod(
funcname => 'reconciliation__get_cleared_balance',
args => {
report_date => $previous->{end_date}
report_date => $previous->{end_date},
fx_balance => $self->{recon_fx}
}
);

Expand Down
19 changes: 14 additions & 5 deletions sql/modules/Reconciliation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,17 @@ reports that have already been approved. To purge old reconciliations you must
disable the block_change_when_approved trigger on cr_report.$$;

DROP FUNCTION IF EXISTS reconciliation__get_cleared_balance(int);
CREATE OR REPLACE FUNCTION reconciliation__get_cleared_balance(in_chart_id int,
in_report_date date DEFAULT date_trunc('second', now()))
DROP FUNCTION IF EXISTS reconciliation__get_cleared_balance(int,date);
CREATE OR REPLACE FUNCTION reconciliation__get_cleared_balance(
in_chart_id int,
in_report_date date DEFAULT date_trunc('second', now()),
in_fx_balance boolean DEFAULT false
)
RETURNS numeric AS
$$
SELECT sum(ac.amount_bc) * CASE WHEN c.category in('A', 'E') THEN -1 ELSE 1 END
SELECT CASE WHEN in_fx_balance THEN sum(ac.amount_tc)
ELSE sum(ac.amount_bc)
END * CASE WHEN c.category in('A', 'E') THEN -1 ELSE 1 END
FROM account c
JOIN acc_trans ac ON ac.chart_id = c.id
JOIN transactions g ON g.id = ac.trans_id
Expand All @@ -308,9 +314,12 @@ $$
GROUP BY c.id, c.category;
$$ LANGUAGE sql;

COMMENT ON FUNCTION reconciliation__get_cleared_balance(in_chart_id int,in_report_date date) IS
COMMENT ON FUNCTION reconciliation__get_cleared_balance(in_chart_id int,
in_report_date date,
in_fx_balance boolean) IS
$$ Gets the cleared balance of the account specified by chart_id, as cleared by reports
on and before in_report_date.
on and before in_report_date. Returns the foreign currency balance when 'in_fx_balance'
is true.

Please note that the cleared balance amount as at a sperific date may differ from the value
returned by this function, if transactions prior to in_report_date are cleared using reports
Expand Down
2 changes: 1 addition & 1 deletion xt/42-reconciliation.pg
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ BEGIN;
SELECT has_function('reconciliation__check',array['date', 'integer']);
SELECT has_function('reconciliation__delete_my_report',array['integer']);
SELECT has_function('reconciliation__delete_unapproved',array['integer']);
SELECT has_function('reconciliation__get_cleared_balance',array['integer', 'date']);
SELECT has_function('reconciliation__get_cleared_balance',array['integer', 'date', 'boolean']);
SELECT has_function('reconciliation__get_current_balance',array['integer', 'date']);
SELECT has_function('reconciliation__new_report',array['integer', 'numeric', 'date', 'boolean', 'bigint']);
SELECT has_function('reconciliation__pending_transactions',array['integer', 'numeric']);
Expand Down

0 comments on commit 6d1a2e3

Please sign in to comment.