From 825b9ce5b342bf9cbf24c3003a43460e237c83b2 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Sat, 12 Oct 2019 21:22:40 +0100 Subject: [PATCH] lib: fixed amount vs amount1 conflict detection in csv parser --- hledger-lib/Hledger/Read/CsvReader.hs | 21 ++++++++++++-------- tests/csv.test | 28 ++++++++++++++++----------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index 7aa6670fc..f86e0cdc3 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -782,14 +782,19 @@ transactionFromCsvRecord sourcepos rules record = t amount = let al = pamount legacy a1 = pamount posting1 - in if al == a1 then al - else if isZeroMixedAmount a1 then al - else error' $ unlines [ "amount/amount-in/amount-out and amount1/amount1-in/amount1-out produced conflicting values" - , showRecord record - , showRules rules record - , "amount/amount-in/amount-out is " ++ show al - , "amount1/amount1-in/amount1-out is" ++ show a1 - ] + in + if al == a1 then al + else + case (isZeroMixedAmount al, isZeroMixedAmount a1) of + (True, _) -> a1 + (False, True) -> al + (False, False) -> + error' $ unlines [ "amount/amount-in/amount-out and amount1/amount1-in/amount1-out produced conflicting values" + , showRecord record + , showRules rules record + , "amount/amount-in/amount-out is " ++ showMixedAmount al + , "amount1/amount1-in/amount1-out is " ++ showMixedAmount a1 + ] in posting {paccount=paccount posting1, pamount=amount, ptransaction=Just t, pbalanceassertion=balanceassertion, pcomment = pcomment posting1} (Nothing, Nothing) -> error' $ unlines [ "sadly, no posting was generated for account1" , showRecord record diff --git a/tests/csv.test b/tests/csv.test index 16d531ba9..b3975578e 100644 --- a/tests/csv.test +++ b/tests/csv.test @@ -311,22 +311,28 @@ $ ./hledger-csv >=0 -# 16. use a script for cleaner csv tests? +# 16. Real life-ish paypal parsing example < -myacct,10/2009/09,Flubber Co,50, +"12/22/2018","06:22:50","PST","Someone","Subscription Payment","Completed","USD","10.00","-0.59","9.41","someone@some.where","simon@joyful.com","123456789","Joyful Systems","","9KCXZZZZZXXAX","","57.60","" RULES - -fields account1, date, description, amount-in, amount-out -date-format %d/%Y/%m +fields date, time, timezone, description, type, status_, currency, grossamount, feeamount, netamount, fromemail, toemail, code, itemtitle, itemid, referencetxnid, receiptid, balance, note +account1 sm:assets:online:paypal +amount1 %netamount +account2 sm:expenses:unknown +account3 JS:expenses:banking:paypal +amount3 %feeamount +balance %18 +code %13 currency $ -if Flubber - account1 assets:%account1 - amount-in (%amount-in) +date %1 +date-format %m/%d/%Y +description %description for %itemtitle $ ./hledger-csv -2009/09/10 Flubber Co - assets:myacct $-50 - income:unknown +2018/12/22 (123456789) Someone for Joyful Systems + sm:assets:online:paypal $9.41 = $57.60 + sm:expenses:unknown + JS:expenses:banking:paypal $-0.59 >=0