Completed
Branch dev (81ae86)
by
unknown
27:37 queued 17:44
created

EEM_Price::getAllDefaultTaxes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
6
/**
7
 * Price Model
8
 *
9
 * @package             Event Espresso
10
 * @subpackage          includes/models/EEM_Price.model.php
11
 * @author              Mike Nelson
12
 */
13
class EEM_Price extends EEM_Soft_Delete_Base
14
{
15
16
    // private instance of the EEM_Price object
17
    protected static $_instance;
18
19
20
    /**
21
     * private constructor to prevent direct creation
22
     *
23
     * @Constructor
24
     * @param string $timezone string representing the timezone we want to set for returned Date Time Strings
25
     *                         (and any incoming timezone data that gets saved).
26
     *                         Note this just sends the timezone info to the date time model field objects.
27
     *                         Default is NULL
28
     *                         (and will be assumed using the set timezone in the 'timezone_string' wp option)
29
     */
30
    protected function __construct($timezone)
31
    {
32
        require_once(EE_MODELS . 'EEM_Price_Type.model.php');
33
        $this->singular_item = __('Price', 'event_espresso');
34
        $this->plural_item   = __('Prices', 'event_espresso');
35
36
        $this->_tables          = [
37
            'Price' => new EE_Primary_Table('esp_price', 'PRC_ID'),
38
        ];
39
        $this->_fields          = [
40
            'Price' => [
41
                'PRC_ID'         => new EE_Primary_Key_Int_Field(
42
                    'PRC_ID',
43
                    'Price ID'
44
                ),
45
                'PRT_ID'         => new EE_Foreign_Key_Int_Field(
46
                    'PRT_ID',
47
                    esc_html__('Price type Id', 'event_espresso'),
48
                    false,
49
                    null,
50
                    'Price_Type'
51
                ),
52
                'PRC_amount'     => new EE_Money_Field(
53
                    'PRC_amount',
54
                    esc_html__('Price Amount', 'event_espresso'),
55
                    false,
56
                    0
57
                ),
58
                'PRC_name'       => new EE_Plain_Text_Field(
59
                    'PRC_name',
60
                    esc_html__('Name of Price', 'event_espresso'),
61
                    false,
62
                    ''
63
                ),
64
                'PRC_desc'       => new EE_Post_Content_Field(
65
                    'PRC_desc',
66
                    esc_html__('Price Description', 'event_espresso'),
67
                    false,
68
                    ''
69
                ),
70
                'PRC_is_default' => new EE_Boolean_Field(
71
                    'PRC_is_default',
72
                    esc_html__('Flag indicating whether price is a default price', 'event_espresso'),
73
                    false,
74
                    false
75
                ),
76
                'PRC_overrides'  => new EE_Integer_Field(
77
                    'PRC_overrides',
78
                    esc_html__(
79
                        'Price ID for a global Price that will be overridden by this Price  ( for replacing default prices )',
80
                        'event_espresso'
81
                    ),
82
                    true,
83
                    0
84
                ),
85
                'PRC_order'      => new EE_Integer_Field(
86
                    'PRC_order',
87
                    esc_html__(
88
                        'Order of Application of Price (lower numbers apply first?)',
89
                        'event_espresso'
90
                    ),
91
                    false,
92
                    1
93
                ),
94
                'PRC_deleted'    => new EE_Trashed_Flag_Field(
95
                    'PRC_deleted',
96
                    esc_html__('Flag Indicating if this has been deleted or not', 'event_espresso'),
97
                    false,
98
                    false
99
                ),
100
                'PRC_parent'     => new EE_Integer_Field(
101
                    'PRC_parent',
102
                    esc_html__('Indicates what PRC_ID is the parent of this PRC_ID', 'event_espresso'),
103
                    true,
104
                    0
105
                ),
106
                'PRC_wp_user'    => new EE_WP_User_Field(
107
                    'PRC_wp_user',
108
                    esc_html__('Price Creator ID', 'event_espresso'),
109
                    false
110
                ),
111
            ],
112
        ];
113
        $this->_model_relations = [
114
            'Ticket'     => new EE_HABTM_Relation('Ticket_Price'),
115
            'Price_Type' => new EE_Belongs_To_Relation(),
116
            'WP_User'    => new EE_Belongs_To_Relation(),
117
        ];
118
        // this model is generally available for reading
119
        $this->_cap_restriction_generators[ EEM_Base::caps_read ] =
120
            new EE_Restriction_Generator_Default_Public(
121
                'PRC_is_default',
122
                'Ticket.Datetime.Event'
123
            );
124
        // account for default tickets in the caps
125
        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] =
126
            new EE_Restriction_Generator_Default_Protected(
127
                'PRC_is_default',
128
                'Ticket.Datetime.Event'
129
            );
130
        $this->_cap_restriction_generators[ EEM_Base::caps_edit ]       =
131
            new EE_Restriction_Generator_Default_Protected(
132
                'PRC_is_default',
133
                'Ticket.Datetime.Event'
134
            );
135
        $this->_cap_restriction_generators[ EEM_Base::caps_delete ]     =
136
            new EE_Restriction_Generator_Default_Protected(
137
                'PRC_is_default',
138
                'Ticket.Datetime.Event'
139
            );
140
        parent::__construct($timezone);
141
    }
