1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains a helper class for money type actions. |
4
|
|
|
* |
5
|
|
|
* @since %VER% |
6
|
|
|
* @package Event Espresso |
7
|
|
|
* @subpackage helper |
8
|
|
|
*/ |
9
|
|
|
if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* Money helper class. |
13
|
|
|
* This class has helper methods that help with money related conversions and calculations. |
14
|
|
|
* |
15
|
|
|
* @since %VER% |
16
|
|
|
* |
17
|
|
|
* @package Event Espresso |
18
|
|
|
* @subpackage helpers |
19
|
|
|
* @author Darren Ethier |
20
|
|
|
* |
21
|
|
|
* ------------------------------------------------------------------------ |
22
|
|
|
*/ |
23
|
|
|
class EEH_Money extends EEH_Base { |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This converts an incoming localized money value into a standard float item (to three decimal places) |
28
|
|
|
* |
29
|
|
|
* @param int|string $incoming_value |
30
|
|
|
* @return float |
31
|
|
|
*/ |
32
|
|
|
public static function convert_to_float_from_localized_money( $incoming_value ) { |
33
|
|
|
//remove thousands separator |
34
|
|
|
$money_value = str_replace( EE_Registry::instance()->CFG->currency->thsnds, '', $incoming_value ); |
35
|
|
|
|
36
|
|
|
//replace decimal place with standard decimal. |
37
|
|
|
$money_value = str_replace( EE_Registry::instance()->CFG->currency->dec_mrk, '.', $money_value ); |
38
|
|
|
|
39
|
|
|
//float it! and round to three decimal places |
40
|
|
|
$money_value = round ( (float) $money_value, 3 ); |
41
|
|
|
return $money_value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* For comparing floats. Default operator is '=', but see the $operator below for all options. |
48
|
|
|
* This should be used to compare floats instead of normal '==' because floats |
49
|
|
|
* are inherently imprecise, and so you can sometimes have two floats that appear to be identical |
50
|
|
|
* but actually differ by 0.00000001. |
51
|
|
|
* |
52
|
|
|
* @see http://biostall.com/php-function-to-compare-floating-point-numbers |
53
|
|
|
* @param float $float1 |
54
|
|
|
* @param float $float2 |
55
|
|
|
* @param string $operator The operator. Valid options are =, <=, <, >=, >, <>, eq, lt, lte, gt, gte, ne |
56
|
|
|
* @return bool whether the equation is true or false |
57
|
|
|
* @throws \EE_Error |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
public static function compare_floats( $float1, $float2, $operator='=' ) { |
61
|
|
|
// Check numbers to 5 digits of precision |
62
|
|
|
$epsilon = 0.00001; |
63
|
|
|
|
64
|
|
|
$float1 = (float)$float1; |
65
|
|
|
$float2 = (float)$float2; |
66
|
|
|
|
67
|
|
|
switch ($operator) { |
68
|
|
|
// equal |
69
|
|
|
case "=": |
70
|
|
|
case "eq": |
71
|
|
|
if (abs($float1 - $float2) < $epsilon) { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
break; |
75
|
|
|
// less than |
76
|
|
|
case "<": |
77
|
|
View Code Duplication |
case "lt": |
|
|
|
|
78
|
|
|
if (abs($float1 - $float2) < $epsilon) { |
79
|
|
|
return false; |
80
|
|
|
} else { |
81
|
|
|
if ($float1 < $float2) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
break; |
86
|
|
|
// less than or equal |
87
|
|
|
case "<=": |
88
|
|
View Code Duplication |
case "lte": |
|
|
|
|
89
|
|
|
if (self::compare_floats($float1, $float2, '<') || self::compare_floats($float1, $float2, '=')) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
break; |
93
|
|
|
// greater than |
94
|
|
|
case ">": |
95
|
|
View Code Duplication |
case "gt": |
|
|
|
|
96
|
|
|
if (abs($float1 - $float2) < $epsilon) { |
97
|
|
|
return false; |
98
|
|
|
} else { |
99
|
|
|
if ($float1 > $float2) { |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
break; |
104
|
|
|
// greater than or equal |
105
|
|
|
case ">=": |
106
|
|
View Code Duplication |
case "gte": |
|
|
|
|
107
|
|
|
if (self::compare_floats($float1, $float2, '>') || self::compare_floats($float1, $float2, '=')) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
break; |
111
|
|
|
case "<>": |
112
|
|
|
case "!=": |
113
|
|
|
case "ne": |
114
|
|
|
if (abs($float1 - $float2) > $epsilon) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
break; |
118
|
|
|
default: |
119
|
|
|
throw new EE_Error(__( "Unknown operator '" . $operator . "' in EEH_Money::compare_floats()", 'event_espresso' ) ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* This returns a localized format string suitable for jQplot. |
128
|
|
|
* |
129
|
|
|
* @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
130
|
|
|
* Otherwise will use currency settings for current active country on site. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public static function get_format_for_jqplot( $CNT_ISO = '') { |
135
|
|
|
//default format |
136
|
|
|
$format = 'f'; |
137
|
|
|
//if CNT_ISO passed lets try to get currency settings for it. |
138
|
|
|
$currency_config = $CNT_ISO !== '' ? new EE_Currency_Config( $CNT_ISO ) : null; |
139
|
|
|
//default currency settings for site if not set |
140
|
|
View Code Duplication |
if ( ! $currency_config instanceof EE_Currency_Config ) { |
|
|
|
|
141
|
|
|
$currency_config = EE_Registry::instance()->CFG->currency instanceof EE_Currency_Config ? EE_Registry::instance()->CFG->currency : new EE_Currency_Config(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
//first get the decimal place and number of places |
145
|
|
|
$format = "%'." . $currency_config->dec_plc . $format; |
146
|
|
|
|
147
|
|
|
//currency symbol on right side. |
148
|
|
|
$format = $currency_config->sign_b4 ? $currency_config->sign . $format : $format . $currency_config->sign; |
149
|
|
|
return $format; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* This returns a localized format string suitable for usage with the Google Charts API format param. |
155
|
|
|
* |
156
|
|
|
* @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
157
|
|
|
* Otherwise will use currency settings for current active country on site. |
158
|
|
|
* |
159
|
|
|
* Note: GoogleCharts uses ICU pattern set (@see http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details) |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public static function get_format_for_google_charts( $CNT_ISO = '' ) { |
164
|
|
|
//if CNT_ISO passed lets try to get currency settings for it. |
165
|
|
|
$currency_config = $CNT_ISO !== '' ? new EE_Currency_Config( $CNT_ISO ) : null; |
166
|
|
|
//default currency settings for site if not set |
167
|
|
View Code Duplication |
if ( ! $currency_config instanceof EE_Currency_Config ) { |
|
|
|
|
168
|
|
|
$currency_config = EE_Registry::instance()->CFG->currency instanceof EE_Currency_Config ? EE_Registry::instance()->CFG->currency : new EE_Currency_Config(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$decimal_places_placeholder = str_pad( '', $currency_config->dec_plc, '0' ); |
172
|
|
|
|
173
|
|
|
//first get the decimal place and number of places |
174
|
|
|
$format = '#' . $currency_config->thsnds . '##0' . $currency_config->dec_mrk . $decimal_places_placeholder; |
175
|
|
|
|
176
|
|
|
//currency symbol on right side. |
177
|
|
|
$format = $currency_config->sign_b4 ? $currency_config->sign . $format : $format . $currency_config->sign; |
178
|
|
|
return $format; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} //end class EEH_Money |
182
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.