2009-04-03 14:58:05 +04:00
|
|
|
{-|
|
|
|
|
|
2012-05-14 22:52:22 +04:00
|
|
|
A 'Posting' represents a change (by some 'MixedAmount') of the balance in
|
|
|
|
some 'Account'. Each 'Transaction' contains two or more postings which
|
|
|
|
should add up to 0. Postings reference their parent transaction, so we can
|
|
|
|
look up the date or description there.
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2012-05-07 18:36:40 +04:00
|
|
|
module Hledger.Data.Posting (
|
|
|
|
-- * Posting
|
|
|
|
nullposting,
|
2012-12-06 04:03:07 +04:00
|
|
|
posting,
|
|
|
|
post,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * operations
|
|
|
|
postingCleared,
|
|
|
|
isReal,
|
|
|
|
isVirtual,
|
|
|
|
isBalancedVirtual,
|
|
|
|
isEmptyPosting,
|
|
|
|
hasAmount,
|
2012-05-28 04:27:55 +04:00
|
|
|
postingAllTags,
|
|
|
|
transactionAllTags,
|
2012-12-22 04:24:38 +04:00
|
|
|
relatedPostings,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * date operations
|
|
|
|
postingDate,
|
2012-12-06 08:43:41 +04:00
|
|
|
postingDate2,
|
2012-05-07 18:36:40 +04:00
|
|
|
isPostingInDateSpan,
|
2014-04-14 01:57:40 +04:00
|
|
|
isPostingInDateSpan',
|
2012-05-07 18:36:40 +04:00
|
|
|
postingsDateSpan,
|
2014-04-14 02:14:07 +04:00
|
|
|
postingsDateSpan',
|
2012-05-14 22:52:22 +04:00
|
|
|
-- * account name operations
|
2012-05-07 18:36:40 +04:00
|
|
|
accountNamesFromPostings,
|
|
|
|
accountNamePostingType,
|
|
|
|
accountNameWithoutPostingType,
|
|
|
|
accountNameWithPostingType,
|
|
|
|
joinAccountNames,
|
|
|
|
concatAccountNames,
|
|
|
|
accountNameApplyAliases,
|
|
|
|
-- * arithmetic
|
|
|
|
sumPostings,
|
|
|
|
-- * rendering
|
|
|
|
showPosting,
|
|
|
|
-- * misc.
|
2012-05-14 22:52:22 +04:00
|
|
|
showComment,
|
2012-05-07 18:36:40 +04:00
|
|
|
tests_Hledger_Data_Posting
|
|
|
|
)
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.List
|
2012-12-06 04:03:07 +04:00
|
|
|
import Data.Maybe
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.Ord
|
|
|
|
import Data.Time.Calendar
|
2011-08-03 03:29:13 +04:00
|
|
|
import Safe
|
2011-05-28 08:11:44 +04:00
|
|
|
import Test.HUnit
|
|
|
|
import Text.Printf
|
|
|
|
|
|
|
|
import Hledger.Utils
|
2010-05-20 03:08:53 +04:00
|
|
|
import Hledger.Data.Types
|
|
|
|
import Hledger.Data.Amount
|
|
|
|
import Hledger.Data.AccountName
|
2010-07-11 22:56:36 +04:00
|
|
|
import Hledger.Data.Dates (nulldate, spanContainsDate)
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
instance Show Posting where show = showPosting
|
|
|
|
|
2012-12-06 04:03:07 +04:00
|
|
|
nullposting, posting :: Posting
|
|
|
|
nullposting = Posting
|
2012-12-06 04:28:23 +04:00
|
|
|
{pdate=Nothing
|
2012-12-06 06:41:37 +04:00
|
|
|
,pdate2=Nothing
|
2012-12-06 04:28:23 +04:00
|
|
|
,pstatus=False
|
2012-12-06 04:03:07 +04:00
|
|
|
,paccount=""
|
|
|
|
,pamount=nullmixedamt
|
|
|
|
,pcomment=""
|
|
|
|
,ptype=RegularPosting
|
|
|
|
,ptags=[]
|
2013-05-29 03:18:15 +04:00
|
|
|
,pbalanceassertion=Nothing
|
2012-12-06 04:03:07 +04:00
|
|
|
,ptransaction=Nothing
|
|
|
|
}
|
|
|
|
posting = nullposting
|
|
|
|
|
|
|
|
post :: AccountName -> Amount -> Posting
|
|
|
|
post acct amt = posting {paccount=acct, pamount=mixed amt}
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2014-05-07 08:35:38 +04:00
|
|
|
-- XXX once rendered user output, but just for debugging now; clean up
|
2009-04-03 14:58:05 +04:00
|
|
|
showPosting :: Posting -> String
|
2012-05-14 22:52:22 +04:00
|
|
|
showPosting p@Posting{paccount=a,pamount=amt,ptype=t} =
|
2014-05-07 08:35:38 +04:00
|
|
|
unlines $ [concatTopPadded [show (postingDate p) ++ " ", showaccountname a ++ " ", showamount amt, showComment (pcomment p)]]
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2009-11-25 09:13:35 +03:00
|
|
|
ledger3ishlayout = False
|
|
|
|
acctnamewidth = if ledger3ishlayout then 25 else 22
|
|
|
|
showaccountname = printf ("%-"++(show acctnamewidth)++"s") . bracket . elideAccountName width
|
2009-12-19 06:44:52 +03:00
|
|
|
(bracket,width) = case t of
|
2009-11-25 09:13:35 +03:00
|
|
|
BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2)
|
|
|
|
VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2)
|
|
|
|
_ -> (id,acctnamewidth)
|
2011-08-31 21:44:20 +04:00
|
|
|
showamount = padleft 12 . showMixedAmount
|
2012-05-14 22:52:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
showComment :: String -> String
|
2012-12-06 04:28:23 +04:00
|
|
|
showComment s = if null s then "" else " ;" ++ s
|
2012-05-14 22:52:22 +04:00
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
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
|
2012-05-27 22:14:20 +04:00
|
|
|
hasAmount = (/= missingmixedamt) . pamount
|
2009-05-25 21:28:41 +04:00
|
|
|
|
2009-12-19 08:57:54 +03:00
|
|
|
accountNamesFromPostings :: [Posting] -> [AccountName]
|
|
|
|
accountNamesFromPostings = nub . map paccount
|
|
|
|
|
|
|
|
sumPostings :: [Posting] -> MixedAmount
|
2011-08-31 20:54:10 +04:00
|
|
|
sumPostings = sum . map pamount
|
2009-12-19 08:57:54 +03:00
|
|
|
|
2012-12-06 06:41:37 +04:00
|
|
|
-- | Get a posting's (primary) date - it's own primary date if specified,
|
|
|
|
-- otherwise the parent transaction's primary date, or the null date if
|
|
|
|
-- there is no parent transaction.
|
2009-12-19 08:57:54 +03:00
|
|
|
postingDate :: Posting -> Day
|
2012-12-06 04:28:23 +04:00
|
|
|
postingDate p = fromMaybe txndate $ pdate p
|
|
|
|
where
|
|
|
|
txndate = maybe nulldate tdate $ ptransaction p
|
2009-12-19 08:57:54 +03:00
|
|
|
|
2012-12-06 08:43:41 +04:00
|
|
|
-- | Get a posting's secondary (secondary) date, which is the first of:
|
2012-12-06 06:41:37 +04:00
|
|
|
-- posting's secondary date, transaction's secondary date, posting's
|
|
|
|
-- primary date, transaction's primary date, or the null date if there is
|
|
|
|
-- no parent transaction.
|
2012-12-06 08:43:41 +04:00
|
|
|
postingDate2 :: Posting -> Day
|
|
|
|
postingDate2 p = headDef nulldate $ catMaybes dates
|
2012-12-06 06:41:37 +04:00
|
|
|
where dates = [pdate2 p
|
2012-12-06 08:43:41 +04:00
|
|
|
,maybe Nothing tdate2 $ ptransaction p
|
2012-12-06 06:41:37 +04:00
|
|
|
,pdate p
|
|
|
|
,maybe Nothing (Just . tdate) $ ptransaction p
|
|
|
|
]
|
2012-12-06 05:10:15 +04:00
|
|
|
|
2010-12-27 01:39:28 +03:00
|
|
|
-- |Is this posting cleared? If this posting was individually marked
|
|
|
|
-- as cleared, returns True. Otherwise, return the parent
|
|
|
|
-- transaction's cleared status or, if there is no parent
|
|
|
|
-- transaction, return False.
|
2009-12-21 08:23:07 +03:00
|
|
|
postingCleared :: Posting -> Bool
|
2010-12-27 01:39:28 +03:00
|
|
|
postingCleared p = if pstatus p
|
|
|
|
then True
|
|
|
|
else maybe False tstatus $ ptransaction p
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2012-05-28 04:27:55 +04:00
|
|
|
-- | Tags for this posting including any inherited from its parent transaction.
|
|
|
|
postingAllTags :: Posting -> [Tag]
|
2014-03-06 02:43:58 +04:00
|
|
|
postingAllTags p = ptags p ++ maybe [] ttags (ptransaction p)
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2014-03-02 05:42:13 +04:00
|
|
|
-- | Tags for this transaction including any from its postings.
|
2012-05-28 04:27:55 +04:00
|
|
|
transactionAllTags :: Transaction -> [Tag]
|
2014-03-02 05:42:13 +04:00
|
|
|
transactionAllTags t = ttags t ++ concatMap ptags (tpostings t)
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2012-12-22 04:24:38 +04:00
|
|
|
-- Get the other postings from this posting's transaction.
|
|
|
|
relatedPostings :: Posting -> [Posting]
|
|
|
|
relatedPostings p@Posting{ptransaction=Just t} = filter (/= p) $ tpostings t
|
|
|
|
relatedPostings _ = []
|
|
|
|
|
2009-12-19 08:57:54 +03:00
|
|
|
-- | Does this posting fall within the given date span ?
|
|
|
|
isPostingInDateSpan :: DateSpan -> Posting -> Bool
|
2010-07-11 22:56:36 +04:00
|
|
|
isPostingInDateSpan s = spanContainsDate s . postingDate
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2014-04-14 01:57:40 +04:00
|
|
|
-- --date2-sensitive version, separate for now to avoid disturbing multiBalanceReport.
|
|
|
|
isPostingInDateSpan' :: WhichDate -> DateSpan -> Posting -> Bool
|
|
|
|
isPostingInDateSpan' PrimaryDate s = spanContainsDate s . postingDate
|
|
|
|
isPostingInDateSpan' SecondaryDate s = spanContainsDate s . postingDate2
|
|
|
|
|
2009-12-21 08:23:07 +03:00
|
|
|
isEmptyPosting :: Posting -> Bool
|
|
|
|
isEmptyPosting = isZeroMixedAmount . pamount
|
|
|
|
|
2012-05-14 22:52:22 +04:00
|
|
|
-- | Get the minimal date span which contains all the postings, or the
|
|
|
|
-- null date span if there are none.
|
2009-12-21 09:03:34 +03:00
|
|
|
postingsDateSpan :: [Posting] -> DateSpan
|
|
|
|
postingsDateSpan [] = DateSpan Nothing Nothing
|
|
|
|
postingsDateSpan ps = DateSpan (Just $ postingDate $ head ps') (Just $ addDays 1 $ postingDate $ last ps')
|
|
|
|
where ps' = sortBy (comparing postingDate) ps
|
|
|
|
|
2014-04-14 02:14:07 +04:00
|
|
|
-- --date2-sensitive version, as above.
|
|
|
|
postingsDateSpan' :: WhichDate -> [Posting] -> DateSpan
|
2014-04-14 19:31:11 +04:00
|
|
|
postingsDateSpan' _ [] = DateSpan Nothing Nothing
|
2014-04-14 02:14:07 +04:00
|
|
|
postingsDateSpan' wd ps = DateSpan (Just $ postingdate $ head ps') (Just $ addDays 1 $ postingdate $ last ps')
|
|
|
|
where
|
|
|
|
ps' = sortBy (comparing postingdate) ps
|
|
|
|
postingdate = if wd == PrimaryDate then postingDate else postingDate2
|
|
|
|
|
2011-08-05 04:05:39 +04:00
|
|
|
-- AccountName stuff that depends on PostingType
|
2011-08-03 03:29:13 +04:00
|
|
|
|
|
|
|
accountNamePostingType :: AccountName -> PostingType
|
|
|
|
accountNamePostingType a
|
|
|
|
| null a = RegularPosting
|
|
|
|
| head a == '[' && last a == ']' = BalancedVirtualPosting
|
|
|
|
| head a == '(' && last a == ')' = VirtualPosting
|
|
|
|
| otherwise = RegularPosting
|
|
|
|
|
|
|
|
accountNameWithoutPostingType :: AccountName -> AccountName
|
|
|
|
accountNameWithoutPostingType a = case accountNamePostingType a of
|
|
|
|
BalancedVirtualPosting -> init $ tail a
|
|
|
|
VirtualPosting -> init $ tail a
|
|
|
|
RegularPosting -> a
|
|
|
|
|
|
|
|
accountNameWithPostingType :: PostingType -> AccountName -> AccountName
|
|
|
|
accountNameWithPostingType BalancedVirtualPosting a = "["++accountNameWithoutPostingType a++"]"
|
|
|
|
accountNameWithPostingType VirtualPosting a = "("++accountNameWithoutPostingType a++")"
|
|
|
|
accountNameWithPostingType RegularPosting a = accountNameWithoutPostingType a
|
|
|
|
|
|
|
|
-- | Prefix one account name to another, preserving posting type
|
|
|
|
-- indicators like concatAccountNames.
|
|
|
|
joinAccountNames :: AccountName -> AccountName -> AccountName
|
|
|
|
joinAccountNames a b = concatAccountNames $ filter (not . null) [a,b]
|
|
|
|
|
|
|
|
-- | Join account names into one. If any of them has () or [] posting type
|
|
|
|
-- indicators, these (the first type encountered) will also be applied to
|
|
|
|
-- the resulting account name.
|
|
|
|
concatAccountNames :: [AccountName] -> AccountName
|
|
|
|
concatAccountNames as = accountNameWithPostingType t $ intercalate ":" $ map accountNameWithoutPostingType as
|
|
|
|
where t = headDef RegularPosting $ filter (/= RegularPosting) $ map accountNamePostingType as
|
|
|
|
|
2011-08-05 04:05:39 +04:00
|
|
|
-- | Rewrite an account name using the first applicable alias from the given list, if any.
|
|
|
|
accountNameApplyAliases :: [(AccountName,AccountName)] -> AccountName -> AccountName
|
|
|
|
accountNameApplyAliases aliases a = withorigtype
|
|
|
|
where
|
|
|
|
(a',t) = (accountNameWithoutPostingType a, accountNamePostingType a)
|
|
|
|
firstmatchingalias = headDef Nothing $ map Just $ filter (\(orig,_) -> orig == a' || orig `isAccountNamePrefixOf` a') aliases
|
|
|
|
rewritten = maybe a' (\(orig,alias) -> alias++drop (length orig) a') firstmatchingalias
|
|
|
|
withorigtype = accountNameWithPostingType t rewritten
|
|
|
|
|
2010-12-27 23:26:22 +03:00
|
|
|
tests_Hledger_Data_Posting = TestList [
|
2011-08-03 03:29:13 +04:00
|
|
|
|
|
|
|
"accountNamePostingType" ~: do
|
|
|
|
accountNamePostingType "a" `is` RegularPosting
|
|
|
|
accountNamePostingType "(a)" `is` VirtualPosting
|
|
|
|
accountNamePostingType "[a]" `is` BalancedVirtualPosting
|
|
|
|
|
|
|
|
,"accountNameWithoutPostingType" ~: do
|
|
|
|
accountNameWithoutPostingType "(a)" `is` "a"
|
|
|
|
|
|
|
|
,"accountNameWithPostingType" ~: do
|
|
|
|
accountNameWithPostingType VirtualPosting "[a]" `is` "(a)"
|
|
|
|
|
|
|
|
,"joinAccountNames" ~: do
|
|
|
|
"a" `joinAccountNames` "b:c" `is` "a:b:c"
|
|
|
|
"a" `joinAccountNames` "(b:c)" `is` "(a:b:c)"
|
|
|
|
"[a]" `joinAccountNames` "(b:c)" `is` "[a:b:c]"
|
|
|
|
"" `joinAccountNames` "a" `is` "a"
|
|
|
|
|
|
|
|
,"concatAccountNames" ~: do
|
|
|
|
concatAccountNames [] `is` ""
|
|
|
|
concatAccountNames ["a","(b)","[c:d]"] `is` "(a:b:c:d)"
|
|
|
|
|
2010-12-27 23:26:22 +03:00
|
|
|
]
|
2012-12-06 04:28:23 +04:00
|
|
|
|