Skip to content

Commit 901481a

Browse files
committed
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.
1 parent b996b7f commit 901481a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

hledger-lib/Hledger/Data/TransactionModifier.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ tmPostingRuleToFunction verbosetags styles query querytxt tmpr =
109109
\p -> styleAmounts styles . renderPostingCommentDates $ pr
110110
{ pdate = pdate pr <|> pdate p
111111
, pdate2 = pdate2 pr <|> pdate2 p
112+
, paccount = account' p
112113
, pamount = amount' p
113114
, pcomment = pcomment pr & (if verbosetags then (`commentAddTag` ("generated-posting",qry)) else id)
114115
, ptags = ptags pr
@@ -119,6 +120,10 @@ tmPostingRuleToFunction verbosetags styles query querytxt tmpr =
119120
pr = tmprPosting tmpr
120121
qry = "= " <> querytxt
121122
symq = filterQuery (liftA2 (||) queryIsSym queryIsAmt) query
123+
account' = if accountTemplate `T.isInfixOf` paccount pr
124+
then \p -> T.replace accountTemplate (paccount p) $ paccount pr
125+
else const $ paccount pr
126+
where accountTemplate = "%account"
122127
amount' = case postingRuleMultiplier tmpr of
123128
Nothing -> const $ pamount pr
124129
Just n -> \p ->

hledger/hledger.m4.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,6 +2706,9 @@ the rule's postings are added to that transaction, immediately below the matched
27062706
Note these generated postings are temporary, existing only for the duration of the report,
27072707
and only when `--auto` is used; they are not saved in the journal file by hledger.
27082708

2709+
The postings can contain the special string `%account` which will be expanded to
2710+
the account name of the matched account.
2711+
27092712
Generated postings' amounts can depend on the matched posting's amount.
27102713
So auto postings can be useful for, eg, adding tax postings with a standard percentage.
27112714
AMOUNT can be:

hledger/test/journal/auto-postings.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,22 @@ $ hledger -f- print --forecast --auto --explicit
426426
a 1
427427

428428
$ hledger -f- --auto check
429+
430+
# 22. any instance of %account in the account name is substituted for the original account
431+
<
432+
2023-01-06 * A client
433+
Income:Revenue -1190 € ; tax: 19%
434+
Assets:Bank
435+
436+
= acct:Income tag:tax=19%
437+
%account *-0.15966387 ; 1-100/119
438+
Liabilities:VAT *0.15966387 ; 1-100/119
439+
440+
$ hledger -f- --auto print
441+
2023-01-06 * A client
442+
Income:Revenue -1190 € ; tax: 19%
443+
Income:Revenue 190.0000053 € ; 1-100/119
444+
Liabilities:VAT -190.0000053 € ; 1-100/119
445+
Assets:Bank
446+
447+
>=0

0 commit comments

Comments
 (0)