lib: do not try to balance transaction in csv reader
This commit is contained in:
parent
95ec5715cc
commit
c66ccc5cee
@ -750,7 +750,7 @@ transactionFromCsvRecord sourcepos rules record = t
|
|||||||
case account of
|
case account of
|
||||||
Nothing -> Nothing
|
Nothing -> Nothing
|
||||||
Just account ->
|
Just account ->
|
||||||
Just $ (number, posting {paccount=account, pamount=fromMaybe missingmixedamt amount, ptransaction=Just t', pbalanceassertion=toAssertion <$> balance, pcomment = comment})
|
Just $ (number, posting {paccount=account, pamount=fromMaybe missingmixedamt amount, ptransaction=Just t, pbalanceassertion=toAssertion <$> balance, pcomment = comment})
|
||||||
|
|
||||||
parsePosting number =
|
parsePosting number =
|
||||||
parsePosting' number
|
parsePosting' number
|
||||||
@ -787,41 +787,33 @@ transactionFromCsvRecord sourcepos rules record = t
|
|||||||
, "amount/amount-in/amount-out is " ++ showMixedAmount al
|
, "amount/amount-in/amount-out is " ++ showMixedAmount al
|
||||||
, "amount1/amount1-in/amount1-out is " ++ showMixedAmount a1
|
, "amount1/amount1-in/amount1-out is " ++ showMixedAmount a1
|
||||||
]
|
]
|
||||||
in Just $ ("1", posting {paccount=paccount posting1, pamount=amount, ptransaction=Just t', pbalanceassertion=balanceassertion, pcomment = pcomment posting1})
|
in Just $ ("1", posting {paccount=paccount posting1, pamount=amount, ptransaction=Just t, pbalanceassertion=balanceassertion, pcomment = pcomment posting1})
|
||||||
(Nothing, Nothing) -> Nothing
|
(Nothing, Nothing) -> Nothing
|
||||||
postings' = catMaybes $ posting1:[ parsePosting i | x<-[2..9], let i = show x]
|
postings' = catMaybes $ posting1:[ parsePosting i | x<-[2..9], let i = show x]
|
||||||
|
|
||||||
|
improveUnknownAccountName p =
|
||||||
|
if paccount p /="unknown"
|
||||||
|
then p
|
||||||
|
else case isNegativeMixedAmount (pamount p) of
|
||||||
|
Just True -> p{paccount = "income:unknown"}
|
||||||
|
Just False -> p{paccount = "expenses:unknown"}
|
||||||
|
_ -> p
|
||||||
|
|
||||||
postings =
|
postings =
|
||||||
case postings' of
|
case postings' of
|
||||||
-- To be compatible with the behavior of the old code which allowed two postings only, we enforce
|
-- To be compatible with the behavior of the old code which allowed two postings only, we enforce
|
||||||
-- second posting when rules generated just first of them.
|
-- second posting when rules generated just first of them.
|
||||||
-- When we have srictly first and second posting, but second posting does not have amount, we fill it in.
|
-- When we have srictly first and second posting, but second posting does not have amount, we fill it in.
|
||||||
[("1",posting1)] -> [posting1,posting{paccount="unknown", pamount=costOfMixedAmount(-(pamount posting1)), ptransaction=Just t'}]
|
[("1",posting1)] ->
|
||||||
|
[posting1,improveUnknownAccountName (posting{paccount="unknown", pamount=costOfMixedAmount(-(pamount posting1)), ptransaction=Just t})]
|
||||||
[("1",posting1),("2",posting2)] ->
|
[("1",posting1),("2",posting2)] ->
|
||||||
case (pamount posting1 == missingmixedamt , pamount posting2 == missingmixedamt) of
|
case (pamount posting1 == missingmixedamt , pamount posting2 == missingmixedamt) of
|
||||||
(False, True) -> [posting1, posting2{pamount=costOfMixedAmount(-(pamount posting1))}]
|
(False, True) -> [posting1, improveUnknownAccountName (posting2{pamount=costOfMixedAmount(-(pamount posting1))})]
|
||||||
_ -> [posting1, posting2]
|
_ -> [posting1, posting2]
|
||||||
_ -> map snd postings'
|
_ -> map snd postings'
|
||||||
|
|
||||||
balanced = balanceTransaction Nothing t'
|
|
||||||
t =
|
|
||||||
case balanced of
|
|
||||||
Left _ -> t'
|
|
||||||
Right balanced ->
|
|
||||||
-- If we managed to balance transaction, lets infer better names for all "unknown" accounts
|
|
||||||
t' {tpostings =
|
|
||||||
[ originalPosting {paccount=newAccount}
|
|
||||||
| (originalPosting,p) <- zip postings (tpostings balanced)
|
|
||||||
, let account = paccount p
|
|
||||||
, let newAccount =
|
|
||||||
if account/="unknown"
|
|
||||||
then account
|
|
||||||
else case isNegativeMixedAmount (pamount p) of
|
|
||||||
Just True -> "income:unknown"
|
|
||||||
Just False -> "expenses:unknown"
|
|
||||||
_ -> "unknown"
|
|
||||||
]}
|
|
||||||
-- build the transaction
|
-- build the transaction
|
||||||
t' = nulltransaction{
|
t = nulltransaction{
|
||||||
tsourcepos = genericSourcePos sourcepos,
|
tsourcepos = genericSourcePos sourcepos,
|
||||||
tdate = date',
|
tdate = date',
|
||||||
tdate2 = mdate2',
|
tdate2 = mdate2',
|
||||||
|
|||||||
@ -217,9 +217,9 @@ account3 expenses:tax
|
|||||||
|
|
||||||
$ ./hledger-csv
|
$ ./hledger-csv
|
||||||
2009/09/10 Flubber Co
|
2009/09/10 Flubber Co
|
||||||
assets:myacct $50 = $321
|
assets:myacct $50 = $321
|
||||||
income:unknown = $123
|
unknown = $123
|
||||||
expenses:tax $0.234 ; VAT
|
expenses:tax $0.234 ; VAT
|
||||||
|
|
||||||
>=0
|
>=0
|
||||||
|
|
||||||
@ -346,8 +346,8 @@ amount %2
|
|||||||
date %1
|
date %1
|
||||||
date-format %Y/%m/%d
|
date-format %Y/%m/%d
|
||||||
$ ./hledger-csv | hledger balance -f - --no-total
|
$ ./hledger-csv | hledger balance -f - --no-total
|
||||||
$1,001.00 expenses:unknown
|
|
||||||
$-1,001.00 income:unknown
|
$-1,001.00 income:unknown
|
||||||
|
$1,001.00 unknown
|
||||||
>=0
|
>=0
|
||||||
|
|
||||||
# 18. Conditional skips
|
# 18. Conditional skips
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user