parsing: balance real and balanced virtual postings separately, allow an amount-less balanced virtual posting
This commit is contained in:
parent
7b5f4f33dd
commit
5451e3ad40
@ -126,19 +126,22 @@ isTransactionBalanced t = isReallyZeroMixedAmountCost rsum && isReallyZeroMixedA
|
|||||||
-- return an error message instead.
|
-- return an error message instead.
|
||||||
balanceTransaction :: Transaction -> Either String Transaction
|
balanceTransaction :: Transaction -> Either String Transaction
|
||||||
balanceTransaction t@Transaction{tpostings=ps}
|
balanceTransaction t@Transaction{tpostings=ps}
|
||||||
| length missingamounts' > 1 = Left $ printerr "could not balance this transaction (too many missing amounts)"
|
| length rwithoutamounts > 1 || length bvwithoutamounts > 1
|
||||||
|
= Left $ printerr "could not balance this transaction (too many missing amounts)"
|
||||||
| not $ isTransactionBalanced t' = Left $ printerr $ nonzerobalanceerror t'
|
| not $ isTransactionBalanced t' = Left $ printerr $ nonzerobalanceerror t'
|
||||||
| otherwise = Right t'
|
| otherwise = Right t'
|
||||||
where
|
where
|
||||||
(withamounts, missingamounts) = partition hasAmount $ filter isReal ps
|
rps = filter isReal ps
|
||||||
(_, missingamounts') = partition hasAmount ps
|
bvps = filter isBalancedVirtual ps
|
||||||
t' = t{tpostings=ps'}
|
(rwithamounts, rwithoutamounts) = partition hasAmount rps
|
||||||
ps' | length missingamounts == 1 = map balance ps
|
(bvwithamounts, bvwithoutamounts) = partition hasAmount bvps
|
||||||
| otherwise = ps
|
t' = t{tpostings=map balance ps}
|
||||||
where
|
where
|
||||||
balance p | isReal p && not (hasAmount p) = p{pamount = costOfMixedAmount (-otherstotal)}
|
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))}
|
||||||
| otherwise = p
|
| otherwise = p
|
||||||
where otherstotal = sum $ map pamount withamounts
|
|
||||||
printerr s = intercalate "\n" [s, showTransactionUnelided t]
|
printerr s = intercalate "\n" [s, showTransactionUnelided t]
|
||||||
|
|
||||||
nonzerobalanceerror :: Transaction -> String
|
nonzerobalanceerror :: Transaction -> String
|
||||||
|
|||||||
@ -1,9 +0,0 @@
|
|||||||
# again, complain like ledger, but we could handle this
|
|
||||||
./hledger -f - print
|
|
||||||
<<<
|
|
||||||
2009/1/1 x
|
|
||||||
a 1
|
|
||||||
b
|
|
||||||
(c)
|
|
||||||
(d)
|
|
||||||
>>>2 /too many missing/
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
# could balance this, but complain instead like ledger
|
|
||||||
./hledger -f - register
|
|
||||||
<<<
|
|
||||||
2009/6/24 carwash
|
|
||||||
equity:draw:personal:transportation:car:carwash $3.50
|
|
||||||
assets:cash
|
|
||||||
[expenses:car] $3.50
|
|
||||||
[simon]
|
|
||||||
>>>2 /too many missing/
|
|
||||||
@ -5,24 +5,44 @@
|
|||||||
(virtual) 100
|
(virtual) 100
|
||||||
a 1
|
a 1
|
||||||
b
|
b
|
||||||
|
#
|
||||||
# 2. balanced virtual postings should be required to balance (themselves)
|
# 2. balanced virtual postings should be required to balance (themselves)
|
||||||
./hledger -f-
|
./hledger -f-
|
||||||
<<<
|
<<<
|
||||||
2010/1/1 x
|
2010/1/1 x
|
||||||
[balanced virtual] 100
|
[balanced virtual] 10
|
||||||
a 1
|
a 1
|
||||||
b
|
b
|
||||||
>>>= !0
|
>>>= !0
|
||||||
|
#
|
||||||
|
# 3. balanced virtual postings should be required to balance (themselves)
|
||||||
./hledger -f-
|
./hledger -f-
|
||||||
<<<
|
<<<
|
||||||
2010/1/1 x
|
2010/1/1 x
|
||||||
[balanced virtual] 100
|
[balanced virtual] 10
|
||||||
[balanced virtual] -100
|
[balanced virtual] -10
|
||||||
a 1
|
a 1
|
||||||
b
|
b
|
||||||
# 3. a virtual posting with implicit amount should be handled correctly
|
#
|
||||||
|
# 4. a virtual posting with implicit amount should be handled correctly
|
||||||
./hledger -f-
|
./hledger -f-
|
||||||
<<<
|
<<<
|
||||||
2010/1/1 x
|
2010/1/1 x
|
||||||
[a] 1
|
[a] 10
|
||||||
[b]
|
[b]
|
||||||
|
#
|
||||||
|
# 5. real and balanced virtual postings are balanced separately, and multiple blank virtuals are ok
|
||||||
|
./hledger -f-
|
||||||
|
<<<
|
||||||
|
2010/1/1 x
|
||||||
|
a 1
|
||||||
|
b
|
||||||
|
[e] 10
|
||||||
|
[f]
|
||||||
|
(c)
|
||||||
|
(d)
|
||||||
|
>>>
|
||||||
|
1 a
|
||||||
|
-1 b
|
||||||
|
10 e
|
||||||
|
-10 f
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user