an Account now knows how many postings it has

This commit is contained in:
Simon Michael 2014-03-26 11:14:20 -07:00
parent 2844333857
commit d21085cc6e
2 changed files with 6 additions and 2 deletions

View File

@ -10,6 +10,7 @@ account, and subaccounting-excluding and -including balances.
module Hledger.Data.Account module Hledger.Data.Account
where where
import Data.List import Data.List
import Data.Maybe
import qualified Data.Map as M import qualified Data.Map as M
import Safe (headMay, lookupJustDef) import Safe (headMay, lookupJustDef)
import Test.HUnit import Test.HUnit
@ -44,6 +45,7 @@ nullacct = Account
{ aname = "" { aname = ""
, aparent = Nothing , aparent = Nothing
, asubs = [] , asubs = []
, anumpostings = 0
, aebalance = nullmixedamt , aebalance = nullmixedamt
, aibalance = nullmixedamt , aibalance = nullmixedamt
, aboring = False , aboring = False
@ -57,10 +59,12 @@ accountsFromPostings ps =
let let
acctamts = [(paccount p,pamount p) | p <- ps] acctamts = [(paccount p,pamount p) | p <- ps]
grouped = groupBy (\a b -> fst a == fst b) $ sort $ acctamts grouped = groupBy (\a b -> fst a == fst b) $ sort $ acctamts
counted = [(a, length acctamts) | acctamts@((a,_):_) <- grouped]
summed = map (\as@((aname,_):_) -> (aname, sum $ map snd as)) grouped -- always non-empty summed = map (\as@((aname,_):_) -> (aname, sum $ map snd as)) grouped -- always non-empty
nametree = treeFromPaths $ map (expandAccountName . fst) summed nametree = treeFromPaths $ map (expandAccountName . fst) summed
acctswithnames = nameTreeToAccount "root" nametree acctswithnames = nameTreeToAccount "root" nametree
acctswithebals = mapAccounts setebalance acctswithnames where setebalance a = a{aebalance=lookupJustDef nullmixedamt (aname a) summed} acctswithnumps = mapAccounts setnumps acctswithnames where setnumps a = a{anumpostings=fromMaybe 0 $ lookup (aname a) counted}
acctswithebals = mapAccounts setebalance acctswithnumps where setebalance a = a{aebalance=lookupJustDef nullmixedamt (aname a) summed}
acctswithibals = sumAccounts acctswithebals acctswithibals = sumAccounts acctswithebals
acctswithparents = tieAccountParents acctswithibals acctswithparents = tieAccountParents acctswithibals
acctsflattened = flattenAccounts acctswithparents acctsflattened = flattenAccounts acctswithparents

View File

@ -210,7 +210,7 @@ data Account = Account {
aname :: AccountName, -- ^ this account's full name aname :: AccountName, -- ^ this account's full name
aebalance :: MixedAmount, -- ^ this account's balance, excluding subaccounts aebalance :: MixedAmount, -- ^ this account's balance, excluding subaccounts
asubs :: [Account], -- ^ sub-accounts asubs :: [Account], -- ^ sub-accounts
-- anumpostings :: Int -- ^ number of postings to this account anumpostings :: Int, -- ^ number of postings to this account
-- derived from the above: -- derived from the above:
aibalance :: MixedAmount, -- ^ this account's balance, including subaccounts aibalance :: MixedAmount, -- ^ this account's balance, including subaccounts
aparent :: Maybe Account, -- ^ parent account aparent :: Maybe Account, -- ^ parent account