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

View File

@ -43,7 +43,7 @@ ledgerFromJournal :: Query -> Journal -> Ledger
ledgerFromJournal q j = nullledger{ljournal=j'', laccounts=as} ledgerFromJournal q j = nullledger{ljournal=j'', laccounts=as}
where where
(q',depthq) = (filterQuery (not . queryIsDepth) q, filterQuery queryIsDepth q) (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 filterJournalPostings q' j
as = accountsFromPostings $ journalPostings j' as = accountsFromPostings $ journalPostings j'
j'' = filterJournalPostings depthq j' j'' = filterJournalPostings depthq j'

View File

@ -85,7 +85,7 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totals)
ps :: [Posting] = ps :: [Posting] =
dbg "ps" $ dbg "ps" $
journalPostings $ 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 filterJournalPostings reportq $ -- remove postings not matched by (adjusted) query
journalSelectingAmountFromOpts opts j journalSelectingAmountFromOpts opts j