2009-04-03 14:58:05 +04:00
|
|
|
{-|
|
|
|
|
|
2010-05-27 07:58:47 +04:00
|
|
|
A 'Transaction' consists of two or more related 'Posting's which balance
|
|
|
|
to zero, representing a movement of some commodity(ies) between accounts,
|
|
|
|
plus a date and optional metadata like description and cleared status.
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2010-05-20 03:08:53 +04:00
|
|
|
module Hledger.Data.Transaction
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2010-05-20 03:08:53 +04:00
|
|
|
import Hledger.Data.Utils
|
|
|
|
import Hledger.Data.Types
|
|
|
|
import Hledger.Data.Dates
|
|
|
|
import Hledger.Data.Posting
|
|
|
|
import Hledger.Data.Amount
|
|
|
|
import Hledger.Data.Commodity (dollars, dollar, unknown)
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2009-12-16 11:07:26 +03:00
|
|
|
instance Show Transaction where show = showTransactionUnelided
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
instance Show ModifierTransaction where
|
2009-09-22 20:51:27 +04:00
|
|
|
show t = "= " ++ mtvalueexpr t ++ "\n" ++ unlines (map show (mtpostings t))
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
instance Show PeriodicTransaction where
|
2009-09-22 20:51:27 +04:00
|
|
|
show t = "~ " ++ ptperiodicexpr t ++ "\n" ++ unlines (map show (ptpostings t))
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2009-12-19 08:57:54 +03:00
|
|
|
nulltransaction :: Transaction
|
|
|
|
nulltransaction = Transaction {
|
|
|
|
tdate=nulldate,
|
|
|
|
teffectivedate=Nothing,
|
|
|
|
tstatus=False,
|
|
|
|
tcode="",
|
|
|
|
tdescription="",
|
|
|
|
tcomment="",
|
|
|
|
tpostings=[],
|
|
|
|
tpreceding_comment_lines=""
|
|
|
|
}
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
{-|
|
2010-07-13 10:30:06 +04:00
|
|
|
Show a journal transaction, formatted for the print command. ledger 2.x's
|
2009-04-03 14:58:05 +04:00
|
|
|
standard format looks like this:
|
|
|
|
|
|
|
|
@
|
|
|
|
yyyy/mm/dd[ *][ CODE] description......... [ ; comment...............]
|
|
|
|
account name 1..................... ...$amount1[ ; comment...............]
|
|
|
|
account name 2..................... ..$-amount1[ ; comment...............]
|
|
|
|
|
|
|
|
pcodewidth = no limit -- 10 -- mimicking ledger layout.
|
|
|
|
pdescwidth = no limit -- 20 -- I don't remember what these mean,
|
|
|
|
pacctwidth = 35 minimum, no maximum -- they were important at the time.
|
|
|
|
pamtwidth = 11
|
|
|
|
pcommentwidth = no limit -- 22
|
|
|
|
@
|
|
|
|
-}
|
2009-12-16 11:07:26 +03:00
|
|
|
showTransaction :: Transaction -> String
|
|
|
|
showTransaction = showTransaction' True False
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2009-12-16 11:07:26 +03:00
|
|
|
showTransactionUnelided :: Transaction -> String
|
|
|
|
showTransactionUnelided = showTransaction' False False
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2009-12-16 11:07:26 +03:00
|
|
|
showTransactionForPrint :: Bool -> Transaction -> String
|
|
|
|
showTransactionForPrint effective = showTransaction' False effective
|
2009-12-08 02:28:33 +03:00
|
|
|
|
2009-12-16 11:07:26 +03:00
|
|
|
showTransaction' :: Bool -> Bool -> Transaction -> String
|
|
|
|
showTransaction' elide effective t =
|
2009-12-16 20:58:51 +03:00
|
|
|
unlines $ [description] ++ showpostings (tpostings t) ++ [""]
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2009-11-26 00:51:31 +03:00
|
|
|
description = concat [date, status, code, desc, comment]
|
2009-12-16 20:58:51 +03:00
|
|
|
date | effective = showdate $ fromMaybe (tdate t) $ teffectivedate t
|
|
|
|
| otherwise = showdate (tdate t) ++ maybe "" showedate (teffectivedate t)
|
|
|
|
status = if tstatus t then " *" else ""
|
|
|
|
code = if length (tcode t) > 0 then printf " (%s)" $ tcode t else ""
|
|
|
|
desc = ' ' : tdescription t
|
|
|
|
comment = if null com then "" else " ; " ++ com where com = tcomment t
|
2009-09-22 19:56:59 +04:00
|
|
|
showdate = printf "%-10s" . showDate
|
2009-12-12 06:03:41 +03:00
|
|
|
showedate = printf "=%s" . showdate
|
2009-04-03 14:58:05 +04:00
|
|
|
showpostings ps
|
2009-12-16 11:07:26 +03:00
|
|
|
| elide && length ps > 1 && isTransactionBalanced t
|
2009-04-08 03:58:04 +04:00
|
|
|
= map showposting (init ps) ++ [showpostingnoamt (last ps)]
|
2009-04-03 14:58:05 +04:00
|
|
|
| otherwise = map showposting ps
|
|
|
|
where
|
2009-09-22 15:55:11 +04:00
|
|
|
showpostingnoamt p = rstrip $ showacct p ++ " " ++ showcomment (pcomment p)
|
2010-07-12 02:51:36 +04:00
|
|
|
showposting p = concatTopPadded [showacct p
|
|
|
|
," "
|
|
|
|
,showamt (pamount p)
|
|
|
|
,showcomment (pcomment p)
|
|
|
|
]
|
2009-09-22 15:55:11 +04:00
|
|
|
showacct p = " " ++ showstatus p ++ printf (printf "%%-%ds" w) (showAccountName Nothing (ptype p) (paccount p))
|
2010-07-12 02:51:36 +04:00
|
|
|
where w = maximum $ map (length . paccount) ps
|
|
|
|
showstatus p = if pstatus p then "* " else ""
|
|
|
|
showamt =
|
|
|
|
padleft 12 . showMixedAmountOrZero
|
2009-11-26 00:51:31 +03:00
|
|
|
showcomment s = if null s then "" else " ; "++s
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2009-05-24 10:22:44 +04:00
|
|
|
-- | Show an account name, clipped to the given width if any, and
|
|
|
|
-- appropriately bracketed/parenthesised for the given posting type.
|
|
|
|
showAccountName :: Maybe Int -> PostingType -> AccountName -> String
|
|
|
|
showAccountName w = fmt
|
|
|
|
where
|
|
|
|
fmt RegularPosting = take w'
|
|
|
|
fmt VirtualPosting = parenthesise . reverse . take (w'-2) . reverse
|
|
|
|
fmt BalancedVirtualPosting = bracket . reverse . take (w'-2) . reverse
|
|
|
|
w' = fromMaybe 999999 w
|
|
|
|
parenthesise s = "("++s++")"
|
|
|
|
bracket s = "["++s++"]"
|
|
|
|
|
2010-02-27 21:06:29 +03:00
|
|
|
realPostings :: Transaction -> [Posting]
|
|
|
|
realPostings = filter isReal . tpostings
|
|
|
|
|
|
|
|
virtualPostings :: Transaction -> [Posting]
|
|
|
|
virtualPostings = filter isVirtual . tpostings
|
|
|
|
|
|
|
|
balancedVirtualPostings :: Transaction -> [Posting]
|
|
|
|
balancedVirtualPostings = filter isBalancedVirtual . tpostings
|
|
|
|
|
|
|
|
-- | Get the sums of a transaction's real, virtual, and balanced virtual postings.
|
|
|
|
transactionPostingBalances :: Transaction -> (MixedAmount,MixedAmount,MixedAmount)
|
|
|
|
transactionPostingBalances t = (sumPostings $ realPostings t
|
|
|
|
,sumPostings $ virtualPostings t
|
|
|
|
,sumPostings $ balancedVirtualPostings t)
|
|
|
|
|
|
|
|
-- | Is this transaction balanced ? A balanced transaction's real
|
|
|
|
-- (non-virtual) postings sum to 0, and any balanced virtual postings
|
|
|
|
-- also sum to 0.
|
2009-12-16 11:07:26 +03:00
|
|
|
isTransactionBalanced :: Transaction -> Bool
|
2010-02-27 21:06:29 +03:00
|
|
|
isTransactionBalanced t = isReallyZeroMixedAmountCost rsum && isReallyZeroMixedAmountCost bvsum
|
|
|
|
where (rsum, _, bvsum) = transactionPostingBalances t
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
-- | Ensure that this entry is balanced, possibly auto-filling a missing
|
|
|
|
-- amount first. We can auto-fill if there is just one non-virtual
|
|
|
|
-- transaction without an amount. The auto-filled balance will be
|
|
|
|
-- converted to cost basis if possible. If the entry can not be balanced,
|
|
|
|
-- return an error message instead.
|
2009-12-16 11:07:26 +03:00
|
|
|
balanceTransaction :: Transaction -> Either String Transaction
|
2009-12-16 20:58:51 +03:00
|
|
|
balanceTransaction t@Transaction{tpostings=ps}
|
2010-04-14 20:59:02 +04:00
|
|
|
| length rwithoutamounts > 1 || length bvwithoutamounts > 1
|
|
|
|
= Left $ printerr "could not balance this transaction (too many missing amounts)"
|
2010-02-27 21:06:29 +03:00
|
|
|
| not $ isTransactionBalanced t' = Left $ printerr $ nonzerobalanceerror t'
|
2009-04-03 14:58:05 +04:00
|
|
|
| otherwise = Right t'
|
|
|
|
where
|
2010-04-14 20:59:02 +04:00
|
|
|
rps = filter isReal ps
|
|
|
|
bvps = filter isBalancedVirtual ps
|
|
|
|
(rwithamounts, rwithoutamounts) = partition hasAmount rps
|
|
|
|
(bvwithamounts, bvwithoutamounts) = partition hasAmount bvps
|
|
|
|
t' = t{tpostings=map balance ps}
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2010-04-14 20:59:02 +04:00
|
|
|
balance p | not (hasAmount p) && isReal p
|
|
|
|
= p{pamount = costOfMixedAmount (-(sum $ map pamount rwithamounts))}
|
|
|
|
| not (hasAmount p) && isBalancedVirtual p
|
|
|
|
= p{pamount = costOfMixedAmount (-(sum $ map pamount bvwithamounts))}
|
2009-04-03 14:58:05 +04:00
|
|
|
| otherwise = p
|
2010-03-10 02:06:27 +03:00
|
|
|
printerr s = intercalate "\n" [s, showTransactionUnelided t]
|
2009-04-10 12:05:56 +04:00
|
|
|
|
2010-02-27 21:06:29 +03:00
|
|
|
nonzerobalanceerror :: Transaction -> String
|
|
|
|
nonzerobalanceerror t = printf "could not balance this transaction (%s%s%s)" rmsg sep bvmsg
|
|
|
|
where
|
|
|
|
(rsum, _, bvsum) = transactionPostingBalances t
|
|
|
|
rmsg | isReallyZeroMixedAmountCost rsum = ""
|
|
|
|
| otherwise = "real postings are off by " ++ show rsum
|
|
|
|
bvmsg | isReallyZeroMixedAmountCost bvsum = ""
|
|
|
|
| otherwise = "balanced virtual postings are off by " ++ show bvsum
|
|
|
|
sep = if not (null rmsg) && not (null bvmsg) then "; " else ""
|
2009-07-09 23:22:27 +04:00
|
|
|
|
|
|
|
-- | Convert the primary date to either the actual or effective date.
|
2010-07-13 10:30:06 +04:00
|
|
|
journalTransactionWithDate :: WhichDate -> Transaction -> Transaction
|
|
|
|
journalTransactionWithDate ActualDate t = t
|
|
|
|
journalTransactionWithDate EffectiveDate t = txnTieKnot t{tdate=fromMaybe (tdate t) (teffectivedate t)}
|
2009-07-09 23:22:27 +04:00
|
|
|
|
2009-12-19 06:44:52 +03:00
|
|
|
|
2009-12-21 08:23:07 +03:00
|
|
|
-- | Ensure a transaction's postings refer back to it.
|
2009-12-19 06:44:52 +03:00
|
|
|
txnTieKnot :: Transaction -> Transaction
|
|
|
|
txnTieKnot t@Transaction{tpostings=ps} = t{tpostings=map (settxn t) ps}
|
|
|
|
|
|
|
|
-- | Set a posting's parent transaction.
|
|
|
|
settxn :: Transaction -> Posting -> Posting
|
|
|
|
settxn t p = p{ptransaction=Just t}
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2010-03-09 06:52:17 +03:00
|
|
|
tests_Transaction = TestList [
|
|
|
|
"showTransaction" ~: do
|
|
|
|
assertEqual "show a balanced transaction, eliding last amount"
|
|
|
|
(unlines
|
|
|
|
["2007/01/28 coopportunity"
|
|
|
|
," expenses:food:groceries $47.18"
|
|
|
|
," assets:checking"
|
|
|
|
,""
|
|
|
|
])
|
|
|
|
(let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" ""
|
|
|
|
[Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting (Just t)
|
|
|
|
,Posting False "assets:checking" (Mixed [dollars (-47.18)]) "" RegularPosting (Just t)
|
|
|
|
] ""
|
|
|
|
in showTransaction t)
|
|
|
|
|
|
|
|
,"showTransaction" ~: do
|
|
|
|
assertEqual "show a balanced transaction, no eliding"
|
|
|
|
(unlines
|
|
|
|
["2007/01/28 coopportunity"
|
|
|
|
," expenses:food:groceries $47.18"
|
|
|
|
," assets:checking $-47.18"
|
|
|
|
,""
|
|
|
|
])
|
|
|
|
(let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" ""
|
|
|
|
[Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting (Just t)
|
|
|
|
,Posting False "assets:checking" (Mixed [dollars (-47.18)]) "" RegularPosting (Just t)
|
|
|
|
] ""
|
|
|
|
in showTransactionUnelided t)
|
|
|
|
|
|
|
|
-- document some cases that arise in debug/testing:
|
|
|
|
,"showTransaction" ~: do
|
|
|
|
assertEqual "show an unbalanced transaction, should not elide"
|
|
|
|
(unlines
|
|
|
|
["2007/01/28 coopportunity"
|
|
|
|
," expenses:food:groceries $47.18"
|
|
|
|
," assets:checking $-47.19"
|
|
|
|
,""
|
|
|
|
])
|
|
|
|
(showTransaction
|
|
|
|
(txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" ""
|
|
|
|
[Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting Nothing
|
|
|
|
,Posting False "assets:checking" (Mixed [dollars (-47.19)]) "" RegularPosting Nothing
|
|
|
|
] ""))
|
|
|
|
|
|
|
|
,"showTransaction" ~: do
|
|
|
|
assertEqual "show an unbalanced transaction with one posting, should not elide"
|
|
|
|
(unlines
|
|
|
|
["2007/01/28 coopportunity"
|
|
|
|
," expenses:food:groceries $47.18"
|
|
|
|
,""
|
|
|
|
])
|
|
|
|
(showTransaction
|
|
|
|
(txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" ""
|
|
|
|
[Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting Nothing
|
|
|
|
] ""))
|
|
|
|
|
|
|
|
,"showTransaction" ~: do
|
|
|
|
assertEqual "show a transaction with one posting and a missing amount"
|
|
|
|
(unlines
|
|
|
|
["2007/01/28 coopportunity"
|
|
|
|
," expenses:food:groceries "
|
|
|
|
,""
|
|
|
|
])
|
|
|
|
(showTransaction
|
|
|
|
(txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" ""
|
|
|
|
[Posting False "expenses:food:groceries" missingamt "" RegularPosting Nothing
|
|
|
|
] ""))
|
|
|
|
|
|
|
|
,"showTransaction" ~: do
|
|
|
|
assertEqual "show a transaction with a priced commodityless amount"
|
|
|
|
(unlines
|
|
|
|
["2010/01/01 x"
|
|
|
|
," a 1 @ $2"
|
|
|
|
," b "
|
|
|
|
,""
|
|
|
|
])
|
|
|
|
(showTransaction
|
|
|
|
(txnTieKnot $ Transaction (parsedate "2010/01/01") Nothing False "" "x" ""
|
|
|
|
[Posting False "a" (Mixed [Amount unknown 1 (Just $ Mixed [Amount dollar{precision=0} 2 Nothing])]) "" RegularPosting Nothing
|
|
|
|
,Posting False "b" missingamt "" RegularPosting Nothing
|
|
|
|
] ""))
|
|
|
|
|
|
|
|
]
|