1 {-|
    2 
    3 A 'Posting' represents a 'MixedAmount' being added to or subtracted from a
    4 single 'Account'.  Each 'LedgerTransaction' contains two or more postings
    5 which should add up to 0.  
    6 
    7 Generally, we use these with the ledger transaction's date and description
    8 added, which we call a 'Transaction'.
    9 
   10 -}
   11 
   12 module Ledger.Posting
   13 where
   14 import Ledger.Utils
   15 import Ledger.Types
   16 import Ledger.Amount
   17 import Ledger.AccountName
   18 
   19 
   20 instance Show Posting where show = showPosting
   21 
   22 nullrawposting = Posting False "" nullmixedamt "" RegularPosting
   23 
   24 showPosting :: Posting -> String
   25 showPosting (Posting s a amt _ ttype) = 
   26     concatTopPadded [showaccountname a ++ " ", showamount amt]
   27     where
   28       showaccountname = printf "%-22s" . bracket . elideAccountName width
   29       (bracket,width) = case ttype of
   30                       BalancedVirtualPosting -> (\s -> "["++s++"]", 20)
   31                       VirtualPosting -> (\s -> "("++s++")", 20)
   32                       otherwise -> (id,22)
   33       showamount = padleft 12 . showMixedAmountOrZero
   34 
   35 isReal :: Posting -> Bool
   36 isReal p = ptype p == RegularPosting
   37 
   38 isVirtual :: Posting -> Bool
   39 isVirtual p = ptype p == VirtualPosting
   40 
   41 isBalancedVirtual :: Posting -> Bool
   42 isBalancedVirtual p = ptype p == BalancedVirtualPosting
   43 
   44 hasAmount :: Posting -> Bool
   45 hasAmount = (/= missingamt) . pamount
   46 
   47 postingTypeFromAccountName a
   48     | head a == '[' && last a == ']' = BalancedVirtualPosting
   49     | head a == '(' && last a == ')' = VirtualPosting
   50     | otherwise = RegularPosting
   51