diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 86d84aaba..bd86b2349 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -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, -- return the error message. A reference date is provided to help interpret -- relative dates in transaction modifier queries. --- The first argument selects whether to modify only generated (--forecast) transactions (False), --- or all transactions (True). The second adds visible tags if true. -journalModifyTransactions :: Bool -> Bool -> Day -> Journal -> Either String Journal -journalModifyTransactions alltxns verbosetags d j = - case modifyTransactions predfn (journalAccountType j) (journalInheritedAccountTags j) (journalCommodityStyles j) d verbosetags (jtxnmodifiers j) (jtxns j) of +-- The first argument selects whether to add visible tags to generated postings & modified transactions. +journalModifyTransactions :: Bool -> Day -> Journal -> Either String Journal +journalModifyTransactions verbosetags d j = + case modifyTransactions (journalAccountType j) (journalInheritedAccountTags j) (journalCommodityStyles j) d verbosetags (jtxnmodifiers j) (jtxns j) of Right ts -> Right j{jtxns=ts} 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 -- amounts in each commodity (see journalCommodityStyles). diff --git a/hledger-lib/Hledger/Data/TransactionModifier.hs b/hledger-lib/Hledger/Data/TransactionModifier.hs index 13b9b4b1d..65225fd4b 100644 --- a/hledger-lib/Hledger/Data/TransactionModifier.hs +++ b/hledger-lib/Hledger/Data/TransactionModifier.hs @@ -26,8 +26,7 @@ import Hledger.Data.Transaction (txnTieKnot) import Hledger.Query (Query, filterQuery, matchesAmount, matchesPostingExtra, parseQuery, queryIsAmt, queryIsSym, simplifyQuery) import Hledger.Data.Posting (commentJoin, commentAddTag, postingAddTags, postingApplyCommodityStyles) -import Hledger.Utils (wrap) -import Hledger.Utils.Debug +import Hledger.Utils (dbg6, wrap) -- $setup -- >>> :set -XOverloadedStrings @@ -35,21 +34,19 @@ import Hledger.Utils.Debug -- >>> import Hledger.Data.Transaction -- >>> import Hledger.Data.Journal --- | Apply all the given transaction modifiers, in turn, to each transaction --- for which the given predicate is true. +-- | Apply all the given transaction modifiers, in turn, to each transaction. -- 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 -- queries. -modifyTransactions :: (Transaction -> Bool) - -> (AccountName -> Maybe AccountType) +modifyTransactions :: (AccountName -> Maybe AccountType) -> (AccountName -> [Tag]) -> M.Map CommoditySymbol AmountStyle -> Day -> Bool -> [TransactionModifier] -> [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 let - maybemodifytxn t = if predfn t then t'' else t + modifytxn t = t'' where t' = foldr (flip (.)) id fs t -- apply each function in turn 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) } - Right $ map maybemodifytxn ts + Right $ map modifytxn ts -- | Converts a 'TransactionModifier' to a 'Transaction'-transforming function -- which applies the modification(s) specified by the TransactionModifier. diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 2a3ff62f0..b466f4844 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -325,8 +325,8 @@ journalFinalise iopts@InputOpts{..} f txt pj = do & journalApplyCommodityStyles -- Infer and apply commodity styles - should be done early <&> 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. - >>= (if not (null $ jtxnmodifiers pj) - then journalAddAutoPostings auto_ verbose_tags_ _ioDay balancingopts_ -- Add auto postings if enabled, and account tags if needed + >>= (if auto_ && not (null $ jtxnmodifiers pj) + then journalAddAutoPostings verbose_tags_ _ioDay balancingopts_ -- Add auto postings if enabled, and account tags if needed else pure) -- >>= Right . dbg0With (concatMap (T.unpack.showTransaction).jtxns) -- debug >>= journalMarkRedundantCosts -- Mark redundant costs, to help journalBalanceTransactions ignore them @@ -347,16 +347,15 @@ journalFinalise iopts@InputOpts{..} f txt pj = do return j -- | 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 second argument, adds visible tags to generated postings and modified transactions. -journalAddAutoPostings :: Bool -> Bool -> Day -> BalancingOpts -> Journal -> Either String Journal -journalAddAutoPostings alltxns verbosetags d bopts = +-- With a true first argument, adds visible tags to generated postings and modified transactions. +journalAddAutoPostings :: Bool -> Day -> BalancingOpts -> Journal -> Either String Journal +journalAddAutoPostings verbosetags d bopts = -- Balance all transactions without checking balance assertions, journalBalanceTransactions bopts{ignore_assertions_=True} -- then add the auto postings -- (Note adding auto postings after balancing means #893b fails; -- 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. -- These transactions are added to the in-memory Journal (but not the on-disk file). diff --git a/hledger/Hledger/Cli/Commands/Rewrite.hs b/hledger/Hledger/Cli/Commands/Rewrite.hs index 7cf2800c0..6f4232483 100644 --- a/hledger/Hledger/Cli/Commands/Rewrite.hs +++ b/hledger/Hledger/Cli/Commands/Rewrite.hs @@ -43,7 +43,7 @@ rewrite opts@CliOpts{rawopts_=rawopts,reportspec_=rspec} j@Journal{jtxns=ts} = d today = _rsDay rspec verbosetags = boolopt "verbose-tags" rawopts 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 printOrDiff rawopts opts{reportspec_=rspec{_rsQuery=Any}} j j' diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 4e24929e0..eed80de95 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -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 tax postings with a standard percentage, for example. -By default, these auto posting rules are applied to transactions generated -with --forecast (since 1.30), but not to transactions recorded in the journal. -This means you can use `~` (periodic transaction) and `=` (auto posting) rules -together to generate forecast transactions, and when such a transaction actually occurs, -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`.) +Note that depending on generated data is not ideal for financial records +(it's less portable, less future-proof, less auditable by others, +and less robust, since other features like balance assertions will depend +on using or not using `--auto`). An auto posting rule looks a bit like a transaction: ```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. - 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 With the balance command's [`--budget` report](#budget-report), diff --git a/hledger/test/journal/auto-postings.test b/hledger/test/journal/auto-postings.test index 6da1deb05..fb716e469 100644 --- a/hledger/test/journal/auto-postings.test +++ b/hledger/test/journal/auto-postings.test @@ -242,7 +242,7 @@ $ hledger -f- print --auto # -## Transaction modifiers affect only forecast transactions by default: +## Transaction modifiers affect forecast transactions (#959) < = ^income (liabilities:tax) *.33 ; income tax @@ -252,15 +252,15 @@ $ hledger -f- print --auto income:donations $-15 assets:bank -2016/1/3 - assets:cash $100 - income:gifts +2016/1/3 withdraw + assets:cash $20 + assets:bank # 13. -$ hledger print -f- --forecast -b 2016-01 -e 2016-03 -2016-01-03 - assets:cash $100 - income:gifts +$ hledger print -f- --auto --forecast -b 2016-01 -e 2016-03 +2016-01-03 withdraw + assets:cash $20 + assets:bank 2016-02-01 paycheck income:remuneration $-100 @@ -271,18 +271,15 @@ $ hledger print -f- --forecast -b 2016-01 -e 2016-03 >= -# 14. With --auto, they affect all transactions: -$ hledger print -f- --auto --forecast -b 2016-01 -e 2016-03 -2016-01-03 - assets:cash $100 - income:gifts - (liabilities:tax) $-33 ; income tax +# 14. and they don't force --auto on +$ hledger print -f- --forecast -b 2016-01 -e 2016-03 +2016-01-03 withdraw + assets:cash $20 + assets:bank 2016-02-01 paycheck income:remuneration $-100 - (liabilities:tax) $-33 ; income tax income:donations $-15 - (liabilities:tax) $-4.95 ; income tax assets:bank >=