feat: cli: Support colon-delimited --pivot (#2050)

feat:cli: --pivot now can construct an account name from multiple colon-delimited fields
This commit is contained in:
Eric Mertens 2023-06-09 11:00:01 -07:00 committed by GitHub
parent 29caeb9e34
commit e668506d26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 12 deletions

View File

@ -1083,16 +1083,23 @@ transactionPivot fieldortagname t = t{tpostings = map (postingPivot fieldortagna
-- | Replace this posting's account name with the value
-- of the given field or tag, if any, otherwise the empty string.
postingPivot :: Text -> Posting -> Posting
postingPivot fieldortagname p = p{paccount = pivotedacct, poriginal = Just $ originalPosting p}
where
pivotedacct
| Just t <- ptransaction p, fieldortagname == "code" = tcode t
| Just t <- ptransaction p, fieldortagname == "description" = tdescription t
| Just t <- ptransaction p, fieldortagname == "payee" = transactionPayee t
| Just t <- ptransaction p, fieldortagname == "note" = transactionNote t
| Just t <- ptransaction p, fieldortagname == "status" = T.pack . show . tstatus $ t
| Just (_, value) <- postingFindTag fieldortagname p = value
| otherwise = ""
postingPivot fieldortagname p =
p{paccount = pivotAccount fieldortagname p, poriginal = Just $ originalPosting p}
pivotAccount :: Text -> Posting -> Text
pivotAccount fieldortagname p =
T.intercalate ":" [pivotComponent x p | x <- T.splitOn ":" fieldortagname]
pivotComponent :: Text -> Posting -> Text
pivotComponent fieldortagname p
| fieldortagname == "acct" = paccount p
| Just t <- ptransaction p, fieldortagname == "code" = tcode t
| Just t <- ptransaction p, fieldortagname == "description" = tdescription t
| Just t <- ptransaction p, fieldortagname == "payee" = transactionPayee t
| Just t <- ptransaction p, fieldortagname == "note" = transactionNote t
| Just t <- ptransaction p, fieldortagname == "status" = T.pack . show . tstatus $ t
| Just (_, value) <- postingFindTag fieldortagname p = value
| otherwise = ""
postingFindTag :: TagName -> Posting -> Maybe (TagName, TagValue)
postingFindTag tagname p = find ((tagname==) . fst) $ postingAllTags p

View File

@ -4742,16 +4742,17 @@ see the discussion at [#1625](https://github.com/simonmichael/hledger/issues/162
Normally, hledger groups and sums amounts within each account.
The `--pivot FIELD` option substitutes some other transaction field for account names,
causing amounts to be grouped and summed by that field's value instead.
FIELD can be any of the transaction fields `status`, `code`, `description`, `payee`, `note`, or a tag name.
FIELD can be any of the transaction fields `acct`, `status`, `code`, `description`, `payee`, `note`, or a tag name.
When pivoting on a tag and a posting has multiple values of that tag, only the first value is displayed.
Values containing `colon:separated:parts` will be displayed hierarchically, like account names.
Multiple, colon-delimited fields can be pivoted simultaneously, generating a hierarchical account name.
Some examples:
```journal
2016/02/16 Yearly Dues Payment
assets:bank account 2 EUR
income:dues -2 EUR ; member: John Doe
income:dues -2 EUR ; member: John Doe, kind: Lifetime
```
Normal balance report showing account names:
```shell
@ -4783,6 +4784,13 @@ $ hledger balance --pivot member acct:.
--------------------
-2 EUR
```
Hierarchical reports can be generated with multiple pivots:
```shell
$ hledger balance Income:Dues --pivot kind:member
-2 EUR Lifetime:John Doe
--------------------
-2 EUR
```
# Generating data

View File

@ -117,3 +117,14 @@ $ hledger -f- --pivot payee reg -D ^expense
2016-02-16 Auchan 22 EUR 22 EUR
StarBars 5 EUR 27 EUR
2016-02-17 Auchan 30 EUR 57 EUR
# 11. pivot on multiple tags
<
2023-01-01 compound purchase
expenses 10 ; project: job1, kind: equipment
expenses 20 ; project: job2, kind: equipment
expenses 25 ; project: job2, kind: fee
assets
$ hledger -f- --pivot acct:kind:project bal ^expense -N
10 expenses:equipment:job1
20 expenses:equipment:job2
25 expenses:fee:job2