fix: Make sure cash and conversion account types are correctly

determined when there are no explicit declarations.
This commit is contained in:
Stephen Morgan 2022-02-03 15:38:45 +11:00 committed by Simon Michael
parent 36fe6bbcff
commit a16c88b1b1
2 changed files with 35 additions and 13 deletions

View File

@ -349,20 +349,22 @@ journalAddAccountTypes j = j{jaccounttypes = journalAccountTypes j}
-- | Build a map of all known account types, explicitly declared -- | Build a map of all known account types, explicitly declared
-- or inferred from the account's parent or name. -- or inferred from the account's parent or name.
journalAccountTypes :: Journal -> M.Map AccountName AccountType journalAccountTypes :: Journal -> M.Map AccountName AccountType
journalAccountTypes j = journalAccountTypes j = M.fromList [(a,acctType) | (a, Just (acctType,_)) <- flatten t']
let
t = accountNameTreeFrom $ journalAccountNames j :: Tree AccountName
t' = settypes Nothing t :: Tree (AccountName, Maybe AccountType)
in
M.fromList [(a,t) | (a, Just t) <- flatten t']
where where
-- Map from the top of the account tree down to the leaves, t = accountNameTreeFrom $ journalAccountNames j :: Tree AccountName
-- propagating account types downward. t' = settypes Nothing t :: Tree (AccountName, Maybe (AccountType, Bool))
settypes :: Maybe AccountType -> Tree AccountName -> Tree (AccountName, Maybe AccountType) -- Map from the top of the account tree down to the leaves, propagating
settypes mparenttype (Node a subs) = -- account types downward. Keep track of whether the account is declared
let mtype = M.lookup a declaredtypes <|> mparenttype <|> accountNameInferType a -- (True), in which case the parent account should be preferred, or merely
in Node (a, mtype) (map (settypes mtype) subs) -- inferred (False), in which case the inferred type should be preferred.
declaredtypes = journalDeclaredAccountTypes j settypes :: Maybe (AccountType, Bool) -> Tree AccountName -> Tree (AccountName, Maybe (AccountType, Bool))
settypes mparenttype (Node a subs) = Node (a, mtype) (map (settypes mtype) subs)
where
mtype = M.lookup a declaredtypes <|> minferred
minferred = if maybe False snd mparenttype
then mparenttype
else (,False) <$> accountNameInferType a <|> mparenttype
declaredtypes = (,True) <$> journalDeclaredAccountTypes j
-- | Build a map of the account types explicitly declared. -- | Build a map of the account types explicitly declared.
journalDeclaredAccountTypes :: Journal -> M.Map AccountName AccountType journalDeclaredAccountTypes :: Journal -> M.Map AccountName AccountType

View File

@ -75,3 +75,23 @@ account liabilities
$ hledger -f- accounts type:a $ hledger -f- accounts type:a
assets assets
# 11. type: can identify cash accounts in the default case
<
account assets
account assets:cash
$ hledger -f- accounts type:c
assets:cash
# 12. type: can identify conversion accounts in the default case
<
account equity
account equity:conversion
account equity:trading
account equity:trade
$ hledger -f- accounts type:v
equity:conversion
equity:trading
equity:trade