From 2da0e67141aea02016d692a91ba77b8048ff6d0f Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Mon, 26 Jul 2021 17:05:45 +1000 Subject: [PATCH] fix: Add balancing amounts in periodic transactions before applying auto-postings. (#1412) --- hledger/Hledger/Cli/Utils.hs | 23 +++++++++------------ hledger/test/journal/auto-postings.test | 27 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index 7330a6e40..c3b4a3fa2 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -122,8 +122,14 @@ journalAddForecast :: CliOpts -> Journal -> Journal journalAddForecast CliOpts{inputopts_=iopts, reportspec_=rspec} j = case forecast_ ropts of Nothing -> j - Just _ -> either (error') id . journalApplyCommodityStyles $ -- PARTIAL: - journalBalanceTransactions' iopts j{ jtxns = concat [jtxns j, forecasttxns'] } + Just _ -> either error id $ do -- PARTIAL: + forecasttxns <- addAutoTxns =<< mapM (balanceTransaction (balancingopts_ iopts)) + [ txnTieKnot t | pt <- jperiodictxns j + , t <- runPeriodicTransaction pt forecastspan + , spanContainsDate forecastspan (tdate t) + ] + journalBalanceTransactions (balancingopts_ iopts) j{ jtxns = concat [jtxns j, forecasttxns] } + >>= journalApplyCommodityStyles where today = _rsDay rspec ropts = _rsReportOpts rspec @@ -141,18 +147,7 @@ journalAddForecast CliOpts{inputopts_=iopts, reportspec_=rspec} j = (fromMaybe nulldatespan $ dbg2 "forecastspan flag" $ forecast_ ropts) (DateSpan (Just forecastbeginDefault) (Just forecastendDefault)) - forecasttxns = - [ txnTieKnot t | pt <- jperiodictxns j - , t <- runPeriodicTransaction pt forecastspan - , spanContainsDate forecastspan (tdate t) - ] - -- With --auto enabled, transaction modifiers are also applied to forecast txns - forecasttxns' = - (if auto_ iopts then either error' id . modifyTransactions today (jtxnmodifiers j) else id) -- PARTIAL: - forecasttxns - - journalBalanceTransactions' iopts j = - either error' id $ journalBalanceTransactions (balancingopts_ iopts) j -- PARTIAL: + addAutoTxns = if auto_ iopts then modifyTransactions today (jtxnmodifiers j) else return -- | Write some output to stdout or to a file selected by --output-file. -- If the file exists it will be overwritten. diff --git a/hledger/test/journal/auto-postings.test b/hledger/test/journal/auto-postings.test index 611e736c6..eddb718c7 100644 --- a/hledger/test/journal/auto-postings.test +++ b/hledger/test/journal/auto-postings.test @@ -384,3 +384,30 @@ $ hledger -f- print --auto (b) 100 EUR ; generated-posting: = assets amt:>50 >=0 + +# 20. Auto-generated postings apply on auto-balanced periodic postings. +< +2021-01-01 Fixed + Checking -10 + Costs + +~ 2021-01-02 Periodic + Checking -10 + Costs + += acct:Costs + (Auto) *1 + +$ hledger -f- print --forecast --auto --explicit +2021-01-01 Fixed ; modified: + Checking -10 + Costs 10 + (Auto) 10 ; generated-posting: = acct:Costs + +2021-01-02 Periodic + ; generated-transaction: ~ 2021-01-02, modified: + Checking -10 + Costs 10 + (Auto) 10 ; generated-posting: = acct:Costs + +>=0