Skip to content

Commit e4263e6

Browse files
committed
areg: begin respecting --date2: show txns' date2 (#1731)
1 parent b4c1188 commit e4263e6

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

hledger-lib/Hledger/Reports/AccountTransactionsReport.hs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ accountTransactionsReport rspec@ReportSpec{_rsReportOpts=ropts} j thisacctq = it
103103
periodq = Date . periodAsDateSpan $ period_ ropts
104104
amtq = filterQuery queryIsCurOrAmt $ _rsQuery rspec
105105
queryIsCurOrAmt q = queryIsSym q || queryIsAmt q
106+
wd = whichDate ropts
106107

107-
-- Note that within this functions, we are only allowed limited
108+
-- Note that within this function, we are only allowed limited
108109
-- transformation of the transaction postings: this is due to the need to
109110
-- pass the original transactions into accountTransactionsReportItem.
110111
-- Generally, we either include a transaction in full, or not at all.
111112
-- Do some limited filtering and valuing of the journal's transactions:
112113
-- - filter them by the account query if any,
113114
-- - discard amounts not matched by the currency and amount query if any,
114115
-- - then apply valuation if any.
116+
-- Additional reportq filtering, such as date filtering, happens down in
117+
-- accountTransactionsReportItem, which discards transactions with no matched postings.
115118
acctJournal =
116119
ptraceAtWith 5 (("ts3:\n"++).pshowTransactions.jtxns)
117120
-- maybe convert these transactions to cost or value
@@ -149,7 +152,7 @@ accountTransactionsReport rspec@ReportSpec{_rsReportOpts=ropts} j thisacctq = it
149152
-- sort by the transaction's register date, then index, for accurate starting balance
150153
. ptraceAtWith 5 (("ts4:\n"++).pshowTransactions.map snd)
151154
. sortBy (comparing (Down . fst) <> comparing (Down . tindex . snd))
152-
. map (\t -> (transactionRegisterDate reportq thisacctq t, t))
155+
. map (\t -> (transactionRegisterDate wd reportq thisacctq t, t))
153156
$ jtxns acctJournal
154157

