lib: do not try to balance transaction in csv reader

This commit is contained in:
Dmitry Astapov 2019-10-19 01:18:07 +01:00
parent 95ec5715cc
commit c66ccc5cee
2 changed files with 19 additions and 27 deletions

View File

@ -750,7 +750,7 @@ transactionFromCsvRecord sourcepos rules record = t
case account of
Nothing -> Nothing
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
@ -787,41 +787,33 @@ transactionFromCsvRecord sourcepos rules record = t
, "amount/amount-in/amount-out is " ++ showMixedAmount al
, "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
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 =
case postings' of
-- 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.
-- 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)] ->
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]
_ -> 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
t' = nulltransaction{
t = nulltransaction{
tsourcepos = genericSourcePos sourcepos,
tdate = date',
tdate2 = mdate2',

View File

@ -217,9 +217,9 @@ account3 expenses:tax
$ ./hledger-csv
2009/09/10 Flubber Co
assets:myacct $50 = $321
income:unknown = $123
expenses:tax $0.234 ; VAT
assets:myacct $50 = $321
unknown = $123
expenses:tax $0.234 ; VAT
>=0
@ -346,8 +346,8 @@ amount %2
date %1
date-format %Y/%m/%d
$ ./hledger-csv | hledger balance -f - --no-total
$1,001.00 expenses:unknown
$-1,001.00 income:unknown
$1,001.00 unknown
>=0
# 18. Conditional skips