lib: accountTransactionsReportItems: rewrite using catMaybes and mapAccumL
I find the report function less convolved without the integrated recursion.
This commit is contained in:
parent
88f3f6fc41
commit
4962b2696a
@ -17,6 +17,7 @@ where
|
|||||||
|
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.Ord
|
import Data.Ord
|
||||||
|
import Data.Maybe
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Time.Calendar
|
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
|
-- function and a balance-summing function. Or with a None current account
|
||||||
-- query, this can also be used for the transactionsReport.
|
-- query, this can also be used for the transactionsReport.
|
||||||
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount) -> [Transaction] -> [AccountTransactionsReportItem]
|
accountTransactionsReportItems :: Query -> Query -> MixedAmount -> (MixedAmount -> MixedAmount) -> [Transaction] -> [AccountTransactionsReportItem]
|
||||||
accountTransactionsReportItems _ _ _ _ [] = []
|
accountTransactionsReportItems reportq thisacctq bal signfn =
|
||||||
accountTransactionsReportItems reportq thisacctq bal signfn (torig:ts) =
|
catMaybes . snd .
|
||||||
case i of Just i' -> i':is
|
mapAccumL (accountTransactionsReportItem reportq thisacctq signfn) bal
|
||||||
Nothing -> is
|
|
||||||
|
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
|
-- 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
|
-- 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
|
-- 201606: we now calculate change and balance from filtered postings, check this still works well for all callers XXX
|
||||||
where
|
where
|
||||||
tfiltered@Transaction{tpostings=reportps} = filterTransactionPostings reportq torig
|
tfiltered@Transaction{tpostings=reportps} = filterTransactionPostings reportq torig
|
||||||
tacct = tfiltered{tdate=transactionRegisterDate reportq thisacctq tfiltered}
|
tacct = tfiltered{tdate=transactionRegisterDate reportq thisacctq tfiltered}
|
||||||
(i,bal') = case reportps of
|
balItem = case reportps of
|
||||||
[] -> (Nothing,bal) -- no matched postings in this transaction, skip it
|
[] -> (bal, Nothing) -- no matched postings in this transaction, skip it
|
||||||
_ -> (Just (torig, tacct, numotheraccts > 1, otheracctstr, a, b), b)
|
_ -> (b, Just (torig, tacct, numotheraccts > 1, otheracctstr, a, b))
|
||||||
where
|
where
|
||||||
(thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps
|
(thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps
|
||||||
numotheraccts = length $ nub $ map paccount otheracctps
|
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)
|
| otherwise = summarisePostingAccounts otheracctps -- summarise matched postings to other account(s)
|
||||||
a = signfn $ negate $ sum $ map pamount thisacctps
|
a = signfn $ negate $ sum $ map pamount thisacctps
|
||||||
b = bal + a
|
b = bal + a
|
||||||
is = accountTransactionsReportItems reportq thisacctq bal' signfn ts
|
|
||||||
|
|
||||||
-- | What is the transaction's date in the context of a particular account
|
-- | 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 ?
|
-- (specified with a query) and report query, as in an account register ?
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user