@@ -5,26 +5,34 @@ defmodule Numscriptex.Balance do
55 can see the results of all transactions.
66 """
77
8+ alias Numscriptex.Utilities
9+
810 @ derive JSON.Encoder
911 defstruct account: nil ,
1012 asset: nil ,
1113 final_balance: nil ,
12- initial_balance: nil
14+ decimal_final_balance: nil ,
15+ initial_balance: nil ,
16+ decimal_initial_balance: nil
1317
1418 @ typedoc """
1519 Type that represents `Numscriptex.Balance` struct.
1620
1721 ## Fields
1822 * `:account` the account name
1923 * `:asset` the asset were the transaction was made
20- * `:final_balance` balance after the transactions
21- * `:initial_balance` balance before the transactions
24+ * `:final_balance` balance after the transactions (integer)
25+ * `:decimal_final_balance` balance after the transactions, but as float
26+ * `:initial_balance` balance before the transactions (integer)
27+ * `:decimal_initial_balance` balance after the transactions, but as float
2228 """
2329 @ type t ( ) :: % __MODULE__ {
2430 account: bitstring ( ) ,
2531 asset: bitstring ( ) ,
26- final_balance: non_neg_integer ( ) ,
27- initial_balance: non_neg_integer ( )
32+ final_balance: integer ( ) ,
33+ decimal_final_balance: float ( ) ,
34+ initial_balance: integer ( ) ,
35+ decimal_initial_balance: float ( )
2836 }
2937
3038 @ doc """
@@ -57,19 +65,25 @@ defmodule Numscriptex.Balance do
5765 account: "foo",
5866 asset: "EUR/2",
5967 final_balance: 300,
60- initial_balance: 300
68+ decimal_final_balance: 3.0,
69+ initial_balance: 300,
70+ decimal_initial_balance: 3.0
6171 },
6272 %Numscriptex.Balance{
6373 account: "foo",
6474 asset: "USD/2",
6575 final_balance: 400,
66- initial_balance: 500
76+ decimal_final_balance: 4.0,
77+ initial_balance: 500,
78+ decimal_initial_balance: 5.0
6779 },
6880 %Numscriptex.Balance{
6981 account: "bar",
7082 asset: "USD/2",
7183 final_balance: 100,
72- initial_balance: 0
84+ decimal_final_balance: 1.0,
85+ initial_balance: 0,
86+ decimal_initial_balance: 0.0
7387 }
7488 ]
7589 ```
@@ -82,9 +96,8 @@ defmodule Numscriptex.Balance do
8296 |> handle_initial_balance ( account_assets )
8397 |> handle_final_balance ( postings )
8498 |> maybe_drop_balance ( )
85- |> then ( fn balances ->
86- Enum . map ( balances , & struct ( __MODULE__ , & 1 ) )
87- end )
99+ |> put_decimal_values ( )
100+ |> Enum . map ( & struct ( __MODULE__ , & 1 ) )
88101 end
89102
90103 defp build_balances ( account_assets , postings ) do
@@ -100,8 +113,10 @@ defmodule Numscriptex.Balance do
100113 % {
101114 account: account ,
102115 asset: asset ,
116+ final_balance: 0 ,
117+ decimal_final_balance: 0 ,
103118 initial_balance: 0 ,
104- final_balance : 0
119+ decimal_initial_balance : 0
105120 }
106121 end )
107122 end )
@@ -113,14 +128,18 @@ defmodule Numscriptex.Balance do
113128 % {
114129 account: posting [ "source" ] ,
115130 asset: posting [ "asset" ] ,
131+ final_balance: 0 ,
132+ decimal_final_balance: 0 ,
116133 initial_balance: 0 ,
117- final_balance : 0
134+ decimal_initial_balance : 0
118135 } ,
119136 % {
120137 account: posting [ "destination" ] ,
121138 asset: posting [ "asset" ] ,
139+ final_balance: 0 ,
140+ decimal_final_balance: 0 ,
122141 initial_balance: 0 ,
123- final_balance : 0
142+ decimal_initial_balance : 0
124143 }
125144 ]
126145 end )
@@ -177,4 +196,19 @@ defmodule Numscriptex.Balance do
177196 balance . initial_balance == 0 and balance . final_balance == 0
178197 end )
179198 end
199+
200+ defp put_decimal_values ( balances ) do
201+ Enum . map ( balances , fn
202+ % { initial_balance: initial_balance , final_balance: final_balance } = balance ->
203+ decimal_places = Utilities . decimal_places_from_asset ( balance . asset )
204+ decimal_initial_balance = Utilities . integer_to_decimal ( initial_balance , decimal_places )
205+ decimal_final_balance = Utilities . integer_to_decimal ( final_balance , decimal_places )
206+
207+ % {
208+ balance
209+ | decimal_initial_balance: decimal_initial_balance ,
210+ decimal_final_balance: decimal_final_balance
211+ }
212+ end )
213+ end
180214end
0 commit comments