lib: allow more account directive info in Account records

This commit is contained in:
Simon Michael 2019-01-14 03:44:51 -08:00
parent 899946f270
commit 90bf354566
2 changed files with 53 additions and 28 deletions

View File

@ -45,14 +45,14 @@ instance Eq Account where
-- ] -- ]
nullacct = Account nullacct = Account
{ aname = "" { aname = ""
, adeclarationorder = Nothing , adeclarationinfo = Nothing
, aparent = Nothing , asubs = []
, asubs = [] , aparent = Nothing
, anumpostings = 0 , aboring = False
, aebalance = nullmixedamt , anumpostings = 0
, aibalance = nullmixedamt , aebalance = nullmixedamt
, aboring = False , aibalance = nullmixedamt
} }
-- | Derive 1. an account tree and 2. each account's total exclusive -- | Derive 1. an account tree and 2. each account's total exclusive
@ -83,7 +83,11 @@ accountTree :: AccountName -> [AccountName] -> Account
accountTree rootname as = nullacct{aname=rootname, asubs=map (uncurry accountTree') $ M.assocs m } accountTree rootname as = nullacct{aname=rootname, asubs=map (uncurry accountTree') $ M.assocs m }
where where
T m = treeFromPaths $ map expandAccountName as :: FastTree AccountName T m = treeFromPaths $ map expandAccountName as :: FastTree AccountName
accountTree' a (T m) = nullacct{aname=a, asubs=map (uncurry accountTree') $ M.assocs m} accountTree' a (T m) =
nullacct{
aname=a
,asubs=map (uncurry accountTree') $ M.assocs m
}
-- | Tie the knot so all subaccounts' parents are set correctly. -- | Tie the knot so all subaccounts' parents are set correctly.
tieAccountParents :: Account -> Account tieAccountParents :: Account -> Account
@ -204,12 +208,17 @@ sortAccountTreeByAmount normalsign a
maybeflip | normalsign==NormallyNegative = id maybeflip | normalsign==NormallyNegative = id
| otherwise = flip | otherwise = flip
-- | Look up an account's declaration order, if any, from the Journal and set it. -- | Add extra info for this account derived from the Journal's
-- This is the relative position of its account directive -- account directives, if any (comment, tags, declaration order..).
-- among the other account directives. -- Currently only sets declaration order.
accountSetDeclarationOrder :: Journal -> Account -> Account -- Expects that this account is among the Journal's jdeclaredaccounts
accountSetDeclarationOrder j a@Account{..} = -- (otherwise sets declaration order to 0).
a{adeclarationorder = findIndex (==aname) (jdeclaredaccounts j)} accountSetDeclarationInfo :: Journal -> Account -> Account
accountSetDeclarationInfo j a@Account{..} =
a{adeclarationinfo=Just nullaccountdeclarationinfo{
adideclarationorder = fromMaybe 0 $ findIndex (==aname) (jdeclaredaccounts j)
}
}
-- | Sort account names by the order in which they were declared in -- | Sort account names by the order in which they were declared in
-- the journal, at each level of the account tree (ie within each -- the journal, at each level of the account tree (ie within each
@ -227,7 +236,7 @@ sortAccountNamesByDeclaration j keepparents as =
drop 1 $ -- drop the root node that was added drop 1 $ -- drop the root node that was added
flattenAccounts $ -- convert to an account list flattenAccounts $ -- convert to an account list
sortAccountTreeByDeclaration $ -- sort by declaration order (and name) sortAccountTreeByDeclaration $ -- sort by declaration order (and name)
mapAccounts (accountSetDeclarationOrder j) $ -- add declaration order info mapAccounts (accountSetDeclarationInfo j) $ -- add declaration order info
accountTree "root" -- convert to an account tree accountTree "root" -- convert to an account tree
as as
@ -243,9 +252,10 @@ sortAccountTreeByDeclaration a
map sortAccountTreeByDeclaration $ asubs a map sortAccountTreeByDeclaration $ asubs a
} }
accountDeclarationOrderAndName :: Account -> (Int, AccountName)
accountDeclarationOrderAndName a = (adeclarationorder', aname a) accountDeclarationOrderAndName a = (adeclarationorder', aname a)
where where
adeclarationorder' = fromMaybe maxBound (adeclarationorder a) adeclarationorder' = maybe maxBound adideclarationorder $ adeclarationinfo a
-- | Search an account list by name. -- | Search an account list by name.
lookupAccount :: AccountName -> [Account] -> Maybe Account lookupAccount :: AccountName -> [Account] -> Maybe Account

View File

@ -439,18 +439,33 @@ type ParsedJournal = Journal
-- The --output-format option selects one of these for output. -- The --output-format option selects one of these for output.
type StorageFormat = String type StorageFormat = String
-- | An account, with name, balances and links to parent/subaccounts -- | Extra information about an account that can be derived from
-- which let you walk up or down the account tree. -- its account directive (and the other account directives).
data AccountDeclarationInfo = AccountDeclarationInfo {
adicomment :: Text -- ^ any comment lines following an account directive for this account
,aditags :: [Tag] -- ^ tags extracted from the account comment, if any
,adideclarationorder :: Int -- ^ the relative position of this account's account directive, if any. Normally a natural number.
} deriving (Data)
nullaccountdeclarationinfo = AccountDeclarationInfo {
adicomment = ""
,aditags = []
,adideclarationorder = 0
}
-- | An account, with its balances, parent/subaccount relationships, etc.
-- Only the name is required; the other fields are added when needed.
data Account = Account { data Account = Account {
aname :: AccountName, -- ^ this account's full name aname :: AccountName -- ^ this account's full name
adeclarationorder :: Maybe Int , -- ^ the relative position of this account's account directive, if any. Normally a natural number. ,adeclarationinfo :: Maybe AccountDeclarationInfo -- ^ optional extra info from account directives
aebalance :: MixedAmount, -- ^ this account's balance, excluding subaccounts -- relationships in the tree
asubs :: [Account], -- ^ sub-accounts ,asubs :: [Account] -- ^ this account's sub-accounts
anumpostings :: Int, -- ^ number of postings to this account ,aparent :: Maybe Account -- ^ parent account
-- derived from the above : ,aboring :: Bool -- ^ used in the accounts report to label elidable parents
aibalance :: MixedAmount, -- ^ this account's balance, including subaccounts -- balance information
aparent :: Maybe Account, -- ^ parent account ,anumpostings :: Int -- ^ the number of postings to this account
aboring :: Bool -- ^ used in the accounts report to label elidable parents ,aebalance :: MixedAmount -- ^ this account's balance, excluding subaccounts
,aibalance :: MixedAmount -- ^ this account's balance, including subaccounts
} deriving (Typeable, Data, Generic) } deriving (Typeable, Data, Generic)
-- | Whether an account's balance is normally a positive number (in -- | Whether an account's balance is normally a positive number (in