2009-04-03 14:58:05 +04:00
|
|
|
{-|
|
|
|
|
|
2009-04-05 03:15:42 +04:00
|
|
|
A 'Posting' represents a 'MixedAmount' being added to or subtracted from a
|
|
|
|
single 'Account'. Each 'LedgerTransaction' contains two or more postings
|
|
|
|
which should add up to 0.
|
|
|
|
|
|
|
|
Generally, we use these with the ledger transaction's date and description
|
|
|
|
added, which we call a 'Transaction'.
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Ledger.Posting
|
|
|
|
where
|
|
|
|
import Ledger.Utils
|
|
|
|
import Ledger.Types
|
|
|
|
import Ledger.Amount
|
|
|
|
import Ledger.AccountName
|
|
|
|
|
|
|
|
|
|
|
|
instance Show Posting where show = showPosting
|
|
|
|
|
|
|
|
nullrawposting = Posting False "" nullmixedamt "" RegularPosting
|
|
|
|
|
|
|
|
showPosting :: Posting -> String
|
2009-06-05 13:44:20 +04:00
|
|
|
showPosting (Posting _ a amt _ ttype) =
|
2009-04-03 14:58:05 +04:00
|
|
|
concatTopPadded [showaccountname a ++ " ", showamount amt]
|
|
|
|
where
|
|
|
|
showaccountname = printf "%-22s" . bracket . elideAccountName width
|
|
|
|
(bracket,width) = case ttype of
|
2009-06-05 13:44:20 +04:00
|
|
|
BalancedVirtualPosting -> (\s -> "["++s++"]", 20)
|
|
|
|
VirtualPosting -> (\s -> "("++s++")", 20)
|
|
|
|
_ -> (id,22)
|
2009-04-03 14:58:05 +04:00
|
|
|
showamount = padleft 12 . showMixedAmountOrZero
|
|
|
|
|
|
|
|
isReal :: Posting -> Bool
|
|
|
|
isReal p = ptype p == RegularPosting
|
|
|
|
|
2009-05-17 03:12:42 +04:00
|
|
|
isVirtual :: Posting -> Bool
|
|
|
|
isVirtual p = ptype p == VirtualPosting
|
|
|
|
|
|
|
|
isBalancedVirtual :: Posting -> Bool
|
|
|
|
isBalancedVirtual p = ptype p == BalancedVirtualPosting
|
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
hasAmount :: Posting -> Bool
|
|
|
|
hasAmount = (/= missingamt) . pamount
|
2009-05-25 21:28:41 +04:00
|
|
|
|
|
|
|
postingTypeFromAccountName a
|
|
|
|
| head a == '[' && last a == ']' = BalancedVirtualPosting
|
|
|
|
| head a == '(' && last a == ')' = VirtualPosting
|
|
|
|
| otherwise = RegularPosting
|
|
|
|
|