Add absamount as --sort field for register

This commit is contained in:
Michael Rees 2024-07-22 21:41:32 -05:00 committed by Simon Michael
parent d96e3a1e5a
commit 52253c01f6
4 changed files with 27 additions and 3 deletions

View File

@ -129,11 +129,14 @@ comparePostings ropts (ex:es) (a, pa) (b, pb) =
description = fmap (\t -> tdescription t) tx
-- If there's no transaction attached, then use empty text for the description
in fromMaybe "" description
absamount = mapMixedAmount (\amt -> amt { aquantity = abs (aquantity amt)})
comparison = case ex of
AbsAmount' False -> compare (absamount (pamount a)) (absamount (pamount b))
Amount' False -> compare (pamount a) (pamount b)
Account' False -> compare (paccount a) (paccount b)
Date' False -> compare (postingDateOrDate2 (whichDate ropts) a) (postingDateOrDate2 (whichDate ropts) b)
Description' False -> compare (getDescription a) (getDescription b)
AbsAmount' True -> compare (Down (absamount (pamount a))) (Down (absamount (pamount b)))
Amount' True -> compare (Down (pamount a)) (Down (pamount b))
Account' True -> compare (Down (paccount a)) (Down (paccount b))
Date' True -> compare (Down (postingDateOrDate2 (whichDate ropts) a)) (Down (postingDateOrDate2 (whichDate ropts) b))

View File

@ -676,7 +676,8 @@ queryFromFlags ReportOpts{..} = simplifyQuery $ And flagsq
-- Each of these takes a bool, which shows if it has been inverted
-- (True -> has been inverted, reverse the order)
data SortField
= Account' Bool
= AbsAmount' Bool
| Account' Bool
| Amount' Bool
| Date' Bool
| Description' Bool
@ -700,6 +701,7 @@ getSortSpec opts =
"amount" -> Amount' isNegated
"desc" -> Description' isNegated
"description" -> Description' isNegated
"absamount" -> AbsAmount' isNegated
_ -> error' $ "unsupported field '" ++ t ++ "' given to --sort"
where isNegated = isPrefixOf "-" t
trimmed = fromMaybe t (stripPrefix "-" t)

View File

@ -83,8 +83,8 @@ It's also useful to show postings on the checking account together with the
related account:
The `--sort=FIELDS` flag sorts by the fields given, which can be any of
`account`, `amount`, `date`, or `desc`/`description`, optionally separated by
commas.
`account`, `amount`, `absamount`, `date`, or `desc`/`description`, optionally
separated by commas.
For example, `--sort account,amount` will group all transactions in each
account, sorted by transaction amount.
Each field can be negated by a preceding `-`, so `--sort -amount` will show

View File

@ -89,3 +89,22 @@ $ hledger -f - register --sort desc
a -1 0
2024-01-01 Other a 1 1
b -1 0
# ** 7. --sort with absamount
<
2024-01-01 Demo
a 15
b 10
c -25
2024-01-02 Other
d 12
e -48
f 36
$ hledger -f - register --sort -absamount
2024-01-02 Other e -48 -48
f 36 -12
2024-01-01 Demo c -25 -37
a 15 -22
2024-01-02 Other d 12 -10
2024-01-01 Demo b 10 0