Skip to content

Commit

Permalink
csv: ignore amount if any amountN's are assigned; improve error msg
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simonmichael committed Mar 26, 2020
1 parent f574b78 commit e15eabd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions hledger-lib/Hledger/Read/CsvReader.hs
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions hledger-lib/hledger_csv.m4.md
Expand Up @@ -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

Expand Down

0 comments on commit e15eabd

Please sign in to comment.