Modif : Calc.Balance.Equilibre : retourne ce qui n’est pas équilibré.
authorJulien Moutinho <julm+hcompta@autogeree.net>
Thu, 30 Apr 2015 19:04:51 +0000 (21:04 +0200)
committerJulien Moutinho <julm+hcompta@autogeree.net>
Thu, 30 Apr 2015 19:07:56 +0000 (21:07 +0200)
lib/Hcompta/Calc/Balance.hs
lib/Hcompta/Format/Ledger/Balance.hs [new file with mode: 0644]
lib/Hcompta/Format/Ledger/Journal.hs
lib/Test/Main.hs
lib/hcompta-lib.cabal

index f37cf1613034800859d28a031ca4df46431cae9e..c491c4d81cc2b9b0e8de8b7e709fee105b74504f 100644 (file)
@@ -30,22 +30,22 @@ data Balance
  { by_account :: By_Account
  , by_unit    :: By_Unit
  } deriving (Data, Eq, Read, Show, Typeable)
-type By_Account
- = Map Account Sum_by_Account
-type By_Unit
- = Map Amount.Unit Sum_by_Unit
 
-
--- | A sum by 'Account' of the 'Amount's of some 'Posting's.
-type Sum_by_Account
+type By_Account
+ = Map Account Account_Sum
+-- | A sum of 'Amount's,
+-- concerning a single 'Account'.
+type Account_Sum
  = Amount.By_Unit
 
