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.
This commit is contained in:
Simon Michael 2020-03-25 18:06:03 -07:00
parent f574b782fa
commit e15eabdf15
2 changed files with 10 additions and 4 deletions

View File

@ -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

View File

@ -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