diff --git a/hledger-lib/Hledger/Data/TransactionModifier.hs b/hledger-lib/Hledger/Data/TransactionModifier.hs index 97b72d7c8..861856c40 100644 --- a/hledger-lib/Hledger/Data/TransactionModifier.hs +++ b/hledger-lib/Hledger/Data/TransactionModifier.hs @@ -109,6 +109,7 @@ tmPostingRuleToFunction verbosetags styles query querytxt tmpr = \p -> styleAmounts styles . renderPostingCommentDates $ pr { pdate = pdate pr <|> pdate p , pdate2 = pdate2 pr <|> pdate2 p + , paccount = account' p , pamount = amount' p , pcomment = pcomment pr & (if verbosetags then (`commentAddTag` ("generated-posting",qry)) else id) , ptags = ptags pr @@ -119,6 +120,10 @@ tmPostingRuleToFunction verbosetags styles query querytxt tmpr = pr = tmprPosting tmpr qry = "= " <> querytxt symq = filterQuery (liftA2 (||) queryIsSym queryIsAmt) query + account' = if accountTemplate `T.isInfixOf` paccount pr + then \p -> T.replace accountTemplate (paccount p) $ paccount pr + else const $ paccount pr + where accountTemplate = "%account" amount' = case postingRuleMultiplier tmpr of Nothing -> const $ pamount pr Just n -> \p -> diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index fa868f5b2..0191c83ce 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -2706,6 +2706,9 @@ the rule's postings are added to that transaction, immediately below the matched Note these generated postings are temporary, existing only for the duration of the report, and only when `--auto` is used; they are not saved in the journal file by hledger. +The postings can contain the special string `%account` which will be expanded to +the account name of the matched account. + Generated postings' amounts can depend on the matched posting's amount. So auto postings can be useful for, eg, adding tax postings with a standard percentage. AMOUNT can be: diff --git a/hledger/test/journal/auto-postings.test b/hledger/test/journal/auto-postings.test index 535f2108a..898f54b2d 100644 --- a/hledger/test/journal/auto-postings.test +++ b/hledger/test/journal/auto-postings.test @@ -426,3 +426,22 @@ $ hledger -f- print --forecast --auto --explicit a 1 $ hledger -f- --auto check + +# 22. any instance of %account in the account name is substituted for the original account +< +2023-01-06 * A client + Income:Revenue -1190 € ; tax: 19% + Assets:Bank + += acct:Income tag:tax=19% + %account *-0.15966387 ; 1-100/119 + Liabilities:VAT *0.15966387 ; 1-100/119 + +$ hledger -f- --auto print +2023-01-06 * A client + Income:Revenue -1190 € ; tax: 19% + Income:Revenue 190.0000053 € ; 1-100/119 + Liabilities:VAT -190.0000053 € ; 1-100/119 + Assets:Bank + +>=0