--- | A sum by 'Unit' of the 'Amount's of some 'Posting's,
---   with the 'Account's involved to build that sum.
-data Sum_by_Unit
- =   Sum_by_Unit
- { accounts :: Map Account () -- ^ The 'Account's involved to build 'amount'.
- , amount   :: Amount -- ^ The sum of 'Amount's for a same 'Unit'.
+type By_Unit
+ = Map Amount.Unit Unit_Sum
+-- | A sum of 'Amount's with their 'Account's involved,
+-- concerning a single 'Unit'.
+data Unit_Sum
+ =   Unit_Sum
+ { amount   :: Amount -- ^ The sum of 'Amount's for a single 'Unit'.
+ , accounts :: Map Account () -- ^ The 'Account's involved to build 'amount'.
  } deriving (Data, Eq, Read, Show, Typeable)
 
 -- ** Constructors
@@ -65,30 +65,30 @@ nil_By_Unit :: By_Unit
 nil_By_Unit =
        Data.Map.empty
 
-nil_Sum_by_Account :: Sum_by_Account
+nil_Sum_by_Account :: Account_Sum
 nil_Sum_by_Account =
        Data.Map.empty
 
-nil_Sum_by_Unit :: Sum_by_Unit
+nil_Sum_by_Unit :: Unit_Sum
 nil_Sum_by_Unit =
-       Sum_by_Unit
+       Unit_Sum
         { accounts = Data.Map.empty
         , amount = Amount.nil
         }
 
--- | Return a tuple associating the given 'Sum_by_Unit' with its 'Unit'.
-assoc_by_amount_unit :: Sum_by_Unit -> (Unit, Sum_by_Unit)
-assoc_by_amount_unit s = (Amount.unit $ amount s, s)
+-- | Return a tuple associating the given 'Unit_Sum' with its 'Unit'.
+assoc_unit_sum :: Unit_Sum -> (Unit, Unit_Sum)
+assoc_unit_sum s = (Amount.unit $ amount s, s)
 
--- | Return a 'Map' associating the given 'Sum_by_Unit' with their respective 'Unit'.
-by_Unit_from_List :: [Sum_by_Unit] -> By_Unit
+-- | Return a 'Map' associating the given 'Unit_Sum' with their respective 'Unit'.
+by_Unit_from_List :: [Unit_Sum] -> By_Unit
 by_Unit_from_List balances =
        Data.Map.fromListWith
-        (\x y -> Sum_by_Unit
+        (\x y -> Unit_Sum
                 { amount=(GHC.Num.+) (amount x) (amount y)
                 , accounts=Data.Map.union (accounts x) (accounts y)
                 }) $
-       Data.List.map assoc_by_amount_unit balances
+       Data.List.map assoc_unit_sum balances
 
 -- ** Incremental constructors
 
@@ -105,13 +105,13 @@ posting post balance =
                 (by_account balance)
         , by_unit =
                Data.Map.unionWith
-                (\x y -> Sum_by_Unit
+                (\x y -> Unit_Sum
                         { amount   = (GHC.Num.+) (amount x) (amount y)
                         , accounts = Data.Map.union (accounts x) (accounts y)
                         })
                 (by_unit balance) $
                Data.Map.map
-                (\amount -> Sum_by_Unit
+                (\amount -> Unit_Sum
                         { amount
                         , accounts = Data.Map.singleton (Posting.account post) ()
                         })
@@ -183,7 +183,7 @@ union b0 b1 =
                 (by_account b1)
         , by_unit =
                Data.Map.unionWith
-                (\x y -> Sum_by_Unit
+                (\x y -> Unit_Sum
                         { amount = (GHC.Num.+) (amount x) (amount y)
                         , accounts = Data.Map.union (accounts x) (accounts y)
                         })
@@ -191,31 +191,85 @@ union b0 b1 =
                 (by_unit b1)
         }
 
+-- * The 'Equilibre' type
+
+-- | See 'equilibre'.
+newtype Equilibre
+ =      Equilibre By_Unit
+ deriving (Data, Eq, Read, Show, Typeable)
+
+-- | Return the 'by_unit' of the given 'Balance' with:
+--
+-- * 'Unit's whose 'Unit_Sum' 'amount' verifying 'Amount.is_zero' removed,
+--
+-- * and remaining 'Unit's having their 'Unit_Sum' 'accounts'
+-- complemented with the 'by_account' of the given 'Balance'
+-- (i.e. now mapping to accounts __not__ involved to build the 'Unit_Sum').
+equilibre :: Balance -> Equilibre
+equilibre balance = do
+       let max_accounts = Data.Map.size $ by_account balance
+       Equilibre $ Data.Map.foldlWithKey
+        (\m unit Unit_Sum{amount, accounts} ->
+               if Amount.is_zero $ amount
+               then m
+               else
+                       case Data.Map.size accounts of
+                        n | n == max_accounts ->
+                               Data.Map.insert unit Unit_Sum{amount, accounts=Data.Map.empty} m
+                        _ -> do
+                               let d = Data.Map.map (const ()) $
+                                       Data.Map.difference (by_account balance) accounts
+                               Data.Map.insert unit Unit_Sum{amount, accounts=d} m
+        )
+        Data.Map.empty
+        (by_unit balance)
+
 -- ** Tests
 
--- | Return 'True' if and only if the 'Balance'
---   has all its 'by_unit' 'amount's verify 'Amount.is_zero'
---   or exactly one 'Account' of the 'by_unit' 'accounts' is not in 'by_account'.
-is_equilibrated :: Balance -> Bool
-is_equilibrated balance =
+-- | Return 'True' if and only if the given 'Equilibre' maps no 'Unit'.
+is_equilibrated :: Equilibre -> Bool
+is_equilibrated (Equilibre eq) = Data.Map.null eq
+
+-- | Return 'True' if and only if the given 'Equilibre' satisfies:
+--
+-- * 'is_equilibrated',
+-- * or 'is_inferrable'.
+is_equilibrable :: Equilibre -> Bool
+is_equilibrable e@(Equilibre eq) =
+       Data.Map.null eq || is_inferrable e
+
+-- | Return 'True' if and only if the given 'Equilibre'
+-- maps only to 'Unit_Sum's whose 'accounts'
+-- maps exactly one 'Account'.
+is_inferrable :: Equilibre -> Bool
+is_inferrable (Equilibre eq) =
        Data.Foldable.all
-        (\s ->
-               (Amount.is_zero $ amount s) ||
-               (Data.Map.size (accounts s) ==
-               (Data.Map.size (by_account balance) - 1))) -- NOTE: Data.Map.size is O(1)
-        (by_unit balance)
+        (\Unit_Sum{accounts} -> Data.Map.size accounts == 1)
+        eq
+
+-- | Return 'True' if and only if the given 'Equilibre'
+-- maps to at least one 'Unit_Sum's whose 'accounts'
+-- maps more than one 'Account'.
+is_non_inferrable :: Equilibre -> Bool
+is_non_inferrable (Equilibre eq) =
+       Data.Foldable.any
+        (\Unit_Sum{accounts} -> Data.Map.size accounts > 1)
+        eq
 
 -- * The 'Expanded' type
 
+-- | See 'expand'.
 newtype Expanded
  =      Expanded By_Account
  deriving (Data, Eq, Read, Show, Typeable)
 
--- | Return the given 'By_Account'
---   with all missing 'Account.ascending' 'Account's inserted,
---   and every mapped Amount.'Amount.By_Unit'
---   added with any 'Account's Amount.'Amount.By_Unit'
---   to which it is 'Account.ascending'.
+-- | Return the given 'By_Account' with:
+--
+-- * all missing 'Account.ascending' 'Account's inserted,
+--
+-- * and every mapped Amount.'Amount.By_Unit'
+-- added with any Amount.'Amount.By_Unit'
+-- of the 'Account's’ for which it is 'Account.ascending'.
 expand :: By_Account -> Expanded
 expand balance =
        -- TODO: because (+) is associative
diff --git a/lib/Hcompta/Format/Ledger/Balance.hs b/lib/Hcompta/Format/Ledger/Balance.hs
new file mode 100644 (file)
index 0000000..e2ec717
--- /dev/null
@@ -0,0 +1,29 @@
+{-# LANGUAGE NamedFieldPuns #-}
+module Hcompta.Format.Ledger.Balance where
+
+import           Hcompta.Model (Transaction)
+import qualified Hcompta.Calc.Balance as Balance
+import qualified Hcompta.Format.Ledger.Journal as Journal
+import           Hcompta.Format.Ledger.Journal (Journal(..))
+import qualified Hcompta.Lib.Foldable as Lib.Foldable
+
+-- * The 'Balance.Equilibre' type
+
+-- | Return the first 'Transaction' (if any) which does not respect 'Balance.is_equilibrable'
+--   with the path of 'Journal's leading to that 'Transaction'.
+equilibre :: Journal -> Maybe ((Transaction, Balance.Equilibre), [Journal])
+equilibre =
+       Journal.find
+        (\Journal{Journal.transactions} ->
+               Lib.Foldable.find
+                (Lib.Foldable.find
+                        (\tr -> do
+                               let bal = Balance.transaction tr Balance.nil
+                               let eq = Balance.equilibre bal
+                               if Balance.is_equilibrable eq
+                                       then Nothing
+                                       else Just (tr, eq)
+                        )
+                )
+                transactions
+        )
index 6165c93bb7627e0a86c49fec186f9a803a549d9d..e4584a3b42b886a203318eb2b124dfcec3463642 100644 (file)
@@ -20,6 +20,8 @@ import qualified Hcompta.Model.Amount as Amount
 import qualified Hcompta.Model.Journal as Model.Journal
 import qualified Hcompta.Model.Transaction as Transaction
 
+-- * The 'Journal' type
+
 data Journal
  =   Journal
  { file :: FilePath
@@ -42,6 +44,8 @@ nil =
         , unit_styles = Data.Map.empty
         }
 
+-- * Iterators
+
 -- | Return the given accumulator folded over
 --   the given 'Journal' and its 'includes' 'Journal's.
 fold :: (Journal -> a -> a) -> Journal -> a -> a
index 9c767398d95d6e4bf6e143eeed6ffb498682076e..61daea1cf7aee3ad4030e0862963079729ed5232 100644 (file)
@@ -137,8 +137,8 @@ test_Hcompta =
                                                         [ (["A"], Amount.from_List [ Amount.usd $ 1 ]) ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
@@ -165,8 +165,8 @@ test_Hcompta =
                                                         [ (["A"], Amount.from_List [ Amount.usd $ 0 ]) ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
@@ -193,13 +193,13 @@ test_Hcompta =
                                                         [ (["A"], Amount.from_List [ Amount.usd $ 1, Amount.eur $ -1 ]) ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ -1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
@@ -228,8 +228,8 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
@@ -257,13 +257,13 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
@@ -292,18 +292,18 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.gbp $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
@@ -326,8 +326,8 @@ test_Hcompta =
                                                                 [ (["A"], Amount.from_List [ Amount.usd $ 1 ]) ]
                                                         , Calc.Balance.by_unit =
                                                                Data.Map.fromList $
-                                                               Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                               [ Calc.Balance.Sum_by_Unit
+                                                               Data.List.map Calc.Balance.assoc_unit_sum $
+                                                               [ Calc.Balance.Unit_Sum
                                                                 { Calc.Balance.amount = Amount.usd $ 1
                                                                 , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                         [["A"]]
@@ -340,8 +340,8 @@ test_Hcompta =
                                                                 [ (["A"], Amount.from_List [ Amount.usd $ 1 ]) ]
                                                         , Calc.Balance.by_unit =
                                                                Data.Map.fromList $
-                                                               Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                               [ Calc.Balance.Sum_by_Unit
+                                                               Data.List.map Calc.Balance.assoc_unit_sum $
+                                                               [ Calc.Balance.Unit_Sum
                                                                 { Calc.Balance.amount = Amount.usd $ 1
                                                                 , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                         [["A"]]
@@ -355,8 +355,8 @@ test_Hcompta =
                                                         [ (["A"], Amount.from_List [ Amount.usd $ 2 ]) ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 2
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
@@ -371,8 +371,8 @@ test_Hcompta =
                                                                 [ (["A"], Amount.from_List [ Amount.usd $ 1 ]) ]
                                                         , Calc.Balance.by_unit =
                                                                Data.Map.fromList $
-                                                               Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                               [ Calc.Balance.Sum_by_Unit
+                                                               Data.List.map Calc.Balance.assoc_unit_sum $
+                                                               [ Calc.Balance.Unit_Sum
                                                                 { Calc.Balance.amount = Amount.usd $ 1
                                                                 , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                         [["A"]]
@@ -385,8 +385,8 @@ test_Hcompta =
                                                                 [ (["B"], Amount.from_List [ Amount.usd $ 1 ]) ]
                                                         , Calc.Balance.by_unit =
                                                                Data.Map.fromList $
-                                                               Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                               [ Calc.Balance.Sum_by_Unit
+                                                               Data.List.map Calc.Balance.assoc_unit_sum $
+                                                               [ Calc.Balance.Unit_Sum
                                                                 { Calc.Balance.amount = Amount.usd $ 1
                                                                 , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                         [["B"]]
@@ -401,8 +401,8 @@ test_Hcompta =
                                                         , (["B"], Amount.from_List [ Amount.usd $ 1 ]) ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 2
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
@@ -417,8 +417,8 @@ test_Hcompta =
                                                                 [ (["A"], Amount.from_List [ Amount.usd $ 1 ]) ]
                                                         , Calc.Balance.by_unit =
                                                                Data.Map.fromList $
-                                                               Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                               [ Calc.Balance.Sum_by_Unit
+                                                               Data.List.map Calc.Balance.assoc_unit_sum $
+                                                               [ Calc.Balance.Unit_Sum
                                                                 { Calc.Balance.amount = Amount.usd $ 1
                                                                 , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                         [["A"]]
@@ -431,8 +431,8 @@ test_Hcompta =
                                                                 [ (["B"], Amount.from_List [ Amount.eur $ 1 ]) ]
                                                         , Calc.Balance.by_unit =
                                                                Data.Map.fromList $
-                                                               Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                               [ Calc.Balance.Sum_by_Unit
+                                                               Data.List.map Calc.Balance.assoc_unit_sum $
+                                                               [ Calc.Balance.Unit_Sum
                                                                 { Calc.Balance.amount = Amount.eur $ 1
                                                                 , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                         [["B"]]
@@ -447,13 +447,13 @@ test_Hcompta =
                                                         , (["B"], Amount.from_List [ Amount.eur $ 1 ]) ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["B"]]
@@ -571,14 +571,16 @@ test_Hcompta =
                                                 , (["B"], Amount.from_List [ Amount.usd $ 1 ])
                                                 , (["B", "A"], Amount.from_List [ Amount.usd $ 1 ]) ])
                                 ]
-                        , "is_equilibrated" ~: TestList
-                                [ "nil = True" ~: TestCase $
+                        , "is_equilibrable" ~: TestList
+                                [ "nil" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.nil
-                                , "{A+$0, $+0} = True" ~: TestCase $
+                                , "{A+$0, $+0}" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.Balance
                                                 { Calc.Balance.by_account =
                                                        Data.Map.fromList
@@ -586,17 +588,18 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
                                                        ]
                                                 }
-                                , "{A+$1, $+1} = False" ~: TestCase $
+                                , "{A+$1, $+1}" ~: TestCase $
                                                (@=?) False $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.Balance
                                                 { Calc.Balance.by_account =
                                                        Data.Map.fromList
@@ -604,17 +607,18 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
                                                        ]
                                                 }
-                                , "{A+$0+€0, $0 €+0} = True" ~: TestCase $
+                                , "{A+$0+€0, $0 €+0}" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.Balance
                                                 { Calc.Balance.by_account =
                                                        Data.Map.fromList
@@ -622,22 +626,23 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
                                                        ]
                                                 }
-                                , "{A+$1, B-$1, $+0} = True" ~: TestCase $
+                                , "{A+$1, B-$1, $+0}" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.Balance
                                                 { Calc.Balance.by_account =
                                                        Data.Map.fromList
@@ -646,17 +651,18 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
                                                         }
                                                        ]
                                                 }
-                                , "{A+$1 B, $+1} = True" ~: TestCase $
+                                , "{A+$1 B, $+1}" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                 Calc.Balance.Balance
                                                         { Calc.Balance.by_account =
                                                                Data.Map.fromList
@@ -665,17 +671,18 @@ test_Hcompta =
                                                                 ]
                                                         , Calc.Balance.by_unit =
                                                                Data.Map.fromList $
-                                                               Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                               [ Calc.Balance.Sum_by_Unit
+                                                               Data.List.map Calc.Balance.assoc_unit_sum $
+                                                               [ Calc.Balance.Unit_Sum
                                                                 { Calc.Balance.amount = Amount.usd $ 1
                                                                 , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                         [["A"]]
                                                                 }
                                                                ]
                                                         }
-                                , "{A+$1 B+€1, $+1 €+1} = True" ~: TestCase $
+                                , "{A+$1 B+€1, $+1 €+1}" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.Balance
                                                 { Calc.Balance.by_account =
                                                        Data.Map.fromList
@@ -684,22 +691,23 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["B"]]
                                                         }
                                                        ]
                                                 }
-                                , "{A+$1 B-$1+€1, $+0 €+1} = True" ~: TestCase $
+                                , "{A+$1 B-$1+€1, $+0 €+1}" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.Balance
                                                 { Calc.Balance.by_account =
                                                        Data.Map.fromList
@@ -708,22 +716,23 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ 1
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["B"]]
                                                         }
                                                        ]
                                                 }
-                                , "{A+$1+€2+£3 B-$1-€2-£3, $+0 €+0 £+0} = True" ~: TestCase $
+                                , "{A+$1+€2+£3 B-$1-€2-£3, $+0 €+0 £+0}" ~: TestCase $
                                                (@=?) True $
-                                               Calc.Balance.is_equilibrated $
+                                               Calc.Balance.is_equilibrable $
+                                               Calc.Balance.equilibre $
                                                Calc.Balance.Balance
                                                 { Calc.Balance.by_account =
                                                        Data.Map.fromList
@@ -732,18 +741,18 @@ test_Hcompta =
                                                         ]
                                                 , Calc.Balance.by_unit =
                                                        Data.Map.fromList $
-                                                       Data.List.map Calc.Balance.assoc_by_amount_unit $
-                                                       [ Calc.Balance.Sum_by_Unit
+                                                       Data.List.map Calc.Balance.assoc_unit_sum $
+                                                       [ Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.usd $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.eur $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
                                                         }
-                                                       , Calc.Balance.Sum_by_Unit
+                                                       , Calc.Balance.Unit_Sum
                                                         { Calc.Balance.amount = Amount.gbp $ 0
                                                         , Calc.Balance.accounts = Data.Map.fromList $ Data.List.map (,())
                                                                 [["A"], ["B"]]
index 802c041cab113f3fd14bbdaded9fd068adbc3de6..a94cd9bd4f8be900f1939b4220f72da31072ff2a 100644 (file)
@@ -53,6 +53,7 @@ Library
     Hcompta.Format
     Hcompta.Format.CSV
     Hcompta.Format.Ledger
+    Hcompta.Format.Ledger.Balance
     Hcompta.Format.Ledger.Journal
     Hcompta.Format.Ledger.Read
     Hcompta.Format.Ledger.Write