142
143
144
    /**
145
     * instantiate a new price object with blank/empty properties
146
     *
147
     * @return mixed array on success, FALSE on fail
148
     */
149
    public function get_new_price()
150
    {
151
        return $this->create_default_object();
152
    }
153
154
155
    /**
156
     * retrieve  ALL prices from db
157
     *
158
     * @return EE_Base_Class[]|EE_PRice[]
159
     * @throws EE_Error
160
     */
161
    public function get_all_prices()
162
    {
163
        // retrieve all prices
164
        return $this->get_all(['order_by' => ['PRC_amount' => 'ASC']]);
165
    }
166
167
168
    /**
169
     * retrieve all active prices for a particular event
170
     *
171
     * @param int $EVT_ID
172
     * @return array on success
173
     * @throws EE_Error
174
     */
175
    public function get_all_event_prices($EVT_ID = 0)
176
    {
177
        return $this->get_all(
178
            [
179
                [
180
                    'EVT_ID'            => $EVT_ID,
181
                    'Price_Type.PBT_ID' => ['!=', EEM_Price_Type::base_type_tax],
182
                ],
183
                'order_by' => $this->_order_by_array_for_get_all_method(),
184
            ]
185
        );
186
    }
187
188
189
    /**
190
     * retrieve all active global prices (that are not taxes (PBT_ID=4)) for a particular event
191
     *
192
     * @param boolean $count return count
193
     * @param bool    $include_taxes
194
     * @return bool|EE_Base_Class[]|EE_PRice[]
195
     * @throws EE_Error
196
     */
197
    public function get_all_default_prices($count = false, $include_taxes = false)
198
    {
199
        $_where = [
200
            'PRC_deleted'    => 0,
201
            'PRC_is_default' => 1,
202
        ];
203
        if (! $include_taxes) {
204
            $_where['Price_Type.PBT_ID'] = ['!=', 4];
205
        }
206
        $_query_params = [
207
            $_where,
208
            'order_by' => $this->_order_by_array_for_get_all_method(),
209
        ];
210
        return $count ? $this->count([$_where]) : $this->get_all($_query_params);
211
    }
212
213
214
    /**
215
     * retrieve all active global prices that are taxes
216
     *
217
     * @return bool|EE_Base_Class[]|EE_PRice[]
218
     * @throws EE_Error
219
     * @since   $VID:$
220
     */
221
    public function getAllDefaultTaxes()
222
    {
223
        return $this->get_all(
224
            [
225
                [
226
                    'PRC_deleted'    => 0,
227
                    'PRC_is_default' => 1,
228
                    'Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax
229
                ],
230
                'order_by' => [
231
                    'Price_Type.PRT_order' => 'ASC',
232
                    'PRC_order' => 'ASC'
233
                ],
234
            ]
235
        );
236
    }
237
238
239
    /**
240
     * retrieve all prices that are taxes
241
     *
242
     * @return EE_Base_Class[]|EE_PRice[]
243
     * @throws EE_Error
244
     * @throws InvalidArgumentException
245
     * @throws ReflectionException
246
     * @throws InvalidDataTypeException
247
     * @throws InvalidInterfaceException
248
     */
249
    public function get_all_prices_that_are_taxes()
