feat: lib: Allow special string %account in auto-posting rules (#1975)

This allows using the special string `%account` in auto posting rules.
When run, this will be substituted with the account name of the matched
posting.
This commit is contained in:
Stephen Morgan 2025-06-18 20:10:41 +10:00 committed by Simon Michael
parent b996b7fadc
commit 8ab2fb6b77
3 changed files with 27 additions and 0 deletions

View File

@ -109,6 +109,7 @@ tmPostingRuleToFunction verbosetags styles query querytxt tmpr =
\p -> styleAmounts styles . renderPostingCommentDates $ pr \p -> styleAmounts styles . renderPostingCommentDates $ pr
{ pdate = pdate pr <|> pdate p { pdate = pdate pr <|> pdate p
, pdate2 = pdate2 pr <|> pdate2 p , pdate2 = pdate2 pr <|> pdate2 p
, paccount = account' p
, pamount = amount' p , pamount = amount' p
, pcomment = pcomment pr & (if verbosetags then (`commentAddTag` ("generated-posting",qry)) else id) , pcomment = pcomment pr & (if verbosetags then (`commentAddTag` ("generated-posting",qry)) else id)
, ptags = ptags pr , ptags = ptags pr
@ -119,6 +120,10 @@ tmPostingRuleToFunction verbosetags styles query querytxt tmpr =
pr = tmprPosting tmpr pr = tmprPosting tmpr
qry = "= " <> querytxt qry = "= " <> querytxt
symq = filterQuery (liftA2 (||) queryIsSym queryIsAmt) query 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 amount' = case postingRuleMultiplier tmpr of
Nothing -> const $ pamount pr Nothing -> const $ pamount pr
Just n -> \p -> Just n -> \p ->

View File

@ -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, 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. 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. 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. So auto postings can be useful for, eg, adding tax postings with a standard percentage.
AMOUNT can be: AMOUNT can be:

View File

@ -426,3 +426,22 @@ $ hledger -f- print --forecast --auto --explicit
a 1 a 1
$ hledger -f- --auto check $ 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