1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE NamedFieldPuns #-}
5 {-# LANGUAGE StandaloneDeriving #-}
6 {-# LANGUAGE TypeFamilies #-}
7 module Hcompta.Calc.Balance where
9 import Control.Exception (assert)
11 import qualified Data.Foldable
12 import Data.Foldable (Foldable(..))
13 import qualified Data.Map.Strict as Data.Map
14 import Data.Map.Strict (Map)
15 import Data.Maybe (fromMaybe)
16 import Data.Typeable ()
18 import qualified Hcompta.Lib.Foldable as Lib.Foldable
19 import qualified Hcompta.Lib.TreeMap as Lib.TreeMap
20 import Hcompta.Lib.TreeMap (TreeMap)
21 import qualified Hcompta.Model.Account as Account
22 import Hcompta.Model.Account (Account)
24 -- * Requirements' interface
28 ( Data (Amount_Unit a)
33 , Show (Amount_Unit a)
35 , Typeable (Amount_Unit a)
39 amount_sign :: a -> Ordering
43 -- | A 'posting' used to produce a 'Balance'
44 -- must be an instance of this class.
45 class Amount (Posting_Amount p) => Posting p where
47 posting_account :: p -> Account
48 posting_amounts :: p -> Map (Amount_Unit (Posting_Amount p)) (Posting_Amount p)
49 posting_set_amounts :: Map (Amount_Unit (Posting_Amount p)) (Posting_Amount p) -> p -> p
51 instance (Amount amount, unit ~ Amount_Unit amount)
52 => Posting (Account, Map unit amount)
54 type Posting_Amount (Account, Map unit amount) = amount
57 posting_set_amounts amounts (acct, _) = (acct, amounts)
59 -- * Type 'Amount_Sum'
61 -- | Sum keeping track of negative and positive 'amount's.
62 data Amount_Sum amount unit
64 { amount_sum_negative :: Map unit amount
65 , amount_sum_positive :: Map unit amount
66 , amount_sum_balance :: Map unit amount
67 } deriving (Data, Eq, Show, Typeable)
72 -> Amount_Sum amount unit
75 { amount_sum_negative = Data.Map.filter ((==) LT . amount_sign) a
76 , amount_sum_positive = Data.Map.filter ((==) GT . amount_sign) a
77 , amount_sum_balance = a
81 :: (Amount amount, Ord unit)
82 => Amount_Sum amount unit
83 -> Amount_Sum amount unit
84 -> Amount_Sum amount unit
86 let add = Data.Map.unionWith (flip (+)) in
88 { amount_sum_negative = add (amount_sum_negative a) (amount_sum_negative b)
89 , amount_sum_positive = add (amount_sum_positive a) (amount_sum_positive b)
90 , amount_sum_balance = add (amount_sum_balance a) (amount_sum_balance b)
95 => Amount_Sum amount unit
96 -> Amount_Sum amount unit
99 { amount_sum_negative = amount_sum_positive a
100 , amount_sum_positive = amount_sum_negative a
101 , amount_sum_balance = Data.Map.map negate $ amount_sum_balance a
106 -- | Sum by 'Account' and sum by 'unit' of some 'Posting's.
107 data Amount amount => Balance amount
109 { balance_by_account :: Balance_by_Account amount (Amount_Unit amount)
110 , balance_by_unit :: Balance_by_Unit amount (Amount_Unit amount)
112 deriving instance Amount amount => Data (Balance amount)
113 deriving instance Amount amount => Eq (Balance amount)
114 deriving instance Amount amount => Show (Balance amount)
115 deriving instance Typeable Balance
117 type Balance_by_Account amount unit
118 = TreeMap Account.Name
119 (Account_Sum amount unit)
121 -- | A sum of 'amount's,
122 -- concerning a single 'Account'.
123 type Account_Sum amount unit
124 = Data.Map.Map unit amount
126 type Balance_by_Unit amount unit
127 = Map unit (Unit_Sum amount)
129 -- | A sum of 'amount's with their 'Account's involved,
130 -- concerning a single 'unit'.
133 { unit_sum_amount :: Amount_Sum amount () -- ^ The sum of 'amount's for a single 'unit'.
134 , unit_sum_accounts :: Map Account () -- ^ The 'Account's involved to build 'unit_sum_amount'.
135 } deriving (Data, Eq, Show, Typeable)
139 nil :: Amount amount => Balance amount
142 { balance_by_account = Lib.TreeMap.empty
143 , balance_by_unit = Data.Map.empty
146 -- | Return the given 'Balance'
147 -- updated by the given 'Posting'.
150 , balance ~ Balance (Posting_Amount posting) )
151 => posting -> balance -> balance
154 { balance_by_account =
156 (Data.Map.unionWith (flip (+)))
157 (posting_account post)
158 (posting_amounts post)
159 (balance_by_account bal)
162 (\new old -> Unit_Sum
163 { unit_sum_amount = amount_sum_add
164 (unit_sum_amount old)
165 (unit_sum_amount new)
166 , unit_sum_accounts = Data.Map.unionWith
168 (unit_sum_accounts old)
169 (unit_sum_accounts new)
171 (balance_by_unit bal) $
174 { unit_sum_amount = amount_sum $ Data.Map.singleton () amount
175 , unit_sum_accounts = Data.Map.singleton (posting_account post) ()
177 (posting_amounts post)
180 -- | Return the given 'Balance'
181 -- updated by the given 'Posting's.
184 , balance ~ Balance (Posting_Amount posting)
185 , Foldable foldable )
186 => foldable posting -> balance -> balance
187 postings = flip (Data.Foldable.foldr balance)
189 -- | Return the first given 'Balance'
190 -- updated by the second given 'Balance'.
191 union :: Amount amount
192 => Balance amount -> Balance amount -> Balance amount
195 { balance_by_account =
197 (Data.Map.unionWith (flip (+)))
198 (balance_by_account b0)
199 (balance_by_account b1)
202 (\new old -> Unit_Sum
203 { unit_sum_amount = amount_sum_add
204 (unit_sum_amount old)
205 (unit_sum_amount new)
206 , unit_sum_accounts = Data.Map.unionWith
208 (unit_sum_accounts old)
209 (unit_sum_accounts new)
215 -- * Type 'Deviation'
217 -- | The 'Balance_by_Unit' whose 'unit_sum_amount'
218 -- is not zero and possible 'Account' to 'infer_equilibrium'.
219 newtype Amount amount
221 = Deviation (Balance_by_Unit amount (Amount_Unit amount))
222 deriving instance Amount amount => Data (Deviation amount)
223 deriving instance Amount amount => Eq (Deviation amount)
224 deriving instance Amount amount => Show (Deviation amount)
225 deriving instance Typeable Deviation
227 -- | Return the 'balance_by_unit' of the given 'Balance' with:
229 -- * 'unit's whose 'unit_sum_amount' verifying 'amount_is_zero' removed,
231 -- * and remaining 'unit's having their 'unit_sum_accounts'
232 -- complemented with the 'balance_by_account' of the given 'Balance'
233 -- (i.e. now mapping to 'Account's __not__ involved to build the 'Unit_Sum').
239 let all_accounts = Lib.TreeMap.flatten (const ()) (balance_by_account bal)
240 let max_accounts = Data.Map.size all_accounts
242 Data.Map.foldlWithKey
243 (\m unit Unit_Sum{unit_sum_amount, unit_sum_accounts} ->
244 if EQ == amount_sign (amount_sum_balance unit_sum_amount Data.Map.! ())
247 case Data.Map.size unit_sum_accounts of
248 n | n == max_accounts ->
249 Data.Map.insert unit Unit_Sum
251 , unit_sum_accounts = Data.Map.empty
254 let diff = Data.Map.difference all_accounts unit_sum_accounts
255 Data.Map.insert unit Unit_Sum
257 , unit_sum_accounts = diff
261 (balance_by_unit bal)
263 -- ** The equilibrium
265 -- | Return the 'Balance' (adjusted by inferred 'Amount's)
266 -- of the given 'Posting's and either:
268 -- * 'Left': the 'Posting's that cannot be inferred.
269 -- * 'Right': the given 'Posting's with inferred 'Amount's inserted.
272 => Map Account [posting]
273 -> ( Balance (Posting_Amount posting)
274 , Either [Unit_Sum (Posting_Amount posting)] (Map Account [posting])
276 infer_equilibrium posts = do
277 let bal_initial = Data.Foldable.foldr postings nil posts
278 let Deviation dev = deviation bal_initial
279 let (bal_adjusted, eithers) =
280 Data.Map.foldrWithKey
281 (\unit unit_sum@(Unit_Sum{unit_sum_amount, unit_sum_accounts})
283 case Data.Map.size unit_sum_accounts of
285 let acct = fst $ Data.Map.elemAt 0 unit_sum_accounts in
286 let amt = (amount_sum_balance $ amount_sum_negate unit_sum_amount) Data.Map.! () in
287 let amts = Data.Map.singleton unit amt in
288 ( balance (acct, amts) bal
289 , Right (acct, unit, amt) : lr
291 _ -> (bal, Left [unit_sum] : lr))
294 let (l, r) = Lib.Foldable.accumLeftsAndFoldrRights
295 (\(acct, unit, amt) ->
297 (\_new_ps -> insert_amount (unit, amt))
298 acct (assert False []))
301 [] -> (bal_adjusted, Right r)
302 _ -> (bal_adjusted, Left l)
306 => (Amount_Unit (Posting_Amount posting), Posting_Amount posting)
307 -> [posting] -> [posting]
308 insert_amount p@(unit, amt) ps =
310 [] -> assert False []
311 (x:xs) | Data.Map.null (posting_amounts x) ->
312 posting_set_amounts (Data.Map.singleton unit amt) x:xs
313 | Data.Map.notMember unit (posting_amounts x) ->
314 let amts = Data.Map.insertWith
315 (assert False undefined)
316 unit amt (posting_amounts x) in
317 posting_set_amounts amts x:xs
318 (x:xs) -> x:insert_amount p xs
320 -- | Return 'True' if and only if the given 'Deviation' maps no 'unit'.
321 is_at_equilibrium :: Amount amount => Deviation amount -> Bool
322 is_at_equilibrium (Deviation dev) = Data.Map.null dev
324 -- | Return 'True' if and only if the given 'Deviation'
325 -- maps only to 'Unit_Sum's whose 'unit_sum_accounts'
326 -- maps exactly one 'Account'.
327 is_equilibrium_inferrable :: Amount amount => Deviation amount -> Bool
328 is_equilibrium_inferrable (Deviation dev) =
330 (\Unit_Sum{unit_sum_accounts} -> Data.Map.size unit_sum_accounts == 1)
333 -- | Return 'True' if and only if the given 'Deviation'
334 -- maps to at least one 'Unit_Sum' whose 'unit_sum_accounts'
335 -- maps more than one 'Account'.
336 is_equilibrium_non_inferrable :: Amount amount => Deviation amount -> Bool
337 is_equilibrium_non_inferrable (Deviation dev) =
339 (\Unit_Sum{unit_sum_accounts} -> Data.Map.size unit_sum_accounts > 1)
344 -- | Descending propagation of 'Amount's accross 'Account's.
346 = TreeMap Account.Name (Account_Sum_Expanded amount)
347 data Amount amount => Account_Sum_Expanded amount
348 = Account_Sum_Expanded
349 { exclusive :: Map (Amount_Unit amount) amount
350 , inclusive :: Amount_Sum amount (Amount_Unit amount) -- ^ 'amount_sum_add' folded over 'exclusive' and 'inclusive' of 'Lib.TreeMap.node_descendants'
352 deriving instance Amount amount => Data (Account_Sum_Expanded amount)
353 deriving instance Amount amount => Eq (Account_Sum_Expanded amount)
354 deriving instance Amount amount => Show (Account_Sum_Expanded amount)
355 deriving instance Typeable Account_Sum_Expanded
357 -- | Return the given 'Balance_by_Account' with:
359 -- * all missing 'Account.ascending' 'Account's inserted,
361 -- * and every mapped 'Amount'
362 -- added with any 'Amount'
363 -- of the 'Account's for which it is 'Account.ascending'.
366 => Balance_by_Account amount (Amount_Unit amount)
369 let from_value = fromMaybe (assert False undefined) . Lib.TreeMap.node_value in
370 Lib.TreeMap.map_by_depth_first
371 (\descendants value ->
372 let nodes = Lib.TreeMap.nodes descendants in
373 let exclusive = fromMaybe Data.Map.empty value in
378 (amount_sum_add . inclusive . from_value)
379 (amount_sum exclusive) nodes