155158
pshowTransactions :: [Transaction] -> String
@@ -160,9 +163,8 @@ pshowTransactions = pshow . map (\t -> unwords [show $ tdate t, T.unpack $ tdesc
160163
-- which account to use as the focus, a starting balance, and a sign-setting
161164
-- function.
162165
-- Each transaction is accompanied by the date that should be shown for it
163-
-- in the report, which is not necessarily the transaction date; it is
164-
-- the earliest of the posting dates which match both thisacctq and reportq,
165-
-- otherwise the transaction's date if there are no matching postings.
166+
-- in the report. This is not necessarily the transaction date - see
167+
-- transactionRegisterDate.
166168
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount)
167169
-> [(Day, Transaction)] -> [AccountTransactionsReportItem]
168170
accountTransactionsReportItems reportq thisacctq bal signfn =
@@ -176,7 +178,7 @@ accountTransactionsReportItem reportq thisacctq signfn bal (d, torig)
176178
| null reportps = (bal, Nothing) -- no matched postings in this transaction, skip it
177179
| otherwise = (b, Just (torig, tacct{tdate=d}, numotheraccts > 1, otheracctstr, a, b))
178180
where
179-
tacct@Transaction{tpostings=reportps} = filterTransactionPostings reportq torig
181+
tacct@Transaction{tpostings=reportps} = filterTransactionPostings reportq torig -- TODO needs to consider --date2, #1731
180182
(thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps
181183
numotheraccts = length $ nub $ map paccount otheracctps
182184
otheracctstr | thisacctq == None = summarisePostingAccounts reportps -- no current account ? summarise all matched postings
@@ -185,16 +187,20 @@ accountTransactionsReportItem reportq thisacctq signfn bal (d, torig)
185187
a = signfn . maNegate $ sumPostings thisacctps
186188
b = bal `maPlus` a
187189

188-
-- | What is the transaction's date in the context of a particular account
189-
-- (specified with a query) and report query, as in an account register ?
190-
-- It's normally the transaction's general date, but if any posting(s)
191-
-- matched by the report query and affecting the matched account(s) have
192-
-- their own earlier dates, it's the earliest of these dates.
193-
-- Secondary transaction/posting dates are ignored.
194-
transactionRegisterDate :: Query -> Query -> Transaction -> Day
195-
transactionRegisterDate reportq thisacctq t
196-
| null thisacctps = tdate t
197-
| otherwise = minimum $ map postingDate thisacctps
190+
-- TODO needs checking, cf #1731
191+
-- | What date should be shown for a transaction in an account register report ?
192+
-- This will be in context of a particular account (the "this account" query)
193+
-- and any additional report query. It could be:
194+
--
195+
-- - if postings are matched by both thisacctq and reportq, the earliest of those
196+
-- matched postings' dates (or their secondary dates if --date2 was used)
197+
--
198+
-- - the transaction date, or its secondary date if --date2 was used.
199+
--
200+
transactionRegisterDate :: WhichDate -> Query -> Query -> Transaction -> Day
201+
transactionRegisterDate wd reportq thisacctq t
202+
| not $ null thisacctps = minimum $ map (postingDateOrDate2 wd) thisacctps
203+
| otherwise = transactionDateOrDate2 wd t
198204
where
199205
reportps = tpostings $ filterTransactionPostings reportq t
200206
thisacctps = filter (matchesPosting thisacctq) reportps

hledger/Hledger/Cli/Commands/Aregister.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ aregister opts@CliOpts{rawopts_=rawopts,reportspec_=rspec} j = do
8888
, balanceaccum_= Historical
8989
, querystring_ = querystring
9090
}
91+
wd = whichDate ropts'
9192
-- and regenerate the ReportSpec, making sure to use the above
9293
rspec' <- either fail return $ updateReportSpec ropts' rspec
9394
let
@@ -98,27 +99,27 @@ aregister opts@CliOpts{rawopts_=rawopts,reportspec_=rspec} j = do
9899
reverse items
99100
-- select renderer
100101
render | fmt=="txt" = accountTransactionsReportAsText opts (_rsQuery rspec') thisacctq
101-
| fmt=="csv" = printCSV . accountTransactionsReportAsCsv (_rsQuery rspec') thisacctq
102+
| fmt=="csv" = printCSV . accountTransactionsReportAsCsv wd (_rsQuery rspec') thisacctq
102103
| fmt=="json" = toJsonText
103104
| otherwise = error' $ unsupportedOutputFormatError fmt -- PARTIAL:
104105
where
105106
fmt = outputFormatFromOpts opts
106107

107108
writeOutputLazyText opts $ render items'
108109

109-
accountTransactionsReportAsCsv :: Query -> Query -> AccountTransactionsReport -> CSV
110-
accountTransactionsReportAsCsv reportq thisacctq is =
110+
accountTransactionsReportAsCsv :: WhichDate -> Query -> Query -> AccountTransactionsReport -> CSV
111+
accountTransactionsReportAsCsv wd reportq thisacctq is =
111112
["txnidx","date","code","description","otheraccounts","change","balance"]
112-
: map (accountTransactionsReportItemAsCsvRecord reportq thisacctq) is
113+
: map (accountTransactionsReportItemAsCsvRecord wd reportq thisacctq) is
113114

114-
accountTransactionsReportItemAsCsvRecord :: Query -> Query -> AccountTransactionsReportItem -> CsvRecord
115+
accountTransactionsReportItemAsCsvRecord :: WhichDate -> Query -> Query -> AccountTransactionsReportItem -> CsvRecord
115116
accountTransactionsReportItemAsCsvRecord
116-
reportq thisacctq
117+
wd reportq thisacctq
117118
(t@Transaction{tindex,tcode,tdescription}, _, _issplit, otheracctsstr, change, balance)
118119
= [idx,date,tcode,tdescription,otheracctsstr,amt,bal]
119120
where
120121
idx = T.pack $ show tindex
121-
date = showDate $ transactionRegisterDate reportq thisacctq t
122+
date = showDate $ transactionRegisterDate wd reportq thisacctq t
122123
amt = wbToText $ showMixedAmountB oneLine change
123124
bal = wbToText $ showMixedAmountB oneLine balance
124125

@@ -156,7 +157,7 @@ accountTransactionsReportAsText copts reportq thisacctq items = TB.toLazyText $
156157
--
157158
accountTransactionsReportItemAsText :: CliOpts -> Query -> Query -> Int -> Int -> AccountTransactionsReportItem -> TB.Builder
158159
accountTransactionsReportItemAsText
159-
copts@CliOpts{reportspec_=ReportSpec{_rsReportOpts=ReportOpts{color_}}}
160+
copts@CliOpts{reportspec_=ReportSpec{_rsReportOpts=ropts@ReportOpts{color_}}}
160161
reportq thisacctq preferredamtwidth preferredbalwidth
161162
(t@Transaction{tdescription}, _, _issplit, otheracctsstr, change, balance) =
162163
-- Transaction -- the transaction, unmodified
@@ -184,7 +185,8 @@ accountTransactionsReportItemAsText
184185
where w = fullwidth - wbWidth amt
185186
-- calculate widths
186187
(totalwidth,mdescwidth) = registerWidthsFromOpts copts
187-
(datewidth, date) = (10, showDate $ transactionRegisterDate reportq thisacctq t)
188+
(datewidth, date) = (10, showDate $ transactionRegisterDate wd reportq thisacctq t)
189+
where wd = whichDate ropts
188190
(amtwidth, balwidth)
189191
| shortfall <= 0 = (preferredamtwidth, preferredbalwidth)
190192
| otherwise = (adjustedamtwidth, adjustedbalwidth)

hledger/test/aregister.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,24 @@ Transactions in a and subaccounts:
100100
-1 B
101101
1 C
102102

103+
# aregister respects --date2 (#1731).
104+
<
105+
2021-01-01=2021-02-02
106+
(a) 1
107+
108+
# 7. With --date2, it should show the secondary transaction date.
109+
$ hledger -f- areg a --date2
110+
Transactions in a and subaccounts:
111+
2021-02-02 a 1 1
112+
113+
# # 8. With --date2, it should match on the secondary transaction date.
114+
# $ hledger -f- areg a --date2 date:202102
115+
# 2021-02-02 a 1 1
116+
117+
# # 9. ditto
118+
# $ hledger -f- areg a --date2 date2:202102
119+
# 2021-02-02 a 1 1
120+
121+
# # 10. should show the transaction, with the january date (?)
122+
# $ hledger -f- areg a date2:202102
123+
# 2021-01-01 a 1 1

0 commit comments

Comments
 (0)