Completed
Branch EDTR/master (fdc436)
by
unknown
25:46 queued 17:14
created

EE_Price::is_surcharge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
6
/**
7
 * EE_Price class
8
 *
9
 * @package            Event Espresso
10
 * @subpackage         includes/classes/EE_Price.class.php
11
 * @author             Mike Nelson
12
 */
13
class EE_Price extends EE_Soft_Delete_Base_Class
14
{
15
16
    /**
17
     * @param array  $props_n_values          incoming values
18
     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
19
     *                                        used.)
20
     * @param array  $date_formats            incoming date_formats in an array where the first value is the
21
     *                                        date_format and the second value is the time format
22
     * @return EE_Price
23
     * @throws EE_Error
24
     * @throws InvalidArgumentException
25
     * @throws ReflectionException
26
     * @throws InvalidDataTypeException
27
     * @throws InvalidInterfaceException
28
     */
29
    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
30
    {
31
        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
32
        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
33
    }
34
35
36
    /**
37
     * @param array  $props_n_values  incoming values from the database
38
     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
39
     *                                the website will be used.
40
     * @return EE_Price
41
     * @throws EE_Error
42
     * @throws InvalidArgumentException
43
     * @throws ReflectionException
44
     * @throws InvalidDataTypeException
45
     * @throws InvalidInterfaceException
46
     */
47
    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
48
    {
49
        return new self($props_n_values, true, $timezone);
50
    }
51
52
53
    /**
54
     * Set Price type ID
55
     *
56
     * @param int $PRT_ID
57
     * @throws EE_Error
58
     * @throws InvalidArgumentException
59
     * @throws ReflectionException
60
     * @throws InvalidDataTypeException
61
     * @throws InvalidInterfaceException
62
     */
63
    public function set_type($PRT_ID = 0)
64
    {
65
        $this->set('PRT_ID', $PRT_ID);
66
    }
67
68
69
    /**
70
     * Set Price Amount
71
     *
72
     * @param float $PRC_amount
73
     * @throws EE_Error
74
     * @throws InvalidArgumentException
75
     * @throws ReflectionException
76
     * @throws InvalidDataTypeException
77
     * @throws InvalidInterfaceException
78
     */
79
    public function set_amount($PRC_amount = 0.00)
80
    {
81
        $this->set('PRC_amount', $PRC_amount);
82
    }
83
84
85
    /**
86
     * Set Price Name
87
     *
88
     * @param string $PRC_name
89
     * @throws EE_Error
90
     * @throws InvalidArgumentException
91
     * @throws ReflectionException
92
     * @throws InvalidDataTypeException
93
     * @throws InvalidInterfaceException
94
     */
95
    public function set_name($PRC_name = '')
96
    {
97
        $this->set('PRC_name', $PRC_name);
98
    }
99
100
101
    /**
102
     * Set Price Description
103
     *
104
     * @param string $PRC_desc
105
     * @throws EE_Error
106
     * @throws InvalidArgumentException
107
     * @throws ReflectionException
108
     * @throws InvalidDataTypeException
109
     * @throws InvalidInterfaceException
110
     */
111
    public function set_description($PRC_desc = '')
112
    {
113
        $this->Set('PRC_desc', $PRC_desc);
114
    }
115
116
117
    /**
118
     * set is_default
119
     *
120
     * @param bool $PRC_is_default
121
     * @throws EE_Error
122
     * @throws InvalidArgumentException
123
     * @throws ReflectionException
124
     * @throws InvalidDataTypeException
125
     * @throws InvalidInterfaceException
126
     */
127
    public function set_is_default($PRC_is_default = false)
128
    {
129
        $this->set('PRC_is_default', $PRC_is_default);
130
    }
131
132
133
    /**
134
     * set deleted
135
     *
136
     * @param bool $PRC_deleted
137
     * @throws EE_Error
138
     * @throws InvalidArgumentException
139
     * @throws ReflectionException
140
     * @throws InvalidDataTypeException
141
     * @throws InvalidInterfaceException
142
     */
143
    public function set_deleted($PRC_deleted = null)
144
    {
145
        $this->set('PRC_deleted', $PRC_deleted);
146
    }
147
148
149
    /**
150
     * get Price type
151
     *
152
     * @return        int
153
     * @throws EE_Error
154
     * @throws InvalidArgumentException
155
     * @throws ReflectionException
156
     * @throws InvalidDataTypeException
157
     * @throws InvalidInterfaceException
158
     */
159
    public function type()
160
    {
161
        return $this->get('PRT_ID');
162
    }
163
164
165
    /**
166
     * get Price Amount
167
     *
168
     * @return        float
169
     * @throws EE_Error
170
     * @throws InvalidArgumentException
171
     * @throws ReflectionException
172
     * @throws InvalidDataTypeException
173
     * @throws InvalidInterfaceException
174
     */
175
    public function amount()
176
    {
177
        return $this->get('PRC_amount');
178
    }
179
180
181
    /**
182
     * get Price Name
183
     *
184
     * @return        string
185
     * @throws EE_Error
186
     * @throws InvalidArgumentException
187
     * @throws ReflectionException
188
     * @throws InvalidDataTypeException
189
     * @throws InvalidInterfaceException
190
     */
191
    public function name()
192
    {
193
        return $this->get('PRC_name');
194
    }
195
196
197
    /**
198
     * get Price description
199
     *
200
     * @return        string
201
     * @throws EE_Error
202
     * @throws InvalidArgumentException
203
     * @throws ReflectionException
204
     * @throws InvalidDataTypeException
205
     * @throws InvalidInterfaceException
206
     */
207
    public function desc()
208
    {
209
        return $this->get('PRC_desc');
210
    }
211
212
213
    /**
214
     * get overrides
215
     *
216
     * @return        int
217
     * @throws EE_Error
218
     * @throws InvalidArgumentException
219
     * @throws ReflectionException
220
     * @throws InvalidDataTypeException
221
     * @throws InvalidInterfaceException
222
     */
223
    public function overrides()
224
    {
225
        return $this->get('PRC_overrides');
226
    }
227
228
229
    /**
230
     * get order
231
     *
232
     * @return int
233
     * @throws EE_Error
234
     * @throws InvalidArgumentException
235
     * @throws ReflectionException
236
     * @throws InvalidDataTypeException
237
     * @throws InvalidInterfaceException
238
     */
239
    public function order()
240
    {
241
        return $this->get('PRC_order');
242
    }
243
244
245
    /**
246
     * get the author of the price
247
     *
248
     * @return int
249
     * @throws EE_Error
250
     * @throws InvalidArgumentException
251
     * @throws ReflectionException
252
     * @throws InvalidDataTypeException
253
     * @throws InvalidInterfaceException
254
     * @since 4.5.0
255
     */
256
    public function wp_user()
257
    {
258
        return $this->get('PRC_wp_user');
259
    }
260
261
262
    /**
263
     * get is_default
264
     *
265
     * @return bool
266
     * @throws EE_Error
267
     * @throws InvalidArgumentException
268
     * @throws ReflectionException
269
     * @throws InvalidDataTypeException
270
     * @throws InvalidInterfaceException
271
     */
272
    public function is_default()
273
    {
274
        return $this->get('PRC_is_default');
275
    }
276
277
278
    /**
279
     * get deleted
280
     *
281
     * @return bool
282
     * @throws EE_Error
283
     * @throws InvalidArgumentException
284
     * @throws ReflectionException
285
     * @throws InvalidDataTypeException
286
     * @throws InvalidInterfaceException
287
     */
288
    public function deleted()
289
    {
290
        return $this->get('PRC_deleted');
291
    }
292
293
294
    /**
295
     * @return bool
296
     * @throws EE_Error
297
     * @throws InvalidArgumentException
298
     * @throws ReflectionException
299
     * @throws InvalidDataTypeException
300
     * @throws InvalidInterfaceException
301
     */
302
    public function parent()
303
    {
304
        return $this->get('PRC_parent');
305
    }
306
307
308
    // some helper methods for getting info on the price_type for this price
309
310
311
    /**
312
     * return whether the price is a base price or not
313
     *
314
     * @return boolean
315
     * @throws EE_Error
316
     * @throws InvalidArgumentException
317
     * @throws InvalidDataTypeException
318
     * @throws InvalidInterfaceException
319
     * @throws ReflectionException
320
     */
321
    public function is_base_price()
322
    {
323
        $price_type = $this->type_obj();
324
        return $price_type->is_base_price();
325
    }
326
327
328
    /**
329
     * @return EE_Base_Class|EE_Price_Type
330
     * @throws EE_Error
331
     * @throws InvalidArgumentException
332
     * @throws ReflectionException
333
     * @throws InvalidDataTypeException
334
     * @throws InvalidInterfaceException
335
     */
336
    public function type_obj()
337
    {
338
        return $this->get_first_related('Price_Type');
339
    }
340
341
342
    /**
343
     * @return int
344
     * @throws EE_Error
345
     * @throws InvalidArgumentException
346
     * @throws ReflectionException
347
     * @throws InvalidDataTypeException
348
     * @throws InvalidInterfaceException
349
     */
350
    public function type_order()
351
    {
352
        return $this->get_first_related('Price_Type')->order();
353
    }
354
355
356
    /**
357
     * Simply indicates whether this price increases or decreases the total
358
     *
359
     * @return boolean true = discount, otherwise adds to the total
360
     * @throws EE_Error
361
     * @throws InvalidArgumentException
362
     * @throws ReflectionException
363
     * @throws InvalidDataTypeException
364
     * @throws InvalidInterfaceException
365
     */
366
    public function is_discount()
367
    {
368
        $price_type = $this->type_obj();
369
        return $price_type->is_discount();
370
    }
371
372
373
    /**
374
     * whether the price is a percentage or not
375
     *
376
     * @return boolean
377
     * @throws EE_Error
378
     * @throws InvalidArgumentException
379
     * @throws InvalidDataTypeException
380
     * @throws InvalidInterfaceException
381
     * @throws ReflectionException
382
     */
383
    public function is_percent()
384
    {
385
        $price_type = $this->type_obj();
386
        return $price_type->is_percent();
387
    }
388
389
390
    /**
391
     * whether the price is a percentage or not
392
     *
393
     * @return boolean
394
     * @throws EE_Error
395
     * @throws InvalidArgumentException
396
     * @throws ReflectionException
397
     * @throws InvalidDataTypeException
398
     * @throws InvalidInterfaceException
399
     */
400
    public function is_surcharge()
401
    {
402
        $price_type = $this->type_obj();
403
        return $price_type->is_surcharge();
404
    }
405
406
    /**
407
     * whether the price is a percentage or not
408
     *
409
     * @return boolean
410
     * @throws EE_Error
411
     * @throws InvalidArgumentException
412
     * @throws ReflectionException
413
     * @throws InvalidDataTypeException
414
     * @throws InvalidInterfaceException
415
     */
416
    public function is_tax()
417
    {
418
        $price_type = $this->type_obj();
419
        return $price_type->is_tax();
420
    }
421
422
423
    /**
424
     * return pretty price dependant on whether its a dollar or percent.
425
     *
426
     * @return string
427
     * @throws EE_Error
428
     * @throws InvalidArgumentException
429
     * @throws ReflectionException
430
     * @throws InvalidDataTypeException
431
     * @throws InvalidInterfaceException
432
     * @since 4.4.0
433
     */
434
    public function pretty_price()
435
    {
436
        return ! $this->is_percent()
437
            ? $this->get_pretty('PRC_amount')
438
            : $this->get('PRC_amount') . '%';
439
    }
440
441
442
    /**
443
     * @return mixed
444
     * @throws EE_Error
445
     * @throws InvalidArgumentException
446
     * @throws ReflectionException
447
     * @throws InvalidDataTypeException
448
     * @throws InvalidInterfaceException
449
     */
450
    public function get_price_without_currency_symbol()
451
    {
452
        return str_replace(
453
            EE_Registry::instance()->CFG->currency->sign,
454
            '',
455
            $this->get_pretty('PRC_amount')
456
        );
457
    }
458
}
459