filter transactions by account substring

This commit is contained in:
Simon Michael 2007-02-12 00:41:50 +00:00
parent 0b5cb44b43
commit 960187f531
3 changed files with 48 additions and 25 deletions

View File

@ -92,11 +92,13 @@ entryLines e =
[firstline] ++ otherlines [firstline] ++ otherlines
where where
t:ts = transactions e t:ts = transactions e
entrydesc = printf "%-10s %-20s " (date e) (take 20 $ description e) firstline = (entrydesc e ++ (show t), amount t)
firstline = (entrydesc ++ (show t), amount t)
otherlines = map (\t -> (prependSpace $ show t, amount t)) ts otherlines = map (\t -> (prependSpace $ show t, amount t)) ts
prependSpace = (replicate 32 ' ' ++) prependSpace = (replicate 32 ' ' ++)
entrydesc e = printf "%-10s %-20s " (date e) (take 20 $ description e)
instance Show Transaction where instance Show Transaction where
show t = printf "%-25s %10s" (take 25 $ account t) (show $ amount t) show t = printf "%-25s %10s" (take 25 $ account t) (show $ amount t)
@ -128,7 +130,9 @@ entryLinesWithBalances ((str,amt):els) bal =
[(str',amt,bal')] ++ entryLinesWithBalances els bal' [(str',amt,bal')] ++ entryLinesWithBalances els bal'
where where
bal' = bal + amt bal' = bal + amt
str' = str ++ (printf " %10.2s" (show bal')) str' = str ++ (showBalance bal')
showBalance b = printf " %10.2s" (show b)
-- misc -- misc
@ -151,12 +155,50 @@ normalAndAutoTransactions ts =
partition isNormal ts partition isNormal ts
where isNormal t = (currency $ amount t) /= "AUTO" where isNormal t = (currency $ amount t) /= "AUTO"
-- transactions
sumTransactions :: [Transaction] -> Amount sumTransactions :: [Transaction] -> Amount
sumTransactions ts = sum [amount t | t <- ts] sumTransactions ts = sum [amount t | t <- ts]
transactionsFromEntries :: [Entry] -> [Transaction] transactionsFromEntries :: [Entry] -> [Transaction]
transactionsFromEntries es = concat $ map transactions es transactionsFromEntries es = concat $ map transactions es
matchTransactionAccount :: String -> Transaction -> Bool
matchTransactionAccount s t = s `isInfixOf` (account t)
transactionsWithEntries :: [Entry] -> [(Transaction,Entry)]
transactionsWithEntries es = [(t,e) | e <- es, t <- transactions e]
showTransactionsWithBalances :: [(Transaction,Entry)] -> Amount -> String
showTransactionsWithBalances [] _ = []
showTransactionsWithBalances tes b =
unlines $ showTransactionsWithBalances' tes b
where
showTransactionsWithBalances' [] _ = []
showTransactionsWithBalances' ((t,e):rest) b =
[showTransactionWithBalance t e b'] ++ (showTransactionsWithBalances' rest b')
where b' = b + (amount t)
showTransactionWithBalance :: Transaction -> Entry -> Amount -> String
showTransactionWithBalance t e b =
(entrydesc e) ++ (show t) ++ (showBalance b)
transactionsMatching :: String -> Ledger -> [(Transaction,Entry)]
transactionsMatching s l = filter (\(t,e) -> matchTransactionAccount s t) (transactionsWithEntries $ entries l)
-- entries
entriesMatching :: String -> Ledger -> [Entry]
entriesMatching s l = filterEntriesByAccount s (entries l)
filterEntriesByAccount :: String -> [Entry] -> [Entry]
filterEntriesByAccount s es = filter (matchEntryAccount s) es
matchEntryAccount :: String -> Entry -> Bool
matchEntryAccount s e = any (matchTransactionAccount s) (transactions e)
-- accounts
accountsFromTransactions :: [Transaction] -> [Account] accountsFromTransactions :: [Transaction] -> [Account]
accountsFromTransactions ts = nub $ map account ts accountsFromTransactions ts = nub $ map account ts
@ -180,14 +222,3 @@ splitAtElement e l =
accountTree :: Ledger -> [Account] accountTree :: Ledger -> [Account]
accountTree = sort . expandAccounts . accountsUsed accountTree = sort . expandAccounts . accountsUsed
entriesMatching :: String -> Ledger -> [Entry]
entriesMatching s l = filterEntriesByAccount s (entries l)
filterEntriesByAccount :: String -> [Entry] -> [Entry]
filterEntriesByAccount s es = filter (matchEntryAccount s) es
matchEntryAccount :: String -> Entry -> Bool
matchEntryAccount s e = any (matchTransactionAccount s) (transactions e)
matchTransactionAccount :: String -> Transaction -> Bool
matchTransactionAccount s t = s `isInfixOf` (account t)

12
TODO
View File

@ -1,16 +1,8 @@
features features
register register
account matching account matching
match transactions, not entries don't show duplicate transaction descriptions
better transaction/entry data structure
$ ledger reg equi
2007/01/01 opening balance equity:opening balan.. $-4.82 $-4.82
2007/01/25 balance adjustment equity $91.15 $86.33
$ hledger reg equi
2007/01/01 opening balance assets:cash $4.82 $4.82
equity:opening balances $-4.82 0
2007/01/25 balance adjustment equity $91.15 $91.15
assets:cash $-91.15 0
description matching description matching
regexp matching regexp matching

View File

@ -61,4 +61,4 @@ doWithParsed a p =
printRegister :: [String] -> Ledger -> IO () printRegister :: [String] -> Ledger -> IO ()
printRegister args ledger = printRegister args ledger =
putStr $ showEntriesWithBalances (entriesMatching (head (args ++ [""])) ledger) 0 putStr $ showTransactionsWithBalances (transactionsMatching (head (args ++ [""])) ledger) 0