lib: more robust "unknown" account assignment in csv parser

This commit is contained in:
Dmitry Astapov 2019-10-12 09:30:25 +01:00
parent 28ca65b99a
commit e4476dd2f1
2 changed files with 27 additions and 14 deletions

View File

@ -729,22 +729,35 @@ transactionFromCsvRecord sourcepos rules record = t
unknownAccountForAmount amt =
case isNegativeMixedAmount amt of
Just True -> "income:unknown"
_ -> "expense:unknown"
Just False -> "expense:unknown"
_ -> "unknown"
parsePosting' number accountFld amtForUnknownAccount amountFld amountInFld amountOutFld balanceFld commentFld =
let currency = maybe (fromMaybe "" mdefaultcurrency) render $
let currency = maybe (fromMaybe "" mdefaultcurrency) render $
(mfieldtemplate ("currency"++number) `or `mfieldtemplate "currency")
amount = chooseAmountStr rules record currency amountFld amountInFld amountOutFld
account = ((T.pack . render) <$> (mfieldtemplate accountFld
account' = ((T.pack . render) <$> (mfieldtemplate accountFld
`or` mdirective ("default-account" ++ number)))
`or` (unknownAccountForAmount <$> amtForUnknownAccount)
balance = (parsebalance currency number.render) =<< mfieldtemplate balanceFld
comment = T.pack $ maybe "" render $ mfieldtemplate commentFld
balance = (parsebalance currency number.render) =<< mfieldtemplate balanceFld
comment = T.pack $ maybe "" render $ mfieldtemplate commentFld
account =
case account' of
Just account -> Just account
Nothing ->
-- If we have amount or balance assertion (which implies potential amount change),
-- but no account name, lets generate "unknown" account name.
-- If we can figure out whether this is income or expense based on amount, do that
-- otherwise stick to "unknown"
case (amount, balance) of
(Just amt, _ ) -> Just $ unknownAccountForAmount amt
(_, Just _) -> Just "unknown"
(Nothing, Nothing) -> Nothing
in
case account of
Nothing -> Nothing
Just account ->
Just $ posting {paccount=account, pamount=fromMaybe nullmixedamt amount, ptransaction=Just t, pbalanceassertion=toAssertion <$> balance, pcomment = comment}
Nothing -> Nothing
Just account ->
Just $ posting {paccount=account, pamount=fromMaybe nullmixedamt amount, ptransaction=Just t, pbalanceassertion=toAssertion <$> balance, pcomment = comment}
parsePosting number =
parsePosting' number

View File

@ -114,18 +114,18 @@ $ printf 'fields date, description, amount, balance1, balance2\ndate-format %%d
# 11. More than two postings
$ printf 'fields date, description, amount, balance1, balance2, amount3,comment3\ndate-format %%d/%%Y/%%m\ncurrency $\naccount1 assets:myacct\naccount3 expenses:tax\n' >t.$$.csv.rules; printf '10/2009/09,Flubber Co,50,321,123,0.234,VAT\n' | hledger -f csv:- --rules-file t.$$.csv.rules print && rm -rf t.$$.csv.rules
2009/09/10 Flubber Co
assets:myacct $50 = $321
expense:unknown = $123
expenses:tax $0.234 ; VAT
assets:myacct $50 = $321
unknown = $123
expenses:tax $0.234 ; VAT
>=0
# 12. More than two postings and different currencies
$ printf 'fields date, description, amount, balance1, balance2, currency3, amount3,comment3\ndate-format %%d/%%Y/%%m\ncurrency $\naccount1 assets:myacct\naccount3 expenses:tax\n' >t.$$.csv.rules; printf '10/2009/09,Flubber Co,50,321,123,£,0.234,VAT\n' | hledger -f csv:- --rules-file t.$$.csv.rules print && rm -rf t.$$.csv.rules
2009/09/10 Flubber Co
assets:myacct $50 = $321
expense:unknown = $123
expenses:tax £0.234 ; VAT
assets:myacct $50 = $321
unknown = $123
expenses:tax £0.234 ; VAT
>=0