From d21085cc6e470e28aa91a6ce8f1c8100635ce65d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 26 Mar 2014 11:14:20 -0700 Subject: [PATCH] an Account now knows how many postings it has --- hledger-lib/Hledger/Data/Account.hs | 6 +++++- hledger-lib/Hledger/Data/Types.hs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Data/Account.hs b/hledger-lib/Hledger/Data/Account.hs index f67372853..0d35fa51b 100644 --- a/hledger-lib/Hledger/Data/Account.hs +++ b/hledger-lib/Hledger/Data/Account.hs @@ -10,6 +10,7 @@ account, and subaccounting-excluding and -including balances. module Hledger.Data.Account where import Data.List +import Data.Maybe import qualified Data.Map as M import Safe (headMay, lookupJustDef) import Test.HUnit @@ -44,6 +45,7 @@ nullacct = Account { aname = "" , aparent = Nothing , asubs = [] + , anumpostings = 0 , aebalance = nullmixedamt , aibalance = nullmixedamt , aboring = False @@ -57,10 +59,12 @@ accountsFromPostings ps = let acctamts = [(paccount p,pamount p) | p <- ps] 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 nametree = treeFromPaths $ map (expandAccountName . fst) summed 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 acctswithparents = tieAccountParents acctswithibals acctsflattened = flattenAccounts acctswithparents diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index 4ef19d8dc..68faf4ad8 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -210,7 +210,7 @@ data Account = Account { aname :: AccountName, -- ^ this account's full name aebalance :: MixedAmount, -- ^ this account's balance, excluding subaccounts asubs :: [Account], -- ^ sub-accounts - -- anumpostings :: Int -- ^ number of postings to this account + anumpostings :: Int, -- ^ number of postings to this account -- derived from the above: aibalance :: MixedAmount, -- ^ this account's balance, including subaccounts aparent :: Maybe Account, -- ^ parent account