diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index e1b2ca6b4..6d0ec5c31 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -523,6 +523,7 @@ directives = ,"newest-first" -- ,"base-account" -- ,"base-currency" + , "balance-style" ] directivevalp :: CsvRulesParser String @@ -820,7 +821,21 @@ transactionFromCsvRecord sourcepos rules record = t tprecedingcomment = T.pack precomment, tpostings = postings } - toAssertion (a, b) = assertion{ + + defaultAssertion = + case mdirective "balance-style" of + Nothing -> assertion + Just "=" -> assertion + Just "==" -> assertion {batotal=True} + Just "=*" -> assertion {bainclusive=True} + Just "==*" -> assertion{batotal=True, bainclusive=True} + Just x -> error' $ unlines + [ "balance-style \"" ++ x ++"\" is invalid. Use =, ==, =* or ==*." + , showRecord record + , showRules rules record + ] + + toAssertion (a, b) = defaultAssertion{ baamount = a, baposition = b } diff --git a/tests/csv.test b/tests/csv.test index a88c7f33f..8b18de0b9 100644 --- a/tests/csv.test +++ b/tests/csv.test @@ -475,6 +475,24 @@ $ ./hledger-csv >=0 +# 22. read CSV with balance-style directive +< +10/2009/09,Flubber Co,50,123 + +RULES +fields date, description, amount, balance +date-format %d/%Y/%m +balance-style ==* +currency $ +account1 assets:myacct + +$ ./hledger-csv +2009/09/10 Flubber Co + assets:myacct $50 ==* $123 + income:unknown $-50 + +>=0 + # . TODO: without --separator gives obscure error # |