close: fix some inappropriate rounding, ensure the txn balances (#1164)

This commit is contained in:
Simon Michael 2020-01-20 17:57:07 -08:00
parent 64546cd54a
commit 69bc8f803b
2 changed files with 38 additions and 5 deletions

View File

@ -50,7 +50,7 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
openingdate = fromMaybe today $ queryEndDate False q
closingdate = addDays (-1) openingdate
(acctbals,_) = balanceReportFromMultiBalanceReport ropts_ q j
balancingamt = sum $ map (\(_,_,_,b) -> normaliseMixedAmount b) acctbals
totalbalancingamt = sum $ map (\(_,_,_,b) -> normaliseMixedAmount b) acctbals
-- since balance assertion amounts are required to be exact, the
-- amounts in opening/closing transactions should be too (#941, #1137)
@ -69,7 +69,7 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
if interleaved then
-- a corresponding posting transferring the above balance to equity
[posting{paccount = closingacct
,pamount = Mixed [b]
,pamount = Mixed [setprec b]
}
]
else []
@ -87,7 +87,7 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
-- a final posting transferring all the balances to equity
-- (print will show it as multiple single-commodity postings)
[posting{paccount = closingacct
,pamount = balancingamt
,pamount = mapMixedAmount setprec totalbalancingamt
}
]
@ -102,7 +102,7 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
if interleaved then
-- a corresponding posting transferring the above balance from equity
[posting{paccount = openingacct
,pamount = Mixed [negate b]
,pamount = Mixed [setprec $ negate b]
}
]
else []
@ -121,7 +121,7 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
-- a final posting transferring all the balances from equity
-- (print will show it as multiple single-commodity postings)
[posting{paccount = openingacct
,pamount = negate balancingamt
,pamount = mapMixedAmount setprec $ negate totalbalancingamt
}
]

View File

@ -267,3 +267,36 @@ $ hledger -f- close -p 2016 assets liabilities --interleaved
>=0
# 10. A tricky case where a closing posting was rounded and failed to balance (#1164)
<
commodity $0.00
commodity AAA 0.00000000
2019-01-01 opening balance
(assets:usd) $49.50
2019-01-02 buy some AAA
assets:usd -$49.2619200
assets:aaa AAA 510
assets:usd -$0.1280810
expenses:banking $0.1280810
$ hledger -f- close -p 2019 assets
2019-12-31 closing balances
assets:aaa AAA -510.00000000 = AAA 0.00000000
assets:usd $-49.50
assets:usd $49.390001 @ AAA 10.3528242505 = $0.00
equity:closing balances $49.50
equity:closing balances $-49.390001 @ AAA 10.3528242505
equity:closing balances AAA 510.00000000
2020-01-01 opening balances
assets:aaa AAA 510.00000000 = AAA 510.00000000
assets:usd $49.50
assets:usd $-49.390001 @ AAA 10.3528242505 = $0.109999
equity:opening balances $-49.50
equity:opening balances $49.390001 @ AAA 10.3528242505
equity:opening balances AAA -510.00000000
>=0