lib: more robust "unknown" account assignment in csv parser
This commit is contained in:
parent
28ca65b99a
commit
e4476dd2f1
@ -729,22 +729,35 @@ transactionFromCsvRecord sourcepos rules record = t
|
|||||||
unknownAccountForAmount amt =
|
unknownAccountForAmount amt =
|
||||||
case isNegativeMixedAmount amt of
|
case isNegativeMixedAmount amt of
|
||||||
Just True -> "income:unknown"
|
Just True -> "income:unknown"
|
||||||
_ -> "expense:unknown"
|
Just False -> "expense:unknown"
|
||||||
|
_ -> "unknown"
|
||||||
|
|
||||||
parsePosting' number accountFld amtForUnknownAccount amountFld amountInFld amountOutFld balanceFld commentFld =
|
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")
|
(mfieldtemplate ("currency"++number) `or `mfieldtemplate "currency")
|
||||||
amount = chooseAmountStr rules record currency amountFld amountInFld amountOutFld
|
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` mdirective ("default-account" ++ number)))
|
||||||
`or` (unknownAccountForAmount <$> amtForUnknownAccount)
|
`or` (unknownAccountForAmount <$> amtForUnknownAccount)
|
||||||
balance = (parsebalance currency number.render) =<< mfieldtemplate balanceFld
|
balance = (parsebalance currency number.render) =<< mfieldtemplate balanceFld
|
||||||
comment = T.pack $ maybe "" render $ mfieldtemplate commentFld
|
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
|
in
|
||||||
case account of
|
case account of
|
||||||
Nothing -> Nothing
|
Nothing -> Nothing
|
||||||
Just account ->
|
Just account ->
|
||||||
Just $ posting {paccount=account, pamount=fromMaybe nullmixedamt amount, ptransaction=Just t, pbalanceassertion=toAssertion <$> balance, pcomment = comment}
|
Just $ posting {paccount=account, pamount=fromMaybe nullmixedamt amount, ptransaction=Just t, pbalanceassertion=toAssertion <$> balance, pcomment = comment}
|
||||||
|
|
||||||
parsePosting number =
|
parsePosting number =
|
||||||
parsePosting' number
|
parsePosting' number
|
||||||
|
|||||||
@ -114,18 +114,18 @@ $ printf 'fields date, description, amount, balance1, balance2\ndate-format %%d
|
|||||||
# 11. More than two postings
|
# 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
|
$ 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
|
2009/09/10 Flubber Co
|
||||||
assets:myacct $50 = $321
|
assets:myacct $50 = $321
|
||||||
expense:unknown = $123
|
unknown = $123
|
||||||
expenses:tax $0.234 ; VAT
|
expenses:tax $0.234 ; VAT
|
||||||
|
|
||||||
>=0
|
>=0
|
||||||
|
|
||||||
# 12. More than two postings and different currencies
|
# 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
|
$ 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
|
2009/09/10 Flubber Co
|
||||||
assets:myacct $50 = $321
|
assets:myacct $50 = $321
|
||||||
expense:unknown = $123
|
unknown = $123
|
||||||
expenses:tax £0.234 ; VAT
|
expenses:tax £0.234 ; VAT
|
||||||
|
|
||||||
>=0
|
>=0
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user