2008-10-03 06:04:15 +04:00
|
|
|
{-|
|
|
|
|
|
|
|
|
A 'RawLedger' is a parsed ledger file. We call it raw to distinguish from
|
|
|
|
the cached 'Ledger'.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2008-10-03 04:40:06 +04:00
|
|
|
module Ledger.RawLedger
|
2007-07-02 23:15:39 +04:00
|
|
|
where
|
2008-10-15 04:34:02 +04:00
|
|
|
import qualified Data.Map as Map
|
2008-10-03 04:05:16 +04:00
|
|
|
import Ledger.Utils
|
2008-10-03 04:12:59 +04:00
|
|
|
import Ledger.Types
|
2008-10-03 04:40:06 +04:00
|
|
|
import Ledger.AccountName
|
2008-10-18 12:39:08 +04:00
|
|
|
import Ledger.Amount
|
2008-10-03 06:37:19 +04:00
|
|
|
import Ledger.Entry
|
2008-10-03 16:10:05 +04:00
|
|
|
import Ledger.Transaction
|
2007-07-02 23:15:39 +04:00
|
|
|
|
|
|
|
|
2008-10-15 21:04:47 +04:00
|
|
|
negativepatternchar = '-'
|
|
|
|
|
2008-10-03 02:17:04 +04:00
|
|
|
instance Show RawLedger where
|
2008-10-09 13:25:58 +04:00
|
|
|
show l = printf "RawLedger with %d entries, %d accounts: %s"
|
2007-07-02 23:15:39 +04:00
|
|
|
((length $ entries l) +
|
|
|
|
(length $ modifier_entries l) +
|
|
|
|
(length $ periodic_entries l))
|
2008-10-09 13:25:58 +04:00
|
|
|
(length accounts)
|
|
|
|
(show accounts)
|
2008-10-16 01:10:59 +04:00
|
|
|
-- ++ (show $ rawLedgerTransactions l)
|
2008-10-09 13:25:58 +04:00
|
|
|
where accounts = flatten $ rawLedgerAccountNameTree l
|
2008-10-03 16:10:05 +04:00
|
|
|
|
|
|
|
rawLedgerTransactions :: RawLedger -> [Transaction]
|
2008-10-18 04:52:49 +04:00
|
|
|
rawLedgerTransactions = txnsof . entries
|
|
|
|
where txnsof es = concat $ map flattenEntry $ zip es [1..]
|
2008-10-03 16:10:05 +04:00
|
|
|
|
|
|
|
rawLedgerAccountNamesUsed :: RawLedger -> [AccountName]
|
|
|
|
rawLedgerAccountNamesUsed = accountNamesFromTransactions . rawLedgerTransactions
|
|
|
|
|
|
|
|
rawLedgerAccountNames :: RawLedger -> [AccountName]
|
|
|
|
rawLedgerAccountNames = sort . expandAccountNames . rawLedgerAccountNamesUsed
|
|
|
|
|
|
|
|
rawLedgerAccountNameTree :: RawLedger -> Tree AccountName
|
|
|
|
rawLedgerAccountNameTree l = accountNameTreeFrom $ rawLedgerAccountNames l
|
2008-10-10 08:23:25 +04:00
|
|
|
|
|
|
|
-- | Remove ledger entries we are not interested in.
|
|
|
|
-- Keep only those which fall between the begin and end dates, and match
|
2008-10-16 13:04:44 +04:00
|
|
|
-- the description pattern, and match the cleared flag.
|
|
|
|
filterRawLedger :: String -> String -> [String] -> Bool -> RawLedger -> RawLedger
|
|
|
|
filterRawLedger begin end pats clearedonly =
|
|
|
|
filterRawLedgerEntriesByClearedStatus clearedonly .
|
2008-10-10 08:23:25 +04:00
|
|
|
filterRawLedgerEntriesByDate begin end .
|
2008-10-15 21:04:47 +04:00
|
|
|
filterRawLedgerEntriesByDescription pats
|
2008-10-10 08:23:25 +04:00
|
|
|
|
|
|
|
-- | Keep only entries whose description matches the description pattern.
|
2008-10-15 21:04:47 +04:00
|
|
|
filterRawLedgerEntriesByDescription :: [String] -> RawLedger -> RawLedger
|
|
|
|
filterRawLedgerEntriesByDescription pats (RawLedger ms ps es f) =
|
2008-10-10 08:23:25 +04:00
|
|
|
RawLedger ms ps (filter matchdesc es) f
|
2008-10-18 04:52:49 +04:00
|
|
|
where matchdesc = matchLedgerPatterns False pats . edescription
|
2008-10-10 08:23:25 +04:00
|
|
|
|
|
|
|
-- | Keep only entries which fall between begin and end dates.
|
|
|
|
-- We include entries on the begin date and exclude entries on the end
|
|
|
|
-- date, like ledger. An empty date string means no restriction.
|
|
|
|
filterRawLedgerEntriesByDate :: String -> String -> RawLedger -> RawLedger
|
|
|
|
filterRawLedgerEntriesByDate begin end (RawLedger ms ps es f) =
|
|
|
|
RawLedger ms ps (filter matchdate es) f
|
2008-10-18 04:52:49 +04:00
|
|
|
where
|
|
|
|
d1 = parsedate begin :: UTCTime
|
|
|
|
d2 = parsedate end
|
|
|
|
matchdate e = (null begin || d >= d1) && (null end || d < d2)
|
|
|
|
where d = parsedate $ edate e
|
2008-10-10 08:23:25 +04:00
|
|
|
|
2008-10-16 13:04:44 +04:00
|
|
|
-- | Keep only entries with cleared status, if the flag is true, otherwise
|
|
|
|
-- do no filtering.
|
|
|
|
filterRawLedgerEntriesByClearedStatus :: Bool -> RawLedger -> RawLedger
|
|
|
|
filterRawLedgerEntriesByClearedStatus False l = l
|
|
|
|
filterRawLedgerEntriesByClearedStatus True (RawLedger ms ps es f) =
|
|
|
|
RawLedger ms ps (filter estatus es) f
|
2008-10-15 04:34:02 +04:00
|
|
|
|
2008-10-15 21:04:47 +04:00
|
|
|
-- | Check if a set of ledger account/description patterns matches the
|
|
|
|
-- given account name or entry description, applying ledger's special
|
|
|
|
-- cases.
|
|
|
|
--
|
2008-10-16 02:18:20 +04:00
|
|
|
-- Patterns are case-insensitive regular expression strings, and those
|
|
|
|
-- beginning with - are negative patterns. The special case is that
|
|
|
|
-- account patterns match the full account name except in balance reports
|
|
|
|
-- when the pattern does not contain : and is a positive pattern, where it
|
|
|
|
-- matches only the leaf name.
|
2008-10-15 21:04:47 +04:00
|
|
|
matchLedgerPatterns :: Bool -> [String] -> String -> Bool
|
|
|
|
matchLedgerPatterns forbalancereport pats str =
|
2008-10-18 04:52:49 +04:00
|
|
|
(null positives || any ismatch positives) && (null negatives || not (any ismatch negatives))
|
2008-10-15 21:04:47 +04:00
|
|
|
where
|
|
|
|
isnegative = (== negativepatternchar) . head
|
|
|
|
(negatives,positives) = partition isnegative pats
|
2008-10-16 02:18:20 +04:00
|
|
|
ismatch pat = containsRegex (mkRegexWithOpts pat' True True) matchee
|
2008-10-15 21:04:47 +04:00
|
|
|
where
|
|
|
|
pat' = if isnegative pat then drop 1 pat else pat
|
2008-10-18 04:52:49 +04:00
|
|
|
matchee = if forbalancereport && not (':' `elem` pat) && not (isnegative pat)
|
2008-10-15 21:04:47 +04:00
|
|
|
then accountLeafName str
|
|
|
|
else str
|
|
|
|
|
2008-10-15 04:34:02 +04:00
|
|
|
-- | Give amounts the display settings of the first one detected in each commodity.
|
|
|
|
normaliseRawLedgerAmounts :: RawLedger -> RawLedger
|
|
|
|
normaliseRawLedgerAmounts l@(RawLedger ms ps es f) = RawLedger ms ps es' f
|
|
|
|
where
|
|
|
|
es' = map normaliseEntryAmounts es
|
|
|
|
normaliseEntryAmounts (Entry d s c desc comm ts pre) = Entry d s c desc comm ts' pre
|
|
|
|
where ts' = map normaliseRawTransactionAmounts ts
|
2008-10-16 10:00:46 +04:00
|
|
|
normaliseRawTransactionAmounts (RawTransaction acct a c t) = RawTransaction acct a' c t
|
2008-10-18 12:39:08 +04:00
|
|
|
where a' = normaliseMixedAmount a
|
2008-10-15 23:28:36 +04:00
|
|
|
firstcommodities = nubBy samesymbol $ allcommodities
|
2008-10-18 14:38:01 +04:00
|
|
|
allcommodities = map commodity $ concat $ map (amounts . amount) $ rawLedgerTransactions l
|
2008-10-15 23:28:36 +04:00
|
|
|
samesymbol (Commodity {symbol=s1}) (Commodity {symbol=s2}) = s1==s2
|
2008-10-15 04:34:02 +04:00
|
|
|
firstoccurrenceof c@(Commodity {symbol=s}) =
|
|
|
|
fromMaybe
|
|
|
|
(error "failed to normalise commodity") -- shouldn't happen
|
|
|
|
(find (\(Commodity {symbol=sym}) -> sym==s) firstcommodities)
|