From 622db5f25dfe6b8fa5d7e3eb048b187854231a4e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 4 Apr 2009 11:19:15 +0000 Subject: [PATCH] datespan utils --- Ledger/Ledger.hs | 5 +++-- Ledger/RawLedger.hs | 9 +++++++++ Ledger/Transaction.hs | 9 +++++++++ RegisterCommand.hs | 10 +--------- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index 2a438a360..cb11eaa49 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -132,8 +132,9 @@ ledgerAccountTree depth l = treemap (ledgerAccount l) $ treeprune depth $ accoun ledgerAccountTreeAt :: Ledger -> Account -> Maybe (Tree Account) ledgerAccountTreeAt l acct = subtreeat acct $ ledgerAccountTree 9999 l --- | The (explicit) date span containing all the ledger's transactions, --- or DateSpan Nothing Nothing if there are no transactions. +-- | The date span containing all the ledger's (filtered) transactions, +-- or DateSpan Nothing Nothing if there are none. +ledgerDateSpan :: Ledger -> DateSpan ledgerDateSpan l | null ts = DateSpan Nothing Nothing | otherwise = DateSpan (Just $ date $ head ts) (Just $ addDays 1 $ date $ last ts) diff --git a/Ledger/RawLedger.hs b/Ledger/RawLedger.hs index 97bba9a16..018d10852 100644 --- a/Ledger/RawLedger.hs +++ b/Ledger/RawLedger.hs @@ -163,3 +163,12 @@ rawLedgerConvertTimeLog t l0 = l0 { ledger_txns = convertedTimeLog ++ ledger_txn } where convertedTimeLog = entriesFromTimeLogEntries t $ open_timelog_entries l0 + +-- | The date span containing all the raw ledger's transactions, +-- or DateSpan Nothing Nothing if there are none. +rawLedgerDateSpan :: RawLedger -> DateSpan +rawLedgerDateSpan rl + | null ts = DateSpan Nothing Nothing + | otherwise = DateSpan (Just $ ltdate $ head ts) (Just $ addDays 1 $ ltdate $ last ts) + where + ts = sortBy (comparing ltdate) $ ledger_txns rl diff --git a/Ledger/Transaction.hs b/Ledger/Transaction.hs index 33672e1b2..43b888f47 100644 --- a/Ledger/Transaction.hs +++ b/Ledger/Transaction.hs @@ -9,6 +9,7 @@ ingrained. module Ledger.Transaction where +import Ledger.Dates import Ledger.Utils import Ledger.Types import Ledger.Dates @@ -39,3 +40,11 @@ sumTransactions = sum . map amount nulltxn :: Transaction nulltxn = Transaction 0 False (parsedate "1900/1/1") "" "" nullmixedamt RegularPosting + +-- | Does the given transaction fall within the given date span ? +isTransactionInDateSpan :: DateSpan -> Transaction -> Bool +isTransactionInDateSpan (DateSpan Nothing Nothing) _ = True +isTransactionInDateSpan (DateSpan Nothing (Just e)) (Transaction{date=d}) = d=b +isTransactionInDateSpan (DateSpan (Just b) (Just e)) (Transaction{date=d}) = d>=b && d compare (date a) (date b)) + ts = sortBy (comparing date) $ filterempties $ filter matchapats $ filterdepth $ ledgerTransactions l filterdepth | interval == NoInterval = filter (\t -> (accountNameLevel $ account t) <= depth) | otherwise = id filterempties @@ -97,13 +96,6 @@ clipAccountNames :: Int -> [AccountName] -> [AccountName] clipAccountNames d as = nub $ map (clip d) as where clip d = accountNameFromComponents . take d . accountNameComponents --- | Does the given transaction fall within the given date span ? -isTransactionInDateSpan :: DateSpan -> Transaction -> Bool -isTransactionInDateSpan (DateSpan Nothing Nothing) _ = True -isTransactionInDateSpan (DateSpan Nothing (Just e)) (Transaction{date=d}) = d=b -isTransactionInDateSpan (DateSpan (Just b) (Just e)) (Transaction{date=d}) = d>=b && d