From e15eabdf1539a13223e2d676cbd5753670f99b05 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 25 Mar 2020 18:06:03 -0700 Subject: [PATCH] csv: ignore amount if any amountN's are assigned; improve error msg The change for hledger 1.17.1 broke one of my csv rules, where I used `amount` but then tried to override it with `amountN`s in a conditional block; the two clashed. Now in that situation any `amountN`s take precedence, causing `amount` to be ignored entirely. Also clarified the "too many non-zero amounts" error message a bit. --- hledger-lib/Hledger/Read/CsvReader.hs | 13 +++++++++---- hledger-lib/hledger_csv.m4.md | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index 1291f44e4..9a7385171 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -880,16 +880,21 @@ getAmount rules record currency p1IsVirtual n = -- flip the sign and convert to cost, as they did before 1.17 , let a' = if f `elem` unnumberedfieldnames && n==2 then costOfMixedAmount (-a) else a ] - in case nonzeroamounts of + -- if there's "amount" and "amountN"s, just discard the former + nonzeroamounts' + | length nonzeroamounts > 1 = filter ((/="amount").fst) nonzeroamounts + | otherwise = nonzeroamounts + in case nonzeroamounts' of [] -> Nothing [(f,a)] | "-out" `isSuffixOf` f -> Just (-a) -- for -out fields, flip the sign [(_,a)] -> Just a fs -> error' $ "more than one non-zero amount for this record, please ensure just one\n" - ++ unlines [" " ++ padright 11 f ++ ": " ++ showMixedAmount a - ++ " from rule: " ++ fromMaybe "" (hledgerField rules record f) - | (f,a) <- fs] ++ " " ++ showRecord record ++ "\n" + ++ unlines [" rule: " ++ f ++ " " ++ + fromMaybe "" (hledgerField rules record f) ++ + " amount parsed: " ++ showMixedAmount a -- XXX not sure this is showing all the right info + | (f,a) <- fs] where -- | Given a non-empty amount string to parse, along with a possibly -- non-empty currency symbol to prepend, parse as a hledger amount (as diff --git a/hledger-lib/hledger_csv.m4.md b/hledger-lib/hledger_csv.m4.md index 4409b8560..9546af348 100644 --- a/hledger-lib/hledger_csv.m4.md +++ b/hledger-lib/hledger_csv.m4.md @@ -434,6 +434,7 @@ Also, for compatibility with hledger <1.17: `amount` or `amount-in`/`amount-out` with no number sets the amount for postings 1 and 2. For posting 2 the amount is negated, and converted to cost if there's a [transaction price](journal.html#transaction-prices). +But if any numbered `amountN`/`amountN-in`/`amountN-out` fields are present, `amount` is ignored. #### currency