budget: only periodic txns with the selected interval are used

This commit is contained in:
Simon Michael 2018-03-29 15:50:48 +01:00
parent 6f8714cdbd
commit 083d9190fd
3 changed files with 26 additions and 6 deletions

View File

@ -16,6 +16,7 @@ module Hledger.Data.AutoTransaction
-- * Accessors
, mtvaluequery
, jdatespan
, periodTransactionInterval
)
where
@ -260,3 +261,14 @@ runPeriodicTransaction pt = generate where
in
if d == firstDate then (i,s)
else error' $ "Unable to generate transactions according to "++(show periodExpr)++" as "++(show d)++" is not a first day of the "++x
-- | What is the interval of this 'PeriodicTransaction's period expression, if it can be parsed ?
periodTransactionInterval :: PeriodicTransaction -> Maybe Interval
periodTransactionInterval pt =
let
expr = ptperiodicexpr pt
err = error' $ "Current date cannot be referenced in " ++ show (T.unpack expr)
in
case parsePeriodExpr err expr of
Left _ -> Nothing
Right (i,_) -> Just i

View File

@ -19,6 +19,7 @@ module Hledger.Reports.ReportOptions (
simplifyStatuses,
whichDateFromOpts,
journalSelectingAmountFromOpts,
intervalFromRawOpts,
queryFromOpts,
queryFromOptsOnly,
queryOptsFromOpts,

View File

@ -368,14 +368,21 @@ budgetRollUp CliOpts{rawopts_=rawopts} budget j = j { jtxns = remapTxn <$> jtxns
remapTxn = mapPostings (map remapPosting)
mapPostings f t = txnTieKnot $ t { tpostings = f $ tpostings t }
-- | Generate journal of all periodic transactions in the given journal for the
-- entirety of its history or reporting period, whatever is smaller.
-- | Select all periodic transactions from the given journal which
-- match the opts-specified report interval, and use them to generate
-- budget transactions (like forecast transactions) in the specified
-- report period.
budgetJournal :: CliOpts -> Journal -> Journal
budgetJournal opts j = journalBalanceTransactions' opts j { jtxns = budget }
budgetJournal opts j = journalBalanceTransactions' opts j { jtxns = budgetts }
where
dates = spanIntersect (jdatespan j) (periodAsDateSpan $ period_ $ reportopts_ opts)
budget = [makeBudget t | pt <- jperiodictxns j, t <- runPeriodicTransaction pt dates]
makeBudget t = txnTieKnot $ t { tdescription = T.pack "Budget transaction" }
interval = intervalFromRawOpts $ rawopts_ opts
dates = spanIntersect (jdatespan j) (periodAsDateSpan $ period_ $ reportopts_ opts)
budgetts = [makeBudgetTxn t
| pt <- jperiodictxns j
, periodTransactionInterval pt == Just interval
, t <- runPeriodicTransaction pt dates
]
makeBudgetTxn t = txnTieKnot $ t { tdescription = T.pack "Budget transaction" }
journalBalanceTransactions' opts j =
let assrt = not . ignore_assertions_ $ inputopts_ opts
in