hledger/LedgerTransaction.hs

37 lines
1.2 KiB
Haskell
Raw Normal View History

module LedgerTransaction
where
import Utils
import Types
import AccountName
import Amount
2007-07-04 14:59:29 +04:00
instance Show LedgerTransaction where show = showLedgerTransaction
2007-07-04 14:59:29 +04:00
showLedgerTransaction :: LedgerTransaction -> String
showLedgerTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ tamount t)
where
showaccountname = printf "%-22s" . elideRight 22
showamount = printf "%11s" . showAmountRoundedOrZero
elideRight width s =
case length s > width of
True -> take (width - 2) s ++ ".."
False -> s
autofillTransactions :: [LedgerTransaction] -> [LedgerTransaction]
autofillTransactions ts =
let (ns, as) = partition isNormal ts
where isNormal t = (symbol $ currency $ tamount t) /= "AUTO" in
case (length as) of
0 -> ns
1 -> ns ++ [balanceTransaction $ head as]
2007-07-04 14:59:29 +04:00
where balanceTransaction t = t{tamount = -(sumLedgerTransactions ns)}
otherwise -> error "too many blank transactions in this entry"
2007-07-04 14:59:29 +04:00
sumLedgerTransactions :: [LedgerTransaction] -> Amount
sumLedgerTransactions = sum . map tamount
2007-07-04 14:59:29 +04:00
ledgerTransactionSetPrecision :: Int -> LedgerTransaction -> LedgerTransaction
ledgerTransactionSetPrecision p (LedgerTransaction a amt) = LedgerTransaction a amt{precision=p}