2007-02-16 12:00:17 +03:00
|
|
|
module Transaction
|
|
|
|
where
|
|
|
|
import Utils
|
2007-07-02 18:54:36 +04:00
|
|
|
import Types
|
2007-02-18 21:12:02 +03:00
|
|
|
import AccountName
|
2007-07-04 13:51:37 +04:00
|
|
|
import LedgerEntry
|
|
|
|
import LedgerTransaction
|
2007-03-12 10:58:11 +03:00
|
|
|
import Amount
|
2007-07-04 13:51:37 +04:00
|
|
|
import Currency
|
|
|
|
|
|
|
|
|
2007-07-07 22:26:28 +04:00
|
|
|
instance Show Transaction where
|
|
|
|
show (Transaction eno d desc a amt) =
|
|
|
|
unwords [d,desc,a,show amt]
|
|
|
|
|
2007-07-04 16:40:26 +04:00
|
|
|
-- we use the entry number e to remember the grouping of txns
|
|
|
|
flattenEntry :: (LedgerEntry, Int) -> [Transaction]
|
|
|
|
flattenEntry (LedgerEntry d _ _ desc _ ts, e) =
|
|
|
|
[Transaction e d desc (taccount t) (tamount t) | t <- ts]
|
2007-07-04 13:51:37 +04:00
|
|
|
|
2007-07-04 14:59:29 +04:00
|
|
|
transactionSetPrecision :: Int -> Transaction -> Transaction
|
2007-07-04 16:40:26 +04:00
|
|
|
transactionSetPrecision p (Transaction e d desc a amt) = Transaction e d desc a amt{precision=p}
|
2007-07-04 13:51:37 +04:00
|
|
|
|
|
|
|
accountNamesFromTransactions :: [Transaction] -> [AccountName]
|
|
|
|
accountNamesFromTransactions ts = nub $ map account ts
|
|
|
|
|
2007-07-04 14:59:29 +04:00
|
|
|
sumTransactions :: [Transaction] -> Amount
|
|
|
|
sumTransactions = sum . map amount
|
2007-07-04 13:51:37 +04:00
|
|
|
|
|
|
|
matchTransactionAccount :: Regex -> Transaction -> Bool
|
|
|
|
matchTransactionAccount r t =
|
|
|
|
case matchRegex r (account t) of
|
|
|
|
Nothing -> False
|
|
|
|
otherwise -> True
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
matchTransactionDescription :: Regex -> Transaction -> Bool
|
|
|
|
matchTransactionDescription r t =
|
|
|
|
case matchRegex r (description t) of
|
|
|
|
Nothing -> False
|
|
|
|
otherwise -> True
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
-- for register command
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
showTransactionsWithBalances :: [Transaction] -> Amount -> String
|
|
|
|
showTransactionsWithBalances [] _ = []
|
|
|
|
showTransactionsWithBalances ts b =
|
|
|
|
unlines $ showTransactionsWithBalances' ts dummyt b
|
|
|
|
where
|
2007-07-04 16:40:26 +04:00
|
|
|
dummyt = Transaction 0 "" "" "" (dollars 0)
|
2007-07-04 13:51:37 +04:00
|
|
|
showTransactionsWithBalances' [] _ _ = []
|
|
|
|
showTransactionsWithBalances' (t:ts) tprev b =
|
2007-07-04 14:59:29 +04:00
|
|
|
(if sameentry t tprev
|
2007-07-04 16:40:26 +04:00
|
|
|
then [showTransactionAndBalance t b']
|
|
|
|
else [showTransactionDescriptionAndBalance t b'])
|
2007-07-04 13:51:37 +04:00
|
|
|
++ (showTransactionsWithBalances' ts t b')
|
2007-07-04 14:59:29 +04:00
|
|
|
where
|
|
|
|
b' = b + (amount t)
|
2007-07-04 16:40:26 +04:00
|
|
|
sameentry (Transaction e1 _ _ _ _) (Transaction e2 _ _ _ _) = e1 == e2
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
showTransactionDescriptionAndBalance :: Transaction -> Amount -> String
|
|
|
|
showTransactionDescriptionAndBalance t b =
|
2007-07-04 16:05:54 +04:00
|
|
|
(showEntryDescription $ LedgerEntry (date t) False "" (description t) "" [])
|
|
|
|
++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t) "") ++ (showBalance b)
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
showTransactionAndBalance :: Transaction -> Amount -> String
|
|
|
|
showTransactionAndBalance t b =
|
2007-07-04 16:05:54 +04:00
|
|
|
(replicate 32 ' ') ++ (showLedgerTransaction $ LedgerTransaction (account t) (amount t) "") ++ (showBalance b)
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
showBalance :: Amount -> String
|
|
|
|
showBalance b = printf " %12s" (showAmountRoundedOrZero b)
|
2007-02-16 12:00:17 +03:00
|
|
|
|
2007-07-04 13:51:37 +04:00
|
|
|
transactionsWithAccountName :: AccountName -> [Transaction] -> [Transaction]
|
|
|
|
transactionsWithAccountName a ts = [t | t <- ts, account t == a]
|
|
|
|
|
|
|
|
transactionsWithOrBelowAccountName :: AccountName -> [Transaction] -> [Transaction]
|
|
|
|
transactionsWithOrBelowAccountName a ts =
|
|
|
|
[t | t <- ts, account t == a || a `isAccountNamePrefixOf` (account t)]
|