fix: csv: Handle multiple zero amounts in postings in csv files. (#1733)

This commit is contained in:
Stephen Morgan 2021-11-17 10:12:55 +11:00 committed by Simon Michael
parent c6beb581a1
commit 87a7a586d4
2 changed files with 37 additions and 21 deletions

View File

@ -996,33 +996,31 @@ getAmount rules record currency p1IsVirtual n =
] ]
-- if any of the numbered field names are present, discard all the unnumbered ones -- if any of the numbered field names are present, discard all the unnumbered ones
assignments' | any isnumbered assignments = filter isnumbered assignments discardUnnumbered xs = if null numbered then xs else numbered
| otherwise = assignments
where where
isnumbered (f,_) = T.any isDigit f numbered = filter (T.any isDigit . fst) xs
-- if there's more than one value and only some are zeros, discard the zeros -- discard all zero amounts, unless all amounts are zero, in which case discard all but the first
assignments'' discardExcessZeros xs = if null nonzeros then take 1 xs else nonzeros
| length assignments' > 1 && not (null nonzeros) = nonzeros where
| otherwise = assignments' nonzeros = filter (not . mixedAmountLooksZero . snd) xs
where nonzeros = filter (not . mixedAmountLooksZero . snd) assignments'
in case -- dbg0 ("amounts for posting "++show n) -- for -out fields, flip the sign XXX unless it's already negative ? back compat issues / too confusing ?
assignments'' of negateIfOut f = if "-out" `T.isSuffixOf` f then maNegate else id
[] -> Nothing
[(f,a)] | "-out" `T.isSuffixOf` f -> Just (maNegate a) -- for -out fields, flip the sign in case discardExcessZeros $ discardUnnumbered assignments of
-- XXX unless it's already negative ? back compat issues / too confusing ? [] -> Nothing
[(_,a)] -> Just a [(f,a)] -> Just $ negateIfOut f a
fs -> error' . T.unpack . T.unlines $ [ -- PARTIAL: fs -> error' . T.unpack . T.unlines $ -- PARTIAL:
"multiple non-zero amounts or multiple zero amounts assigned," ["multiple non-zero amounts assigned,"
,"please ensure just one. (https://hledger.org/csv.html#amount)" ,"please ensure just one. (https://hledger.org/csv.html#amount)"
," " <> showRecord record ," " <> showRecord record
," for posting: " <> T.pack (show n) ," for posting: " <> T.pack (show n)
] ] ++
++ [" assignment: " <> f <> " " <> [" assignment: " <> f <> " " <>
fromMaybe "" (hledgerField rules record f) <> fromMaybe "" (hledgerField rules record f) <>
"\t=> value: " <> wbToText (showMixedAmountB noColour a) -- XXX not sure this is showing all the right info "\t=> value: " <> wbToText (showMixedAmountB noColour a) -- XXX not sure this is showing all the right info
| (f,a) <- fs] | (f,a) <- fs]
-- | Figure out the expected balance (assertion or assignment) specified for posting N, -- | Figure out the expected balance (assertion or assignment) specified for posting N,
-- if any (and its parse position). -- if any (and its parse position).

View File

@ -996,6 +996,24 @@ $ ./csvtest.sh
>=0 >=0
# 49. Handle an entry with all zeros
<
Date;Description;Category;Debit;Credit;Balance
"2020-01-21","Client card point of sale fee",Fees,"0","0","1068.94"
RULES
skip 1
fields date, description, category, amount-out, amount-in, balance
currency $
account1 assets:bank:checking
$ ./csvtest.sh
2020-01-21 Client card point of sale fee
assets:bank:checking 0 = $1068.94
expenses:unknown 0
>=0
## . ## .
#< #<
#$ ./csvtest.sh #$ ./csvtest.sh