hledger/Ledger/RawTransaction.hs

39 lines
1.2 KiB
Haskell
Raw Normal View History

{-|
A 'RawTransaction' represents a single transaction line within a ledger
entry. We call it raw to distinguish from the cached 'Transaction'.
-}
module Ledger.RawTransaction
where
import Ledger.Utils
2008-10-03 04:12:59 +04:00
import Ledger.Types
import Ledger.Amount
2008-10-15 23:14:34 +04:00
import Ledger.AccountName
instance Show RawTransaction where show = showRawTransaction
showRawTransaction :: RawTransaction -> String
showRawTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ tamount t)
where
2008-10-15 23:14:34 +04:00
showaccountname = printf "%-22s" . elideAccountName 22
2008-10-15 04:37:38 +04:00
showamount = printf "%12s" . showAmountOrZero
-- | Fill in the missing balance in an entry's transactions. There can be
-- at most one missing balance, otherwise we'll return Nothing.
autofillTransactions :: [RawTransaction] -> Maybe [RawTransaction]
autofillTransactions ts =
case (length blanks) of
0 -> Just ts
1 -> Just $ map balance ts
otherwise -> Nothing
where
(normals, blanks) = partition isnormal ts
isnormal t = (symbol $ commodity $ tamount t) /= "AUTO"
2007-07-11 12:15:58 +04:00
balance t = if isnormal t then t else t{tamount = -(sumLedgerTransactions normals)}
sumLedgerTransactions :: [RawTransaction] -> Amount
sumLedgerTransactions = sumAmounts . map tamount