diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index 709e3ccc2..01568f5dd 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -664,9 +664,10 @@ transactionFromCsvRecord sourcepos rules record = t _ -> "expenses:unknown" account1 = T.pack $ maybe "" render (mfieldtemplate "account1") `or` defaccount1 account2 = T.pack $ maybe "" render (mfieldtemplate "account2") `or` defaccount2 - balance = maybe Nothing parsebalance $ mfieldtemplate "balance" - parsebalance "" = Nothing - parsebalance str = Just $ either (balanceerror str) id $ runParser (evalStateT (amountp <* eof) mempty) "" $ T.pack $ (currency++) $ negateIfParenthesised $ render str + balance = maybe Nothing (parsebalance.render) $ mfieldtemplate "balance" + parsebalance str + | all isSpace str = Nothing + | otherwise = Just $ either (balanceerror str) id $ runParser (evalStateT (amountp <* eof) mempty) "" $ T.pack $ (currency++) $ negateIfParenthesised str balanceerror str err = error' $ unlines ["error: could not parse \""++str++"\" as balance amount" ,showRecord record diff --git a/tests/csv/read.test b/tests/csv/read.test index f4f2bb113..9ad2986e0 100644 --- a/tests/csv/read.test +++ b/tests/csv/read.test @@ -67,4 +67,29 @@ >>>2 /using conversion rules file.*t.rules/ >>>=0 +# 7. read CSV with blank balance field + rm -rf t.rules$$; printf 'fields date, description, amount, balance\ndate-format %%d/%%Y/%%m\ncurrency $\naccount1 assets:myacct\n' >t.rules$$; echo '10/2009/09,Flubber Co,50,123\n11/2009/09,Blubber Co,60,' | hledger -f- print --rules-file t.rules$$; rm -rf t.rules$$ +>>> +2009/09/10 Flubber Co + income:unknown $-50 + assets:myacct $50 = $123 +2009/09/11 Blubber Co + income:unknown $-60 + assets:myacct $60 + +>>>2 /using conversion rules file.*t.rules/ +>>>=0 +# 8. read CSV with empty balance field + rm -rf t.rules$$; printf 'fields date, description, amount, balance\ndate-format %%d/%%Y/%%m\ncurrency $\naccount1 assets:myacct\n' >t.rules$$; echo '10/2009/09,Flubber Co,50,123\n11/2009/09,Blubber Co,60, ' | hledger -f- print --rules-file t.rules$$; rm -rf t.rules$$ +>>> +2009/09/10 Flubber Co + income:unknown $-50 + assets:myacct $50 = $123 + +2009/09/11 Blubber Co + income:unknown $-60 + assets:myacct $60 + +>>>2 /using conversion rules file.*t.rules/ +>>>=0