From 7b47fb608ec5e872f181cc9919512958c4b14999 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 16 Jan 2019 14:46:20 -0800 Subject: [PATCH] close: use exact, minimal amounts; ensure assertions pass (#941) --- hledger/Hledger/Cli/Commands/Close.hs | 18 +++++++++++++----- tests/misc/close.test | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Close.hs b/hledger/Hledger/Cli/Commands/Close.hs index 2613872c0..4f92e5bd5 100755 --- a/hledger/Hledger/Cli/Commands/Close.hs +++ b/hledger/Hledger/Cli/Commands/Close.hs @@ -34,8 +34,11 @@ This command may also be useful for closing out expense/income accounts for a period (ie "closing the books" in accounting). Both transactions include balance assertions for the closed/reopened accounts. + You probably shouldn't use status or realness queries (eg -C or -R) with this -command, or the balance assertions will require that query to pass. +command, or the balance assertions will require that query to pass. +Likewise, if you generate them with --auto, the assertions will depend on +any auto postings and --auto will be required to make them pass. By default, the closing transaction is dated yesterday, with balances calculated as of end of yesterday, and the opening transaction is dated today. @@ -83,17 +86,22 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do closingdate = addDays (-1) openingdate (acctbals,_) = balanceReportFromMultiBalanceReport ropts_ q j balancingamt = negate $ sum $ map (\(_,_,_,b) -> normaliseMixedAmountSquashPricesForDisplay b) acctbals + + -- since balance assertion amounts are required to be exact, the + -- amounts in opening/closing transactions should be too (#941) + -- setprec = setFullPrecision + setprec = setMinimalPrecision ps = [posting{paccount=a - ,pamount=mixed [b] - ,pbalanceassertion=Just assertion{ baamount=b } + ,pamount=mixed [setprec b] + ,pbalanceassertion=Just assertion{ baamount=setprec b } } |(a,_,_,mb) <- acctbals ,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb ] ++ [posting{paccount="equity:opening balances", pamount=balancingamt}] nps = [posting{paccount=a - ,pamount=mixed [negate b] - ,pbalanceassertion=Just assertion{ baamount=b{aquantity=0} } + ,pamount=mixed [setprec $ negate b] + ,pbalanceassertion=Just assertion{ baamount= setprec b{aquantity=0} } } |(a,_,_,mb) <- acctbals ,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb diff --git a/tests/misc/close.test b/tests/misc/close.test index 7f8d91850..fa7d59739 100644 --- a/tests/misc/close.test +++ b/tests/misc/close.test @@ -1,5 +1,5 @@ -# Simple case +# 1. Simple case hledger close -f- -p 2016 assets liabilities <<< 2016/1/1 open @@ -33,7 +33,7 @@ hledger close -f- -p 2016 assets liabilities >>>=0 -# Test aggregation of postings with prices +# 2. Test aggregation of postings with prices hledger close -f- -p 2016 assets liabilities <<< Y2016 @@ -58,18 +58,18 @@ Y2016 liabilities:employer >>> 2016/12/31 closing balances - assets:bank -5,733.00 EUR = 0.00 EUR - liabilities:employer -1.00 EUR = 0.00 EUR + assets:bank -5,733 EUR = 0 EUR + liabilities:employer -1 EUR = 0 EUR equity:closing balances 2017/01/01 opening balances - assets:bank 5,733.00 EUR = 5,733.00 EUR - liabilities:employer 1.00 EUR = 1.00 EUR + assets:bank 5,733 EUR = 5,733 EUR + liabilities:employer 1 EUR = 1 EUR equity:opening balances >>>=0 -# A begin date should be ignored +# 3. A begin date should be ignored hledger close -f- -b2017/6/1 -e2018 <<< 2017/1/1 @@ -85,7 +85,7 @@ hledger close -f- -b2017/6/1 -e2018 >>>=0 -# Print just the opening transaction +# 4. Print just the opening transaction hledger close -f- -p 2016 assets liabilities --opening <<< 2016/1/1 open @@ -113,7 +113,7 @@ hledger close -f- -p 2016 assets liabilities --opening >>>=0 -# Print just the closing transaction +# 5. Print just the closing transaction hledger close -f- -p 2016 assets liabilities --closing <<< 2016/1/1 open @@ -141,7 +141,7 @@ hledger close -f- -p 2016 assets liabilities --closing >>>=0 -# Supplying --opening --closing is the same as just "close" +# 6. Supplying --opening --closing is the same as just "close" hledger close -f- -p 2016 assets liabilities --opening --closing <<< 2016/1/1 open