250
    {
251
        $taxes     = [];
252
        $all_taxes = $this->get_all(
253
            [
254
                ['Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax],
255
                'order_by' => ['Price_Type.PRT_order' => 'ASC', 'PRC_order' => 'ASC'],
256
            ]
257
        );
258
        foreach ($all_taxes as $tax) {
259
            if ($tax instanceof EE_Price) {
260
                $taxes[ $tax->order() ][ $tax->ID() ] = $tax;
261
            }
262
        }
263
        return $taxes;
264
    }
265
266
267
    /**
268
     * retrieve all prices for an ticket plus default global prices, but not taxes
269
     *
270
     * @param int $TKT_ID the id of the event.  If not included then we assume that this is a new ticket.
271
     * @return EE_Base_Class[]|EE_PRice[]|boolean
272
     * @throws EE_Error
273
     */
274
    public function get_all_ticket_prices_for_admin($TKT_ID = 0)
275
    {
276
        $array_of_price_objects = [];
277
        if (empty($TKT_ID)) {
278
            // if there is no tkt, get prices with no tkt ID, are global, are not a tax, and are active
279
            // return that list
280
            $default_prices = $this->get_all_default_prices();
281
282
            if ($default_prices) {
283
                foreach ($default_prices as $price) {
0 ignored issues
show
Bug introduced by
The expression $default_prices of type integer|array<integer,object<EE_Base_Class>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
284
                    if ($price instanceof EE_Price) {
285
                        $array_of_price_objects[ $price->type() ][] = $price;
286
                    }
287
                }
288
                return $array_of_price_objects;
289
            }
290
            return [];
291
        }
292
        $ticket_prices = $this->get_all(
293
            [
294
                [
295
                    'TKT_ID'      => $TKT_ID,
296
                    'PRC_deleted' => 0,
297
                ],
298
                'order_by' => ['PRC_order' => 'ASC'],
299
            ]
300
        );
301
302
        if (! empty($ticket_prices)) {
303
            foreach ($ticket_prices as $price) {
304
                if ($price instanceof EE_Price) {
305
                    $array_of_price_objects[ $price->type() ][] = $price;
306
                }
307
            }
308
            return $array_of_price_objects;
309
        }
310
        return false;
311
    }
312
313
314
    /**
315
     * _sort_event_prices_by_type
316
     *
317
     * @param EE_Price $price_a
318
     * @param EE_Price $price_b
319
     * @return bool false on fail
320
     */
321
    public function _sort_event_prices_by_type(EE_Price $price_a, EE_Price $price_b)
322
    {
323
        if ($price_a->type_obj()->order() === $price_b->type_obj()->order()) {
324
            return $this->_sort_event_prices_by_order($price_a, $price_b);
325
        }
326
        return $price_a->type_obj()->order() < $price_b->type_obj()->order() ? -1 : 1;
327
    }
328
329
330
    /**
331
     *        _sort_event_prices_by_order
332
     *
333
     * @param EE_Price $price_a
334
     * @param EE_Price $price_b
335
     * @return bool false on fail
336
     */
337
    public function _sort_event_prices_by_order(EE_Price $price_a, EE_Price $price_b)
338
    {
339
        if ($price_a->order() === $price_b->order()) {
340
            return 0;
341
        }
342
        return $price_a->order() < $price_b->order() ? -1 : 1;
343
    }
344
345
346
    /**
347
     * get all prices of a specific type
348
     *
349
     * @param int $type - PRT_ID
350
     * @return EE_Base_Class[]|EE_PRice[]
351
     * @throws EE_Error
352
     */
353
    public function get_all_prices_that_are_type($type = 0)
354
    {
355
        return $this->get_all(
356
            [
357
                [
358
                    'PRT_ID' => $type,
359
                ],
360
                'order_by' => $this->_order_by_array_for_get_all_method(),
361
            ]
362
        );
363
    }
364
365
366
    /**
367
     * Returns an array of the normal 'order_by' query parameter provided to the get_all query.
368
     * Of course you don't have to use it, but this is the order we usually want to sort prices by
369
     *
370
     * @return array which can be used like so: $this->get_all(array(array(...where
371
     *               stuff...),'order_by'=>$this->_order_by_array_for_get_all_method()));
372
     */
373
    public function _order_by_array_for_get_all_method()
374
    {
375
        return [
376
            'PRC_order'            => 'ASC',
377
            'Price_Type.PRT_order' => 'ASC',
378
            'PRC_ID'               => 'ASC',
379
        ];
380
    }
381
}
382