refactor: clean up Posting construction

This commit is contained in:
Simon Michael 2012-12-06 00:03:07 +00:00
parent 26ad56e1b2
commit 4aafeb32e6
7 changed files with 108 additions and 310 deletions

View File

@ -524,26 +524,10 @@ Right samplejournal = journalBalanceTransactions $
tdescription="income",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="assets:bank:checking",
pamount=(Mixed [usd 1]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="income:salary",
pamount=(missingmixedamt),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["assets:bank:checking" `post` usd 1
,"income:salary" `post` missingamt
],
tpreceding_comment_lines=""
}
,
@ -555,26 +539,10 @@ Right samplejournal = journalBalanceTransactions $
tdescription="gift",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="assets:bank:checking",
pamount=(Mixed [usd 1]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="income:gifts",
pamount=(missingmixedamt),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["assets:bank:checking" `post` usd 1
,"income:gifts" `post` missingamt
],
tpreceding_comment_lines=""
}
,
@ -586,26 +554,10 @@ Right samplejournal = journalBalanceTransactions $
tdescription="save",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="assets:bank:saving",
pamount=(Mixed [usd 1]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:bank:checking",
pamount=(Mixed [usd (-1)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["assets:bank:saving" `post` usd 1
,"assets:bank:checking" `post` usd (-1)
],
tpreceding_comment_lines=""
}
,
@ -617,35 +569,10 @@ Right samplejournal = journalBalanceTransactions $
tdescription="eat & shop",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="expenses:food",
pamount=(Mixed [usd 1]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="expenses:supplies",
pamount=(Mixed [usd 1]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:cash",
pamount=(missingmixedamt),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=["expenses:food" `post` usd 1
,"expenses:supplies" `post` usd 1
,"assets:cash" `post` missingamt
],
tpreceding_comment_lines=""
}
,
@ -657,26 +584,9 @@ Right samplejournal = journalBalanceTransactions $
tdescription="pay off",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="liabilities:debts",
pamount=(Mixed [usd 1]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:bank:checking",
pamount=(Mixed [usd (-1)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=["liabilities:debts" `post` usd 1
,"assets:bank:checking" `post` usd (-1)
],
tpreceding_comment_lines=""
}
]

View File

@ -10,6 +10,8 @@ look up the date or description there.
module Hledger.Data.Posting (
-- * Posting
nullposting,
posting,
post,
-- * operations
postingCleared,
isReal,
@ -44,6 +46,7 @@ module Hledger.Data.Posting (
)
where
import Data.List
import Data.Maybe
import Data.Ord
import Data.Time.Calendar
import Safe
@ -59,8 +62,20 @@ import Hledger.Data.Dates (nulldate, spanContainsDate)
instance Show Posting where show = showPosting
nullposting :: Posting
nullposting = Posting False "" nullmixedamt "" RegularPosting [] Nothing
nullposting, posting :: Posting
nullposting = Posting
{pstatus=False
,paccount=""
,pamount=nullmixedamt
,pcomment=""
,ptype=RegularPosting
,ptags=[]
,ptransaction=Nothing
}
posting = nullposting
post :: AccountName -> Amount -> Posting
post acct amt = posting {paccount=acct, pamount=mixed amt}
showPosting :: Posting -> String
showPosting p@Posting{paccount=a,pamount=amt,ptype=t} =

View File

@ -21,6 +21,7 @@ import Hledger.Utils
import Hledger.Data.Types
import Hledger.Data.Dates
import Hledger.Data.Amount
import Hledger.Data.Posting
import Hledger.Data.Transaction
instance Show TimeLogEntry where
@ -94,8 +95,8 @@ entryFromTimeLogInOut i o
idate = localDay itime
hours = elapsedSeconds (toutc otime) (toutc itime) / 3600 where toutc = localTimeToUTC utc
amount = Mixed [hrs hours]
ps = [Posting{pstatus=False,paccount=acctname,pamount=amount,
pcomment="",ptype=VirtualPosting,ptags=[],ptransaction=Just t}]
ps = [posting{paccount=acctname, pamount=amount, ptype=VirtualPosting, ptransaction=Just t}]
tests_Hledger_Data_TimeLog = TestList [

View File

@ -376,8 +376,8 @@ tests_Hledger_Data_Transaction = TestList $ concat [
,""
])
(let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" []
[Posting False "expenses:food:groceries" (Mixed [usd 47.18]) "" RegularPosting [] (Just t)
,Posting False "assets:checking" (Mixed [usd (-47.18)]) "" RegularPosting [] (Just t)
[posting{paccount="expenses:food:groceries", pamount=Mixed [usd 47.18], ptransaction=Just t}
,posting{paccount="assets:checking", pamount=Mixed [usd (-47.18)], ptransaction=Just t}
] ""
in showTransaction t)
@ -390,8 +390,8 @@ tests_Hledger_Data_Transaction = TestList $ concat [
,""
])
(let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" []
[Posting False "expenses:food:groceries" (Mixed [usd 47.18]) "" RegularPosting [] (Just t)
,Posting False "assets:checking" (Mixed [usd (-47.18)]) "" RegularPosting [] (Just t)
[posting{paccount="expenses:food:groceries", pamount=Mixed [usd 47.18], ptransaction=Just t}
,posting{paccount="assets:checking", pamount=Mixed [usd (-47.18)], ptransaction=Just t}
] ""
in showTransactionUnelided t)
@ -406,8 +406,8 @@ tests_Hledger_Data_Transaction = TestList $ concat [
])
(showTransaction
(txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" []
[Posting False "expenses:food:groceries" (Mixed [usd 47.18]) "" RegularPosting [] Nothing
,Posting False "assets:checking" (Mixed [usd (-47.19)]) "" RegularPosting [] Nothing
[posting{paccount="expenses:food:groceries", pamount=Mixed [usd 47.18]}
,posting{paccount="assets:checking", pamount=Mixed [usd (-47.19)]}
] ""))
,"showTransaction" ~: do
@ -419,7 +419,7 @@ tests_Hledger_Data_Transaction = TestList $ concat [
])
(showTransaction
(txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" []
[Posting False "expenses:food:groceries" (Mixed [usd 47.18]) "" RegularPosting [] Nothing
[posting{paccount="expenses:food:groceries", pamount=Mixed [usd 47.18]}
] ""))
,"showTransaction" ~: do
@ -431,7 +431,7 @@ tests_Hledger_Data_Transaction = TestList $ concat [
])
(showTransaction
(txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" []
[Posting False "expenses:food:groceries" missingmixedamt "" RegularPosting [] Nothing
[posting{paccount="expenses:food:groceries", pamount=missingmixedamt}
] ""))
,"showTransaction" ~: do
@ -444,28 +444,28 @@ tests_Hledger_Data_Transaction = TestList $ concat [
])
(showTransaction
(txnTieKnot $ Transaction (parsedate "2010/01/01") Nothing False "" "x" "" []
[Posting False "a" (Mixed [num 1 `at` (usd 2 `withPrecision` 0)]) "" RegularPosting [] Nothing
,Posting False "b" missingmixedamt "" RegularPosting [] Nothing
[posting{paccount="a", pamount=Mixed [num 1 `at` (usd 2 `withPrecision` 0)]}
,posting{paccount="b", pamount= missingmixedamt}
] ""))
,"balanceTransaction" ~: do
assertBool "detect unbalanced entry, sign error"
(isLeft $ balanceTransaction Nothing
(Transaction (parsedate "2007/01/28") Nothing False "" "test" "" []
[Posting False "a" (Mixed [usd 1]) "" RegularPosting [] Nothing,
Posting False "b" (Mixed [usd 1]) "" RegularPosting [] Nothing
[posting{paccount="a", pamount=Mixed [usd 1]}
,posting{paccount="b", pamount=Mixed [usd 1]}
] ""))
assertBool "detect unbalanced entry, multiple missing amounts"
(isLeft $ balanceTransaction Nothing
(Transaction (parsedate "2007/01/28") Nothing False "" "test" "" []
[Posting False "a" missingmixedamt "" RegularPosting [] Nothing,
Posting False "b" missingmixedamt "" RegularPosting [] Nothing
[posting{paccount="a", pamount=missingmixedamt}
,posting{paccount="b", pamount=missingmixedamt}
] ""))
let e = balanceTransaction Nothing (Transaction (parsedate "2007/01/28") Nothing False "" "" "" []
[Posting False "a" (Mixed [usd 1]) "" RegularPosting [] Nothing,
Posting False "b" missingmixedamt "" RegularPosting [] Nothing
[posting{paccount="a", pamount=Mixed [usd 1]}
,posting{paccount="b", pamount=missingmixedamt}
] "")
assertBool "balanceTransaction allows one missing amount" (isRight e)
assertEqual "balancing amount is inferred"
@ -475,8 +475,8 @@ tests_Hledger_Data_Transaction = TestList $ concat [
Left _ -> error' "should not happen")
let e = balanceTransaction Nothing (Transaction (parsedate "2011/01/01") Nothing False "" "" "" []
[Posting False "a" (Mixed [usd 1.35]) "" RegularPosting [] Nothing,
Posting False "b" (Mixed [eur (-1)]) "" RegularPosting [] Nothing
[posting{paccount="a", pamount=Mixed [usd 1.35]}
,posting{paccount="b", pamount=Mixed [eur (-1)]}
] "")
assertBool "balanceTransaction can infer conversion price" (isRight e)
assertEqual "balancing conversion price is inferred"
@ -487,52 +487,52 @@ tests_Hledger_Data_Transaction = TestList $ concat [
assertBool "balanceTransaction balances based on cost if there are unit prices" (isRight $
balanceTransaction Nothing (Transaction (parsedate "2011/01/01") Nothing False "" "" "" []
[Posting False "a" (Mixed [usd 1 `at` eur 2]) "" RegularPosting [] Nothing
,Posting False "a" (Mixed [usd (-2) `at` eur 1]) "" RegularPosting [] Nothing
[posting{paccount="a", pamount=Mixed [usd 1 `at` eur 2]}
,posting{paccount="a", pamount=Mixed [usd (-2) `at` eur 1]}
] ""))
assertBool "balanceTransaction balances based on cost if there are total prices" (isRight $
balanceTransaction Nothing (Transaction (parsedate "2011/01/01") Nothing False "" "" "" []
[Posting False "a" (Mixed [usd 1 @@ eur 1]) "" RegularPosting [] Nothing
,Posting False "a" (Mixed [usd (-2) @@ eur 1]) "" RegularPosting [] Nothing
[posting{paccount="a", pamount=Mixed [usd 1 @@ eur 1]}
,posting{paccount="a", pamount=Mixed [usd (-2) @@ eur 1]}
] ""))
,"isTransactionBalanced" ~: do
let t = Transaction (parsedate "2009/01/01") Nothing False "" "a" "" []
[Posting False "b" (Mixed [usd 1.00]) "" RegularPosting [] (Just t)
,Posting False "c" (Mixed [usd (-1.00)]) "" RegularPosting [] (Just t)
[posting{paccount="b", pamount=Mixed [usd 1.00], ptransaction=Just t}
,posting{paccount="c", pamount=Mixed [usd (-1.00)], ptransaction=Just t}
] ""
assertBool "detect balanced" (isTransactionBalanced Nothing t)
let t = Transaction (parsedate "2009/01/01") Nothing False "" "a" "" []
[Posting False "b" (Mixed [usd 1.00]) "" RegularPosting [] (Just t)
,Posting False "c" (Mixed [usd (-1.01)]) "" RegularPosting [] (Just t)
[posting{paccount="b", pamount=Mixed [usd 1.00], ptransaction=Just t}
,posting{paccount="c", pamount=Mixed [usd (-1.01)], ptransaction=Just t}
] ""
assertBool "detect unbalanced" (not $ isTransactionBalanced Nothing t)
let t = Transaction (parsedate "2009/01/01") Nothing False "" "a" "" []
[Posting False "b" (Mixed [usd 1.00]) "" RegularPosting [] (Just t)
[posting{paccount="b", pamount=Mixed [usd 1.00], ptransaction=Just t}
] ""
assertBool "detect unbalanced, one posting" (not $ isTransactionBalanced Nothing t)
let t = Transaction (parsedate "2009/01/01") Nothing False "" "a" "" []
[Posting False "b" (Mixed [usd 0]) "" RegularPosting [] (Just t)
[posting{paccount="b", pamount=Mixed [usd 0], ptransaction=Just t}
] ""
assertBool "one zero posting is considered balanced for now" (isTransactionBalanced Nothing t)
let t = Transaction (parsedate "2009/01/01") Nothing False "" "a" "" []
[Posting False "b" (Mixed [usd 1.00]) "" RegularPosting [] (Just t)
,Posting False "c" (Mixed [usd (-1.00)]) "" RegularPosting [] (Just t)
,Posting False "d" (Mixed [usd 100]) "" VirtualPosting [] (Just t)
[posting{paccount="b", pamount=Mixed [usd 1.00], ptransaction=Just t}
,posting{paccount="c", pamount=Mixed [usd (-1.00)], ptransaction=Just t}
,posting{paccount="d", pamount=Mixed [usd 100], ptype=VirtualPosting, ptransaction=Just t}
] ""
assertBool "virtual postings don't need to balance" (isTransactionBalanced Nothing t)
let t = Transaction (parsedate "2009/01/01") Nothing False "" "a" "" []
[Posting False "b" (Mixed [usd 1.00]) "" RegularPosting [] (Just t)
,Posting False "c" (Mixed [usd (-1.00)]) "" RegularPosting [] (Just t)
,Posting False "d" (Mixed [usd 100]) "" BalancedVirtualPosting [] (Just t)
[posting{paccount="b", pamount=Mixed [usd 1.00], ptransaction=Just t}
,posting{paccount="c", pamount=Mixed [usd (-1.00)], ptransaction=Just t}
,posting{paccount="d", pamount=Mixed [usd 100], ptype=BalancedVirtualPosting, ptransaction=Just t}
] ""
assertBool "balanced virtual postings need to balance among themselves" (not $ isTransactionBalanced Nothing t)
let t = Transaction (parsedate "2009/01/01") Nothing False "" "a" "" []
[Posting False "b" (Mixed [usd 1.00]) "" RegularPosting [] (Just t)
,Posting False "c" (Mixed [usd (-1.00)]) "" RegularPosting [] (Just t)
,Posting False "d" (Mixed [usd 100]) "" BalancedVirtualPosting [] (Just t)
,Posting False "e" (Mixed [usd (-100)]) "" BalancedVirtualPosting [] (Just t)
[posting{paccount="b", pamount=Mixed [usd 1.00], ptransaction=Just t}
,posting{paccount="c", pamount=Mixed [usd (-1.00)], ptransaction=Just t}
,posting{paccount="d", pamount=Mixed [usd 100], ptype=BalancedVirtualPosting, ptransaction=Just t}
,posting{paccount="3", pamount=Mixed [usd (-100)], ptype=BalancedVirtualPosting, ptransaction=Just t}
] ""
assertBool "balanced virtual postings need to balance among themselves (2)" (isTransactionBalanced Nothing t)

View File

@ -456,25 +456,9 @@ transactionFromCsvRecord rules fields =
tcomment=comment,
tpreceding_comment_lines=precomment,
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount=acct,
pamount=a,
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Just t
},
Posting {
pstatus=False,
paccount=baseacc,
pamount=(-baseamount),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Just t
}
tpostings=
[posting {paccount=acct, pamount=a, ptransaction=Just t}
,posting {paccount=baseacc, pamount=(-baseamount), ptransaction=Just t}
]
}
in t

View File

@ -969,26 +969,10 @@ Right samplejournal2 = journalBalanceTransactions $
tdescription="income",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="assets:bank:checking",
pamount=(Mixed [usd 1]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="income:salary",
pamount=(missingmixedamt),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
[posting {paccount="assets:bank:checking", pamount=Mixed [usd 1]}
,posting {paccount="income:salary", pamount=missingmixedamt}
],
tpreceding_comment_lines=""
}
]

View File

@ -342,26 +342,10 @@ journal7 = nulljournal {jtxns =
tdescription="opening balance",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="assets:cash",
pamount=(Mixed [usd 4.82]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="equity:opening balances",
pamount=(Mixed [usd (-4.82)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["assets:cash" `post` usd 4.82
,"equity:opening balances" `post` usd (-4.82)
],
tpreceding_comment_lines=""
}
,
@ -373,26 +357,10 @@ journal7 = nulljournal {jtxns =
tdescription="ayres suites",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="expenses:vacation",
pamount=(Mixed [usd 179.92]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:checking",
pamount=(Mixed [usd (-179.92)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["expenses:vacation" `post` usd 179.92
,"assets:checking" `post` usd (-179.92)
],
tpreceding_comment_lines=""
}
,
@ -404,26 +372,10 @@ journal7 = nulljournal {jtxns =
tdescription="auto transfer to savings",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="assets:saving",
pamount=(Mixed [usd 200]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:checking",
pamount=(Mixed [usd (-200)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["assets:saving" `post` usd 200
,"assets:checking" `post` usd (-200)
],
tpreceding_comment_lines=""
}
,
@ -435,26 +387,10 @@ journal7 = nulljournal {jtxns =
tdescription="poquito mas",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="expenses:food:dining",
pamount=(Mixed [usd 4.82]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:cash",
pamount=(Mixed [usd (-4.82)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["expenses:food:dining" `post` usd 4.82
,"assets:cash" `post` usd (-4.82)
],
tpreceding_comment_lines=""
}
,
@ -466,26 +402,10 @@ journal7 = nulljournal {jtxns =
tdescription="verizon",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="expenses:phone",
pamount=(Mixed [usd 95.11]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:checking",
pamount=(Mixed [usd (-95.11)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["expenses:phone" `post` usd 95.11
,"assets:checking" `post` usd (-95.11)
],
tpreceding_comment_lines=""
}
,
@ -497,26 +417,10 @@ journal7 = nulljournal {jtxns =
tdescription="discover",
tcomment="",
ttags=[],
tpostings=[
Posting {
pstatus=False,
paccount="liabilities:credit cards:discover",
pamount=(Mixed [usd 80]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
},
Posting {
pstatus=False,
paccount="assets:checking",
pamount=(Mixed [usd (-80)]),
pcomment="",
ptype=RegularPosting,
ptags=[],
ptransaction=Nothing
}
],
tpostings=
["liabilities:credit cards:discover" `post` usd 80
,"assets:checking" `post` usd (-80)
],
tpreceding_comment_lines=""
}
]