lib: better compatibility code in csv reader

This commit is contained in:
Dmitry Astapov 2019-10-19 00:49:55 +01:00
parent d62f84bec2
commit 95ec5715cc
2 changed files with 18 additions and 19 deletions

View File

@ -750,7 +750,7 @@ transactionFromCsvRecord sourcepos rules record = t
case account of case account of
Nothing -> Nothing Nothing -> Nothing
Just account -> Just account ->
Just $ posting {paccount=account, pamount=fromMaybe missingmixedamt amount, ptransaction=Just t', pbalanceassertion=toAssertion <$> balance, pcomment = comment} Just $ (number, posting {paccount=account, pamount=fromMaybe missingmixedamt amount, ptransaction=Just t', pbalanceassertion=toAssertion <$> balance, pcomment = comment})
parsePosting number = parsePosting number =
parsePosting' number parsePosting' number
@ -765,9 +765,9 @@ transactionFromCsvRecord sourcepos rules record = t
posting1' = parsePosting "1" posting1' = parsePosting "1"
posting1 = posting1 =
case (postingLegacy,posting1') of case (postingLegacy,posting1') of
(Just legacy, Nothing) -> Just legacy (Just (_,legacy), Nothing) -> Just ("1", legacy)
(Nothing, Just posting1) -> Just posting1 (Nothing, Just (_,posting1)) -> Just ("1", posting1)
(Just legacy, Just posting1) -> (Just (_,legacy), Just (_,posting1)) ->
-- Here we merge legacy fields such as "amount" with "amount1", etc -- Here we merge legacy fields such as "amount" with "amount1", etc
-- Account and Comment would be the same by construction -- Account and Comment would be the same by construction
let balanceassertion = (pbalanceassertion legacy) `or` (pbalanceassertion posting1) let balanceassertion = (pbalanceassertion legacy) `or` (pbalanceassertion posting1)
@ -787,15 +787,20 @@ transactionFromCsvRecord sourcepos rules record = t
, "amount/amount-in/amount-out is " ++ showMixedAmount al , "amount/amount-in/amount-out is " ++ showMixedAmount al
, "amount1/amount1-in/amount1-out is " ++ showMixedAmount a1 , "amount1/amount1-in/amount1-out is " ++ showMixedAmount a1
] ]
in Just $ posting {paccount=paccount posting1, pamount=amount, ptransaction=Just t', pbalanceassertion=balanceassertion, pcomment = pcomment posting1} in Just $ ("1", posting {paccount=paccount posting1, pamount=amount, ptransaction=Just t', pbalanceassertion=balanceassertion, pcomment = pcomment posting1})
(Nothing, Nothing) -> Nothing (Nothing, Nothing) -> Nothing
postings' = catMaybes $ posting1:[ parsePosting i | x<-[2..9], let i = show x] postings' = catMaybes $ posting1:[ parsePosting i | x<-[2..9], let i = show x]
postings = postings =
case postings' of case postings' of
-- To be compatible with the behavior of the old code which allowed two postings only, we enforce -- To be compatible with the behavior of the old code which allowed two postings only, we enforce
-- second posting when rules generated just one of them. -- second posting when rules generated just first of them.
[posting1] -> [posting1,posting{paccount="unknown", pamount=missingmixedamt, ptransaction=Just t'}] -- When we have srictly first and second posting, but second posting does not have amount, we fill it in.
_ -> postings' [("1",posting1)] -> [posting1,posting{paccount="unknown", pamount=costOfMixedAmount(-(pamount posting1)), ptransaction=Just t'}]
[("1",posting1),("2",posting2)] ->
case (pamount posting1 == missingmixedamt , pamount posting2 == missingmixedamt) of
(False, True) -> [posting1, posting2{pamount=costOfMixedAmount(-(pamount posting1))}]
_ -> [posting1, posting2]
_ -> map snd postings'
balanced = balanceTransaction Nothing t' balanced = balanceTransaction Nothing t'
t = t =
@ -804,7 +809,7 @@ transactionFromCsvRecord sourcepos rules record = t
Right balanced -> Right balanced ->
-- If we managed to balance transaction, lets infer better names for all "unknown" accounts -- If we managed to balance transaction, lets infer better names for all "unknown" accounts
t' {tpostings = t' {tpostings =
[ originalPosting {paccount=newAccount, pamount=newAmount} [ originalPosting {paccount=newAccount}
| (originalPosting,p) <- zip postings (tpostings balanced) | (originalPosting,p) <- zip postings (tpostings balanced)
, let account = paccount p , let account = paccount p
, let newAccount = , let newAccount =
@ -814,11 +819,6 @@ transactionFromCsvRecord sourcepos rules record = t
Just True -> "income:unknown" Just True -> "income:unknown"
Just False -> "expenses:unknown" Just False -> "expenses:unknown"
_ -> "unknown" _ -> "unknown"
, let newAmount =
if pamount originalPosting == missingmixedamt &&
pamount p /= missingmixedamt
then pamount p
else pamount originalPosting
]} ]}
-- build the transaction -- build the transaction
t' = nulltransaction{ t' = nulltransaction{

View File

@ -218,7 +218,7 @@ account3 expenses:tax
$ ./hledger-csv $ ./hledger-csv
2009/09/10 Flubber Co 2009/09/10 Flubber Co
assets:myacct $50 = $321 assets:myacct $50 = $321
income:unknown $-50.234 = $123 income:unknown = $123
expenses:tax $0.234 ; VAT expenses:tax $0.234 ; VAT
>=0 >=0
@ -237,8 +237,7 @@ account3 expenses:tax
$ ./hledger-csv $ ./hledger-csv
2009/09/10 Flubber Co 2009/09/10 Flubber Co
assets:myacct $50 = $321 assets:myacct $50 = $321
unknown $-50 = $123 unknown = $123
unknown £-0.234 = $123
expenses:tax £0.234 ; VAT expenses:tax £0.234 ; VAT
>=0 >=0
@ -332,7 +331,7 @@ description %description for %itemtitle
$ ./hledger-csv $ ./hledger-csv
2018/12/22 (123456789) Someone for Joyful Systems 2018/12/22 (123456789) Someone for Joyful Systems
sm:assets:online:paypal $9.41 = $57.60 sm:assets:online:paypal $9.41 = $57.60
sm:expenses:unknown $-8.82 sm:expenses:unknown
JS:expenses:banking:paypal $-0.59 JS:expenses:banking:paypal $-0.59
>=0 >=0
@ -475,7 +474,7 @@ if Conditional Empty Fee
$ ./hledger-csv $ ./hledger-csv
2018/12/22 (123456789) Someone for Joyful Systems 2018/12/22 (123456789) Someone for Joyful Systems
sm:assets:online:paypal $9.41 = $57.60 sm:assets:online:paypal $9.41 = $57.60
sm:expenses:unknown $-8.82 sm:expenses:unknown
JS:expenses:banking:paypal $-0.59 JS:expenses:banking:paypal $-0.59
2018/12/22 (987654321) Someone for Joyful Systems 2018/12/22 (987654321) Someone for Joyful Systems