2007-02-16 12:00:17 +03:00
|
|
|
|
|
|
|
module Transaction
|
|
|
|
where
|
|
|
|
import Utils
|
|
|
|
import BasicTypes
|
2007-02-18 21:12:02 +03:00
|
|
|
import AccountName
|
2007-03-12 10:58:11 +03:00
|
|
|
import Amount
|
2007-02-16 12:00:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
data Transaction = Transaction {
|
|
|
|
taccount :: AccountName,
|
|
|
|
tamount :: Amount
|
2007-03-12 20:53:39 +03:00
|
|
|
} deriving (Eq)
|
2007-02-16 12:00:17 +03:00
|
|
|
|
|
|
|
instance Show Transaction where show = showTransaction
|
|
|
|
|
|
|
|
showTransaction t = (showAccountName $ taccount t) ++ " " ++ (showAmount $ tamount t)
|
|
|
|
showAmount amt = printf "%11s" (show amt)
|
|
|
|
showAccountName s = printf "%-22s" (elideRight 22 s)
|
|
|
|
|
|
|
|
elideRight width s =
|
|
|
|
case length s > width of
|
|
|
|
True -> take (width - 2) s ++ ".."
|
|
|
|
False -> s
|
|
|
|
|
|
|
|
autofillTransactions :: [Transaction] -> [Transaction]
|
|
|
|
autofillTransactions ts =
|
|
|
|
let (ns, as) = partition isNormal ts
|
2007-03-12 20:53:39 +03:00
|
|
|
where isNormal t = (symbol $ currency $ tamount t) /= "AUTO" in
|
2007-02-16 12:00:17 +03:00
|
|
|
case (length as) of
|
|
|
|
0 -> ns
|
|
|
|
1 -> ns ++ [balanceTransaction $ head as]
|
|
|
|
where balanceTransaction t = t{tamount = -(sumTransactions ns)}
|
|
|
|
otherwise -> error "too many blank transactions in this entry"
|
|
|
|
|
|
|
|
sumTransactions :: [Transaction] -> Amount
|
|
|
|
sumTransactions ts = sum [tamount t | t <- ts]
|
|
|
|
|