83 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Haskell
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Haskell
		
	
	
	
	
	
| 
 | |
| module EntryTransaction
 | |
| where
 | |
| 
 | |
| import Debug.Trace
 | |
| import Text.Printf
 | |
| import Text.Regex
 | |
| import Data.List
 | |
| 
 | |
| import Utils
 | |
| import BasicTypes
 | |
| import Account
 | |
| import Entry
 | |
| import Transaction
 | |
| 
 | |
| 
 | |
| -- We parse Entries containing Transactions and flatten them into
 | |
| -- (entry,transaction) pairs (entrytransactions, hereafter referred to as
 | |
| -- "transactions") for easier processing. (So far, these types have
 | |
| -- morphed through E->T; (T,E); ET; E<->T; (E,T)).
 | |
| 
 | |
| type EntryTransaction = (Entry,Transaction)
 | |
| 
 | |
| entry       (e,t) = e
 | |
| transaction (e,t) = t
 | |
| date        (e,t) = edate e
 | |
| status      (e,t) = estatus e
 | |
| code        (e,t) = ecode e
 | |
| description (e,t) = edescription e
 | |
| account     (e,t) = taccount t
 | |
| amount      (e,t) = tamount t
 | |
|                                          
 | |
| flattenEntry :: Entry -> [EntryTransaction]
 | |
| flattenEntry e = [(e,t) | t <- etransactions e]
 | |
| 
 | |
| entryTransactionsFrom :: [Entry] -> [EntryTransaction]
 | |
| entryTransactionsFrom es = concat $ map flattenEntry es
 | |
| 
 | |
| sumEntryTransactions :: [EntryTransaction] -> Amount
 | |
| sumEntryTransactions ets = 
 | |
|     sumTransactions $ map transaction ets
 | |
| 
 | |
| accountNamesFromTransactions :: [EntryTransaction] -> [AccountName]
 | |
| accountNamesFromTransactions ts = nub $ map account ts
 | |
| 
 | |
| matchTransactionAccount :: String -> EntryTransaction -> Bool
 | |
| matchTransactionAccount s t =
 | |
|     case matchRegex (mkRegex s) (account t) of
 | |
|       Nothing -> False
 | |
|       otherwise -> True
 | |
| 
 | |
| matchTransactionDescription :: String -> EntryTransaction -> Bool
 | |
| matchTransactionDescription s t =
 | |
|     case matchRegex (mkRegex s) (description t) of
 | |
|       Nothing -> False
 | |
|       otherwise -> True
 | |
| 
 | |
| showTransactionsWithBalances :: [EntryTransaction] -> Amount -> String
 | |
| showTransactionsWithBalances [] _ = []
 | |
| showTransactionsWithBalances ts b =
 | |
|     unlines $ showTransactionsWithBalances' ts dummyt b
 | |
|         where
 | |
|           dummyt = (Entry "" False "" "" [], Transaction "" (Amount "" 0))
 | |
|           showTransactionsWithBalances' [] _ _ = []
 | |
|           showTransactionsWithBalances' (t:ts) tprev b =
 | |
|               (if (entry t /= (entry tprev))
 | |
|                then [showTransactionDescriptionAndBalance t b']
 | |
|                else [showTransactionAndBalance t b'])
 | |
|               ++ (showTransactionsWithBalances' ts t b')
 | |
|                   where b' = b + (amount t)
 | |
| 
 | |
| showTransactionDescriptionAndBalance :: EntryTransaction -> Amount -> String
 | |
| showTransactionDescriptionAndBalance t b =
 | |
|     (showEntry $ entry t) ++ (showTransaction $ transaction t) ++ (showBalance b)
 | |
| 
 | |
| showTransactionAndBalance :: EntryTransaction -> Amount -> String
 | |
| showTransactionAndBalance t b =
 | |
|     (replicate 32 ' ') ++ (showTransaction $ transaction t) ++ (showBalance b)
 | |
| 
 | |
| showBalance :: Amount -> String
 | |
| showBalance b = printf " %12s" (amountRoundedOrZero b)
 | |
| 
 |