|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use EventEspresso\core\domain\values\currency\UsesMoneyInterface; |
|
4
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
5
|
|
|
use EventEspresso\core\exceptions\InvalidEntityException; |
|
6
|
|
|
use EventEspresso\core\exceptions\InvalidIdentifierException; |
|
7
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
8
|
|
|
use EventEspresso\core\services\currency\MoneyFactory; |
|
9
|
|
|
use EventEspresso\core\services\loaders\LoaderFactory; |
|
10
|
|
|
|
|
11
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit('NO direct script access allowed'); |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* EE_Price class |
|
16
|
|
|
* |
|
17
|
|
|
* @package Event Espresso |
|
18
|
|
|
* @subpackage includes/classes/EE_Price.class.php |
|
19
|
|
|
* @author Mike Nelson |
|
20
|
|
|
*/ |
|
21
|
|
|
class EE_Price extends EE_Soft_Delete_Base_Class implements UsesMoneyInterface |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $props_n_values incoming values |
|
26
|
|
|
* @param string $timezone incoming timezone (if not set the timezone set for the website will be |
|
27
|
|
|
* used.) |
|
28
|
|
|
* @param array $date_formats incoming date_formats in an array where the first value is the |
|
29
|
|
|
* date_format and the second value is the time format |
|
30
|
|
|
* @param MoneyFactory $money_factory |
|
31
|
|
|
* @return EE_Price |
|
32
|
|
|
* @throws InvalidArgumentException |
|
33
|
|
|
* @throws InvalidInterfaceException |
|
34
|
|
|
* @throws InvalidDataTypeException |
|
35
|
|
|
* @throws EE_Error |
|
36
|
|
|
*/ |
|
37
|
|
View Code Duplication |
public static function new_instance( |
|
38
|
|
|
$props_n_values = array(), |
|
39
|
|
|
$timezone = null, |
|
40
|
|
|
$date_formats = array(), |
|
41
|
|
|
MoneyFactory $money_factory = null |
|
42
|
|
|
) { |
|
43
|
|
|
$has_object = parent::_check_for_object( |
|
44
|
|
|
$props_n_values, |
|
45
|
|
|
__CLASS__, |
|
46
|
|
|
$timezone, |
|
47
|
|
|
$date_formats |
|
48
|
|
|
); |
|
49
|
|
|
return $has_object |
|
50
|
|
|
? $has_object |
|
51
|
|
|
: new self( |
|
52
|
|
|
$props_n_values, |
|
53
|
|
|
false, |
|
54
|
|
|
$timezone, |
|
55
|
|
|
$date_formats, |
|
56
|
|
|
$money_factory |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array $props_n_values incoming values from the database |
|
63
|
|
|
* @param string $timezone incoming timezone as set by the model. If not set the timezone for |
|
64
|
|
|
* the website will be used. |
|
65
|
|
|
* @param MoneyFactory $money_factory |
|
66
|
|
|
* @return EE_Price |
|
67
|
|
|
* @throws InvalidArgumentException |
|
68
|
|
|
* @throws InvalidInterfaceException |
|
69
|
|
|
* @throws InvalidDataTypeException |
|
70
|
|
|
* @throws EE_Error |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function new_instance_from_db( |
|
73
|
|
|
$props_n_values = array(), |
|
74
|
|
|
$timezone = null, |
|
75
|
|
|
MoneyFactory $money_factory = null |
|
76
|
|
|
) { |
|
77
|
|
|
return new self( |
|
78
|
|
|
$props_n_values, |
|
79
|
|
|
true, |
|
80
|
|
|
$timezone, |
|
81
|
|
|
array(), |
|
82
|
|
|
$money_factory |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
89
|
|
|
* play nice |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $fieldValues where each key is a field (ie, array key in the 2nd layer of the model's |
|
92
|
|
|
* _fields array, (eg, EVT_ID, TXN_amount, QST_name, etc) and values are their |
|
93
|
|
|
* values |
|
94
|
|
|
* @param boolean $bydb a flag for setting if the class is instantiated by the corresponding db model |
|
95
|
|
|
* or not. |
|
96
|
|
|
* @param string $timezone indicate what timezone you want any datetime fields to be in when |
|
97
|
|
|
* instantiating |
|
98
|
|
|
* a EE_Base_Class object. |
|
99
|
|
|
* @param array $date_formats An array of date formats to set on construct where first value is the |
|
100
|
|
|
* date_format and second value is the time format. |
|
101
|
|
|
* @param MoneyFactory $money_factory |
|
102
|
|
|
* @throws InvalidArgumentException |
|
103
|
|
|
* @throws InvalidInterfaceException |
|
104
|
|
|
* @throws InvalidDataTypeException |
|
105
|
|
|
* @throws EE_Error |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
protected function __construct( |
|
108
|
|
|
array $fieldValues = array(), |
|
109
|
|
|
$bydb = false, |
|
110
|
|
|
$timezone = '', |
|
111
|
|
|
array $date_formats = array(), |
|
112
|
|
|
MoneyFactory $money_factory = null |
|
113
|
|
|
) { |
|
114
|
|
|
if (! $money_factory instanceof MoneyFactory) { |
|
115
|
|
|
$money_factory = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\currency\MoneyFactory'); |
|
116
|
|
|
} |
|
117
|
|
|
$this->money_factory = $money_factory; |
|
118
|
|
|
parent::__construct($fieldValues, $bydb, $timezone, $date_formats); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set Price type ID |
|
124
|
|
|
* |
|
125
|
|
|
* @access public |
|
126
|
|
|
* @param int $PRT_ID |
|
127
|
|
|
*/ |
|
128
|
|
|
public function set_type( $PRT_ID = 0 ) { |
|
129
|
|
|
$this->set( 'PRT_ID', $PRT_ID ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set Price Amount |
|
136
|
|
|
* |
|
137
|
|
|
* @access public |
|
138
|
|
|
* @param float $PRC_amount |
|
139
|
|
|
*/ |
|
140
|
|
|
public function set_amount( $PRC_amount = 0.00 ) { |
|
141
|
|
|
$this->set( 'PRC_amount', $PRC_amount ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set Price Name |
|
148
|
|
|
* |
|
149
|
|
|
* @access public |
|
150
|
|
|
* @param string $PRC_name |
|
151
|
|
|
*/ |
|
152
|
|
|
public function set_name( $PRC_name = '' ) { |
|
153
|
|
|
$this->set( 'PRC_name', $PRC_name ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Set Price Description |
|
160
|
|
|
* |
|
161
|
|
|
* @access public |
|
162
|
|
|
* @param string $PRC_desc |
|
163
|
|
|
*/ |
|
164
|
|
|
public function set_description( $PRC_desc = '' ) { |
|
165
|
|
|
$this->Set( 'PRC_desc', $PRC_desc ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* set is_default |
|
172
|
|
|
* |
|
173
|
|
|
* @access public |
|
174
|
|
|
* @param bool $PRC_is_default |
|
175
|
|
|
*/ |
|
176
|
|
|
public function set_is_default( $PRC_is_default = FALSE ) { |
|
177
|
|
|
$this->set( 'PRC_is_default', $PRC_is_default ); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* set deleted |
|
184
|
|
|
* |
|
185
|
|
|
* @access public |
|
186
|
|
|
* @param bool $PRC_deleted |
|
187
|
|
|
*/ |
|
188
|
|
|
public function set_deleted( $PRC_deleted = NULL ) { |
|
189
|
|
|
$this->set( 'PRC_deleted', $PRC_deleted ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* get Price type |
|
196
|
|
|
* @access public |
|
197
|
|
|
* @return int |
|
198
|
|
|
*/ |
|
199
|
|
|
public function type() { |
|
200
|
|
|
return $this->get( 'PRT_ID' ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* get Price Amount |
|
207
|
|
|
* @access public |
|
208
|
|
|
* @return float |
|
209
|
|
|
*/ |
|
210
|
|
|
public function amount() { |
|
211
|
|
|
return $this->get( 'PRC_amount' ); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* get Price Name |
|
218
|
|
|
* @access public |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
|
public function name() { |
|
222
|
|
|
return $this->get( 'PRC_name' ); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* get Price description |
|
229
|
|
|
* @access public |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
public function desc() { |
|
233
|
|
|
return $this->get( 'PRC_desc' ); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* get overrides |
|
240
|
|
|
* @access public |
|
241
|
|
|
* @return int |
|
242
|
|
|
*/ |
|
243
|
|
|
public function overrides() { |
|
244
|
|
|
return $this->get( 'PRC_overrides' ); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* get order |
|
251
|
|
|
* @access public |
|
252
|
|
|
* @return int |
|
253
|
|
|
*/ |
|
254
|
|
|
public function order() { |
|
255
|
|
|
return $this->get( 'PRC_order' ); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* get the author of the price |
|
262
|
|
|
* |
|
263
|
|
|
* @since 4.5.0 |
|
264
|
|
|
* |
|
265
|
|
|
* @return int |
|
266
|
|
|
*/ |
|
267
|
|
|
public function wp_user() { |
|
268
|
|
|
return $this->get('PRC_wp_user'); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* get is_default |
|
275
|
|
|
* @access public |
|
276
|
|
|
* @return bool |
|
277
|
|
|
*/ |
|
278
|
|
|
public function is_default() { |
|
279
|
|
|
return $this->get( 'PRC_is_default' ); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* get deleted |
|
286
|
|
|
* @access public |
|
287
|
|
|
* @return bool |
|
288
|
|
|
*/ |
|
289
|
|
|
public function deleted() { |
|
290
|
|
|
return $this->get( 'PRC_deleted' ); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @return bool |
|
297
|
|
|
*/ |
|
298
|
|
|
public function parent() { |
|
299
|
|
|
return $this->get( 'PRC_parent' ); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
//some helper methods for getting info on the price_type for this price |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* return whether the price is a base price or not |
|
307
|
|
|
* @return boolean |
|
308
|
|
|
*/ |
|
309
|
|
|
public function is_base_price() { |
|
310
|
|
|
$price_type = $this->type_obj(); |
|
311
|
|
|
return $price_type->base_type() === 1; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* |
|
318
|
|
|
* @return EE_Price_Type |
|
319
|
|
|
*/ |
|
320
|
|
|
public function type_obj() { |
|
321
|
|
|
return $this->get_first_related( 'Price_Type' ); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
|
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Simply indicates whether this price increases or decreases the total |
|
328
|
|
|
* @return boolean true = discount, otherwise adds to the total |
|
329
|
|
|
*/ |
|
330
|
|
|
public function is_discount() { |
|
331
|
|
|
$price_type = $this->type_obj(); |
|
332
|
|
|
return $price_type->is_discount(); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* whether the price is a percentage or not |
|
339
|
|
|
* @return boolean |
|
340
|
|
|
*/ |
|
341
|
|
|
public function is_percent() { |
|
342
|
|
|
$price_type = $this->type_obj(); |
|
343
|
|
|
return $price_type->get( 'PRT_is_percent' ); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* return pretty price dependant on whether its a dollar or percent. |
|
349
|
|
|
* |
|
350
|
|
|
* @since 4.4.0 |
|
351
|
|
|
* |
|
352
|
|
|
* @return string |
|
353
|
|
|
*/ |
|
354
|
|
|
public function pretty_price() { |
|
355
|
|
|
return ! $this->is_percent() ? $this->get_pretty('PRC_amount') : $this->get('PRC_amount') . '%'; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
|
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* @return mixed |
|
362
|
|
|
*/ |
|
363
|
|
|
public function get_price_without_currency_symbol() { |
|
364
|
|
|
return str_replace( EE_Registry::instance()->CFG->currency->sign, '', $this->get_pretty( 'PRC_amount' ) ); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Returns the payment's amount in subunits (if the currency has subunits; otherwise this will actually be |
|
370
|
|
|
* in the currency's main units) |
|
371
|
|
|
* |
|
372
|
|
|
* @return int |
|
373
|
|
|
* @throws EE_Error |
|
374
|
|
|
* @throws InvalidEntityException |
|
375
|
|
|
* @throws DomainException |
|
376
|
|
|
*/ |
|
377
|
|
|
public function amountInSubunits() |
|
378
|
|
|
{ |
|
379
|
|
|
return $this->moneyInSubunits('PRC_amount'); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Sets the payment's amount based on the incoming monetary subunits (eg pennies). If the currency has no subunits, |
|
385
|
|
|
* the amount is actually assumed to be in the currency's main units |
|
386
|
|
|
* |
|
387
|
|
|
* @param int $amount_in_subunits |
|
388
|
|
|
* @return void |
|
389
|
|
|
* @throws EE_Error |
|
390
|
|
|
* @throws InvalidArgumentException |
|
391
|
|
|
* @throws InvalidInterfaceException |
|
392
|
|
|
* @throws InvalidIdentifierException |
|
393
|
|
|
* @throws InvalidDataTypeException |
|
394
|
|
|
* @throws DomainException |
|
395
|
|
|
*/ |
|
396
|
|
|
public function setAmountInSubunits($amount_in_subunits) |
|
397
|
|
|
{ |
|
398
|
|
|
$this->setMoneySubunits('PRC_amount', $amount_in_subunits); |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
/* End of file EE_Price.class.php */ |
|
402
|
|
|
/* Location: /includes/classes/EE_Price.class.php */ |
|
403
|
|
|
|