wl_lib_0.1.3_1c94dc7c/src/wl-generic_money.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
with Ada.Strings.Fixed;

with WL.Generic_Real_Images;

package body WL.Generic_Money is

   package Real_Images is
     new WL.Generic_Real_Images (Real);

   Local_Currency_Symbol       : String := "$   ";
   Local_Digit_Grouping_Symbol : String := ",   ";
   Local_Decimal_Symbol        : String := ".   ";

   -------------
   -- Add_Tax --
   -------------

   function Add_Tax (Money    : Money_Type;
                     Tax_Rate : Real)
                     return Money_Type
   is
   begin
      if Money > 0 then
         return Money + Tax (Money, Tax_Rate);
      else
         return Money;
      end if;
   end Add_Tax;

   -------------
   -- Add_Tax --
   -------------

   function Add_Tax
     (Price : Price_Type;
      Tax_Rate : Real)
      return Price_Type
   is
   begin
      return Price_Type (Add_Tax (Money_Type (Price), Tax_Rate));
   end Add_Tax;

   ------------
   -- Adjust --
   ------------

   function Adjust (Money    : Money_Type;
                    Factor   : Real)
                    return Money_Type
   is
   begin
      return To_Money (To_Real (Money) * Factor);
   end Adjust;

   ------------------
   -- Adjust_Price --
   ------------------

   function Adjust_Price (Price    : Price_Type;
                          Factor   : Real)
                         return Price_Type
   is
   begin
      return Price_Type (Real (Price) * Factor);
   end Adjust_Price;

   ---------------------
   -- Currency_Symbol --
   ---------------------

   function Currency_Symbol return String is
   begin
      return Ada.Strings.Fixed.Trim
        (Local_Currency_Symbol, Ada.Strings.Right);
   end Currency_Symbol;

   --------------------
   -- Decimal_Symbol --
   --------------------

   function Decimal_Symbol return String is
   begin
      return Ada.Strings.Fixed.Trim
        (Local_Decimal_Symbol, Ada.Strings.Right);
   end Decimal_Symbol;

   ---------------------------
   -- Digit_Grouping_Symbol --
   ---------------------------

   function Digit_Grouping_Symbol return String is
   begin
      return Ada.Strings.Fixed.Trim
        (Local_Digit_Grouping_Symbol, Ada.Strings.Right);
   end Digit_Grouping_Symbol;

   ------------------
   -- Get_Quantity --
   ------------------

   function Get_Quantity
     (Total_Cash : Money_Type;
      Price      : Price_Type)
      return Quantities.Quantity_Type
   is
   begin
      if Total_Cash > Zero then
         return Quantities.To_Quantity
           (Real (Total_Cash / Money_Type (Price)));
      else
         return Quantities.Zero;
      end if;
   end Get_Quantity;

   -----------
   -- Image --
   -----------

   function Image (Item : Money_Type) return String is
   begin
      if Item < 0 then
         return "-" & Image (Price_Type (-Item));
      else
         return Image (Price_Type (Item));
      end if;
   end Image;

   -----------
   -- Image --
   -----------

   function Image (Item : Price_Type) return String is
      Base_Image : constant String :=
                     Ada.Strings.Fixed.Trim
                       (Price_Type'Image (Item / 1000),
                        Ada.Strings.Left);
      Decimal_Image : constant String :=
                        Ada.Strings.Fixed.Trim
                          (Price_Type'Image (Item mod 1000),
                           Ada.Strings.Left);
   begin
      return Result : String := Base_Image & ".000" do
         Result (Result'Last - Decimal_Image'Length + 1 .. Result'Last) :=
           Decimal_Image;
      end return;
   end Image;

   ---------
   -- Max --
   ---------

   function Max (X, Y : Money_Type) return Money_Type is
   begin
      return Money_Type'Max (X, Y);
   end Max;

   ---------
   -- Max --
   ---------

   function Max (X, Y : Price_Type) return Price_Type is
   begin
      return Price_Type'Max (X, Y);
   end Max;

   ---------
   -- Min --
   ---------

   function Min (X, Y : Money_Type) return Money_Type is
   begin
      return Money_Type'Min (X, Y);
   end Min;

   ---------
   -- Min --
   ---------

   function Min (X, Y : Price_Type) return Price_Type is
   begin
      return Price_Type'Min (X, Y);
   end Min;

   -----------
   -- Price --
   -----------

   function Price (Total    : Money_Type;
                   Quantity_Type : Quantities.Quantity_Type)
                   return Price_Type
   is
      use type Quantities.Quantity_Type;
   begin
      if Quantity_Type = Quantities.Zero then
         return Zero;
      else
         return Price_Type
           (Real (Total) / Quantities.To_Real (Quantity_Type));
      end if;
   end Price;

   --------------------------
   -- Set_Image_Properties --
   --------------------------

   procedure Set_Image_Properties
     (Currency_Symbol       : String := "$";
      Digit_Grouping_Symbol : String := ",";
      Decimal_Symbol        : String := ".")
   is
   begin
      Local_Currency_Symbol (1 .. Currency_Symbol'Length) :=
        Currency_Symbol;
      Local_Digit_Grouping_Symbol (1 .. Digit_Grouping_Symbol'Length) :=
        Digit_Grouping_Symbol;
      Local_Decimal_Symbol (1 .. Decimal_Symbol'Length) :=
        Decimal_Symbol;
   end Set_Image_Properties;

   ----------
   -- Show --
   ----------

   function Show
     (Item  : Money_Type;
      Exact : Boolean := False)
      return String
   is
   begin
      if Item < 0 then
         return "(" & Show (Price_Type (abs Item), Exact) & ")";
      else
         return Show (Price_Type (Item), Exact);
      end if;
   end Show;

   ----------
   -- Show --
   ----------

   function Show
     (Price : Price_Type;
      Exact : Boolean := False)
      return String
   is

      function Show_Exact
        (Value : Price_Type;
         Cents : Boolean)
         return String;

      ----------------
      -- Show_Exact --
      ----------------
      function Show_Exact
        (Value : Price_Type;
         Cents : Boolean)
         return String
      is
         Image    : constant String :=
                      Ada.Strings.Fixed.Trim
                        (Price_Type'Image
                           ((Value + 5) / (if Cents then 10 else 100)),
                         Ada.Strings.Left);
         Currency : constant String := Currency_Symbol;

         function Group (S : String) return String
         is (if S'Length <= 3
             then S
             else Group (S (S'First .. S'Last - 3))
             & Digit_Grouping_Symbol
             & S (S'Last - 2 .. S'Last));

      begin
         if Image'Length = 1 then
            return Currency & "0" & Decimal_Symbol & "0" & Image;
         elsif Image'Length = 2 then
            return Currency & "0" & Decimal_Symbol & Image;
         elsif not Cents then
            return Currency
              & Group (Image (Image'First .. Image'Last - 1))
              & Decimal_Symbol
              & Image (Image'Last .. Image'Last);
         else
            return Currency
              & Group (Image (Image'First .. Image'Last - 2))
              & Decimal_Symbol
              & Image (Image'Last - 1 .. Image'Last);
         end if;
      end Show_Exact;

      Real_Value : constant Real := To_Real (Price);

   begin
      if Exact or else Real_Value < 10_000.0 then
         return Show_Exact (Price, True);
      else
         return Currency_Symbol & Real_Images.Approximate_Image (Real_Value);
      end if;
   end Show;

   -----------
   -- Split --
   -----------

   function Split
     (Amount  : Money_Type;
      Portion : Real)
      return Money_Type
   is
   begin
      return Money_Type (Real (Amount) * Portion);
   end Split;

   ---------
   -- Tax --
   ---------

   function Tax (Money : Money_Type;
                 Tax   : Real)
                 return Money_Type
   is
   begin
      if Money < 0 then
         return 0;
      else
         return Money_Type (Real (Money) * Tax);
      end if;
   end Tax;

   ---------
   -- Tax --
   ---------

   function Tax (Price   : Price_Type;
                            Tax     : Real)
                            return Price_Type
   is
   begin
      return Price_Type (Real (Price) * Tax);
   end Tax;

   -----------
   -- Total --
   -----------

   function Total (Price  : Price_Type;
                   Quantity_Type : Quantities.Quantity_Type)
                  return Money_Type
   is
      use Quantities;
   begin
      return Money_Type (Real (Price) * To_Real (Quantity_Type));
   end Total;

   -----------
   -- Value --
   -----------

   function Value (Image : String) return Money_Type is
      Currency : constant String := Currency_Symbol;
   begin
      if Image = "" then
         return Zero;
      elsif Image (Image'First) = '~' then
         declare
            Result : constant Money_Type :=
                       Value (Image (Image'First + 1 .. Image'Last));
         begin
            return Result;
         end;
      elsif Image'Length > Currency'Length
        and then Image (Image'First .. Image'First + Currency'Length - 1)
        = Currency
      then
         return Value (Image (Image'First + Currency'Length .. Image'Last));
      elsif Image (Image'Last) = 'K' then
         return Value (Image (Image'First .. Image'Last - 1)) * 1E3;
      elsif Image (Image'Last) = 'M' then
         return Value (Image (Image'First .. Image'Last - 1)) * 1E6;
      elsif Image (Image'Last) = 'G' then
         return Value (Image (Image'First .. Image'Last - 1)) * 1E9;
      else
         return Money_Type (Real'Value (Image));
      end if;
   end Value;

   -----------
   -- Value --
   -----------

   function Value (Image : String) return Price_Type is
   begin
      return To_Price (Real'Value (Image));
   end Value;

   -----------------
   -- Without_Tax --
   -----------------

   function Without_Tax
     (Money : Money_Type;
      Tax   : Real)
      return Money_Type
   is
   begin
      if Money < 0 then
         return Money;
      else
         return Money_Type (Real (Money) / (1.0 + Tax));
      end if;
   end Without_Tax;

   -----------------
   -- Without_Tax --
   -----------------

   function Without_Tax
     (Price   : Price_Type;
      Tax     : Real)
      return Price_Type
   is
   begin
      return Price_Type (Without_Tax (Money_Type (Price), Tax));
   end Without_Tax;

   ----------
   -- Zero --
   ----------

   function Zero return Money_Type is
   begin
      return 0;
   end Zero;

   ----------
   -- Zero --
   ----------

   function Zero return Price_Type is
   begin
      return 0;
   end Zero;

end WL.Generic_Money;