2008-10-03 06:04:15 +04:00
|
|
|
{-|
|
|
|
|
|
|
|
|
A 'RawTransaction' represents a single transaction line within a ledger
|
|
|
|
entry. We call it raw to distinguish from the cached 'Transaction'.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2008-10-03 04:40:06 +04:00
|
|
|
module Ledger.RawTransaction
|
2007-07-04 13:51:37 +04:00
|
|
|
where
|
2008-10-03 04:05:16 +04:00
|
|
|
import Ledger.Utils
|
2008-10-03 04:12:59 +04:00
|
|
|
import Ledger.Types
|
2008-10-03 04:40:06 +04:00
|
|
|
import Ledger.Amount
|
2008-10-15 23:14:34 +04:00
|
|
|
import Ledger.AccountName
|
2007-07-04 13:51:37 +04:00
|
|
|
|
|
|
|
|
2008-10-16 10:00:46 +04:00
|
|
|
instance Show RawTransaction where show = showRawTransaction
|
2007-07-04 13:51:37 +04:00
|
|
|
|
2008-10-16 10:00:46 +04:00
|
|
|
showRawTransaction :: RawTransaction -> String
|
|
|
|
showRawTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ tamount t)
|
2007-07-04 13:51:37 +04:00
|
|
|
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
|
2007-07-04 13:51:37 +04:00
|
|
|
|
2008-10-16 07:56:43 +04:00
|
|
|
-- | 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]
|
2007-07-04 13:51:37 +04:00
|
|
|
autofillTransactions ts =
|
2007-07-09 21:39:00 +04:00
|
|
|
case (length blanks) of
|
2008-10-16 07:56:43 +04:00
|
|
|
0 -> Just ts
|
|
|
|
1 -> Just $ map balance ts
|
|
|
|
otherwise -> Nothing
|
2007-07-09 21:39:00 +04:00
|
|
|
where
|
|
|
|
(normals, blanks) = partition isnormal ts
|
2008-10-13 01:52:48 +04:00
|
|
|
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)}
|
2007-07-04 13:51:37 +04:00
|
|
|
|
2008-10-03 03:55:01 +04:00
|
|
|
sumLedgerTransactions :: [RawTransaction] -> Amount
|
2008-10-15 11:00:47 +04:00
|
|
|
sumLedgerTransactions = sumAmounts . map tamount
|