Skip to content
Permalink
Browse files

lib: accountTransactionsReportItems: rewrite using catMaybes and mapA…

…ccumL

I find the report function less convolved without the integrated recursion.
  • Loading branch information...
thielema authored and simonmichael committed Jun 9, 2019
1 parent 88f3f6f commit 4962b2696a55f7fa9b3667e75748a3fbaa3fc029
Showing with 10 additions and 8 deletions.
  1. +10 −8 hledger-lib/Hledger/Reports/AccountTransactionsReport.hs
@@ -17,6 +17,7 @@ where

import Data.List
import Data.Ord
import Data.Maybe
import qualified Data.Text as T
import Data.Time.Calendar

@@ -124,19 +125,21 @@ accountTransactionsReport opts j reportq thisacctq = (label, items)
-- function and a balance-summing function. Or with a None current account
-- query, this can also be used for the transactionsReport.
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount) -> [Transaction] -> [AccountTransactionsReportItem]
accountTransactionsReportItems _ _ _ _ [] = []
accountTransactionsReportItems reportq thisacctq bal signfn (torig:ts) =
case i of Just i' -> i':is
Nothing -> is
accountTransactionsReportItems reportq thisacctq bal signfn =
catMaybes . snd .
mapAccumL (accountTransactionsReportItem reportq thisacctq signfn) bal

accountTransactionsReportItem :: Query -> Query -> (MixedAmount -> MixedAmount) -> MixedAmount -> Transaction -> (MixedAmount, Maybe AccountTransactionsReportItem)
accountTransactionsReportItem reportq thisacctq signfn bal torig = balItem
-- 201403: This is used for both accountTransactionsReport and transactionsReport, which makes it a bit overcomplicated
-- 201407: I've lost my grip on this, let's just hope for the best
-- 201606: we now calculate change and balance from filtered postings, check this still works well for all callers XXX
where
tfiltered@Transaction{tpostings=reportps} = filterTransactionPostings reportq torig
tacct = tfiltered{tdate=transactionRegisterDate reportq thisacctq tfiltered}
(i,bal') = case reportps of
[] -> (Nothing,bal) -- no matched postings in this transaction, skip it
_ -> (Just (torig, tacct, numotheraccts > 1, otheracctstr, a, b), b)
balItem = case reportps of
[] -> (bal, Nothing) -- no matched postings in this transaction, skip it
_ -> (b, Just (torig, tacct, numotheraccts > 1, otheracctstr, a, b))
where
(thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps
numotheraccts = length $ nub $ map paccount otheracctps
@@ -145,7 +148,6 @@ accountTransactionsReportItems reportq thisacctq bal signfn (torig:ts) =
| otherwise = summarisePostingAccounts otheracctps -- summarise matched postings to other account(s)
a = signfn $ negate $ sum $ map pamount thisacctps
b = bal + a
is = accountTransactionsReportItems reportq thisacctq bal' signfn ts

-- | What is the transaction's date in the context of a particular account
-- (specified with a query) and report query, as in an account register ?

0 comments on commit 4962b26

Please sign in to comment.
You can’t perform that action at this time.