imp: revert 2b5194238 imp: generate auto postings on forecast transactions by default (#2027)

I found at least one user for whom this would be a breaking change
(they generate forecast txns, and have auto posting rules, but don't
want the latter applied to the former). I guess it's better to keep
things as they were for now: if you need auto postings on your
forecast txns you must use two flags, --forecast --auto.
This commit is contained in:
Simon Michael 2023-04-30 07:53:01 -10:00
parent 4885b3ad32
commit a771c8fc19
6 changed files with 34 additions and 59 deletions

View File

@ -785,16 +785,12 @@ journalUntieTransactions t@Transaction{tpostings=ps} = t{tpostings=map (\p -> p{
-- postings to transactions, eg). Or if a modifier rule fails to parse, -- postings to transactions, eg). Or if a modifier rule fails to parse,
-- return the error message. A reference date is provided to help interpret -- return the error message. A reference date is provided to help interpret
-- relative dates in transaction modifier queries. -- relative dates in transaction modifier queries.
-- The first argument selects whether to modify only generated (--forecast) transactions (False), -- The first argument selects whether to add visible tags to generated postings & modified transactions.
-- or all transactions (True). The second adds visible tags if true. journalModifyTransactions :: Bool -> Day -> Journal -> Either String Journal
journalModifyTransactions :: Bool -> Bool -> Day -> Journal -> Either String Journal journalModifyTransactions verbosetags d j =
journalModifyTransactions alltxns verbosetags d j = case modifyTransactions (journalAccountType j) (journalInheritedAccountTags j) (journalCommodityStyles j) d verbosetags (jtxnmodifiers j) (jtxns j) of
case modifyTransactions predfn (journalAccountType j) (journalInheritedAccountTags j) (journalCommodityStyles j) d verbosetags (jtxnmodifiers j) (jtxns j) of
Right ts -> Right j{jtxns=ts} Right ts -> Right j{jtxns=ts}
Left err -> Left err Left err -> Left err
where
predfn = if alltxns then const True else isgenerated
isgenerated = matchesTransaction (Tag (toRegex' "_generated-transaction") Nothing)
-- | Choose and apply a consistent display style to the posting -- | Choose and apply a consistent display style to the posting
-- amounts in each commodity (see journalCommodityStyles). -- amounts in each commodity (see journalCommodityStyles).

View File

@ -26,8 +26,7 @@ import Hledger.Data.Transaction (txnTieKnot)
import Hledger.Query (Query, filterQuery, matchesAmount, matchesPostingExtra, import Hledger.Query (Query, filterQuery, matchesAmount, matchesPostingExtra,
parseQuery, queryIsAmt, queryIsSym, simplifyQuery) parseQuery, queryIsAmt, queryIsSym, simplifyQuery)
import Hledger.Data.Posting (commentJoin, commentAddTag, postingAddTags, postingApplyCommodityStyles) import Hledger.Data.Posting (commentJoin, commentAddTag, postingAddTags, postingApplyCommodityStyles)
import Hledger.Utils (wrap) import Hledger.Utils (dbg6, wrap)
import Hledger.Utils.Debug
-- $setup -- $setup
-- >>> :set -XOverloadedStrings -- >>> :set -XOverloadedStrings
@ -35,21 +34,19 @@ import Hledger.Utils.Debug
-- >>> import Hledger.Data.Transaction -- >>> import Hledger.Data.Transaction
-- >>> import Hledger.Data.Journal -- >>> import Hledger.Data.Journal
-- | Apply all the given transaction modifiers, in turn, to each transaction -- | Apply all the given transaction modifiers, in turn, to each transaction.
-- for which the given predicate is true.
-- Or if any of them fails to be parsed, return the first error. A reference -- Or if any of them fails to be parsed, return the first error. A reference
-- date is provided to help interpret relative dates in transaction modifier -- date is provided to help interpret relative dates in transaction modifier
-- queries. -- queries.
modifyTransactions :: (Transaction -> Bool) modifyTransactions :: (AccountName -> Maybe AccountType)
-> (AccountName -> Maybe AccountType)
-> (AccountName -> [Tag]) -> (AccountName -> [Tag])
-> M.Map CommoditySymbol AmountStyle -> M.Map CommoditySymbol AmountStyle
-> Day -> Bool -> [TransactionModifier] -> [Transaction] -> Day -> Bool -> [TransactionModifier] -> [Transaction]
-> Either String [Transaction] -> Either String [Transaction]
modifyTransactions predfn atypes atags styles d verbosetags tmods ts = do modifyTransactions atypes atags styles d verbosetags tmods ts = do
fs <- mapM (transactionModifierToFunction atypes atags styles d verbosetags) tmods -- convert modifiers to functions, or return a parse error fs <- mapM (transactionModifierToFunction atypes atags styles d verbosetags) tmods -- convert modifiers to functions, or return a parse error
let let
maybemodifytxn t = if predfn t then t'' else t modifytxn t = t''
where where
t' = foldr (flip (.)) id fs t -- apply each function in turn t' = foldr (flip (.)) id fs t -- apply each function in turn
t'' = if t' == t t'' = if t' == t
@ -58,7 +55,7 @@ modifyTransactions predfn atypes atags styles d verbosetags tmods ts = do
,ttags=ttags t' & (("_modified","") :) & (if verbosetags then (("modified","") :) else id) ,ttags=ttags t' & (("_modified","") :) & (if verbosetags then (("modified","") :) else id)
} }
Right $ map maybemodifytxn ts Right $ map modifytxn ts
-- | Converts a 'TransactionModifier' to a 'Transaction'-transforming function -- | Converts a 'TransactionModifier' to a 'Transaction'-transforming function
-- which applies the modification(s) specified by the TransactionModifier. -- which applies the modification(s) specified by the TransactionModifier.

View File

@ -325,8 +325,8 @@ journalFinalise iopts@InputOpts{..} f txt pj = do
& journalApplyCommodityStyles -- Infer and apply commodity styles - should be done early & journalApplyCommodityStyles -- Infer and apply commodity styles - should be done early
<&> journalAddForecast (verbose_tags_) (forecastPeriod iopts pj) -- Add forecast transactions if enabled <&> journalAddForecast (verbose_tags_) (forecastPeriod iopts pj) -- Add forecast transactions if enabled
<&> journalPostingsAddAccountTags -- Add account tags to postings, so they can be matched by auto postings. <&> journalPostingsAddAccountTags -- Add account tags to postings, so they can be matched by auto postings.
>>= (if not (null $ jtxnmodifiers pj) >>= (if auto_ && not (null $ jtxnmodifiers pj)
then journalAddAutoPostings auto_ verbose_tags_ _ioDay balancingopts_ -- Add auto postings if enabled, and account tags if needed then journalAddAutoPostings verbose_tags_ _ioDay balancingopts_ -- Add auto postings if enabled, and account tags if needed
else pure) else pure)
-- >>= Right . dbg0With (concatMap (T.unpack.showTransaction).jtxns) -- debug -- >>= Right . dbg0With (concatMap (T.unpack.showTransaction).jtxns) -- debug
>>= journalMarkRedundantCosts -- Mark redundant costs, to help journalBalanceTransactions ignore them >>= journalMarkRedundantCosts -- Mark redundant costs, to help journalBalanceTransactions ignore them
@ -347,16 +347,15 @@ journalFinalise iopts@InputOpts{..} f txt pj = do
return j return j
-- | Apply any auto posting rules to generate extra postings on this journal's transactions. -- | Apply any auto posting rules to generate extra postings on this journal's transactions.
-- With a true first argument, applies them to all transactions, otherwise only to generated transactions. -- With a true first argument, adds visible tags to generated postings and modified transactions.
-- With a true second argument, adds visible tags to generated postings and modified transactions. journalAddAutoPostings :: Bool -> Day -> BalancingOpts -> Journal -> Either String Journal
journalAddAutoPostings :: Bool -> Bool -> Day -> BalancingOpts -> Journal -> Either String Journal journalAddAutoPostings verbosetags d bopts =
journalAddAutoPostings alltxns verbosetags d bopts =
-- Balance all transactions without checking balance assertions, -- Balance all transactions without checking balance assertions,
journalBalanceTransactions bopts{ignore_assertions_=True} journalBalanceTransactions bopts{ignore_assertions_=True}
-- then add the auto postings -- then add the auto postings
-- (Note adding auto postings after balancing means #893b fails; -- (Note adding auto postings after balancing means #893b fails;
-- adding them before balancing probably means #893a, #928, #938 fail.) -- adding them before balancing probably means #893a, #928, #938 fail.)
>=> journalModifyTransactions alltxns verbosetags d >=> journalModifyTransactions verbosetags d
-- | Generate periodic transactions from all periodic transaction rules in the journal. -- | Generate periodic transactions from all periodic transaction rules in the journal.
-- These transactions are added to the in-memory Journal (but not the on-disk file). -- These transactions are added to the in-memory Journal (but not the on-disk file).

View File

@ -43,7 +43,7 @@ rewrite opts@CliOpts{rawopts_=rawopts,reportspec_=rspec} j@Journal{jtxns=ts} = d
today = _rsDay rspec today = _rsDay rspec
verbosetags = boolopt "verbose-tags" rawopts verbosetags = boolopt "verbose-tags" rawopts
modifiers = transactionModifierFromOpts opts : jtxnmodifiers j modifiers = transactionModifierFromOpts opts : jtxnmodifiers j
let j' = j{jtxns=either error' id $ modifyTransactions (const True) (journalAccountType j) (journalInheritedAccountTags j) mempty today verbosetags modifiers ts} -- PARTIAL: let j' = j{jtxns=either error' id $ modifyTransactions (journalAccountType j) (journalInheritedAccountTags j) mempty today verbosetags modifiers ts} -- PARTIAL:
-- run the print command, showing all transactions, or show diffs -- run the print command, showing all transactions, or show diffs
printOrDiff rawopts opts{reportspec_=rspec{_rsQuery=Any}} j j' printOrDiff rawopts opts{reportspec_=rspec{_rsQuery=Any}} j j'

View File

@ -2444,18 +2444,10 @@ add one or more companion postings below that one, optionally influenced
by the matched posting's amount. This can be useful for generating by the matched posting's amount. This can be useful for generating
tax postings with a standard percentage, for example. tax postings with a standard percentage, for example.
By default, these auto posting rules are applied to transactions generated Note that depending on generated data is not ideal for financial records
with --forecast (since 1.30), but not to transactions recorded in the journal. (it's less portable, less future-proof, less auditable by others,
This means you can use `~` (periodic transaction) and `=` (auto posting) rules and less robust, since other features like balance assertions will depend
together to generate forecast transactions, and when such a transaction actually occurs, on using or not using `--auto`).
you can save the generated entry to the journal, finalising it.
If instead you want to apply auto posting rules to recorded transactions
as well, then use the `--auto` flag.
This is not the default behaviour because depending on generated data
is not ideal for financial records (it's less portable, less future-proof,
less auditable, and less robust, since other features like balance assertions
will be affected by the use or non-use of `--auto`.)
An auto posting rule looks a bit like a transaction: An auto posting rule looks a bit like a transaction:
```journal ```journal
@ -5014,12 +5006,6 @@ When --forecast is not doing what you expect, one of these tips should help:
- Consult [Forecast period, in detail](#forecast-period-in-detail), above. - Consult [Forecast period, in detail](#forecast-period-in-detail), above.
- Check inside the engine: add `--debug=2` (eg). - Check inside the engine: add `--debug=2` (eg).
## Forecast and auto postings
Forecast transactions have one more feature: when they are generated,
any applicable [auto posting rules](#auto-postings) will also be applied to them,
generating additional postings. These are described below.
# Budgeting # Budgeting
With the balance command's [`--budget` report](#budget-report), With the balance command's [`--budget` report](#budget-report),

View File

@ -242,7 +242,7 @@ $ hledger -f- print --auto
# #
## Transaction modifiers affect only forecast transactions by default: ## Transaction modifiers affect forecast transactions (#959)
< <
= ^income = ^income
(liabilities:tax) *.33 ; income tax (liabilities:tax) *.33 ; income tax
@ -252,15 +252,15 @@ $ hledger -f- print --auto
income:donations $-15 income:donations $-15
assets:bank assets:bank
2016/1/3 2016/1/3 withdraw
assets:cash $100 assets:cash $20
income:gifts assets:bank
# 13. # 13.
$ hledger print -f- --forecast -b 2016-01 -e 2016-03 $ hledger print -f- --auto --forecast -b 2016-01 -e 2016-03
2016-01-03 2016-01-03 withdraw
assets:cash $100 assets:cash $20
income:gifts assets:bank
2016-02-01 paycheck 2016-02-01 paycheck
income:remuneration $-100 income:remuneration $-100
@ -271,18 +271,15 @@ $ hledger print -f- --forecast -b 2016-01 -e 2016-03
>= >=
# 14. With --auto, they affect all transactions: # 14. and they don't force --auto on
$ hledger print -f- --auto --forecast -b 2016-01 -e 2016-03 $ hledger print -f- --forecast -b 2016-01 -e 2016-03
2016-01-03 2016-01-03 withdraw
assets:cash $100 assets:cash $20
income:gifts assets:bank
(liabilities:tax) $-33 ; income tax
2016-02-01 paycheck 2016-02-01 paycheck
income:remuneration $-100 income:remuneration $-100
(liabilities:tax) $-33 ; income tax
income:donations $-15 income:donations $-15
(liabilities:tax) $-4.95 ; income tax
assets:bank assets:bank
>= >=