From c66ccc5cee27ea4348f6d0f03bc677b26be04077 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Sat, 19 Oct 2019 01:18:07 +0100 Subject: [PATCH] lib: do not try to balance transaction in csv reader --- hledger-lib/Hledger/Read/CsvReader.hs | 38 +++++++++++---------------- tests/csv.test | 8 +++--- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index f66e4ff17..b4568c899 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -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', diff --git a/tests/csv.test b/tests/csv.test index ca99ac413..dfb5f1e47 100644 --- a/tests/csv.test +++ b/tests/csv.test @@ -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