fix: Add balancing amounts in periodic transactions before applying auto-postings. (#1412)

This commit is contained in:
Stephen Morgan 2021-07-26 17:05:45 +10:00 committed by Simon Michael
parent 04a36d4942
commit 2da0e67141
2 changed files with 36 additions and 14 deletions

View File

@ -122,8 +122,14 @@ journalAddForecast :: CliOpts -> Journal -> Journal
journalAddForecast CliOpts{inputopts_=iopts, reportspec_=rspec} j = journalAddForecast CliOpts{inputopts_=iopts, reportspec_=rspec} j =
case forecast_ ropts of case forecast_ ropts of
Nothing -> j Nothing -> j
Just _ -> either (error') id . journalApplyCommodityStyles $ -- PARTIAL: Just _ -> either error id $ do -- PARTIAL:
journalBalanceTransactions' iopts j{ jtxns = concat [jtxns j, forecasttxns'] } 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 where
today = _rsDay rspec today = _rsDay rspec
ropts = _rsReportOpts rspec ropts = _rsReportOpts rspec
@ -141,18 +147,7 @@ journalAddForecast CliOpts{inputopts_=iopts, reportspec_=rspec} j =
(fromMaybe nulldatespan $ dbg2 "forecastspan flag" $ forecast_ ropts) (fromMaybe nulldatespan $ dbg2 "forecastspan flag" $ forecast_ ropts)
(DateSpan (Just forecastbeginDefault) (Just forecastendDefault)) (DateSpan (Just forecastbeginDefault) (Just forecastendDefault))
forecasttxns = addAutoTxns = if auto_ iopts then modifyTransactions today (jtxnmodifiers j) else return
[ 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:
-- | Write some output to stdout or to a file selected by --output-file. -- | Write some output to stdout or to a file selected by --output-file.
-- If the file exists it will be overwritten. -- If the file exists it will be overwritten.

View File

@ -384,3 +384,30 @@ $ hledger -f- print --auto
(b) 100 EUR ; generated-posting: = assets amt:>50 (b) 100 EUR ; generated-posting: = assets amt:>50
>=0 >=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