refactor amount filtering helpers

This commit is contained in:
Simon Michael 2014-05-23 13:10:36 -07:00
parent 3ff7fe7f1b
commit b22cb66319
3 changed files with 16 additions and 12 deletions

View File

@ -22,7 +22,8 @@ module Hledger.Data.Journal (
-- * Filtering
filterJournalTransactions,
filterJournalPostings,
filterJournalPostingAmounts,
filterJournalAmounts,
filterTransactionAmounts,
filterPostingAmount,
-- * Querying
journalAccountNames,
@ -231,6 +232,10 @@ journalCashAccountQuery j = And [journalAssetAccountQuery j, Not $ Acct "(receiv
-------------------------------------------------------------------------------
-- filtering V2
-- | Keep only transactions matching the query expression.
filterJournalTransactions :: Query -> Journal -> Journal
filterJournalTransactions q j@Journal{jtxns=ts} = j{jtxns=filter (q `matchesTransaction`) ts}
-- | Keep only postings matching the query expression.
-- This can leave unbalanced transactions.
filterJournalPostings :: Query -> Journal -> Journal
@ -238,21 +243,20 @@ filterJournalPostings q j@Journal{jtxns=ts} = j{jtxns=map filtertransactionposti
where
filtertransactionpostings t@Transaction{tpostings=ps} = t{tpostings=filter (q `matchesPosting`) ps}
-- Within each posting's amount, keep only the parts matching the query.
-- | Within each posting's amount, keep only the parts matching the query.
-- This can leave unbalanced transactions.
filterJournalPostingAmounts :: Query -> Journal -> Journal
filterJournalPostingAmounts q j@Journal{jtxns=ts} = j{jtxns=map filtertransactionpostings ts}
where
filtertransactionpostings t@Transaction{tpostings=ps} = t{tpostings=map (filterPostingAmount q) ps}
filterJournalAmounts :: Query -> Journal -> Journal
filterJournalAmounts q j@Journal{jtxns=ts} = j{jtxns=map (filterTransactionAmounts q) ts}
-- | Filter out all parts of this transaction's amounts which do not match the query.
-- This can leave the transaction unbalanced.
filterTransactionAmounts :: Query -> Transaction -> Transaction
filterTransactionAmounts q t@Transaction{tpostings=ps} = t{tpostings=map (filterPostingAmount q) ps}
-- | Filter out all parts of this posting's amount which do not match the query.
filterPostingAmount :: Query -> Posting -> Posting
filterPostingAmount q p@Posting{pamount=Mixed as} = p{pamount=Mixed $ filter (q `matchesAmount`) as}
-- | Keep only transactions matching the query expression.
filterJournalTransactions :: Query -> Journal -> Journal
filterJournalTransactions q j@Journal{jtxns=ts} = j{jtxns=filter (q `matchesTransaction`) ts}
{-
-------------------------------------------------------------------------------
-- filtering V1

View File

@ -43,7 +43,7 @@ ledgerFromJournal :: Query -> Journal -> Ledger
ledgerFromJournal q j = nullledger{ljournal=j'', laccounts=as}
where
(q',depthq) = (filterQuery (not . queryIsDepth) q, filterQuery queryIsDepth q)
j' = filterJournalPostingAmounts (filterQuery queryIsSym q) $ -- remove amount parts which the query's sym: terms would exclude
j' = filterJournalAmounts (filterQuery queryIsSym q) $ -- remove amount parts which the query's sym: terms would exclude
filterJournalPostings q' j
as = accountsFromPostings $ journalPostings j'
j'' = filterJournalPostings depthq j'

View File

@ -85,7 +85,7 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totals)
ps :: [Posting] =
dbg "ps" $
journalPostings $
filterJournalPostingAmounts symq $ -- remove amount parts excluded by cur:
filterJournalAmounts symq $ -- remove amount parts excluded by cur:
filterJournalPostings reportq $ -- remove postings not matched by (adjusted) query
journalSelectingAmountFromOpts opts j