@@ -107,20 +107,20 @@ accountFromBalances name bal = Account
107
107
-- The accounts are returned as a list in flattened tree order,
108
108
-- and also reference each other as a tree.
109
109
-- (The first account is the root of the tree.)
110
- accountsFromPostings :: (Posting -> Day ) -> [ Day ] -> [Posting ] -> [Account AccountBalance ]
111
- accountsFromPostings getPostingDate days = flattenAccounts . accountFromPostings getPostingDate days
110
+ accountsFromPostings :: (Posting -> Maybe Day ) -> [Posting ] -> [Account AccountBalance ]
111
+ accountsFromPostings getPostingDate = flattenAccounts . accountFromPostings getPostingDate
112
112
113
113
-- | Derive 1. an account tree and 2. each account's total exclusive
114
114
-- and inclusive changes from a list of postings.
115
115
-- This is the core of the balance command (and of *ledger).
116
116
-- The accounts are returned as tree.
117
- accountFromPostings :: (Posting -> Day ) -> [ Day ] -> [Posting ] -> Account AccountBalance
118
- accountFromPostings getPostingDate days ps =
117
+ accountFromPostings :: (Posting -> Maybe Day ) -> [Posting ] -> Account AccountBalance
118
+ accountFromPostings getPostingDate ps =
119
119
tieAccountParents . sumAccounts $ mapAccounts setBalance acctTree
120
120
where
121
121
-- The special name "..." is stored in the root of the tree
122
122
acctTree = accountTree " root" . HM. keys $ HM. delete " ..." accountMap
123
- setBalance a = a{abalances = HM. lookupDefault emptyMap name accountMap}
123
+ setBalance a = a{abalances = HM. lookupDefault mempty name accountMap}
124
124
where name = if aname a == " root" then " ..." else aname a
125
125
accountMap = processPostings ps
126
126
@@ -130,9 +130,7 @@ accountFromPostings getPostingDate days ps =
130
130
processAccountName p = HM. alter (updateAccountBalance p) (paccount p)
131
131
updateAccountBalance p = Just
132
132
. insertAccountBalances (getPostingDate p) (AccountBalance 1 (pamount p) nullmixedamt)
133
- . fromMaybe emptyMap
134
-
135
- emptyMap = emptyAccountBalances days
133
+ . fromMaybe mempty
136
134
137
135
-- | Convert a list of account names to a tree of Account objects,
138
136
-- with just the account names filled in.
@@ -208,12 +206,12 @@ sumAccounts a = a{asubs = subs, abalances = setImplicitBalances $ abalances a}
208
206
subtotals = foldMap abalances subs
209
207
210
208
setImplicitBalances :: AccountBalances AccountBalance -> AccountBalances AccountBalance
211
- setImplicitBalances = if null subs
212
- then fmap setibal
213
- else opAccountBalances addibal subtotals
209
+ setImplicitBalances = mergeAccountBalances combineChildren (fmap onlyChildren) (fmap noChildren) subtotals
210
+
211
+ combineChildren children this = this {abibalance = abebalance this <> abibalance children}
212
+ onlyChildren children = mempty {abibalance = abibalance children}
213
+ noChildren this = this {abibalance = abebalance this}
214
214
215
- setibal bal@ (AccountBalance _ ebal _) = bal{abibalance = ebal}
216
- addibal (AccountBalance _ _ ibal) bal@ (AccountBalance _ ebal _) = bal{abibalance = ebal <> ibal}
217
215
218
216
-- | Remove all subaccounts below a certain depth.
219
217
clipAccounts :: Int -> Account a -> Account a
@@ -288,12 +286,6 @@ filterAccounts p a
288
286
-- | Merge two accounts and their subaccounts.
289
287
--
290
288
-- This assumes that the top-level 'Account's have the same name.
291
- --
292
- -- Unless the 'Account's and all their subaccounts have the same collection of
293
- -- 'Day' keys, it will probably produce unhelpful output: do not do this unless
294
- -- you really know what you're doing. Merging two accounts with unequal
295
- -- 'Day' keys can be useful when they have the same Intervals but not
296
- -- necessarily equal spans, as in the budget reports.
297
289
mergeAccounts :: Account a -> Account b -> Account (These a b )
298
290
mergeAccounts a = tieAccountParents . merge a
299
291
where
@@ -384,9 +376,9 @@ showAccountDebug a = printf "%-25s %s %4s"
384
376
tests_Account = testGroup " Account" [
385
377
testGroup " accountFromPostings" [
386
378
testCase " no postings, no days" $
387
- accountFromPostings undefined [] [] @?= accountTree " root" []
379
+ accountFromPostings undefined [] @?= accountTree " root" []
388
380
,testCase " no postings, only 2000-01-01" $
389
381
allAccounts (all (\ d -> (ModifiedJulianDay $ toInteger d) == fromGregorian 2000 01 01 ) . IM. keys . abdatemap . abalances)
390
- (accountFromPostings undefined [fromGregorian 2000 01 01 ] [ ] ) @? " Not all abalances have exactly 2000-01-01"
382
+ (accountFromPostings undefined [] ) @? " Not all abalances have exactly 2000-01-01"
391
383
]
392
384
]
0 commit comments