Completed
Branch TASK/update-about-page (5cee29)
by
unknown
34:34 queued 26:08
created
core/domain/services/event/EventSpacesCalculator.php 1 patch
Indentation   +711 added lines, -711 removed lines patch added patch discarded remove patch
@@ -26,715 +26,715 @@
 block discarded – undo
26 26
 class EventSpacesCalculator
27 27
 {
28 28
 
29
-    /**
30
-     * @var EE_Event $event
31
-     */
32
-    private $event;
33
-
34
-    /**
35
-     * @var array $datetime_query_params
36
-     */
37
-    private $datetime_query_params;
38
-
39
-    /**
40
-     * @var EE_Ticket[] $active_tickets
41
-     */
42
-    private $active_tickets = array();
43
-
44
-    /**
45
-     * @var EE_Datetime[] $datetimes
46
-     */
47
-    private $datetimes = array();
48
-
49
-    /**
50
-     * Array of Ticket IDs grouped by Datetime
51
-     *
52
-     * @var array $datetimes
53
-     */
54
-    private $datetime_tickets = array();
55
-
56
-    /**
57
-     * Max spaces for each Datetime (reg limit - previous sold)
58
-     *
59
-     * @var array $datetime_spaces
60
-     */
61
-    private $datetime_spaces = array();
62
-
63
-    /**
64
-     * Array of Datetime IDs grouped by Ticket
65
-     *
66
-     * @var array[] $ticket_datetimes
67
-     */
68
-    private $ticket_datetimes = array();
69
-
70
-    /**
71
-     * maximum ticket quantities for each ticket (adjusted for reg limit)
72
-     *
73
-     * @var array $ticket_quantities
74
-     */
75
-    private $ticket_quantities = array();
76
-
77
-    /**
78
-     * total quantity of sold and reserved for each ticket
79
-     *
80
-     * @var array $tickets_sold
81
-     */
82
-    private $tickets_sold = array();
83
-
84
-    /**
85
-     * total spaces available across all datetimes
86
-     *
87
-     * @var array $total_spaces
88
-     */
89
-    private $total_spaces = array();
90
-
91
-    /**
92
-     * @var boolean $debug
93
-     */
94
-    private $debug = false; // true false
95
-
96
-    /**
97
-     * @var null|int $spaces_remaining
98
-     */
99
-    private $spaces_remaining;
100
-
101
-    /**
102
-     * @var null|int $total_spaces_available
103
-     */
104
-    private $total_spaces_available;
105
-
106
-
107
-    /**
108
-     * EventSpacesCalculator constructor.
109
-     *
110
-     * @param EE_Event $event
111
-     * @param array    $datetime_query_params
112
-     * @throws EE_Error
113
-     */
114
-    public function __construct(EE_Event $event, array $datetime_query_params = array())
115
-    {
116
-        if ($this->debug) {
117
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
118
-            \EEH_Debug_Tools::printr((string) $event->ID(), 'For event', __FILE__, __LINE__);
119
-        }
120
-        $this->event = $event;
121
-        $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC'));
122
-        $this->setHooks();
123
-    }
124
-
125
-
126
-    /**
127
-     * @return void
128
-     */
129
-    private function setHooks()
130
-    {
131
-        add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults'));
132
-        add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults'));
133
-        add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults'));
134
-        add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults'));
135
-        add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults'));
136
-        add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults'));
137
-        add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults'));
138
-        add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults'));
139
-    }
140
-
141
-
142
-    /**
143
-     * @return void
144
-     */
145
-    public function clearResults()
146
-    {
147
-        if ($this->debug) {
148
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
149
-        }
150
-        $this->spaces_remaining = null;
151
-        $this->total_spaces_available = null;
152
-    }
153
-
154
-
155
-    /**
156
-     * @return EE_Ticket[]
157
-     * @throws EE_Error
158
-     * @throws InvalidDataTypeException
159
-     * @throws InvalidInterfaceException
160
-     * @throws InvalidArgumentException
161
-     */
162
-    public function getActiveTickets()
163
-    {
164
-        if (empty($this->active_tickets)) {
165
-            $this->active_tickets = $this->event->tickets(
166
-                array(
167
-                    array('TKT_deleted' => false),
168
-                    'order_by' => array('TKT_qty' => 'ASC'),
169
-                )
170
-            );
171
-        }
172
-        return $this->active_tickets;
173
-    }
174
-
175
-
176
-    /**
177
-     * @param EE_Ticket[] $active_tickets
178
-     * @throws EE_Error
179
-     * @throws DomainException
180
-     * @throws UnexpectedEntityException
181
-     */
182
-    public function setActiveTickets(array $active_tickets = array())
183
-    {
184
-        if (! empty($active_tickets)) {
185
-            foreach ($active_tickets as $active_ticket) {
186
-                $this->validateTicket($active_ticket);
187
-            }
188
-            // sort incoming array by ticket quantity (asc)
189
-            usort(
190
-                $active_tickets,
191
-                function (EE_Ticket $a, EE_Ticket $b) {
192
-                    if ($a->qty() === $b->qty()) {
193
-                        return 0;
194
-                    }
195
-                    return ($a->qty() < $b->qty())
196
-                        ? -1
197
-                        : 1;
198
-                }
199
-            );
200
-        }
201
-        $this->active_tickets = $active_tickets;
202
-    }
203
-
204
-
205
-    /**
206
-     * @param $ticket
207
-     * @throws DomainException
208
-     * @throws EE_Error
209
-     * @throws UnexpectedEntityException
210
-     */
211
-    private function validateTicket($ticket)
212
-    {
213
-        if (! $ticket instanceof EE_Ticket) {
214
-            throw new DomainException(
215
-                esc_html__(
216
-                    'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.',
217
-                    'event_espresso'
218
-                )
219
-            );
220
-        }
221
-        if ($ticket->get_event_ID() !== $this->event->ID()) {
222
-            throw new DomainException(
223
-                sprintf(
224
-                    esc_html__(
225
-                        'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.',
226
-                        'event_espresso'
227
-                    ),
228
-                    $ticket->get_event_ID(),
229
-                    $this->event->ID()
230
-                )
231
-            );
232
-        }
233
-    }
234
-
235
-
236
-    /**
237
-     * @return EE_Datetime[]
238
-     */
239
-    public function getDatetimes()
240
-    {
241
-        return $this->datetimes;
242
-    }
243
-
244
-
245
-    /**
246
-     * @param EE_Datetime $datetime
247
-     * @throws EE_Error
248
-     * @throws DomainException
249
-     */
250
-    public function setDatetime(EE_Datetime $datetime)
251
-    {
252
-        if ($datetime->event()->ID() !== $this->event->ID()) {
253
-            throw new DomainException(
254
-                sprintf(
255
-                    esc_html__(
256
-                        'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.',
257
-                        'event_espresso'
258
-                    ),
259
-                    $datetime->event()->ID(),
260
-                    $this->event->ID()
261
-                )
262
-            );
263
-        }
264
-        $this->datetimes[ $datetime->ID() ] = $datetime;
265
-    }
266
-
267
-
268
-    /**
269
-     * calculate spaces remaining based on "saleable" tickets
270
-     *
271
-     * @return float|int
272
-     * @throws EE_Error
273
-     * @throws DomainException
274
-     * @throws UnexpectedEntityException
275
-     * @throws InvalidDataTypeException
276
-     * @throws InvalidInterfaceException
277
-     * @throws InvalidArgumentException
278
-     */
279
-    public function spacesRemaining()
280
-    {
281
-        if ($this->debug) {
282
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
283
-        }
284
-        if ($this->spaces_remaining === null) {
285
-            $this->initialize();
286
-            $this->spaces_remaining = $this->calculate();
287
-        }
288
-        return $this->spaces_remaining;
289
-    }
290
-
291
-
292
-    /**
293
-     * calculates total available spaces for an event with no regard for sold tickets
294
-     *
295
-     * @return int|float
296
-     * @throws EE_Error
297
-     * @throws DomainException
298
-     * @throws UnexpectedEntityException
299
-     * @throws InvalidDataTypeException
300
-     * @throws InvalidInterfaceException
301
-     * @throws InvalidArgumentException
302
-     */
303
-    public function totalSpacesAvailable()
304
-    {
305
-        if ($this->debug) {
306
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
307
-        }
308
-        if ($this->total_spaces_available === null) {
309
-            $this->initialize();
310
-            $this->total_spaces_available = $this->calculate(false);
311
-        }
312
-        return $this->total_spaces_available;
313
-    }
314
-
315
-
316
-    /**
317
-     * Loops through the active tickets for the event
318
-     * and builds a series of data arrays that will be used for calculating
319
-     * the total maximum available spaces, as well as the spaces remaining.
320
-     * Because ticket quantities affect datetime spaces and vice versa,
321
-     * we need to be constantly updating these data arrays as things change,
322
-     * which is the entire reason for their existence.
323
-     *
324
-     * @throws EE_Error
325
-     * @throws DomainException
326
-     * @throws UnexpectedEntityException
327
-     * @throws InvalidDataTypeException
328
-     * @throws InvalidInterfaceException
329
-     * @throws InvalidArgumentException
330
-     */
331
-    private function initialize()
332
-    {
333
-        if ($this->debug) {
334
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
335
-        }
336
-        $this->datetime_tickets = array();
337
-        $this->datetime_spaces = array();
338
-        $this->ticket_datetimes = array();
339
-        $this->ticket_quantities = array();
340
-        $this->tickets_sold = array();
341
-        $this->total_spaces = array();
342
-        $active_tickets = $this->getActiveTickets();
343
-        if (! empty($active_tickets)) {
344
-            foreach ($active_tickets as $ticket) {
345
-                $this->validateTicket($ticket);
346
-                // we need to index our data arrays using strings for the purpose of sorting,
347
-                // but we also need them to be unique, so  we'll just prepend a letter T to the ID
348
-                $ticket_identifier = "T{$ticket->ID()}";
349
-                // to start, we'll just consider the raw qty to be the maximum availability for this ticket,
350
-                // unless the ticket is past its "sell until" date, in which case the qty will be 0
351
-                $max_tickets = $ticket->is_expired() ? 0 : $ticket->qty();
352
-                // but we'll adjust that after looping over each datetime for the ticket and checking reg limits
353
-                $ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
354
-                foreach ($ticket_datetimes as $datetime) {
355
-                    // save all datetimes
356
-                    $this->setDatetime($datetime);
357
-                    $datetime_identifier = "D{$datetime->ID()}";
358
-                    $reg_limit = $datetime->reg_limit();
359
-                    // ticket quantity can not exceed datetime reg limit
360
-                    $max_tickets = min($max_tickets, $reg_limit);
361
-                    // as described earlier, because we need to be able to constantly adjust numbers for things,
362
-                    // we are going to move all of our data into the following arrays:
363
-                    // datetime spaces initially represents the reg limit for each datetime,
364
-                    // but this will get adjusted as tickets are accounted for
365
-                    $this->datetime_spaces[ $datetime_identifier ] = $reg_limit;
366
-                    // just an array of ticket IDs grouped by datetime
367
-                    $this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier;
368
-                    // and an array of datetime IDs grouped by ticket
369
-                    $this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier;
370
-                }
371
-                // total quantity of sold and reserved for each ticket
372
-                $this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved();
373
-                // and the maximum ticket quantities for each ticket (adjusted for reg limit)
374
-                $this->ticket_quantities[ $ticket_identifier ] = $max_tickets;
375
-            }
376
-        }
377
-        // sort datetime spaces by reg limit, but maintain our string indexes
378
-        asort($this->datetime_spaces, SORT_NUMERIC);
379
-        // datetime tickets need to be sorted in the SAME order as the above array...
380
-        // so we'll just use array_merge() to take the structure of datetime_spaces
381
-        // but overwrite all of the data with that from datetime_tickets
382
-        $this->datetime_tickets = array_merge(
383
-            $this->datetime_spaces,
384
-            $this->datetime_tickets
385
-        );
386
-        if ($this->debug) {
387
-            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
388
-            \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__);
389
-            \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__);
390
-        }
391
-    }
392
-
393
-
394
-    /**
395
-     * performs calculations on initialized data
396
-     *
397
-     * @param bool $consider_sold
398
-     * @return int|float
399
-     */
400
-    private function calculate($consider_sold = true)
401
-    {
402
-        if ($this->debug) {
403
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
404
-            \EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__);
405
-        }
406
-        if ($consider_sold) {
407
-            // subtract amounts sold from all ticket quantities and datetime spaces
408
-            $this->adjustTicketQuantitiesDueToSales();
409
-        }
410
-        foreach ($this->datetime_tickets as $datetime_identifier => $tickets) {
411
-            $this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets);
412
-        }
413
-        // total spaces available is just the sum of the spaces available for each datetime
414
-        $spaces_remaining = array_sum($this->total_spaces);
415
-        if ($this->debug) {
416
-            \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__);
417
-            \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__);
418
-            \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__);
419
-        }
420
-        return $spaces_remaining;
421
-    }
422
-
423
-
424
-    /**
425
-     * subtracts amount of  tickets sold from ticket quantities and datetime spaces
426
-     */
427
-    private function adjustTicketQuantitiesDueToSales()
428
-    {
429
-        if ($this->debug) {
430
-            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
431
-        }
432
-        foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) {
433
-            if (isset($this->ticket_quantities[ $ticket_identifier ])) {
434
-                $this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold;
435
-                // don't let values go below zero
436
-                $this->ticket_quantities[ $ticket_identifier ] = max(
437
-                    $this->ticket_quantities[ $ticket_identifier ],
438
-                    0
439
-                );
440
-                if ($this->debug) {
441
-                    \EEH_Debug_Tools::printr(
442
-                        "{$tickets_sold} sales for ticket {$ticket_identifier} ",
443
-                        'subtracting',
444
-                        __FILE__,
445
-                        __LINE__
446
-                    );
447
-                }
448
-            }
449
-            if (isset($this->ticket_datetimes[ $ticket_identifier ])
450
-                && is_array($this->ticket_datetimes[ $ticket_identifier ])
451
-            ) {
452
-                foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) {
453
-                    if (isset($this->ticket_quantities[ $ticket_identifier ])) {
454
-                        $this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold;
455
-                        // don't let values go below zero
456
-                        $this->datetime_spaces[ $ticket_datetime ] = max(
457
-                            $this->datetime_spaces[ $ticket_datetime ],
458
-                            0
459
-                        );
460
-                        if ($this->debug) {
461
-                            \EEH_Debug_Tools::printr(
462
-                                "{$tickets_sold} sales for datetime {$ticket_datetime} ",
463
-                                'subtracting',
464
-                                __FILE__,
465
-                                __LINE__
466
-                            );
467
-                        }
468
-                    }
469
-                }
470
-            }
471
-        }
472
-    }
473
-
474
-
475
-    /**
476
-     * @param string $datetime_identifier
477
-     * @param array  $tickets
478
-     */
479
-    private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets)
480
-    {
481
-        // make sure a reg limit is set for the datetime
482
-        $reg_limit = isset($this->datetime_spaces[ $datetime_identifier ])
483
-            ? $this->datetime_spaces[ $datetime_identifier ]
484
-            : 0;
485
-        // and bail if it is not
486
-        if (! $reg_limit) {
487
-            if ($this->debug) {
488
-                \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__);
489
-            }
490
-            return;
491
-        }
492
-        if ($this->debug) {
493
-            \EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1);
494
-            \EEH_Debug_Tools::printr(
495
-                "{$reg_limit}",
496
-                'REG LIMIT',
497
-                __FILE__,
498
-                __LINE__
499
-            );
500
-        }
501
-        // number of allocated spaces always starts at zero
502
-        $spaces_allocated = 0;
503
-        $this->total_spaces[ $datetime_identifier ] = 0;
504
-        foreach ($tickets as $ticket_identifier) {
505
-            $spaces_allocated = $this->calculateAvailableSpacesForTicket(
506
-                $datetime_identifier,
507
-                $reg_limit,
508
-                $ticket_identifier,
509
-                $spaces_allocated
510
-            );
511
-        }
512
-        // spaces can't be negative
513
-        $spaces_allocated = max($spaces_allocated, 0);
514
-        if ($spaces_allocated) {
515
-            // track any non-zero values
516
-            $this->total_spaces[ $datetime_identifier ] += $spaces_allocated;
517
-            if ($this->debug) {
518
-                \EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__);
519
-            }
520
-        } else {
521
-            if ($this->debug) {
522
-                \EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__);
523
-            }
524
-        }
525
-        if ($this->debug) {
526
-            \EEH_Debug_Tools::printr(
527
-                $this->total_spaces[ $datetime_identifier ],
528
-                '$total_spaces',
529
-                __FILE__,
530
-                __LINE__
531
-            );
532
-            \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__);
533
-            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
534
-        }
535
-    }
536
-
537
-
538
-    /**
539
-     * @param string $datetime_identifier
540
-     * @param int    $reg_limit
541
-     * @param string $ticket_identifier
542
-     * @param int    $spaces_allocated
543
-     * @return int
544
-     */
545
-    private function calculateAvailableSpacesForTicket(
546
-        $datetime_identifier,
547
-        $reg_limit,
548
-        $ticket_identifier,
549
-        $spaces_allocated
550
-    ) {
551
-        // make sure ticket quantity is set
552
-        $ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ])
553
-            ? $this->ticket_quantities[ $ticket_identifier ]
554
-            : 0;
555
-        if ($this->debug) {
556
-            \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__);
557
-            \EEH_Debug_Tools::printr(
558
-                "{$ticket_quantity}",
559
-                "ticket $ticket_identifier quantity: ",
560
-                __FILE__,
561
-                __LINE__,
562
-                2
563
-            );
564
-        }
565
-        if ($ticket_quantity) {
566
-            if ($this->debug) {
567
-                \EEH_Debug_Tools::printr(
568
-                    ($spaces_allocated <= $reg_limit)
569
-                        ? 'true'
570
-                        : 'false',
571
-                    ' . spaces_allocated <= reg_limit = ',
572
-                    __FILE__,
573
-                    __LINE__
574
-                );
575
-            }
576
-            // if the datetime is NOT at full capacity yet
577
-            if ($spaces_allocated <= $reg_limit) {
578
-                // then the maximum ticket quantity we can allocate is the lowest value of either:
579
-                //  the number of remaining spaces for the datetime, which is the limit - spaces already taken
580
-                //  or the maximum ticket quantity
581
-                $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity);
582
-                // adjust the available quantity in our tracking array
583
-                $this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity;
584
-                // and increment spaces allocated for this datetime
585
-                $spaces_allocated += $ticket_quantity;
586
-                $at_capacity = $spaces_allocated >= $reg_limit;
587
-                if ($this->debug) {
588
-                    \EEH_Debug_Tools::printr(
589
-                        "{$ticket_quantity} {$ticket_identifier} tickets",
590
-                        ' > > allocate ',
591
-                        __FILE__,
592
-                        __LINE__,
593
-                        3
594
-                    );
595
-                    if ($at_capacity) {
596
-                        \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3);
597
-                    }
598
-                }
599
-                // now adjust all other datetimes that allow access to this ticket
600
-                $this->adjustDatetimes(
601
-                    $datetime_identifier,
602
-                    $ticket_identifier,
603
-                    $ticket_quantity,
604
-                    $at_capacity
605
-                );
606
-            }
607
-        }
608
-        return $spaces_allocated;
609
-    }
610
-
611
-
612
-    /**
613
-     * subtracts ticket amounts from all datetime reg limits
614
-     * that allow access to the ticket specified,
615
-     * because that ticket could be used
616
-     * to attend any of the datetimes it has access to
617
-     *
618
-     * @param string $datetime_identifier
619
-     * @param string $ticket_identifier
620
-     * @param bool   $at_capacity
621
-     * @param int    $ticket_quantity
622
-     */
623
-    private function adjustDatetimes(
624
-        $datetime_identifier,
625
-        $ticket_identifier,
626
-        $ticket_quantity,
627
-        $at_capacity
628
-    ) {
629
-        /** @var array $datetime_tickets */
630
-        foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) {
631
-            if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) {
632
-                continue;
633
-            }
634
-            $adjusted = $this->adjustDatetimeSpaces(
635
-                $datetime_ID,
636
-                $ticket_identifier,
637
-                $ticket_quantity
638
-            );
639
-            // skip to next ticket if nothing changed
640
-            if (! ($adjusted || $at_capacity)) {
641
-                continue;
642
-            }
643
-            // then all of it's tickets are now unavailable
644
-            foreach ($datetime_tickets as $datetime_ticket) {
645
-                if (($ticket_identifier === $datetime_ticket || $at_capacity)
646
-                    && isset($this->ticket_quantities[ $datetime_ticket ])
647
-                    && $this->ticket_quantities[ $datetime_ticket ] > 0
648
-                ) {
649
-                    if ($this->debug) {
650
-                        \EEH_Debug_Tools::printr(
651
-                            $datetime_ticket,
652
-                            ' . . . adjust ticket quantities for',
653
-                            __FILE__,
654
-                            __LINE__
655
-                        );
656
-                    }
657
-                    // if this datetime is at full capacity, set any tracked available quantities to zero
658
-                    // otherwise just subtract the ticket quantity
659
-                    $new_quantity = $at_capacity
660
-                        ? 0
661
-                        : $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity;
662
-                    // don't let ticket quantity go below zero
663
-                    $this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0);
664
-                    if ($this->debug) {
665
-                        \EEH_Debug_Tools::printr(
666
-                            $at_capacity
667
-                                ? "0 because Datetime {$datetime_identifier} is at capacity"
668
-                                : "{$this->ticket_quantities[ $datetime_ticket ]}",
669
-                            " . . . . {$datetime_ticket} quantity set to ",
670
-                            __FILE__,
671
-                            __LINE__
672
-                        );
673
-                    }
674
-                }
675
-                // but we also need to adjust spaces for any other datetimes this ticket has access to
676
-                if ($datetime_ticket === $ticket_identifier) {
677
-                    if (isset($this->ticket_datetimes[ $datetime_ticket ])
678
-                        && is_array($this->ticket_datetimes[ $datetime_ticket ])
679
-                    ) {
680
-                        if ($this->debug) {
681
-                            \EEH_Debug_Tools::printr(
682
-                                $datetime_ticket,
683
-                                ' . . adjust other Datetimes for',
684
-                                __FILE__,
685
-                                __LINE__
686
-                            );
687
-                        }
688
-                        foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) {
689
-                            // don't adjust the current datetime twice
690
-                            if ($datetime !== $datetime_identifier) {
691
-                                $this->adjustDatetimeSpaces(
692
-                                    $datetime,
693
-                                    $datetime_ticket,
694
-                                    $ticket_quantity
695
-                                );
696
-                            }
697
-                        }
698
-                    }
699
-                }
700
-            }
701
-        }
702
-    }
703
-
704
-    private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0)
705
-    {
706
-        // does datetime have spaces available?
707
-        // and does the supplied ticket have access to this datetime ?
708
-        if ($this->datetime_spaces[ $datetime_identifier ] > 0
709
-            && isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ])
710
-            && in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true)
711
-        ) {
712
-            if ($this->debug) {
713
-                \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__);
714
-                \EEH_Debug_Tools::printr(
715
-                    "{$this->datetime_spaces[ $datetime_identifier ]}",
716
-                    " . . current  {$datetime_identifier} spaces available",
717
-                    __FILE__,
718
-                    __LINE__
719
-                );
720
-            }
721
-            // then decrement the available spaces for the datetime
722
-            $this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity;
723
-            // but don't let quantities go below zero
724
-            $this->datetime_spaces[ $datetime_identifier ] = max(
725
-                $this->datetime_spaces[ $datetime_identifier ],
726
-                0
727
-            );
728
-            if ($this->debug) {
729
-                \EEH_Debug_Tools::printr(
730
-                    "{$ticket_quantity}",
731
-                    " . . . {$datetime_identifier} capacity reduced by",
732
-                    __FILE__,
733
-                    __LINE__
734
-                );
735
-            }
736
-            return true;
737
-        }
738
-        return false;
739
-    }
29
+	/**
30
+	 * @var EE_Event $event
31
+	 */
32
+	private $event;
33
+
34
+	/**
35
+	 * @var array $datetime_query_params
36
+	 */
37
+	private $datetime_query_params;
38
+
39
+	/**
40
+	 * @var EE_Ticket[] $active_tickets
41
+	 */
42
+	private $active_tickets = array();
43
+
44
+	/**
45
+	 * @var EE_Datetime[] $datetimes
46
+	 */
47
+	private $datetimes = array();
48
+
49
+	/**
50
+	 * Array of Ticket IDs grouped by Datetime
51
+	 *
52
+	 * @var array $datetimes
53
+	 */
54
+	private $datetime_tickets = array();
55
+
56
+	/**
57
+	 * Max spaces for each Datetime (reg limit - previous sold)
58
+	 *
59
+	 * @var array $datetime_spaces
60
+	 */
61
+	private $datetime_spaces = array();
62
+
63
+	/**
64
+	 * Array of Datetime IDs grouped by Ticket
65
+	 *
66
+	 * @var array[] $ticket_datetimes
67
+	 */
68
+	private $ticket_datetimes = array();
69
+
70
+	/**
71
+	 * maximum ticket quantities for each ticket (adjusted for reg limit)
72
+	 *
73
+	 * @var array $ticket_quantities
74
+	 */
75
+	private $ticket_quantities = array();
76
+
77
+	/**
78
+	 * total quantity of sold and reserved for each ticket
79
+	 *
80
+	 * @var array $tickets_sold
81
+	 */
82
+	private $tickets_sold = array();
83
+
84
+	/**
85
+	 * total spaces available across all datetimes
86
+	 *
87
+	 * @var array $total_spaces
88
+	 */
89
+	private $total_spaces = array();
90
+
91
+	/**
92
+	 * @var boolean $debug
93
+	 */
94
+	private $debug = false; // true false
95
+
96
+	/**
97
+	 * @var null|int $spaces_remaining
98
+	 */
99
+	private $spaces_remaining;
100
+
101
+	/**
102
+	 * @var null|int $total_spaces_available
103
+	 */
104
+	private $total_spaces_available;
105
+
106
+
107
+	/**
108
+	 * EventSpacesCalculator constructor.
109
+	 *
110
+	 * @param EE_Event $event
111
+	 * @param array    $datetime_query_params
112
+	 * @throws EE_Error
113
+	 */
114
+	public function __construct(EE_Event $event, array $datetime_query_params = array())
115
+	{
116
+		if ($this->debug) {
117
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
118
+			\EEH_Debug_Tools::printr((string) $event->ID(), 'For event', __FILE__, __LINE__);
119
+		}
120
+		$this->event = $event;
121
+		$this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC'));
122
+		$this->setHooks();
123
+	}
124
+
125
+
126
+	/**
127
+	 * @return void
128
+	 */
129
+	private function setHooks()
130
+	{
131
+		add_action('AHEE__EE_Ticket__increase_sold', array($this, 'clearResults'));
132
+		add_action('AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults'));
133
+		add_action('AHEE__EE_Datetime__increase_sold', array($this, 'clearResults'));
134
+		add_action('AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults'));
135
+		add_action('AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults'));
136
+		add_action('AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults'));
137
+		add_action('AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults'));
138
+		add_action('AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults'));
139
+	}
140
+
141
+
142
+	/**
143
+	 * @return void
144
+	 */
145
+	public function clearResults()
146
+	{
147
+		if ($this->debug) {
148
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 1);
149
+		}
150
+		$this->spaces_remaining = null;
151
+		$this->total_spaces_available = null;
152
+	}
153
+
154
+
155
+	/**
156
+	 * @return EE_Ticket[]
157
+	 * @throws EE_Error
158
+	 * @throws InvalidDataTypeException
159
+	 * @throws InvalidInterfaceException
160
+	 * @throws InvalidArgumentException
161
+	 */
162
+	public function getActiveTickets()
163
+	{
164
+		if (empty($this->active_tickets)) {
165
+			$this->active_tickets = $this->event->tickets(
166
+				array(
167
+					array('TKT_deleted' => false),
168
+					'order_by' => array('TKT_qty' => 'ASC'),
169
+				)
170
+			);
171
+		}
172
+		return $this->active_tickets;
173
+	}
174
+
175
+
176
+	/**
177
+	 * @param EE_Ticket[] $active_tickets
178
+	 * @throws EE_Error
179
+	 * @throws DomainException
180
+	 * @throws UnexpectedEntityException
181
+	 */
182
+	public function setActiveTickets(array $active_tickets = array())
183
+	{
184
+		if (! empty($active_tickets)) {
185
+			foreach ($active_tickets as $active_ticket) {
186
+				$this->validateTicket($active_ticket);
187
+			}
188
+			// sort incoming array by ticket quantity (asc)
189
+			usort(
190
+				$active_tickets,
191
+				function (EE_Ticket $a, EE_Ticket $b) {
192
+					if ($a->qty() === $b->qty()) {
193
+						return 0;
194
+					}
195
+					return ($a->qty() < $b->qty())
196
+						? -1
197
+						: 1;
198
+				}
199
+			);
200
+		}
201
+		$this->active_tickets = $active_tickets;
202
+	}
203
+
204
+
205
+	/**
206
+	 * @param $ticket
207
+	 * @throws DomainException
208
+	 * @throws EE_Error
209
+	 * @throws UnexpectedEntityException
210
+	 */
211
+	private function validateTicket($ticket)
212
+	{
213
+		if (! $ticket instanceof EE_Ticket) {
214
+			throw new DomainException(
215
+				esc_html__(
216
+					'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.',
217
+					'event_espresso'
218
+				)
219
+			);
220
+		}
221
+		if ($ticket->get_event_ID() !== $this->event->ID()) {
222
+			throw new DomainException(
223
+				sprintf(
224
+					esc_html__(
225
+						'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.',
226
+						'event_espresso'
227
+					),
228
+					$ticket->get_event_ID(),
229
+					$this->event->ID()
230
+				)
231
+			);
232
+		}
233
+	}
234
+
235
+
236
+	/**
237
+	 * @return EE_Datetime[]
238
+	 */
239
+	public function getDatetimes()
240
+	{
241
+		return $this->datetimes;
242
+	}
243
+
244
+
245
+	/**
246
+	 * @param EE_Datetime $datetime
247
+	 * @throws EE_Error
248
+	 * @throws DomainException
249
+	 */
250
+	public function setDatetime(EE_Datetime $datetime)
251
+	{
252
+		if ($datetime->event()->ID() !== $this->event->ID()) {
253
+			throw new DomainException(
254
+				sprintf(
255
+					esc_html__(
256
+						'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.',
257
+						'event_espresso'
258
+					),
259
+					$datetime->event()->ID(),
260
+					$this->event->ID()
261
+				)
262
+			);
263
+		}
264
+		$this->datetimes[ $datetime->ID() ] = $datetime;
265
+	}
266
+
267
+
268
+	/**
269
+	 * calculate spaces remaining based on "saleable" tickets
270
+	 *
271
+	 * @return float|int
272
+	 * @throws EE_Error
273
+	 * @throws DomainException
274
+	 * @throws UnexpectedEntityException
275
+	 * @throws InvalidDataTypeException
276
+	 * @throws InvalidInterfaceException
277
+	 * @throws InvalidArgumentException
278
+	 */
279
+	public function spacesRemaining()
280
+	{
281
+		if ($this->debug) {
282
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
283
+		}
284
+		if ($this->spaces_remaining === null) {
285
+			$this->initialize();
286
+			$this->spaces_remaining = $this->calculate();
287
+		}
288
+		return $this->spaces_remaining;
289
+	}
290
+
291
+
292
+	/**
293
+	 * calculates total available spaces for an event with no regard for sold tickets
294
+	 *
295
+	 * @return int|float
296
+	 * @throws EE_Error
297
+	 * @throws DomainException
298
+	 * @throws UnexpectedEntityException
299
+	 * @throws InvalidDataTypeException
300
+	 * @throws InvalidInterfaceException
301
+	 * @throws InvalidArgumentException
302
+	 */
303
+	public function totalSpacesAvailable()
304
+	{
305
+		if ($this->debug) {
306
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
307
+		}
308
+		if ($this->total_spaces_available === null) {
309
+			$this->initialize();
310
+			$this->total_spaces_available = $this->calculate(false);
311
+		}
312
+		return $this->total_spaces_available;
313
+	}
314
+
315
+
316
+	/**
317
+	 * Loops through the active tickets for the event
318
+	 * and builds a series of data arrays that will be used for calculating
319
+	 * the total maximum available spaces, as well as the spaces remaining.
320
+	 * Because ticket quantities affect datetime spaces and vice versa,
321
+	 * we need to be constantly updating these data arrays as things change,
322
+	 * which is the entire reason for their existence.
323
+	 *
324
+	 * @throws EE_Error
325
+	 * @throws DomainException
326
+	 * @throws UnexpectedEntityException
327
+	 * @throws InvalidDataTypeException
328
+	 * @throws InvalidInterfaceException
329
+	 * @throws InvalidArgumentException
330
+	 */
331
+	private function initialize()
332
+	{
333
+		if ($this->debug) {
334
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
335
+		}
336
+		$this->datetime_tickets = array();
337
+		$this->datetime_spaces = array();
338
+		$this->ticket_datetimes = array();
339
+		$this->ticket_quantities = array();
340
+		$this->tickets_sold = array();
341
+		$this->total_spaces = array();
342
+		$active_tickets = $this->getActiveTickets();
343
+		if (! empty($active_tickets)) {
344
+			foreach ($active_tickets as $ticket) {
345
+				$this->validateTicket($ticket);
346
+				// we need to index our data arrays using strings for the purpose of sorting,
347
+				// but we also need them to be unique, so  we'll just prepend a letter T to the ID
348
+				$ticket_identifier = "T{$ticket->ID()}";
349
+				// to start, we'll just consider the raw qty to be the maximum availability for this ticket,
350
+				// unless the ticket is past its "sell until" date, in which case the qty will be 0
351
+				$max_tickets = $ticket->is_expired() ? 0 : $ticket->qty();
352
+				// but we'll adjust that after looping over each datetime for the ticket and checking reg limits
353
+				$ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
354
+				foreach ($ticket_datetimes as $datetime) {
355
+					// save all datetimes
356
+					$this->setDatetime($datetime);
357
+					$datetime_identifier = "D{$datetime->ID()}";
358
+					$reg_limit = $datetime->reg_limit();
359
+					// ticket quantity can not exceed datetime reg limit
360
+					$max_tickets = min($max_tickets, $reg_limit);
361
+					// as described earlier, because we need to be able to constantly adjust numbers for things,
362
+					// we are going to move all of our data into the following arrays:
363
+					// datetime spaces initially represents the reg limit for each datetime,
364
+					// but this will get adjusted as tickets are accounted for
365
+					$this->datetime_spaces[ $datetime_identifier ] = $reg_limit;
366
+					// just an array of ticket IDs grouped by datetime
367
+					$this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier;
368
+					// and an array of datetime IDs grouped by ticket
369
+					$this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier;
370
+				}
371
+				// total quantity of sold and reserved for each ticket
372
+				$this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved();
373
+				// and the maximum ticket quantities for each ticket (adjusted for reg limit)
374
+				$this->ticket_quantities[ $ticket_identifier ] = $max_tickets;
375
+			}
376
+		}
377
+		// sort datetime spaces by reg limit, but maintain our string indexes
378
+		asort($this->datetime_spaces, SORT_NUMERIC);
379
+		// datetime tickets need to be sorted in the SAME order as the above array...
380
+		// so we'll just use array_merge() to take the structure of datetime_spaces
381
+		// but overwrite all of the data with that from datetime_tickets
382
+		$this->datetime_tickets = array_merge(
383
+			$this->datetime_spaces,
384
+			$this->datetime_tickets
385
+		);
386
+		if ($this->debug) {
387
+			\EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
388
+			\EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__);
389
+			\EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__);
390
+		}
391
+	}
392
+
393
+
394
+	/**
395
+	 * performs calculations on initialized data
396
+	 *
397
+	 * @param bool $consider_sold
398
+	 * @return int|float
399
+	 */
400
+	private function calculate($consider_sold = true)
401
+	{
402
+		if ($this->debug) {
403
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
404
+			\EEH_Debug_Tools::printr($consider_sold, '$consider_sold', __FILE__, __LINE__);
405
+		}
406
+		if ($consider_sold) {
407
+			// subtract amounts sold from all ticket quantities and datetime spaces
408
+			$this->adjustTicketQuantitiesDueToSales();
409
+		}
410
+		foreach ($this->datetime_tickets as $datetime_identifier => $tickets) {
411
+			$this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets);
412
+		}
413
+		// total spaces available is just the sum of the spaces available for each datetime
414
+		$spaces_remaining = array_sum($this->total_spaces);
415
+		if ($this->debug) {
416
+			\EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__);
417
+			\EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__);
418
+			\EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__);
419
+		}
420
+		return $spaces_remaining;
421
+	}
422
+
423
+
424
+	/**
425
+	 * subtracts amount of  tickets sold from ticket quantities and datetime spaces
426
+	 */
427
+	private function adjustTicketQuantitiesDueToSales()
428
+	{
429
+		if ($this->debug) {
430
+			\EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
431
+		}
432
+		foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) {
433
+			if (isset($this->ticket_quantities[ $ticket_identifier ])) {
434
+				$this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold;
435
+				// don't let values go below zero
436
+				$this->ticket_quantities[ $ticket_identifier ] = max(
437
+					$this->ticket_quantities[ $ticket_identifier ],
438
+					0
439
+				);
440
+				if ($this->debug) {
441
+					\EEH_Debug_Tools::printr(
442
+						"{$tickets_sold} sales for ticket {$ticket_identifier} ",
443
+						'subtracting',
444
+						__FILE__,
445
+						__LINE__
446
+					);
447
+				}
448
+			}
449
+			if (isset($this->ticket_datetimes[ $ticket_identifier ])
450
+				&& is_array($this->ticket_datetimes[ $ticket_identifier ])
451
+			) {
452
+				foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) {
453
+					if (isset($this->ticket_quantities[ $ticket_identifier ])) {
454
+						$this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold;
455
+						// don't let values go below zero
456
+						$this->datetime_spaces[ $ticket_datetime ] = max(
457
+							$this->datetime_spaces[ $ticket_datetime ],
458
+							0
459
+						);
460
+						if ($this->debug) {
461
+							\EEH_Debug_Tools::printr(
462
+								"{$tickets_sold} sales for datetime {$ticket_datetime} ",
463
+								'subtracting',
464
+								__FILE__,
465
+								__LINE__
466
+							);
467
+						}
468
+					}
469
+				}
470
+			}
471
+		}
472
+	}
473
+
474
+
475
+	/**
476
+	 * @param string $datetime_identifier
477
+	 * @param array  $tickets
478
+	 */
479
+	private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets)
480
+	{
481
+		// make sure a reg limit is set for the datetime
482
+		$reg_limit = isset($this->datetime_spaces[ $datetime_identifier ])
483
+			? $this->datetime_spaces[ $datetime_identifier ]
484
+			: 0;
485
+		// and bail if it is not
486
+		if (! $reg_limit) {
487
+			if ($this->debug) {
488
+				\EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__);
489
+			}
490
+			return;
491
+		}
492
+		if ($this->debug) {
493
+			\EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1);
494
+			\EEH_Debug_Tools::printr(
495
+				"{$reg_limit}",
496
+				'REG LIMIT',
497
+				__FILE__,
498
+				__LINE__
499
+			);
500
+		}
501
+		// number of allocated spaces always starts at zero
502
+		$spaces_allocated = 0;
503
+		$this->total_spaces[ $datetime_identifier ] = 0;
504
+		foreach ($tickets as $ticket_identifier) {
505
+			$spaces_allocated = $this->calculateAvailableSpacesForTicket(
506
+				$datetime_identifier,
507
+				$reg_limit,
508
+				$ticket_identifier,
509
+				$spaces_allocated
510
+			);
511
+		}
512
+		// spaces can't be negative
513
+		$spaces_allocated = max($spaces_allocated, 0);
514
+		if ($spaces_allocated) {
515
+			// track any non-zero values
516
+			$this->total_spaces[ $datetime_identifier ] += $spaces_allocated;
517
+			if ($this->debug) {
518
+				\EEH_Debug_Tools::printr((string) $spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__);
519
+			}
520
+		} else {
521
+			if ($this->debug) {
522
+				\EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__);
523
+			}
524
+		}
525
+		if ($this->debug) {
526
+			\EEH_Debug_Tools::printr(
527
+				$this->total_spaces[ $datetime_identifier ],
528
+				'$total_spaces',
529
+				__FILE__,
530
+				__LINE__
531
+			);
532
+			\EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__);
533
+			\EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
534
+		}
535
+	}
536
+
537
+
538
+	/**
539
+	 * @param string $datetime_identifier
540
+	 * @param int    $reg_limit
541
+	 * @param string $ticket_identifier
542
+	 * @param int    $spaces_allocated
543
+	 * @return int
544
+	 */
545
+	private function calculateAvailableSpacesForTicket(
546
+		$datetime_identifier,
547
+		$reg_limit,
548
+		$ticket_identifier,
549
+		$spaces_allocated
550
+	) {
551
+		// make sure ticket quantity is set
552
+		$ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ])
553
+			? $this->ticket_quantities[ $ticket_identifier ]
554
+			: 0;
555
+		if ($this->debug) {
556
+			\EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__);
557
+			\EEH_Debug_Tools::printr(
558
+				"{$ticket_quantity}",
559
+				"ticket $ticket_identifier quantity: ",
560
+				__FILE__,
561
+				__LINE__,
562
+				2
563
+			);
564
+		}
565
+		if ($ticket_quantity) {
566
+			if ($this->debug) {
567
+				\EEH_Debug_Tools::printr(
568
+					($spaces_allocated <= $reg_limit)
569
+						? 'true'
570
+						: 'false',
571
+					' . spaces_allocated <= reg_limit = ',
572
+					__FILE__,
573
+					__LINE__
574
+				);
575
+			}
576
+			// if the datetime is NOT at full capacity yet
577
+			if ($spaces_allocated <= $reg_limit) {
578
+				// then the maximum ticket quantity we can allocate is the lowest value of either:
579
+				//  the number of remaining spaces for the datetime, which is the limit - spaces already taken
580
+				//  or the maximum ticket quantity
581
+				$ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity);
582
+				// adjust the available quantity in our tracking array
583
+				$this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity;
584
+				// and increment spaces allocated for this datetime
585
+				$spaces_allocated += $ticket_quantity;
586
+				$at_capacity = $spaces_allocated >= $reg_limit;
587
+				if ($this->debug) {
588
+					\EEH_Debug_Tools::printr(
589
+						"{$ticket_quantity} {$ticket_identifier} tickets",
590
+						' > > allocate ',
591
+						__FILE__,
592
+						__LINE__,
593
+						3
594
+					);
595
+					if ($at_capacity) {
596
+						\EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3);
597
+					}
598
+				}
599
+				// now adjust all other datetimes that allow access to this ticket
600
+				$this->adjustDatetimes(
601
+					$datetime_identifier,
602
+					$ticket_identifier,
603
+					$ticket_quantity,
604
+					$at_capacity
605
+				);
606
+			}
607
+		}
608
+		return $spaces_allocated;
609
+	}
610
+
611
+
612
+	/**
613
+	 * subtracts ticket amounts from all datetime reg limits
614
+	 * that allow access to the ticket specified,
615
+	 * because that ticket could be used
616
+	 * to attend any of the datetimes it has access to
617
+	 *
618
+	 * @param string $datetime_identifier
619
+	 * @param string $ticket_identifier
620
+	 * @param bool   $at_capacity
621
+	 * @param int    $ticket_quantity
622
+	 */
623
+	private function adjustDatetimes(
624
+		$datetime_identifier,
625
+		$ticket_identifier,
626
+		$ticket_quantity,
627
+		$at_capacity
628
+	) {
629
+		/** @var array $datetime_tickets */
630
+		foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) {
631
+			if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) {
632
+				continue;
633
+			}
634
+			$adjusted = $this->adjustDatetimeSpaces(
635
+				$datetime_ID,
636
+				$ticket_identifier,
637
+				$ticket_quantity
638
+			);
639
+			// skip to next ticket if nothing changed
640
+			if (! ($adjusted || $at_capacity)) {
641
+				continue;
642
+			}
643
+			// then all of it's tickets are now unavailable
644
+			foreach ($datetime_tickets as $datetime_ticket) {
645
+				if (($ticket_identifier === $datetime_ticket || $at_capacity)
646
+					&& isset($this->ticket_quantities[ $datetime_ticket ])
647
+					&& $this->ticket_quantities[ $datetime_ticket ] > 0
648
+				) {
649
+					if ($this->debug) {
650
+						\EEH_Debug_Tools::printr(
651
+							$datetime_ticket,
652
+							' . . . adjust ticket quantities for',
653
+							__FILE__,
654
+							__LINE__
655
+						);
656
+					}
657
+					// if this datetime is at full capacity, set any tracked available quantities to zero
658
+					// otherwise just subtract the ticket quantity
659
+					$new_quantity = $at_capacity
660
+						? 0
661
+						: $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity;
662
+					// don't let ticket quantity go below zero
663
+					$this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0);
664
+					if ($this->debug) {
665
+						\EEH_Debug_Tools::printr(
666
+							$at_capacity
667
+								? "0 because Datetime {$datetime_identifier} is at capacity"
668
+								: "{$this->ticket_quantities[ $datetime_ticket ]}",
669
+							" . . . . {$datetime_ticket} quantity set to ",
670
+							__FILE__,
671
+							__LINE__
672
+						);
673
+					}
674
+				}
675
+				// but we also need to adjust spaces for any other datetimes this ticket has access to
676
+				if ($datetime_ticket === $ticket_identifier) {
677
+					if (isset($this->ticket_datetimes[ $datetime_ticket ])
678
+						&& is_array($this->ticket_datetimes[ $datetime_ticket ])
679
+					) {
680
+						if ($this->debug) {
681
+							\EEH_Debug_Tools::printr(
682
+								$datetime_ticket,
683
+								' . . adjust other Datetimes for',
684
+								__FILE__,
685
+								__LINE__
686
+							);
687
+						}
688
+						foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) {
689
+							// don't adjust the current datetime twice
690
+							if ($datetime !== $datetime_identifier) {
691
+								$this->adjustDatetimeSpaces(
692
+									$datetime,
693
+									$datetime_ticket,
694
+									$ticket_quantity
695
+								);
696
+							}
697
+						}
698
+					}
699
+				}
700
+			}
701
+		}
702
+	}
703
+
704
+	private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0)
705
+	{
706
+		// does datetime have spaces available?
707
+		// and does the supplied ticket have access to this datetime ?
708
+		if ($this->datetime_spaces[ $datetime_identifier ] > 0
709
+			&& isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ])
710
+			&& in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true)
711
+		) {
712
+			if ($this->debug) {
713
+				\EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__);
714
+				\EEH_Debug_Tools::printr(
715
+					"{$this->datetime_spaces[ $datetime_identifier ]}",
716
+					" . . current  {$datetime_identifier} spaces available",
717
+					__FILE__,
718
+					__LINE__
719
+				);
720
+			}
721
+			// then decrement the available spaces for the datetime
722
+			$this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity;
723
+			// but don't let quantities go below zero
724
+			$this->datetime_spaces[ $datetime_identifier ] = max(
725
+				$this->datetime_spaces[ $datetime_identifier ],
726
+				0
727
+			);
728
+			if ($this->debug) {
729
+				\EEH_Debug_Tools::printr(
730
+					"{$ticket_quantity}",
731
+					" . . . {$datetime_identifier} capacity reduced by",
732
+					__FILE__,
733
+					__LINE__
734
+				);
735
+			}
736
+			return true;
737
+		}
738
+		return false;
739
+	}
740 740
 }
Please login to merge, or discard this patch.
core/services/database/TableManager.php 1 patch
Indentation   +231 added lines, -231 removed lines patch added patch discarded remove patch
@@ -13,254 +13,254 @@
 block discarded – undo
13 13
 class TableManager extends \EE_Base
14 14
 {
15 15
 
16
-    /**
17
-     * @var TableAnalysis $table_analysis
18
-     */
19
-    private $table_analysis;
16
+	/**
17
+	 * @var TableAnalysis $table_analysis
18
+	 */
19
+	private $table_analysis;
20 20
 
21 21
 
22
-    /**
23
-     * TableManager constructor.
24
-     *
25
-     * @param TableAnalysis $TableAnalysis
26
-     */
27
-    public function __construct(TableAnalysis $TableAnalysis)
28
-    {
29
-        $this->table_analysis = $TableAnalysis;
30
-    }
22
+	/**
23
+	 * TableManager constructor.
24
+	 *
25
+	 * @param TableAnalysis $TableAnalysis
26
+	 */
27
+	public function __construct(TableAnalysis $TableAnalysis)
28
+	{
29
+		$this->table_analysis = $TableAnalysis;
30
+	}
31 31
 
32 32
 
33
-    /**
34
-     * Gets the injected table analyzer, or throws an exception
35
-     *
36
-     * @return TableAnalysis
37
-     * @throws \EE_Error
38
-     */
39
-    protected function getTableAnalysis()
40
-    {
41
-        if ($this->table_analysis instanceof TableAnalysis) {
42
-            return $this->table_analysis;
43
-        } else {
44
-            throw new \EE_Error(
45
-                sprintf(
46
-                    __('Table analysis class on class %1$s is not set properly.', 'event_espresso'),
47
-                    get_class($this)
48
-                )
49
-            );
50
-        }
51
-    }
33
+	/**
34
+	 * Gets the injected table analyzer, or throws an exception
35
+	 *
36
+	 * @return TableAnalysis
37
+	 * @throws \EE_Error
38
+	 */
39
+	protected function getTableAnalysis()
40
+	{
41
+		if ($this->table_analysis instanceof TableAnalysis) {
42
+			return $this->table_analysis;
43
+		} else {
44
+			throw new \EE_Error(
45
+				sprintf(
46
+					__('Table analysis class on class %1$s is not set properly.', 'event_espresso'),
47
+					get_class($this)
48
+				)
49
+			);
50
+		}
51
+	}
52 52
 
53 53
 
54
-    /**
55
-     * @param string $table_name which can optionally start with $wpdb->prefix or not
56
-     * @param string $column_name
57
-     * @param string $column_info
58
-     * @return bool|false|int
59
-     */
60
-    public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL')
61
-    {
62
-        if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) {
63
-            return false;
64
-        }
65
-        global $wpdb;
66
-        $full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
67
-        $columns = $this->getTableColumns($table_name);
68
-        if (! in_array($column_name, $columns)) {
69
-            $alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}";
70
-            return $wpdb->query($alter_query);
71
-        }
72
-        return true;
73
-    }
54
+	/**
55
+	 * @param string $table_name which can optionally start with $wpdb->prefix or not
56
+	 * @param string $column_name
57
+	 * @param string $column_info
58
+	 * @return bool|false|int
59
+	 */
60
+	public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL')
61
+	{
62
+		if (apply_filters('FHEE__EEH_Activation__add_column_if_it_doesnt_exist__short_circuit', false)) {
63
+			return false;
64
+		}
65
+		global $wpdb;
66
+		$full_table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
67
+		$columns = $this->getTableColumns($table_name);
68
+		if (! in_array($column_name, $columns)) {
69
+			$alter_query = "ALTER TABLE {$full_table_name} ADD {$column_name} {$column_info}";
70
+			return $wpdb->query($alter_query);
71
+		}
72
+		return true;
73
+	}
74 74
 
75 75
 
76
-    /**
77
-     * Gets the name of all columns on the  table. $table_name can
78
-     * optionally start with $wpdb->prefix or not
79
-     *
80
-     * @global \wpdb $wpdb
81
-     * @param string $table_name
82
-     * @return array
83
-     */
84
-    public function getTableColumns($table_name)
85
-    {
86
-        global $wpdb;
87
-        $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
88
-        $field_array = array();
89
-        if (! empty($table_name)) {
90
-            $columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} ");
91
-            if ($columns !== false) {
92
-                foreach ($columns as $column) {
93
-                    $field_array[] = $column->Field;
94
-                }
95
-            }
96
-        }
97
-        return $field_array;
98
-    }
76
+	/**
77
+	 * Gets the name of all columns on the  table. $table_name can
78
+	 * optionally start with $wpdb->prefix or not
79
+	 *
80
+	 * @global \wpdb $wpdb
81
+	 * @param string $table_name
82
+	 * @return array
83
+	 */
84
+	public function getTableColumns($table_name)
85
+	{
86
+		global $wpdb;
87
+		$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
88
+		$field_array = array();
89
+		if (! empty($table_name)) {
90
+			$columns = $wpdb->get_results("SHOW COLUMNS FROM {$table_name} ");
91
+			if ($columns !== false) {
92
+				foreach ($columns as $column) {
93
+					$field_array[] = $column->Field;
94
+				}
95
+			}
96
+		}
97
+		return $field_array;
98
+	}
99 99
 
100 100
 
101
-    /**
102
-     * Drops the specified table from the database. $table_name can
103
-     * optionally start with $wpdb->prefix or not
104
-     *
105
-     * @global \wpdb $wpdb
106
-     * @param string $table_name
107
-     * @return int
108
-     */
109
-    public function dropTable($table_name)
110
-    {
111
-        global $wpdb;
112
-        if ($this->getTableAnalysis()->tableExists($table_name)) {
113
-            $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
114
-            return $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
115
-        }
116
-        return 0;
117
-    }
101
+	/**
102
+	 * Drops the specified table from the database. $table_name can
103
+	 * optionally start with $wpdb->prefix or not
104
+	 *
105
+	 * @global \wpdb $wpdb
106
+	 * @param string $table_name
107
+	 * @return int
108
+	 */
109
+	public function dropTable($table_name)
110
+	{
111
+		global $wpdb;
112
+		if ($this->getTableAnalysis()->tableExists($table_name)) {
113
+			$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
114
+			return $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
115
+		}
116
+		return 0;
117
+	}
118 118
 
119 119
 
120
-    /**
121
-     * Drops all the tables mentioned in a single MYSQL query. Double-checks
122
-     * each table name provided has a wpdb prefix attached, and that it exists.
123
-     * Returns the list actually deleted
124
-     *
125
-     * @global WPDB $wpdb
126
-     * @param array $table_names
127
-     * @return array of table names which we deleted
128
-     */
129
-    public function dropTables($table_names)
130
-    {
131
-        $tables_to_delete = array();
132
-        foreach ($table_names as $table_name) {
133
-            $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
134
-            if ($this->getTableAnalysis()->tableExists($table_name)) {
135
-                $tables_to_delete[ $table_name ] = $table_name;
136
-            }
137
-        }
138
-        if (! empty($tables_to_delete)) {
139
-            global $wpdb;
140
-            // make sure we only have a unique strings in the array.
141
-            $tables_to_delete = array_unique($tables_to_delete);
142
-            $wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete));
143
-        }
144
-        return $tables_to_delete;
145
-    }
120
+	/**
121
+	 * Drops all the tables mentioned in a single MYSQL query. Double-checks
122
+	 * each table name provided has a wpdb prefix attached, and that it exists.
123
+	 * Returns the list actually deleted
124
+	 *
125
+	 * @global WPDB $wpdb
126
+	 * @param array $table_names
127
+	 * @return array of table names which we deleted
128
+	 */
129
+	public function dropTables($table_names)
130
+	{
131
+		$tables_to_delete = array();
132
+		foreach ($table_names as $table_name) {
133
+			$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
134
+			if ($this->getTableAnalysis()->tableExists($table_name)) {
135
+				$tables_to_delete[ $table_name ] = $table_name;
136
+			}
137
+		}
138
+		if (! empty($tables_to_delete)) {
139
+			global $wpdb;
140
+			// make sure we only have a unique strings in the array.
141
+			$tables_to_delete = array_unique($tables_to_delete);
142
+			$wpdb->query('DROP TABLE ' . implode(', ', $tables_to_delete));
143
+		}
144
+		return $tables_to_delete;
145
+	}
146 146
 
147 147
 
148
-    /**
149
-     * Drops the specified index from the specified table. $table_name can
150
-     * optionally start with $wpdb->prefix or not
151
-     *
152
-     * @global \wpdb $wpdb
153
-     * @param string $table_name
154
-     * @param string $index_name
155
-     * @return int the number of indexes dropped. False if there was a datbase error
156
-     */
157
-    public function dropIndex($table_name, $index_name)
158
-    {
159
-        if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) {
160
-            return 0;
161
-        }
162
-        global $wpdb;
163
-        $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
164
-        $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE key_name = '{$index_name}'";
165
-        if ($this->getTableAnalysis()->tableExists($table_name)
166
-            && $wpdb->get_var($index_exists_query)
167
-               === $table_name // using get_var with the $index_exists_query returns the table's name
168
-        ) {
169
-            return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}");
170
-        }
171
-        return 0;
172
-    }
148
+	/**
149
+	 * Drops the specified index from the specified table. $table_name can
150
+	 * optionally start with $wpdb->prefix or not
151
+	 *
152
+	 * @global \wpdb $wpdb
153
+	 * @param string $table_name
154
+	 * @param string $index_name
155
+	 * @return int the number of indexes dropped. False if there was a datbase error
156
+	 */
157
+	public function dropIndex($table_name, $index_name)
158
+	{
159
+		if (apply_filters('FHEE__EEH_Activation__drop_index__short_circuit', false)) {
160
+			return 0;
161
+		}
162
+		global $wpdb;
163
+		$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
164
+		$index_exists_query = "SHOW INDEX FROM {$table_name} WHERE key_name = '{$index_name}'";
165
+		if ($this->getTableAnalysis()->tableExists($table_name)
166
+			&& $wpdb->get_var($index_exists_query)
167
+			   === $table_name // using get_var with the $index_exists_query returns the table's name
168
+		) {
169
+			return $wpdb->query("ALTER TABLE {$table_name} DROP INDEX {$index_name}");
170
+		}
171
+		return 0;
172
+	}
173 173
 
174 174
 
175
-    /**
176
-     * Just creates the requested table. $table_name can
177
-     * optionally start with $wpdb->prefix or not
178
-     *
179
-     * @param string $table_name
180
-     * @param string $create_sql defining the table's columns and indexes
181
-     * @param string $engine     (no need to specify "ENGINE=", that's implied)
182
-     * @return void
183
-     * @throws \EE_Error
184
-     */
185
-    public function createTable($table_name, $create_sql, $engine = 'MyISAM')
186
-    {
187
-        $engine = apply_filters(
188
-            'FHEE__EventEspresso_core_services_database_TableManager__createTable__engine',
189
-            $engine,
190
-            $table_name,
191
-            $create_sql
192
-        );
193
-        // does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns )
194
-        if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) {
195
-            $table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
196
-            /** @var \wpdb $wpdb */
197
-            global $wpdb;
198
-            $SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate();
175
+	/**
176
+	 * Just creates the requested table. $table_name can
177
+	 * optionally start with $wpdb->prefix or not
178
+	 *
179
+	 * @param string $table_name
180
+	 * @param string $create_sql defining the table's columns and indexes
181
+	 * @param string $engine     (no need to specify "ENGINE=", that's implied)
182
+	 * @return void
183
+	 * @throws \EE_Error
184
+	 */
185
+	public function createTable($table_name, $create_sql, $engine = 'MyISAM')
186
+	{
187
+		$engine = apply_filters(
188
+			'FHEE__EventEspresso_core_services_database_TableManager__createTable__engine',
189
+			$engine,
190
+			$table_name,
191
+			$create_sql
192
+		);
193
+		// does $sql contain valid column information? ( LPT: https://regex101.com/ is great for working out regex patterns )
194
+		if (preg_match('((((.*?))(,\s))+)', $create_sql, $valid_column_data)) {
195
+			$table_name = $this->getTableAnalysis()->ensureTableNameHasPrefix($table_name);
196
+			/** @var \wpdb $wpdb */
197
+			global $wpdb;
198
+			$SQL = "CREATE TABLE {$table_name} ( {$create_sql} ) ENGINE={$engine} " . $wpdb->get_charset_collate();
199 199
 
200
-            // get $wpdb to echo errors, but buffer them. This way at least WE know an error
201
-            // happened. And then we can choose to tell the end user
202
-            $old_show_errors_policy = $wpdb->show_errors(true);
203
-            $old_error_suppression_policy = $wpdb->suppress_errors(false);
204
-            ob_start();
205
-            dbDelta($SQL);
206
-            $output = ob_get_contents();
207
-            ob_end_clean();
208
-            $wpdb->show_errors($old_show_errors_policy);
209
-            $wpdb->suppress_errors($old_error_suppression_policy);
210
-            if (! empty($output)) {
211
-                throw new \EE_Error($output);
212
-            }
213
-        } else {
214
-            throw new \EE_Error(
215
-                sprintf(
216
-                    __(
217
-                        'The following table creation SQL does not contain valid information about the table columns: %1$s %2$s',
218
-                        'event_espresso'
219
-                    ),
220
-                    '<br />',
221
-                    $create_sql
222
-                )
223
-            );
224
-        }
225
-    }
200
+			// get $wpdb to echo errors, but buffer them. This way at least WE know an error
201
+			// happened. And then we can choose to tell the end user
202
+			$old_show_errors_policy = $wpdb->show_errors(true);
203
+			$old_error_suppression_policy = $wpdb->suppress_errors(false);
204
+			ob_start();
205
+			dbDelta($SQL);
206
+			$output = ob_get_contents();
207
+			ob_end_clean();
208
+			$wpdb->show_errors($old_show_errors_policy);
209
+			$wpdb->suppress_errors($old_error_suppression_policy);
210
+			if (! empty($output)) {
211
+				throw new \EE_Error($output);
212
+			}
213
+		} else {
214
+			throw new \EE_Error(
215
+				sprintf(
216
+					__(
217
+						'The following table creation SQL does not contain valid information about the table columns: %1$s %2$s',
218
+						'event_espresso'
219
+					),
220
+					'<br />',
221
+					$create_sql
222
+				)
223
+			);
224
+		}
225
+	}
226 226
 
227 227
 
228
-    /**
229
-     * Drops the specified index if it's size differs from $desired_index_size.
230
-     * WordPress' dbdelta method doesn't automatically change index sizes, so this
231
-     * method can be used to only drop the index if needed, and afterwards dbdelta can be used as normal.
232
-     * If the table doesn't exist, or it exists but the index does not, or returns false
233
-     *
234
-     * @param string     $table_name
235
-     * @param string     $index_name
236
-     * @param string     $column_name        if none is provided, we assume the column name matches the index (often
237
-     *                                       true in EE)
238
-     * @param string|int $desired_index_size defaults to TableAnalysis::index_col_size, the max for utf8mb4.
239
-     * @return bool whether an index was dropped or not
240
-     * @throws /EE_Error if table analysis object isn't defined
241
-     */
242
-    public function dropIndexIfSizeNot(
243
-        $table_name,
244
-        $index_name,
245
-        $column_name = null,
246
-        $desired_index_size = TableAnalysis::INDEX_COLUMN_SIZE
247
-    ) {
248
-        if ($column_name === null) {
249
-            $column_name = $index_name;
250
-        }
251
-        if (! $this->getTableAnalysis()->tableExists($table_name)) {
252
-            return false;
253
-        }
254
-        $index_entries = $this->getTableAnalysis()->showIndexes($table_name, $index_name);
255
-        if (empty($index_entries)) {
256
-            return false;
257
-        }
258
-        foreach ($index_entries as $index_entry) {
259
-            if ($column_name === $index_entry->Column_name
260
-                && (string) $desired_index_size !== $index_entry->Sub_part) {
261
-                return $this->dropIndex($table_name, $index_name);
262
-            }
263
-        }
264
-        return false;
265
-    }
228
+	/**
229
+	 * Drops the specified index if it's size differs from $desired_index_size.
230
+	 * WordPress' dbdelta method doesn't automatically change index sizes, so this
231
+	 * method can be used to only drop the index if needed, and afterwards dbdelta can be used as normal.
232
+	 * If the table doesn't exist, or it exists but the index does not, or returns false
233
+	 *
234
+	 * @param string     $table_name
235
+	 * @param string     $index_name
236
+	 * @param string     $column_name        if none is provided, we assume the column name matches the index (often
237
+	 *                                       true in EE)
238
+	 * @param string|int $desired_index_size defaults to TableAnalysis::index_col_size, the max for utf8mb4.
239
+	 * @return bool whether an index was dropped or not
240
+	 * @throws /EE_Error if table analysis object isn't defined
241
+	 */
242
+	public function dropIndexIfSizeNot(
243
+		$table_name,
244
+		$index_name,
245
+		$column_name = null,
246
+		$desired_index_size = TableAnalysis::INDEX_COLUMN_SIZE
247
+	) {
248
+		if ($column_name === null) {
249
+			$column_name = $index_name;
250
+		}
251
+		if (! $this->getTableAnalysis()->tableExists($table_name)) {
252
+			return false;
253
+		}
254
+		$index_entries = $this->getTableAnalysis()->showIndexes($table_name, $index_name);
255
+		if (empty($index_entries)) {
256
+			return false;
257
+		}
258
+		foreach ($index_entries as $index_entry) {
259
+			if ($column_name === $index_entry->Column_name
260
+				&& (string) $desired_index_size !== $index_entry->Sub_part) {
261
+				return $this->dropIndex($table_name, $index_name);
262
+			}
263
+		}
264
+		return false;
265
+	}
266 266
 }
Please login to merge, or discard this patch.
modules/ticket_sales_monitor/EED_Ticket_Sales_Monitor.module.php 3 patches
Doc Comments   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -333,7 +333,7 @@  discard block
 block discarded – undo
333 333
     /**
334 334
      * @param  EE_Ticket $ticket
335 335
      * @param  int       $quantity
336
-     * @return bool
336
+     * @return integer
337 337
      * @throws EE_Error
338 338
      */
339 339
     protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
@@ -970,6 +970,7 @@  discard block
 block discarded – undo
970 970
     /**
971 971
      * @param EE_Ticket[]    $tickets_with_reservations
972 972
      * @param EE_Line_Item[] $valid_reserved_ticket_line_items
973
+     * @param string $source
973 974
      * @return int
974 975
      * @throws UnexpectedEntityException
975 976
      * @throws DomainException
Please login to merge, or discard this patch.
Indentation   +1045 added lines, -1045 removed lines patch added patch discarded remove patch
@@ -20,1050 +20,1050 @@
 block discarded – undo
20 20
 class EED_Ticket_Sales_Monitor extends EED_Module
21 21
 {
22 22
 
23
-    const debug = false;
24
-
25
-    private static $nl = '';
26
-
27
-    /**
28
-     * an array of raw ticket data from EED_Ticket_Selector
29
-     *
30
-     * @var array $ticket_selections
31
-     */
32
-    protected $ticket_selections = array();
33
-
34
-    /**
35
-     * the raw ticket data from EED_Ticket_Selector is organized in rows
36
-     * according to how they are displayed in the actual Ticket_Selector
37
-     * this tracks the current row being processed
38
-     *
39
-     * @var int $current_row
40
-     */
41
-    protected $current_row = 0;
42
-
43
-    /**
44
-     * an array for tracking names of tickets that have sold out
45
-     *
46
-     * @var array $sold_out_tickets
47
-     */
48
-    protected $sold_out_tickets = array();
49
-
50
-    /**
51
-     * an array for tracking names of tickets that have had their quantities reduced
52
-     *
53
-     * @var array $decremented_tickets
54
-     */
55
-    protected $decremented_tickets = array();
56
-
57
-
58
-    /**
59
-     * set_hooks - for hooking into EE Core, other modules, etc
60
-     *
61
-     * @return    void
62
-     */
63
-    public static function set_hooks()
64
-    {
65
-        self::$nl = defined('EE_TESTS_DIR') ? "\n" : '<br />';
66
-        // release tickets for expired carts
67
-        add_action(
68
-            'EED_Ticket_Selector__process_ticket_selections__before',
69
-            array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'),
70
-            1
71
-        );
72
-        // check ticket reserves AFTER MER does it's check (hence priority 20)
73
-        add_filter(
74
-            'FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty',
75
-            array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'),
76
-            20,
77
-            3
78
-        );
79
-        // add notices for sold out tickets
80
-        add_action(
81
-            'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart',
82
-            array('EED_Ticket_Sales_Monitor', 'post_notices'),
83
-            10
84
-        );
85
-
86
-        // handle tickets deleted from cart
87
-        add_action(
88
-            'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart',
89
-            array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'),
90
-            10,
91
-            2
92
-        );
93
-        // handle emptied carts
94
-        add_action(
95
-            'AHEE__EE_Session__reset_cart__before_reset',
96
-            array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
97
-            10,
98
-            1
99
-        );
100
-        add_action(
101
-            'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart',
102
-            array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
103
-            10,
104
-            1
105
-        );
106
-        // handle cancelled registrations
107
-        add_action(
108
-            'AHEE__EE_Session__reset_checkout__before_reset',
109
-            array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'),
110
-            10,
111
-            1
112
-        );
113
-        // cron tasks
114
-        add_action(
115
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__abandoned_transaction',
116
-            array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
117
-            10,
118
-            1
119
-        );
120
-        add_action(
121
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction',
122
-            array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
123
-            10,
124
-            1
125
-        );
126
-        add_action(
127
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction',
128
-            array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'),
129
-            10,
130
-            1
131
-        );
132
-    }
133
-
134
-
135
-    /**
136
-     * set_hooks_admin - for hooking into EE Admin Core, other modules, etc
137
-     *
138
-     * @return void
139
-     */
140
-    public static function set_hooks_admin()
141
-    {
142
-        EED_Ticket_Sales_Monitor::set_hooks();
143
-    }
144
-
145
-
146
-    /**
147
-     * @return EED_Ticket_Sales_Monitor|EED_Module
148
-     */
149
-    public static function instance()
150
-    {
151
-        return parent::get_instance(__CLASS__);
152
-    }
153
-
154
-
155
-    /**
156
-     * @param WP_Query $WP_Query
157
-     * @return    void
158
-     */
159
-    public function run($WP_Query)
160
-    {
161
-    }
162
-
163
-
164
-
165
-    /********************************** PRE_TICKET_SALES  **********************************/
166
-
167
-
168
-    /**
169
-     * Retrieves grand totals from the line items that have no TXN ID
170
-     * and timestamps less than the current time minus the session lifespan.
171
-     * These are carts that have been abandoned before the "registrant" even attempted to checkout.
172
-     * We're going to release the tickets for these line items before attempting to add more to the cart.
173
-     *
174
-     * @return void
175
-     * @throws DomainException
176
-     * @throws EE_Error
177
-     * @throws InvalidArgumentException
178
-     * @throws InvalidDataTypeException
179
-     * @throws InvalidInterfaceException
180
-     * @throws UnexpectedEntityException
181
-     */
182
-    public static function release_tickets_for_expired_carts()
183
-    {
184
-        if (self::debug) {
185
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
186
-        }
187
-        do_action('AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__begin');
188
-        $expired_ticket_IDs = array();
189
-        /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
190
-        $session_lifespan = LoaderFactory::getLoader()->getShared(
191
-            'EventEspresso\core\domain\values\session\SessionLifespan'
192
-        );
193
-        $timestamp = $session_lifespan->expiration();
194
-        $expired_ticket_line_items = EEM_Line_Item::instance()->getTicketLineItemsForExpiredCarts($timestamp);
195
-        if (self::debug) {
196
-            echo self::$nl . ' . time(): ' . time();
197
-            echo self::$nl . ' . time() as date: ' . date('Y-m-d H:i a');
198
-            echo self::$nl . ' . session expiration: ' . $session_lifespan->expiration();
199
-            echo self::$nl . ' . session expiration as date: ' . date('Y-m-d H:i a', $session_lifespan->expiration());
200
-            echo self::$nl . ' . timestamp: ' . $timestamp;
201
-            echo self::$nl . ' . $expired_ticket_line_items: ' . count($expired_ticket_line_items);
202
-        }
203
-        if (! empty($expired_ticket_line_items)) {
204
-            foreach ($expired_ticket_line_items as $expired_ticket_line_item) {
205
-                if (! $expired_ticket_line_item instanceof EE_Line_Item) {
206
-                    continue;
207
-                }
208
-                $expired_ticket_IDs[ $expired_ticket_line_item->OBJ_ID() ] = $expired_ticket_line_item->OBJ_ID();
209
-                if (self::debug) {
210
-                    echo self::$nl . ' . $expired_ticket_line_item->OBJ_ID(): ' . $expired_ticket_line_item->OBJ_ID();
211
-                    echo self::$nl . ' . $expired_ticket_line_item->timestamp(): '
212
-                         . date(
213
-                             'Y-m-d h:i a',
214
-                             $expired_ticket_line_item->timestamp(true)
215
-                         );
216
-                }
217
-            }
218
-            if (! empty($expired_ticket_IDs)) {
219
-                EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
220
-                    \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
221
-                    array(),
222
-                    __FUNCTION__
223
-                );
224
-                // now  let's get rid of expired line items so that they can't interfere with tracking
225
-                EED_Ticket_Sales_Monitor::clear_expired_line_items_with_no_transaction($timestamp);
226
-            }
227
-        }
228
-        do_action(
229
-            'AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__end',
230
-            $expired_ticket_IDs,
231
-            $expired_ticket_line_items
232
-        );
233
-    }
234
-
235
-
236
-
237
-    /********************************** VALIDATE_TICKET_SALE  **********************************/
238
-
239
-
240
-    /**
241
-     * callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data'
242
-     *
243
-     * @param int       $qty
244
-     * @param EE_Ticket $ticket
245
-     * @return bool
246
-     * @throws UnexpectedEntityException
247
-     * @throws EE_Error
248
-     */
249
-    public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket)
250
-    {
251
-        $qty = absint($qty);
252
-        if ($qty > 0) {
253
-            $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
254
-        }
255
-        if (self::debug) {
256
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
257
-            echo self::$nl . self::$nl . '<b> RETURNED QTY: ' . $qty . '</b>';
258
-        }
259
-        return $qty;
260
-    }
261
-
262
-
263
-    /**
264
-     * checks whether an individual ticket is available for purchase based on datetime, and ticket details
265
-     *
266
-     * @param   EE_Ticket $ticket
267
-     * @param int         $qty
268
-     * @return int
269
-     * @throws UnexpectedEntityException
270
-     * @throws EE_Error
271
-     */
272
-    protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
273
-    {
274
-        if (self::debug) {
275
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
276
-        }
277
-        if (! $ticket instanceof EE_Ticket) {
278
-            return 0;
279
-        }
280
-        if (self::debug) {
281
-            echo self::$nl . '<b> . ticket->ID: ' . $ticket->ID() . '</b>';
282
-            echo self::$nl . ' . original ticket->reserved: ' . $ticket->reserved();
283
-        }
284
-        $ticket->refresh_from_db();
285
-        // first let's determine the ticket availability based on sales
286
-        $available = $ticket->qty('saleable');
287
-        if (self::debug) {
288
-            echo self::$nl . ' . . . ticket->qty: ' . $ticket->qty();
289
-            echo self::$nl . ' . . . ticket->sold: ' . $ticket->sold();
290
-            echo self::$nl . ' . . . ticket->reserved: ' . $ticket->reserved();
291
-            echo self::$nl . ' . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
292
-            echo self::$nl . ' . . . available: ' . $available;
293
-        }
294
-        if ($available < 1) {
295
-            $this->_ticket_sold_out($ticket);
296
-            return 0;
297
-        }
298
-        if (self::debug) {
299
-            echo self::$nl . ' . . . qty: ' . $qty;
300
-        }
301
-        if ($available < $qty) {
302
-            $qty = $available;
303
-            if (self::debug) {
304
-                echo self::$nl . ' . . . QTY ADJUSTED: ' . $qty;
305
-            }
306
-            $this->_ticket_quantity_decremented($ticket);
307
-        }
308
-        if ($this->_reserve_ticket($ticket, $qty)) {
309
-            return $qty;
310
-        } else {
311
-            return 0;
312
-        }
313
-    }
314
-
315
-
316
-    /**
317
-     * increments ticket reserved based on quantity passed
318
-     *
319
-     * @param    EE_Ticket $ticket
320
-     * @param int          $quantity
321
-     * @return bool indicating success or failure
322
-     * @throws EE_Error
323
-     */
324
-    protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
325
-    {
326
-        if (self::debug) {
327
-            echo self::$nl . self::$nl . ' . . . INCREASE RESERVED: ' . $quantity;
328
-        }
329
-        return $ticket->increaseReserved($quantity, 'TicketSalesMonitor:' . __LINE__);
330
-    }
331
-
332
-
333
-    /**
334
-     * @param  EE_Ticket $ticket
335
-     * @param  int       $quantity
336
-     * @return bool
337
-     * @throws EE_Error
338
-     */
339
-    protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
340
-    {
341
-        if (self::debug) {
342
-            echo self::$nl . ' . . . ticket->ID: ' . $ticket->ID();
343
-            echo self::$nl . ' . . . ticket->reserved before: ' . $ticket->reserved();
344
-        }
345
-        $ticket->decreaseReserved($quantity, true, 'TicketSalesMonitor:' . __LINE__);
346
-        if (self::debug) {
347
-            echo self::$nl . ' . . . ticket->reserved after: ' . $ticket->reserved();
348
-        }
349
-        return $ticket->save() ? 1 : 0;
350
-    }
351
-
352
-
353
-    /**
354
-     * removes quantities within the ticket selector based on zero ticket availability
355
-     *
356
-     * @param    EE_Ticket $ticket
357
-     * @return    void
358
-     * @throws UnexpectedEntityException
359
-     * @throws EE_Error
360
-     */
361
-    protected function _ticket_sold_out(EE_Ticket $ticket)
362
-    {
363
-        if (self::debug) {
364
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
365
-            echo self::$nl . ' . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
366
-        }
367
-        $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
368
-    }
369
-
370
-
371
-    /**
372
-     * adjusts quantities within the ticket selector based on decreased ticket availability
373
-     *
374
-     * @param    EE_Ticket $ticket
375
-     * @return void
376
-     * @throws UnexpectedEntityException
377
-     * @throws EE_Error
378
-     */
379
-    protected function _ticket_quantity_decremented(EE_Ticket $ticket)
380
-    {
381
-        if (self::debug) {
382
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
383
-            echo self::$nl . ' . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
384
-        }
385
-        $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
386
-    }
387
-
388
-
389
-    /**
390
-     * builds string out of ticket and event name
391
-     *
392
-     * @param    EE_Ticket $ticket
393
-     * @return string
394
-     * @throws UnexpectedEntityException
395
-     * @throws EE_Error
396
-     */
397
-    protected function _get_ticket_and_event_name(EE_Ticket $ticket)
398
-    {
399
-        $event = $ticket->get_related_event();
400
-        if ($event instanceof EE_Event) {
401
-            $ticket_name = sprintf(
402
-                _x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'),
403
-                $ticket->name(),
404
-                $event->name()
405
-            );
406
-        } else {
407
-            $ticket_name = $ticket->name();
408
-        }
409
-        return $ticket_name;
410
-    }
411
-
412
-
413
-
414
-    /********************************** EVENT CART  **********************************/
415
-
416
-
417
-    /**
418
-     * releases or reserves ticket(s) based on quantity passed
419
-     *
420
-     * @param  EE_Line_Item $line_item
421
-     * @param  int          $quantity
422
-     * @return void
423
-     * @throws EE_Error
424
-     * @throws InvalidArgumentException
425
-     * @throws InvalidDataTypeException
426
-     * @throws InvalidInterfaceException
427
-     */
428
-    public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1)
429
-    {
430
-        $ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID()));
431
-        if ($ticket instanceof EE_Ticket) {
432
-            $ticket->add_extra_meta(
433
-                EE_Ticket::META_KEY_TICKET_RESERVATIONS,
434
-                __LINE__ . ') ' . __METHOD__ . '()'
435
-            );
436
-            if ($quantity > 0) {
437
-                EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
438
-            } else {
439
-                EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
440
-            }
441
-        }
442
-    }
443
-
444
-
445
-    /**
446
-     * releases reserved ticket(s) based on quantity passed
447
-     *
448
-     * @param  EE_Ticket $ticket
449
-     * @param  int       $quantity
450
-     * @return void
451
-     * @throws EE_Error
452
-     */
453
-    public static function ticket_removed_from_cart(EE_Ticket $ticket, $quantity = 1)
454
-    {
455
-        $ticket->add_extra_meta(
456
-            EE_Ticket::META_KEY_TICKET_RESERVATIONS,
457
-            __LINE__ . ') ' . __METHOD__ . '()'
458
-        );
459
-        EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
460
-    }
461
-
462
-
463
-
464
-    /********************************** POST_NOTICES  **********************************/
465
-
466
-
467
-    /**
468
-     * @return void
469
-     * @throws EE_Error
470
-     * @throws InvalidArgumentException
471
-     * @throws ReflectionException
472
-     * @throws InvalidDataTypeException
473
-     * @throws InvalidInterfaceException
474
-     */
475
-    public static function post_notices()
476
-    {
477
-        EED_Ticket_Sales_Monitor::instance()->_post_notices();
478
-    }
479
-
480
-
481
-    /**
482
-     * @return void
483
-     * @throws EE_Error
484
-     * @throws InvalidArgumentException
485
-     * @throws ReflectionException
486
-     * @throws InvalidDataTypeException
487
-     * @throws InvalidInterfaceException
488
-     */
489
-    protected function _post_notices()
490
-    {
491
-        if (self::debug) {
492
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
493
-        }
494
-        $refresh_msg = '';
495
-        $none_added_msg = '';
496
-        if (defined('DOING_AJAX') && DOING_AJAX) {
497
-            $refresh_msg = __(
498
-                'Please refresh the page to view updated ticket quantities.',
499
-                'event_espresso'
500
-            );
501
-            $none_added_msg = __('No tickets were added for the event.', 'event_espresso');
502
-        }
503
-        if (! empty($this->sold_out_tickets)) {
504
-            EE_Error::add_attention(
505
-                sprintf(
506
-                    apply_filters(
507
-                        'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice',
508
-                        __(
509
-                            'We\'re sorry...%1$sThe following items have sold out since you first viewed this page, and can no longer be registered for:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
510
-                            'event_espresso'
511
-                        )
512
-                    ),
513
-                    '<br />',
514
-                    implode('<br />', $this->sold_out_tickets),
515
-                    $none_added_msg,
516
-                    $refresh_msg
517
-                )
518
-            );
519
-            // alter code flow in the Ticket Selector for better UX
520
-            add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true');
521
-            add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false');
522
-            $this->sold_out_tickets = array();
523
-            // and reset the cart
524
-            EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
525
-        }
526
-        if (! empty($this->decremented_tickets)) {
527
-            EE_Error::add_attention(
528
-                sprintf(
529
-                    apply_filters(
530
-                        'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice',
531
-                        __(
532
-                            'We\'re sorry...%1$sDue to sales that have occurred since you first viewed the last page, the following items have had their quantities adjusted to match the current available amount:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
533
-                            'event_espresso'
534
-                        )
535
-                    ),
536
-                    '<br />',
537
-                    implode('<br />', $this->decremented_tickets),
538
-                    $none_added_msg,
539
-                    $refresh_msg
540
-                )
541
-            );
542
-            $this->decremented_tickets = array();
543
-        }
544
-    }
545
-
546
-
547
-
548
-    /********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION  **********************************/
549
-
550
-
551
-    /**
552
-     * releases reserved tickets for all registrations of an EE_Transaction
553
-     * by default, will NOT release tickets for finalized transactions
554
-     *
555
-     * @param    EE_Transaction $transaction
556
-     * @return int
557
-     * @throws EE_Error
558
-     * @throws InvalidSessionDataException
559
-     */
560
-    protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
561
-    {
562
-        if (self::debug) {
563
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
564
-            echo self::$nl . ' . transaction->ID: ' . $transaction->ID();
565
-            echo self::$nl . ' . TXN status_ID: ' . $transaction->status_ID();
566
-        }
567
-        // check if 'finalize_registration' step has been completed...
568
-        $finalized = $transaction->reg_step_completed('finalize_registration');
569
-        if (self::debug) {
570
-            // DEBUG LOG
571
-            EEH_Debug_Tools::log(
572
-                __CLASS__,
573
-                __FUNCTION__,
574
-                __LINE__,
575
-                array('finalized' => $finalized),
576
-                false,
577
-                'EE_Transaction: ' . $transaction->ID()
578
-            );
579
-        }
580
-        // how many tickets were released
581
-        $count = 0;
582
-        if (self::debug) {
583
-            echo self::$nl . ' . . . TXN finalized: ' . $finalized;
584
-        }
585
-        $release_tickets_with_TXN_status = array(
586
-            EEM_Transaction::failed_status_code,
587
-            EEM_Transaction::abandoned_status_code,
588
-            EEM_Transaction::incomplete_status_code,
589
-        );
590
-        $events = array();
591
-        // if the session is getting cleared BEFORE the TXN has been finalized or the transaction is not completed
592
-        if (! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
593
-            // cancel any reserved tickets for registrations that were not approved
594
-            $registrations = $transaction->registrations();
595
-            if (self::debug) {
596
-                echo self::$nl . ' . . . # registrations: ' . count($registrations);
597
-                $reg = reset($registrations);
598
-                $ticket = $reg->ticket();
599
-                if ($ticket instanceof EE_Ticket) {
600
-                    $ticket->add_extra_meta(
601
-                        EE_Ticket::META_KEY_TICKET_RESERVATIONS,
602
-                        __LINE__ . ') Release All Tickets TXN:' . $transaction->ID()
603
-                    );
604
-                }
605
-            }
606
-            if (! empty($registrations)) {
607
-                foreach ($registrations as $registration) {
608
-                    if ($registration instanceof EE_Registration
609
-                        && $this->_release_reserved_ticket_for_registration($registration, $transaction)
610
-                    ) {
611
-                        $count++;
612
-                        $events[ $registration->event_ID() ] = $registration->event();
613
-                    }
614
-                }
615
-            }
616
-        }
617
-        if ($events !== array()) {
618
-            foreach ($events as $event) {
619
-                /** @var EE_Event $event */
620
-                $event->perform_sold_out_status_check();
621
-            }
622
-        }
623
-        return $count;
624
-    }
625
-
626
-
627
-    /**
628
-     * releases reserved tickets for an EE_Registration
629
-     * by default, will NOT release tickets for APPROVED registrations
630
-     *
631
-     * @param EE_Registration $registration
632
-     * @param EE_Transaction  $transaction
633
-     * @return int
634
-     * @throws EE_Error
635
-     */
636
-    protected function _release_reserved_ticket_for_registration(
637
-        EE_Registration $registration,
638
-        EE_Transaction $transaction
639
-    ) {
640
-        $STS_ID = $transaction->status_ID();
641
-        if (self::debug) {
642
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
643
-            echo self::$nl . ' . . registration->ID: ' . $registration->ID();
644
-            echo self::$nl . ' . . registration->status_ID: ' . $registration->status_ID();
645
-            echo self::$nl . ' . . transaction->status_ID(): ' . $STS_ID;
646
-        }
647
-        if (// release Tickets for Failed Transactions and Abandoned Transactions
648
-            $STS_ID === EEM_Transaction::failed_status_code
649
-            || $STS_ID === EEM_Transaction::abandoned_status_code
650
-            || (
651
-                // also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved
652
-                $STS_ID === EEM_Transaction::incomplete_status_code
653
-                && $registration->status_ID() !== EEM_Registration::status_id_approved
654
-            )
655
-        ) {
656
-            if (self::debug) {
657
-                echo self::$nl . self::$nl . ' . . RELEASE RESERVED TICKET';
658
-                $rsrvd = $registration->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true);
659
-                echo self::$nl . ' . . . registration HAS_RESERVED_TICKET_KEY: ';
660
-                var_dump($rsrvd);
661
-            }
662
-            $registration->release_reserved_ticket(true, 'TicketSalesMonitor:' . __LINE__);
663
-            return 1;
664
-        }
665
-        return 0;
666
-    }
667
-
668
-
669
-
670
-    /********************************** SESSION_CART_RESET  **********************************/
671
-
672
-
673
-    /**
674
-     * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset'
675
-     *
676
-     * @param EE_Session $session
677
-     * @return void
678
-     * @throws EE_Error
679
-     * @throws InvalidArgumentException
680
-     * @throws ReflectionException
681
-     * @throws InvalidDataTypeException
682
-     * @throws InvalidInterfaceException
683
-     */
684
-    public static function session_cart_reset(EE_Session $session)
685
-    {
686
-        // don't release tickets if checkout was already reset
687
-        if (did_action('AHEE__EE_Session__reset_checkout__before_reset')) {
688
-            return;
689
-        }
690
-        if (self::debug) {
691
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
692
-        }
693
-        // first check of the session has a valid Checkout object
694
-        $checkout = $session->checkout();
695
-        if ($checkout instanceof EE_Checkout) {
696
-            // and use that to clear ticket reservations because it will update the associated registration meta data
697
-            EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
698
-            return;
699
-        }
700
-        $cart = $session->cart();
701
-        if ($cart instanceof EE_Cart) {
702
-            if (self::debug) {
703
-                echo self::$nl . self::$nl . ' cart instance of EE_Cart: ';
704
-            }
705
-            EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart, $session);
706
-        } else {
707
-            if (self::debug) {
708
-                echo self::$nl . self::$nl . ' invalid EE_Cart: ';
709
-                var_export($cart, true);
710
-            }
711
-        }
712
-    }
713
-
714
-
715
-    /**
716
-     * releases reserved tickets in the EE_Cart
717
-     *
718
-     * @param EE_Cart $cart
719
-     * @return void
720
-     * @throws EE_Error
721
-     * @throws InvalidArgumentException
722
-     * @throws ReflectionException
723
-     * @throws InvalidDataTypeException
724
-     * @throws InvalidInterfaceException
725
-     */
726
-    protected function _session_cart_reset(EE_Cart $cart, EE_Session $session)
727
-    {
728
-        if (self::debug) {
729
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
730
-        }
731
-        $ticket_line_items = $cart->get_tickets();
732
-        if (empty($ticket_line_items)) {
733
-            return;
734
-        }
735
-        if (self::debug) {
736
-            echo '<br /> . ticket_line_item count: ' . count($ticket_line_items);
737
-        }
738
-        foreach ($ticket_line_items as $ticket_line_item) {
739
-            if (self::debug) {
740
-                echo self::$nl . ' . ticket_line_item->ID(): ' . $ticket_line_item->ID();
741
-            }
742
-            if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
743
-                if (self::debug) {
744
-                    echo self::$nl . ' . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
745
-                }
746
-                $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
747
-                if ($ticket instanceof EE_Ticket) {
748
-                    if (self::debug) {
749
-                        echo self::$nl . ' . . ticket->ID(): ' . $ticket->ID();
750
-                        echo self::$nl . ' . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
751
-                    }
752
-                    $ticket->add_extra_meta(
753
-                        EE_Ticket::META_KEY_TICKET_RESERVATIONS,
754
-                        __LINE__ . ') ' . __METHOD__ . '() SID = ' . $session->id()
755
-                    );
756
-                    $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
757
-                }
758
-            }
759
-        }
760
-        if (self::debug) {
761
-            echo self::$nl . self::$nl . ' RESET COMPLETED ';
762
-        }
763
-    }
764
-
765
-
766
-
767
-    /********************************** SESSION_CHECKOUT_RESET  **********************************/
768
-
769
-
770
-    /**
771
-     * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset'
772
-     *
773
-     * @param EE_Session $session
774
-     * @return void
775
-     * @throws EE_Error
776
-     * @throws InvalidSessionDataException
777
-     */
778
-    public static function session_checkout_reset(EE_Session $session)
779
-    {
780
-        // don't release tickets if cart was already reset
781
-        if (did_action('AHEE__EE_Session__reset_cart__before_reset')) {
782
-            return;
783
-        }
784
-        $checkout = $session->checkout();
785
-        if ($checkout instanceof EE_Checkout) {
786
-            EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
787
-        }
788
-    }
789
-
790
-
791
-    /**
792
-     * releases reserved tickets for the EE_Checkout->transaction
793
-     *
794
-     * @param EE_Checkout $checkout
795
-     * @return void
796
-     * @throws EE_Error
797
-     * @throws InvalidSessionDataException
798
-     */
799
-    protected function _session_checkout_reset(EE_Checkout $checkout)
800
-    {
801
-        if (self::debug) {
802
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
803
-        }
804
-        // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
805
-        if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
806
-            return;
807
-        }
808
-        $this->_release_all_reserved_tickets_for_transaction($checkout->transaction);
809
-    }
810
-
811
-
812
-
813
-    /********************************** SESSION_EXPIRED_RESET  **********************************/
814
-
815
-
816
-    /**
817
-     * @param    EE_Session $session
818
-     * @return    void
819
-     */
820
-    public static function session_expired_reset(EE_Session $session)
821
-    {
822
-    }
823
-
824
-
825
-
826
-    /********************************** PROCESS_ABANDONED_TRANSACTIONS  **********************************/
827
-
828
-
829
-    /**
830
-     * releases reserved tickets for all registrations of an ABANDONED EE_Transaction
831
-     * by default, will NOT release tickets for free transactions, or any that have received a payment
832
-     *
833
-     * @param EE_Transaction $transaction
834
-     * @return void
835
-     * @throws EE_Error
836
-     * @throws InvalidSessionDataException
837
-     */
838
-    public static function process_abandoned_transactions(EE_Transaction $transaction)
839
-    {
840
-        // is this TXN free or has any money been paid towards this TXN? If so, then leave it alone
841
-        if ($transaction->is_free() || $transaction->paid() > 0) {
842
-            if (self::debug) {
843
-                // DEBUG LOG
844
-                EEH_Debug_Tools::log(
845
-                    __CLASS__,
846
-                    __FUNCTION__,
847
-                    __LINE__,
848
-                    array($transaction),
849
-                    false,
850
-                    'EE_Transaction: ' . $transaction->ID()
851
-                );
852
-            }
853
-            return;
854
-        }
855
-        // have their been any successful payments made ?
856
-        $payments = $transaction->payments();
857
-        foreach ($payments as $payment) {
858
-            if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) {
859
-                if (self::debug) {
860
-                    // DEBUG LOG
861
-                    EEH_Debug_Tools::log(
862
-                        __CLASS__,
863
-                        __FUNCTION__,
864
-                        __LINE__,
865
-                        array($payment),
866
-                        false,
867
-                        'EE_Transaction: ' . $transaction->ID()
868
-                    );
869
-                }
870
-                return;
871
-            }
872
-        }
873
-        // since you haven't even attempted to pay for your ticket...
874
-        EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
875
-    }
876
-
877
-
878
-
879
-    /********************************** PROCESS_FAILED_TRANSACTIONS  **********************************/
880
-
881
-
882
-    /**
883
-     * releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction
884
-     *
885
-     * @param EE_Transaction $transaction
886
-     * @return void
887
-     * @throws EE_Error
888
-     * @throws InvalidSessionDataException
889
-     */
890
-    public static function process_failed_transactions(EE_Transaction $transaction)
891
-    {
892
-        // since you haven't even attempted to pay for your ticket...
893
-        EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
894
-    }
895
-
896
-
897
-
898
-    /********************************** RESET RESERVATION COUNTS  *********************************/
899
-
900
-
901
-    /**
902
-     * Resets all ticket and datetime reserved counts to zero
903
-     * Tickets that are currently associated with a Transaction that is in progress
904
-     *
905
-     * @throws EE_Error
906
-     * @throws DomainException
907
-     * @throws InvalidDataTypeException
908
-     * @throws InvalidInterfaceException
909
-     * @throws InvalidArgumentException
910
-     * @throws UnexpectedEntityException
911
-     */
912
-    public static function reset_reservation_counts()
913
-    {
914
-        /** @var EE_Line_Item[] $valid_reserved_tickets */
915
-        $valid_reserved_tickets = array();
916
-        /** @var EE_Transaction[] $transactions_not_in_progress */
917
-        $transactions_not_in_progress = EEM_Transaction::instance()->get_transactions_not_in_progress();
918
-        foreach ($transactions_not_in_progress as $transaction) {
919
-            // if this TXN has been fully completed, then skip it
920
-            if ($transaction->reg_step_completed('finalize_registration')) {
921
-                continue;
922
-            }
923
-            $total_line_item = $transaction->total_line_item();
924
-            // $transaction_in_progress->line
925
-            if (! $total_line_item instanceof EE_Line_Item) {
926
-                throw new DomainException(
927
-                    esc_html__(
928
-                        'Transaction does not have a valid Total Line Item associated with it.',
929
-                        'event_espresso'
930
-                    )
931
-                );
932
-            }
933
-            $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
934
-                $total_line_item
935
-            );
936
-        }
937
-        $total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts();
938
-        foreach ($total_line_items as $total_line_item) {
939
-            $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
940
-                $total_line_item
941
-            );
942
-        }
943
-        $tickets_with_reservations = EEM_Ticket::instance()->get_tickets_with_reservations();
944
-        return EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
945
-            $tickets_with_reservations,
946
-            $valid_reserved_tickets,
947
-            __FUNCTION__
948
-        );
949
-    }
950
-
951
-
952
-    /**
953
-     * @param EE_Line_Item $total_line_item
954
-     * @return EE_Line_Item[]
955
-     */
956
-    private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item)
957
-    {
958
-        /** @var EE_Line_Item[] $valid_reserved_tickets */
959
-        $valid_reserved_tickets = array();
960
-        $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
961
-        foreach ($ticket_line_items as $ticket_line_item) {
962
-            if ($ticket_line_item instanceof EE_Line_Item) {
963
-                $valid_reserved_tickets[] = $ticket_line_item;
964
-            }
965
-        }
966
-        return $valid_reserved_tickets;
967
-    }
968
-
969
-
970
-    /**
971
-     * @param EE_Ticket[]    $tickets_with_reservations
972
-     * @param EE_Line_Item[] $valid_reserved_ticket_line_items
973
-     * @return int
974
-     * @throws UnexpectedEntityException
975
-     * @throws DomainException
976
-     * @throws EE_Error
977
-     */
978
-    private static function release_reservations_for_tickets(
979
-        array $tickets_with_reservations,
980
-        array $valid_reserved_ticket_line_items = array(),
981
-        $source
982
-    ) {
983
-        if (self::debug) {
984
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
985
-        }
986
-        $total_tickets_released = 0;
987
-        $sold_out_events = array();
988
-        foreach ($tickets_with_reservations as $ticket_with_reservations) {
989
-            if (! $ticket_with_reservations instanceof EE_Ticket) {
990
-                continue;
991
-            }
992
-            $reserved_qty = $ticket_with_reservations->reserved();
993
-            if (self::debug) {
994
-                echo self::$nl . ' . $ticket_with_reservations->ID(): ' . $ticket_with_reservations->ID();
995
-                echo self::$nl . ' . $reserved_qty: ' . $reserved_qty;
996
-            }
997
-            foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) {
998
-                if ($valid_reserved_ticket_line_item instanceof EE_Line_Item
999
-                    && $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID()
1000
-                ) {
1001
-                    if (self::debug) {
1002
-                        echo self::$nl . ' . $valid_reserved_ticket_line_item->quantity(): '
1003
-                             . $valid_reserved_ticket_line_item->quantity();
1004
-                    }
1005
-                    $reserved_qty -= $valid_reserved_ticket_line_item->quantity();
1006
-                }
1007
-            }
1008
-            if ($reserved_qty > 0) {
1009
-                $ticket_with_reservations->add_extra_meta(
1010
-                    EE_Ticket::META_KEY_TICKET_RESERVATIONS,
1011
-                    __LINE__ . ') ' . $source . '()'
1012
-                );
1013
-                $ticket_with_reservations->decreaseReserved($reserved_qty, true, 'TicketSalesMonitor:' . __LINE__);
1014
-                $ticket_with_reservations->save();
1015
-                $total_tickets_released += $reserved_qty;
1016
-                $event = $ticket_with_reservations->get_related_event();
1017
-                // track sold out events
1018
-                if ($event instanceof EE_Event && $event->is_sold_out()) {
1019
-                    $sold_out_events[] = $event;
1020
-                }
1021
-            }
1022
-        }
1023
-        if (self::debug) {
1024
-            echo self::$nl . ' . $total_tickets_released: ' . $total_tickets_released;
1025
-        }
1026
-        // double check whether sold out events should remain sold out after releasing tickets
1027
-        if ($sold_out_events !== array()) {
1028
-            foreach ($sold_out_events as $sold_out_event) {
1029
-                /** @var EE_Event $sold_out_event */
1030
-                $sold_out_event->perform_sold_out_status_check();
1031
-            }
1032
-        }
1033
-        return $total_tickets_released;
1034
-    }
1035
-
1036
-
1037
-
1038
-    /********************************** SHUTDOWN  **********************************/
1039
-
1040
-
1041
-    /**
1042
-     * @param int $timestamp
1043
-     * @return false|int
1044
-     * @throws EE_Error
1045
-     * @throws InvalidArgumentException
1046
-     * @throws InvalidDataTypeException
1047
-     * @throws InvalidInterfaceException
1048
-     */
1049
-    public static function clear_expired_line_items_with_no_transaction($timestamp = 0)
1050
-    {
1051
-        /** @type WPDB $wpdb */
1052
-        global $wpdb;
1053
-        if (! absint($timestamp)) {
1054
-            /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
1055
-            $session_lifespan = LoaderFactory::getLoader()->getShared(
1056
-                'EventEspresso\core\domain\values\session\SessionLifespan'
1057
-            );
1058
-            $timestamp = $session_lifespan->expiration();
1059
-        }
1060
-        return $wpdb->query(
1061
-            $wpdb->prepare(
1062
-                'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
23
+	const debug = false;
24
+
25
+	private static $nl = '';
26
+
27
+	/**
28
+	 * an array of raw ticket data from EED_Ticket_Selector
29
+	 *
30
+	 * @var array $ticket_selections
31
+	 */
32
+	protected $ticket_selections = array();
33
+
34
+	/**
35
+	 * the raw ticket data from EED_Ticket_Selector is organized in rows
36
+	 * according to how they are displayed in the actual Ticket_Selector
37
+	 * this tracks the current row being processed
38
+	 *
39
+	 * @var int $current_row
40
+	 */
41
+	protected $current_row = 0;
42
+
43
+	/**
44
+	 * an array for tracking names of tickets that have sold out
45
+	 *
46
+	 * @var array $sold_out_tickets
47
+	 */
48
+	protected $sold_out_tickets = array();
49
+
50
+	/**
51
+	 * an array for tracking names of tickets that have had their quantities reduced
52
+	 *
53
+	 * @var array $decremented_tickets
54
+	 */
55
+	protected $decremented_tickets = array();
56
+
57
+
58
+	/**
59
+	 * set_hooks - for hooking into EE Core, other modules, etc
60
+	 *
61
+	 * @return    void
62
+	 */
63
+	public static function set_hooks()
64
+	{
65
+		self::$nl = defined('EE_TESTS_DIR') ? "\n" : '<br />';
66
+		// release tickets for expired carts
67
+		add_action(
68
+			'EED_Ticket_Selector__process_ticket_selections__before',
69
+			array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'),
70
+			1
71
+		);
72
+		// check ticket reserves AFTER MER does it's check (hence priority 20)
73
+		add_filter(
74
+			'FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty',
75
+			array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'),
76
+			20,
77
+			3
78
+		);
79
+		// add notices for sold out tickets
80
+		add_action(
81
+			'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart',
82
+			array('EED_Ticket_Sales_Monitor', 'post_notices'),
83
+			10
84
+		);
85
+
86
+		// handle tickets deleted from cart
87
+		add_action(
88
+			'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart',
89
+			array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'),
90
+			10,
91
+			2
92
+		);
93
+		// handle emptied carts
94
+		add_action(
95
+			'AHEE__EE_Session__reset_cart__before_reset',
96
+			array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
97
+			10,
98
+			1
99
+		);
100
+		add_action(
101
+			'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart',
102
+			array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
103
+			10,
104
+			1
105
+		);
106
+		// handle cancelled registrations
107
+		add_action(
108
+			'AHEE__EE_Session__reset_checkout__before_reset',
109
+			array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'),
110
+			10,
111
+			1
112
+		);
113
+		// cron tasks
114
+		add_action(
115
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__abandoned_transaction',
116
+			array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
117
+			10,
118
+			1
119
+		);
120
+		add_action(
121
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction',
122
+			array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
123
+			10,
124
+			1
125
+		);
126
+		add_action(
127
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction',
128
+			array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'),
129
+			10,
130
+			1
131
+		);
132
+	}
133
+
134
+
135
+	/**
136
+	 * set_hooks_admin - for hooking into EE Admin Core, other modules, etc
137
+	 *
138
+	 * @return void
139
+	 */
140
+	public static function set_hooks_admin()
141
+	{
142
+		EED_Ticket_Sales_Monitor::set_hooks();
143
+	}
144
+
145
+
146
+	/**
147
+	 * @return EED_Ticket_Sales_Monitor|EED_Module
148
+	 */
149
+	public static function instance()
150
+	{
151
+		return parent::get_instance(__CLASS__);
152
+	}
153
+
154
+
155
+	/**
156
+	 * @param WP_Query $WP_Query
157
+	 * @return    void
158
+	 */
159
+	public function run($WP_Query)
160
+	{
161
+	}
162
+
163
+
164
+
165
+	/********************************** PRE_TICKET_SALES  **********************************/
166
+
167
+
168
+	/**
169
+	 * Retrieves grand totals from the line items that have no TXN ID
170
+	 * and timestamps less than the current time minus the session lifespan.
171
+	 * These are carts that have been abandoned before the "registrant" even attempted to checkout.
172
+	 * We're going to release the tickets for these line items before attempting to add more to the cart.
173
+	 *
174
+	 * @return void
175
+	 * @throws DomainException
176
+	 * @throws EE_Error
177
+	 * @throws InvalidArgumentException
178
+	 * @throws InvalidDataTypeException
179
+	 * @throws InvalidInterfaceException
180
+	 * @throws UnexpectedEntityException
181
+	 */
182
+	public static function release_tickets_for_expired_carts()
183
+	{
184
+		if (self::debug) {
185
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
186
+		}
187
+		do_action('AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__begin');
188
+		$expired_ticket_IDs = array();
189
+		/** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
190
+		$session_lifespan = LoaderFactory::getLoader()->getShared(
191
+			'EventEspresso\core\domain\values\session\SessionLifespan'
192
+		);
193
+		$timestamp = $session_lifespan->expiration();
194
+		$expired_ticket_line_items = EEM_Line_Item::instance()->getTicketLineItemsForExpiredCarts($timestamp);
195
+		if (self::debug) {
196
+			echo self::$nl . ' . time(): ' . time();
197
+			echo self::$nl . ' . time() as date: ' . date('Y-m-d H:i a');
198
+			echo self::$nl . ' . session expiration: ' . $session_lifespan->expiration();
199
+			echo self::$nl . ' . session expiration as date: ' . date('Y-m-d H:i a', $session_lifespan->expiration());
200
+			echo self::$nl . ' . timestamp: ' . $timestamp;
201
+			echo self::$nl . ' . $expired_ticket_line_items: ' . count($expired_ticket_line_items);
202
+		}
203
+		if (! empty($expired_ticket_line_items)) {
204
+			foreach ($expired_ticket_line_items as $expired_ticket_line_item) {
205
+				if (! $expired_ticket_line_item instanceof EE_Line_Item) {
206
+					continue;
207
+				}
208
+				$expired_ticket_IDs[ $expired_ticket_line_item->OBJ_ID() ] = $expired_ticket_line_item->OBJ_ID();
209
+				if (self::debug) {
210
+					echo self::$nl . ' . $expired_ticket_line_item->OBJ_ID(): ' . $expired_ticket_line_item->OBJ_ID();
211
+					echo self::$nl . ' . $expired_ticket_line_item->timestamp(): '
212
+						 . date(
213
+							 'Y-m-d h:i a',
214
+							 $expired_ticket_line_item->timestamp(true)
215
+						 );
216
+				}
217
+			}
218
+			if (! empty($expired_ticket_IDs)) {
219
+				EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
220
+					\EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
221
+					array(),
222
+					__FUNCTION__
223
+				);
224
+				// now  let's get rid of expired line items so that they can't interfere with tracking
225
+				EED_Ticket_Sales_Monitor::clear_expired_line_items_with_no_transaction($timestamp);
226
+			}
227
+		}
228
+		do_action(
229
+			'AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__end',
230
+			$expired_ticket_IDs,
231
+			$expired_ticket_line_items
232
+		);
233
+	}
234
+
235
+
236
+
237
+	/********************************** VALIDATE_TICKET_SALE  **********************************/
238
+
239
+
240
+	/**
241
+	 * callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data'
242
+	 *
243
+	 * @param int       $qty
244
+	 * @param EE_Ticket $ticket
245
+	 * @return bool
246
+	 * @throws UnexpectedEntityException
247
+	 * @throws EE_Error
248
+	 */
249
+	public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket)
250
+	{
251
+		$qty = absint($qty);
252
+		if ($qty > 0) {
253
+			$qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
254
+		}
255
+		if (self::debug) {
256
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
257
+			echo self::$nl . self::$nl . '<b> RETURNED QTY: ' . $qty . '</b>';
258
+		}
259
+		return $qty;
260
+	}
261
+
262
+
263
+	/**
264
+	 * checks whether an individual ticket is available for purchase based on datetime, and ticket details
265
+	 *
266
+	 * @param   EE_Ticket $ticket
267
+	 * @param int         $qty
268
+	 * @return int
269
+	 * @throws UnexpectedEntityException
270
+	 * @throws EE_Error
271
+	 */
272
+	protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
273
+	{
274
+		if (self::debug) {
275
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
276
+		}
277
+		if (! $ticket instanceof EE_Ticket) {
278
+			return 0;
279
+		}
280
+		if (self::debug) {
281
+			echo self::$nl . '<b> . ticket->ID: ' . $ticket->ID() . '</b>';
282
+			echo self::$nl . ' . original ticket->reserved: ' . $ticket->reserved();
283
+		}
284
+		$ticket->refresh_from_db();
285
+		// first let's determine the ticket availability based on sales
286
+		$available = $ticket->qty('saleable');
287
+		if (self::debug) {
288
+			echo self::$nl . ' . . . ticket->qty: ' . $ticket->qty();
289
+			echo self::$nl . ' . . . ticket->sold: ' . $ticket->sold();
290
+			echo self::$nl . ' . . . ticket->reserved: ' . $ticket->reserved();
291
+			echo self::$nl . ' . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
292
+			echo self::$nl . ' . . . available: ' . $available;
293
+		}
294
+		if ($available < 1) {
295
+			$this->_ticket_sold_out($ticket);
296
+			return 0;
297
+		}
298
+		if (self::debug) {
299
+			echo self::$nl . ' . . . qty: ' . $qty;
300
+		}
301
+		if ($available < $qty) {
302
+			$qty = $available;
303
+			if (self::debug) {
304
+				echo self::$nl . ' . . . QTY ADJUSTED: ' . $qty;
305
+			}
306
+			$this->_ticket_quantity_decremented($ticket);
307
+		}
308
+		if ($this->_reserve_ticket($ticket, $qty)) {
309
+			return $qty;
310
+		} else {
311
+			return 0;
312
+		}
313
+	}
314
+
315
+
316
+	/**
317
+	 * increments ticket reserved based on quantity passed
318
+	 *
319
+	 * @param    EE_Ticket $ticket
320
+	 * @param int          $quantity
321
+	 * @return bool indicating success or failure
322
+	 * @throws EE_Error
323
+	 */
324
+	protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
325
+	{
326
+		if (self::debug) {
327
+			echo self::$nl . self::$nl . ' . . . INCREASE RESERVED: ' . $quantity;
328
+		}
329
+		return $ticket->increaseReserved($quantity, 'TicketSalesMonitor:' . __LINE__);
330
+	}
331
+
332
+
333
+	/**
334
+	 * @param  EE_Ticket $ticket
335
+	 * @param  int       $quantity
336
+	 * @return bool
337
+	 * @throws EE_Error
338
+	 */
339
+	protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
340
+	{
341
+		if (self::debug) {
342
+			echo self::$nl . ' . . . ticket->ID: ' . $ticket->ID();
343
+			echo self::$nl . ' . . . ticket->reserved before: ' . $ticket->reserved();
344
+		}
345
+		$ticket->decreaseReserved($quantity, true, 'TicketSalesMonitor:' . __LINE__);
346
+		if (self::debug) {
347
+			echo self::$nl . ' . . . ticket->reserved after: ' . $ticket->reserved();
348
+		}
349
+		return $ticket->save() ? 1 : 0;
350
+	}
351
+
352
+
353
+	/**
354
+	 * removes quantities within the ticket selector based on zero ticket availability
355
+	 *
356
+	 * @param    EE_Ticket $ticket
357
+	 * @return    void
358
+	 * @throws UnexpectedEntityException
359
+	 * @throws EE_Error
360
+	 */
361
+	protected function _ticket_sold_out(EE_Ticket $ticket)
362
+	{
363
+		if (self::debug) {
364
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
365
+			echo self::$nl . ' . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
366
+		}
367
+		$this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
368
+	}
369
+
370
+
371
+	/**
372
+	 * adjusts quantities within the ticket selector based on decreased ticket availability
373
+	 *
374
+	 * @param    EE_Ticket $ticket
375
+	 * @return void
376
+	 * @throws UnexpectedEntityException
377
+	 * @throws EE_Error
378
+	 */
379
+	protected function _ticket_quantity_decremented(EE_Ticket $ticket)
380
+	{
381
+		if (self::debug) {
382
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
383
+			echo self::$nl . ' . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
384
+		}
385
+		$this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
386
+	}
387
+
388
+
389
+	/**
390
+	 * builds string out of ticket and event name
391
+	 *
392
+	 * @param    EE_Ticket $ticket
393
+	 * @return string
394
+	 * @throws UnexpectedEntityException
395
+	 * @throws EE_Error
396
+	 */
397
+	protected function _get_ticket_and_event_name(EE_Ticket $ticket)
398
+	{
399
+		$event = $ticket->get_related_event();
400
+		if ($event instanceof EE_Event) {
401
+			$ticket_name = sprintf(
402
+				_x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'),
403
+				$ticket->name(),
404
+				$event->name()
405
+			);
406
+		} else {
407
+			$ticket_name = $ticket->name();
408
+		}
409
+		return $ticket_name;
410
+	}
411
+
412
+
413
+
414
+	/********************************** EVENT CART  **********************************/
415
+
416
+
417
+	/**
418
+	 * releases or reserves ticket(s) based on quantity passed
419
+	 *
420
+	 * @param  EE_Line_Item $line_item
421
+	 * @param  int          $quantity
422
+	 * @return void
423
+	 * @throws EE_Error
424
+	 * @throws InvalidArgumentException
425
+	 * @throws InvalidDataTypeException
426
+	 * @throws InvalidInterfaceException
427
+	 */
428
+	public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1)
429
+	{
430
+		$ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID()));
431
+		if ($ticket instanceof EE_Ticket) {
432
+			$ticket->add_extra_meta(
433
+				EE_Ticket::META_KEY_TICKET_RESERVATIONS,
434
+				__LINE__ . ') ' . __METHOD__ . '()'
435
+			);
436
+			if ($quantity > 0) {
437
+				EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
438
+			} else {
439
+				EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
440
+			}
441
+		}
442
+	}
443
+
444
+
445
+	/**
446
+	 * releases reserved ticket(s) based on quantity passed
447
+	 *
448
+	 * @param  EE_Ticket $ticket
449
+	 * @param  int       $quantity
450
+	 * @return void
451
+	 * @throws EE_Error
452
+	 */
453
+	public static function ticket_removed_from_cart(EE_Ticket $ticket, $quantity = 1)
454
+	{
455
+		$ticket->add_extra_meta(
456
+			EE_Ticket::META_KEY_TICKET_RESERVATIONS,
457
+			__LINE__ . ') ' . __METHOD__ . '()'
458
+		);
459
+		EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
460
+	}
461
+
462
+
463
+
464
+	/********************************** POST_NOTICES  **********************************/
465
+
466
+
467
+	/**
468
+	 * @return void
469
+	 * @throws EE_Error
470
+	 * @throws InvalidArgumentException
471
+	 * @throws ReflectionException
472
+	 * @throws InvalidDataTypeException
473
+	 * @throws InvalidInterfaceException
474
+	 */
475
+	public static function post_notices()
476
+	{
477
+		EED_Ticket_Sales_Monitor::instance()->_post_notices();
478
+	}
479
+
480
+
481
+	/**
482
+	 * @return void
483
+	 * @throws EE_Error
484
+	 * @throws InvalidArgumentException
485
+	 * @throws ReflectionException
486
+	 * @throws InvalidDataTypeException
487
+	 * @throws InvalidInterfaceException
488
+	 */
489
+	protected function _post_notices()
490
+	{
491
+		if (self::debug) {
492
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
493
+		}
494
+		$refresh_msg = '';
495
+		$none_added_msg = '';
496
+		if (defined('DOING_AJAX') && DOING_AJAX) {
497
+			$refresh_msg = __(
498
+				'Please refresh the page to view updated ticket quantities.',
499
+				'event_espresso'
500
+			);
501
+			$none_added_msg = __('No tickets were added for the event.', 'event_espresso');
502
+		}
503
+		if (! empty($this->sold_out_tickets)) {
504
+			EE_Error::add_attention(
505
+				sprintf(
506
+					apply_filters(
507
+						'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice',
508
+						__(
509
+							'We\'re sorry...%1$sThe following items have sold out since you first viewed this page, and can no longer be registered for:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
510
+							'event_espresso'
511
+						)
512
+					),
513
+					'<br />',
514
+					implode('<br />', $this->sold_out_tickets),
515
+					$none_added_msg,
516
+					$refresh_msg
517
+				)
518
+			);
519
+			// alter code flow in the Ticket Selector for better UX
520
+			add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true');
521
+			add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false');
522
+			$this->sold_out_tickets = array();
523
+			// and reset the cart
524
+			EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
525
+		}
526
+		if (! empty($this->decremented_tickets)) {
527
+			EE_Error::add_attention(
528
+				sprintf(
529
+					apply_filters(
530
+						'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice',
531
+						__(
532
+							'We\'re sorry...%1$sDue to sales that have occurred since you first viewed the last page, the following items have had their quantities adjusted to match the current available amount:%1$s%1$s%2$s%1$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%1$s%1$s%3$s%1$s%4$s%1$s',
533
+							'event_espresso'
534
+						)
535
+					),
536
+					'<br />',
537
+					implode('<br />', $this->decremented_tickets),
538
+					$none_added_msg,
539
+					$refresh_msg
540
+				)
541
+			);
542
+			$this->decremented_tickets = array();
543
+		}
544
+	}
545
+
546
+
547
+
548
+	/********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION  **********************************/
549
+
550
+
551
+	/**
552
+	 * releases reserved tickets for all registrations of an EE_Transaction
553
+	 * by default, will NOT release tickets for finalized transactions
554
+	 *
555
+	 * @param    EE_Transaction $transaction
556
+	 * @return int
557
+	 * @throws EE_Error
558
+	 * @throws InvalidSessionDataException
559
+	 */
560
+	protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
561
+	{
562
+		if (self::debug) {
563
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
564
+			echo self::$nl . ' . transaction->ID: ' . $transaction->ID();
565
+			echo self::$nl . ' . TXN status_ID: ' . $transaction->status_ID();
566
+		}
567
+		// check if 'finalize_registration' step has been completed...
568
+		$finalized = $transaction->reg_step_completed('finalize_registration');
569
+		if (self::debug) {
570
+			// DEBUG LOG
571
+			EEH_Debug_Tools::log(
572
+				__CLASS__,
573
+				__FUNCTION__,
574
+				__LINE__,
575
+				array('finalized' => $finalized),
576
+				false,
577
+				'EE_Transaction: ' . $transaction->ID()
578
+			);
579
+		}
580
+		// how many tickets were released
581
+		$count = 0;
582
+		if (self::debug) {
583
+			echo self::$nl . ' . . . TXN finalized: ' . $finalized;
584
+		}
585
+		$release_tickets_with_TXN_status = array(
586
+			EEM_Transaction::failed_status_code,
587
+			EEM_Transaction::abandoned_status_code,
588
+			EEM_Transaction::incomplete_status_code,
589
+		);
590
+		$events = array();
591
+		// if the session is getting cleared BEFORE the TXN has been finalized or the transaction is not completed
592
+		if (! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
593
+			// cancel any reserved tickets for registrations that were not approved
594
+			$registrations = $transaction->registrations();
595
+			if (self::debug) {
596
+				echo self::$nl . ' . . . # registrations: ' . count($registrations);
597
+				$reg = reset($registrations);
598
+				$ticket = $reg->ticket();
599
+				if ($ticket instanceof EE_Ticket) {
600
+					$ticket->add_extra_meta(
601
+						EE_Ticket::META_KEY_TICKET_RESERVATIONS,
602
+						__LINE__ . ') Release All Tickets TXN:' . $transaction->ID()
603
+					);
604
+				}
605
+			}
606
+			if (! empty($registrations)) {
607
+				foreach ($registrations as $registration) {
608
+					if ($registration instanceof EE_Registration
609
+						&& $this->_release_reserved_ticket_for_registration($registration, $transaction)
610
+					) {
611
+						$count++;
612
+						$events[ $registration->event_ID() ] = $registration->event();
613
+					}
614
+				}
615
+			}
616
+		}
617
+		if ($events !== array()) {
618
+			foreach ($events as $event) {
619
+				/** @var EE_Event $event */
620
+				$event->perform_sold_out_status_check();
621
+			}
622
+		}
623
+		return $count;
624
+	}
625
+
626
+
627
+	/**
628
+	 * releases reserved tickets for an EE_Registration
629
+	 * by default, will NOT release tickets for APPROVED registrations
630
+	 *
631
+	 * @param EE_Registration $registration
632
+	 * @param EE_Transaction  $transaction
633
+	 * @return int
634
+	 * @throws EE_Error
635
+	 */
636
+	protected function _release_reserved_ticket_for_registration(
637
+		EE_Registration $registration,
638
+		EE_Transaction $transaction
639
+	) {
640
+		$STS_ID = $transaction->status_ID();
641
+		if (self::debug) {
642
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
643
+			echo self::$nl . ' . . registration->ID: ' . $registration->ID();
644
+			echo self::$nl . ' . . registration->status_ID: ' . $registration->status_ID();
645
+			echo self::$nl . ' . . transaction->status_ID(): ' . $STS_ID;
646
+		}
647
+		if (// release Tickets for Failed Transactions and Abandoned Transactions
648
+			$STS_ID === EEM_Transaction::failed_status_code
649
+			|| $STS_ID === EEM_Transaction::abandoned_status_code
650
+			|| (
651
+				// also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved
652
+				$STS_ID === EEM_Transaction::incomplete_status_code
653
+				&& $registration->status_ID() !== EEM_Registration::status_id_approved
654
+			)
655
+		) {
656
+			if (self::debug) {
657
+				echo self::$nl . self::$nl . ' . . RELEASE RESERVED TICKET';
658
+				$rsrvd = $registration->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true);
659
+				echo self::$nl . ' . . . registration HAS_RESERVED_TICKET_KEY: ';
660
+				var_dump($rsrvd);
661
+			}
662
+			$registration->release_reserved_ticket(true, 'TicketSalesMonitor:' . __LINE__);
663
+			return 1;
664
+		}
665
+		return 0;
666
+	}
667
+
668
+
669
+
670
+	/********************************** SESSION_CART_RESET  **********************************/
671
+
672
+
673
+	/**
674
+	 * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset'
675
+	 *
676
+	 * @param EE_Session $session
677
+	 * @return void
678
+	 * @throws EE_Error
679
+	 * @throws InvalidArgumentException
680
+	 * @throws ReflectionException
681
+	 * @throws InvalidDataTypeException
682
+	 * @throws InvalidInterfaceException
683
+	 */
684
+	public static function session_cart_reset(EE_Session $session)
685
+	{
686
+		// don't release tickets if checkout was already reset
687
+		if (did_action('AHEE__EE_Session__reset_checkout__before_reset')) {
688
+			return;
689
+		}
690
+		if (self::debug) {
691
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
692
+		}
693
+		// first check of the session has a valid Checkout object
694
+		$checkout = $session->checkout();
695
+		if ($checkout instanceof EE_Checkout) {
696
+			// and use that to clear ticket reservations because it will update the associated registration meta data
697
+			EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
698
+			return;
699
+		}
700
+		$cart = $session->cart();
701
+		if ($cart instanceof EE_Cart) {
702
+			if (self::debug) {
703
+				echo self::$nl . self::$nl . ' cart instance of EE_Cart: ';
704
+			}
705
+			EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart, $session);
706
+		} else {
707
+			if (self::debug) {
708
+				echo self::$nl . self::$nl . ' invalid EE_Cart: ';
709
+				var_export($cart, true);
710
+			}
711
+		}
712
+	}
713
+
714
+
715
+	/**
716
+	 * releases reserved tickets in the EE_Cart
717
+	 *
718
+	 * @param EE_Cart $cart
719
+	 * @return void
720
+	 * @throws EE_Error
721
+	 * @throws InvalidArgumentException
722
+	 * @throws ReflectionException
723
+	 * @throws InvalidDataTypeException
724
+	 * @throws InvalidInterfaceException
725
+	 */
726
+	protected function _session_cart_reset(EE_Cart $cart, EE_Session $session)
727
+	{
728
+		if (self::debug) {
729
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
730
+		}
731
+		$ticket_line_items = $cart->get_tickets();
732
+		if (empty($ticket_line_items)) {
733
+			return;
734
+		}
735
+		if (self::debug) {
736
+			echo '<br /> . ticket_line_item count: ' . count($ticket_line_items);
737
+		}
738
+		foreach ($ticket_line_items as $ticket_line_item) {
739
+			if (self::debug) {
740
+				echo self::$nl . ' . ticket_line_item->ID(): ' . $ticket_line_item->ID();
741
+			}
742
+			if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
743
+				if (self::debug) {
744
+					echo self::$nl . ' . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
745
+				}
746
+				$ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
747
+				if ($ticket instanceof EE_Ticket) {
748
+					if (self::debug) {
749
+						echo self::$nl . ' . . ticket->ID(): ' . $ticket->ID();
750
+						echo self::$nl . ' . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
751
+					}
752
+					$ticket->add_extra_meta(
753
+						EE_Ticket::META_KEY_TICKET_RESERVATIONS,
754
+						__LINE__ . ') ' . __METHOD__ . '() SID = ' . $session->id()
755
+					);
756
+					$this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
757
+				}
758
+			}
759
+		}
760
+		if (self::debug) {
761
+			echo self::$nl . self::$nl . ' RESET COMPLETED ';
762
+		}
763
+	}
764
+
765
+
766
+
767
+	/********************************** SESSION_CHECKOUT_RESET  **********************************/
768
+
769
+
770
+	/**
771
+	 * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset'
772
+	 *
773
+	 * @param EE_Session $session
774
+	 * @return void
775
+	 * @throws EE_Error
776
+	 * @throws InvalidSessionDataException
777
+	 */
778
+	public static function session_checkout_reset(EE_Session $session)
779
+	{
780
+		// don't release tickets if cart was already reset
781
+		if (did_action('AHEE__EE_Session__reset_cart__before_reset')) {
782
+			return;
783
+		}
784
+		$checkout = $session->checkout();
785
+		if ($checkout instanceof EE_Checkout) {
786
+			EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
787
+		}
788
+	}
789
+
790
+
791
+	/**
792
+	 * releases reserved tickets for the EE_Checkout->transaction
793
+	 *
794
+	 * @param EE_Checkout $checkout
795
+	 * @return void
796
+	 * @throws EE_Error
797
+	 * @throws InvalidSessionDataException
798
+	 */
799
+	protected function _session_checkout_reset(EE_Checkout $checkout)
800
+	{
801
+		if (self::debug) {
802
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
803
+		}
804
+		// we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
805
+		if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
806
+			return;
807
+		}
808
+		$this->_release_all_reserved_tickets_for_transaction($checkout->transaction);
809
+	}
810
+
811
+
812
+
813
+	/********************************** SESSION_EXPIRED_RESET  **********************************/
814
+
815
+
816
+	/**
817
+	 * @param    EE_Session $session
818
+	 * @return    void
819
+	 */
820
+	public static function session_expired_reset(EE_Session $session)
821
+	{
822
+	}
823
+
824
+
825
+
826
+	/********************************** PROCESS_ABANDONED_TRANSACTIONS  **********************************/
827
+
828
+
829
+	/**
830
+	 * releases reserved tickets for all registrations of an ABANDONED EE_Transaction
831
+	 * by default, will NOT release tickets for free transactions, or any that have received a payment
832
+	 *
833
+	 * @param EE_Transaction $transaction
834
+	 * @return void
835
+	 * @throws EE_Error
836
+	 * @throws InvalidSessionDataException
837
+	 */
838
+	public static function process_abandoned_transactions(EE_Transaction $transaction)
839
+	{
840
+		// is this TXN free or has any money been paid towards this TXN? If so, then leave it alone
841
+		if ($transaction->is_free() || $transaction->paid() > 0) {
842
+			if (self::debug) {
843
+				// DEBUG LOG
844
+				EEH_Debug_Tools::log(
845
+					__CLASS__,
846
+					__FUNCTION__,
847
+					__LINE__,
848
+					array($transaction),
849
+					false,
850
+					'EE_Transaction: ' . $transaction->ID()
851
+				);
852
+			}
853
+			return;
854
+		}
855
+		// have their been any successful payments made ?
856
+		$payments = $transaction->payments();
857
+		foreach ($payments as $payment) {
858
+			if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) {
859
+				if (self::debug) {
860
+					// DEBUG LOG
861
+					EEH_Debug_Tools::log(
862
+						__CLASS__,
863
+						__FUNCTION__,
864
+						__LINE__,
865
+						array($payment),
866
+						false,
867
+						'EE_Transaction: ' . $transaction->ID()
868
+					);
869
+				}
870
+				return;
871
+			}
872
+		}
873
+		// since you haven't even attempted to pay for your ticket...
874
+		EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
875
+	}
876
+
877
+
878
+
879
+	/********************************** PROCESS_FAILED_TRANSACTIONS  **********************************/
880
+
881
+
882
+	/**
883
+	 * releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction
884
+	 *
885
+	 * @param EE_Transaction $transaction
886
+	 * @return void
887
+	 * @throws EE_Error
888
+	 * @throws InvalidSessionDataException
889
+	 */
890
+	public static function process_failed_transactions(EE_Transaction $transaction)
891
+	{
892
+		// since you haven't even attempted to pay for your ticket...
893
+		EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
894
+	}
895
+
896
+
897
+
898
+	/********************************** RESET RESERVATION COUNTS  *********************************/
899
+
900
+
901
+	/**
902
+	 * Resets all ticket and datetime reserved counts to zero
903
+	 * Tickets that are currently associated with a Transaction that is in progress
904
+	 *
905
+	 * @throws EE_Error
906
+	 * @throws DomainException
907
+	 * @throws InvalidDataTypeException
908
+	 * @throws InvalidInterfaceException
909
+	 * @throws InvalidArgumentException
910
+	 * @throws UnexpectedEntityException
911
+	 */
912
+	public static function reset_reservation_counts()
913
+	{
914
+		/** @var EE_Line_Item[] $valid_reserved_tickets */
915
+		$valid_reserved_tickets = array();
916
+		/** @var EE_Transaction[] $transactions_not_in_progress */
917
+		$transactions_not_in_progress = EEM_Transaction::instance()->get_transactions_not_in_progress();
918
+		foreach ($transactions_not_in_progress as $transaction) {
919
+			// if this TXN has been fully completed, then skip it
920
+			if ($transaction->reg_step_completed('finalize_registration')) {
921
+				continue;
922
+			}
923
+			$total_line_item = $transaction->total_line_item();
924
+			// $transaction_in_progress->line
925
+			if (! $total_line_item instanceof EE_Line_Item) {
926
+				throw new DomainException(
927
+					esc_html__(
928
+						'Transaction does not have a valid Total Line Item associated with it.',
929
+						'event_espresso'
930
+					)
931
+				);
932
+			}
933
+			$valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
934
+				$total_line_item
935
+			);
936
+		}
937
+		$total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts();
938
+		foreach ($total_line_items as $total_line_item) {
939
+			$valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
940
+				$total_line_item
941
+			);
942
+		}
943
+		$tickets_with_reservations = EEM_Ticket::instance()->get_tickets_with_reservations();
944
+		return EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
945
+			$tickets_with_reservations,
946
+			$valid_reserved_tickets,
947
+			__FUNCTION__
948
+		);
949
+	}
950
+
951
+
952
+	/**
953
+	 * @param EE_Line_Item $total_line_item
954
+	 * @return EE_Line_Item[]
955
+	 */
956
+	private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item)
957
+	{
958
+		/** @var EE_Line_Item[] $valid_reserved_tickets */
959
+		$valid_reserved_tickets = array();
960
+		$ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
961
+		foreach ($ticket_line_items as $ticket_line_item) {
962
+			if ($ticket_line_item instanceof EE_Line_Item) {
963
+				$valid_reserved_tickets[] = $ticket_line_item;
964
+			}
965
+		}
966
+		return $valid_reserved_tickets;
967
+	}
968
+
969
+
970
+	/**
971
+	 * @param EE_Ticket[]    $tickets_with_reservations
972
+	 * @param EE_Line_Item[] $valid_reserved_ticket_line_items
973
+	 * @return int
974
+	 * @throws UnexpectedEntityException
975
+	 * @throws DomainException
976
+	 * @throws EE_Error
977
+	 */
978
+	private static function release_reservations_for_tickets(
979
+		array $tickets_with_reservations,
980
+		array $valid_reserved_ticket_line_items = array(),
981
+		$source
982
+	) {
983
+		if (self::debug) {
984
+			echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
985
+		}
986
+		$total_tickets_released = 0;
987
+		$sold_out_events = array();
988
+		foreach ($tickets_with_reservations as $ticket_with_reservations) {
989
+			if (! $ticket_with_reservations instanceof EE_Ticket) {
990
+				continue;
991
+			}
992
+			$reserved_qty = $ticket_with_reservations->reserved();
993
+			if (self::debug) {
994
+				echo self::$nl . ' . $ticket_with_reservations->ID(): ' . $ticket_with_reservations->ID();
995
+				echo self::$nl . ' . $reserved_qty: ' . $reserved_qty;
996
+			}
997
+			foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) {
998
+				if ($valid_reserved_ticket_line_item instanceof EE_Line_Item
999
+					&& $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID()
1000
+				) {
1001
+					if (self::debug) {
1002
+						echo self::$nl . ' . $valid_reserved_ticket_line_item->quantity(): '
1003
+							 . $valid_reserved_ticket_line_item->quantity();
1004
+					}
1005
+					$reserved_qty -= $valid_reserved_ticket_line_item->quantity();
1006
+				}
1007
+			}
1008
+			if ($reserved_qty > 0) {
1009
+				$ticket_with_reservations->add_extra_meta(
1010
+					EE_Ticket::META_KEY_TICKET_RESERVATIONS,
1011
+					__LINE__ . ') ' . $source . '()'
1012
+				);
1013
+				$ticket_with_reservations->decreaseReserved($reserved_qty, true, 'TicketSalesMonitor:' . __LINE__);
1014
+				$ticket_with_reservations->save();
1015
+				$total_tickets_released += $reserved_qty;
1016
+				$event = $ticket_with_reservations->get_related_event();
1017
+				// track sold out events
1018
+				if ($event instanceof EE_Event && $event->is_sold_out()) {
1019
+					$sold_out_events[] = $event;
1020
+				}
1021
+			}
1022
+		}
1023
+		if (self::debug) {
1024
+			echo self::$nl . ' . $total_tickets_released: ' . $total_tickets_released;
1025
+		}
1026
+		// double check whether sold out events should remain sold out after releasing tickets
1027
+		if ($sold_out_events !== array()) {
1028
+			foreach ($sold_out_events as $sold_out_event) {
1029
+				/** @var EE_Event $sold_out_event */
1030
+				$sold_out_event->perform_sold_out_status_check();
1031
+			}
1032
+		}
1033
+		return $total_tickets_released;
1034
+	}
1035
+
1036
+
1037
+
1038
+	/********************************** SHUTDOWN  **********************************/
1039
+
1040
+
1041
+	/**
1042
+	 * @param int $timestamp
1043
+	 * @return false|int
1044
+	 * @throws EE_Error
1045
+	 * @throws InvalidArgumentException
1046
+	 * @throws InvalidDataTypeException
1047
+	 * @throws InvalidInterfaceException
1048
+	 */
1049
+	public static function clear_expired_line_items_with_no_transaction($timestamp = 0)
1050
+	{
1051
+		/** @type WPDB $wpdb */
1052
+		global $wpdb;
1053
+		if (! absint($timestamp)) {
1054
+			/** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
1055
+			$session_lifespan = LoaderFactory::getLoader()->getShared(
1056
+				'EventEspresso\core\domain\values\session\SessionLifespan'
1057
+			);
1058
+			$timestamp = $session_lifespan->expiration();
1059
+		}
1060
+		return $wpdb->query(
1061
+			$wpdb->prepare(
1062
+				'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
1063 1063
                 WHERE TXN_ID = 0 AND LIN_timestamp <= %s',
1064
-                // use GMT time because that's what LIN_timestamps are in
1065
-                date('Y-m-d H:i:s', $timestamp)
1066
-            )
1067
-        );
1068
-    }
1064
+				// use GMT time because that's what LIN_timestamps are in
1065
+				date('Y-m-d H:i:s', $timestamp)
1066
+			)
1067
+		);
1068
+	}
1069 1069
 }
Please login to merge, or discard this patch.
Spacing   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
     public static function release_tickets_for_expired_carts()
183 183
     {
184 184
         if (self::debug) {
185
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
185
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'()';
186 186
         }
187 187
         do_action('AHEE__EED_Ticket_Sales_Monitor__release_tickets_for_expired_carts__begin');
188 188
         $expired_ticket_IDs = array();
@@ -193,29 +193,29 @@  discard block
 block discarded – undo
193 193
         $timestamp = $session_lifespan->expiration();
194 194
         $expired_ticket_line_items = EEM_Line_Item::instance()->getTicketLineItemsForExpiredCarts($timestamp);
195 195
         if (self::debug) {
196
-            echo self::$nl . ' . time(): ' . time();
197
-            echo self::$nl . ' . time() as date: ' . date('Y-m-d H:i a');
198
-            echo self::$nl . ' . session expiration: ' . $session_lifespan->expiration();
199
-            echo self::$nl . ' . session expiration as date: ' . date('Y-m-d H:i a', $session_lifespan->expiration());
200
-            echo self::$nl . ' . timestamp: ' . $timestamp;
201
-            echo self::$nl . ' . $expired_ticket_line_items: ' . count($expired_ticket_line_items);
196
+            echo self::$nl.' . time(): '.time();
197
+            echo self::$nl.' . time() as date: '.date('Y-m-d H:i a');
198
+            echo self::$nl.' . session expiration: '.$session_lifespan->expiration();
199
+            echo self::$nl.' . session expiration as date: '.date('Y-m-d H:i a', $session_lifespan->expiration());
200
+            echo self::$nl.' . timestamp: '.$timestamp;
201
+            echo self::$nl.' . $expired_ticket_line_items: '.count($expired_ticket_line_items);
202 202
         }
203
-        if (! empty($expired_ticket_line_items)) {
203
+        if ( ! empty($expired_ticket_line_items)) {
204 204
             foreach ($expired_ticket_line_items as $expired_ticket_line_item) {
205
-                if (! $expired_ticket_line_item instanceof EE_Line_Item) {
205
+                if ( ! $expired_ticket_line_item instanceof EE_Line_Item) {
206 206
                     continue;
207 207
                 }
208
-                $expired_ticket_IDs[ $expired_ticket_line_item->OBJ_ID() ] = $expired_ticket_line_item->OBJ_ID();
208
+                $expired_ticket_IDs[$expired_ticket_line_item->OBJ_ID()] = $expired_ticket_line_item->OBJ_ID();
209 209
                 if (self::debug) {
210
-                    echo self::$nl . ' . $expired_ticket_line_item->OBJ_ID(): ' . $expired_ticket_line_item->OBJ_ID();
211
-                    echo self::$nl . ' . $expired_ticket_line_item->timestamp(): '
210
+                    echo self::$nl.' . $expired_ticket_line_item->OBJ_ID(): '.$expired_ticket_line_item->OBJ_ID();
211
+                    echo self::$nl.' . $expired_ticket_line_item->timestamp(): '
212 212
                          . date(
213 213
                              'Y-m-d h:i a',
214 214
                              $expired_ticket_line_item->timestamp(true)
215 215
                          );
216 216
                 }
217 217
             }
218
-            if (! empty($expired_ticket_IDs)) {
218
+            if ( ! empty($expired_ticket_IDs)) {
219 219
                 EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
220 220
                     \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
221 221
                     array(),
@@ -253,8 +253,8 @@  discard block
 block discarded – undo
253 253
             $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
254 254
         }
255 255
         if (self::debug) {
256
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
257
-            echo self::$nl . self::$nl . '<b> RETURNED QTY: ' . $qty . '</b>';
256
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'()';
257
+            echo self::$nl.self::$nl.'<b> RETURNED QTY: '.$qty.'</b>';
258 258
         }
259 259
         return $qty;
260 260
     }
@@ -272,36 +272,36 @@  discard block
 block discarded – undo
272 272
     protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
273 273
     {
274 274
         if (self::debug) {
275
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
275
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
276 276
         }
277
-        if (! $ticket instanceof EE_Ticket) {
277
+        if ( ! $ticket instanceof EE_Ticket) {
278 278
             return 0;
279 279
         }
280 280
         if (self::debug) {
281
-            echo self::$nl . '<b> . ticket->ID: ' . $ticket->ID() . '</b>';
282
-            echo self::$nl . ' . original ticket->reserved: ' . $ticket->reserved();
281
+            echo self::$nl.'<b> . ticket->ID: '.$ticket->ID().'</b>';
282
+            echo self::$nl.' . original ticket->reserved: '.$ticket->reserved();
283 283
         }
284 284
         $ticket->refresh_from_db();
285 285
         // first let's determine the ticket availability based on sales
286 286
         $available = $ticket->qty('saleable');
287 287
         if (self::debug) {
288
-            echo self::$nl . ' . . . ticket->qty: ' . $ticket->qty();
289
-            echo self::$nl . ' . . . ticket->sold: ' . $ticket->sold();
290
-            echo self::$nl . ' . . . ticket->reserved: ' . $ticket->reserved();
291
-            echo self::$nl . ' . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
292
-            echo self::$nl . ' . . . available: ' . $available;
288
+            echo self::$nl.' . . . ticket->qty: '.$ticket->qty();
289
+            echo self::$nl.' . . . ticket->sold: '.$ticket->sold();
290
+            echo self::$nl.' . . . ticket->reserved: '.$ticket->reserved();
291
+            echo self::$nl.' . . . ticket->qty(saleable): '.$ticket->qty('saleable');
292
+            echo self::$nl.' . . . available: '.$available;
293 293
         }
294 294
         if ($available < 1) {
295 295
             $this->_ticket_sold_out($ticket);
296 296
             return 0;
297 297
         }
298 298
         if (self::debug) {
299
-            echo self::$nl . ' . . . qty: ' . $qty;
299
+            echo self::$nl.' . . . qty: '.$qty;
300 300
         }
301 301
         if ($available < $qty) {
302 302
             $qty = $available;
303 303
             if (self::debug) {
304
-                echo self::$nl . ' . . . QTY ADJUSTED: ' . $qty;
304
+                echo self::$nl.' . . . QTY ADJUSTED: '.$qty;
305 305
             }
306 306
             $this->_ticket_quantity_decremented($ticket);
307 307
         }
@@ -324,9 +324,9 @@  discard block
 block discarded – undo
324 324
     protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
325 325
     {
326 326
         if (self::debug) {
327
-            echo self::$nl . self::$nl . ' . . . INCREASE RESERVED: ' . $quantity;
327
+            echo self::$nl.self::$nl.' . . . INCREASE RESERVED: '.$quantity;
328 328
         }
329
-        return $ticket->increaseReserved($quantity, 'TicketSalesMonitor:' . __LINE__);
329
+        return $ticket->increaseReserved($quantity, 'TicketSalesMonitor:'.__LINE__);
330 330
     }
331 331
 
332 332
 
@@ -339,12 +339,12 @@  discard block
 block discarded – undo
339 339
     protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
340 340
     {
341 341
         if (self::debug) {
342
-            echo self::$nl . ' . . . ticket->ID: ' . $ticket->ID();
343
-            echo self::$nl . ' . . . ticket->reserved before: ' . $ticket->reserved();
342
+            echo self::$nl.' . . . ticket->ID: '.$ticket->ID();
343
+            echo self::$nl.' . . . ticket->reserved before: '.$ticket->reserved();
344 344
         }
345
-        $ticket->decreaseReserved($quantity, true, 'TicketSalesMonitor:' . __LINE__);
345
+        $ticket->decreaseReserved($quantity, true, 'TicketSalesMonitor:'.__LINE__);
346 346
         if (self::debug) {
347
-            echo self::$nl . ' . . . ticket->reserved after: ' . $ticket->reserved();
347
+            echo self::$nl.' . . . ticket->reserved after: '.$ticket->reserved();
348 348
         }
349 349
         return $ticket->save() ? 1 : 0;
350 350
     }
@@ -361,8 +361,8 @@  discard block
 block discarded – undo
361 361
     protected function _ticket_sold_out(EE_Ticket $ticket)
362 362
     {
363 363
         if (self::debug) {
364
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
365
-            echo self::$nl . ' . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
364
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
365
+            echo self::$nl.' . . ticket->name: '.$this->_get_ticket_and_event_name($ticket);
366 366
         }
367 367
         $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
368 368
     }
@@ -379,8 +379,8 @@  discard block
 block discarded – undo
379 379
     protected function _ticket_quantity_decremented(EE_Ticket $ticket)
380 380
     {
381 381
         if (self::debug) {
382
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
383
-            echo self::$nl . ' . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
382
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
383
+            echo self::$nl.' . . ticket->name: '.$this->_get_ticket_and_event_name($ticket);
384 384
         }
385 385
         $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
386 386
     }
@@ -431,7 +431,7 @@  discard block
 block discarded – undo
431 431
         if ($ticket instanceof EE_Ticket) {
432 432
             $ticket->add_extra_meta(
433 433
                 EE_Ticket::META_KEY_TICKET_RESERVATIONS,
434
-                __LINE__ . ') ' . __METHOD__ . '()'
434
+                __LINE__.') '.__METHOD__.'()'
435 435
             );
436 436
             if ($quantity > 0) {
437 437
                 EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
@@ -454,7 +454,7 @@  discard block
 block discarded – undo
454 454
     {
455 455
         $ticket->add_extra_meta(
456 456
             EE_Ticket::META_KEY_TICKET_RESERVATIONS,
457
-            __LINE__ . ') ' . __METHOD__ . '()'
457
+            __LINE__.') '.__METHOD__.'()'
458 458
         );
459 459
         EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
460 460
     }
@@ -489,7 +489,7 @@  discard block
 block discarded – undo
489 489
     protected function _post_notices()
490 490
     {
491 491
         if (self::debug) {
492
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
492
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
493 493
         }
494 494
         $refresh_msg = '';
495 495
         $none_added_msg = '';
@@ -500,7 +500,7 @@  discard block
 block discarded – undo
500 500
             );
501 501
             $none_added_msg = __('No tickets were added for the event.', 'event_espresso');
502 502
         }
503
-        if (! empty($this->sold_out_tickets)) {
503
+        if ( ! empty($this->sold_out_tickets)) {
504 504
             EE_Error::add_attention(
505 505
                 sprintf(
506 506
                     apply_filters(
@@ -523,7 +523,7 @@  discard block
 block discarded – undo
523 523
             // and reset the cart
524 524
             EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
525 525
         }
526
-        if (! empty($this->decremented_tickets)) {
526
+        if ( ! empty($this->decremented_tickets)) {
527 527
             EE_Error::add_attention(
528 528
                 sprintf(
529 529
                     apply_filters(
@@ -560,9 +560,9 @@  discard block
 block discarded – undo
560 560
     protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
561 561
     {
562 562
         if (self::debug) {
563
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
564
-            echo self::$nl . ' . transaction->ID: ' . $transaction->ID();
565
-            echo self::$nl . ' . TXN status_ID: ' . $transaction->status_ID();
563
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
564
+            echo self::$nl.' . transaction->ID: '.$transaction->ID();
565
+            echo self::$nl.' . TXN status_ID: '.$transaction->status_ID();
566 566
         }
567 567
         // check if 'finalize_registration' step has been completed...
568 568
         $finalized = $transaction->reg_step_completed('finalize_registration');
@@ -574,13 +574,13 @@  discard block
 block discarded – undo
574 574
                 __LINE__,
575 575
                 array('finalized' => $finalized),
576 576
                 false,
577
-                'EE_Transaction: ' . $transaction->ID()
577
+                'EE_Transaction: '.$transaction->ID()
578 578
             );
579 579
         }
580 580
         // how many tickets were released
581 581
         $count = 0;
582 582
         if (self::debug) {
583
-            echo self::$nl . ' . . . TXN finalized: ' . $finalized;
583
+            echo self::$nl.' . . . TXN finalized: '.$finalized;
584 584
         }
585 585
         $release_tickets_with_TXN_status = array(
586 586
             EEM_Transaction::failed_status_code,
@@ -589,27 +589,27 @@  discard block
 block discarded – undo
589 589
         );
590 590
         $events = array();
591 591
         // if the session is getting cleared BEFORE the TXN has been finalized or the transaction is not completed
592
-        if (! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
592
+        if ( ! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
593 593
             // cancel any reserved tickets for registrations that were not approved
594 594
             $registrations = $transaction->registrations();
595 595
             if (self::debug) {
596
-                echo self::$nl . ' . . . # registrations: ' . count($registrations);
596
+                echo self::$nl.' . . . # registrations: '.count($registrations);
597 597
                 $reg = reset($registrations);
598 598
                 $ticket = $reg->ticket();
599 599
                 if ($ticket instanceof EE_Ticket) {
600 600
                     $ticket->add_extra_meta(
601 601
                         EE_Ticket::META_KEY_TICKET_RESERVATIONS,
602
-                        __LINE__ . ') Release All Tickets TXN:' . $transaction->ID()
602
+                        __LINE__.') Release All Tickets TXN:'.$transaction->ID()
603 603
                     );
604 604
                 }
605 605
             }
606
-            if (! empty($registrations)) {
606
+            if ( ! empty($registrations)) {
607 607
                 foreach ($registrations as $registration) {
608 608
                     if ($registration instanceof EE_Registration
609 609
                         && $this->_release_reserved_ticket_for_registration($registration, $transaction)
610 610
                     ) {
611 611
                         $count++;
612
-                        $events[ $registration->event_ID() ] = $registration->event();
612
+                        $events[$registration->event_ID()] = $registration->event();
613 613
                     }
614 614
                 }
615 615
             }
@@ -639,10 +639,10 @@  discard block
 block discarded – undo
639 639
     ) {
640 640
         $STS_ID = $transaction->status_ID();
641 641
         if (self::debug) {
642
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
643
-            echo self::$nl . ' . . registration->ID: ' . $registration->ID();
644
-            echo self::$nl . ' . . registration->status_ID: ' . $registration->status_ID();
645
-            echo self::$nl . ' . . transaction->status_ID(): ' . $STS_ID;
642
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
643
+            echo self::$nl.' . . registration->ID: '.$registration->ID();
644
+            echo self::$nl.' . . registration->status_ID: '.$registration->status_ID();
645
+            echo self::$nl.' . . transaction->status_ID(): '.$STS_ID;
646 646
         }
647 647
         if (// release Tickets for Failed Transactions and Abandoned Transactions
648 648
             $STS_ID === EEM_Transaction::failed_status_code
@@ -654,12 +654,12 @@  discard block
 block discarded – undo
654 654
             )
655 655
         ) {
656 656
             if (self::debug) {
657
-                echo self::$nl . self::$nl . ' . . RELEASE RESERVED TICKET';
657
+                echo self::$nl.self::$nl.' . . RELEASE RESERVED TICKET';
658 658
                 $rsrvd = $registration->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true);
659
-                echo self::$nl . ' . . . registration HAS_RESERVED_TICKET_KEY: ';
659
+                echo self::$nl.' . . . registration HAS_RESERVED_TICKET_KEY: ';
660 660
                 var_dump($rsrvd);
661 661
             }
662
-            $registration->release_reserved_ticket(true, 'TicketSalesMonitor:' . __LINE__);
662
+            $registration->release_reserved_ticket(true, 'TicketSalesMonitor:'.__LINE__);
663 663
             return 1;
664 664
         }
665 665
         return 0;
@@ -688,7 +688,7 @@  discard block
 block discarded – undo
688 688
             return;
689 689
         }
690 690
         if (self::debug) {
691
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
691
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
692 692
         }
693 693
         // first check of the session has a valid Checkout object
694 694
         $checkout = $session->checkout();
@@ -700,12 +700,12 @@  discard block
 block discarded – undo
700 700
         $cart = $session->cart();
701 701
         if ($cart instanceof EE_Cart) {
702 702
             if (self::debug) {
703
-                echo self::$nl . self::$nl . ' cart instance of EE_Cart: ';
703
+                echo self::$nl.self::$nl.' cart instance of EE_Cart: ';
704 704
             }
705 705
             EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart, $session);
706 706
         } else {
707 707
             if (self::debug) {
708
-                echo self::$nl . self::$nl . ' invalid EE_Cart: ';
708
+                echo self::$nl.self::$nl.' invalid EE_Cart: ';
709 709
                 var_export($cart, true);
710 710
             }
711 711
         }
@@ -726,39 +726,39 @@  discard block
 block discarded – undo
726 726
     protected function _session_cart_reset(EE_Cart $cart, EE_Session $session)
727 727
     {
728 728
         if (self::debug) {
729
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
729
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
730 730
         }
731 731
         $ticket_line_items = $cart->get_tickets();
732 732
         if (empty($ticket_line_items)) {
733 733
             return;
734 734
         }
735 735
         if (self::debug) {
736
-            echo '<br /> . ticket_line_item count: ' . count($ticket_line_items);
736
+            echo '<br /> . ticket_line_item count: '.count($ticket_line_items);
737 737
         }
738 738
         foreach ($ticket_line_items as $ticket_line_item) {
739 739
             if (self::debug) {
740
-                echo self::$nl . ' . ticket_line_item->ID(): ' . $ticket_line_item->ID();
740
+                echo self::$nl.' . ticket_line_item->ID(): '.$ticket_line_item->ID();
741 741
             }
742 742
             if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
743 743
                 if (self::debug) {
744
-                    echo self::$nl . ' . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
744
+                    echo self::$nl.' . . ticket_line_item->OBJ_ID(): '.$ticket_line_item->OBJ_ID();
745 745
                 }
746 746
                 $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
747 747
                 if ($ticket instanceof EE_Ticket) {
748 748
                     if (self::debug) {
749
-                        echo self::$nl . ' . . ticket->ID(): ' . $ticket->ID();
750
-                        echo self::$nl . ' . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
749
+                        echo self::$nl.' . . ticket->ID(): '.$ticket->ID();
750
+                        echo self::$nl.' . . ticket_line_item->quantity(): '.$ticket_line_item->quantity();
751 751
                     }
752 752
                     $ticket->add_extra_meta(
753 753
                         EE_Ticket::META_KEY_TICKET_RESERVATIONS,
754
-                        __LINE__ . ') ' . __METHOD__ . '() SID = ' . $session->id()
754
+                        __LINE__.') '.__METHOD__.'() SID = '.$session->id()
755 755
                     );
756 756
                     $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
757 757
                 }
758 758
             }
759 759
         }
760 760
         if (self::debug) {
761
-            echo self::$nl . self::$nl . ' RESET COMPLETED ';
761
+            echo self::$nl.self::$nl.' RESET COMPLETED ';
762 762
         }
763 763
     }
764 764
 
@@ -799,7 +799,7 @@  discard block
 block discarded – undo
799 799
     protected function _session_checkout_reset(EE_Checkout $checkout)
800 800
     {
801 801
         if (self::debug) {
802
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '() ';
802
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'() ';
803 803
         }
804 804
         // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
805 805
         if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
@@ -847,7 +847,7 @@  discard block
 block discarded – undo
847 847
                     __LINE__,
848 848
                     array($transaction),
849 849
                     false,
850
-                    'EE_Transaction: ' . $transaction->ID()
850
+                    'EE_Transaction: '.$transaction->ID()
851 851
                 );
852 852
             }
853 853
             return;
@@ -864,7 +864,7 @@  discard block
 block discarded – undo
864 864
                         __LINE__,
865 865
                         array($payment),
866 866
                         false,
867
-                        'EE_Transaction: ' . $transaction->ID()
867
+                        'EE_Transaction: '.$transaction->ID()
868 868
                     );
869 869
                 }
870 870
                 return;
@@ -922,7 +922,7 @@  discard block
 block discarded – undo
922 922
             }
923 923
             $total_line_item = $transaction->total_line_item();
924 924
             // $transaction_in_progress->line
925
-            if (! $total_line_item instanceof EE_Line_Item) {
925
+            if ( ! $total_line_item instanceof EE_Line_Item) {
926 926
                 throw new DomainException(
927 927
                     esc_html__(
928 928
                         'Transaction does not have a valid Total Line Item associated with it.',
@@ -981,25 +981,25 @@  discard block
 block discarded – undo
981 981
         $source
982 982
     ) {
983 983
         if (self::debug) {
984
-            echo self::$nl . self::$nl . __LINE__ . ') ' . __METHOD__ . '()';
984
+            echo self::$nl.self::$nl.__LINE__.') '.__METHOD__.'()';
985 985
         }
986 986
         $total_tickets_released = 0;
987 987
         $sold_out_events = array();
988 988
         foreach ($tickets_with_reservations as $ticket_with_reservations) {
989
-            if (! $ticket_with_reservations instanceof EE_Ticket) {
989
+            if ( ! $ticket_with_reservations instanceof EE_Ticket) {
990 990
                 continue;
991 991
             }
992 992
             $reserved_qty = $ticket_with_reservations->reserved();
993 993
             if (self::debug) {
994
-                echo self::$nl . ' . $ticket_with_reservations->ID(): ' . $ticket_with_reservations->ID();
995
-                echo self::$nl . ' . $reserved_qty: ' . $reserved_qty;
994
+                echo self::$nl.' . $ticket_with_reservations->ID(): '.$ticket_with_reservations->ID();
995
+                echo self::$nl.' . $reserved_qty: '.$reserved_qty;
996 996
             }
997 997
             foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) {
998 998
                 if ($valid_reserved_ticket_line_item instanceof EE_Line_Item
999 999
                     && $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID()
1000 1000
                 ) {
1001 1001
                     if (self::debug) {
1002
-                        echo self::$nl . ' . $valid_reserved_ticket_line_item->quantity(): '
1002
+                        echo self::$nl.' . $valid_reserved_ticket_line_item->quantity(): '
1003 1003
                              . $valid_reserved_ticket_line_item->quantity();
1004 1004
                     }
1005 1005
                     $reserved_qty -= $valid_reserved_ticket_line_item->quantity();
@@ -1008,9 +1008,9 @@  discard block
 block discarded – undo
1008 1008
             if ($reserved_qty > 0) {
1009 1009
                 $ticket_with_reservations->add_extra_meta(
1010 1010
                     EE_Ticket::META_KEY_TICKET_RESERVATIONS,
1011
-                    __LINE__ . ') ' . $source . '()'
1011
+                    __LINE__.') '.$source.'()'
1012 1012
                 );
1013
-                $ticket_with_reservations->decreaseReserved($reserved_qty, true, 'TicketSalesMonitor:' . __LINE__);
1013
+                $ticket_with_reservations->decreaseReserved($reserved_qty, true, 'TicketSalesMonitor:'.__LINE__);
1014 1014
                 $ticket_with_reservations->save();
1015 1015
                 $total_tickets_released += $reserved_qty;
1016 1016
                 $event = $ticket_with_reservations->get_related_event();
@@ -1021,7 +1021,7 @@  discard block
 block discarded – undo
1021 1021
             }
1022 1022
         }
1023 1023
         if (self::debug) {
1024
-            echo self::$nl . ' . $total_tickets_released: ' . $total_tickets_released;
1024
+            echo self::$nl.' . $total_tickets_released: '.$total_tickets_released;
1025 1025
         }
1026 1026
         // double check whether sold out events should remain sold out after releasing tickets
1027 1027
         if ($sold_out_events !== array()) {
@@ -1050,7 +1050,7 @@  discard block
 block discarded – undo
1050 1050
     {
1051 1051
         /** @type WPDB $wpdb */
1052 1052
         global $wpdb;
1053
-        if (! absint($timestamp)) {
1053
+        if ( ! absint($timestamp)) {
1054 1054
             /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
1055 1055
             $session_lifespan = LoaderFactory::getLoader()->getShared(
1056 1056
                 'EventEspresso\core\domain\values\session\SessionLifespan'
@@ -1059,7 +1059,7 @@  discard block
 block discarded – undo
1059 1059
         }
1060 1060
         return $wpdb->query(
1061 1061
             $wpdb->prepare(
1062
-                'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
1062
+                'DELETE FROM '.EEM_Line_Item::instance()->table().'
1063 1063
                 WHERE TXN_ID = 0 AND LIN_timestamp <= %s',
1064 1064
                 // use GMT time because that's what LIN_timestamps are in
1065 1065
                 date('Y-m-d H:i:s', $timestamp)
Please login to merge, or discard this patch.
core/db_classes/EE_Ticket.class.php 3 patches
Doc Comments   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -123,7 +123,8 @@
 block discarded – undo
123 123
      *                               relevant status const
124 124
      * @param bool | null $remaining if it is already known that tickets are available, then simply pass a bool to save
125 125
      *               further processing
126
-     * @return mixed status int if the display string isn't requested
126
+     * @param boolean $remaining
127
+     * @return string status int if the display string isn't requested
127 128
      * @throws EE_Error
128 129
      */
129 130
     public function ticket_status($display = false, $remaining = null)
Please login to merge, or discard this patch.
Indentation   +1723 added lines, -1723 removed lines patch added patch discarded remove patch
@@ -14,1731 +14,1731 @@
 block discarded – undo
14 14
 class EE_Ticket extends EE_Soft_Delete_Base_Class implements EEI_Line_Item_Object, EEI_Event_Relation, EEI_Has_Icon
15 15
 {
16 16
 
17
-    /**
18
-     * The following constants are used by the ticket_status() method to indicate whether a ticket is on sale or not.
19
-     */
20
-    const sold_out = 'TKS';
21
-
22
-    /**
23
-     *
24
-     */
25
-    const expired = 'TKE';
26
-
27
-    /**
28
-     *
29
-     */
30
-    const archived = 'TKA';
31
-
32
-    /**
33
-     *
34
-     */
35
-    const pending = 'TKP';
36
-
37
-    /**
38
-     *
39
-     */
40
-    const onsale = 'TKO';
41
-
42
-    /**
43
-     * extra meta key for tracking ticket reservations
44
-     *
45
-     * @type string
46
-     */
47
-    const META_KEY_TICKET_RESERVATIONS = 'ticket_reservations';
48
-
49
-    /**
50
-     * cached result from method of the same name
51
-     *
52
-     * @var float $_ticket_total_with_taxes
53
-     */
54
-    private $_ticket_total_with_taxes;
55
-
56
-
57
-    /**
58
-     * @param array  $props_n_values          incoming values
59
-     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
60
-     *                                        used.)
61
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
62
-     *                                        date_format and the second value is the time format
63
-     * @return EE_Ticket
64
-     * @throws EE_Error
65
-     */
66
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
67
-    {
68
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
69
-        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
70
-    }
71
-
72
-
73
-    /**
74
-     * @param array  $props_n_values  incoming values from the database
75
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
76
-     *                                the website will be used.
77
-     * @return EE_Ticket
78
-     * @throws EE_Error
79
-     */
80
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
81
-    {
82
-        return new self($props_n_values, true, $timezone);
83
-    }
84
-
85
-
86
-    /**
87
-     * @return bool
88
-     * @throws EE_Error
89
-     */
90
-    public function parent()
91
-    {
92
-        return $this->get('TKT_parent');
93
-    }
94
-
95
-
96
-    /**
97
-     * return if a ticket has quantities available for purchase
98
-     *
99
-     * @param  int $DTT_ID the primary key for a particular datetime
100
-     * @return boolean
101
-     * @throws EE_Error
102
-     */
103
-    public function available($DTT_ID = 0)
104
-    {
105
-        // are we checking availability for a particular datetime ?
106
-        if ($DTT_ID) {
107
-            // get that datetime object
108
-            $datetime = $this->get_first_related('Datetime', array(array('DTT_ID' => $DTT_ID)));
109
-            // if  ticket sales for this datetime have exceeded the reg limit...
110
-            if ($datetime instanceof EE_Datetime && $datetime->sold_out()) {
111
-                return false;
112
-            }
113
-        }
114
-        // datetime is still open for registration, but is this ticket sold out ?
115
-        return $this->qty() < 1 || $this->qty() > $this->sold() ? true : false;
116
-    }
117
-
118
-
119
-    /**
120
-     * Using the start date and end date this method calculates whether the ticket is On Sale, Pending, or Expired
121
-     *
122
-     * @param bool        $display   true = we'll return a localized string, otherwise we just return the value of the
123
-     *                               relevant status const
124
-     * @param bool | null $remaining if it is already known that tickets are available, then simply pass a bool to save
125
-     *               further processing
126
-     * @return mixed status int if the display string isn't requested
127
-     * @throws EE_Error
128
-     */
129
-    public function ticket_status($display = false, $remaining = null)
130
-    {
131
-        $remaining = is_bool($remaining) ? $remaining : $this->is_remaining();
132
-        if (! $remaining) {
133
-            return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out;
134
-        }
135
-        if ($this->get('TKT_deleted')) {
136
-            return $display ? EEH_Template::pretty_status(EE_Ticket::archived, false, 'sentence') : EE_Ticket::archived;
137
-        }
138
-        if ($this->is_expired()) {
139
-            return $display ? EEH_Template::pretty_status(EE_Ticket::expired, false, 'sentence') : EE_Ticket::expired;
140
-        }
141
-        if ($this->is_pending()) {
142
-            return $display ? EEH_Template::pretty_status(EE_Ticket::pending, false, 'sentence') : EE_Ticket::pending;
143
-        }
144
-        if ($this->is_on_sale()) {
145
-            return $display ? EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence') : EE_Ticket::onsale;
146
-        }
147
-        return '';
148
-    }
149
-
150
-
151
-    /**
152
-     * The purpose of this method is to simply return a boolean for whether there are any tickets remaining for sale
153
-     * considering ALL the factors used for figuring that out.
154
-     *
155
-     * @access public
156
-     * @param  int $DTT_ID if an int above 0 is included here then we get a specific dtt.
157
-     * @return boolean         true = tickets remaining, false not.
158
-     * @throws EE_Error
159
-     */
160
-    public function is_remaining($DTT_ID = 0)
161
-    {
162
-        $num_remaining = $this->remaining($DTT_ID);
163
-        if ($num_remaining === 0) {
164
-            return false;
165
-        }
166
-        if ($num_remaining > 0 && $num_remaining < $this->min()) {
167
-            return false;
168
-        }
169
-        return true;
170
-    }
171
-
172
-
173
-    /**
174
-     * return the total number of tickets available for purchase
175
-     *
176
-     * @param  int $DTT_ID the primary key for a particular datetime.
177
-     *                     set to 0 for all related datetimes
178
-     * @return int
179
-     * @throws EE_Error
180
-     */
181
-    public function remaining($DTT_ID = 0)
182
-    {
183
-        return $this->real_quantity_on_ticket('saleable', $DTT_ID);
184
-    }
185
-
186
-
187
-    /**
188
-     * Gets min
189
-     *
190
-     * @return int
191
-     * @throws EE_Error
192
-     */
193
-    public function min()
194
-    {
195
-        return $this->get('TKT_min');
196
-    }
197
-
198
-
199
-    /**
200
-     * return if a ticket is no longer available cause its available dates have expired.
201
-     *
202
-     * @return boolean
203
-     * @throws EE_Error
204
-     */
205
-    public function is_expired()
206
-    {
207
-        return ($this->get_raw('TKT_end_date') < time());
208
-    }
209
-
210
-
211
-    /**
212
-     * Return if a ticket is yet to go on sale or not
213
-     *
214
-     * @return boolean
215
-     * @throws EE_Error
216
-     */
217
-    public function is_pending()
218
-    {
219
-        return ($this->get_raw('TKT_start_date') > time());
220
-    }
221
-
222
-
223
-    /**
224
-     * Return if a ticket is on sale or not
225
-     *
226
-     * @return boolean
227
-     * @throws EE_Error
228
-     */
229
-    public function is_on_sale()
230
-    {
231
-        return ($this->get_raw('TKT_start_date') < time() && $this->get_raw('TKT_end_date') > time());
232
-    }
233
-
234
-
235
-    /**
236
-     * This returns the chronologically last datetime that this ticket is associated with
237
-     *
238
-     * @param string $dt_frmt
239
-     * @param string $conjunction - conjunction junction what's your function ? this string joins the start date with
240
-     *                            the end date ie: Jan 01 "to" Dec 31
241
-     * @return string
242
-     * @throws EE_Error
243
-     */
244
-    public function date_range($dt_frmt = '', $conjunction = ' - ')
245
-    {
246
-        $first_date = $this->first_datetime() instanceof EE_Datetime ? $this->first_datetime()->start_date($dt_frmt)
247
-            : '';
248
-        $last_date = $this->last_datetime() instanceof EE_Datetime ? $this->last_datetime()->end_date($dt_frmt) : '';
249
-
250
-        return $first_date && $last_date ? $first_date . $conjunction . $last_date : '';
251
-    }
252
-
253
-
254
-    /**
255
-     * This returns the chronologically first datetime that this ticket is associated with
256
-     *
257
-     * @return EE_Datetime
258
-     * @throws EE_Error
259
-     */
260
-    public function first_datetime()
261
-    {
262
-        $datetimes = $this->datetimes(array('limit' => 1));
263
-        return reset($datetimes);
264
-    }
265
-
266
-
267
-    /**
268
-     * Gets all the datetimes this ticket can be used for attending.
269
-     * Unless otherwise specified, orders datetimes by start date.
270
-     *
271
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
272
-     * @return EE_Datetime[]|EE_Base_Class[]
273
-     * @throws EE_Error
274
-     */
275
-    public function datetimes($query_params = array())
276
-    {
277
-        if (! isset($query_params['order_by'])) {
278
-            $query_params['order_by']['DTT_order'] = 'ASC';
279
-        }
280
-        return $this->get_many_related('Datetime', $query_params);
281
-    }
282
-
283
-
284
-    /**
285
-     * This returns the chronologically last datetime that this ticket is associated with
286
-     *
287
-     * @return EE_Datetime
288
-     * @throws EE_Error
289
-     */
290
-    public function last_datetime()
291
-    {
292
-        $datetimes = $this->datetimes(array('limit' => 1, 'order_by' => array('DTT_EVT_start' => 'DESC')));
293
-        return end($datetimes);
294
-    }
295
-
296
-
297
-    /**
298
-     * This returns the total tickets sold depending on the given parameters.
299
-     *
300
-     * @param  string $what   Can be one of two options: 'ticket', 'datetime'.
301
-     *                        'ticket' = total ticket sales for all datetimes this ticket is related to
302
-     *                        'datetime' = total ticket sales for a specified datetime (required $dtt_id)
303
-     *                        'datetime' = total ticket sales in the datetime_ticket table.
304
-     *                        If $dtt_id is not given then we return an array of sales indexed by datetime.
305
-     *                        If $dtt_id IS given then we return the tickets sold for that given datetime.
306
-     * @param  int    $dtt_id [optional] include the dtt_id with $what = 'datetime'.
307
-     * @return mixed (array|int)          how many tickets have sold
308
-     * @throws EE_Error
309
-     */
310
-    public function tickets_sold($what = 'ticket', $dtt_id = null)
311
-    {
312
-        $total = 0;
313
-        $tickets_sold = $this->_all_tickets_sold();
314
-        switch ($what) {
315
-            case 'ticket':
316
-                return $tickets_sold['ticket'];
317
-                break;
318
-            case 'datetime':
319
-                if (empty($tickets_sold['datetime'])) {
320
-                    return $total;
321
-                }
322
-                if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) {
323
-                    EE_Error::add_error(
324
-                        __(
325
-                            'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included.  Are you SURE that is a datetime related to this ticket?',
326
-                            'event_espresso'
327
-                        ),
328
-                        __FILE__,
329
-                        __FUNCTION__,
330
-                        __LINE__
331
-                    );
332
-                    return $total;
333
-                }
334
-                return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ];
335
-                break;
336
-            default:
337
-                return $total;
338
-        }
339
-    }
340
-
341
-
342
-    /**
343
-     * This returns an array indexed by datetime_id for tickets sold with this ticket.
344
-     *
345
-     * @return EE_Ticket[]
346
-     * @throws EE_Error
347
-     */
348
-    protected function _all_tickets_sold()
349
-    {
350
-        $datetimes = $this->get_many_related('Datetime');
351
-        $tickets_sold = array();
352
-        if (! empty($datetimes)) {
353
-            foreach ($datetimes as $datetime) {
354
-                $tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold');
355
-            }
356
-        }
357
-        // Tickets sold
358
-        $tickets_sold['ticket'] = $this->sold();
359
-        return $tickets_sold;
360
-    }
361
-
362
-
363
-    /**
364
-     * This returns the base price object for the ticket.
365
-     *
366
-     * @param  bool $return_array whether to return as an array indexed by price id or just the object.
367
-     * @return EE_Price|EE_Base_Class|EE_Price[]|EE_Base_Class[]
368
-     * @throws EE_Error
369
-     */
370
-    public function base_price($return_array = false)
371
-    {
372
-        $_where = array('Price_Type.PBT_ID' => EEM_Price_Type::base_type_base_price);
373
-        return $return_array
374
-            ? $this->get_many_related('Price', array($_where))
375
-            : $this->get_first_related('Price', array($_where));
376
-    }
377
-
378
-
379
-    /**
380
-     * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price)
381
-     *
382
-     * @access public
383
-     * @return EE_Price[]
384
-     * @throws EE_Error
385
-     */
386
-    public function price_modifiers()
387
-    {
388
-        $query_params = array(
389
-            0 => array(
390
-                'Price_Type.PBT_ID' => array(
391
-                    'NOT IN',
392
-                    array(EEM_Price_Type::base_type_base_price, EEM_Price_Type::base_type_tax),
393
-                ),
394
-            ),
395
-        );
396
-        return $this->prices($query_params);
397
-    }
398
-
399
-
400
-    /**
401
-     * Gets all the prices that combine to form the final price of this ticket
402
-     *
403
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
404
-     * @return EE_Price[]|EE_Base_Class[]
405
-     * @throws EE_Error
406
-     */
407
-    public function prices($query_params = array())
408
-    {
409
-        return $this->get_many_related('Price', $query_params);
410
-    }
411
-
412
-
413
-    /**
414
-     * Gets all the ticket applicabilities (ie, relations between datetimes and tickets)
415
-     *
416
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
417
-     * @return EE_Datetime_Ticket|EE_Base_Class[]
418
-     * @throws EE_Error
419
-     */
420
-    public function datetime_tickets($query_params = array())
421
-    {
422
-        return $this->get_many_related('Datetime_Ticket', $query_params);
423
-    }
424
-
425
-
426
-    /**
427
-     * Gets all the datetimes from the db ordered by DTT_order
428
-     *
429
-     * @param boolean $show_expired
430
-     * @param boolean $show_deleted
431
-     * @return EE_Datetime[]
432
-     * @throws EE_Error
433
-     */
434
-    public function datetimes_ordered($show_expired = true, $show_deleted = false)
435
-    {
436
-        return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_ticket_ordered_by_DTT_order(
437
-            $this->ID(),
438
-            $show_expired,
439
-            $show_deleted
440
-        );
441
-    }
442
-
443
-
444
-    /**
445
-     * Gets ID
446
-     *
447
-     * @return string
448
-     * @throws EE_Error
449
-     */
450
-    public function ID()
451
-    {
452
-        return $this->get('TKT_ID');
453
-    }
454
-
455
-
456
-    /**
457
-     * get the author of the ticket.
458
-     *
459
-     * @since 4.5.0
460
-     * @return int
461
-     * @throws EE_Error
462
-     */
463
-    public function wp_user()
464
-    {
465
-        return $this->get('TKT_wp_user');
466
-    }
467
-
468
-
469
-    /**
470
-     * Gets the template for the ticket
471
-     *
472
-     * @return EE_Ticket_Template|EE_Base_Class
473
-     * @throws EE_Error
474
-     */
475
-    public function template()
476
-    {
477
-        return $this->get_first_related('Ticket_Template');
478
-    }
479
-
480
-
481
-    /**
482
-     * Simply returns an array of EE_Price objects that are taxes.
483
-     *
484
-     * @return EE_Price[]
485
-     * @throws EE_Error
486
-     */
487
-    public function get_ticket_taxes_for_admin()
488
-    {
489
-        return EE_Taxes::get_taxes_for_admin();
490
-    }
491
-
492
-
493
-    /**
494
-     * @return float
495
-     * @throws EE_Error
496
-     */
497
-    public function ticket_price()
498
-    {
499
-        return $this->get('TKT_price');
500
-    }
501
-
502
-
503
-    /**
504
-     * @return mixed
505
-     * @throws EE_Error
506
-     */
507
-    public function pretty_price()
508
-    {
509
-        return $this->get_pretty('TKT_price');
510
-    }
511
-
512
-
513
-    /**
514
-     * @return bool
515
-     * @throws EE_Error
516
-     */
517
-    public function is_free()
518
-    {
519
-        return $this->get_ticket_total_with_taxes() === (float) 0;
520
-    }
521
-
522
-
523
-    /**
524
-     * get_ticket_total_with_taxes
525
-     *
526
-     * @param bool $no_cache
527
-     * @return float
528
-     * @throws EE_Error
529
-     */
530
-    public function get_ticket_total_with_taxes($no_cache = false)
531
-    {
532
-        if ($this->_ticket_total_with_taxes === null || $no_cache) {
533
-            $this->_ticket_total_with_taxes = $this->get_ticket_subtotal() + $this->get_ticket_taxes_total_for_admin();
534
-        }
535
-        return (float) $this->_ticket_total_with_taxes;
536
-    }
537
-
538
-
539
-    public function ensure_TKT_Price_correct()
540
-    {
541
-        $this->set('TKT_price', EE_Taxes::get_subtotal_for_admin($this));
542
-        $this->save();
543
-    }
544
-
545
-
546
-    /**
547
-     * @return float
548
-     * @throws EE_Error
549
-     */
550
-    public function get_ticket_subtotal()
551
-    {
552
-        return EE_Taxes::get_subtotal_for_admin($this);
553
-    }
554
-
555
-
556
-    /**
557
-     * Returns the total taxes applied to this ticket
558
-     *
559
-     * @return float
560
-     * @throws EE_Error
561
-     */
562
-    public function get_ticket_taxes_total_for_admin()
563
-    {
564
-        return EE_Taxes::get_total_taxes_for_admin($this);
565
-    }
566
-
567
-
568
-    /**
569
-     * Sets name
570
-     *
571
-     * @param string $name
572
-     * @throws EE_Error
573
-     */
574
-    public function set_name($name)
575
-    {
576
-        $this->set('TKT_name', $name);
577
-    }
578
-
579
-
580
-    /**
581
-     * Gets description
582
-     *
583
-     * @return string
584
-     * @throws EE_Error
585
-     */
586
-    public function description()
587
-    {
588
-        return $this->get('TKT_description');
589
-    }
590
-
591
-
592
-    /**
593
-     * Sets description
594
-     *
595
-     * @param string $description
596
-     * @throws EE_Error
597
-     */
598
-    public function set_description($description)
599
-    {
600
-        $this->set('TKT_description', $description);
601
-    }
602
-
603
-
604
-    /**
605
-     * Gets start_date
606
-     *
607
-     * @param string $dt_frmt
608
-     * @param string $tm_frmt
609
-     * @return string
610
-     * @throws EE_Error
611
-     */
612
-    public function start_date($dt_frmt = '', $tm_frmt = '')
613
-    {
614
-        return $this->_get_datetime('TKT_start_date', $dt_frmt, $tm_frmt);
615
-    }
616
-
617
-
618
-    /**
619
-     * Sets start_date
620
-     *
621
-     * @param string $start_date
622
-     * @return void
623
-     * @throws EE_Error
624
-     */
625
-    public function set_start_date($start_date)
626
-    {
627
-        $this->_set_date_time('B', $start_date, 'TKT_start_date');
628
-    }
629
-
630
-
631
-    /**
632
-     * Gets end_date
633
-     *
634
-     * @param string $dt_frmt
635
-     * @param string $tm_frmt
636
-     * @return string
637
-     * @throws EE_Error
638
-     */
639
-    public function end_date($dt_frmt = '', $tm_frmt = '')
640
-    {
641
-        return $this->_get_datetime('TKT_end_date', $dt_frmt, $tm_frmt);
642
-    }
643
-
644
-
645
-    /**
646
-     * Sets end_date
647
-     *
648
-     * @param string $end_date
649
-     * @return void
650
-     * @throws EE_Error
651
-     */
652
-    public function set_end_date($end_date)
653
-    {
654
-        $this->_set_date_time('B', $end_date, 'TKT_end_date');
655
-    }
656
-
657
-
658
-    /**
659
-     * Sets sell until time
660
-     *
661
-     * @since 4.5.0
662
-     * @param string $time a string representation of the sell until time (ex 9am or 7:30pm)
663
-     * @throws EE_Error
664
-     */
665
-    public function set_end_time($time)
666
-    {
667
-        $this->_set_time_for($time, 'TKT_end_date');
668
-    }
669
-
670
-
671
-    /**
672
-     * Sets min
673
-     *
674
-     * @param int $min
675
-     * @return void
676
-     * @throws EE_Error
677
-     */
678
-    public function set_min($min)
679
-    {
680
-        $this->set('TKT_min', $min);
681
-    }
682
-
683
-
684
-    /**
685
-     * Gets max
686
-     *
687
-     * @return int
688
-     * @throws EE_Error
689
-     */
690
-    public function max()
691
-    {
692
-        return $this->get('TKT_max');
693
-    }
694
-
695
-
696
-    /**
697
-     * Sets max
698
-     *
699
-     * @param int $max
700
-     * @return void
701
-     * @throws EE_Error
702
-     */
703
-    public function set_max($max)
704
-    {
705
-        $this->set('TKT_max', $max);
706
-    }
707
-
708
-
709
-    /**
710
-     * Sets price
711
-     *
712
-     * @param float $price
713
-     * @return void
714
-     * @throws EE_Error
715
-     */
716
-    public function set_price($price)
717
-    {
718
-        $this->set('TKT_price', $price);
719
-    }
720
-
721
-
722
-    /**
723
-     * Gets sold
724
-     *
725
-     * @return int
726
-     * @throws EE_Error
727
-     */
728
-    public function sold()
729
-    {
730
-        return $this->get_raw('TKT_sold');
731
-    }
732
-
733
-
734
-    /**
735
-     * Sets sold
736
-     *
737
-     * @param int $sold
738
-     * @return void
739
-     * @throws EE_Error
740
-     */
741
-    public function set_sold($sold)
742
-    {
743
-        // sold can not go below zero
744
-        $sold = max(0, $sold);
745
-        $this->set('TKT_sold', $sold);
746
-    }
747
-
748
-
749
-    /**
750
-     * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
751
-     * associated datetimes.
752
-     *
753
-     * @since $VID:$
754
-     * @param int $qty
755
-     * @return boolean
756
-     * @throws EE_Error
757
-     * @throws InvalidArgumentException
758
-     * @throws InvalidDataTypeException
759
-     * @throws InvalidInterfaceException
760
-     * @throws ReflectionException
761
-     */
762
-    public function increaseSold($qty = 1)
763
-    {
764
-        $qty = absint($qty);
765
-        // increment sold and decrement reserved datetime quantities simultaneously
766
-        // don't worry about failures, because they must have already had a spot reserved
767
-        $this->increaseSoldForDatetimes($qty);
768
-        // Increment and decrement ticket quantities simultaneously
769
-        $success = $this->adjustNumericFieldsInDb(
770
-            [
771
-                'TKT_reserved' => $qty * -1,
772
-                'TKT_sold' => $qty
773
-            ]
774
-        );
775
-        do_action(
776
-            'AHEE__EE_Ticket__increase_sold',
777
-            $this,
778
-            $qty,
779
-            $this->sold(),
780
-            $success
781
-        );
782
-        return $success;
783
-    }
784
-
785
-    /**
786
-     * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
787
-     *
788
-     * @since $VID:$
789
-     * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts),
790
-     *             Negative means to decreases old counts (and increase reserved counts).
791
-     * @param EE_Datetime[] $datetimes
792
-     * @throws EE_Error
793
-     * @throws InvalidArgumentException
794
-     * @throws InvalidDataTypeException
795
-     * @throws InvalidInterfaceException
796
-     * @throws ReflectionException
797
-     */
798
-    protected function increaseSoldForDatetimes($qty, array $datetimes = [])
799
-    {
800
-        $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
801
-        foreach ($datetimes as $datetime) {
802
-            $datetime->increaseSold($qty);
803
-        }
804
-    }
805
-
806
-
807
-
808
-    /**
809
-     * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
810
-     * DB and then updates the model objects.
811
-     * Does not affect the reserved counts.
812
-     *
813
-     * @since $VID:$
814
-     * @param int $qty
815
-     * @return boolean
816
-     * @throws EE_Error
817
-     * @throws InvalidArgumentException
818
-     * @throws InvalidDataTypeException
819
-     * @throws InvalidInterfaceException
820
-     * @throws ReflectionException
821
-     */
822
-    public function decreaseSold($qty = 1)
823
-    {
824
-        $qty = absint($qty);
825
-        $this->decreaseSoldForDatetimes($qty);
826
-        $success = $this->adjustNumericFieldsInDb(
827
-            [
828
-                'TKT_sold' => $qty * -1
829
-            ]
830
-        );
831
-        do_action(
832
-            'AHEE__EE_Ticket__decrease_sold',
833
-            $this,
834
-            $qty,
835
-            $this->sold(),
836
-            $success
837
-        );
838
-        return $success;
839
-    }
840
-
841
-
842
-    /**
843
-     * Decreases sold on related datetimes
844
-     *
845
-     * @since $VID:$
846
-     * @param int $qty
847
-     * @param EE_Datetime[] $datetimes
848
-     * @return void
849
-     * @throws EE_Error
850
-     * @throws InvalidArgumentException
851
-     * @throws InvalidDataTypeException
852
-     * @throws InvalidInterfaceException
853
-     * @throws ReflectionException
854
-     */
855
-    protected function decreaseSoldForDatetimes($qty = 1, array $datetimes = [])
856
-    {
857
-        $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
858
-        if (is_array($datetimes)) {
859
-            foreach ($datetimes as $datetime) {
860
-                if ($datetime instanceof EE_Datetime) {
861
-                    $datetime->decreaseSold($qty);
862
-                }
863
-            }
864
-        }
865
-    }
866
-
867
-
868
-    /**
869
-     * Gets qty of reserved tickets
870
-     *
871
-     * @return int
872
-     * @throws EE_Error
873
-     */
874
-    public function reserved()
875
-    {
876
-        return $this->get_raw('TKT_reserved');
877
-    }
878
-
879
-
880
-    /**
881
-     * Sets reserved
882
-     *
883
-     * @param int $reserved
884
-     * @return void
885
-     * @throws EE_Error
886
-     */
887
-    public function set_reserved($reserved)
888
-    {
889
-        // reserved can not go below zero
890
-        $reserved = max(0, (int) $reserved);
891
-        $this->set('TKT_reserved', $reserved);
892
-    }
893
-
894
-
895
-    /**
896
-     * Increments reserved by amount passed by $qty, and persists it immediately to the database.
897
-     *
898
-     * @since $VID:$
899
-     * @param int    $qty
900
-     * @param string $source
901
-     * @return bool whether we successfully reserved the ticket or not.
902
-     * @throws EE_Error
903
-     * @throws InvalidArgumentException
904
-     * @throws ReflectionException
905
-     * @throws InvalidDataTypeException
906
-     * @throws InvalidInterfaceException
907
-     */
908
-    public function increaseReserved($qty = 1, $source = 'unknown')
909
-    {
910
-        $qty = absint($qty);
911
-        do_action(
912
-            'AHEE__EE_Ticket__increase_reserved__begin',
913
-            $this,
914
-            $qty,
915
-            $source
916
-        );
917
-        $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "{$qty} from {$source}");
918
-        $success = false;
919
-        $datetimes_adjusted_successfully = $this->increaseReservedForDatetimes($qty);
920
-        if ($datetimes_adjusted_successfully) {
921
-            $success = $this->incrementFieldConditionallyInDb(
922
-                'TKT_reserved',
923
-                'TKT_sold',
924
-                'TKT_qty',
925
-                $qty
926
-            );
927
-            if (! $success) {
928
-                // The datetimes were successfully bumped, but not the
929
-                // ticket. So we need to manually rollback the datetimes.
930
-                $this->decreaseReservedForDatetimes($qty);
931
-            }
932
-        }
933
-        do_action(
934
-            'AHEE__EE_Ticket__increase_reserved',
935
-            $this,
936
-            $qty,
937
-            $this->reserved(),
938
-            $success
939
-        );
940
-        return $success;
941
-    }
942
-
943
-
944
-    /**
945
-     * Increases reserved counts on related datetimes
946
-     *
947
-     * @since $VID:$
948
-     * @param int $qty
949
-     * @param EE_Datetime[] $datetimes
950
-     * @return boolean indicating success
951
-     * @throws EE_Error
952
-     * @throws InvalidArgumentException
953
-     * @throws InvalidDataTypeException
954
-     * @throws InvalidInterfaceException
955
-     * @throws ReflectionException
956
-     */
957
-    protected function increaseReservedForDatetimes($qty = 1, array $datetimes = [])
958
-    {
959
-        $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
960
-        $datetimes_updated = [];
961
-        $limit_exceeded = false;
962
-        if (is_array($datetimes)) {
963
-            foreach ($datetimes as $datetime) {
964
-                if ($datetime instanceof EE_Datetime) {
965
-                    if ($datetime->increaseReserved($qty)) {
966
-                        $datetimes_updated[] = $datetime;
967
-                    } else {
968
-                        $limit_exceeded = true;
969
-                        break;
970
-                    }
971
-                }
972
-            }
973
-            // If somewhere along the way we detected a datetime whose
974
-            // limit was exceeded, do a manual rollback.
975
-            if ($limit_exceeded) {
976
-                $this->decreaseReservedForDatetimes($qty, $datetimes_updated);
977
-                return false;
978
-            }
979
-        }
980
-        return true;
981
-    }
982
-
983
-
984
-    /**
985
-     * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
986
-     *
987
-     * @since $VID:$
988
-     * @param int    $qty
989
-     * @param bool   $adjust_datetimes
990
-     * @param string $source
991
-     * @return boolean
992
-     * @throws EE_Error
993
-     * @throws InvalidArgumentException
994
-     * @throws ReflectionException
995
-     * @throws InvalidDataTypeException
996
-     * @throws InvalidInterfaceException
997
-     */
998
-    public function decreaseReserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
999
-    {
1000
-        $qty = absint($qty);
1001
-        $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "-{$qty} from {$source}");
1002
-        if ($adjust_datetimes) {
1003
-            $this->decreaseReservedForDatetimes($qty);
1004
-        }
1005
-        $success = $this->adjustNumericFieldsInDb(
1006
-            [
1007
-                'TKT_reserved' => $qty * -1
1008
-            ]
1009
-        );
1010
-        do_action(
1011
-            'AHEE__EE_Ticket__decrease_reserved',
1012
-            $this,
1013
-            $qty,
1014
-            $this->reserved(),
1015
-            $success
1016
-        );
1017
-        return $success;
1018
-    }
1019
-
1020
-
1021
-    /**
1022
-     * Decreases the reserved count on the specified datetimes.
1023
-     *
1024
-     * @since $VID:$
1025
-     * @param int           $qty
1026
-     * @param EE_Datetime[] $datetimes
1027
-     * @throws EE_Error
1028
-     * @throws InvalidArgumentException
1029
-     * @throws ReflectionException
1030
-     * @throws InvalidDataTypeException
1031
-     * @throws InvalidInterfaceException
1032
-     */
1033
-    protected function decreaseReservedForDatetimes($qty = 1, array $datetimes = [])
1034
-    {
1035
-        $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
1036
-        foreach ($datetimes as $datetime) {
1037
-            if ($datetime instanceof EE_Datetime) {
1038
-                $datetime->decreaseReserved($qty);
1039
-            }
1040
-        }
1041
-    }
1042
-
1043
-
1044
-    /**
1045
-     * Gets ticket quantity
1046
-     *
1047
-     * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1048
-     *                            therefore $context can be one of three values: '', 'reg_limit', or 'saleable'
1049
-     *                            '' (default) quantity is the actual db value for TKT_qty, unaffected by other objects
1050
-     *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1051
-     *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1052
-     *                            is therefore the truest measure of tickets that can be purchased at the moment
1053
-     * @return int
1054
-     * @throws EE_Error
1055
-     */
1056
-    public function qty($context = '')
1057
-    {
1058
-        switch ($context) {
1059
-            case 'reg_limit':
1060
-                return $this->real_quantity_on_ticket();
1061
-            case 'saleable':
1062
-                return $this->real_quantity_on_ticket('saleable');
1063
-            default:
1064
-                return $this->get_raw('TKT_qty');
1065
-        }
1066
-    }
1067
-
1068
-
1069
-    /**
1070
-     * Gets ticket quantity
1071
-     *
1072
-     * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1073
-     *                            therefore $context can be one of two values: 'reg_limit', or 'saleable'
1074
-     *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1075
-     *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1076
-     *                            is therefore the truest measure of tickets that can be purchased at the moment
1077
-     * @param  int   $DTT_ID      the primary key for a particular datetime.
1078
-     *                            set to 0 for all related datetimes
1079
-     * @return int
1080
-     * @throws EE_Error
1081
-     */
1082
-    public function real_quantity_on_ticket($context = 'reg_limit', $DTT_ID = 0)
1083
-    {
1084
-        $raw = $this->get_raw('TKT_qty');
1085
-        // return immediately if it's zero
1086
-        if ($raw === 0) {
1087
-            return $raw;
1088
-        }
1089
-        // echo "\n\n<br />Ticket: " . $this->name() . '<br />';
1090
-        // ensure qty doesn't exceed raw value for THIS ticket
1091
-        $qty = min(EE_INF, $raw);
1092
-        // echo "\n . qty: " . $qty . '<br />';
1093
-        // calculate this ticket's total sales and reservations
1094
-        $sold_and_reserved_for_this_ticket = $this->sold() + $this->reserved();
1095
-        // echo "\n . sold: " . $this->sold() . '<br />';
1096
-        // echo "\n . reserved: " . $this->reserved() . '<br />';
1097
-        // echo "\n . sold_and_reserved_for_this_ticket: " . $sold_and_reserved_for_this_ticket . '<br />';
1098
-        // first we need to calculate the maximum number of tickets available for the datetime
1099
-        // do we want data for one datetime or all of them ?
1100
-        $query_params = $DTT_ID ? array(array('DTT_ID' => $DTT_ID)) : array();
1101
-        $datetimes = $this->datetimes($query_params);
1102
-        if (is_array($datetimes) && ! empty($datetimes)) {
1103
-            foreach ($datetimes as $datetime) {
1104
-                if ($datetime instanceof EE_Datetime) {
1105
-                    $datetime->refresh_from_db();
1106
-                    // echo "\n . . datetime name: " . $datetime->name() . '<br />';
1107
-                    // echo "\n . . datetime ID: " . $datetime->ID() . '<br />';
1108
-                    // initialize with no restrictions for each datetime
1109
-                    // but adjust datetime qty based on datetime reg limit
1110
-                    $datetime_qty = min(EE_INF, $datetime->reg_limit());
1111
-                    // echo "\n . . . datetime reg_limit: " . $datetime->reg_limit() . '<br />';
1112
-                    // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1113
-                    // if we want the actual saleable amount, then we need to consider OTHER ticket sales
1114
-                    // and reservations for this datetime, that do NOT include sales and reservations
1115
-                    // for this ticket (so we add $this->sold() and $this->reserved() back in)
1116
-                    if ($context === 'saleable') {
1117
-                        $datetime_qty = max(
1118
-                            $datetime_qty - $datetime->sold_and_reserved() + $sold_and_reserved_for_this_ticket,
1119
-                            0
1120
-                        );
1121
-                        // echo "\n . . . datetime sold: " . $datetime->sold() . '<br />';
1122
-                        // echo "\n . . . datetime reserved: " . $datetime->reserved() . '<br />';
1123
-                        // echo "\n . . . datetime sold_and_reserved: " . $datetime->sold_and_reserved() . '<br />';
1124
-                        // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1125
-                        $datetime_qty = ! $datetime->sold_out() ? $datetime_qty : 0;
1126
-                        // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1127
-                    }
1128
-                    $qty = min($datetime_qty, $qty);
1129
-                    // echo "\n . . qty: " . $qty . '<br />';
1130
-                }
1131
-            }
1132
-        }
1133
-        // NOW that we know the  maximum number of tickets available for the datetime
1134
-        // we can finally factor in the details for this specific ticket
1135
-        if ($qty > 0 && $context === 'saleable') {
1136
-            // and subtract the sales for THIS ticket
1137
-            $qty = max($qty - $sold_and_reserved_for_this_ticket, 0);
1138
-            // echo "\n . qty: " . $qty . '<br />';
1139
-        }
1140
-        // echo "\nFINAL QTY: " . $qty . "<br /><br />";
1141
-        return $qty;
1142
-    }
1143
-
1144
-
1145
-    /**
1146
-     * Sets qty - IMPORTANT!!! Does NOT allow QTY to be set higher than the lowest reg limit of any related datetimes
1147
-     *
1148
-     * @param int $qty
1149
-     * @return void
1150
-     * @throws EE_Error
1151
-     */
1152
-    public function set_qty($qty)
1153
-    {
1154
-        $datetimes = $this->datetimes();
1155
-        foreach ($datetimes as $datetime) {
1156
-            if ($datetime instanceof EE_Datetime) {
1157
-                $qty = min($qty, $datetime->reg_limit());
1158
-            }
1159
-        }
1160
-        $this->set('TKT_qty', $qty);
1161
-    }
1162
-
1163
-
1164
-    /**
1165
-     * Gets uses
1166
-     *
1167
-     * @return int
1168
-     * @throws EE_Error
1169
-     */
1170
-    public function uses()
1171
-    {
1172
-        return $this->get('TKT_uses');
1173
-    }
1174
-
1175
-
1176
-    /**
1177
-     * Sets uses
1178
-     *
1179
-     * @param int $uses
1180
-     * @return void
1181
-     * @throws EE_Error
1182
-     */
1183
-    public function set_uses($uses)
1184
-    {
1185
-        $this->set('TKT_uses', $uses);
1186
-    }
1187
-
1188
-
1189
-    /**
1190
-     * returns whether ticket is required or not.
1191
-     *
1192
-     * @return boolean
1193
-     * @throws EE_Error
1194
-     */
1195
-    public function required()
1196
-    {
1197
-        return $this->get('TKT_required');
1198
-    }
1199
-
1200
-
1201
-    /**
1202
-     * sets the TKT_required property
1203
-     *
1204
-     * @param boolean $required
1205
-     * @return void
1206
-     * @throws EE_Error
1207
-     */
1208
-    public function set_required($required)
1209
-    {
1210
-        $this->set('TKT_required', $required);
1211
-    }
1212
-
1213
-
1214
-    /**
1215
-     * Gets taxable
1216
-     *
1217
-     * @return boolean
1218
-     * @throws EE_Error
1219
-     */
1220
-    public function taxable()
1221
-    {
1222
-        return $this->get('TKT_taxable');
1223
-    }
1224
-
1225
-
1226
-    /**
1227
-     * Sets taxable
1228
-     *
1229
-     * @param boolean $taxable
1230
-     * @return void
1231
-     * @throws EE_Error
1232
-     */
1233
-    public function set_taxable($taxable)
1234
-    {
1235
-        $this->set('TKT_taxable', $taxable);
1236
-    }
1237
-
1238
-
1239
-    /**
1240
-     * Gets is_default
1241
-     *
1242
-     * @return boolean
1243
-     * @throws EE_Error
1244
-     */
1245
-    public function is_default()
1246
-    {
1247
-        return $this->get('TKT_is_default');
1248
-    }
1249
-
1250
-
1251
-    /**
1252
-     * Sets is_default
1253
-     *
1254
-     * @param boolean $is_default
1255
-     * @return void
1256
-     * @throws EE_Error
1257
-     */
1258
-    public function set_is_default($is_default)
1259
-    {
1260
-        $this->set('TKT_is_default', $is_default);
1261
-    }
1262
-
1263
-
1264
-    /**
1265
-     * Gets order
1266
-     *
1267
-     * @return int
1268
-     * @throws EE_Error
1269
-     */
1270
-    public function order()
1271
-    {
1272
-        return $this->get('TKT_order');
1273
-    }
1274
-
1275
-
1276
-    /**
1277
-     * Sets order
1278
-     *
1279
-     * @param int $order
1280
-     * @return void
1281
-     * @throws EE_Error
1282
-     */
1283
-    public function set_order($order)
1284
-    {
1285
-        $this->set('TKT_order', $order);
1286
-    }
1287
-
1288
-
1289
-    /**
1290
-     * Gets row
1291
-     *
1292
-     * @return int
1293
-     * @throws EE_Error
1294
-     */
1295
-    public function row()
1296
-    {
1297
-        return $this->get('TKT_row');
1298
-    }
1299
-
1300
-
1301
-    /**
1302
-     * Sets row
1303
-     *
1304
-     * @param int $row
1305
-     * @return void
1306
-     * @throws EE_Error
1307
-     */
1308
-    public function set_row($row)
1309
-    {
1310
-        $this->set('TKT_row', $row);
1311
-    }
1312
-
1313
-
1314
-    /**
1315
-     * Gets deleted
1316
-     *
1317
-     * @return boolean
1318
-     * @throws EE_Error
1319
-     */
1320
-    public function deleted()
1321
-    {
1322
-        return $this->get('TKT_deleted');
1323
-    }
1324
-
1325
-
1326
-    /**
1327
-     * Sets deleted
1328
-     *
1329
-     * @param boolean $deleted
1330
-     * @return void
1331
-     * @throws EE_Error
1332
-     */
1333
-    public function set_deleted($deleted)
1334
-    {
1335
-        $this->set('TKT_deleted', $deleted);
1336
-    }
1337
-
1338
-
1339
-    /**
1340
-     * Gets parent
1341
-     *
1342
-     * @return int
1343
-     * @throws EE_Error
1344
-     */
1345
-    public function parent_ID()
1346
-    {
1347
-        return $this->get('TKT_parent');
1348
-    }
1349
-
1350
-
1351
-    /**
1352
-     * Sets parent
1353
-     *
1354
-     * @param int $parent
1355
-     * @return void
1356
-     * @throws EE_Error
1357
-     */
1358
-    public function set_parent_ID($parent)
1359
-    {
1360
-        $this->set('TKT_parent', $parent);
1361
-    }
1362
-
1363
-
1364
-    /**
1365
-     * Gets a string which is handy for showing in gateways etc that describes the ticket.
1366
-     *
1367
-     * @return string
1368
-     * @throws EE_Error
1369
-     */
1370
-    public function name_and_info()
1371
-    {
1372
-        $times = array();
1373
-        foreach ($this->datetimes() as $datetime) {
1374
-            $times[] = $datetime->start_date_and_time();
1375
-        }
1376
-        return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price();
1377
-    }
1378
-
1379
-
1380
-    /**
1381
-     * Gets name
1382
-     *
1383
-     * @return string
1384
-     * @throws EE_Error
1385
-     */
1386
-    public function name()
1387
-    {
1388
-        return $this->get('TKT_name');
1389
-    }
1390
-
1391
-
1392
-    /**
1393
-     * Gets price
1394
-     *
1395
-     * @return float
1396
-     * @throws EE_Error
1397
-     */
1398
-    public function price()
1399
-    {
1400
-        return $this->get('TKT_price');
1401
-    }
1402
-
1403
-
1404
-    /**
1405
-     * Gets all the registrations for this ticket
1406
-     *
1407
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1408
-     * @return EE_Registration[]|EE_Base_Class[]
1409
-     * @throws EE_Error
1410
-     */
1411
-    public function registrations($query_params = array())
1412
-    {
1413
-        return $this->get_many_related('Registration', $query_params);
1414
-    }
1415
-
1416
-
1417
-    /**
1418
-     * Updates the TKT_sold attribute (and saves) based on the number of APPROVED registrations for this ticket.
1419
-     *
1420
-     * @return int
1421
-     * @throws EE_Error
1422
-     */
1423
-    public function update_tickets_sold()
1424
-    {
1425
-        $count_regs_for_this_ticket = $this->count_registrations(
1426
-            array(
1427
-                array(
1428
-                    'STS_ID'      => EEM_Registration::status_id_approved,
1429
-                    'REG_deleted' => 0,
1430
-                ),
1431
-            )
1432
-        );
1433
-        $this->set_sold($count_regs_for_this_ticket);
1434
-        $this->save();
1435
-        return $count_regs_for_this_ticket;
1436
-    }
1437
-
1438
-
1439
-    /**
1440
-     * Counts the registrations for this ticket
1441
-     *
1442
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1443
-     * @return int
1444
-     */
1445
-    public function count_registrations($query_params = array())
1446
-    {
1447
-        return $this->count_related('Registration', $query_params);
1448
-    }
1449
-
1450
-
1451
-    /**
1452
-     * Implementation for EEI_Has_Icon interface method.
1453
-     *
1454
-     * @see EEI_Visual_Representation for comments
1455
-     * @return string
1456
-     */
1457
-    public function get_icon()
1458
-    {
1459
-        return '<span class="dashicons dashicons-tickets-alt"></span>';
1460
-    }
1461
-
1462
-
1463
-    /**
1464
-     * Implementation of the EEI_Event_Relation interface method
1465
-     *
1466
-     * @see EEI_Event_Relation for comments
1467
-     * @return EE_Event
1468
-     * @throws EE_Error
1469
-     * @throws UnexpectedEntityException
1470
-     */
1471
-    public function get_related_event()
1472
-    {
1473
-        // get one datetime to use for getting the event
1474
-        $datetime = $this->first_datetime();
1475
-        if (! $datetime instanceof \EE_Datetime) {
1476
-            throw new UnexpectedEntityException(
1477
-                $datetime,
1478
-                'EE_Datetime',
1479
-                sprintf(
1480
-                    __('The ticket (%s) is not associated with any valid datetimes.', 'event_espresso'),
1481
-                    $this->name()
1482
-                )
1483
-            );
1484
-        }
1485
-        $event = $datetime->event();
1486
-        if (! $event instanceof \EE_Event) {
1487
-            throw new UnexpectedEntityException(
1488
-                $event,
1489
-                'EE_Event',
1490
-                sprintf(
1491
-                    __('The ticket (%s) is not associated with a valid event.', 'event_espresso'),
1492
-                    $this->name()
1493
-                )
1494
-            );
1495
-        }
1496
-        return $event;
1497
-    }
1498
-
1499
-
1500
-    /**
1501
-     * Implementation of the EEI_Event_Relation interface method
1502
-     *
1503
-     * @see EEI_Event_Relation for comments
1504
-     * @return string
1505
-     * @throws UnexpectedEntityException
1506
-     * @throws EE_Error
1507
-     */
1508
-    public function get_event_name()
1509
-    {
1510
-        $event = $this->get_related_event();
1511
-        return $event instanceof EE_Event ? $event->name() : '';
1512
-    }
1513
-
1514
-
1515
-    /**
1516
-     * Implementation of the EEI_Event_Relation interface method
1517
-     *
1518
-     * @see EEI_Event_Relation for comments
1519
-     * @return int
1520
-     * @throws UnexpectedEntityException
1521
-     * @throws EE_Error
1522
-     */
1523
-    public function get_event_ID()
1524
-    {
1525
-        $event = $this->get_related_event();
1526
-        return $event instanceof EE_Event ? $event->ID() : 0;
1527
-    }
1528
-
1529
-
1530
-    /**
1531
-     * This simply returns whether a ticket can be permanently deleted or not.
1532
-     * The criteria for determining this is whether the ticket has any related registrations.
1533
-     * If there are none then it can be permanently deleted.
1534
-     *
1535
-     * @return bool
1536
-     */
1537
-    public function is_permanently_deleteable()
1538
-    {
1539
-        return $this->count_registrations() === 0;
1540
-    }
1541
-
1542
-
1543
-    /*******************************************************************
17
+	/**
18
+	 * The following constants are used by the ticket_status() method to indicate whether a ticket is on sale or not.
19
+	 */
20
+	const sold_out = 'TKS';
21
+
22
+	/**
23
+	 *
24
+	 */
25
+	const expired = 'TKE';
26
+
27
+	/**
28
+	 *
29
+	 */
30
+	const archived = 'TKA';
31
+
32
+	/**
33
+	 *
34
+	 */
35
+	const pending = 'TKP';
36
+
37
+	/**
38
+	 *
39
+	 */
40
+	const onsale = 'TKO';
41
+
42
+	/**
43
+	 * extra meta key for tracking ticket reservations
44
+	 *
45
+	 * @type string
46
+	 */
47
+	const META_KEY_TICKET_RESERVATIONS = 'ticket_reservations';
48
+
49
+	/**
50
+	 * cached result from method of the same name
51
+	 *
52
+	 * @var float $_ticket_total_with_taxes
53
+	 */
54
+	private $_ticket_total_with_taxes;
55
+
56
+
57
+	/**
58
+	 * @param array  $props_n_values          incoming values
59
+	 * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
60
+	 *                                        used.)
61
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
62
+	 *                                        date_format and the second value is the time format
63
+	 * @return EE_Ticket
64
+	 * @throws EE_Error
65
+	 */
66
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
67
+	{
68
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
69
+		return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
70
+	}
71
+
72
+
73
+	/**
74
+	 * @param array  $props_n_values  incoming values from the database
75
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
76
+	 *                                the website will be used.
77
+	 * @return EE_Ticket
78
+	 * @throws EE_Error
79
+	 */
80
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
81
+	{
82
+		return new self($props_n_values, true, $timezone);
83
+	}
84
+
85
+
86
+	/**
87
+	 * @return bool
88
+	 * @throws EE_Error
89
+	 */
90
+	public function parent()
91
+	{
92
+		return $this->get('TKT_parent');
93
+	}
94
+
95
+
96
+	/**
97
+	 * return if a ticket has quantities available for purchase
98
+	 *
99
+	 * @param  int $DTT_ID the primary key for a particular datetime
100
+	 * @return boolean
101
+	 * @throws EE_Error
102
+	 */
103
+	public function available($DTT_ID = 0)
104
+	{
105
+		// are we checking availability for a particular datetime ?
106
+		if ($DTT_ID) {
107
+			// get that datetime object
108
+			$datetime = $this->get_first_related('Datetime', array(array('DTT_ID' => $DTT_ID)));
109
+			// if  ticket sales for this datetime have exceeded the reg limit...
110
+			if ($datetime instanceof EE_Datetime && $datetime->sold_out()) {
111
+				return false;
112
+			}
113
+		}
114
+		// datetime is still open for registration, but is this ticket sold out ?
115
+		return $this->qty() < 1 || $this->qty() > $this->sold() ? true : false;
116
+	}
117
+
118
+
119
+	/**
120
+	 * Using the start date and end date this method calculates whether the ticket is On Sale, Pending, or Expired
121
+	 *
122
+	 * @param bool        $display   true = we'll return a localized string, otherwise we just return the value of the
123
+	 *                               relevant status const
124
+	 * @param bool | null $remaining if it is already known that tickets are available, then simply pass a bool to save
125
+	 *               further processing
126
+	 * @return mixed status int if the display string isn't requested
127
+	 * @throws EE_Error
128
+	 */
129
+	public function ticket_status($display = false, $remaining = null)
130
+	{
131
+		$remaining = is_bool($remaining) ? $remaining : $this->is_remaining();
132
+		if (! $remaining) {
133
+			return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out;
134
+		}
135
+		if ($this->get('TKT_deleted')) {
136
+			return $display ? EEH_Template::pretty_status(EE_Ticket::archived, false, 'sentence') : EE_Ticket::archived;
137
+		}
138
+		if ($this->is_expired()) {
139
+			return $display ? EEH_Template::pretty_status(EE_Ticket::expired, false, 'sentence') : EE_Ticket::expired;
140
+		}
141
+		if ($this->is_pending()) {
142
+			return $display ? EEH_Template::pretty_status(EE_Ticket::pending, false, 'sentence') : EE_Ticket::pending;
143
+		}
144
+		if ($this->is_on_sale()) {
145
+			return $display ? EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence') : EE_Ticket::onsale;
146
+		}
147
+		return '';
148
+	}
149
+
150
+
151
+	/**
152
+	 * The purpose of this method is to simply return a boolean for whether there are any tickets remaining for sale
153
+	 * considering ALL the factors used for figuring that out.
154
+	 *
155
+	 * @access public
156
+	 * @param  int $DTT_ID if an int above 0 is included here then we get a specific dtt.
157
+	 * @return boolean         true = tickets remaining, false not.
158
+	 * @throws EE_Error
159
+	 */
160
+	public function is_remaining($DTT_ID = 0)
161
+	{
162
+		$num_remaining = $this->remaining($DTT_ID);
163
+		if ($num_remaining === 0) {
164
+			return false;
165
+		}
166
+		if ($num_remaining > 0 && $num_remaining < $this->min()) {
167
+			return false;
168
+		}
169
+		return true;
170
+	}
171
+
172
+
173
+	/**
174
+	 * return the total number of tickets available for purchase
175
+	 *
176
+	 * @param  int $DTT_ID the primary key for a particular datetime.
177
+	 *                     set to 0 for all related datetimes
178
+	 * @return int
179
+	 * @throws EE_Error
180
+	 */
181
+	public function remaining($DTT_ID = 0)
182
+	{
183
+		return $this->real_quantity_on_ticket('saleable', $DTT_ID);
184
+	}
185
+
186
+
187
+	/**
188
+	 * Gets min
189
+	 *
190
+	 * @return int
191
+	 * @throws EE_Error
192
+	 */
193
+	public function min()
194
+	{
195
+		return $this->get('TKT_min');
196
+	}
197
+
198
+
199
+	/**
200
+	 * return if a ticket is no longer available cause its available dates have expired.
201
+	 *
202
+	 * @return boolean
203
+	 * @throws EE_Error
204
+	 */
205
+	public function is_expired()
206
+	{
207
+		return ($this->get_raw('TKT_end_date') < time());
208
+	}
209
+
210
+
211
+	/**
212
+	 * Return if a ticket is yet to go on sale or not
213
+	 *
214
+	 * @return boolean
215
+	 * @throws EE_Error
216
+	 */
217
+	public function is_pending()
218
+	{
219
+		return ($this->get_raw('TKT_start_date') > time());
220
+	}
221
+
222
+
223
+	/**
224
+	 * Return if a ticket is on sale or not
225
+	 *
226
+	 * @return boolean
227
+	 * @throws EE_Error
228
+	 */
229
+	public function is_on_sale()
230
+	{
231
+		return ($this->get_raw('TKT_start_date') < time() && $this->get_raw('TKT_end_date') > time());
232
+	}
233
+
234
+
235
+	/**
236
+	 * This returns the chronologically last datetime that this ticket is associated with
237
+	 *
238
+	 * @param string $dt_frmt
239
+	 * @param string $conjunction - conjunction junction what's your function ? this string joins the start date with
240
+	 *                            the end date ie: Jan 01 "to" Dec 31
241
+	 * @return string
242
+	 * @throws EE_Error
243
+	 */
244
+	public function date_range($dt_frmt = '', $conjunction = ' - ')
245
+	{
246
+		$first_date = $this->first_datetime() instanceof EE_Datetime ? $this->first_datetime()->start_date($dt_frmt)
247
+			: '';
248
+		$last_date = $this->last_datetime() instanceof EE_Datetime ? $this->last_datetime()->end_date($dt_frmt) : '';
249
+
250
+		return $first_date && $last_date ? $first_date . $conjunction . $last_date : '';
251
+	}
252
+
253
+
254
+	/**
255
+	 * This returns the chronologically first datetime that this ticket is associated with
256
+	 *
257
+	 * @return EE_Datetime
258
+	 * @throws EE_Error
259
+	 */
260
+	public function first_datetime()
261
+	{
262
+		$datetimes = $this->datetimes(array('limit' => 1));
263
+		return reset($datetimes);
264
+	}
265
+
266
+
267
+	/**
268
+	 * Gets all the datetimes this ticket can be used for attending.
269
+	 * Unless otherwise specified, orders datetimes by start date.
270
+	 *
271
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
272
+	 * @return EE_Datetime[]|EE_Base_Class[]
273
+	 * @throws EE_Error
274
+	 */
275
+	public function datetimes($query_params = array())
276
+	{
277
+		if (! isset($query_params['order_by'])) {
278
+			$query_params['order_by']['DTT_order'] = 'ASC';
279
+		}
280
+		return $this->get_many_related('Datetime', $query_params);
281
+	}
282
+
283
+
284
+	/**
285
+	 * This returns the chronologically last datetime that this ticket is associated with
286
+	 *
287
+	 * @return EE_Datetime
288
+	 * @throws EE_Error
289
+	 */
290
+	public function last_datetime()
291
+	{
292
+		$datetimes = $this->datetimes(array('limit' => 1, 'order_by' => array('DTT_EVT_start' => 'DESC')));
293
+		return end($datetimes);
294
+	}
295
+
296
+
297
+	/**
298
+	 * This returns the total tickets sold depending on the given parameters.
299
+	 *
300
+	 * @param  string $what   Can be one of two options: 'ticket', 'datetime'.
301
+	 *                        'ticket' = total ticket sales for all datetimes this ticket is related to
302
+	 *                        'datetime' = total ticket sales for a specified datetime (required $dtt_id)
303
+	 *                        'datetime' = total ticket sales in the datetime_ticket table.
304
+	 *                        If $dtt_id is not given then we return an array of sales indexed by datetime.
305
+	 *                        If $dtt_id IS given then we return the tickets sold for that given datetime.
306
+	 * @param  int    $dtt_id [optional] include the dtt_id with $what = 'datetime'.
307
+	 * @return mixed (array|int)          how many tickets have sold
308
+	 * @throws EE_Error
309
+	 */
310
+	public function tickets_sold($what = 'ticket', $dtt_id = null)
311
+	{
312
+		$total = 0;
313
+		$tickets_sold = $this->_all_tickets_sold();
314
+		switch ($what) {
315
+			case 'ticket':
316
+				return $tickets_sold['ticket'];
317
+				break;
318
+			case 'datetime':
319
+				if (empty($tickets_sold['datetime'])) {
320
+					return $total;
321
+				}
322
+				if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) {
323
+					EE_Error::add_error(
324
+						__(
325
+							'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included.  Are you SURE that is a datetime related to this ticket?',
326
+							'event_espresso'
327
+						),
328
+						__FILE__,
329
+						__FUNCTION__,
330
+						__LINE__
331
+					);
332
+					return $total;
333
+				}
334
+				return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ];
335
+				break;
336
+			default:
337
+				return $total;
338
+		}
339
+	}
340
+
341
+
342
+	/**
343
+	 * This returns an array indexed by datetime_id for tickets sold with this ticket.
344
+	 *
345
+	 * @return EE_Ticket[]
346
+	 * @throws EE_Error
347
+	 */
348
+	protected function _all_tickets_sold()
349
+	{
350
+		$datetimes = $this->get_many_related('Datetime');
351
+		$tickets_sold = array();
352
+		if (! empty($datetimes)) {
353
+			foreach ($datetimes as $datetime) {
354
+				$tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold');
355
+			}
356
+		}
357
+		// Tickets sold
358
+		$tickets_sold['ticket'] = $this->sold();
359
+		return $tickets_sold;
360
+	}
361
+
362
+
363
+	/**
364
+	 * This returns the base price object for the ticket.
365
+	 *
366
+	 * @param  bool $return_array whether to return as an array indexed by price id or just the object.
367
+	 * @return EE_Price|EE_Base_Class|EE_Price[]|EE_Base_Class[]
368
+	 * @throws EE_Error
369
+	 */
370
+	public function base_price($return_array = false)
371
+	{
372
+		$_where = array('Price_Type.PBT_ID' => EEM_Price_Type::base_type_base_price);
373
+		return $return_array
374
+			? $this->get_many_related('Price', array($_where))
375
+			: $this->get_first_related('Price', array($_where));
376
+	}
377
+
378
+
379
+	/**
380
+	 * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price)
381
+	 *
382
+	 * @access public
383
+	 * @return EE_Price[]
384
+	 * @throws EE_Error
385
+	 */
386
+	public function price_modifiers()
387
+	{
388
+		$query_params = array(
389
+			0 => array(
390
+				'Price_Type.PBT_ID' => array(
391
+					'NOT IN',
392
+					array(EEM_Price_Type::base_type_base_price, EEM_Price_Type::base_type_tax),
393
+				),
394
+			),
395
+		);
396
+		return $this->prices($query_params);
397
+	}
398
+
399
+
400
+	/**
401
+	 * Gets all the prices that combine to form the final price of this ticket
402
+	 *
403
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
404
+	 * @return EE_Price[]|EE_Base_Class[]
405
+	 * @throws EE_Error
406
+	 */
407
+	public function prices($query_params = array())
408
+	{
409
+		return $this->get_many_related('Price', $query_params);
410
+	}
411
+
412
+
413
+	/**
414
+	 * Gets all the ticket applicabilities (ie, relations between datetimes and tickets)
415
+	 *
416
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
417
+	 * @return EE_Datetime_Ticket|EE_Base_Class[]
418
+	 * @throws EE_Error
419
+	 */
420
+	public function datetime_tickets($query_params = array())
421
+	{
422
+		return $this->get_many_related('Datetime_Ticket', $query_params);
423
+	}
424
+
425
+
426
+	/**
427
+	 * Gets all the datetimes from the db ordered by DTT_order
428
+	 *
429
+	 * @param boolean $show_expired
430
+	 * @param boolean $show_deleted
431
+	 * @return EE_Datetime[]
432
+	 * @throws EE_Error
433
+	 */
434
+	public function datetimes_ordered($show_expired = true, $show_deleted = false)
435
+	{
436
+		return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_ticket_ordered_by_DTT_order(
437
+			$this->ID(),
438
+			$show_expired,
439
+			$show_deleted
440
+		);
441
+	}
442
+
443
+
444
+	/**
445
+	 * Gets ID
446
+	 *
447
+	 * @return string
448
+	 * @throws EE_Error
449
+	 */
450
+	public function ID()
451
+	{
452
+		return $this->get('TKT_ID');
453
+	}
454
+
455
+
456
+	/**
457
+	 * get the author of the ticket.
458
+	 *
459
+	 * @since 4.5.0
460
+	 * @return int
461
+	 * @throws EE_Error
462
+	 */
463
+	public function wp_user()
464
+	{
465
+		return $this->get('TKT_wp_user');
466
+	}
467
+
468
+
469
+	/**
470
+	 * Gets the template for the ticket
471
+	 *
472
+	 * @return EE_Ticket_Template|EE_Base_Class
473
+	 * @throws EE_Error
474
+	 */
475
+	public function template()
476
+	{
477
+		return $this->get_first_related('Ticket_Template');
478
+	}
479
+
480
+
481
+	/**
482
+	 * Simply returns an array of EE_Price objects that are taxes.
483
+	 *
484
+	 * @return EE_Price[]
485
+	 * @throws EE_Error
486
+	 */
487
+	public function get_ticket_taxes_for_admin()
488
+	{
489
+		return EE_Taxes::get_taxes_for_admin();
490
+	}
491
+
492
+
493
+	/**
494
+	 * @return float
495
+	 * @throws EE_Error
496
+	 */
497
+	public function ticket_price()
498
+	{
499
+		return $this->get('TKT_price');
500
+	}
501
+
502
+
503
+	/**
504
+	 * @return mixed
505
+	 * @throws EE_Error
506
+	 */
507
+	public function pretty_price()
508
+	{
509
+		return $this->get_pretty('TKT_price');
510
+	}
511
+
512
+
513
+	/**
514
+	 * @return bool
515
+	 * @throws EE_Error
516
+	 */
517
+	public function is_free()
518
+	{
519
+		return $this->get_ticket_total_with_taxes() === (float) 0;
520
+	}
521
+
522
+
523
+	/**
524
+	 * get_ticket_total_with_taxes
525
+	 *
526
+	 * @param bool $no_cache
527
+	 * @return float
528
+	 * @throws EE_Error
529
+	 */
530
+	public function get_ticket_total_with_taxes($no_cache = false)
531
+	{
532
+		if ($this->_ticket_total_with_taxes === null || $no_cache) {
533
+			$this->_ticket_total_with_taxes = $this->get_ticket_subtotal() + $this->get_ticket_taxes_total_for_admin();
534
+		}
535
+		return (float) $this->_ticket_total_with_taxes;
536
+	}
537
+
538
+
539
+	public function ensure_TKT_Price_correct()
540
+	{
541
+		$this->set('TKT_price', EE_Taxes::get_subtotal_for_admin($this));
542
+		$this->save();
543
+	}
544
+
545
+
546
+	/**
547
+	 * @return float
548
+	 * @throws EE_Error
549
+	 */
550
+	public function get_ticket_subtotal()
551
+	{
552
+		return EE_Taxes::get_subtotal_for_admin($this);
553
+	}
554
+
555
+
556
+	/**
557
+	 * Returns the total taxes applied to this ticket
558
+	 *
559
+	 * @return float
560
+	 * @throws EE_Error
561
+	 */
562
+	public function get_ticket_taxes_total_for_admin()
563
+	{
564
+		return EE_Taxes::get_total_taxes_for_admin($this);
565
+	}
566
+
567
+
568
+	/**
569
+	 * Sets name
570
+	 *
571
+	 * @param string $name
572
+	 * @throws EE_Error
573
+	 */
574
+	public function set_name($name)
575
+	{
576
+		$this->set('TKT_name', $name);
577
+	}
578
+
579
+
580
+	/**
581
+	 * Gets description
582
+	 *
583
+	 * @return string
584
+	 * @throws EE_Error
585
+	 */
586
+	public function description()
587
+	{
588
+		return $this->get('TKT_description');
589
+	}
590
+
591
+
592
+	/**
593
+	 * Sets description
594
+	 *
595
+	 * @param string $description
596
+	 * @throws EE_Error
597
+	 */
598
+	public function set_description($description)
599
+	{
600
+		$this->set('TKT_description', $description);
601
+	}
602
+
603
+
604
+	/**
605
+	 * Gets start_date
606
+	 *
607
+	 * @param string $dt_frmt
608
+	 * @param string $tm_frmt
609
+	 * @return string
610
+	 * @throws EE_Error
611
+	 */
612
+	public function start_date($dt_frmt = '', $tm_frmt = '')
613
+	{
614
+		return $this->_get_datetime('TKT_start_date', $dt_frmt, $tm_frmt);
615
+	}
616
+
617
+
618
+	/**
619
+	 * Sets start_date
620
+	 *
621
+	 * @param string $start_date
622
+	 * @return void
623
+	 * @throws EE_Error
624
+	 */
625
+	public function set_start_date($start_date)
626
+	{
627
+		$this->_set_date_time('B', $start_date, 'TKT_start_date');
628
+	}
629
+
630
+
631
+	/**
632
+	 * Gets end_date
633
+	 *
634
+	 * @param string $dt_frmt
635
+	 * @param string $tm_frmt
636
+	 * @return string
637
+	 * @throws EE_Error
638
+	 */
639
+	public function end_date($dt_frmt = '', $tm_frmt = '')
640
+	{
641
+		return $this->_get_datetime('TKT_end_date', $dt_frmt, $tm_frmt);
642
+	}
643
+
644
+
645
+	/**
646
+	 * Sets end_date
647
+	 *
648
+	 * @param string $end_date
649
+	 * @return void
650
+	 * @throws EE_Error
651
+	 */
652
+	public function set_end_date($end_date)
653
+	{
654
+		$this->_set_date_time('B', $end_date, 'TKT_end_date');
655
+	}
656
+
657
+
658
+	/**
659
+	 * Sets sell until time
660
+	 *
661
+	 * @since 4.5.0
662
+	 * @param string $time a string representation of the sell until time (ex 9am or 7:30pm)
663
+	 * @throws EE_Error
664
+	 */
665
+	public function set_end_time($time)
666
+	{
667
+		$this->_set_time_for($time, 'TKT_end_date');
668
+	}
669
+
670
+
671
+	/**
672
+	 * Sets min
673
+	 *
674
+	 * @param int $min
675
+	 * @return void
676
+	 * @throws EE_Error
677
+	 */
678
+	public function set_min($min)
679
+	{
680
+		$this->set('TKT_min', $min);
681
+	}
682
+
683
+
684
+	/**
685
+	 * Gets max
686
+	 *
687
+	 * @return int
688
+	 * @throws EE_Error
689
+	 */
690
+	public function max()
691
+	{
692
+		return $this->get('TKT_max');
693
+	}
694
+
695
+
696
+	/**
697
+	 * Sets max
698
+	 *
699
+	 * @param int $max
700
+	 * @return void
701
+	 * @throws EE_Error
702
+	 */
703
+	public function set_max($max)
704
+	{
705
+		$this->set('TKT_max', $max);
706
+	}
707
+
708
+
709
+	/**
710
+	 * Sets price
711
+	 *
712
+	 * @param float $price
713
+	 * @return void
714
+	 * @throws EE_Error
715
+	 */
716
+	public function set_price($price)
717
+	{
718
+		$this->set('TKT_price', $price);
719
+	}
720
+
721
+
722
+	/**
723
+	 * Gets sold
724
+	 *
725
+	 * @return int
726
+	 * @throws EE_Error
727
+	 */
728
+	public function sold()
729
+	{
730
+		return $this->get_raw('TKT_sold');
731
+	}
732
+
733
+
734
+	/**
735
+	 * Sets sold
736
+	 *
737
+	 * @param int $sold
738
+	 * @return void
739
+	 * @throws EE_Error
740
+	 */
741
+	public function set_sold($sold)
742
+	{
743
+		// sold can not go below zero
744
+		$sold = max(0, $sold);
745
+		$this->set('TKT_sold', $sold);
746
+	}
747
+
748
+
749
+	/**
750
+	 * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
751
+	 * associated datetimes.
752
+	 *
753
+	 * @since $VID:$
754
+	 * @param int $qty
755
+	 * @return boolean
756
+	 * @throws EE_Error
757
+	 * @throws InvalidArgumentException
758
+	 * @throws InvalidDataTypeException
759
+	 * @throws InvalidInterfaceException
760
+	 * @throws ReflectionException
761
+	 */
762
+	public function increaseSold($qty = 1)
763
+	{
764
+		$qty = absint($qty);
765
+		// increment sold and decrement reserved datetime quantities simultaneously
766
+		// don't worry about failures, because they must have already had a spot reserved
767
+		$this->increaseSoldForDatetimes($qty);
768
+		// Increment and decrement ticket quantities simultaneously
769
+		$success = $this->adjustNumericFieldsInDb(
770
+			[
771
+				'TKT_reserved' => $qty * -1,
772
+				'TKT_sold' => $qty
773
+			]
774
+		);
775
+		do_action(
776
+			'AHEE__EE_Ticket__increase_sold',
777
+			$this,
778
+			$qty,
779
+			$this->sold(),
780
+			$success
781
+		);
782
+		return $success;
783
+	}
784
+
785
+	/**
786
+	 * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
787
+	 *
788
+	 * @since $VID:$
789
+	 * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts),
790
+	 *             Negative means to decreases old counts (and increase reserved counts).
791
+	 * @param EE_Datetime[] $datetimes
792
+	 * @throws EE_Error
793
+	 * @throws InvalidArgumentException
794
+	 * @throws InvalidDataTypeException
795
+	 * @throws InvalidInterfaceException
796
+	 * @throws ReflectionException
797
+	 */
798
+	protected function increaseSoldForDatetimes($qty, array $datetimes = [])
799
+	{
800
+		$datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
801
+		foreach ($datetimes as $datetime) {
802
+			$datetime->increaseSold($qty);
803
+		}
804
+	}
805
+
806
+
807
+
808
+	/**
809
+	 * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
810
+	 * DB and then updates the model objects.
811
+	 * Does not affect the reserved counts.
812
+	 *
813
+	 * @since $VID:$
814
+	 * @param int $qty
815
+	 * @return boolean
816
+	 * @throws EE_Error
817
+	 * @throws InvalidArgumentException
818
+	 * @throws InvalidDataTypeException
819
+	 * @throws InvalidInterfaceException
820
+	 * @throws ReflectionException
821
+	 */
822
+	public function decreaseSold($qty = 1)
823
+	{
824
+		$qty = absint($qty);
825
+		$this->decreaseSoldForDatetimes($qty);
826
+		$success = $this->adjustNumericFieldsInDb(
827
+			[
828
+				'TKT_sold' => $qty * -1
829
+			]
830
+		);
831
+		do_action(
832
+			'AHEE__EE_Ticket__decrease_sold',
833
+			$this,
834
+			$qty,
835
+			$this->sold(),
836
+			$success
837
+		);
838
+		return $success;
839
+	}
840
+
841
+
842
+	/**
843
+	 * Decreases sold on related datetimes
844
+	 *
845
+	 * @since $VID:$
846
+	 * @param int $qty
847
+	 * @param EE_Datetime[] $datetimes
848
+	 * @return void
849
+	 * @throws EE_Error
850
+	 * @throws InvalidArgumentException
851
+	 * @throws InvalidDataTypeException
852
+	 * @throws InvalidInterfaceException
853
+	 * @throws ReflectionException
854
+	 */
855
+	protected function decreaseSoldForDatetimes($qty = 1, array $datetimes = [])
856
+	{
857
+		$datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
858
+		if (is_array($datetimes)) {
859
+			foreach ($datetimes as $datetime) {
860
+				if ($datetime instanceof EE_Datetime) {
861
+					$datetime->decreaseSold($qty);
862
+				}
863
+			}
864
+		}
865
+	}
866
+
867
+
868
+	/**
869
+	 * Gets qty of reserved tickets
870
+	 *
871
+	 * @return int
872
+	 * @throws EE_Error
873
+	 */
874
+	public function reserved()
875
+	{
876
+		return $this->get_raw('TKT_reserved');
877
+	}
878
+
879
+
880
+	/**
881
+	 * Sets reserved
882
+	 *
883
+	 * @param int $reserved
884
+	 * @return void
885
+	 * @throws EE_Error
886
+	 */
887
+	public function set_reserved($reserved)
888
+	{
889
+		// reserved can not go below zero
890
+		$reserved = max(0, (int) $reserved);
891
+		$this->set('TKT_reserved', $reserved);
892
+	}
893
+
894
+
895
+	/**
896
+	 * Increments reserved by amount passed by $qty, and persists it immediately to the database.
897
+	 *
898
+	 * @since $VID:$
899
+	 * @param int    $qty
900
+	 * @param string $source
901
+	 * @return bool whether we successfully reserved the ticket or not.
902
+	 * @throws EE_Error
903
+	 * @throws InvalidArgumentException
904
+	 * @throws ReflectionException
905
+	 * @throws InvalidDataTypeException
906
+	 * @throws InvalidInterfaceException
907
+	 */
908
+	public function increaseReserved($qty = 1, $source = 'unknown')
909
+	{
910
+		$qty = absint($qty);
911
+		do_action(
912
+			'AHEE__EE_Ticket__increase_reserved__begin',
913
+			$this,
914
+			$qty,
915
+			$source
916
+		);
917
+		$this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "{$qty} from {$source}");
918
+		$success = false;
919
+		$datetimes_adjusted_successfully = $this->increaseReservedForDatetimes($qty);
920
+		if ($datetimes_adjusted_successfully) {
921
+			$success = $this->incrementFieldConditionallyInDb(
922
+				'TKT_reserved',
923
+				'TKT_sold',
924
+				'TKT_qty',
925
+				$qty
926
+			);
927
+			if (! $success) {
928
+				// The datetimes were successfully bumped, but not the
929
+				// ticket. So we need to manually rollback the datetimes.
930
+				$this->decreaseReservedForDatetimes($qty);
931
+			}
932
+		}
933
+		do_action(
934
+			'AHEE__EE_Ticket__increase_reserved',
935
+			$this,
936
+			$qty,
937
+			$this->reserved(),
938
+			$success
939
+		);
940
+		return $success;
941
+	}
942
+
943
+
944
+	/**
945
+	 * Increases reserved counts on related datetimes
946
+	 *
947
+	 * @since $VID:$
948
+	 * @param int $qty
949
+	 * @param EE_Datetime[] $datetimes
950
+	 * @return boolean indicating success
951
+	 * @throws EE_Error
952
+	 * @throws InvalidArgumentException
953
+	 * @throws InvalidDataTypeException
954
+	 * @throws InvalidInterfaceException
955
+	 * @throws ReflectionException
956
+	 */
957
+	protected function increaseReservedForDatetimes($qty = 1, array $datetimes = [])
958
+	{
959
+		$datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
960
+		$datetimes_updated = [];
961
+		$limit_exceeded = false;
962
+		if (is_array($datetimes)) {
963
+			foreach ($datetimes as $datetime) {
964
+				if ($datetime instanceof EE_Datetime) {
965
+					if ($datetime->increaseReserved($qty)) {
966
+						$datetimes_updated[] = $datetime;
967
+					} else {
968
+						$limit_exceeded = true;
969
+						break;
970
+					}
971
+				}
972
+			}
973
+			// If somewhere along the way we detected a datetime whose
974
+			// limit was exceeded, do a manual rollback.
975
+			if ($limit_exceeded) {
976
+				$this->decreaseReservedForDatetimes($qty, $datetimes_updated);
977
+				return false;
978
+			}
979
+		}
980
+		return true;
981
+	}
982
+
983
+
984
+	/**
985
+	 * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
986
+	 *
987
+	 * @since $VID:$
988
+	 * @param int    $qty
989
+	 * @param bool   $adjust_datetimes
990
+	 * @param string $source
991
+	 * @return boolean
992
+	 * @throws EE_Error
993
+	 * @throws InvalidArgumentException
994
+	 * @throws ReflectionException
995
+	 * @throws InvalidDataTypeException
996
+	 * @throws InvalidInterfaceException
997
+	 */
998
+	public function decreaseReserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
999
+	{
1000
+		$qty = absint($qty);
1001
+		$this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "-{$qty} from {$source}");
1002
+		if ($adjust_datetimes) {
1003
+			$this->decreaseReservedForDatetimes($qty);
1004
+		}
1005
+		$success = $this->adjustNumericFieldsInDb(
1006
+			[
1007
+				'TKT_reserved' => $qty * -1
1008
+			]
1009
+		);
1010
+		do_action(
1011
+			'AHEE__EE_Ticket__decrease_reserved',
1012
+			$this,
1013
+			$qty,
1014
+			$this->reserved(),
1015
+			$success
1016
+		);
1017
+		return $success;
1018
+	}
1019
+
1020
+
1021
+	/**
1022
+	 * Decreases the reserved count on the specified datetimes.
1023
+	 *
1024
+	 * @since $VID:$
1025
+	 * @param int           $qty
1026
+	 * @param EE_Datetime[] $datetimes
1027
+	 * @throws EE_Error
1028
+	 * @throws InvalidArgumentException
1029
+	 * @throws ReflectionException
1030
+	 * @throws InvalidDataTypeException
1031
+	 * @throws InvalidInterfaceException
1032
+	 */
1033
+	protected function decreaseReservedForDatetimes($qty = 1, array $datetimes = [])
1034
+	{
1035
+		$datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
1036
+		foreach ($datetimes as $datetime) {
1037
+			if ($datetime instanceof EE_Datetime) {
1038
+				$datetime->decreaseReserved($qty);
1039
+			}
1040
+		}
1041
+	}
1042
+
1043
+
1044
+	/**
1045
+	 * Gets ticket quantity
1046
+	 *
1047
+	 * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1048
+	 *                            therefore $context can be one of three values: '', 'reg_limit', or 'saleable'
1049
+	 *                            '' (default) quantity is the actual db value for TKT_qty, unaffected by other objects
1050
+	 *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1051
+	 *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1052
+	 *                            is therefore the truest measure of tickets that can be purchased at the moment
1053
+	 * @return int
1054
+	 * @throws EE_Error
1055
+	 */
1056
+	public function qty($context = '')
1057
+	{
1058
+		switch ($context) {
1059
+			case 'reg_limit':
1060
+				return $this->real_quantity_on_ticket();
1061
+			case 'saleable':
1062
+				return $this->real_quantity_on_ticket('saleable');
1063
+			default:
1064
+				return $this->get_raw('TKT_qty');
1065
+		}
1066
+	}
1067
+
1068
+
1069
+	/**
1070
+	 * Gets ticket quantity
1071
+	 *
1072
+	 * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1073
+	 *                            therefore $context can be one of two values: 'reg_limit', or 'saleable'
1074
+	 *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1075
+	 *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1076
+	 *                            is therefore the truest measure of tickets that can be purchased at the moment
1077
+	 * @param  int   $DTT_ID      the primary key for a particular datetime.
1078
+	 *                            set to 0 for all related datetimes
1079
+	 * @return int
1080
+	 * @throws EE_Error
1081
+	 */
1082
+	public function real_quantity_on_ticket($context = 'reg_limit', $DTT_ID = 0)
1083
+	{
1084
+		$raw = $this->get_raw('TKT_qty');
1085
+		// return immediately if it's zero
1086
+		if ($raw === 0) {
1087
+			return $raw;
1088
+		}
1089
+		// echo "\n\n<br />Ticket: " . $this->name() . '<br />';
1090
+		// ensure qty doesn't exceed raw value for THIS ticket
1091
+		$qty = min(EE_INF, $raw);
1092
+		// echo "\n . qty: " . $qty . '<br />';
1093
+		// calculate this ticket's total sales and reservations
1094
+		$sold_and_reserved_for_this_ticket = $this->sold() + $this->reserved();
1095
+		// echo "\n . sold: " . $this->sold() . '<br />';
1096
+		// echo "\n . reserved: " . $this->reserved() . '<br />';
1097
+		// echo "\n . sold_and_reserved_for_this_ticket: " . $sold_and_reserved_for_this_ticket . '<br />';
1098
+		// first we need to calculate the maximum number of tickets available for the datetime
1099
+		// do we want data for one datetime or all of them ?
1100
+		$query_params = $DTT_ID ? array(array('DTT_ID' => $DTT_ID)) : array();
1101
+		$datetimes = $this->datetimes($query_params);
1102
+		if (is_array($datetimes) && ! empty($datetimes)) {
1103
+			foreach ($datetimes as $datetime) {
1104
+				if ($datetime instanceof EE_Datetime) {
1105
+					$datetime->refresh_from_db();
1106
+					// echo "\n . . datetime name: " . $datetime->name() . '<br />';
1107
+					// echo "\n . . datetime ID: " . $datetime->ID() . '<br />';
1108
+					// initialize with no restrictions for each datetime
1109
+					// but adjust datetime qty based on datetime reg limit
1110
+					$datetime_qty = min(EE_INF, $datetime->reg_limit());
1111
+					// echo "\n . . . datetime reg_limit: " . $datetime->reg_limit() . '<br />';
1112
+					// echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1113
+					// if we want the actual saleable amount, then we need to consider OTHER ticket sales
1114
+					// and reservations for this datetime, that do NOT include sales and reservations
1115
+					// for this ticket (so we add $this->sold() and $this->reserved() back in)
1116
+					if ($context === 'saleable') {
1117
+						$datetime_qty = max(
1118
+							$datetime_qty - $datetime->sold_and_reserved() + $sold_and_reserved_for_this_ticket,
1119
+							0
1120
+						);
1121
+						// echo "\n . . . datetime sold: " . $datetime->sold() . '<br />';
1122
+						// echo "\n . . . datetime reserved: " . $datetime->reserved() . '<br />';
1123
+						// echo "\n . . . datetime sold_and_reserved: " . $datetime->sold_and_reserved() . '<br />';
1124
+						// echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1125
+						$datetime_qty = ! $datetime->sold_out() ? $datetime_qty : 0;
1126
+						// echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1127
+					}
1128
+					$qty = min($datetime_qty, $qty);
1129
+					// echo "\n . . qty: " . $qty . '<br />';
1130
+				}
1131
+			}
1132
+		}
1133
+		// NOW that we know the  maximum number of tickets available for the datetime
1134
+		// we can finally factor in the details for this specific ticket
1135
+		if ($qty > 0 && $context === 'saleable') {
1136
+			// and subtract the sales for THIS ticket
1137
+			$qty = max($qty - $sold_and_reserved_for_this_ticket, 0);
1138
+			// echo "\n . qty: " . $qty . '<br />';
1139
+		}
1140
+		// echo "\nFINAL QTY: " . $qty . "<br /><br />";
1141
+		return $qty;
1142
+	}
1143
+
1144
+
1145
+	/**
1146
+	 * Sets qty - IMPORTANT!!! Does NOT allow QTY to be set higher than the lowest reg limit of any related datetimes
1147
+	 *
1148
+	 * @param int $qty
1149
+	 * @return void
1150
+	 * @throws EE_Error
1151
+	 */
1152
+	public function set_qty($qty)
1153
+	{
1154
+		$datetimes = $this->datetimes();
1155
+		foreach ($datetimes as $datetime) {
1156
+			if ($datetime instanceof EE_Datetime) {
1157
+				$qty = min($qty, $datetime->reg_limit());
1158
+			}
1159
+		}
1160
+		$this->set('TKT_qty', $qty);
1161
+	}
1162
+
1163
+
1164
+	/**
1165
+	 * Gets uses
1166
+	 *
1167
+	 * @return int
1168
+	 * @throws EE_Error
1169
+	 */
1170
+	public function uses()
1171
+	{
1172
+		return $this->get('TKT_uses');
1173
+	}
1174
+
1175
+
1176
+	/**
1177
+	 * Sets uses
1178
+	 *
1179
+	 * @param int $uses
1180
+	 * @return void
1181
+	 * @throws EE_Error
1182
+	 */
1183
+	public function set_uses($uses)
1184
+	{
1185
+		$this->set('TKT_uses', $uses);
1186
+	}
1187
+
1188
+
1189
+	/**
1190
+	 * returns whether ticket is required or not.
1191
+	 *
1192
+	 * @return boolean
1193
+	 * @throws EE_Error
1194
+	 */
1195
+	public function required()
1196
+	{
1197
+		return $this->get('TKT_required');
1198
+	}
1199
+
1200
+
1201
+	/**
1202
+	 * sets the TKT_required property
1203
+	 *
1204
+	 * @param boolean $required
1205
+	 * @return void
1206
+	 * @throws EE_Error
1207
+	 */
1208
+	public function set_required($required)
1209
+	{
1210
+		$this->set('TKT_required', $required);
1211
+	}
1212
+
1213
+
1214
+	/**
1215
+	 * Gets taxable
1216
+	 *
1217
+	 * @return boolean
1218
+	 * @throws EE_Error
1219
+	 */
1220
+	public function taxable()
1221
+	{
1222
+		return $this->get('TKT_taxable');
1223
+	}
1224
+
1225
+
1226
+	/**
1227
+	 * Sets taxable
1228
+	 *
1229
+	 * @param boolean $taxable
1230
+	 * @return void
1231
+	 * @throws EE_Error
1232
+	 */
1233
+	public function set_taxable($taxable)
1234
+	{
1235
+		$this->set('TKT_taxable', $taxable);
1236
+	}
1237
+
1238
+
1239
+	/**
1240
+	 * Gets is_default
1241
+	 *
1242
+	 * @return boolean
1243
+	 * @throws EE_Error
1244
+	 */
1245
+	public function is_default()
1246
+	{
1247
+		return $this->get('TKT_is_default');
1248
+	}
1249
+
1250
+
1251
+	/**
1252
+	 * Sets is_default
1253
+	 *
1254
+	 * @param boolean $is_default
1255
+	 * @return void
1256
+	 * @throws EE_Error
1257
+	 */
1258
+	public function set_is_default($is_default)
1259
+	{
1260
+		$this->set('TKT_is_default', $is_default);
1261
+	}
1262
+
1263
+
1264
+	/**
1265
+	 * Gets order
1266
+	 *
1267
+	 * @return int
1268
+	 * @throws EE_Error
1269
+	 */
1270
+	public function order()
1271
+	{
1272
+		return $this->get('TKT_order');
1273
+	}
1274
+
1275
+
1276
+	/**
1277
+	 * Sets order
1278
+	 *
1279
+	 * @param int $order
1280
+	 * @return void
1281
+	 * @throws EE_Error
1282
+	 */
1283
+	public function set_order($order)
1284
+	{
1285
+		$this->set('TKT_order', $order);
1286
+	}
1287
+
1288
+
1289
+	/**
1290
+	 * Gets row
1291
+	 *
1292
+	 * @return int
1293
+	 * @throws EE_Error
1294
+	 */
1295
+	public function row()
1296
+	{
1297
+		return $this->get('TKT_row');
1298
+	}
1299
+
1300
+
1301
+	/**
1302
+	 * Sets row
1303
+	 *
1304
+	 * @param int $row
1305
+	 * @return void
1306
+	 * @throws EE_Error
1307
+	 */
1308
+	public function set_row($row)
1309
+	{
1310
+		$this->set('TKT_row', $row);
1311
+	}
1312
+
1313
+
1314
+	/**
1315
+	 * Gets deleted
1316
+	 *
1317
+	 * @return boolean
1318
+	 * @throws EE_Error
1319
+	 */
1320
+	public function deleted()
1321
+	{
1322
+		return $this->get('TKT_deleted');
1323
+	}
1324
+
1325
+
1326
+	/**
1327
+	 * Sets deleted
1328
+	 *
1329
+	 * @param boolean $deleted
1330
+	 * @return void
1331
+	 * @throws EE_Error
1332
+	 */
1333
+	public function set_deleted($deleted)
1334
+	{
1335
+		$this->set('TKT_deleted', $deleted);
1336
+	}
1337
+
1338
+
1339
+	/**
1340
+	 * Gets parent
1341
+	 *
1342
+	 * @return int
1343
+	 * @throws EE_Error
1344
+	 */
1345
+	public function parent_ID()
1346
+	{
1347
+		return $this->get('TKT_parent');
1348
+	}
1349
+
1350
+
1351
+	/**
1352
+	 * Sets parent
1353
+	 *
1354
+	 * @param int $parent
1355
+	 * @return void
1356
+	 * @throws EE_Error
1357
+	 */
1358
+	public function set_parent_ID($parent)
1359
+	{
1360
+		$this->set('TKT_parent', $parent);
1361
+	}
1362
+
1363
+
1364
+	/**
1365
+	 * Gets a string which is handy for showing in gateways etc that describes the ticket.
1366
+	 *
1367
+	 * @return string
1368
+	 * @throws EE_Error
1369
+	 */
1370
+	public function name_and_info()
1371
+	{
1372
+		$times = array();
1373
+		foreach ($this->datetimes() as $datetime) {
1374
+			$times[] = $datetime->start_date_and_time();
1375
+		}
1376
+		return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price();
1377
+	}
1378
+
1379
+
1380
+	/**
1381
+	 * Gets name
1382
+	 *
1383
+	 * @return string
1384
+	 * @throws EE_Error
1385
+	 */
1386
+	public function name()
1387
+	{
1388
+		return $this->get('TKT_name');
1389
+	}
1390
+
1391
+
1392
+	/**
1393
+	 * Gets price
1394
+	 *
1395
+	 * @return float
1396
+	 * @throws EE_Error
1397
+	 */
1398
+	public function price()
1399
+	{
1400
+		return $this->get('TKT_price');
1401
+	}
1402
+
1403
+
1404
+	/**
1405
+	 * Gets all the registrations for this ticket
1406
+	 *
1407
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1408
+	 * @return EE_Registration[]|EE_Base_Class[]
1409
+	 * @throws EE_Error
1410
+	 */
1411
+	public function registrations($query_params = array())
1412
+	{
1413
+		return $this->get_many_related('Registration', $query_params);
1414
+	}
1415
+
1416
+
1417
+	/**
1418
+	 * Updates the TKT_sold attribute (and saves) based on the number of APPROVED registrations for this ticket.
1419
+	 *
1420
+	 * @return int
1421
+	 * @throws EE_Error
1422
+	 */
1423
+	public function update_tickets_sold()
1424
+	{
1425
+		$count_regs_for_this_ticket = $this->count_registrations(
1426
+			array(
1427
+				array(
1428
+					'STS_ID'      => EEM_Registration::status_id_approved,
1429
+					'REG_deleted' => 0,
1430
+				),
1431
+			)
1432
+		);
1433
+		$this->set_sold($count_regs_for_this_ticket);
1434
+		$this->save();
1435
+		return $count_regs_for_this_ticket;
1436
+	}
1437
+
1438
+
1439
+	/**
1440
+	 * Counts the registrations for this ticket
1441
+	 *
1442
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1443
+	 * @return int
1444
+	 */
1445
+	public function count_registrations($query_params = array())
1446
+	{
1447
+		return $this->count_related('Registration', $query_params);
1448
+	}
1449
+
1450
+
1451
+	/**
1452
+	 * Implementation for EEI_Has_Icon interface method.
1453
+	 *
1454
+	 * @see EEI_Visual_Representation for comments
1455
+	 * @return string
1456
+	 */
1457
+	public function get_icon()
1458
+	{
1459
+		return '<span class="dashicons dashicons-tickets-alt"></span>';
1460
+	}
1461
+
1462
+
1463
+	/**
1464
+	 * Implementation of the EEI_Event_Relation interface method
1465
+	 *
1466
+	 * @see EEI_Event_Relation for comments
1467
+	 * @return EE_Event
1468
+	 * @throws EE_Error
1469
+	 * @throws UnexpectedEntityException
1470
+	 */
1471
+	public function get_related_event()
1472
+	{
1473
+		// get one datetime to use for getting the event
1474
+		$datetime = $this->first_datetime();
1475
+		if (! $datetime instanceof \EE_Datetime) {
1476
+			throw new UnexpectedEntityException(
1477
+				$datetime,
1478
+				'EE_Datetime',
1479
+				sprintf(
1480
+					__('The ticket (%s) is not associated with any valid datetimes.', 'event_espresso'),
1481
+					$this->name()
1482
+				)
1483
+			);
1484
+		}
1485
+		$event = $datetime->event();
1486
+		if (! $event instanceof \EE_Event) {
1487
+			throw new UnexpectedEntityException(
1488
+				$event,
1489
+				'EE_Event',
1490
+				sprintf(
1491
+					__('The ticket (%s) is not associated with a valid event.', 'event_espresso'),
1492
+					$this->name()
1493
+				)
1494
+			);
1495
+		}
1496
+		return $event;
1497
+	}
1498
+
1499
+
1500
+	/**
1501
+	 * Implementation of the EEI_Event_Relation interface method
1502
+	 *
1503
+	 * @see EEI_Event_Relation for comments
1504
+	 * @return string
1505
+	 * @throws UnexpectedEntityException
1506
+	 * @throws EE_Error
1507
+	 */
1508
+	public function get_event_name()
1509
+	{
1510
+		$event = $this->get_related_event();
1511
+		return $event instanceof EE_Event ? $event->name() : '';
1512
+	}
1513
+
1514
+
1515
+	/**
1516
+	 * Implementation of the EEI_Event_Relation interface method
1517
+	 *
1518
+	 * @see EEI_Event_Relation for comments
1519
+	 * @return int
1520
+	 * @throws UnexpectedEntityException
1521
+	 * @throws EE_Error
1522
+	 */
1523
+	public function get_event_ID()
1524
+	{
1525
+		$event = $this->get_related_event();
1526
+		return $event instanceof EE_Event ? $event->ID() : 0;
1527
+	}
1528
+
1529
+
1530
+	/**
1531
+	 * This simply returns whether a ticket can be permanently deleted or not.
1532
+	 * The criteria for determining this is whether the ticket has any related registrations.
1533
+	 * If there are none then it can be permanently deleted.
1534
+	 *
1535
+	 * @return bool
1536
+	 */
1537
+	public function is_permanently_deleteable()
1538
+	{
1539
+		return $this->count_registrations() === 0;
1540
+	}
1541
+
1542
+
1543
+	/*******************************************************************
1544 1544
      ***********************  DEPRECATED METHODS  **********************
1545 1545
      *******************************************************************/
1546 1546
 
1547 1547
 
1548
-    /**
1549
-     * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
1550
-     * associated datetimes.
1551
-     *
1552
-     * @deprecated $VID:$
1553
-     * @param int $qty
1554
-     * @return void
1555
-     * @throws EE_Error
1556
-     * @throws InvalidArgumentException
1557
-     * @throws InvalidDataTypeException
1558
-     * @throws InvalidInterfaceException
1559
-     * @throws ReflectionException
1560
-     */
1561
-    public function increase_sold($qty = 1)
1562
-    {
1563
-        EE_Error::doing_it_wrong(
1564
-            __FUNCTION__,
1565
-            esc_html__('Please use EE_Ticket::increaseSold() instead', 'event_espresso'),
1566
-            '$VID:$',
1567
-            '5.0.0.p'
1568
-        );
1569
-        $this->increaseSold($qty);
1570
-    }
1571
-
1572
-
1573
-    /**
1574
-     * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
1575
-     *
1576
-     * @deprecated $VID:$
1577
-     * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts),
1578
-     *                 Negative means to decreases old counts (and increase reserved counts).
1579
-     * @throws EE_Error
1580
-     * @throws InvalidArgumentException
1581
-     * @throws InvalidDataTypeException
1582
-     * @throws InvalidInterfaceException
1583
-     * @throws ReflectionException
1584
-     */
1585
-    protected function _increase_sold_for_datetimes($qty)
1586
-    {
1587
-        EE_Error::doing_it_wrong(
1588
-            __FUNCTION__,
1589
-            esc_html__('Please use EE_Ticket::increaseSoldForDatetimes() instead', 'event_espresso'),
1590
-            '$VID:$',
1591
-            '5.0.0.p'
1592
-        );
1593
-        $this->increaseSoldForDatetimes($qty);
1594
-    }
1595
-
1596
-
1597
-    /**
1598
-     * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
1599
-     * DB and then updates the model objects.
1600
-     * Does not affect the reserved counts.
1601
-     *
1602
-     * @deprecated $VID:$
1603
-     * @param int $qty
1604
-     * @return void
1605
-     * @throws EE_Error
1606
-     * @throws InvalidArgumentException
1607
-     * @throws InvalidDataTypeException
1608
-     * @throws InvalidInterfaceException
1609
-     * @throws ReflectionException
1610
-     */
1611
-    public function decrease_sold($qty = 1)
1612
-    {
1613
-        EE_Error::doing_it_wrong(
1614
-            __FUNCTION__,
1615
-            esc_html__('Please use EE_Ticket::decreaseSold() instead', 'event_espresso'),
1616
-            '$VID:$',
1617
-            '5.0.0.p'
1618
-        );
1619
-        $this->decreaseSold($qty);
1620
-    }
1621
-
1622
-
1623
-    /**
1624
-     * Decreases sold on related datetimes
1625
-     *
1626
-     * @deprecated $VID:$
1627
-     * @param int $qty
1628
-     * @return void
1629
-     * @throws EE_Error
1630
-     * @throws InvalidArgumentException
1631
-     * @throws InvalidDataTypeException
1632
-     * @throws InvalidInterfaceException
1633
-     * @throws ReflectionException
1634
-     */
1635
-    protected function _decrease_sold_for_datetimes($qty = 1)
1636
-    {
1637
-        EE_Error::doing_it_wrong(
1638
-            __FUNCTION__,
1639
-            esc_html__('Please use EE_Ticket::decreaseSoldForDatetimes() instead', 'event_espresso'),
1640
-            '$VID:$',
1641
-            '5.0.0.p'
1642
-        );
1643
-        $this->decreaseSoldForDatetimes($qty);
1644
-    }
1645
-
1646
-
1647
-    /**
1648
-     * Increments reserved by amount passed by $qty, and persists it immediately to the database.
1649
-     *
1650
-     * @deprecated $VID:$
1651
-     * @param int    $qty
1652
-     * @param string $source
1653
-     * @return bool whether we successfully reserved the ticket or not.
1654
-     * @throws EE_Error
1655
-     * @throws InvalidArgumentException
1656
-     * @throws ReflectionException
1657
-     * @throws InvalidDataTypeException
1658
-     * @throws InvalidInterfaceException
1659
-     */
1660
-    public function increase_reserved($qty = 1, $source = 'unknown')
1661
-    {
1662
-        EE_Error::doing_it_wrong(
1663
-            __FUNCTION__,
1664
-            esc_html__('Please use EE_Ticket::increaseReserved() instead', 'event_espresso'),
1665
-            '$VID:$',
1666
-            '5.0.0.p'
1667
-        );
1668
-        return $this->increaseReserved($qty);
1669
-    }
1670
-
1671
-
1672
-    /**
1673
-     * Increases sold on related datetimes
1674
-     *
1675
-     * @deprecated $VID:$
1676
-     * @param int $qty
1677
-     * @return boolean indicating success
1678
-     * @throws EE_Error
1679
-     * @throws InvalidArgumentException
1680
-     * @throws InvalidDataTypeException
1681
-     * @throws InvalidInterfaceException
1682
-     * @throws ReflectionException
1683
-     */
1684
-    protected function _increase_reserved_for_datetimes($qty = 1)
1685
-    {
1686
-        EE_Error::doing_it_wrong(
1687
-            __FUNCTION__,
1688
-            esc_html__('Please use EE_Ticket::increaseReservedForDatetimes() instead', 'event_espresso'),
1689
-            '$VID:$',
1690
-            '5.0.0.p'
1691
-        );
1692
-        return $this->increaseReservedForDatetimes($qty);
1693
-    }
1694
-
1695
-
1696
-    /**
1697
-     * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
1698
-     *
1699
-     * @deprecated $VID:$
1700
-     * @param int    $qty
1701
-     * @param bool   $adjust_datetimes
1702
-     * @param string $source
1703
-     * @return void
1704
-     * @throws EE_Error
1705
-     * @throws InvalidArgumentException
1706
-     * @throws ReflectionException
1707
-     * @throws InvalidDataTypeException
1708
-     * @throws InvalidInterfaceException
1709
-     */
1710
-    public function decrease_reserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
1711
-    {
1712
-        EE_Error::doing_it_wrong(
1713
-            __FUNCTION__,
1714
-            esc_html__('Please use EE_Ticket::decreaseReserved() instead', 'event_espresso'),
1715
-            '$VID:$',
1716
-            '5.0.0.p'
1717
-        );
1718
-        $this->decreaseReserved($qty);
1719
-    }
1720
-
1721
-
1722
-    /**
1723
-     * Decreases reserved on related datetimes
1724
-     *
1725
-     * @deprecated $VID:$
1726
-     * @param int $qty
1727
-     * @return void
1728
-     * @throws EE_Error
1729
-     * @throws InvalidArgumentException
1730
-     * @throws ReflectionException
1731
-     * @throws InvalidDataTypeException
1732
-     * @throws InvalidInterfaceException
1733
-     */
1734
-    protected function _decrease_reserved_for_datetimes($qty = 1)
1735
-    {
1736
-        EE_Error::doing_it_wrong(
1737
-            __FUNCTION__,
1738
-            esc_html__('Please use EE_Ticket::decreaseReservedForDatetimes() instead', 'event_espresso'),
1739
-            '$VID:$',
1740
-            '5.0.0.p'
1741
-        );
1742
-        $this->decreaseReservedForDatetimes($qty);
1743
-    }
1548
+	/**
1549
+	 * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
1550
+	 * associated datetimes.
1551
+	 *
1552
+	 * @deprecated $VID:$
1553
+	 * @param int $qty
1554
+	 * @return void
1555
+	 * @throws EE_Error
1556
+	 * @throws InvalidArgumentException
1557
+	 * @throws InvalidDataTypeException
1558
+	 * @throws InvalidInterfaceException
1559
+	 * @throws ReflectionException
1560
+	 */
1561
+	public function increase_sold($qty = 1)
1562
+	{
1563
+		EE_Error::doing_it_wrong(
1564
+			__FUNCTION__,
1565
+			esc_html__('Please use EE_Ticket::increaseSold() instead', 'event_espresso'),
1566
+			'$VID:$',
1567
+			'5.0.0.p'
1568
+		);
1569
+		$this->increaseSold($qty);
1570
+	}
1571
+
1572
+
1573
+	/**
1574
+	 * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
1575
+	 *
1576
+	 * @deprecated $VID:$
1577
+	 * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts),
1578
+	 *                 Negative means to decreases old counts (and increase reserved counts).
1579
+	 * @throws EE_Error
1580
+	 * @throws InvalidArgumentException
1581
+	 * @throws InvalidDataTypeException
1582
+	 * @throws InvalidInterfaceException
1583
+	 * @throws ReflectionException
1584
+	 */
1585
+	protected function _increase_sold_for_datetimes($qty)
1586
+	{
1587
+		EE_Error::doing_it_wrong(
1588
+			__FUNCTION__,
1589
+			esc_html__('Please use EE_Ticket::increaseSoldForDatetimes() instead', 'event_espresso'),
1590
+			'$VID:$',
1591
+			'5.0.0.p'
1592
+		);
1593
+		$this->increaseSoldForDatetimes($qty);
1594
+	}
1595
+
1596
+
1597
+	/**
1598
+	 * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
1599
+	 * DB and then updates the model objects.
1600
+	 * Does not affect the reserved counts.
1601
+	 *
1602
+	 * @deprecated $VID:$
1603
+	 * @param int $qty
1604
+	 * @return void
1605
+	 * @throws EE_Error
1606
+	 * @throws InvalidArgumentException
1607
+	 * @throws InvalidDataTypeException
1608
+	 * @throws InvalidInterfaceException
1609
+	 * @throws ReflectionException
1610
+	 */
1611
+	public function decrease_sold($qty = 1)
1612
+	{
1613
+		EE_Error::doing_it_wrong(
1614
+			__FUNCTION__,
1615
+			esc_html__('Please use EE_Ticket::decreaseSold() instead', 'event_espresso'),
1616
+			'$VID:$',
1617
+			'5.0.0.p'
1618
+		);
1619
+		$this->decreaseSold($qty);
1620
+	}
1621
+
1622
+
1623
+	/**
1624
+	 * Decreases sold on related datetimes
1625
+	 *
1626
+	 * @deprecated $VID:$
1627
+	 * @param int $qty
1628
+	 * @return void
1629
+	 * @throws EE_Error
1630
+	 * @throws InvalidArgumentException
1631
+	 * @throws InvalidDataTypeException
1632
+	 * @throws InvalidInterfaceException
1633
+	 * @throws ReflectionException
1634
+	 */
1635
+	protected function _decrease_sold_for_datetimes($qty = 1)
1636
+	{
1637
+		EE_Error::doing_it_wrong(
1638
+			__FUNCTION__,
1639
+			esc_html__('Please use EE_Ticket::decreaseSoldForDatetimes() instead', 'event_espresso'),
1640
+			'$VID:$',
1641
+			'5.0.0.p'
1642
+		);
1643
+		$this->decreaseSoldForDatetimes($qty);
1644
+	}
1645
+
1646
+
1647
+	/**
1648
+	 * Increments reserved by amount passed by $qty, and persists it immediately to the database.
1649
+	 *
1650
+	 * @deprecated $VID:$
1651
+	 * @param int    $qty
1652
+	 * @param string $source
1653
+	 * @return bool whether we successfully reserved the ticket or not.
1654
+	 * @throws EE_Error
1655
+	 * @throws InvalidArgumentException
1656
+	 * @throws ReflectionException
1657
+	 * @throws InvalidDataTypeException
1658
+	 * @throws InvalidInterfaceException
1659
+	 */
1660
+	public function increase_reserved($qty = 1, $source = 'unknown')
1661
+	{
1662
+		EE_Error::doing_it_wrong(
1663
+			__FUNCTION__,
1664
+			esc_html__('Please use EE_Ticket::increaseReserved() instead', 'event_espresso'),
1665
+			'$VID:$',
1666
+			'5.0.0.p'
1667
+		);
1668
+		return $this->increaseReserved($qty);
1669
+	}
1670
+
1671
+
1672
+	/**
1673
+	 * Increases sold on related datetimes
1674
+	 *
1675
+	 * @deprecated $VID:$
1676
+	 * @param int $qty
1677
+	 * @return boolean indicating success
1678
+	 * @throws EE_Error
1679
+	 * @throws InvalidArgumentException
1680
+	 * @throws InvalidDataTypeException
1681
+	 * @throws InvalidInterfaceException
1682
+	 * @throws ReflectionException
1683
+	 */
1684
+	protected function _increase_reserved_for_datetimes($qty = 1)
1685
+	{
1686
+		EE_Error::doing_it_wrong(
1687
+			__FUNCTION__,
1688
+			esc_html__('Please use EE_Ticket::increaseReservedForDatetimes() instead', 'event_espresso'),
1689
+			'$VID:$',
1690
+			'5.0.0.p'
1691
+		);
1692
+		return $this->increaseReservedForDatetimes($qty);
1693
+	}
1694
+
1695
+
1696
+	/**
1697
+	 * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
1698
+	 *
1699
+	 * @deprecated $VID:$
1700
+	 * @param int    $qty
1701
+	 * @param bool   $adjust_datetimes
1702
+	 * @param string $source
1703
+	 * @return void
1704
+	 * @throws EE_Error
1705
+	 * @throws InvalidArgumentException
1706
+	 * @throws ReflectionException
1707
+	 * @throws InvalidDataTypeException
1708
+	 * @throws InvalidInterfaceException
1709
+	 */
1710
+	public function decrease_reserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
1711
+	{
1712
+		EE_Error::doing_it_wrong(
1713
+			__FUNCTION__,
1714
+			esc_html__('Please use EE_Ticket::decreaseReserved() instead', 'event_espresso'),
1715
+			'$VID:$',
1716
+			'5.0.0.p'
1717
+		);
1718
+		$this->decreaseReserved($qty);
1719
+	}
1720
+
1721
+
1722
+	/**
1723
+	 * Decreases reserved on related datetimes
1724
+	 *
1725
+	 * @deprecated $VID:$
1726
+	 * @param int $qty
1727
+	 * @return void
1728
+	 * @throws EE_Error
1729
+	 * @throws InvalidArgumentException
1730
+	 * @throws ReflectionException
1731
+	 * @throws InvalidDataTypeException
1732
+	 * @throws InvalidInterfaceException
1733
+	 */
1734
+	protected function _decrease_reserved_for_datetimes($qty = 1)
1735
+	{
1736
+		EE_Error::doing_it_wrong(
1737
+			__FUNCTION__,
1738
+			esc_html__('Please use EE_Ticket::decreaseReservedForDatetimes() instead', 'event_espresso'),
1739
+			'$VID:$',
1740
+			'5.0.0.p'
1741
+		);
1742
+		$this->decreaseReservedForDatetimes($qty);
1743
+	}
1744 1744
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
     public function ticket_status($display = false, $remaining = null)
130 130
     {
131 131
         $remaining = is_bool($remaining) ? $remaining : $this->is_remaining();
132
-        if (! $remaining) {
132
+        if ( ! $remaining) {
133 133
             return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out;
134 134
         }
135 135
         if ($this->get('TKT_deleted')) {
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
             : '';
248 248
         $last_date = $this->last_datetime() instanceof EE_Datetime ? $this->last_datetime()->end_date($dt_frmt) : '';
249 249
 
250
-        return $first_date && $last_date ? $first_date . $conjunction . $last_date : '';
250
+        return $first_date && $last_date ? $first_date.$conjunction.$last_date : '';
251 251
     }
252 252
 
253 253
 
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
      */
275 275
     public function datetimes($query_params = array())
276 276
     {
277
-        if (! isset($query_params['order_by'])) {
277
+        if ( ! isset($query_params['order_by'])) {
278 278
             $query_params['order_by']['DTT_order'] = 'ASC';
279 279
         }
280 280
         return $this->get_many_related('Datetime', $query_params);
@@ -319,7 +319,7 @@  discard block
 block discarded – undo
319 319
                 if (empty($tickets_sold['datetime'])) {
320 320
                     return $total;
321 321
                 }
322
-                if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) {
322
+                if ( ! empty($dtt_id) && ! isset($tickets_sold['datetime'][$dtt_id])) {
323 323
                     EE_Error::add_error(
324 324
                         __(
325 325
                             'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included.  Are you SURE that is a datetime related to this ticket?',
@@ -331,7 +331,7 @@  discard block
 block discarded – undo
331 331
                     );
332 332
                     return $total;
333 333
                 }
334
-                return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ];
334
+                return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][$dtt_id];
335 335
                 break;
336 336
             default:
337 337
                 return $total;
@@ -349,9 +349,9 @@  discard block
 block discarded – undo
349 349
     {
350 350
         $datetimes = $this->get_many_related('Datetime');
351 351
         $tickets_sold = array();
352
-        if (! empty($datetimes)) {
352
+        if ( ! empty($datetimes)) {
353 353
             foreach ($datetimes as $datetime) {
354
-                $tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold');
354
+                $tickets_sold['datetime'][$datetime->ID()] = $datetime->get('DTT_sold');
355 355
             }
356 356
         }
357 357
         // Tickets sold
@@ -924,7 +924,7 @@  discard block
 block discarded – undo
924 924
                 'TKT_qty',
925 925
                 $qty
926 926
             );
927
-            if (! $success) {
927
+            if ( ! $success) {
928 928
                 // The datetimes were successfully bumped, but not the
929 929
                 // ticket. So we need to manually rollback the datetimes.
930 930
                 $this->decreaseReservedForDatetimes($qty);
@@ -1373,7 +1373,7 @@  discard block
 block discarded – undo
1373 1373
         foreach ($this->datetimes() as $datetime) {
1374 1374
             $times[] = $datetime->start_date_and_time();
1375 1375
         }
1376
-        return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price();
1376
+        return $this->name().' @ '.implode(', ', $times).' for '.$this->pretty_price();
1377 1377
     }
1378 1378
 
1379 1379
 
@@ -1472,7 +1472,7 @@  discard block
 block discarded – undo
1472 1472
     {
1473 1473
         // get one datetime to use for getting the event
1474 1474
         $datetime = $this->first_datetime();
1475
-        if (! $datetime instanceof \EE_Datetime) {
1475
+        if ( ! $datetime instanceof \EE_Datetime) {
1476 1476
             throw new UnexpectedEntityException(
1477 1477
                 $datetime,
1478 1478
                 'EE_Datetime',
@@ -1483,7 +1483,7 @@  discard block
 block discarded – undo
1483 1483
             );
1484 1484
         }
1485 1485
         $event = $datetime->event();
1486
-        if (! $event instanceof \EE_Event) {
1486
+        if ( ! $event instanceof \EE_Event) {
1487 1487
             throw new UnexpectedEntityException(
1488 1488
                 $event,
1489 1489
                 'EE_Event',
Please login to merge, or discard this patch.
core/db_classes/EE_Registration.class.php 2 patches
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
     {
120 120
         switch ($field_name) {
121 121
             case 'REG_code':
122
-                if (! empty($field_value) && $this->reg_code() === null) {
122
+                if ( ! empty($field_value) && $this->reg_code() === null) {
123 123
                     $this->set_reg_code($field_value, $use_default);
124 124
                 }
125 125
                 break;
@@ -400,7 +400,7 @@  discard block
 block discarded – undo
400 400
     public function event()
401 401
     {
402 402
         $event = $this->get_first_related('Event');
403
-        if (! $event instanceof \EE_Event) {
403
+        if ( ! $event instanceof \EE_Event) {
404 404
             throw new EntityNotFoundException('Event ID', $this->event_ID());
405 405
         }
406 406
         return $event;
@@ -443,7 +443,7 @@  discard block
 block discarded – undo
443 443
     {
444 444
         // reserved ticket and datetime counts will be decremented as sold counts are incremented
445 445
         // so stop tracking that this reg has a ticket reserved
446
-        $this->release_reserved_ticket(false, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
446
+        $this->release_reserved_ticket(false, "REG: {$this->ID()} (ln:".__LINE__.')');
447 447
         $ticket = $this->ticket();
448 448
         $ticket->increaseSold();
449 449
         // possibly set event status to sold out
@@ -497,7 +497,7 @@  discard block
 block discarded – undo
497 497
                 && $update_ticket
498 498
             ) {
499 499
                 $ticket = $this->ticket();
500
-                $ticket->increaseReserved(1, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
500
+                $ticket->increaseReserved(1, "REG: {$this->ID()} (ln:".__LINE__.')');
501 501
                 $ticket->save();
502 502
             }
503 503
         }
@@ -528,7 +528,7 @@  discard block
 block discarded – undo
528 528
                 && $update_ticket
529 529
             ) {
530 530
                 $ticket = $this->ticket();
531
-                $ticket->decreaseReserved(1, true, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
531
+                $ticket->decreaseReserved(1, true, "REG: {$this->ID()} (ln:".__LINE__.')');
532 532
             }
533 533
         }
534 534
     }
@@ -1202,7 +1202,7 @@  discard block
 block discarded – undo
1202 1202
                     : '';
1203 1203
                 break;
1204 1204
         }
1205
-        return $icon . $status[ $this->status_ID() ];
1205
+        return $icon.$status[$this->status_ID()];
1206 1206
     }
1207 1207
 
1208 1208
 
@@ -1420,7 +1420,7 @@  discard block
 block discarded – undo
1420 1420
             return false;
1421 1421
         }
1422 1422
         // is there a datetime ticket that matches this dtt_ID?
1423
-        if (! (EEM_Datetime_Ticket::instance()->exists(
1423
+        if ( ! (EEM_Datetime_Ticket::instance()->exists(
1424 1424
             array(
1425 1425
                 array(
1426 1426
                     'TKT_ID' => $this->get('TKT_ID'),
@@ -1451,7 +1451,7 @@  discard block
 block discarded – undo
1451 1451
     {
1452 1452
         $DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1453 1453
 
1454
-        if (! $DTT_ID) {
1454
+        if ( ! $DTT_ID) {
1455 1455
             return false;
1456 1456
         }
1457 1457
 
@@ -1459,7 +1459,7 @@  discard block
 block discarded – undo
1459 1459
 
1460 1460
         // if max uses is not set or equals infinity then return true cause its not a factor for whether user can
1461 1461
         // check-in or not.
1462
-        if (! $max_uses || $max_uses === EE_INF) {
1462
+        if ( ! $max_uses || $max_uses === EE_INF) {
1463 1463
             return true;
1464 1464
         }
1465 1465
 
@@ -1519,7 +1519,7 @@  discard block
 block discarded – undo
1519 1519
             $datetime = $this->get_latest_related_datetime();
1520 1520
             $DTT_ID = $datetime instanceof EE_Datetime ? $datetime->ID() : 0;
1521 1521
             // verify the registration can checkin for the given DTT_ID
1522
-        } elseif (! $this->can_checkin($DTT_ID, $verify)) {
1522
+        } elseif ( ! $this->can_checkin($DTT_ID, $verify)) {
1523 1523
             EE_Error::add_error(
1524 1524
                 sprintf(
1525 1525
                     esc_html__(
@@ -1542,7 +1542,7 @@  discard block
 block discarded – undo
1542 1542
         );
1543 1543
         // start by getting the current status so we know what status we'll be changing to.
1544 1544
         $cur_status = $this->check_in_status_for_datetime($DTT_ID, null);
1545
-        $status_to = $status_paths[ $cur_status ];
1545
+        $status_to = $status_paths[$cur_status];
1546 1546
         // database only records true for checked IN or false for checked OUT
1547 1547
         // no record ( null ) means checked in NEVER, but we obviously don't save that
1548 1548
         $new_status = $status_to === EE_Checkin::status_checked_in ? true : false;
@@ -1707,7 +1707,7 @@  discard block
 block discarded – undo
1707 1707
     public function transaction()
1708 1708
     {
1709 1709
         $transaction = $this->get_first_related('Transaction');
1710
-        if (! $transaction instanceof \EE_Transaction) {
1710
+        if ( ! $transaction instanceof \EE_Transaction) {
1711 1711
             throw new EntityNotFoundException('Transaction ID', $this->transaction_ID());
1712 1712
         }
1713 1713
         return $transaction;
@@ -1761,11 +1761,11 @@  discard block
 block discarded – undo
1761 1761
             );
1762 1762
             return;
1763 1763
         }
1764
-        if (! $this->reg_code()) {
1764
+        if ( ! $this->reg_code()) {
1765 1765
             parent::set('REG_code', $REG_code, $use_default);
1766 1766
         } else {
1767 1767
             EE_Error::doing_it_wrong(
1768
-                __CLASS__ . '::' . __FUNCTION__,
1768
+                __CLASS__.'::'.__FUNCTION__,
1769 1769
                 esc_html__('Can not change a registration REG_code once it has been set.', 'event_espresso'),
1770 1770
                 '4.6.0'
1771 1771
             );
@@ -1916,7 +1916,7 @@  discard block
 block discarded – undo
1916 1916
                 break;
1917 1917
             }
1918 1918
         }
1919
-        if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) {
1919
+        if ( ! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) {
1920 1920
             throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID());
1921 1921
         }
1922 1922
         return $line_item;
Please login to merge, or discard this patch.
Indentation   +2071 added lines, -2071 removed lines patch added patch discarded remove patch
@@ -17,2075 +17,2075 @@
 block discarded – undo
17 17
 {
18 18
 
19 19
 
20
-    /**
21
-     * Used to reference when a registration has never been checked in.
22
-     *
23
-     * @deprecated use \EE_Checkin::status_checked_never instead
24
-     * @type int
25
-     */
26
-    const checkin_status_never = 2;
27
-
28
-    /**
29
-     * Used to reference when a registration has been checked in.
30
-     *
31
-     * @deprecated use \EE_Checkin::status_checked_in instead
32
-     * @type int
33
-     */
34
-    const checkin_status_in = 1;
35
-
36
-
37
-    /**
38
-     * Used to reference when a registration has been checked out.
39
-     *
40
-     * @deprecated use \EE_Checkin::status_checked_out instead
41
-     * @type int
42
-     */
43
-    const checkin_status_out = 0;
44
-
45
-
46
-    /**
47
-     * extra meta key for tracking reg status os trashed registrations
48
-     *
49
-     * @type string
50
-     */
51
-    const PRE_TRASH_REG_STATUS_KEY = 'pre_trash_registration_status';
52
-
53
-
54
-    /**
55
-     * extra meta key for tracking if registration has reserved ticket
56
-     *
57
-     * @type string
58
-     */
59
-    const HAS_RESERVED_TICKET_KEY = 'has_reserved_ticket';
60
-
61
-
62
-    /**
63
-     * @param array  $props_n_values          incoming values
64
-     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
65
-     *                                        used.)
66
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
67
-     *                                        date_format and the second value is the time format
68
-     * @return EE_Registration
69
-     * @throws EE_Error
70
-     */
71
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
72
-    {
73
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
74
-        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
75
-    }
76
-
77
-
78
-    /**
79
-     * @param array  $props_n_values  incoming values from the database
80
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
81
-     *                                the website will be used.
82
-     * @return EE_Registration
83
-     */
84
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
85
-    {
86
-        return new self($props_n_values, true, $timezone);
87
-    }
88
-
89
-
90
-    /**
91
-     *        Set Event ID
92
-     *
93
-     * @param        int $EVT_ID Event ID
94
-     * @throws EE_Error
95
-     * @throws RuntimeException
96
-     */
97
-    public function set_event($EVT_ID = 0)
98
-    {
99
-        $this->set('EVT_ID', $EVT_ID);
100
-    }
101
-
102
-
103
-    /**
104
-     * Overrides parent set() method so that all calls to set( 'REG_code', $REG_code ) OR set( 'STS_ID', $STS_ID ) can
105
-     * be routed to internal methods
106
-     *
107
-     * @param string $field_name
108
-     * @param mixed  $field_value
109
-     * @param bool   $use_default
110
-     * @throws EE_Error
111
-     * @throws EntityNotFoundException
112
-     * @throws InvalidArgumentException
113
-     * @throws InvalidDataTypeException
114
-     * @throws InvalidInterfaceException
115
-     * @throws ReflectionException
116
-     * @throws RuntimeException
117
-     */
118
-    public function set($field_name, $field_value, $use_default = false)
119
-    {
120
-        switch ($field_name) {
121
-            case 'REG_code':
122
-                if (! empty($field_value) && $this->reg_code() === null) {
123
-                    $this->set_reg_code($field_value, $use_default);
124
-                }
125
-                break;
126
-            case 'STS_ID':
127
-                $this->set_status($field_value, $use_default);
128
-                break;
129
-            default:
130
-                parent::set($field_name, $field_value, $use_default);
131
-        }
132
-    }
133
-
134
-
135
-    /**
136
-     * Set Status ID
137
-     * updates the registration status and ALSO...
138
-     * calls reserve_registration_space() if the reg status changes TO approved from any other reg status
139
-     * calls release_registration_space() if the reg status changes FROM approved to any other reg status
140
-     *
141
-     * @param string                $new_STS_ID
142
-     * @param boolean               $use_default
143
-     * @param ContextInterface|null $context
144
-     * @return bool
145
-     * @throws DomainException
146
-     * @throws EE_Error
147
-     * @throws EntityNotFoundException
148
-     * @throws InvalidArgumentException
149
-     * @throws InvalidDataTypeException
150
-     * @throws InvalidInterfaceException
151
-     * @throws ReflectionException
152
-     * @throws RuntimeException
153
-     * @throws UnexpectedEntityException
154
-     */
155
-    public function set_status($new_STS_ID = null, $use_default = false, ContextInterface $context = null)
156
-    {
157
-        // get current REG_Status
158
-        $old_STS_ID = $this->status_ID();
159
-        // if status has changed
160
-        if ($old_STS_ID !== $new_STS_ID // and that status has actually changed
161
-            && ! empty($old_STS_ID) // and that old status is actually set
162
-            && ! empty($new_STS_ID) // as well as the new status
163
-            && $this->ID() // ensure registration is in the db
164
-        ) {
165
-            // update internal status first
166
-            parent::set('STS_ID', $new_STS_ID, $use_default);
167
-            // THEN handle other changes that occur when reg status changes
168
-            // TO approved
169
-            if ($new_STS_ID === EEM_Registration::status_id_approved) {
170
-                // reserve a space by incrementing ticket and datetime sold values
171
-                $this->reserveRegistrationSpace();
172
-                do_action('AHEE__EE_Registration__set_status__to_approved', $this, $old_STS_ID, $new_STS_ID, $context);
173
-                // OR FROM  approved
174
-            } elseif ($old_STS_ID === EEM_Registration::status_id_approved) {
175
-                // release a space by decrementing ticket and datetime sold values
176
-                $this->releaseRegistrationSpace();
177
-                do_action(
178
-                    'AHEE__EE_Registration__set_status__from_approved',
179
-                    $this,
180
-                    $old_STS_ID,
181
-                    $new_STS_ID,
182
-                    $context
183
-                );
184
-            }
185
-            // update status
186
-            parent::set('STS_ID', $new_STS_ID, $use_default);
187
-            $this->updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, $context);
188
-            if ($this->statusChangeUpdatesTransaction($context)) {
189
-                $this->updateTransactionAfterStatusChange();
190
-            }
191
-            do_action('AHEE__EE_Registration__set_status__after_update', $this, $old_STS_ID, $new_STS_ID, $context);
192
-            return true;
193
-        }
194
-        // even though the old value matches the new value, it's still good to
195
-        // allow the parent set method to have a say
196
-        parent::set('STS_ID', $new_STS_ID, $use_default);
197
-        return true;
198
-    }
199
-
200
-
201
-    /**
202
-     * update REGs and TXN when cancelled or declined registrations involved
203
-     *
204
-     * @param string                $new_STS_ID
205
-     * @param string                $old_STS_ID
206
-     * @param ContextInterface|null $context
207
-     * @throws EE_Error
208
-     * @throws InvalidArgumentException
209
-     * @throws InvalidDataTypeException
210
-     * @throws InvalidInterfaceException
211
-     * @throws ReflectionException
212
-     * @throws RuntimeException
213
-     */
214
-    private function updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, ContextInterface $context = null)
215
-    {
216
-        // these reg statuses should not be considered in any calculations involving monies owing
217
-        $closed_reg_statuses = EEM_Registration::closed_reg_statuses();
218
-        // true if registration has been cancelled or declined
219
-        $this->updateIfCanceled(
220
-            $closed_reg_statuses,
221
-            $new_STS_ID,
222
-            $old_STS_ID,
223
-            $context
224
-        );
225
-        $this->updateIfReinstated(
226
-            $closed_reg_statuses,
227
-            $new_STS_ID,
228
-            $old_STS_ID,
229
-            $context
230
-        );
231
-    }
232
-
233
-
234
-    /**
235
-     * update REGs and TXN when cancelled or declined registrations involved
236
-     *
237
-     * @param array                 $closed_reg_statuses
238
-     * @param string                $new_STS_ID
239
-     * @param string                $old_STS_ID
240
-     * @param ContextInterface|null $context
241
-     * @throws EE_Error
242
-     * @throws InvalidArgumentException
243
-     * @throws InvalidDataTypeException
244
-     * @throws InvalidInterfaceException
245
-     * @throws ReflectionException
246
-     * @throws RuntimeException
247
-     */
248
-    private function updateIfCanceled(
249
-        array $closed_reg_statuses,
250
-        $new_STS_ID,
251
-        $old_STS_ID,
252
-        ContextInterface $context = null
253
-    ) {
254
-        // true if registration has been cancelled or declined
255
-        if (in_array($new_STS_ID, $closed_reg_statuses, true)
256
-            && ! in_array($old_STS_ID, $closed_reg_statuses, true)
257
-        ) {
258
-            /** @type EE_Registration_Processor $registration_processor */
259
-            $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
260
-            /** @type EE_Transaction_Processor $transaction_processor */
261
-            $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
262
-            // cancelled or declined registration
263
-            $registration_processor->update_registration_after_being_canceled_or_declined(
264
-                $this,
265
-                $closed_reg_statuses
266
-            );
267
-            $transaction_processor->update_transaction_after_canceled_or_declined_registration(
268
-                $this,
269
-                $closed_reg_statuses,
270
-                false
271
-            );
272
-            do_action(
273
-                'AHEE__EE_Registration__set_status__canceled_or_declined',
274
-                $this,
275
-                $old_STS_ID,
276
-                $new_STS_ID,
277
-                $context
278
-            );
279
-            return;
280
-        }
281
-    }
282
-
283
-
284
-    /**
285
-     * update REGs and TXN when cancelled or declined registrations involved
286
-     *
287
-     * @param array                 $closed_reg_statuses
288
-     * @param string                $new_STS_ID
289
-     * @param string                $old_STS_ID
290
-     * @param ContextInterface|null $context
291
-     * @throws EE_Error
292
-     * @throws InvalidArgumentException
293
-     * @throws InvalidDataTypeException
294
-     * @throws InvalidInterfaceException
295
-     * @throws ReflectionException
296
-     */
297
-    private function updateIfReinstated(
298
-        array $closed_reg_statuses,
299
-        $new_STS_ID,
300
-        $old_STS_ID,
301
-        ContextInterface $context = null
302
-    ) {
303
-        // true if reinstating cancelled or declined registration
304
-        if (in_array($old_STS_ID, $closed_reg_statuses, true)
305
-            && ! in_array($new_STS_ID, $closed_reg_statuses, true)
306
-        ) {
307
-            /** @type EE_Registration_Processor $registration_processor */
308
-            $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
309
-            /** @type EE_Transaction_Processor $transaction_processor */
310
-            $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
311
-            // reinstating cancelled or declined registration
312
-            $registration_processor->update_canceled_or_declined_registration_after_being_reinstated(
313
-                $this,
314
-                $closed_reg_statuses
315
-            );
316
-            $transaction_processor->update_transaction_after_reinstating_canceled_registration(
317
-                $this,
318
-                $closed_reg_statuses,
319
-                false
320
-            );
321
-            do_action(
322
-                'AHEE__EE_Registration__set_status__after_reinstated',
323
-                $this,
324
-                $old_STS_ID,
325
-                $new_STS_ID,
326
-                $context
327
-            );
328
-        }
329
-    }
330
-
331
-
332
-    /**
333
-     * @param ContextInterface|null $context
334
-     * @return bool
335
-     */
336
-    private function statusChangeUpdatesTransaction(ContextInterface $context = null)
337
-    {
338
-        $contexts_that_do_not_update_transaction = (array) apply_filters(
339
-            'AHEE__EE_Registration__statusChangeUpdatesTransaction__contexts_that_do_not_update_transaction',
340
-            array('spco_reg_step_attendee_information_process_registrations'),
341
-            $context,
342
-            $this
343
-        );
344
-        return ! (
345
-            $context instanceof ContextInterface
346
-            && in_array($context->slug(), $contexts_that_do_not_update_transaction, true)
347
-        );
348
-    }
349
-
350
-
351
-    /**
352
-     * @throws EE_Error
353
-     * @throws EntityNotFoundException
354
-     * @throws InvalidArgumentException
355
-     * @throws InvalidDataTypeException
356
-     * @throws InvalidInterfaceException
357
-     * @throws ReflectionException
358
-     * @throws RuntimeException
359
-     */
360
-    private function updateTransactionAfterStatusChange()
361
-    {
362
-        /** @type EE_Transaction_Payments $transaction_payments */
363
-        $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
364
-        $transaction_payments->recalculate_transaction_total($this->transaction(), false);
365
-        $this->transaction()->update_status_based_on_total_paid(true);
366
-    }
367
-
368
-
369
-    /**
370
-     *        get Status ID
371
-     */
372
-    public function status_ID()
373
-    {
374
-        return $this->get('STS_ID');
375
-    }
376
-
377
-
378
-    /**
379
-     * Gets the ticket this registration is for
380
-     *
381
-     * @param boolean $include_archived whether to include archived tickets or not.
382
-     *
383
-     * @return EE_Ticket|EE_Base_Class
384
-     * @throws EE_Error
385
-     */
386
-    public function ticket($include_archived = true)
387
-    {
388
-        $query_params = array();
389
-        if ($include_archived) {
390
-            $query_params['default_where_conditions'] = 'none';
391
-        }
392
-        return $this->get_first_related('Ticket', $query_params);
393
-    }
394
-
395
-
396
-    /**
397
-     * Gets the event this registration is for
398
-     *
399
-     * @return EE_Event
400
-     * @throws EE_Error
401
-     * @throws EntityNotFoundException
402
-     */
403
-    public function event()
404
-    {
405
-        $event = $this->get_first_related('Event');
406
-        if (! $event instanceof \EE_Event) {
407
-            throw new EntityNotFoundException('Event ID', $this->event_ID());
408
-        }
409
-        return $event;
410
-    }
411
-
412
-
413
-    /**
414
-     * Gets the "author" of the registration.  Note that for the purposes of registrations, the author will correspond
415
-     * with the author of the event this registration is for.
416
-     *
417
-     * @since 4.5.0
418
-     * @return int
419
-     * @throws EE_Error
420
-     * @throws EntityNotFoundException
421
-     */
422
-    public function wp_user()
423
-    {
424
-        $event = $this->event();
425
-        if ($event instanceof EE_Event) {
426
-            return $event->wp_user();
427
-        }
428
-        return 0;
429
-    }
430
-
431
-
432
-    /**
433
-     * increments this registration's related ticket sold and corresponding datetime sold values
434
-     *
435
-     * @return void
436
-     * @throws DomainException
437
-     * @throws EE_Error
438
-     * @throws EntityNotFoundException
439
-     * @throws InvalidArgumentException
440
-     * @throws InvalidDataTypeException
441
-     * @throws InvalidInterfaceException
442
-     * @throws ReflectionException
443
-     * @throws UnexpectedEntityException
444
-     */
445
-    private function reserveRegistrationSpace()
446
-    {
447
-        // reserved ticket and datetime counts will be decremented as sold counts are incremented
448
-        // so stop tracking that this reg has a ticket reserved
449
-        $this->release_reserved_ticket(false, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
450
-        $ticket = $this->ticket();
451
-        $ticket->increaseSold();
452
-        // possibly set event status to sold out
453
-        $this->event()->perform_sold_out_status_check();
454
-    }
455
-
456
-
457
-    /**
458
-     * decrements (subtracts) this registration's related ticket sold and corresponding datetime sold values
459
-     *
460
-     * @return void
461
-     * @throws DomainException
462
-     * @throws EE_Error
463
-     * @throws EntityNotFoundException
464
-     * @throws InvalidArgumentException
465
-     * @throws InvalidDataTypeException
466
-     * @throws InvalidInterfaceException
467
-     * @throws ReflectionException
468
-     * @throws UnexpectedEntityException
469
-     */
470
-    private function releaseRegistrationSpace()
471
-    {
472
-        $ticket = $this->ticket();
473
-        $ticket->decreaseSold();
474
-        // possibly change event status from sold out back to previous status
475
-        $this->event()->perform_sold_out_status_check();
476
-    }
477
-
478
-
479
-    /**
480
-     * tracks this registration's ticket reservation in extra meta
481
-     * and can increment related ticket reserved and corresponding datetime reserved values
482
-     *
483
-     * @param bool $update_ticket if true, will increment ticket and datetime reserved count
484
-     * @return void
485
-     * @throws EE_Error
486
-     * @throws InvalidArgumentException
487
-     * @throws InvalidDataTypeException
488
-     * @throws InvalidInterfaceException
489
-     * @throws ReflectionException
490
-     */
491
-    public function reserve_ticket($update_ticket = false, $source = 'unknown')
492
-    {
493
-        // only reserve ticket if space is not currently reserved
494
-        if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) !== true) {
495
-            $this->update_extra_meta('reserve_ticket', "{$this->ticket_ID()} from {$source}");
496
-            // IMPORTANT !!!
497
-            // although checking $update_ticket first would be more efficient,
498
-            // we NEED to ALWAYS call update_extra_meta(), which is why that is done first
499
-            if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true)
500
-                && $update_ticket
501
-            ) {
502
-                $ticket = $this->ticket();
503
-                $ticket->increaseReserved(1, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
504
-                $ticket->save();
505
-            }
506
-        }
507
-    }
508
-
509
-
510
-    /**
511
-     * stops tracking this registration's ticket reservation in extra meta
512
-     * decrements (subtracts) related ticket reserved and corresponding datetime reserved values
513
-     *
514
-     * @param bool $update_ticket if true, will decrement ticket and datetime reserved count
515
-     * @return void
516
-     * @throws EE_Error
517
-     * @throws InvalidArgumentException
518
-     * @throws InvalidDataTypeException
519
-     * @throws InvalidInterfaceException
520
-     * @throws ReflectionException
521
-     */
522
-    public function release_reserved_ticket($update_ticket = false, $source = 'unknown')
523
-    {
524
-        // only release ticket if space is currently reserved
525
-        if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) === true) {
526
-            $this->update_extra_meta('release_reserved_ticket', "{$this->ticket_ID()} from {$source}");
527
-            // IMPORTANT !!!
528
-            // although checking $update_ticket first would be more efficient,
529
-            // we NEED to ALWAYS call update_extra_meta(), which is why that is done first
530
-            if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, false)
531
-                && $update_ticket
532
-            ) {
533
-                $ticket = $this->ticket();
534
-                $ticket->decreaseReserved(1, true, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
535
-            }
536
-        }
537
-    }
538
-
539
-
540
-    /**
541
-     * Set Attendee ID
542
-     *
543
-     * @param        int $ATT_ID Attendee ID
544
-     * @throws EE_Error
545
-     * @throws RuntimeException
546
-     */
547
-    public function set_attendee_id($ATT_ID = 0)
548
-    {
549
-        $this->set('ATT_ID', $ATT_ID);
550
-    }
551
-
552
-
553
-    /**
554
-     *        Set Transaction ID
555
-     *
556
-     * @param        int $TXN_ID Transaction ID
557
-     * @throws EE_Error
558
-     * @throws RuntimeException
559
-     */
560
-    public function set_transaction_id($TXN_ID = 0)
561
-    {
562
-        $this->set('TXN_ID', $TXN_ID);
563
-    }
564
-
565
-
566
-    /**
567
-     *        Set Session
568
-     *
569
-     * @param    string $REG_session PHP Session ID
570
-     * @throws EE_Error
571
-     * @throws RuntimeException
572
-     */
573
-    public function set_session($REG_session = '')
574
-    {
575
-        $this->set('REG_session', $REG_session);
576
-    }
577
-
578
-
579
-    /**
580
-     *        Set Registration URL Link
581
-     *
582
-     * @param    string $REG_url_link Registration URL Link
583
-     * @throws EE_Error
584
-     * @throws RuntimeException
585
-     */
586
-    public function set_reg_url_link($REG_url_link = '')
587
-    {
588
-        $this->set('REG_url_link', $REG_url_link);
589
-    }
590
-
591
-
592
-    /**
593
-     *        Set Attendee Counter
594
-     *
595
-     * @param        int $REG_count Primary Attendee
596
-     * @throws EE_Error
597
-     * @throws RuntimeException
598
-     */
599
-    public function set_count($REG_count = 1)
600
-    {
601
-        $this->set('REG_count', $REG_count);
602
-    }
603
-
604
-
605
-    /**
606
-     *        Set Group Size
607
-     *
608
-     * @param        boolean $REG_group_size Group Registration
609
-     * @throws EE_Error
610
-     * @throws RuntimeException
611
-     */
612
-    public function set_group_size($REG_group_size = false)
613
-    {
614
-        $this->set('REG_group_size', $REG_group_size);
615
-    }
616
-
617
-
618
-    /**
619
-     *    is_not_approved -  convenience method that returns TRUE if REG status ID ==
620
-     *    EEM_Registration::status_id_not_approved
621
-     *
622
-     * @return        boolean
623
-     */
624
-    public function is_not_approved()
625
-    {
626
-        return $this->status_ID() == EEM_Registration::status_id_not_approved ? true : false;
627
-    }
628
-
629
-
630
-    /**
631
-     *    is_pending_payment -  convenience method that returns TRUE if REG status ID ==
632
-     *    EEM_Registration::status_id_pending_payment
633
-     *
634
-     * @return        boolean
635
-     */
636
-    public function is_pending_payment()
637
-    {
638
-        return $this->status_ID() == EEM_Registration::status_id_pending_payment ? true : false;
639
-    }
640
-
641
-
642
-    /**
643
-     *    is_approved -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_approved
644
-     *
645
-     * @return        boolean
646
-     */
647
-    public function is_approved()
648
-    {
649
-        return $this->status_ID() == EEM_Registration::status_id_approved ? true : false;
650
-    }
651
-
652
-
653
-    /**
654
-     *    is_cancelled -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_cancelled
655
-     *
656
-     * @return        boolean
657
-     */
658
-    public function is_cancelled()
659
-    {
660
-        return $this->status_ID() == EEM_Registration::status_id_cancelled ? true : false;
661
-    }
662
-
663
-
664
-    /**
665
-     *    is_declined -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_declined
666
-     *
667
-     * @return        boolean
668
-     */
669
-    public function is_declined()
670
-    {
671
-        return $this->status_ID() == EEM_Registration::status_id_declined ? true : false;
672
-    }
673
-
674
-
675
-    /**
676
-     *    is_incomplete -  convenience method that returns TRUE if REG status ID ==
677
-     *    EEM_Registration::status_id_incomplete
678
-     *
679
-     * @return        boolean
680
-     */
681
-    public function is_incomplete()
682
-    {
683
-        return $this->status_ID() == EEM_Registration::status_id_incomplete ? true : false;
684
-    }
685
-
686
-
687
-    /**
688
-     *        Set Registration Date
689
-     *
690
-     * @param        mixed ( int or string ) $REG_date Registration Date - Unix timestamp or string representation of
691
-     *                                                 Date
692
-     * @throws EE_Error
693
-     * @throws RuntimeException
694
-     */
695
-    public function set_reg_date($REG_date = false)
696
-    {
697
-        $this->set('REG_date', $REG_date);
698
-    }
699
-
700
-
701
-    /**
702
-     *    Set final price owing for this registration after all ticket/price modifications
703
-     *
704
-     * @access    public
705
-     * @param    float $REG_final_price
706
-     * @throws EE_Error
707
-     * @throws RuntimeException
708
-     */
709
-    public function set_final_price($REG_final_price = 0.00)
710
-    {
711
-        $this->set('REG_final_price', $REG_final_price);
712
-    }
713
-
714
-
715
-    /**
716
-     *    Set amount paid towards this registration's final price
717
-     *
718
-     * @access    public
719
-     * @param    float $REG_paid
720
-     * @throws EE_Error
721
-     * @throws RuntimeException
722
-     */
723
-    public function set_paid($REG_paid = 0.00)
724
-    {
725
-        $this->set('REG_paid', $REG_paid);
726
-    }
727
-
728
-
729
-    /**
730
-     *        Attendee Is Going
731
-     *
732
-     * @param        boolean $REG_att_is_going Attendee Is Going
733
-     * @throws EE_Error
734
-     * @throws RuntimeException
735
-     */
736
-    public function set_att_is_going($REG_att_is_going = false)
737
-    {
738
-        $this->set('REG_att_is_going', $REG_att_is_going);
739
-    }
740
-
741
-
742
-    /**
743
-     * Gets the related attendee
744
-     *
745
-     * @return EE_Attendee
746
-     * @throws EE_Error
747
-     */
748
-    public function attendee()
749
-    {
750
-        return $this->get_first_related('Attendee');
751
-    }
752
-
753
-
754
-    /**
755
-     *        get Event ID
756
-     */
757
-    public function event_ID()
758
-    {
759
-        return $this->get('EVT_ID');
760
-    }
761
-
762
-
763
-    /**
764
-     *        get Event ID
765
-     */
766
-    public function event_name()
767
-    {
768
-        $event = $this->event_obj();
769
-        if ($event) {
770
-            return $event->name();
771
-        } else {
772
-            return null;
773
-        }
774
-    }
775
-
776
-
777
-    /**
778
-     * Fetches the event this registration is for
779
-     *
780
-     * @return EE_Event
781
-     * @throws EE_Error
782
-     */
783
-    public function event_obj()
784
-    {
785
-        return $this->get_first_related('Event');
786
-    }
787
-
788
-
789
-    /**
790
-     *        get Attendee ID
791
-     */
792
-    public function attendee_ID()
793
-    {
794
-        return $this->get('ATT_ID');
795
-    }
796
-
797
-
798
-    /**
799
-     *        get PHP Session ID
800
-     */
801
-    public function session_ID()
802
-    {
803
-        return $this->get('REG_session');
804
-    }
805
-
806
-
807
-    /**
808
-     * Gets the string which represents the URL trigger for the receipt template in the message template system.
809
-     *
810
-     * @param string $messenger 'pdf' or 'html'.  Default 'html'.
811
-     * @return string
812
-     */
813
-    public function receipt_url($messenger = 'html')
814
-    {
815
-
816
-        /**
817
-         * The below will be deprecated one version after this.  We check first if there is a custom receipt template
818
-         * already in use on old system.  If there is then we just return the standard url for it.
819
-         *
820
-         * @since 4.5.0
821
-         */
822
-        $template_relative_path = 'modules/gateways/Invoice/lib/templates/receipt_body.template.php';
823
-        $has_custom = EEH_Template::locate_template(
824
-            $template_relative_path,
825
-            array(),
826
-            true,
827
-            true,
828
-            true
829
-        );
830
-
831
-        if ($has_custom) {
832
-            return add_query_arg(array('receipt' => 'true'), $this->invoice_url('launch'));
833
-        }
834
-        return apply_filters('FHEE__EE_Registration__receipt_url__receipt_url', '', $this, $messenger, 'receipt');
835
-    }
836
-
837
-
838
-    /**
839
-     * Gets the string which represents the URL trigger for the invoice template in the message template system.
840
-     *
841
-     * @param string $messenger 'pdf' or 'html'.  Default 'html'.
842
-     * @return string
843
-     * @throws EE_Error
844
-     */
845
-    public function invoice_url($messenger = 'html')
846
-    {
847
-        /**
848
-         * The below will be deprecated one version after this.  We check first if there is a custom invoice template
849
-         * already in use on old system.  If there is then we just return the standard url for it.
850
-         *
851
-         * @since 4.5.0
852
-         */
853
-        $template_relative_path = 'modules/gateways/Invoice/lib/templates/invoice_body.template.php';
854
-        $has_custom = EEH_Template::locate_template(
855
-            $template_relative_path,
856
-            array(),
857
-            true,
858
-            true,
859
-            true
860
-        );
861
-
862
-        if ($has_custom) {
863
-            if ($messenger == 'html') {
864
-                return $this->invoice_url('launch');
865
-            }
866
-            $route = $messenger == 'download' || $messenger == 'pdf' ? 'download_invoice' : 'launch_invoice';
867
-
868
-            $query_args = array('ee' => $route, 'id' => $this->reg_url_link());
869
-            if ($messenger == 'html') {
870
-                $query_args['html'] = true;
871
-            }
872
-            return add_query_arg($query_args, get_permalink(EE_Registry::instance()->CFG->core->thank_you_page_id));
873
-        }
874
-        return apply_filters('FHEE__EE_Registration__invoice_url__invoice_url', '', $this, $messenger, 'invoice');
875
-    }
876
-
877
-
878
-    /**
879
-     * get Registration URL Link
880
-     *
881
-     * @access public
882
-     * @return string
883
-     * @throws EE_Error
884
-     */
885
-    public function reg_url_link()
886
-    {
887
-        return (string) $this->get('REG_url_link');
888
-    }
889
-
890
-
891
-    /**
892
-     * Echoes out invoice_url()
893
-     *
894
-     * @param string $type 'download','launch', or 'html' (default is 'launch')
895
-     * @return void
896
-     * @throws EE_Error
897
-     */
898
-    public function e_invoice_url($type = 'launch')
899
-    {
900
-        echo $this->invoice_url($type);
901
-    }
902
-
903
-
904
-    /**
905
-     * Echoes out payment_overview_url
906
-     */
907
-    public function e_payment_overview_url()
908
-    {
909
-        echo $this->payment_overview_url();
910
-    }
911
-
912
-
913
-    /**
914
-     * Gets the URL for the checkout payment options reg step
915
-     * with this registration's REG_url_link added as a query parameter
916
-     *
917
-     * @param bool $clear_session Set to true when you want to clear the session on revisiting the
918
-     *                            payment overview url.
919
-     * @return string
920
-     * @throws InvalidInterfaceException
921
-     * @throws InvalidDataTypeException
922
-     * @throws EE_Error
923
-     * @throws InvalidArgumentException
924
-     */
925
-    public function payment_overview_url($clear_session = false)
926
-    {
927
-        return add_query_arg(
928
-            (array) apply_filters(
929
-                'FHEE__EE_Registration__payment_overview_url__query_args',
930
-                array(
931
-                    'e_reg_url_link' => $this->reg_url_link(),
932
-                    'step'           => 'payment_options',
933
-                    'revisit'        => true,
934
-                    'clear_session'  => (bool) $clear_session,
935
-                ),
936
-                $this
937
-            ),
938
-            EE_Registry::instance()->CFG->core->reg_page_url()
939
-        );
940
-    }
941
-
942
-
943
-    /**
944
-     * Gets the URL for the checkout attendee information reg step
945
-     * with this registration's REG_url_link added as a query parameter
946
-     *
947
-     * @return string
948
-     * @throws InvalidInterfaceException
949
-     * @throws InvalidDataTypeException
950
-     * @throws EE_Error
951
-     * @throws InvalidArgumentException
952
-     */
953
-    public function edit_attendee_information_url()
954
-    {
955
-        return add_query_arg(
956
-            (array) apply_filters(
957
-                'FHEE__EE_Registration__edit_attendee_information_url__query_args',
958
-                array(
959
-                    'e_reg_url_link' => $this->reg_url_link(),
960
-                    'step'           => 'attendee_information',
961
-                    'revisit'        => true,
962
-                ),
963
-                $this
964
-            ),
965
-            EE_Registry::instance()->CFG->core->reg_page_url()
966
-        );
967
-    }
968
-
969
-
970
-    /**
971
-     * Simply generates and returns the appropriate admin_url link to edit this registration
972
-     *
973
-     * @return string
974
-     * @throws EE_Error
975
-     */
976
-    public function get_admin_edit_url()
977
-    {
978
-        return EEH_URL::add_query_args_and_nonce(
979
-            array(
980
-                'page'    => 'espresso_registrations',
981
-                'action'  => 'view_registration',
982
-                '_REG_ID' => $this->ID(),
983
-            ),
984
-            admin_url('admin.php')
985
-        );
986
-    }
987
-
988
-
989
-    /**
990
-     *    is_primary_registrant?
991
-     */
992
-    public function is_primary_registrant()
993
-    {
994
-        return $this->get('REG_count') == 1 ? true : false;
995
-    }
996
-
997
-
998
-    /**
999
-     * This returns the primary registration object for this registration group (which may be this object).
1000
-     *
1001
-     * @return EE_Registration
1002
-     * @throws EE_Error
1003
-     */
1004
-    public function get_primary_registration()
1005
-    {
1006
-        if ($this->is_primary_registrant()) {
1007
-            return $this;
1008
-        }
1009
-
1010
-        // k reg_count !== 1 so let's get the EE_Registration object matching this txn_id and reg_count == 1
1011
-        /** @var EE_Registration $primary_registrant */
1012
-        $primary_registrant = EEM_Registration::instance()->get_one(
1013
-            array(
1014
-                array(
1015
-                    'TXN_ID'    => $this->transaction_ID(),
1016
-                    'REG_count' => 1,
1017
-                ),
1018
-            )
1019
-        );
1020
-        return $primary_registrant;
1021
-    }
1022
-
1023
-
1024
-    /**
1025
-     *        get  Attendee Number
1026
-     *
1027
-     * @access        public
1028
-     */
1029
-    public function count()
1030
-    {
1031
-        return $this->get('REG_count');
1032
-    }
1033
-
1034
-
1035
-    /**
1036
-     *        get Group Size
1037
-     */
1038
-    public function group_size()
1039
-    {
1040
-        return $this->get('REG_group_size');
1041
-    }
1042
-
1043
-
1044
-    /**
1045
-     *        get Registration Date
1046
-     */
1047
-    public function date()
1048
-    {
1049
-        return $this->get('REG_date');
1050
-    }
1051
-
1052
-
1053
-    /**
1054
-     * gets a pretty date
1055
-     *
1056
-     * @param string $date_format
1057
-     * @param string $time_format
1058
-     * @return string
1059
-     * @throws EE_Error
1060
-     */
1061
-    public function pretty_date($date_format = null, $time_format = null)
1062
-    {
1063
-        return $this->get_datetime('REG_date', $date_format, $time_format);
1064
-    }
1065
-
1066
-
1067
-    /**
1068
-     * final_price
1069
-     * the registration's share of the transaction total, so that the
1070
-     * sum of all the transaction's REG_final_prices equal the transaction's total
1071
-     *
1072
-     * @return float
1073
-     * @throws EE_Error
1074
-     */
1075
-    public function final_price()
1076
-    {
1077
-        return $this->get('REG_final_price');
1078
-    }
1079
-
1080
-
1081
-    /**
1082
-     * pretty_final_price
1083
-     *  final price as formatted string, with correct decimal places and currency symbol
1084
-     *
1085
-     * @return string
1086
-     * @throws EE_Error
1087
-     */
1088
-    public function pretty_final_price()
1089
-    {
1090
-        return $this->get_pretty('REG_final_price');
1091
-    }
1092
-
1093
-
1094
-    /**
1095
-     * get paid (yeah)
1096
-     *
1097
-     * @return float
1098
-     * @throws EE_Error
1099
-     */
1100
-    public function paid()
1101
-    {
1102
-        return $this->get('REG_paid');
1103
-    }
1104
-
1105
-
1106
-    /**
1107
-     * pretty_paid
1108
-     *
1109
-     * @return float
1110
-     * @throws EE_Error
1111
-     */
1112
-    public function pretty_paid()
1113
-    {
1114
-        return $this->get_pretty('REG_paid');
1115
-    }
1116
-
1117
-
1118
-    /**
1119
-     * owes_monies_and_can_pay
1120
-     * whether or not this registration has monies owing and it's' status allows payment
1121
-     *
1122
-     * @param array $requires_payment
1123
-     * @return bool
1124
-     * @throws EE_Error
1125
-     */
1126
-    public function owes_monies_and_can_pay($requires_payment = array())
1127
-    {
1128
-        // these reg statuses require payment (if event is not free)
1129
-        $requires_payment = ! empty($requires_payment)
1130
-            ? $requires_payment
1131
-            : EEM_Registration::reg_statuses_that_allow_payment();
1132
-        if (in_array($this->status_ID(), $requires_payment) &&
1133
-            $this->final_price() != 0 &&
1134
-            $this->final_price() != $this->paid()
1135
-        ) {
1136
-            return true;
1137
-        } else {
1138
-            return false;
1139
-        }
1140
-    }
1141
-
1142
-
1143
-    /**
1144
-     * Prints out the return value of $this->pretty_status()
1145
-     *
1146
-     * @param bool $show_icons
1147
-     * @return void
1148
-     * @throws EE_Error
1149
-     */
1150
-    public function e_pretty_status($show_icons = false)
1151
-    {
1152
-        echo $this->pretty_status($show_icons);
1153
-    }
1154
-
1155
-
1156
-    /**
1157
-     * Returns a nice version of the status for displaying to customers
1158
-     *
1159
-     * @param bool $show_icons
1160
-     * @return string
1161
-     * @throws EE_Error
1162
-     */
1163
-    public function pretty_status($show_icons = false)
1164
-    {
1165
-        $status = EEM_Status::instance()->localized_status(
1166
-            array($this->status_ID() => esc_html__('unknown', 'event_espresso')),
1167
-            false,
1168
-            'sentence'
1169
-        );
1170
-        $icon = '';
1171
-        switch ($this->status_ID()) {
1172
-            case EEM_Registration::status_id_approved:
1173
-                $icon = $show_icons
1174
-                    ? '<span class="dashicons dashicons-star-filled ee-icon-size-16 green-text"></span>'
1175
-                    : '';
1176
-                break;
1177
-            case EEM_Registration::status_id_pending_payment:
1178
-                $icon = $show_icons
1179
-                    ? '<span class="dashicons dashicons-star-half ee-icon-size-16 orange-text"></span>'
1180
-                    : '';
1181
-                break;
1182
-            case EEM_Registration::status_id_not_approved:
1183
-                $icon = $show_icons
1184
-                    ? '<span class="dashicons dashicons-marker ee-icon-size-16 orange-text"></span>'
1185
-                    : '';
1186
-                break;
1187
-            case EEM_Registration::status_id_cancelled:
1188
-                $icon = $show_icons
1189
-                    ? '<span class="dashicons dashicons-no ee-icon-size-16 lt-grey-text"></span>'
1190
-                    : '';
1191
-                break;
1192
-            case EEM_Registration::status_id_incomplete:
1193
-                $icon = $show_icons
1194
-                    ? '<span class="dashicons dashicons-no ee-icon-size-16 lt-orange-text"></span>'
1195
-                    : '';
1196
-                break;
1197
-            case EEM_Registration::status_id_declined:
1198
-                $icon = $show_icons
1199
-                    ? '<span class="dashicons dashicons-no ee-icon-size-16 red-text"></span>'
1200
-                    : '';
1201
-                break;
1202
-            case EEM_Registration::status_id_wait_list:
1203
-                $icon = $show_icons
1204
-                    ? '<span class="dashicons dashicons-clipboard ee-icon-size-16 purple-text"></span>'
1205
-                    : '';
1206
-                break;
1207
-        }
1208
-        return $icon . $status[ $this->status_ID() ];
1209
-    }
1210
-
1211
-
1212
-    /**
1213
-     *        get Attendee Is Going
1214
-     */
1215
-    public function att_is_going()
1216
-    {
1217
-        return $this->get('REG_att_is_going');
1218
-    }
1219
-
1220
-
1221
-    /**
1222
-     * Gets related answers
1223
-     *
1224
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1225
-     * @return EE_Answer[]
1226
-     * @throws EE_Error
1227
-     */
1228
-    public function answers($query_params = null)
1229
-    {
1230
-        return $this->get_many_related('Answer', $query_params);
1231
-    }
1232
-
1233
-
1234
-    /**
1235
-     * Gets the registration's answer value to the specified question
1236
-     * (either the question's ID or a question object)
1237
-     *
1238
-     * @param EE_Question|int $question
1239
-     * @param bool            $pretty_value
1240
-     * @return array|string if pretty_value= true, the result will always be a string
1241
-     * (because the answer might be an array of answer values, so passing pretty_value=true
1242
-     * will convert it into some kind of string)
1243
-     * @throws EE_Error
1244
-     */
1245
-    public function answer_value_to_question($question, $pretty_value = true)
1246
-    {
1247
-        $question_id = EEM_Question::instance()->ensure_is_ID($question);
1248
-        return EEM_Answer::instance()->get_answer_value_to_question($this, $question_id, $pretty_value);
1249
-    }
1250
-
1251
-
1252
-    /**
1253
-     * question_groups
1254
-     * returns an array of EE_Question_Group objects for this registration
1255
-     *
1256
-     * @return EE_Question_Group[]
1257
-     * @throws EE_Error
1258
-     * @throws EntityNotFoundException
1259
-     */
1260
-    public function question_groups()
1261
-    {
1262
-        $question_groups = array();
1263
-        if ($this->event() instanceof EE_Event) {
1264
-            $question_groups = $this->event()->question_groups(
1265
-                array(
1266
-                    array(
1267
-                        'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1268
-                    ),
1269
-                    'order_by' => array('QSG_order' => 'ASC'),
1270
-                )
1271
-            );
1272
-        }
1273
-        return $question_groups;
1274
-    }
1275
-
1276
-
1277
-    /**
1278
-     * count_question_groups
1279
-     * returns a count of the number of EE_Question_Group objects for this registration
1280
-     *
1281
-     * @return int
1282
-     * @throws EE_Error
1283
-     * @throws EntityNotFoundException
1284
-     */
1285
-    public function count_question_groups()
1286
-    {
1287
-        $qg_count = 0;
1288
-        if ($this->event() instanceof EE_Event) {
1289
-            $qg_count = $this->event()->count_related(
1290
-                'Question_Group',
1291
-                array(
1292
-                    array(
1293
-                        'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1294
-                    ),
1295
-                )
1296
-            );
1297
-        }
1298
-        return $qg_count;
1299
-    }
1300
-
1301
-
1302
-    /**
1303
-     * Returns the registration date in the 'standard' string format
1304
-     * (function may be improved in the future to allow for different formats and timezones)
1305
-     *
1306
-     * @return string
1307
-     * @throws EE_Error
1308
-     */
1309
-    public function reg_date()
1310
-    {
1311
-        return $this->get_datetime('REG_date');
1312
-    }
1313
-
1314
-
1315
-    /**
1316
-     * Gets the datetime-ticket for this registration (ie, it can be used to isolate
1317
-     * the ticket this registration purchased, or the datetime they have registered
1318
-     * to attend)
1319
-     *
1320
-     * @return EE_Datetime_Ticket
1321
-     * @throws EE_Error
1322
-     */
1323
-    public function datetime_ticket()
1324
-    {
1325
-        return $this->get_first_related('Datetime_Ticket');
1326
-    }
1327
-
1328
-
1329
-    /**
1330
-     * Sets the registration's datetime_ticket.
1331
-     *
1332
-     * @param EE_Datetime_Ticket $datetime_ticket
1333
-     * @return EE_Datetime_Ticket
1334
-     * @throws EE_Error
1335
-     */
1336
-    public function set_datetime_ticket($datetime_ticket)
1337
-    {
1338
-        return $this->_add_relation_to($datetime_ticket, 'Datetime_Ticket');
1339
-    }
1340
-
1341
-    /**
1342
-     * Gets deleted
1343
-     *
1344
-     * @return bool
1345
-     * @throws EE_Error
1346
-     */
1347
-    public function deleted()
1348
-    {
1349
-        return $this->get('REG_deleted');
1350
-    }
1351
-
1352
-    /**
1353
-     * Sets deleted
1354
-     *
1355
-     * @param boolean $deleted
1356
-     * @return bool
1357
-     * @throws EE_Error
1358
-     * @throws RuntimeException
1359
-     */
1360
-    public function set_deleted($deleted)
1361
-    {
1362
-        if ($deleted) {
1363
-            $this->delete();
1364
-        } else {
1365
-            $this->restore();
1366
-        }
1367
-    }
1368
-
1369
-
1370
-    /**
1371
-     * Get the status object of this object
1372
-     *
1373
-     * @return EE_Status
1374
-     * @throws EE_Error
1375
-     */
1376
-    public function status_obj()
1377
-    {
1378
-        return $this->get_first_related('Status');
1379
-    }
1380
-
1381
-
1382
-    /**
1383
-     * Returns the number of times this registration has checked into any of the datetimes
1384
-     * its available for
1385
-     *
1386
-     * @return int
1387
-     * @throws EE_Error
1388
-     */
1389
-    public function count_checkins()
1390
-    {
1391
-        return $this->get_model()->count_related($this, 'Checkin');
1392
-    }
1393
-
1394
-
1395
-    /**
1396
-     * Returns the number of current Check-ins this registration is checked into for any of the datetimes the
1397
-     * registration is for.  Note, this is ONLY checked in (does not include checkedout)
1398
-     *
1399
-     * @return int
1400
-     * @throws EE_Error
1401
-     */
1402
-    public function count_checkins_not_checkedout()
1403
-    {
1404
-        return $this->get_model()->count_related($this, 'Checkin', array(array('CHK_in' => 1)));
1405
-    }
1406
-
1407
-
1408
-    /**
1409
-     * The purpose of this method is simply to check whether this registration can checkin to the given datetime.
1410
-     *
1411
-     * @param int | EE_Datetime $DTT_OR_ID      The datetime the registration is being checked against
1412
-     * @param bool              $check_approved This is used to indicate whether the caller wants can_checkin to also
1413
-     *                                          consider registration status as well as datetime access.
1414
-     * @return bool
1415
-     * @throws EE_Error
1416
-     */
1417
-    public function can_checkin($DTT_OR_ID, $check_approved = true)
1418
-    {
1419
-        $DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1420
-
1421
-        // first check registration status
1422
-        if (($check_approved && ! $this->is_approved()) || ! $DTT_ID) {
1423
-            return false;
1424
-        }
1425
-        // is there a datetime ticket that matches this dtt_ID?
1426
-        if (! (EEM_Datetime_Ticket::instance()->exists(
1427
-            array(
1428
-                array(
1429
-                    'TKT_ID' => $this->get('TKT_ID'),
1430
-                    'DTT_ID' => $DTT_ID,
1431
-                ),
1432
-            )
1433
-        ))
1434
-        ) {
1435
-            return false;
1436
-        }
1437
-
1438
-        // final check is against TKT_uses
1439
-        return $this->verify_can_checkin_against_TKT_uses($DTT_ID);
1440
-    }
1441
-
1442
-
1443
-    /**
1444
-     * This method verifies whether the user can checkin for the given datetime considering the max uses value set on
1445
-     * the ticket. To do this,  a query is done to get the count of the datetime records already checked into.  If the
1446
-     * datetime given does not have a check-in record and checking in for that datetime will exceed the allowed uses,
1447
-     * then return false.  Otherwise return true.
1448
-     *
1449
-     * @param int | EE_Datetime $DTT_OR_ID The datetime the registration is being checked against
1450
-     * @return bool true means can checkin.  false means cannot checkin.
1451
-     * @throws EE_Error
1452
-     */
1453
-    public function verify_can_checkin_against_TKT_uses($DTT_OR_ID)
1454
-    {
1455
-        $DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1456
-
1457
-        if (! $DTT_ID) {
1458
-            return false;
1459
-        }
1460
-
1461
-        $max_uses = $this->ticket() instanceof EE_Ticket ? $this->ticket()->uses() : EE_INF;
1462
-
1463
-        // if max uses is not set or equals infinity then return true cause its not a factor for whether user can
1464
-        // check-in or not.
1465
-        if (! $max_uses || $max_uses === EE_INF) {
1466
-            return true;
1467
-        }
1468
-
1469
-        // does this datetime have a checkin record?  If so, then the dtt count has already been verified so we can just
1470
-        // go ahead and toggle.
1471
-        if (EEM_Checkin::instance()->exists(array(array('REG_ID' => $this->ID(), 'DTT_ID' => $DTT_ID)))) {
1472
-            return true;
1473
-        }
1474
-
1475
-        // made it here so the last check is whether the number of checkins per unique datetime on this registration
1476
-        // disallows further check-ins.
1477
-        $count_unique_dtt_checkins = EEM_Checkin::instance()->count(
1478
-            array(
1479
-                array(
1480
-                    'REG_ID' => $this->ID(),
1481
-                    'CHK_in' => true,
1482
-                ),
1483
-            ),
1484
-            'DTT_ID',
1485
-            true
1486
-        );
1487
-        // checkins have already reached their max number of uses
1488
-        // so registrant can NOT checkin
1489
-        if ($count_unique_dtt_checkins >= $max_uses) {
1490
-            EE_Error::add_error(
1491
-                esc_html__(
1492
-                    'Check-in denied because number of datetime uses for the ticket has been reached or exceeded.',
1493
-                    'event_espresso'
1494
-                ),
1495
-                __FILE__,
1496
-                __FUNCTION__,
1497
-                __LINE__
1498
-            );
1499
-            return false;
1500
-        }
1501
-        return true;
1502
-    }
1503
-
1504
-
1505
-    /**
1506
-     * toggle Check-in status for this registration
1507
-     * Check-ins are toggled in the following order:
1508
-     * never checked in -> checked in
1509
-     * checked in -> checked out
1510
-     * checked out -> checked in
1511
-     *
1512
-     * @param  int $DTT_ID  include specific datetime to toggle Check-in for.
1513
-     *                      If not included or null, then it is assumed latest datetime is being toggled.
1514
-     * @param bool $verify  If true then can_checkin() is used to verify whether the person
1515
-     *                      can be checked in or not.  Otherwise this forces change in checkin status.
1516
-     * @return bool|int     the chk_in status toggled to OR false if nothing got changed.
1517
-     * @throws EE_Error
1518
-     */
1519
-    public function toggle_checkin_status($DTT_ID = null, $verify = false)
1520
-    {
1521
-        if (empty($DTT_ID)) {
1522
-            $datetime = $this->get_latest_related_datetime();
1523
-            $DTT_ID = $datetime instanceof EE_Datetime ? $datetime->ID() : 0;
1524
-            // verify the registration can checkin for the given DTT_ID
1525
-        } elseif (! $this->can_checkin($DTT_ID, $verify)) {
1526
-            EE_Error::add_error(
1527
-                sprintf(
1528
-                    esc_html__(
1529
-                        'The given registration (ID:%1$d) can not be checked in to the given DTT_ID (%2$d), because the registration does not have access',
1530
-                        'event_espresso'
1531
-                    ),
1532
-                    $this->ID(),
1533
-                    $DTT_ID
1534
-                ),
1535
-                __FILE__,
1536
-                __FUNCTION__,
1537
-                __LINE__
1538
-            );
1539
-            return false;
1540
-        }
1541
-        $status_paths = array(
1542
-            EE_Checkin::status_checked_never => EE_Checkin::status_checked_in,
1543
-            EE_Checkin::status_checked_in    => EE_Checkin::status_checked_out,
1544
-            EE_Checkin::status_checked_out   => EE_Checkin::status_checked_in,
1545
-        );
1546
-        // start by getting the current status so we know what status we'll be changing to.
1547
-        $cur_status = $this->check_in_status_for_datetime($DTT_ID, null);
1548
-        $status_to = $status_paths[ $cur_status ];
1549
-        // database only records true for checked IN or false for checked OUT
1550
-        // no record ( null ) means checked in NEVER, but we obviously don't save that
1551
-        $new_status = $status_to === EE_Checkin::status_checked_in ? true : false;
1552
-        // add relation - note Check-ins are always creating new rows
1553
-        // because we are keeping track of Check-ins over time.
1554
-        // Eventually we'll probably want to show a list table
1555
-        // for the individual Check-ins so that they can be managed.
1556
-        $checkin = EE_Checkin::new_instance(
1557
-            array(
1558
-                'REG_ID' => $this->ID(),
1559
-                'DTT_ID' => $DTT_ID,
1560
-                'CHK_in' => $new_status,
1561
-            )
1562
-        );
1563
-        // if the record could not be saved then return false
1564
-        if ($checkin->save() === 0) {
1565
-            if (WP_DEBUG) {
1566
-                global $wpdb;
1567
-                $error = sprintf(
1568
-                    esc_html__(
1569
-                        'Registration check in update failed because of the following database error: %1$s%2$s',
1570
-                        'event_espresso'
1571
-                    ),
1572
-                    '<br />',
1573
-                    $wpdb->last_error
1574
-                );
1575
-            } else {
1576
-                $error = esc_html__(
1577
-                    'Registration check in update failed because of an unknown database error',
1578
-                    'event_espresso'
1579
-                );
1580
-            }
1581
-            EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__);
1582
-            return false;
1583
-        }
1584
-        return $status_to;
1585
-    }
1586
-
1587
-
1588
-    /**
1589
-     * Returns the latest datetime related to this registration (via the ticket attached to the registration).
1590
-     * "Latest" is defined by the `DTT_EVT_start` column.
1591
-     *
1592
-     * @return EE_Datetime|null
1593
-     * @throws EE_Error
1594
-     */
1595
-    public function get_latest_related_datetime()
1596
-    {
1597
-        return EEM_Datetime::instance()->get_one(
1598
-            array(
1599
-                array(
1600
-                    'Ticket.Registration.REG_ID' => $this->ID(),
1601
-                ),
1602
-                'order_by' => array('DTT_EVT_start' => 'DESC'),
1603
-            )
1604
-        );
1605
-    }
1606
-
1607
-
1608
-    /**
1609
-     * Returns the earliest datetime related to this registration (via the ticket attached to the registration).
1610
-     * "Earliest" is defined by the `DTT_EVT_start` column.
1611
-     *
1612
-     * @throws EE_Error
1613
-     */
1614
-    public function get_earliest_related_datetime()
1615
-    {
1616
-        return EEM_Datetime::instance()->get_one(
1617
-            array(
1618
-                array(
1619
-                    'Ticket.Registration.REG_ID' => $this->ID(),
1620
-                ),
1621
-                'order_by' => array('DTT_EVT_start' => 'ASC'),
1622
-            )
1623
-        );
1624
-    }
1625
-
1626
-
1627
-    /**
1628
-     * This method simply returns the check-in status for this registration and the given datetime.
1629
-     * If neither the datetime nor the checkin values are provided as arguments,
1630
-     * then this will return the LATEST check-in status for the registration across all datetimes it belongs to.
1631
-     *
1632
-     * @param  int       $DTT_ID  The ID of the datetime we're checking against
1633
-     *                            (if empty we'll get the primary datetime for
1634
-     *                            this registration (via event) and use it's ID);
1635
-     * @param EE_Checkin $checkin If present, we use the given checkin object rather than the dtt_id.
1636
-     *
1637
-     * @return int                Integer representing Check-in status.
1638
-     * @throws EE_Error
1639
-     */
1640
-    public function check_in_status_for_datetime($DTT_ID = 0, $checkin = null)
1641
-    {
1642
-        $checkin_query_params = array(
1643
-            'order_by' => array('CHK_timestamp' => 'DESC'),
1644
-        );
1645
-
1646
-        if ($DTT_ID > 0) {
1647
-            $checkin_query_params[0] = array('DTT_ID' => $DTT_ID);
1648
-        }
1649
-
1650
-        // get checkin object (if exists)
1651
-        $checkin = $checkin instanceof EE_Checkin
1652
-            ? $checkin
1653
-            : $this->get_first_related('Checkin', $checkin_query_params);
1654
-        if ($checkin instanceof EE_Checkin) {
1655
-            if ($checkin->get('CHK_in')) {
1656
-                return EE_Checkin::status_checked_in; // checked in
1657
-            }
1658
-            return EE_Checkin::status_checked_out; // had checked in but is now checked out.
1659
-        }
1660
-        return EE_Checkin::status_checked_never; // never been checked in
1661
-    }
1662
-
1663
-
1664
-    /**
1665
-     * This method returns a localized message for the toggled Check-in message.
1666
-     *
1667
-     * @param  int $DTT_ID include specific datetime to get the correct Check-in message.  If not included or null,
1668
-     *                     then it is assumed Check-in for primary datetime was toggled.
1669
-     * @param bool $error  This just flags that you want an error message returned. This is put in so that the error
1670
-     *                     message can be customized with the attendee name.
1671
-     * @return string internationalized message
1672
-     * @throws EE_Error
1673
-     */
1674
-    public function get_checkin_msg($DTT_ID, $error = false)
1675
-    {
1676
-        // let's get the attendee first so we can include the name of the attendee
1677
-        $attendee = $this->get_first_related('Attendee');
1678
-        if ($attendee instanceof EE_Attendee) {
1679
-            if ($error) {
1680
-                return sprintf(__("%s's check-in status was not changed.", "event_espresso"), $attendee->full_name());
1681
-            }
1682
-            $cur_status = $this->check_in_status_for_datetime($DTT_ID);
1683
-            // what is the status message going to be?
1684
-            switch ($cur_status) {
1685
-                case EE_Checkin::status_checked_never:
1686
-                    return sprintf(
1687
-                        __("%s has been removed from Check-in records", "event_espresso"),
1688
-                        $attendee->full_name()
1689
-                    );
1690
-                    break;
1691
-                case EE_Checkin::status_checked_in:
1692
-                    return sprintf(__('%s has been checked in', 'event_espresso'), $attendee->full_name());
1693
-                    break;
1694
-                case EE_Checkin::status_checked_out:
1695
-                    return sprintf(__('%s has been checked out', 'event_espresso'), $attendee->full_name());
1696
-                    break;
1697
-            }
1698
-        }
1699
-        return esc_html__("The check-in status could not be determined.", "event_espresso");
1700
-    }
1701
-
1702
-
1703
-    /**
1704
-     * Returns the related EE_Transaction to this registration
1705
-     *
1706
-     * @return EE_Transaction
1707
-     * @throws EE_Error
1708
-     * @throws EntityNotFoundException
1709
-     */
1710
-    public function transaction()
1711
-    {
1712
-        $transaction = $this->get_first_related('Transaction');
1713
-        if (! $transaction instanceof \EE_Transaction) {
1714
-            throw new EntityNotFoundException('Transaction ID', $this->transaction_ID());
1715
-        }
1716
-        return $transaction;
1717
-    }
1718
-
1719
-
1720
-    /**
1721
-     *        get Registration Code
1722
-     */
1723
-    public function reg_code()
1724
-    {
1725
-        return $this->get('REG_code');
1726
-    }
1727
-
1728
-
1729
-    /**
1730
-     *        get Transaction ID
1731
-     */
1732
-    public function transaction_ID()
1733
-    {
1734
-        return $this->get('TXN_ID');
1735
-    }
1736
-
1737
-
1738
-    /**
1739
-     * @return int
1740
-     * @throws EE_Error
1741
-     */
1742
-    public function ticket_ID()
1743
-    {
1744
-        return $this->get('TKT_ID');
1745
-    }
1746
-
1747
-
1748
-    /**
1749
-     *        Set Registration Code
1750
-     *
1751
-     * @access    public
1752
-     * @param    string  $REG_code Registration Code
1753
-     * @param    boolean $use_default
1754
-     * @throws EE_Error
1755
-     */
1756
-    public function set_reg_code($REG_code, $use_default = false)
1757
-    {
1758
-        if (empty($REG_code)) {
1759
-            EE_Error::add_error(
1760
-                esc_html__('REG_code can not be empty.', 'event_espresso'),
1761
-                __FILE__,
1762
-                __FUNCTION__,
1763
-                __LINE__
1764
-            );
1765
-            return;
1766
-        }
1767
-        if (! $this->reg_code()) {
1768
-            parent::set('REG_code', $REG_code, $use_default);
1769
-        } else {
1770
-            EE_Error::doing_it_wrong(
1771
-                __CLASS__ . '::' . __FUNCTION__,
1772
-                esc_html__('Can not change a registration REG_code once it has been set.', 'event_espresso'),
1773
-                '4.6.0'
1774
-            );
1775
-        }
1776
-    }
1777
-
1778
-
1779
-    /**
1780
-     * Returns all other registrations in the same group as this registrant who have the same ticket option.
1781
-     * Note, if you want to just get all registrations in the same transaction (group), use:
1782
-     *    $registration->transaction()->registrations();
1783
-     *
1784
-     * @since 4.5.0
1785
-     * @return EE_Registration[] or empty array if this isn't a group registration.
1786
-     * @throws EE_Error
1787
-     */
1788
-    public function get_all_other_registrations_in_group()
1789
-    {
1790
-        if ($this->group_size() < 2) {
1791
-            return array();
1792
-        }
1793
-
1794
-        $query[0] = array(
1795
-            'TXN_ID' => $this->transaction_ID(),
1796
-            'REG_ID' => array('!=', $this->ID()),
1797
-            'TKT_ID' => $this->ticket_ID(),
1798
-        );
1799
-        /** @var EE_Registration[] $registrations */
1800
-        $registrations = $this->get_model()->get_all($query);
1801
-        return $registrations;
1802
-    }
1803
-
1804
-    /**
1805
-     * Return the link to the admin details for the object.
1806
-     *
1807
-     * @return string
1808
-     * @throws EE_Error
1809
-     */
1810
-    public function get_admin_details_link()
1811
-    {
1812
-        EE_Registry::instance()->load_helper('URL');
1813
-        return EEH_URL::add_query_args_and_nonce(
1814
-            array(
1815
-                'page'    => 'espresso_registrations',
1816
-                'action'  => 'view_registration',
1817
-                '_REG_ID' => $this->ID(),
1818
-            ),
1819
-            admin_url('admin.php')
1820
-        );
1821
-    }
1822
-
1823
-    /**
1824
-     * Returns the link to the editor for the object.  Sometimes this is the same as the details.
1825
-     *
1826
-     * @return string
1827
-     * @throws EE_Error
1828
-     */
1829
-    public function get_admin_edit_link()
1830
-    {
1831
-        return $this->get_admin_details_link();
1832
-    }
1833
-
1834
-    /**
1835
-     * Returns the link to a settings page for the object.
1836
-     *
1837
-     * @return string
1838
-     * @throws EE_Error
1839
-     */
1840
-    public function get_admin_settings_link()
1841
-    {
1842
-        return $this->get_admin_details_link();
1843
-    }
1844
-
1845
-    /**
1846
-     * Returns the link to the "overview" for the object (typically the "list table" view).
1847
-     *
1848
-     * @return string
1849
-     */
1850
-    public function get_admin_overview_link()
1851
-    {
1852
-        EE_Registry::instance()->load_helper('URL');
1853
-        return EEH_URL::add_query_args_and_nonce(
1854
-            array(
1855
-                'page' => 'espresso_registrations',
1856
-            ),
1857
-            admin_url('admin.php')
1858
-        );
1859
-    }
1860
-
1861
-
1862
-    /**
1863
-     * @param array $query_params
1864
-     *
1865
-     * @return \EE_Registration[]
1866
-     * @throws EE_Error
1867
-     */
1868
-    public function payments($query_params = array())
1869
-    {
1870
-        return $this->get_many_related('Payment', $query_params);
1871
-    }
1872
-
1873
-
1874
-    /**
1875
-     * @param array $query_params
1876
-     *
1877
-     * @return \EE_Registration_Payment[]
1878
-     * @throws EE_Error
1879
-     */
1880
-    public function registration_payments($query_params = array())
1881
-    {
1882
-        return $this->get_many_related('Registration_Payment', $query_params);
1883
-    }
1884
-
1885
-
1886
-    /**
1887
-     * This grabs the payment method corresponding to the last payment made for the amount owing on the registration.
1888
-     * Note: if there are no payments on the registration there will be no payment method returned.
1889
-     *
1890
-     * @return EE_Payment_Method|null
1891
-     */
1892
-    public function payment_method()
1893
-    {
1894
-        return EEM_Payment_Method::instance()->get_last_used_for_registration($this);
1895
-    }
1896
-
1897
-
1898
-    /**
1899
-     * @return \EE_Line_Item
1900
-     * @throws EntityNotFoundException
1901
-     * @throws EE_Error
1902
-     */
1903
-    public function ticket_line_item()
1904
-    {
1905
-        $ticket = $this->ticket();
1906
-        $transaction = $this->transaction();
1907
-        $line_item = null;
1908
-        $ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs(
1909
-            $transaction->total_line_item(),
1910
-            'Ticket',
1911
-            array($ticket->ID())
1912
-        );
1913
-        foreach ($ticket_line_items as $ticket_line_item) {
1914
-            if ($ticket_line_item instanceof \EE_Line_Item
1915
-                && $ticket_line_item->OBJ_type() === 'Ticket'
1916
-                && $ticket_line_item->OBJ_ID() === $ticket->ID()
1917
-            ) {
1918
-                $line_item = $ticket_line_item;
1919
-                break;
1920
-            }
1921
-        }
1922
-        if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) {
1923
-            throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID());
1924
-        }
1925
-        return $line_item;
1926
-    }
1927
-
1928
-
1929
-    /**
1930
-     * Soft Deletes this model object.
1931
-     *
1932
-     * @return boolean | int
1933
-     * @throws RuntimeException
1934
-     * @throws EE_Error
1935
-     */
1936
-    public function delete()
1937
-    {
1938
-        if ($this->update_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY, $this->status_ID()) === true) {
1939
-            $this->set_status(EEM_Registration::status_id_cancelled);
1940
-        }
1941
-        return parent::delete();
1942
-    }
1943
-
1944
-
1945
-    /**
1946
-     * Restores whatever the previous status was on a registration before it was trashed (if possible)
1947
-     *
1948
-     * @throws EE_Error
1949
-     * @throws RuntimeException
1950
-     */
1951
-    public function restore()
1952
-    {
1953
-        $previous_status = $this->get_extra_meta(
1954
-            EE_Registration::PRE_TRASH_REG_STATUS_KEY,
1955
-            true,
1956
-            EEM_Registration::status_id_cancelled
1957
-        );
1958
-        if ($previous_status) {
1959
-            $this->delete_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY);
1960
-            $this->set_status($previous_status);
1961
-        }
1962
-        return parent::restore();
1963
-    }
1964
-
1965
-
1966
-    /**
1967
-     * possibly toggle Registration status based on comparison of REG_paid vs REG_final_price
1968
-     *
1969
-     * @param  boolean $trigger_set_status_logic EE_Registration::set_status() can trigger additional logic
1970
-     *                                           depending on whether the reg status changes to or from "Approved"
1971
-     * @return boolean whether the Registration status was updated
1972
-     * @throws EE_Error
1973
-     * @throws RuntimeException
1974
-     */
1975
-    public function updateStatusBasedOnTotalPaid($trigger_set_status_logic = true)
1976
-    {
1977
-        $paid = $this->paid();
1978
-        $price = $this->final_price();
1979
-        switch (true) {
1980
-            // overpaid or paid
1981
-            case EEH_Money::compare_floats($paid, $price, '>'):
1982
-            case EEH_Money::compare_floats($paid, $price):
1983
-                $new_status = EEM_Registration::status_id_approved;
1984
-                break;
1985
-            //  underpaid
1986
-            case EEH_Money::compare_floats($paid, $price, '<'):
1987
-                $new_status = EEM_Registration::status_id_pending_payment;
1988
-                break;
1989
-            // uhhh Houston...
1990
-            default:
1991
-                throw new RuntimeException(
1992
-                    esc_html__('The total paid calculation for this registration is inaccurate.', 'event_espresso')
1993
-                );
1994
-        }
1995
-        if ($new_status !== $this->status_ID()) {
1996
-            if ($trigger_set_status_logic) {
1997
-                return $this->set_status($new_status);
1998
-            }
1999
-            parent::set('STS_ID', $new_status);
2000
-            return true;
2001
-        }
2002
-        return false;
2003
-    }
2004
-
2005
-
2006
-    /*************************** DEPRECATED ***************************/
2007
-
2008
-
2009
-    /**
2010
-     * @deprecated
2011
-     * @since     4.7.0
2012
-     * @access    public
2013
-     */
2014
-    public function price_paid()
2015
-    {
2016
-        EE_Error::doing_it_wrong(
2017
-            'EE_Registration::price_paid()',
2018
-            esc_html__(
2019
-                'This method is deprecated, please use EE_Registration::final_price() instead.',
2020
-                'event_espresso'
2021
-            ),
2022
-            '4.7.0'
2023
-        );
2024
-        return $this->final_price();
2025
-    }
2026
-
2027
-
2028
-    /**
2029
-     * @deprecated
2030
-     * @since     4.7.0
2031
-     * @access    public
2032
-     * @param    float $REG_final_price
2033
-     * @throws EE_Error
2034
-     * @throws RuntimeException
2035
-     */
2036
-    public function set_price_paid($REG_final_price = 0.00)
2037
-    {
2038
-        EE_Error::doing_it_wrong(
2039
-            'EE_Registration::set_price_paid()',
2040
-            esc_html__(
2041
-                'This method is deprecated, please use EE_Registration::set_final_price() instead.',
2042
-                'event_espresso'
2043
-            ),
2044
-            '4.7.0'
2045
-        );
2046
-        $this->set_final_price($REG_final_price);
2047
-    }
2048
-
2049
-
2050
-    /**
2051
-     * @deprecated
2052
-     * @since 4.7.0
2053
-     * @return string
2054
-     * @throws EE_Error
2055
-     */
2056
-    public function pretty_price_paid()
2057
-    {
2058
-        EE_Error::doing_it_wrong(
2059
-            'EE_Registration::pretty_price_paid()',
2060
-            esc_html__(
2061
-                'This method is deprecated, please use EE_Registration::pretty_final_price() instead.',
2062
-                'event_espresso'
2063
-            ),
2064
-            '4.7.0'
2065
-        );
2066
-        return $this->pretty_final_price();
2067
-    }
2068
-
2069
-
2070
-    /**
2071
-     * Gets the primary datetime related to this registration via the related Event to this registration
2072
-     *
2073
-     * @deprecated 4.9.17
2074
-     * @return EE_Datetime
2075
-     * @throws EE_Error
2076
-     * @throws EntityNotFoundException
2077
-     */
2078
-    public function get_related_primary_datetime()
2079
-    {
2080
-        EE_Error::doing_it_wrong(
2081
-            __METHOD__,
2082
-            esc_html__(
2083
-                'Use EE_Registration::get_latest_related_datetime() or EE_Registration::get_earliest_related_datetime()',
2084
-                'event_espresso'
2085
-            ),
2086
-            '4.9.17',
2087
-            '5.0.0'
2088
-        );
2089
-        return $this->event()->primary_datetime();
2090
-    }
20
+	/**
21
+	 * Used to reference when a registration has never been checked in.
22
+	 *
23
+	 * @deprecated use \EE_Checkin::status_checked_never instead
24
+	 * @type int
25
+	 */
26
+	const checkin_status_never = 2;
27
+
28
+	/**
29
+	 * Used to reference when a registration has been checked in.
30
+	 *
31
+	 * @deprecated use \EE_Checkin::status_checked_in instead
32
+	 * @type int
33
+	 */
34
+	const checkin_status_in = 1;
35
+
36
+
37
+	/**
38
+	 * Used to reference when a registration has been checked out.
39
+	 *
40
+	 * @deprecated use \EE_Checkin::status_checked_out instead
41
+	 * @type int
42
+	 */
43
+	const checkin_status_out = 0;
44
+
45
+
46
+	/**
47
+	 * extra meta key for tracking reg status os trashed registrations
48
+	 *
49
+	 * @type string
50
+	 */
51
+	const PRE_TRASH_REG_STATUS_KEY = 'pre_trash_registration_status';
52
+
53
+
54
+	/**
55
+	 * extra meta key for tracking if registration has reserved ticket
56
+	 *
57
+	 * @type string
58
+	 */
59
+	const HAS_RESERVED_TICKET_KEY = 'has_reserved_ticket';
60
+
61
+
62
+	/**
63
+	 * @param array  $props_n_values          incoming values
64
+	 * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
65
+	 *                                        used.)
66
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
67
+	 *                                        date_format and the second value is the time format
68
+	 * @return EE_Registration
69
+	 * @throws EE_Error
70
+	 */
71
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
72
+	{
73
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
74
+		return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
75
+	}
76
+
77
+
78
+	/**
79
+	 * @param array  $props_n_values  incoming values from the database
80
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
81
+	 *                                the website will be used.
82
+	 * @return EE_Registration
83
+	 */
84
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
85
+	{
86
+		return new self($props_n_values, true, $timezone);
87
+	}
88
+
89
+
90
+	/**
91
+	 *        Set Event ID
92
+	 *
93
+	 * @param        int $EVT_ID Event ID
94
+	 * @throws EE_Error
95
+	 * @throws RuntimeException
96
+	 */
97
+	public function set_event($EVT_ID = 0)
98
+	{
99
+		$this->set('EVT_ID', $EVT_ID);
100
+	}
101
+
102
+
103
+	/**
104
+	 * Overrides parent set() method so that all calls to set( 'REG_code', $REG_code ) OR set( 'STS_ID', $STS_ID ) can
105
+	 * be routed to internal methods
106
+	 *
107
+	 * @param string $field_name
108
+	 * @param mixed  $field_value
109
+	 * @param bool   $use_default
110
+	 * @throws EE_Error
111
+	 * @throws EntityNotFoundException
112
+	 * @throws InvalidArgumentException
113
+	 * @throws InvalidDataTypeException
114
+	 * @throws InvalidInterfaceException
115
+	 * @throws ReflectionException
116
+	 * @throws RuntimeException
117
+	 */
118
+	public function set($field_name, $field_value, $use_default = false)
119
+	{
120
+		switch ($field_name) {
121
+			case 'REG_code':
122
+				if (! empty($field_value) && $this->reg_code() === null) {
123
+					$this->set_reg_code($field_value, $use_default);
124
+				}
125
+				break;
126
+			case 'STS_ID':
127
+				$this->set_status($field_value, $use_default);
128
+				break;
129
+			default:
130
+				parent::set($field_name, $field_value, $use_default);
131
+		}
132
+	}
133
+
134
+
135
+	/**
136
+	 * Set Status ID
137
+	 * updates the registration status and ALSO...
138
+	 * calls reserve_registration_space() if the reg status changes TO approved from any other reg status
139
+	 * calls release_registration_space() if the reg status changes FROM approved to any other reg status
140
+	 *
141
+	 * @param string                $new_STS_ID
142
+	 * @param boolean               $use_default
143
+	 * @param ContextInterface|null $context
144
+	 * @return bool
145
+	 * @throws DomainException
146
+	 * @throws EE_Error
147
+	 * @throws EntityNotFoundException
148
+	 * @throws InvalidArgumentException
149
+	 * @throws InvalidDataTypeException
150
+	 * @throws InvalidInterfaceException
151
+	 * @throws ReflectionException
152
+	 * @throws RuntimeException
153
+	 * @throws UnexpectedEntityException
154
+	 */
155
+	public function set_status($new_STS_ID = null, $use_default = false, ContextInterface $context = null)
156
+	{
157
+		// get current REG_Status
158
+		$old_STS_ID = $this->status_ID();
159
+		// if status has changed
160
+		if ($old_STS_ID !== $new_STS_ID // and that status has actually changed
161
+			&& ! empty($old_STS_ID) // and that old status is actually set
162
+			&& ! empty($new_STS_ID) // as well as the new status
163
+			&& $this->ID() // ensure registration is in the db
164
+		) {
165
+			// update internal status first
166
+			parent::set('STS_ID', $new_STS_ID, $use_default);
167
+			// THEN handle other changes that occur when reg status changes
168
+			// TO approved
169
+			if ($new_STS_ID === EEM_Registration::status_id_approved) {
170
+				// reserve a space by incrementing ticket and datetime sold values
171
+				$this->reserveRegistrationSpace();
172
+				do_action('AHEE__EE_Registration__set_status__to_approved', $this, $old_STS_ID, $new_STS_ID, $context);
173
+				// OR FROM  approved
174
+			} elseif ($old_STS_ID === EEM_Registration::status_id_approved) {
175
+				// release a space by decrementing ticket and datetime sold values
176
+				$this->releaseRegistrationSpace();
177
+				do_action(
178
+					'AHEE__EE_Registration__set_status__from_approved',
179
+					$this,
180
+					$old_STS_ID,
181
+					$new_STS_ID,
182
+					$context
183
+				);
184
+			}
185
+			// update status
186
+			parent::set('STS_ID', $new_STS_ID, $use_default);
187
+			$this->updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, $context);
188
+			if ($this->statusChangeUpdatesTransaction($context)) {
189
+				$this->updateTransactionAfterStatusChange();
190
+			}
191
+			do_action('AHEE__EE_Registration__set_status__after_update', $this, $old_STS_ID, $new_STS_ID, $context);
192
+			return true;
193
+		}
194
+		// even though the old value matches the new value, it's still good to
195
+		// allow the parent set method to have a say
196
+		parent::set('STS_ID', $new_STS_ID, $use_default);
197
+		return true;
198
+	}
199
+
200
+
201
+	/**
202
+	 * update REGs and TXN when cancelled or declined registrations involved
203
+	 *
204
+	 * @param string                $new_STS_ID
205
+	 * @param string                $old_STS_ID
206
+	 * @param ContextInterface|null $context
207
+	 * @throws EE_Error
208
+	 * @throws InvalidArgumentException
209
+	 * @throws InvalidDataTypeException
210
+	 * @throws InvalidInterfaceException
211
+	 * @throws ReflectionException
212
+	 * @throws RuntimeException
213
+	 */
214
+	private function updateIfCanceledOrReinstated($new_STS_ID, $old_STS_ID, ContextInterface $context = null)
215
+	{
216
+		// these reg statuses should not be considered in any calculations involving monies owing
217
+		$closed_reg_statuses = EEM_Registration::closed_reg_statuses();
218
+		// true if registration has been cancelled or declined
219
+		$this->updateIfCanceled(
220
+			$closed_reg_statuses,
221
+			$new_STS_ID,
222
+			$old_STS_ID,
223
+			$context
224
+		);
225
+		$this->updateIfReinstated(
226
+			$closed_reg_statuses,
227
+			$new_STS_ID,
228
+			$old_STS_ID,
229
+			$context
230
+		);
231
+	}
232
+
233
+
234
+	/**
235
+	 * update REGs and TXN when cancelled or declined registrations involved
236
+	 *
237
+	 * @param array                 $closed_reg_statuses
238
+	 * @param string                $new_STS_ID
239
+	 * @param string                $old_STS_ID
240
+	 * @param ContextInterface|null $context
241
+	 * @throws EE_Error
242
+	 * @throws InvalidArgumentException
243
+	 * @throws InvalidDataTypeException
244
+	 * @throws InvalidInterfaceException
245
+	 * @throws ReflectionException
246
+	 * @throws RuntimeException
247
+	 */
248
+	private function updateIfCanceled(
249
+		array $closed_reg_statuses,
250
+		$new_STS_ID,
251
+		$old_STS_ID,
252
+		ContextInterface $context = null
253
+	) {
254
+		// true if registration has been cancelled or declined
255
+		if (in_array($new_STS_ID, $closed_reg_statuses, true)
256
+			&& ! in_array($old_STS_ID, $closed_reg_statuses, true)
257
+		) {
258
+			/** @type EE_Registration_Processor $registration_processor */
259
+			$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
260
+			/** @type EE_Transaction_Processor $transaction_processor */
261
+			$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
262
+			// cancelled or declined registration
263
+			$registration_processor->update_registration_after_being_canceled_or_declined(
264
+				$this,
265
+				$closed_reg_statuses
266
+			);
267
+			$transaction_processor->update_transaction_after_canceled_or_declined_registration(
268
+				$this,
269
+				$closed_reg_statuses,
270
+				false
271
+			);
272
+			do_action(
273
+				'AHEE__EE_Registration__set_status__canceled_or_declined',
274
+				$this,
275
+				$old_STS_ID,
276
+				$new_STS_ID,
277
+				$context
278
+			);
279
+			return;
280
+		}
281
+	}
282
+
283
+
284
+	/**
285
+	 * update REGs and TXN when cancelled or declined registrations involved
286
+	 *
287
+	 * @param array                 $closed_reg_statuses
288
+	 * @param string                $new_STS_ID
289
+	 * @param string                $old_STS_ID
290
+	 * @param ContextInterface|null $context
291
+	 * @throws EE_Error
292
+	 * @throws InvalidArgumentException
293
+	 * @throws InvalidDataTypeException
294
+	 * @throws InvalidInterfaceException
295
+	 * @throws ReflectionException
296
+	 */
297
+	private function updateIfReinstated(
298
+		array $closed_reg_statuses,
299
+		$new_STS_ID,
300
+		$old_STS_ID,
301
+		ContextInterface $context = null
302
+	) {
303
+		// true if reinstating cancelled or declined registration
304
+		if (in_array($old_STS_ID, $closed_reg_statuses, true)
305
+			&& ! in_array($new_STS_ID, $closed_reg_statuses, true)
306
+		) {
307
+			/** @type EE_Registration_Processor $registration_processor */
308
+			$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
309
+			/** @type EE_Transaction_Processor $transaction_processor */
310
+			$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
311
+			// reinstating cancelled or declined registration
312
+			$registration_processor->update_canceled_or_declined_registration_after_being_reinstated(
313
+				$this,
314
+				$closed_reg_statuses
315
+			);
316
+			$transaction_processor->update_transaction_after_reinstating_canceled_registration(
317
+				$this,
318
+				$closed_reg_statuses,
319
+				false
320
+			);
321
+			do_action(
322
+				'AHEE__EE_Registration__set_status__after_reinstated',
323
+				$this,
324
+				$old_STS_ID,
325
+				$new_STS_ID,
326
+				$context
327
+			);
328
+		}
329
+	}
330
+
331
+
332
+	/**
333
+	 * @param ContextInterface|null $context
334
+	 * @return bool
335
+	 */
336
+	private function statusChangeUpdatesTransaction(ContextInterface $context = null)
337
+	{
338
+		$contexts_that_do_not_update_transaction = (array) apply_filters(
339
+			'AHEE__EE_Registration__statusChangeUpdatesTransaction__contexts_that_do_not_update_transaction',
340
+			array('spco_reg_step_attendee_information_process_registrations'),
341
+			$context,
342
+			$this
343
+		);
344
+		return ! (
345
+			$context instanceof ContextInterface
346
+			&& in_array($context->slug(), $contexts_that_do_not_update_transaction, true)
347
+		);
348
+	}
349
+
350
+
351
+	/**
352
+	 * @throws EE_Error
353
+	 * @throws EntityNotFoundException
354
+	 * @throws InvalidArgumentException
355
+	 * @throws InvalidDataTypeException
356
+	 * @throws InvalidInterfaceException
357
+	 * @throws ReflectionException
358
+	 * @throws RuntimeException
359
+	 */
360
+	private function updateTransactionAfterStatusChange()
361
+	{
362
+		/** @type EE_Transaction_Payments $transaction_payments */
363
+		$transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
364
+		$transaction_payments->recalculate_transaction_total($this->transaction(), false);
365
+		$this->transaction()->update_status_based_on_total_paid(true);
366
+	}
367
+
368
+
369
+	/**
370
+	 *        get Status ID
371
+	 */
372
+	public function status_ID()
373
+	{
374
+		return $this->get('STS_ID');
375
+	}
376
+
377
+
378
+	/**
379
+	 * Gets the ticket this registration is for
380
+	 *
381
+	 * @param boolean $include_archived whether to include archived tickets or not.
382
+	 *
383
+	 * @return EE_Ticket|EE_Base_Class
384
+	 * @throws EE_Error
385
+	 */
386
+	public function ticket($include_archived = true)
387
+	{
388
+		$query_params = array();
389
+		if ($include_archived) {
390
+			$query_params['default_where_conditions'] = 'none';
391
+		}
392
+		return $this->get_first_related('Ticket', $query_params);
393
+	}
394
+
395
+
396
+	/**
397
+	 * Gets the event this registration is for
398
+	 *
399
+	 * @return EE_Event
400
+	 * @throws EE_Error
401
+	 * @throws EntityNotFoundException
402
+	 */
403
+	public function event()
404
+	{
405
+		$event = $this->get_first_related('Event');
406
+		if (! $event instanceof \EE_Event) {
407
+			throw new EntityNotFoundException('Event ID', $this->event_ID());
408
+		}
409
+		return $event;
410
+	}
411
+
412
+
413
+	/**
414
+	 * Gets the "author" of the registration.  Note that for the purposes of registrations, the author will correspond
415
+	 * with the author of the event this registration is for.
416
+	 *
417
+	 * @since 4.5.0
418
+	 * @return int
419
+	 * @throws EE_Error
420
+	 * @throws EntityNotFoundException
421
+	 */
422
+	public function wp_user()
423
+	{
424
+		$event = $this->event();
425
+		if ($event instanceof EE_Event) {
426
+			return $event->wp_user();
427
+		}
428
+		return 0;
429
+	}
430
+
431
+
432
+	/**
433
+	 * increments this registration's related ticket sold and corresponding datetime sold values
434
+	 *
435
+	 * @return void
436
+	 * @throws DomainException
437
+	 * @throws EE_Error
438
+	 * @throws EntityNotFoundException
439
+	 * @throws InvalidArgumentException
440
+	 * @throws InvalidDataTypeException
441
+	 * @throws InvalidInterfaceException
442
+	 * @throws ReflectionException
443
+	 * @throws UnexpectedEntityException
444
+	 */
445
+	private function reserveRegistrationSpace()
446
+	{
447
+		// reserved ticket and datetime counts will be decremented as sold counts are incremented
448
+		// so stop tracking that this reg has a ticket reserved
449
+		$this->release_reserved_ticket(false, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
450
+		$ticket = $this->ticket();
451
+		$ticket->increaseSold();
452
+		// possibly set event status to sold out
453
+		$this->event()->perform_sold_out_status_check();
454
+	}
455
+
456
+
457
+	/**
458
+	 * decrements (subtracts) this registration's related ticket sold and corresponding datetime sold values
459
+	 *
460
+	 * @return void
461
+	 * @throws DomainException
462
+	 * @throws EE_Error
463
+	 * @throws EntityNotFoundException
464
+	 * @throws InvalidArgumentException
465
+	 * @throws InvalidDataTypeException
466
+	 * @throws InvalidInterfaceException
467
+	 * @throws ReflectionException
468
+	 * @throws UnexpectedEntityException
469
+	 */
470
+	private function releaseRegistrationSpace()
471
+	{
472
+		$ticket = $this->ticket();
473
+		$ticket->decreaseSold();
474
+		// possibly change event status from sold out back to previous status
475
+		$this->event()->perform_sold_out_status_check();
476
+	}
477
+
478
+
479
+	/**
480
+	 * tracks this registration's ticket reservation in extra meta
481
+	 * and can increment related ticket reserved and corresponding datetime reserved values
482
+	 *
483
+	 * @param bool $update_ticket if true, will increment ticket and datetime reserved count
484
+	 * @return void
485
+	 * @throws EE_Error
486
+	 * @throws InvalidArgumentException
487
+	 * @throws InvalidDataTypeException
488
+	 * @throws InvalidInterfaceException
489
+	 * @throws ReflectionException
490
+	 */
491
+	public function reserve_ticket($update_ticket = false, $source = 'unknown')
492
+	{
493
+		// only reserve ticket if space is not currently reserved
494
+		if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) !== true) {
495
+			$this->update_extra_meta('reserve_ticket', "{$this->ticket_ID()} from {$source}");
496
+			// IMPORTANT !!!
497
+			// although checking $update_ticket first would be more efficient,
498
+			// we NEED to ALWAYS call update_extra_meta(), which is why that is done first
499
+			if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true)
500
+				&& $update_ticket
501
+			) {
502
+				$ticket = $this->ticket();
503
+				$ticket->increaseReserved(1, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
504
+				$ticket->save();
505
+			}
506
+		}
507
+	}
508
+
509
+
510
+	/**
511
+	 * stops tracking this registration's ticket reservation in extra meta
512
+	 * decrements (subtracts) related ticket reserved and corresponding datetime reserved values
513
+	 *
514
+	 * @param bool $update_ticket if true, will decrement ticket and datetime reserved count
515
+	 * @return void
516
+	 * @throws EE_Error
517
+	 * @throws InvalidArgumentException
518
+	 * @throws InvalidDataTypeException
519
+	 * @throws InvalidInterfaceException
520
+	 * @throws ReflectionException
521
+	 */
522
+	public function release_reserved_ticket($update_ticket = false, $source = 'unknown')
523
+	{
524
+		// only release ticket if space is currently reserved
525
+		if ((bool) $this->get_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, true) === true) {
526
+			$this->update_extra_meta('release_reserved_ticket', "{$this->ticket_ID()} from {$source}");
527
+			// IMPORTANT !!!
528
+			// although checking $update_ticket first would be more efficient,
529
+			// we NEED to ALWAYS call update_extra_meta(), which is why that is done first
530
+			if ($this->update_extra_meta(EE_Registration::HAS_RESERVED_TICKET_KEY, false)
531
+				&& $update_ticket
532
+			) {
533
+				$ticket = $this->ticket();
534
+				$ticket->decreaseReserved(1, true, "REG: {$this->ID()} (ln:" . __LINE__ . ')');
535
+			}
536
+		}
537
+	}
538
+
539
+
540
+	/**
541
+	 * Set Attendee ID
542
+	 *
543
+	 * @param        int $ATT_ID Attendee ID
544
+	 * @throws EE_Error
545
+	 * @throws RuntimeException
546
+	 */
547
+	public function set_attendee_id($ATT_ID = 0)
548
+	{
549
+		$this->set('ATT_ID', $ATT_ID);
550
+	}
551
+
552
+
553
+	/**
554
+	 *        Set Transaction ID
555
+	 *
556
+	 * @param        int $TXN_ID Transaction ID
557
+	 * @throws EE_Error
558
+	 * @throws RuntimeException
559
+	 */
560
+	public function set_transaction_id($TXN_ID = 0)
561
+	{
562
+		$this->set('TXN_ID', $TXN_ID);
563
+	}
564
+
565
+
566
+	/**
567
+	 *        Set Session
568
+	 *
569
+	 * @param    string $REG_session PHP Session ID
570
+	 * @throws EE_Error
571
+	 * @throws RuntimeException
572
+	 */
573
+	public function set_session($REG_session = '')
574
+	{
575
+		$this->set('REG_session', $REG_session);
576
+	}
577
+
578
+
579
+	/**
580
+	 *        Set Registration URL Link
581
+	 *
582
+	 * @param    string $REG_url_link Registration URL Link
583
+	 * @throws EE_Error
584
+	 * @throws RuntimeException
585
+	 */
586
+	public function set_reg_url_link($REG_url_link = '')
587
+	{
588
+		$this->set('REG_url_link', $REG_url_link);
589
+	}
590
+
591
+
592
+	/**
593
+	 *        Set Attendee Counter
594
+	 *
595
+	 * @param        int $REG_count Primary Attendee
596
+	 * @throws EE_Error
597
+	 * @throws RuntimeException
598
+	 */
599
+	public function set_count($REG_count = 1)
600
+	{
601
+		$this->set('REG_count', $REG_count);
602
+	}
603
+
604
+
605
+	/**
606
+	 *        Set Group Size
607
+	 *
608
+	 * @param        boolean $REG_group_size Group Registration
609
+	 * @throws EE_Error
610
+	 * @throws RuntimeException
611
+	 */
612
+	public function set_group_size($REG_group_size = false)
613
+	{
614
+		$this->set('REG_group_size', $REG_group_size);
615
+	}
616
+
617
+
618
+	/**
619
+	 *    is_not_approved -  convenience method that returns TRUE if REG status ID ==
620
+	 *    EEM_Registration::status_id_not_approved
621
+	 *
622
+	 * @return        boolean
623
+	 */
624
+	public function is_not_approved()
625
+	{
626
+		return $this->status_ID() == EEM_Registration::status_id_not_approved ? true : false;
627
+	}
628
+
629
+
630
+	/**
631
+	 *    is_pending_payment -  convenience method that returns TRUE if REG status ID ==
632
+	 *    EEM_Registration::status_id_pending_payment
633
+	 *
634
+	 * @return        boolean
635
+	 */
636
+	public function is_pending_payment()
637
+	{
638
+		return $this->status_ID() == EEM_Registration::status_id_pending_payment ? true : false;
639
+	}
640
+
641
+
642
+	/**
643
+	 *    is_approved -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_approved
644
+	 *
645
+	 * @return        boolean
646
+	 */
647
+	public function is_approved()
648
+	{
649
+		return $this->status_ID() == EEM_Registration::status_id_approved ? true : false;
650
+	}
651
+
652
+
653
+	/**
654
+	 *    is_cancelled -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_cancelled
655
+	 *
656
+	 * @return        boolean
657
+	 */
658
+	public function is_cancelled()
659
+	{
660
+		return $this->status_ID() == EEM_Registration::status_id_cancelled ? true : false;
661
+	}
662
+
663
+
664
+	/**
665
+	 *    is_declined -  convenience method that returns TRUE if REG status ID == EEM_Registration::status_id_declined
666
+	 *
667
+	 * @return        boolean
668
+	 */
669
+	public function is_declined()
670
+	{
671
+		return $this->status_ID() == EEM_Registration::status_id_declined ? true : false;
672
+	}
673
+
674
+
675
+	/**
676
+	 *    is_incomplete -  convenience method that returns TRUE if REG status ID ==
677
+	 *    EEM_Registration::status_id_incomplete
678
+	 *
679
+	 * @return        boolean
680
+	 */
681
+	public function is_incomplete()
682
+	{
683
+		return $this->status_ID() == EEM_Registration::status_id_incomplete ? true : false;
684
+	}
685
+
686
+
687
+	/**
688
+	 *        Set Registration Date
689
+	 *
690
+	 * @param        mixed ( int or string ) $REG_date Registration Date - Unix timestamp or string representation of
691
+	 *                                                 Date
692
+	 * @throws EE_Error
693
+	 * @throws RuntimeException
694
+	 */
695
+	public function set_reg_date($REG_date = false)
696
+	{
697
+		$this->set('REG_date', $REG_date);
698
+	}
699
+
700
+
701
+	/**
702
+	 *    Set final price owing for this registration after all ticket/price modifications
703
+	 *
704
+	 * @access    public
705
+	 * @param    float $REG_final_price
706
+	 * @throws EE_Error
707
+	 * @throws RuntimeException
708
+	 */
709
+	public function set_final_price($REG_final_price = 0.00)
710
+	{
711
+		$this->set('REG_final_price', $REG_final_price);
712
+	}
713
+
714
+
715
+	/**
716
+	 *    Set amount paid towards this registration's final price
717
+	 *
718
+	 * @access    public
719
+	 * @param    float $REG_paid
720
+	 * @throws EE_Error
721
+	 * @throws RuntimeException
722
+	 */
723
+	public function set_paid($REG_paid = 0.00)
724
+	{
725
+		$this->set('REG_paid', $REG_paid);
726
+	}
727
+
728
+
729
+	/**
730
+	 *        Attendee Is Going
731
+	 *
732
+	 * @param        boolean $REG_att_is_going Attendee Is Going
733
+	 * @throws EE_Error
734
+	 * @throws RuntimeException
735
+	 */
736
+	public function set_att_is_going($REG_att_is_going = false)
737
+	{
738
+		$this->set('REG_att_is_going', $REG_att_is_going);
739
+	}
740
+
741
+
742
+	/**
743
+	 * Gets the related attendee
744
+	 *
745
+	 * @return EE_Attendee
746
+	 * @throws EE_Error
747
+	 */
748
+	public function attendee()
749
+	{
750
+		return $this->get_first_related('Attendee');
751
+	}
752
+
753
+
754
+	/**
755
+	 *        get Event ID
756
+	 */
757
+	public function event_ID()
758
+	{
759
+		return $this->get('EVT_ID');
760
+	}
761
+
762
+
763
+	/**
764
+	 *        get Event ID
765
+	 */
766
+	public function event_name()
767
+	{
768
+		$event = $this->event_obj();
769
+		if ($event) {
770
+			return $event->name();
771
+		} else {
772
+			return null;
773
+		}
774
+	}
775
+
776
+
777
+	/**
778
+	 * Fetches the event this registration is for
779
+	 *
780
+	 * @return EE_Event
781
+	 * @throws EE_Error
782
+	 */
783
+	public function event_obj()
784
+	{
785
+		return $this->get_first_related('Event');
786
+	}
787
+
788
+
789
+	/**
790
+	 *        get Attendee ID
791
+	 */
792
+	public function attendee_ID()
793
+	{
794
+		return $this->get('ATT_ID');
795
+	}
796
+
797
+
798
+	/**
799
+	 *        get PHP Session ID
800
+	 */
801
+	public function session_ID()
802
+	{
803
+		return $this->get('REG_session');
804
+	}
805
+
806
+
807
+	/**
808
+	 * Gets the string which represents the URL trigger for the receipt template in the message template system.
809
+	 *
810
+	 * @param string $messenger 'pdf' or 'html'.  Default 'html'.
811
+	 * @return string
812
+	 */
813
+	public function receipt_url($messenger = 'html')
814
+	{
815
+
816
+		/**
817
+		 * The below will be deprecated one version after this.  We check first if there is a custom receipt template
818
+		 * already in use on old system.  If there is then we just return the standard url for it.
819
+		 *
820
+		 * @since 4.5.0
821
+		 */
822
+		$template_relative_path = 'modules/gateways/Invoice/lib/templates/receipt_body.template.php';
823
+		$has_custom = EEH_Template::locate_template(
824
+			$template_relative_path,
825
+			array(),
826
+			true,
827
+			true,
828
+			true
829
+		);
830
+
831
+		if ($has_custom) {
832
+			return add_query_arg(array('receipt' => 'true'), $this->invoice_url('launch'));
833
+		}
834
+		return apply_filters('FHEE__EE_Registration__receipt_url__receipt_url', '', $this, $messenger, 'receipt');
835
+	}
836
+
837
+
838
+	/**
839
+	 * Gets the string which represents the URL trigger for the invoice template in the message template system.
840
+	 *
841
+	 * @param string $messenger 'pdf' or 'html'.  Default 'html'.
842
+	 * @return string
843
+	 * @throws EE_Error
844
+	 */
845
+	public function invoice_url($messenger = 'html')
846
+	{
847
+		/**
848
+		 * The below will be deprecated one version after this.  We check first if there is a custom invoice template
849
+		 * already in use on old system.  If there is then we just return the standard url for it.
850
+		 *
851
+		 * @since 4.5.0
852
+		 */
853
+		$template_relative_path = 'modules/gateways/Invoice/lib/templates/invoice_body.template.php';
854
+		$has_custom = EEH_Template::locate_template(
855
+			$template_relative_path,
856
+			array(),
857
+			true,
858
+			true,
859
+			true
860
+		);
861
+
862
+		if ($has_custom) {
863
+			if ($messenger == 'html') {
864
+				return $this->invoice_url('launch');
865
+			}
866
+			$route = $messenger == 'download' || $messenger == 'pdf' ? 'download_invoice' : 'launch_invoice';
867
+
868
+			$query_args = array('ee' => $route, 'id' => $this->reg_url_link());
869
+			if ($messenger == 'html') {
870
+				$query_args['html'] = true;
871
+			}
872
+			return add_query_arg($query_args, get_permalink(EE_Registry::instance()->CFG->core->thank_you_page_id));
873
+		}
874
+		return apply_filters('FHEE__EE_Registration__invoice_url__invoice_url', '', $this, $messenger, 'invoice');
875
+	}
876
+
877
+
878
+	/**
879
+	 * get Registration URL Link
880
+	 *
881
+	 * @access public
882
+	 * @return string
883
+	 * @throws EE_Error
884
+	 */
885
+	public function reg_url_link()
886
+	{
887
+		return (string) $this->get('REG_url_link');
888
+	}
889
+
890
+
891
+	/**
892
+	 * Echoes out invoice_url()
893
+	 *
894
+	 * @param string $type 'download','launch', or 'html' (default is 'launch')
895
+	 * @return void
896
+	 * @throws EE_Error
897
+	 */
898
+	public function e_invoice_url($type = 'launch')
899
+	{
900
+		echo $this->invoice_url($type);
901
+	}
902
+
903
+
904
+	/**
905
+	 * Echoes out payment_overview_url
906
+	 */
907
+	public function e_payment_overview_url()
908
+	{
909
+		echo $this->payment_overview_url();
910
+	}
911
+
912
+
913
+	/**
914
+	 * Gets the URL for the checkout payment options reg step
915
+	 * with this registration's REG_url_link added as a query parameter
916
+	 *
917
+	 * @param bool $clear_session Set to true when you want to clear the session on revisiting the
918
+	 *                            payment overview url.
919
+	 * @return string
920
+	 * @throws InvalidInterfaceException
921
+	 * @throws InvalidDataTypeException
922
+	 * @throws EE_Error
923
+	 * @throws InvalidArgumentException
924
+	 */
925
+	public function payment_overview_url($clear_session = false)
926
+	{
927
+		return add_query_arg(
928
+			(array) apply_filters(
929
+				'FHEE__EE_Registration__payment_overview_url__query_args',
930
+				array(
931
+					'e_reg_url_link' => $this->reg_url_link(),
932
+					'step'           => 'payment_options',
933
+					'revisit'        => true,
934
+					'clear_session'  => (bool) $clear_session,
935
+				),
936
+				$this
937
+			),
938
+			EE_Registry::instance()->CFG->core->reg_page_url()
939
+		);
940
+	}
941
+
942
+
943
+	/**
944
+	 * Gets the URL for the checkout attendee information reg step
945
+	 * with this registration's REG_url_link added as a query parameter
946
+	 *
947
+	 * @return string
948
+	 * @throws InvalidInterfaceException
949
+	 * @throws InvalidDataTypeException
950
+	 * @throws EE_Error
951
+	 * @throws InvalidArgumentException
952
+	 */
953
+	public function edit_attendee_information_url()
954
+	{
955
+		return add_query_arg(
956
+			(array) apply_filters(
957
+				'FHEE__EE_Registration__edit_attendee_information_url__query_args',
958
+				array(
959
+					'e_reg_url_link' => $this->reg_url_link(),
960
+					'step'           => 'attendee_information',
961
+					'revisit'        => true,
962
+				),
963
+				$this
964
+			),
965
+			EE_Registry::instance()->CFG->core->reg_page_url()
966
+		);
967
+	}
968
+
969
+
970
+	/**
971
+	 * Simply generates and returns the appropriate admin_url link to edit this registration
972
+	 *
973
+	 * @return string
974
+	 * @throws EE_Error
975
+	 */
976
+	public function get_admin_edit_url()
977
+	{
978
+		return EEH_URL::add_query_args_and_nonce(
979
+			array(
980
+				'page'    => 'espresso_registrations',
981
+				'action'  => 'view_registration',
982
+				'_REG_ID' => $this->ID(),
983
+			),
984
+			admin_url('admin.php')
985
+		);
986
+	}
987
+
988
+
989
+	/**
990
+	 *    is_primary_registrant?
991
+	 */
992
+	public function is_primary_registrant()
993
+	{
994
+		return $this->get('REG_count') == 1 ? true : false;
995
+	}
996
+
997
+
998
+	/**
999
+	 * This returns the primary registration object for this registration group (which may be this object).
1000
+	 *
1001
+	 * @return EE_Registration
1002
+	 * @throws EE_Error
1003
+	 */
1004
+	public function get_primary_registration()
1005
+	{
1006
+		if ($this->is_primary_registrant()) {
1007
+			return $this;
1008
+		}
1009
+
1010
+		// k reg_count !== 1 so let's get the EE_Registration object matching this txn_id and reg_count == 1
1011
+		/** @var EE_Registration $primary_registrant */
1012
+		$primary_registrant = EEM_Registration::instance()->get_one(
1013
+			array(
1014
+				array(
1015
+					'TXN_ID'    => $this->transaction_ID(),
1016
+					'REG_count' => 1,
1017
+				),
1018
+			)
1019
+		);
1020
+		return $primary_registrant;
1021
+	}
1022
+
1023
+
1024
+	/**
1025
+	 *        get  Attendee Number
1026
+	 *
1027
+	 * @access        public
1028
+	 */
1029
+	public function count()
1030
+	{
1031
+		return $this->get('REG_count');
1032
+	}
1033
+
1034
+
1035
+	/**
1036
+	 *        get Group Size
1037
+	 */
1038
+	public function group_size()
1039
+	{
1040
+		return $this->get('REG_group_size');
1041
+	}
1042
+
1043
+
1044
+	/**
1045
+	 *        get Registration Date
1046
+	 */
1047
+	public function date()
1048
+	{
1049
+		return $this->get('REG_date');
1050
+	}
1051
+
1052
+
1053
+	/**
1054
+	 * gets a pretty date
1055
+	 *
1056
+	 * @param string $date_format
1057
+	 * @param string $time_format
1058
+	 * @return string
1059
+	 * @throws EE_Error
1060
+	 */
1061
+	public function pretty_date($date_format = null, $time_format = null)
1062
+	{
1063
+		return $this->get_datetime('REG_date', $date_format, $time_format);
1064
+	}
1065
+
1066
+
1067
+	/**
1068
+	 * final_price
1069
+	 * the registration's share of the transaction total, so that the
1070
+	 * sum of all the transaction's REG_final_prices equal the transaction's total
1071
+	 *
1072
+	 * @return float
1073
+	 * @throws EE_Error
1074
+	 */
1075
+	public function final_price()
1076
+	{
1077
+		return $this->get('REG_final_price');
1078
+	}
1079
+
1080
+
1081
+	/**
1082
+	 * pretty_final_price
1083
+	 *  final price as formatted string, with correct decimal places and currency symbol
1084
+	 *
1085
+	 * @return string
1086
+	 * @throws EE_Error
1087
+	 */
1088
+	public function pretty_final_price()
1089
+	{
1090
+		return $this->get_pretty('REG_final_price');
1091
+	}
1092
+
1093
+
1094
+	/**
1095
+	 * get paid (yeah)
1096
+	 *
1097
+	 * @return float
1098
+	 * @throws EE_Error
1099
+	 */
1100
+	public function paid()
1101
+	{
1102
+		return $this->get('REG_paid');
1103
+	}
1104
+
1105
+
1106
+	/**
1107
+	 * pretty_paid
1108
+	 *
1109
+	 * @return float
1110
+	 * @throws EE_Error
1111
+	 */
1112
+	public function pretty_paid()
1113
+	{
1114
+		return $this->get_pretty('REG_paid');
1115
+	}
1116
+
1117
+
1118
+	/**
1119
+	 * owes_monies_and_can_pay
1120
+	 * whether or not this registration has monies owing and it's' status allows payment
1121
+	 *
1122
+	 * @param array $requires_payment
1123
+	 * @return bool
1124
+	 * @throws EE_Error
1125
+	 */
1126
+	public function owes_monies_and_can_pay($requires_payment = array())
1127
+	{
1128
+		// these reg statuses require payment (if event is not free)
1129
+		$requires_payment = ! empty($requires_payment)
1130
+			? $requires_payment
1131
+			: EEM_Registration::reg_statuses_that_allow_payment();
1132
+		if (in_array($this->status_ID(), $requires_payment) &&
1133
+			$this->final_price() != 0 &&
1134
+			$this->final_price() != $this->paid()
1135
+		) {
1136
+			return true;
1137
+		} else {
1138
+			return false;
1139
+		}
1140
+	}
1141
+
1142
+
1143
+	/**
1144
+	 * Prints out the return value of $this->pretty_status()
1145
+	 *
1146
+	 * @param bool $show_icons
1147
+	 * @return void
1148
+	 * @throws EE_Error
1149
+	 */
1150
+	public function e_pretty_status($show_icons = false)
1151
+	{
1152
+		echo $this->pretty_status($show_icons);
1153
+	}
1154
+
1155
+
1156
+	/**
1157
+	 * Returns a nice version of the status for displaying to customers
1158
+	 *
1159
+	 * @param bool $show_icons
1160
+	 * @return string
1161
+	 * @throws EE_Error
1162
+	 */
1163
+	public function pretty_status($show_icons = false)
1164
+	{
1165
+		$status = EEM_Status::instance()->localized_status(
1166
+			array($this->status_ID() => esc_html__('unknown', 'event_espresso')),
1167
+			false,
1168
+			'sentence'
1169
+		);
1170
+		$icon = '';
1171
+		switch ($this->status_ID()) {
1172
+			case EEM_Registration::status_id_approved:
1173
+				$icon = $show_icons
1174
+					? '<span class="dashicons dashicons-star-filled ee-icon-size-16 green-text"></span>'
1175
+					: '';
1176
+				break;
1177
+			case EEM_Registration::status_id_pending_payment:
1178
+				$icon = $show_icons
1179
+					? '<span class="dashicons dashicons-star-half ee-icon-size-16 orange-text"></span>'
1180
+					: '';
1181
+				break;
1182
+			case EEM_Registration::status_id_not_approved:
1183
+				$icon = $show_icons
1184
+					? '<span class="dashicons dashicons-marker ee-icon-size-16 orange-text"></span>'
1185
+					: '';
1186
+				break;
1187
+			case EEM_Registration::status_id_cancelled:
1188
+				$icon = $show_icons
1189
+					? '<span class="dashicons dashicons-no ee-icon-size-16 lt-grey-text"></span>'
1190
+					: '';
1191
+				break;
1192
+			case EEM_Registration::status_id_incomplete:
1193
+				$icon = $show_icons
1194
+					? '<span class="dashicons dashicons-no ee-icon-size-16 lt-orange-text"></span>'
1195
+					: '';
1196
+				break;
1197
+			case EEM_Registration::status_id_declined:
1198
+				$icon = $show_icons
1199
+					? '<span class="dashicons dashicons-no ee-icon-size-16 red-text"></span>'
1200
+					: '';
1201
+				break;
1202
+			case EEM_Registration::status_id_wait_list:
1203
+				$icon = $show_icons
1204
+					? '<span class="dashicons dashicons-clipboard ee-icon-size-16 purple-text"></span>'
1205
+					: '';
1206
+				break;
1207
+		}
1208
+		return $icon . $status[ $this->status_ID() ];
1209
+	}
1210
+
1211
+
1212
+	/**
1213
+	 *        get Attendee Is Going
1214
+	 */
1215
+	public function att_is_going()
1216
+	{
1217
+		return $this->get('REG_att_is_going');
1218
+	}
1219
+
1220
+
1221
+	/**
1222
+	 * Gets related answers
1223
+	 *
1224
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1225
+	 * @return EE_Answer[]
1226
+	 * @throws EE_Error
1227
+	 */
1228
+	public function answers($query_params = null)
1229
+	{
1230
+		return $this->get_many_related('Answer', $query_params);
1231
+	}
1232
+
1233
+
1234
+	/**
1235
+	 * Gets the registration's answer value to the specified question
1236
+	 * (either the question's ID or a question object)
1237
+	 *
1238
+	 * @param EE_Question|int $question
1239
+	 * @param bool            $pretty_value
1240
+	 * @return array|string if pretty_value= true, the result will always be a string
1241
+	 * (because the answer might be an array of answer values, so passing pretty_value=true
1242
+	 * will convert it into some kind of string)
1243
+	 * @throws EE_Error
1244
+	 */
1245
+	public function answer_value_to_question($question, $pretty_value = true)
1246
+	{
1247
+		$question_id = EEM_Question::instance()->ensure_is_ID($question);
1248
+		return EEM_Answer::instance()->get_answer_value_to_question($this, $question_id, $pretty_value);
1249
+	}
1250
+
1251
+
1252
+	/**
1253
+	 * question_groups
1254
+	 * returns an array of EE_Question_Group objects for this registration
1255
+	 *
1256
+	 * @return EE_Question_Group[]
1257
+	 * @throws EE_Error
1258
+	 * @throws EntityNotFoundException
1259
+	 */
1260
+	public function question_groups()
1261
+	{
1262
+		$question_groups = array();
1263
+		if ($this->event() instanceof EE_Event) {
1264
+			$question_groups = $this->event()->question_groups(
1265
+				array(
1266
+					array(
1267
+						'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1268
+					),
1269
+					'order_by' => array('QSG_order' => 'ASC'),
1270
+				)
1271
+			);
1272
+		}
1273
+		return $question_groups;
1274
+	}
1275
+
1276
+
1277
+	/**
1278
+	 * count_question_groups
1279
+	 * returns a count of the number of EE_Question_Group objects for this registration
1280
+	 *
1281
+	 * @return int
1282
+	 * @throws EE_Error
1283
+	 * @throws EntityNotFoundException
1284
+	 */
1285
+	public function count_question_groups()
1286
+	{
1287
+		$qg_count = 0;
1288
+		if ($this->event() instanceof EE_Event) {
1289
+			$qg_count = $this->event()->count_related(
1290
+				'Question_Group',
1291
+				array(
1292
+					array(
1293
+						'Event_Question_Group.EQG_primary' => $this->count() == 1 ? true : false,
1294
+					),
1295
+				)
1296
+			);
1297
+		}
1298
+		return $qg_count;
1299
+	}
1300
+
1301
+
1302
+	/**
1303
+	 * Returns the registration date in the 'standard' string format
1304
+	 * (function may be improved in the future to allow for different formats and timezones)
1305
+	 *
1306
+	 * @return string
1307
+	 * @throws EE_Error
1308
+	 */
1309
+	public function reg_date()
1310
+	{
1311
+		return $this->get_datetime('REG_date');
1312
+	}
1313
+
1314
+
1315
+	/**
1316
+	 * Gets the datetime-ticket for this registration (ie, it can be used to isolate
1317
+	 * the ticket this registration purchased, or the datetime they have registered
1318
+	 * to attend)
1319
+	 *
1320
+	 * @return EE_Datetime_Ticket
1321
+	 * @throws EE_Error
1322
+	 */
1323
+	public function datetime_ticket()
1324
+	{
1325
+		return $this->get_first_related('Datetime_Ticket');
1326
+	}
1327
+
1328
+
1329
+	/**
1330
+	 * Sets the registration's datetime_ticket.
1331
+	 *
1332
+	 * @param EE_Datetime_Ticket $datetime_ticket
1333
+	 * @return EE_Datetime_Ticket
1334
+	 * @throws EE_Error
1335
+	 */
1336
+	public function set_datetime_ticket($datetime_ticket)
1337
+	{
1338
+		return $this->_add_relation_to($datetime_ticket, 'Datetime_Ticket');
1339
+	}
1340
+
1341
+	/**
1342
+	 * Gets deleted
1343
+	 *
1344
+	 * @return bool
1345
+	 * @throws EE_Error
1346
+	 */
1347
+	public function deleted()
1348
+	{
1349
+		return $this->get('REG_deleted');
1350
+	}
1351
+
1352
+	/**
1353
+	 * Sets deleted
1354
+	 *
1355
+	 * @param boolean $deleted
1356
+	 * @return bool
1357
+	 * @throws EE_Error
1358
+	 * @throws RuntimeException
1359
+	 */
1360
+	public function set_deleted($deleted)
1361
+	{
1362
+		if ($deleted) {
1363
+			$this->delete();
1364
+		} else {
1365
+			$this->restore();
1366
+		}
1367
+	}
1368
+
1369
+
1370
+	/**
1371
+	 * Get the status object of this object
1372
+	 *
1373
+	 * @return EE_Status
1374
+	 * @throws EE_Error
1375
+	 */
1376
+	public function status_obj()
1377
+	{
1378
+		return $this->get_first_related('Status');
1379
+	}
1380
+
1381
+
1382
+	/**
1383
+	 * Returns the number of times this registration has checked into any of the datetimes
1384
+	 * its available for
1385
+	 *
1386
+	 * @return int
1387
+	 * @throws EE_Error
1388
+	 */
1389
+	public function count_checkins()
1390
+	{
1391
+		return $this->get_model()->count_related($this, 'Checkin');
1392
+	}
1393
+
1394
+
1395
+	/**
1396
+	 * Returns the number of current Check-ins this registration is checked into for any of the datetimes the
1397
+	 * registration is for.  Note, this is ONLY checked in (does not include checkedout)
1398
+	 *
1399
+	 * @return int
1400
+	 * @throws EE_Error
1401
+	 */
1402
+	public function count_checkins_not_checkedout()
1403
+	{
1404
+		return $this->get_model()->count_related($this, 'Checkin', array(array('CHK_in' => 1)));
1405
+	}
1406
+
1407
+
1408
+	/**
1409
+	 * The purpose of this method is simply to check whether this registration can checkin to the given datetime.
1410
+	 *
1411
+	 * @param int | EE_Datetime $DTT_OR_ID      The datetime the registration is being checked against
1412
+	 * @param bool              $check_approved This is used to indicate whether the caller wants can_checkin to also
1413
+	 *                                          consider registration status as well as datetime access.
1414
+	 * @return bool
1415
+	 * @throws EE_Error
1416
+	 */
1417
+	public function can_checkin($DTT_OR_ID, $check_approved = true)
1418
+	{
1419
+		$DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1420
+
1421
+		// first check registration status
1422
+		if (($check_approved && ! $this->is_approved()) || ! $DTT_ID) {
1423
+			return false;
1424
+		}
1425
+		// is there a datetime ticket that matches this dtt_ID?
1426
+		if (! (EEM_Datetime_Ticket::instance()->exists(
1427
+			array(
1428
+				array(
1429
+					'TKT_ID' => $this->get('TKT_ID'),
1430
+					'DTT_ID' => $DTT_ID,
1431
+				),
1432
+			)
1433
+		))
1434
+		) {
1435
+			return false;
1436
+		}
1437
+
1438
+		// final check is against TKT_uses
1439
+		return $this->verify_can_checkin_against_TKT_uses($DTT_ID);
1440
+	}
1441
+
1442
+
1443
+	/**
1444
+	 * This method verifies whether the user can checkin for the given datetime considering the max uses value set on
1445
+	 * the ticket. To do this,  a query is done to get the count of the datetime records already checked into.  If the
1446
+	 * datetime given does not have a check-in record and checking in for that datetime will exceed the allowed uses,
1447
+	 * then return false.  Otherwise return true.
1448
+	 *
1449
+	 * @param int | EE_Datetime $DTT_OR_ID The datetime the registration is being checked against
1450
+	 * @return bool true means can checkin.  false means cannot checkin.
1451
+	 * @throws EE_Error
1452
+	 */
1453
+	public function verify_can_checkin_against_TKT_uses($DTT_OR_ID)
1454
+	{
1455
+		$DTT_ID = EEM_Datetime::instance()->ensure_is_ID($DTT_OR_ID);
1456
+
1457
+		if (! $DTT_ID) {
1458
+			return false;
1459
+		}
1460
+
1461
+		$max_uses = $this->ticket() instanceof EE_Ticket ? $this->ticket()->uses() : EE_INF;
1462
+
1463
+		// if max uses is not set or equals infinity then return true cause its not a factor for whether user can
1464
+		// check-in or not.
1465
+		if (! $max_uses || $max_uses === EE_INF) {
1466
+			return true;
1467
+		}
1468
+
1469
+		// does this datetime have a checkin record?  If so, then the dtt count has already been verified so we can just
1470
+		// go ahead and toggle.
1471
+		if (EEM_Checkin::instance()->exists(array(array('REG_ID' => $this->ID(), 'DTT_ID' => $DTT_ID)))) {
1472
+			return true;
1473
+		}
1474
+
1475
+		// made it here so the last check is whether the number of checkins per unique datetime on this registration
1476
+		// disallows further check-ins.
1477
+		$count_unique_dtt_checkins = EEM_Checkin::instance()->count(
1478
+			array(
1479
+				array(
1480
+					'REG_ID' => $this->ID(),
1481
+					'CHK_in' => true,
1482
+				),
1483
+			),
1484
+			'DTT_ID',
1485
+			true
1486
+		);
1487
+		// checkins have already reached their max number of uses
1488
+		// so registrant can NOT checkin
1489
+		if ($count_unique_dtt_checkins >= $max_uses) {
1490
+			EE_Error::add_error(
1491
+				esc_html__(
1492
+					'Check-in denied because number of datetime uses for the ticket has been reached or exceeded.',
1493
+					'event_espresso'
1494
+				),
1495
+				__FILE__,
1496
+				__FUNCTION__,
1497
+				__LINE__
1498
+			);
1499
+			return false;
1500
+		}
1501
+		return true;
1502
+	}
1503
+
1504
+
1505
+	/**
1506
+	 * toggle Check-in status for this registration
1507
+	 * Check-ins are toggled in the following order:
1508
+	 * never checked in -> checked in
1509
+	 * checked in -> checked out
1510
+	 * checked out -> checked in
1511
+	 *
1512
+	 * @param  int $DTT_ID  include specific datetime to toggle Check-in for.
1513
+	 *                      If not included or null, then it is assumed latest datetime is being toggled.
1514
+	 * @param bool $verify  If true then can_checkin() is used to verify whether the person
1515
+	 *                      can be checked in or not.  Otherwise this forces change in checkin status.
1516
+	 * @return bool|int     the chk_in status toggled to OR false if nothing got changed.
1517
+	 * @throws EE_Error
1518
+	 */
1519
+	public function toggle_checkin_status($DTT_ID = null, $verify = false)
1520
+	{
1521
+		if (empty($DTT_ID)) {
1522
+			$datetime = $this->get_latest_related_datetime();
1523
+			$DTT_ID = $datetime instanceof EE_Datetime ? $datetime->ID() : 0;
1524
+			// verify the registration can checkin for the given DTT_ID
1525
+		} elseif (! $this->can_checkin($DTT_ID, $verify)) {
1526
+			EE_Error::add_error(
1527
+				sprintf(
1528
+					esc_html__(
1529
+						'The given registration (ID:%1$d) can not be checked in to the given DTT_ID (%2$d), because the registration does not have access',
1530
+						'event_espresso'
1531
+					),
1532
+					$this->ID(),
1533
+					$DTT_ID
1534
+				),
1535
+				__FILE__,
1536
+				__FUNCTION__,
1537
+				__LINE__
1538
+			);
1539
+			return false;
1540
+		}
1541
+		$status_paths = array(
1542
+			EE_Checkin::status_checked_never => EE_Checkin::status_checked_in,
1543
+			EE_Checkin::status_checked_in    => EE_Checkin::status_checked_out,
1544
+			EE_Checkin::status_checked_out   => EE_Checkin::status_checked_in,
1545
+		);
1546
+		// start by getting the current status so we know what status we'll be changing to.
1547
+		$cur_status = $this->check_in_status_for_datetime($DTT_ID, null);
1548
+		$status_to = $status_paths[ $cur_status ];
1549
+		// database only records true for checked IN or false for checked OUT
1550
+		// no record ( null ) means checked in NEVER, but we obviously don't save that
1551
+		$new_status = $status_to === EE_Checkin::status_checked_in ? true : false;
1552
+		// add relation - note Check-ins are always creating new rows
1553
+		// because we are keeping track of Check-ins over time.
1554
+		// Eventually we'll probably want to show a list table
1555
+		// for the individual Check-ins so that they can be managed.
1556
+		$checkin = EE_Checkin::new_instance(
1557
+			array(
1558
+				'REG_ID' => $this->ID(),
1559
+				'DTT_ID' => $DTT_ID,
1560
+				'CHK_in' => $new_status,
1561
+			)
1562
+		);
1563
+		// if the record could not be saved then return false
1564
+		if ($checkin->save() === 0) {
1565
+			if (WP_DEBUG) {
1566
+				global $wpdb;
1567
+				$error = sprintf(
1568
+					esc_html__(
1569
+						'Registration check in update failed because of the following database error: %1$s%2$s',
1570
+						'event_espresso'
1571
+					),
1572
+					'<br />',
1573
+					$wpdb->last_error
1574
+				);
1575
+			} else {
1576
+				$error = esc_html__(
1577
+					'Registration check in update failed because of an unknown database error',
1578
+					'event_espresso'
1579
+				);
1580
+			}
1581
+			EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__);
1582
+			return false;
1583
+		}
1584
+		return $status_to;
1585
+	}
1586
+
1587
+
1588
+	/**
1589
+	 * Returns the latest datetime related to this registration (via the ticket attached to the registration).
1590
+	 * "Latest" is defined by the `DTT_EVT_start` column.
1591
+	 *
1592
+	 * @return EE_Datetime|null
1593
+	 * @throws EE_Error
1594
+	 */
1595
+	public function get_latest_related_datetime()
1596
+	{
1597
+		return EEM_Datetime::instance()->get_one(
1598
+			array(
1599
+				array(
1600
+					'Ticket.Registration.REG_ID' => $this->ID(),
1601
+				),
1602
+				'order_by' => array('DTT_EVT_start' => 'DESC'),
1603
+			)
1604
+		);
1605
+	}
1606
+
1607
+
1608
+	/**
1609
+	 * Returns the earliest datetime related to this registration (via the ticket attached to the registration).
1610
+	 * "Earliest" is defined by the `DTT_EVT_start` column.
1611
+	 *
1612
+	 * @throws EE_Error
1613
+	 */
1614
+	public function get_earliest_related_datetime()
1615
+	{
1616
+		return EEM_Datetime::instance()->get_one(
1617
+			array(
1618
+				array(
1619
+					'Ticket.Registration.REG_ID' => $this->ID(),
1620
+				),
1621
+				'order_by' => array('DTT_EVT_start' => 'ASC'),
1622
+			)
1623
+		);
1624
+	}
1625
+
1626
+
1627
+	/**
1628
+	 * This method simply returns the check-in status for this registration and the given datetime.
1629
+	 * If neither the datetime nor the checkin values are provided as arguments,
1630
+	 * then this will return the LATEST check-in status for the registration across all datetimes it belongs to.
1631
+	 *
1632
+	 * @param  int       $DTT_ID  The ID of the datetime we're checking against
1633
+	 *                            (if empty we'll get the primary datetime for
1634
+	 *                            this registration (via event) and use it's ID);
1635
+	 * @param EE_Checkin $checkin If present, we use the given checkin object rather than the dtt_id.
1636
+	 *
1637
+	 * @return int                Integer representing Check-in status.
1638
+	 * @throws EE_Error
1639
+	 */
1640
+	public function check_in_status_for_datetime($DTT_ID = 0, $checkin = null)
1641
+	{
1642
+		$checkin_query_params = array(
1643
+			'order_by' => array('CHK_timestamp' => 'DESC'),
1644
+		);
1645
+
1646
+		if ($DTT_ID > 0) {
1647
+			$checkin_query_params[0] = array('DTT_ID' => $DTT_ID);
1648
+		}
1649
+
1650
+		// get checkin object (if exists)
1651
+		$checkin = $checkin instanceof EE_Checkin
1652
+			? $checkin
1653
+			: $this->get_first_related('Checkin', $checkin_query_params);
1654
+		if ($checkin instanceof EE_Checkin) {
1655
+			if ($checkin->get('CHK_in')) {
1656
+				return EE_Checkin::status_checked_in; // checked in
1657
+			}
1658
+			return EE_Checkin::status_checked_out; // had checked in but is now checked out.
1659
+		}
1660
+		return EE_Checkin::status_checked_never; // never been checked in
1661
+	}
1662
+
1663
+
1664
+	/**
1665
+	 * This method returns a localized message for the toggled Check-in message.
1666
+	 *
1667
+	 * @param  int $DTT_ID include specific datetime to get the correct Check-in message.  If not included or null,
1668
+	 *                     then it is assumed Check-in for primary datetime was toggled.
1669
+	 * @param bool $error  This just flags that you want an error message returned. This is put in so that the error
1670
+	 *                     message can be customized with the attendee name.
1671
+	 * @return string internationalized message
1672
+	 * @throws EE_Error
1673
+	 */
1674
+	public function get_checkin_msg($DTT_ID, $error = false)
1675
+	{
1676
+		// let's get the attendee first so we can include the name of the attendee
1677
+		$attendee = $this->get_first_related('Attendee');
1678
+		if ($attendee instanceof EE_Attendee) {
1679
+			if ($error) {
1680
+				return sprintf(__("%s's check-in status was not changed.", "event_espresso"), $attendee->full_name());
1681
+			}
1682
+			$cur_status = $this->check_in_status_for_datetime($DTT_ID);
1683
+			// what is the status message going to be?
1684
+			switch ($cur_status) {
1685
+				case EE_Checkin::status_checked_never:
1686
+					return sprintf(
1687
+						__("%s has been removed from Check-in records", "event_espresso"),
1688
+						$attendee->full_name()
1689
+					);
1690
+					break;
1691
+				case EE_Checkin::status_checked_in:
1692
+					return sprintf(__('%s has been checked in', 'event_espresso'), $attendee->full_name());
1693
+					break;
1694
+				case EE_Checkin::status_checked_out:
1695
+					return sprintf(__('%s has been checked out', 'event_espresso'), $attendee->full_name());
1696
+					break;
1697
+			}
1698
+		}
1699
+		return esc_html__("The check-in status could not be determined.", "event_espresso");
1700
+	}
1701
+
1702
+
1703
+	/**
1704
+	 * Returns the related EE_Transaction to this registration
1705
+	 *
1706
+	 * @return EE_Transaction
1707
+	 * @throws EE_Error
1708
+	 * @throws EntityNotFoundException
1709
+	 */
1710
+	public function transaction()
1711
+	{
1712
+		$transaction = $this->get_first_related('Transaction');
1713
+		if (! $transaction instanceof \EE_Transaction) {
1714
+			throw new EntityNotFoundException('Transaction ID', $this->transaction_ID());
1715
+		}
1716
+		return $transaction;
1717
+	}
1718
+
1719
+
1720
+	/**
1721
+	 *        get Registration Code
1722
+	 */
1723
+	public function reg_code()
1724
+	{
1725
+		return $this->get('REG_code');
1726
+	}
1727
+
1728
+
1729
+	/**
1730
+	 *        get Transaction ID
1731
+	 */
1732
+	public function transaction_ID()
1733
+	{
1734
+		return $this->get('TXN_ID');
1735
+	}
1736
+
1737
+
1738
+	/**
1739
+	 * @return int
1740
+	 * @throws EE_Error
1741
+	 */
1742
+	public function ticket_ID()
1743
+	{
1744
+		return $this->get('TKT_ID');
1745
+	}
1746
+
1747
+
1748
+	/**
1749
+	 *        Set Registration Code
1750
+	 *
1751
+	 * @access    public
1752
+	 * @param    string  $REG_code Registration Code
1753
+	 * @param    boolean $use_default
1754
+	 * @throws EE_Error
1755
+	 */
1756
+	public function set_reg_code($REG_code, $use_default = false)
1757
+	{
1758
+		if (empty($REG_code)) {
1759
+			EE_Error::add_error(
1760
+				esc_html__('REG_code can not be empty.', 'event_espresso'),
1761
+				__FILE__,
1762
+				__FUNCTION__,
1763
+				__LINE__
1764
+			);
1765
+			return;
1766
+		}
1767
+		if (! $this->reg_code()) {
1768
+			parent::set('REG_code', $REG_code, $use_default);
1769
+		} else {
1770
+			EE_Error::doing_it_wrong(
1771
+				__CLASS__ . '::' . __FUNCTION__,
1772
+				esc_html__('Can not change a registration REG_code once it has been set.', 'event_espresso'),
1773
+				'4.6.0'
1774
+			);
1775
+		}
1776
+	}
1777
+
1778
+
1779
+	/**
1780
+	 * Returns all other registrations in the same group as this registrant who have the same ticket option.
1781
+	 * Note, if you want to just get all registrations in the same transaction (group), use:
1782
+	 *    $registration->transaction()->registrations();
1783
+	 *
1784
+	 * @since 4.5.0
1785
+	 * @return EE_Registration[] or empty array if this isn't a group registration.
1786
+	 * @throws EE_Error
1787
+	 */
1788
+	public function get_all_other_registrations_in_group()
1789
+	{
1790
+		if ($this->group_size() < 2) {
1791
+			return array();
1792
+		}
1793
+
1794
+		$query[0] = array(
1795
+			'TXN_ID' => $this->transaction_ID(),
1796
+			'REG_ID' => array('!=', $this->ID()),
1797
+			'TKT_ID' => $this->ticket_ID(),
1798
+		);
1799
+		/** @var EE_Registration[] $registrations */
1800
+		$registrations = $this->get_model()->get_all($query);
1801
+		return $registrations;
1802
+	}
1803
+
1804
+	/**
1805
+	 * Return the link to the admin details for the object.
1806
+	 *
1807
+	 * @return string
1808
+	 * @throws EE_Error
1809
+	 */
1810
+	public function get_admin_details_link()
1811
+	{
1812
+		EE_Registry::instance()->load_helper('URL');
1813
+		return EEH_URL::add_query_args_and_nonce(
1814
+			array(
1815
+				'page'    => 'espresso_registrations',
1816
+				'action'  => 'view_registration',
1817
+				'_REG_ID' => $this->ID(),
1818
+			),
1819
+			admin_url('admin.php')
1820
+		);
1821
+	}
1822
+
1823
+	/**
1824
+	 * Returns the link to the editor for the object.  Sometimes this is the same as the details.
1825
+	 *
1826
+	 * @return string
1827
+	 * @throws EE_Error
1828
+	 */
1829
+	public function get_admin_edit_link()
1830
+	{
1831
+		return $this->get_admin_details_link();
1832
+	}
1833
+
1834
+	/**
1835
+	 * Returns the link to a settings page for the object.
1836
+	 *
1837
+	 * @return string
1838
+	 * @throws EE_Error
1839
+	 */
1840
+	public function get_admin_settings_link()
1841
+	{
1842
+		return $this->get_admin_details_link();
1843
+	}
1844
+
1845
+	/**
1846
+	 * Returns the link to the "overview" for the object (typically the "list table" view).
1847
+	 *
1848
+	 * @return string
1849
+	 */
1850
+	public function get_admin_overview_link()
1851
+	{
1852
+		EE_Registry::instance()->load_helper('URL');
1853
+		return EEH_URL::add_query_args_and_nonce(
1854
+			array(
1855
+				'page' => 'espresso_registrations',
1856
+			),
1857
+			admin_url('admin.php')
1858
+		);
1859
+	}
1860
+
1861
+
1862
+	/**
1863
+	 * @param array $query_params
1864
+	 *
1865
+	 * @return \EE_Registration[]
1866
+	 * @throws EE_Error
1867
+	 */
1868
+	public function payments($query_params = array())
1869
+	{
1870
+		return $this->get_many_related('Payment', $query_params);
1871
+	}
1872
+
1873
+
1874
+	/**
1875
+	 * @param array $query_params
1876
+	 *
1877
+	 * @return \EE_Registration_Payment[]
1878
+	 * @throws EE_Error
1879
+	 */
1880
+	public function registration_payments($query_params = array())
1881
+	{
1882
+		return $this->get_many_related('Registration_Payment', $query_params);
1883
+	}
1884
+
1885
+
1886
+	/**
1887
+	 * This grabs the payment method corresponding to the last payment made for the amount owing on the registration.
1888
+	 * Note: if there are no payments on the registration there will be no payment method returned.
1889
+	 *
1890
+	 * @return EE_Payment_Method|null
1891
+	 */
1892
+	public function payment_method()
1893
+	{
1894
+		return EEM_Payment_Method::instance()->get_last_used_for_registration($this);
1895
+	}
1896
+
1897
+
1898
+	/**
1899
+	 * @return \EE_Line_Item
1900
+	 * @throws EntityNotFoundException
1901
+	 * @throws EE_Error
1902
+	 */
1903
+	public function ticket_line_item()
1904
+	{
1905
+		$ticket = $this->ticket();
1906
+		$transaction = $this->transaction();
1907
+		$line_item = null;
1908
+		$ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs(
1909
+			$transaction->total_line_item(),
1910
+			'Ticket',
1911
+			array($ticket->ID())
1912
+		);
1913
+		foreach ($ticket_line_items as $ticket_line_item) {
1914
+			if ($ticket_line_item instanceof \EE_Line_Item
1915
+				&& $ticket_line_item->OBJ_type() === 'Ticket'
1916
+				&& $ticket_line_item->OBJ_ID() === $ticket->ID()
1917
+			) {
1918
+				$line_item = $ticket_line_item;
1919
+				break;
1920
+			}
1921
+		}
1922
+		if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) {
1923
+			throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID());
1924
+		}
1925
+		return $line_item;
1926
+	}
1927
+
1928
+
1929
+	/**
1930
+	 * Soft Deletes this model object.
1931
+	 *
1932
+	 * @return boolean | int
1933
+	 * @throws RuntimeException
1934
+	 * @throws EE_Error
1935
+	 */
1936
+	public function delete()
1937
+	{
1938
+		if ($this->update_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY, $this->status_ID()) === true) {
1939
+			$this->set_status(EEM_Registration::status_id_cancelled);
1940
+		}
1941
+		return parent::delete();
1942
+	}
1943
+
1944
+
1945
+	/**
1946
+	 * Restores whatever the previous status was on a registration before it was trashed (if possible)
1947
+	 *
1948
+	 * @throws EE_Error
1949
+	 * @throws RuntimeException
1950
+	 */
1951
+	public function restore()
1952
+	{
1953
+		$previous_status = $this->get_extra_meta(
1954
+			EE_Registration::PRE_TRASH_REG_STATUS_KEY,
1955
+			true,
1956
+			EEM_Registration::status_id_cancelled
1957
+		);
1958
+		if ($previous_status) {
1959
+			$this->delete_extra_meta(EE_Registration::PRE_TRASH_REG_STATUS_KEY);
1960
+			$this->set_status($previous_status);
1961
+		}
1962
+		return parent::restore();
1963
+	}
1964
+
1965
+
1966
+	/**
1967
+	 * possibly toggle Registration status based on comparison of REG_paid vs REG_final_price
1968
+	 *
1969
+	 * @param  boolean $trigger_set_status_logic EE_Registration::set_status() can trigger additional logic
1970
+	 *                                           depending on whether the reg status changes to or from "Approved"
1971
+	 * @return boolean whether the Registration status was updated
1972
+	 * @throws EE_Error
1973
+	 * @throws RuntimeException
1974
+	 */
1975
+	public function updateStatusBasedOnTotalPaid($trigger_set_status_logic = true)
1976
+	{
1977
+		$paid = $this->paid();
1978
+		$price = $this->final_price();
1979
+		switch (true) {
1980
+			// overpaid or paid
1981
+			case EEH_Money::compare_floats($paid, $price, '>'):
1982
+			case EEH_Money::compare_floats($paid, $price):
1983
+				$new_status = EEM_Registration::status_id_approved;
1984
+				break;
1985
+			//  underpaid
1986
+			case EEH_Money::compare_floats($paid, $price, '<'):
1987
+				$new_status = EEM_Registration::status_id_pending_payment;
1988
+				break;
1989
+			// uhhh Houston...
1990
+			default:
1991
+				throw new RuntimeException(
1992
+					esc_html__('The total paid calculation for this registration is inaccurate.', 'event_espresso')
1993
+				);
1994
+		}
1995
+		if ($new_status !== $this->status_ID()) {
1996
+			if ($trigger_set_status_logic) {
1997
+				return $this->set_status($new_status);
1998
+			}
1999
+			parent::set('STS_ID', $new_status);
2000
+			return true;
2001
+		}
2002
+		return false;
2003
+	}
2004
+
2005
+
2006
+	/*************************** DEPRECATED ***************************/
2007
+
2008
+
2009
+	/**
2010
+	 * @deprecated
2011
+	 * @since     4.7.0
2012
+	 * @access    public
2013
+	 */
2014
+	public function price_paid()
2015
+	{
2016
+		EE_Error::doing_it_wrong(
2017
+			'EE_Registration::price_paid()',
2018
+			esc_html__(
2019
+				'This method is deprecated, please use EE_Registration::final_price() instead.',
2020
+				'event_espresso'
2021
+			),
2022
+			'4.7.0'
2023
+		);
2024
+		return $this->final_price();
2025
+	}
2026
+
2027
+
2028
+	/**
2029
+	 * @deprecated
2030
+	 * @since     4.7.0
2031
+	 * @access    public
2032
+	 * @param    float $REG_final_price
2033
+	 * @throws EE_Error
2034
+	 * @throws RuntimeException
2035
+	 */
2036
+	public function set_price_paid($REG_final_price = 0.00)
2037
+	{
2038
+		EE_Error::doing_it_wrong(
2039
+			'EE_Registration::set_price_paid()',
2040
+			esc_html__(
2041
+				'This method is deprecated, please use EE_Registration::set_final_price() instead.',
2042
+				'event_espresso'
2043
+			),
2044
+			'4.7.0'
2045
+		);
2046
+		$this->set_final_price($REG_final_price);
2047
+	}
2048
+
2049
+
2050
+	/**
2051
+	 * @deprecated
2052
+	 * @since 4.7.0
2053
+	 * @return string
2054
+	 * @throws EE_Error
2055
+	 */
2056
+	public function pretty_price_paid()
2057
+	{
2058
+		EE_Error::doing_it_wrong(
2059
+			'EE_Registration::pretty_price_paid()',
2060
+			esc_html__(
2061
+				'This method is deprecated, please use EE_Registration::pretty_final_price() instead.',
2062
+				'event_espresso'
2063
+			),
2064
+			'4.7.0'
2065
+		);
2066
+		return $this->pretty_final_price();
2067
+	}
2068
+
2069
+
2070
+	/**
2071
+	 * Gets the primary datetime related to this registration via the related Event to this registration
2072
+	 *
2073
+	 * @deprecated 4.9.17
2074
+	 * @return EE_Datetime
2075
+	 * @throws EE_Error
2076
+	 * @throws EntityNotFoundException
2077
+	 */
2078
+	public function get_related_primary_datetime()
2079
+	{
2080
+		EE_Error::doing_it_wrong(
2081
+			__METHOD__,
2082
+			esc_html__(
2083
+				'Use EE_Registration::get_latest_related_datetime() or EE_Registration::get_earliest_related_datetime()',
2084
+				'event_espresso'
2085
+			),
2086
+			'4.9.17',
2087
+			'5.0.0'
2088
+		);
2089
+		return $this->event()->primary_datetime();
2090
+	}
2091 2091
 }
Please login to merge, or discard this patch.
core/db_classes/EE_Base_Class.class.php 2 patches
Spacing   +121 added lines, -121 removed lines patch added patch discarded remove patch
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
         $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues);
147 147
         // verify client code has not passed any invalid field names
148 148
         foreach ($fieldValues as $field_name => $field_value) {
149
-            if (! isset($model_fields[ $field_name ])) {
149
+            if ( ! isset($model_fields[$field_name])) {
150 150
                 throw new EE_Error(
151 151
                     sprintf(
152 152
                         esc_html__(
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
             }
162 162
         }
163 163
         $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
164
-        if (! empty($date_formats) && is_array($date_formats)) {
164
+        if ( ! empty($date_formats) && is_array($date_formats)) {
165 165
             list($this->_dt_frmt, $this->_tm_frmt) = $date_formats;
166 166
         } else {
167 167
             // set default formats for date and time
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
             foreach ($model_fields as $fieldName => $field) {
175 175
                 $this->set_from_db(
176 176
                     $fieldName,
177
-                    isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null
177
+                    isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null
178 178
                 );
179 179
             }
180 180
         } else {
@@ -183,7 +183,7 @@  discard block
 block discarded – undo
183 183
             foreach ($model_fields as $fieldName => $field) {
184 184
                 $this->set(
185 185
                     $fieldName,
186
-                    isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null,
186
+                    isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null,
187 187
                     true
188 188
                 );
189 189
             }
@@ -191,15 +191,15 @@  discard block
 block discarded – undo
191 191
         // remember what values were passed to this constructor
192 192
         $this->_props_n_values_provided_in_constructor = $fieldValues;
193 193
         // remember in entity mapper
194
-        if (! $bydb && $model->has_primary_key_field() && $this->ID()) {
194
+        if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) {
195 195
             $model->add_to_entity_map($this);
196 196
         }
197 197
         // setup all the relations
198 198
         foreach ($model->relation_settings() as $relation_name => $relation_obj) {
199 199
             if ($relation_obj instanceof EE_Belongs_To_Relation) {
200
-                $this->_model_relations[ $relation_name ] = null;
200
+                $this->_model_relations[$relation_name] = null;
201 201
             } else {
202
-                $this->_model_relations[ $relation_name ] = array();
202
+                $this->_model_relations[$relation_name] = array();
203 203
             }
204 204
         }
205 205
         /**
@@ -250,10 +250,10 @@  discard block
 block discarded – undo
250 250
      */
251 251
     public function get_original($field_name)
252 252
     {
253
-        if (isset($this->_props_n_values_provided_in_constructor[ $field_name ])
253
+        if (isset($this->_props_n_values_provided_in_constructor[$field_name])
254 254
             && $field_settings = $this->get_model()->field_settings_for($field_name)
255 255
         ) {
256
-            return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]);
256
+            return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]);
257 257
         }
258 258
         return null;
259 259
     }
@@ -288,8 +288,8 @@  discard block
 block discarded – undo
288 288
     {
289 289
         // if not using default and nothing has changed, and object has already been setup (has ID),
290 290
         // then don't do anything
291
-        if (! $use_default
292
-            && $this->_fields[ $field_name ] === $field_value
291
+        if ( ! $use_default
292
+            && $this->_fields[$field_name] === $field_value
293 293
             && $this->ID()
294 294
         ) {
295 295
             return;
@@ -307,7 +307,7 @@  discard block
 block discarded – undo
307 307
             $holder_of_value = $field_obj->prepare_for_set($field_value);
308 308
             // should the value be null?
309 309
             if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) {
310
-                $this->_fields[ $field_name ] = $field_obj->get_default_value();
310
+                $this->_fields[$field_name] = $field_obj->get_default_value();
311 311
                 /**
312 312
                  * To save having to refactor all the models, if a default value is used for a
313 313
                  * EE_Datetime_Field, and that value is not null nor is it a DateTime
@@ -317,15 +317,15 @@  discard block
 block discarded – undo
317 317
                  * @since 4.6.10+
318 318
                  */
319 319
                 if ($field_obj instanceof EE_Datetime_Field
320
-                    && $this->_fields[ $field_name ] !== null
321
-                    && ! $this->_fields[ $field_name ] instanceof DateTime
320
+                    && $this->_fields[$field_name] !== null
321
+                    && ! $this->_fields[$field_name] instanceof DateTime
322 322
                 ) {
323
-                    empty($this->_fields[ $field_name ])
323
+                    empty($this->_fields[$field_name])
324 324
                         ? $this->set($field_name, time())
325
-                        : $this->set($field_name, $this->_fields[ $field_name ]);
325
+                        : $this->set($field_name, $this->_fields[$field_name]);
326 326
                 }
327 327
             } else {
328
-                $this->_fields[ $field_name ] = $holder_of_value;
328
+                $this->_fields[$field_name] = $holder_of_value;
329 329
             }
330 330
             // if we're not in the constructor...
331 331
             // now check if what we set was a primary key
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
                 $fields_on_model = self::_get_model(get_class($this))->field_settings();
342 342
                 $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value);
343 343
                 foreach ($fields_on_model as $field_obj) {
344
-                    if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor)
344
+                    if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor)
345 345
                         && $field_obj->get_name() !== $field_name
346 346
                     ) {
347 347
                         $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name()));
@@ -386,8 +386,8 @@  discard block
 block discarded – undo
386 386
      */
387 387
     public function getCustomSelect($alias)
388 388
     {
389
-        return isset($this->custom_selection_results[ $alias ])
390
-            ? $this->custom_selection_results[ $alias ]
389
+        return isset($this->custom_selection_results[$alias])
390
+            ? $this->custom_selection_results[$alias]
391 391
             : null;
392 392
     }
393 393
 
@@ -474,8 +474,8 @@  discard block
 block discarded – undo
474 474
         foreach ($model_fields as $field_name => $field_obj) {
475 475
             if ($field_obj instanceof EE_Datetime_Field) {
476 476
                 $field_obj->set_timezone($this->_timezone);
477
-                if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) {
478
-                    EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone));
477
+                if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) {
478
+                    EEH_DTT_Helper::setTimezone($this->_fields[$field_name], new DateTimeZone($this->_timezone));
479 479
                 }
480 480
             }
481 481
         }
@@ -533,7 +533,7 @@  discard block
 block discarded – undo
533 533
      */
534 534
     public function get_format($full = true)
535 535
     {
536
-        return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
536
+        return $full ? $this->_dt_frmt.' '.$this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
537 537
     }
538 538
 
539 539
 
@@ -559,11 +559,11 @@  discard block
 block discarded – undo
559 559
     public function cache($relationName = '', $object_to_cache = null, $cache_id = null)
560 560
     {
561 561
         // its entirely possible that there IS no related object yet in which case there is nothing to cache.
562
-        if (! $object_to_cache instanceof EE_Base_Class) {
562
+        if ( ! $object_to_cache instanceof EE_Base_Class) {
563 563
             return false;
564 564
         }
565 565
         // also get "how" the object is related, or throw an error
566
-        if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) {
566
+        if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) {
567 567
             throw new EE_Error(
568 568
                 sprintf(
569 569
                     esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'),
@@ -577,38 +577,38 @@  discard block
 block discarded – undo
577 577
             // if it's a "belongs to" relationship, then there's only one related model object
578 578
             // eg, if this is a registration, there's only 1 attendee for it
579 579
             // so for these model objects just set it to be cached
580
-            $this->_model_relations[ $relationName ] = $object_to_cache;
580
+            $this->_model_relations[$relationName] = $object_to_cache;
581 581
             $return = true;
582 582
         } else {
583 583
             // otherwise, this is the "many" side of a one to many relationship,
584 584
             // so we'll add the object to the array of related objects for that type.
585 585
             // eg: if this is an event, there are many registrations for that event,
586 586
             // so we cache the registrations in an array
587
-            if (! is_array($this->_model_relations[ $relationName ])) {
587
+            if ( ! is_array($this->_model_relations[$relationName])) {
588 588
                 // if for some reason, the cached item is a model object,
589 589
                 // then stick that in the array, otherwise start with an empty array
590
-                $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ]
590
+                $this->_model_relations[$relationName] = $this->_model_relations[$relationName]
591 591
                                                            instanceof
592 592
                                                            EE_Base_Class
593
-                    ? array($this->_model_relations[ $relationName ]) : array();
593
+                    ? array($this->_model_relations[$relationName]) : array();
594 594
             }
595 595
             // first check for a cache_id which is normally empty
596
-            if (! empty($cache_id)) {
596
+            if ( ! empty($cache_id)) {
597 597
                 // if the cache_id exists, then it means we are purposely trying to cache this
598 598
                 // with a known key that can then be used to retrieve the object later on
599
-                $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache;
599
+                $this->_model_relations[$relationName][$cache_id] = $object_to_cache;
600 600
                 $return = $cache_id;
601 601
             } elseif ($object_to_cache->ID()) {
602 602
                 // OR the cached object originally came from the db, so let's just use it's PK for an ID
603
-                $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache;
603
+                $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache;
604 604
                 $return = $object_to_cache->ID();
605 605
             } else {
606 606
                 // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID
607
-                $this->_model_relations[ $relationName ][] = $object_to_cache;
607
+                $this->_model_relations[$relationName][] = $object_to_cache;
608 608
                 // move the internal pointer to the end of the array
609
-                end($this->_model_relations[ $relationName ]);
609
+                end($this->_model_relations[$relationName]);
610 610
                 // and grab the key so that we can return it
611
-                $return = key($this->_model_relations[ $relationName ]);
611
+                $return = key($this->_model_relations[$relationName]);
612 612
             }
613 613
         }
614 614
         return $return;
@@ -634,7 +634,7 @@  discard block
 block discarded – undo
634 634
         // first make sure this property exists
635 635
         $this->get_model()->field_settings_for($fieldname);
636 636
         $cache_type = empty($cache_type) ? 'standard' : $cache_type;
637
-        $this->_cached_properties[ $fieldname ][ $cache_type ] = $value;
637
+        $this->_cached_properties[$fieldname][$cache_type] = $value;
638 638
     }
639 639
 
640 640
 
@@ -663,9 +663,9 @@  discard block
 block discarded – undo
663 663
         $model = $this->get_model();
664 664
         $model->field_settings_for($fieldname);
665 665
         $cache_type = $pretty ? 'pretty' : 'standard';
666
-        $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : '';
667
-        if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) {
668
-            return $this->_cached_properties[ $fieldname ][ $cache_type ];
666
+        $cache_type .= ! empty($extra_cache_ref) ? '_'.$extra_cache_ref : '';
667
+        if (isset($this->_cached_properties[$fieldname][$cache_type])) {
668
+            return $this->_cached_properties[$fieldname][$cache_type];
669 669
         }
670 670
         $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref);
671 671
         $this->_set_cached_property($fieldname, $value, $cache_type);
@@ -693,12 +693,12 @@  discard block
 block discarded – undo
693 693
         if ($field_obj instanceof EE_Datetime_Field) {
694 694
             $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref);
695 695
         }
696
-        if (! isset($this->_fields[ $fieldname ])) {
697
-            $this->_fields[ $fieldname ] = null;
696
+        if ( ! isset($this->_fields[$fieldname])) {
697
+            $this->_fields[$fieldname] = null;
698 698
         }
699 699
         $value = $pretty
700
-            ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref)
701
-            : $field_obj->prepare_for_get($this->_fields[ $fieldname ]);
700
+            ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref)
701
+            : $field_obj->prepare_for_get($this->_fields[$fieldname]);
702 702
         return $value;
703 703
     }
704 704
 
@@ -756,8 +756,8 @@  discard block
 block discarded – undo
756 756
      */
757 757
     protected function _clear_cached_property($property_name)
758 758
     {
759
-        if (isset($this->_cached_properties[ $property_name ])) {
760
-            unset($this->_cached_properties[ $property_name ]);
759
+        if (isset($this->_cached_properties[$property_name])) {
760
+            unset($this->_cached_properties[$property_name]);
761 761
         }
762 762
     }
763 763
 
@@ -809,7 +809,7 @@  discard block
 block discarded – undo
809 809
     {
810 810
         $relationship_to_model = $this->get_model()->related_settings_for($relationName);
811 811
         $index_in_cache = '';
812
-        if (! $relationship_to_model) {
812
+        if ( ! $relationship_to_model) {
813 813
             throw new EE_Error(
814 814
                 sprintf(
815 815
                     esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'),
@@ -820,21 +820,21 @@  discard block
 block discarded – undo
820 820
         }
821 821
         if ($clear_all) {
822 822
             $obj_removed = true;
823
-            $this->_model_relations[ $relationName ] = null;
823
+            $this->_model_relations[$relationName] = null;
824 824
         } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) {
825
-            $obj_removed = $this->_model_relations[ $relationName ];
826
-            $this->_model_relations[ $relationName ] = null;
825
+            $obj_removed = $this->_model_relations[$relationName];
826
+            $this->_model_relations[$relationName] = null;
827 827
         } else {
828 828
             if ($object_to_remove_or_index_into_array instanceof EE_Base_Class
829 829
                 && $object_to_remove_or_index_into_array->ID()
830 830
             ) {
831 831
                 $index_in_cache = $object_to_remove_or_index_into_array->ID();
832
-                if (is_array($this->_model_relations[ $relationName ])
833
-                    && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ])
832
+                if (is_array($this->_model_relations[$relationName])
833
+                    && ! isset($this->_model_relations[$relationName][$index_in_cache])
834 834
                 ) {
835 835
                     $index_found_at = null;
836 836
                     // find this object in the array even though it has a different key
837
-                    foreach ($this->_model_relations[ $relationName ] as $index => $obj) {
837
+                    foreach ($this->_model_relations[$relationName] as $index => $obj) {
838 838
                         /** @noinspection TypeUnsafeComparisonInspection */
839 839
                         if ($obj instanceof EE_Base_Class
840 840
                             && (
@@ -867,9 +867,9 @@  discard block
 block discarded – undo
867 867
             }
868 868
             // supposedly we've found it. But it could just be that the client code
869 869
             // provided a bad index/object
870
-            if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) {
871
-                $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ];
872
-                unset($this->_model_relations[ $relationName ][ $index_in_cache ]);
870
+            if (isset($this->_model_relations[$relationName][$index_in_cache])) {
871
+                $obj_removed = $this->_model_relations[$relationName][$index_in_cache];
872
+                unset($this->_model_relations[$relationName][$index_in_cache]);
873 873
             } else {
874 874
                 // that thing was never cached anyways.
875 875
                 $obj_removed = null;
@@ -900,7 +900,7 @@  discard block
 block discarded – undo
900 900
         $current_cache_id = ''
901 901
     ) {
902 902
         // verify that incoming object is of the correct type
903
-        $obj_class = 'EE_' . $relationName;
903
+        $obj_class = 'EE_'.$relationName;
904 904
         if ($newly_saved_object instanceof $obj_class) {
905 905
             /* @type EE_Base_Class $newly_saved_object */
906 906
             // now get the type of relation
@@ -908,17 +908,17 @@  discard block
 block discarded – undo
908 908
             // if this is a 1:1 relationship
909 909
             if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
910 910
                 // then just replace the cached object with the newly saved object
911
-                $this->_model_relations[ $relationName ] = $newly_saved_object;
911
+                $this->_model_relations[$relationName] = $newly_saved_object;
912 912
                 return true;
913 913
                 // or if it's some kind of sordid feral polyamorous relationship...
914 914
             }
915
-            if (is_array($this->_model_relations[ $relationName ])
916
-                && isset($this->_model_relations[ $relationName ][ $current_cache_id ])
915
+            if (is_array($this->_model_relations[$relationName])
916
+                && isset($this->_model_relations[$relationName][$current_cache_id])
917 917
             ) {
918 918
                 // then remove the current cached item
919
-                unset($this->_model_relations[ $relationName ][ $current_cache_id ]);
919
+                unset($this->_model_relations[$relationName][$current_cache_id]);
920 920
                 // and cache the newly saved object using it's new ID
921
-                $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object;
921
+                $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object;
922 922
                 return true;
923 923
             }
924 924
         }
@@ -935,8 +935,8 @@  discard block
 block discarded – undo
935 935
      */
936 936
     public function get_one_from_cache($relationName)
937 937
     {
938
-        $cached_array_or_object = isset($this->_model_relations[ $relationName ])
939
-            ? $this->_model_relations[ $relationName ]
938
+        $cached_array_or_object = isset($this->_model_relations[$relationName])
939
+            ? $this->_model_relations[$relationName]
940 940
             : null;
941 941
         if (is_array($cached_array_or_object)) {
942 942
             return array_shift($cached_array_or_object);
@@ -959,7 +959,7 @@  discard block
 block discarded – undo
959 959
      */
960 960
     public function get_all_from_cache($relationName)
961 961
     {
962
-        $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array();
962
+        $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array();
963 963
         // if the result is not an array, but exists, make it an array
964 964
         $objects = is_array($objects) ? $objects : array($objects);
965 965
         // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143
@@ -1143,7 +1143,7 @@  discard block
 block discarded – undo
1143 1143
             } else {
1144 1144
                 $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db);
1145 1145
             }
1146
-            $this->_fields[ $field_name ] = $field_value;
1146
+            $this->_fields[$field_name] = $field_value;
1147 1147
             $this->_clear_cached_property($field_name);
1148 1148
         }
1149 1149
     }
@@ -1183,9 +1183,9 @@  discard block
 block discarded – undo
1183 1183
     public function get_raw($field_name)
1184 1184
     {
1185 1185
         $field_settings = $this->get_model()->field_settings_for($field_name);
1186
-        return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime
1187
-            ? $this->_fields[ $field_name ]->format('U')
1188
-            : $this->_fields[ $field_name ];
1186
+        return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime
1187
+            ? $this->_fields[$field_name]->format('U')
1188
+            : $this->_fields[$field_name];
1189 1189
     }
1190 1190
 
1191 1191
 
@@ -1207,7 +1207,7 @@  discard block
 block discarded – undo
1207 1207
     public function get_DateTime_object($field_name)
1208 1208
     {
1209 1209
         $field_settings = $this->get_model()->field_settings_for($field_name);
1210
-        if (! $field_settings instanceof EE_Datetime_Field) {
1210
+        if ( ! $field_settings instanceof EE_Datetime_Field) {
1211 1211
             EE_Error::add_error(
1212 1212
                 sprintf(
1213 1213
                     esc_html__(
@@ -1222,8 +1222,8 @@  discard block
 block discarded – undo
1222 1222
             );
1223 1223
             return false;
1224 1224
         }
1225
-        return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime
1226
-            ? clone $this->_fields[ $field_name ]
1225
+        return isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime
1226
+            ? clone $this->_fields[$field_name]
1227 1227
             : null;
1228 1228
     }
1229 1229
 
@@ -1465,7 +1465,7 @@  discard block
 block discarded – undo
1465 1465
      */
1466 1466
     public function get_i18n_datetime($field_name, $format = '')
1467 1467
     {
1468
-        $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format;
1468
+        $format = empty($format) ? $this->_dt_frmt.' '.$this->_tm_frmt : $format;
1469 1469
         return date_i18n(
1470 1470
             $format,
1471 1471
             EEH_DTT_Helper::get_timestamp_with_offset(
@@ -1577,19 +1577,19 @@  discard block
 block discarded – undo
1577 1577
         $field->set_time_format($this->_tm_frmt);
1578 1578
         switch ($what) {
1579 1579
             case 'T':
1580
-                $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time(
1580
+                $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time(
1581 1581
                     $datetime_value,
1582
-                    $this->_fields[ $fieldname ]
1582
+                    $this->_fields[$fieldname]
1583 1583
                 );
1584 1584
                 break;
1585 1585
             case 'D':
1586
-                $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date(
1586
+                $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date(
1587 1587
                     $datetime_value,
1588
-                    $this->_fields[ $fieldname ]
1588
+                    $this->_fields[$fieldname]
1589 1589
                 );
1590 1590
                 break;
1591 1591
             case 'B':
1592
-                $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value);
1592
+                $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value);
1593 1593
                 break;
1594 1594
         }
1595 1595
         $this->_clear_cached_property($fieldname);
@@ -1631,7 +1631,7 @@  discard block
 block discarded – undo
1631 1631
         $this->set_timezone($timezone);
1632 1632
         $fn = (array) $field_name;
1633 1633
         $args = array_merge($fn, (array) $args);
1634
-        if (! method_exists($this, $callback)) {
1634
+        if ( ! method_exists($this, $callback)) {
1635 1635
             throw new EE_Error(
1636 1636
                 sprintf(
1637 1637
                     esc_html__(
@@ -1643,7 +1643,7 @@  discard block
 block discarded – undo
1643 1643
             );
1644 1644
         }
1645 1645
         $args = (array) $args;
1646
-        $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append;
1646
+        $return = $prepend.call_user_func_array(array($this, $callback), $args).$append;
1647 1647
         $this->set_timezone($original_timezone);
1648 1648
         return $return;
1649 1649
     }
@@ -1758,8 +1758,8 @@  discard block
 block discarded – undo
1758 1758
     {
1759 1759
         $model = $this->get_model();
1760 1760
         foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1761
-            if (! empty($this->_model_relations[ $relation_name ])) {
1762
-                $related_objects = $this->_model_relations[ $relation_name ];
1761
+            if ( ! empty($this->_model_relations[$relation_name])) {
1762
+                $related_objects = $this->_model_relations[$relation_name];
1763 1763
                 if ($relation_obj instanceof EE_Belongs_To_Relation) {
1764 1764
                     // this relation only stores a single model object, not an array
1765 1765
                     // but let's make it consistent
@@ -1816,7 +1816,7 @@  discard block
 block discarded – undo
1816 1816
             $this->set($column, $value);
1817 1817
         }
1818 1818
         // no changes ? then don't do anything
1819
-        if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1819
+        if ( ! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1820 1820
             return 0;
1821 1821
         }
1822 1822
         /**
@@ -1826,7 +1826,7 @@  discard block
 block discarded – undo
1826 1826
          * @param EE_Base_Class $model_object the model object about to be saved.
1827 1827
          */
1828 1828
         do_action('AHEE__EE_Base_Class__save__begin', $this);
1829
-        if (! $this->allow_persist()) {
1829
+        if ( ! $this->allow_persist()) {
1830 1830
             return 0;
1831 1831
         }
1832 1832
         // now get current attribute values
@@ -1841,10 +1841,10 @@  discard block
 block discarded – undo
1841 1841
         if ($model->has_primary_key_field()) {
1842 1842
             if ($model->get_primary_key_field()->is_auto_increment()) {
1843 1843
                 // ok check if it's set, if so: update; if not, insert
1844
-                if (! empty($save_cols_n_values[ $model->primary_key_name() ])) {
1844
+                if ( ! empty($save_cols_n_values[$model->primary_key_name()])) {
1845 1845
                     $results = $model->update_by_ID($save_cols_n_values, $this->ID());
1846 1846
                 } else {
1847
-                    unset($save_cols_n_values[ $model->primary_key_name() ]);
1847
+                    unset($save_cols_n_values[$model->primary_key_name()]);
1848 1848
                     $results = $model->insert($save_cols_n_values);
1849 1849
                     if ($results) {
1850 1850
                         // if successful, set the primary key
@@ -1854,7 +1854,7 @@  discard block
 block discarded – undo
1854 1854
                         // will get added to the mapper before we can add this one!
1855 1855
                         // but if we just avoid using the SET method, all that headache can be avoided
1856 1856
                         $pk_field_name = $model->primary_key_name();
1857
-                        $this->_fields[ $pk_field_name ] = $results;
1857
+                        $this->_fields[$pk_field_name] = $results;
1858 1858
                         $this->_clear_cached_property($pk_field_name);
1859 1859
                         $model->add_to_entity_map($this);
1860 1860
                         $this->_update_cached_related_model_objs_fks();
@@ -1871,8 +1871,8 @@  discard block
 block discarded – undo
1871 1871
                                     'event_espresso'
1872 1872
                                 ),
1873 1873
                                 get_class($this),
1874
-                                get_class($model) . '::instance()->add_to_entity_map()',
1875
-                                get_class($model) . '::instance()->get_one_by_ID()',
1874
+                                get_class($model).'::instance()->add_to_entity_map()',
1875
+                                get_class($model).'::instance()->get_one_by_ID()',
1876 1876
                                 '<br />'
1877 1877
                             )
1878 1878
                         );
@@ -1974,27 +1974,27 @@  discard block
 block discarded – undo
1974 1974
     public function save_new_cached_related_model_objs()
1975 1975
     {
1976 1976
         // make sure this has been saved
1977
-        if (! $this->ID()) {
1977
+        if ( ! $this->ID()) {
1978 1978
             $id = $this->save();
1979 1979
         } else {
1980 1980
             $id = $this->ID();
1981 1981
         }
1982 1982
         // now save all the NEW cached model objects  (ie they don't exist in the DB)
1983 1983
         foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) {
1984
-            if ($this->_model_relations[ $relationName ]) {
1984
+            if ($this->_model_relations[$relationName]) {
1985 1985
                 // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation)
1986 1986
                 // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)?
1987 1987
                 /* @var $related_model_obj EE_Base_Class */
1988 1988
                 if ($relationObj instanceof EE_Belongs_To_Relation) {
1989 1989
                     // add a relation to that relation type (which saves the appropriate thing in the process)
1990 1990
                     // but ONLY if it DOES NOT exist in the DB
1991
-                    $related_model_obj = $this->_model_relations[ $relationName ];
1991
+                    $related_model_obj = $this->_model_relations[$relationName];
1992 1992
                     // if( ! $related_model_obj->ID()){
1993 1993
                     $this->_add_relation_to($related_model_obj, $relationName);
1994 1994
                     $related_model_obj->save_new_cached_related_model_objs();
1995 1995
                     // }
1996 1996
                 } else {
1997
-                    foreach ($this->_model_relations[ $relationName ] as $related_model_obj) {
1997
+                    foreach ($this->_model_relations[$relationName] as $related_model_obj) {
1998 1998
                         // add a relation to that relation type (which saves the appropriate thing in the process)
1999 1999
                         // but ONLY if it DOES NOT exist in the DB
2000 2000
                         // if( ! $related_model_obj->ID()){
@@ -2021,7 +2021,7 @@  discard block
 block discarded – undo
2021 2021
      */
2022 2022
     public function get_model()
2023 2023
     {
2024
-        if (! $this->_model) {
2024
+        if ( ! $this->_model) {
2025 2025
             $modelName = self::_get_model_classname(get_class($this));
2026 2026
             $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone);
2027 2027
         } else {
@@ -2046,9 +2046,9 @@  discard block
 block discarded – undo
2046 2046
         // TODO: will not work for Term_Relationships because they have no PK!
2047 2047
         $primary_id_ref = self::_get_primary_key_name($classname);
2048 2048
         if (array_key_exists($primary_id_ref, $props_n_values)
2049
-            && ! empty($props_n_values[ $primary_id_ref ])
2049
+            && ! empty($props_n_values[$primary_id_ref])
2050 2050
         ) {
2051
-            $id = $props_n_values[ $primary_id_ref ];
2051
+            $id = $props_n_values[$primary_id_ref];
2052 2052
             return self::_get_model($classname)->get_from_entity_map($id);
2053 2053
         }
2054 2054
         return false;
@@ -2082,10 +2082,10 @@  discard block
 block discarded – undo
2082 2082
         if ($model->has_primary_key_field()) {
2083 2083
             $primary_id_ref = self::_get_primary_key_name($classname);
2084 2084
             if (array_key_exists($primary_id_ref, $props_n_values)
2085
-                && ! empty($props_n_values[ $primary_id_ref ])
2085
+                && ! empty($props_n_values[$primary_id_ref])
2086 2086
             ) {
2087 2087
                 $existing = $model->get_one_by_ID(
2088
-                    $props_n_values[ $primary_id_ref ]
2088
+                    $props_n_values[$primary_id_ref]
2089 2089
                 );
2090 2090
             }
2091 2091
         } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) {
@@ -2097,7 +2097,7 @@  discard block
 block discarded – undo
2097 2097
         }
2098 2098
         if ($existing) {
2099 2099
             // set date formats if present before setting values
2100
-            if (! empty($date_formats) && is_array($date_formats)) {
2100
+            if ( ! empty($date_formats) && is_array($date_formats)) {
2101 2101
                 $existing->set_date_format($date_formats[0]);
2102 2102
                 $existing->set_time_format($date_formats[1]);
2103 2103
             } else {
@@ -2130,7 +2130,7 @@  discard block
 block discarded – undo
2130 2130
     protected static function _get_model($classname, $timezone = null)
2131 2131
     {
2132 2132
         // find model for this class
2133
-        if (! $classname) {
2133
+        if ( ! $classname) {
2134 2134
             throw new EE_Error(
2135 2135
                 sprintf(
2136 2136
                     esc_html__(
@@ -2179,7 +2179,7 @@  discard block
 block discarded – undo
2179 2179
         if (strpos($model_name, 'EE_') === 0) {
2180 2180
             $model_classname = str_replace('EE_', 'EEM_', $model_name);
2181 2181
         } else {
2182
-            $model_classname = 'EEM_' . $model_name;
2182
+            $model_classname = 'EEM_'.$model_name;
2183 2183
         }
2184 2184
         return $model_classname;
2185 2185
     }
@@ -2198,7 +2198,7 @@  discard block
 block discarded – undo
2198 2198
      */
2199 2199
     protected static function _get_primary_key_name($classname = null)
2200 2200
     {
2201
-        if (! $classname) {
2201
+        if ( ! $classname) {
2202 2202
             throw new EE_Error(
2203 2203
                 sprintf(
2204 2204
                     esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'),
@@ -2228,7 +2228,7 @@  discard block
 block discarded – undo
2228 2228
         $model = $this->get_model();
2229 2229
         // now that we know the name of the variable, use a variable variable to get its value and return its
2230 2230
         if ($model->has_primary_key_field()) {
2231
-            return $this->_fields[ $model->primary_key_name() ];
2231
+            return $this->_fields[$model->primary_key_name()];
2232 2232
         }
2233 2233
         return $model->get_index_primary_key_string($this->_fields);
2234 2234
     }
@@ -2281,7 +2281,7 @@  discard block
 block discarded – undo
2281 2281
             }
2282 2282
         } else {
2283 2283
             // this thing doesn't exist in the DB,  so just cache it
2284
-            if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) {
2284
+            if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) {
2285 2285
                 throw new EE_Error(
2286 2286
                     sprintf(
2287 2287
                         esc_html__(
@@ -2446,7 +2446,7 @@  discard block
 block discarded – undo
2446 2446
             } else {
2447 2447
                 // did we already cache the result of this query?
2448 2448
                 $cached_results = $this->get_all_from_cache($relationName);
2449
-                if (! $cached_results) {
2449
+                if ( ! $cached_results) {
2450 2450
                     $related_model_objects = $this->get_model()->get_all_related(
2451 2451
                         $this,
2452 2452
                         $relationName,
@@ -2556,7 +2556,7 @@  discard block
 block discarded – undo
2556 2556
             } else {
2557 2557
                 // first, check if we've already cached the result of this query
2558 2558
                 $cached_result = $this->get_one_from_cache($relationName);
2559
-                if (! $cached_result) {
2559
+                if ( ! $cached_result) {
2560 2560
                     $related_model_object = $model->get_first_related(
2561 2561
                         $this,
2562 2562
                         $relationName,
@@ -2580,7 +2580,7 @@  discard block
 block discarded – undo
2580 2580
             }
2581 2581
             // this doesn't exist in the DB and apparently the thing it belongs to doesn't either,
2582 2582
             // just get what's cached on this object
2583
-            if (! $related_model_object) {
2583
+            if ( ! $related_model_object) {
2584 2584
                 $related_model_object = $this->get_one_from_cache($relationName);
2585 2585
             }
2586 2586
         }
@@ -2662,7 +2662,7 @@  discard block
 block discarded – undo
2662 2662
      */
2663 2663
     public function is_set($field_name)
2664 2664
     {
2665
-        return isset($this->_fields[ $field_name ]);
2665
+        return isset($this->_fields[$field_name]);
2666 2666
     }
2667 2667
 
2668 2668
 
@@ -2678,7 +2678,7 @@  discard block
 block discarded – undo
2678 2678
     {
2679 2679
         foreach ((array) $properties as $property_name) {
2680 2680
             // first make sure this property exists
2681
-            if (! $this->_fields[ $property_name ]) {
2681
+            if ( ! $this->_fields[$property_name]) {
2682 2682
                 throw new EE_Error(
2683 2683
                     sprintf(
2684 2684
                         esc_html__(
@@ -2710,7 +2710,7 @@  discard block
 block discarded – undo
2710 2710
         $properties = array();
2711 2711
         // remove prepended underscore
2712 2712
         foreach ($fields as $field_name => $settings) {
2713
-            $properties[ $field_name ] = $this->get($field_name);
2713
+            $properties[$field_name] = $this->get($field_name);
2714 2714
         }
2715 2715
         return $properties;
2716 2716
     }
@@ -2747,7 +2747,7 @@  discard block
 block discarded – undo
2747 2747
     {
2748 2748
         $className = get_class($this);
2749 2749
         $tagName = "FHEE__{$className}__{$methodName}";
2750
-        if (! has_filter($tagName)) {
2750
+        if ( ! has_filter($tagName)) {
2751 2751
             throw new EE_Error(
2752 2752
                 sprintf(
2753 2753
                     esc_html__(
@@ -2792,7 +2792,7 @@  discard block
 block discarded – undo
2792 2792
             $query_params[0]['EXM_value'] = $meta_value;
2793 2793
         }
2794 2794
         $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params);
2795
-        if (! $existing_rows_like_that) {
2795
+        if ( ! $existing_rows_like_that) {
2796 2796
             return $this->add_extra_meta($meta_key, $meta_value);
2797 2797
         }
2798 2798
         foreach ($existing_rows_like_that as $existing_row) {
@@ -2910,7 +2910,7 @@  discard block
 block discarded – undo
2910 2910
                 $values = array();
2911 2911
                 foreach ($results as $result) {
2912 2912
                     if ($result instanceof EE_Extra_Meta) {
2913
-                        $values[ $result->ID() ] = $result->value();
2913
+                        $values[$result->ID()] = $result->value();
2914 2914
                     }
2915 2915
                 }
2916 2916
                 return $values;
@@ -2955,17 +2955,17 @@  discard block
 block discarded – undo
2955 2955
             );
2956 2956
             foreach ($extra_meta_objs as $extra_meta_obj) {
2957 2957
                 if ($extra_meta_obj instanceof EE_Extra_Meta) {
2958
-                    $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value();
2958
+                    $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value();
2959 2959
                 }
2960 2960
             }
2961 2961
         } else {
2962 2962
             $extra_meta_objs = $this->get_many_related('Extra_Meta');
2963 2963
             foreach ($extra_meta_objs as $extra_meta_obj) {
2964 2964
                 if ($extra_meta_obj instanceof EE_Extra_Meta) {
2965
-                    if (! isset($return_array[ $extra_meta_obj->key() ])) {
2966
-                        $return_array[ $extra_meta_obj->key() ] = array();
2965
+                    if ( ! isset($return_array[$extra_meta_obj->key()])) {
2966
+                        $return_array[$extra_meta_obj->key()] = array();
2967 2967
                     }
2968
-                    $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value();
2968
+                    $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value();
2969 2969
                 }
2970 2970
             }
2971 2971
         }
@@ -3046,8 +3046,8 @@  discard block
 block discarded – undo
3046 3046
                             'event_espresso'
3047 3047
                         ),
3048 3048
                         $this->ID(),
3049
-                        get_class($this->get_model()) . '::instance()->add_to_entity_map()',
3050
-                        get_class($this->get_model()) . '::instance()->refresh_entity_map()'
3049
+                        get_class($this->get_model()).'::instance()->add_to_entity_map()',
3050
+                        get_class($this->get_model()).'::instance()->refresh_entity_map()'
3051 3051
                     )
3052 3052
                 );
3053 3053
             }
@@ -3080,7 +3080,7 @@  discard block
 block discarded – undo
3080 3080
     {
3081 3081
         // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB
3082 3082
         // if it wasn't even there to start off.
3083
-        if (! $this->ID()) {
3083
+        if ( ! $this->ID()) {
3084 3084
             $this->save();
3085 3085
         }
3086 3086
         global $wpdb;
@@ -3308,7 +3308,7 @@  discard block
 block discarded – undo
3308 3308
         $model = $this->get_model();
3309 3309
         foreach ($model->relation_settings() as $relation_name => $relation_obj) {
3310 3310
             if ($relation_obj instanceof EE_Belongs_To_Relation) {
3311
-                $classname = 'EE_' . $model->get_this_model_name();
3311
+                $classname = 'EE_'.$model->get_this_model_name();
3312 3312
                 if ($this->get_one_from_cache($relation_name) instanceof $classname
3313 3313
                     && $this->get_one_from_cache($relation_name)->ID()
3314 3314
                 ) {
@@ -3348,7 +3348,7 @@  discard block
 block discarded – undo
3348 3348
         // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes).
3349 3349
         foreach ($this->_fields as $field => $value) {
3350 3350
             if ($value instanceof DateTime) {
3351
-                $this->_fields[ $field ] = clone $value;
3351
+                $this->_fields[$field] = clone $value;
3352 3352
             }
3353 3353
         }
3354 3354
     }
Please login to merge, or discard this patch.
Indentation   +3330 added lines, -3330 removed lines patch added patch discarded remove patch
@@ -13,3345 +13,3345 @@
 block discarded – undo
13 13
 abstract class EE_Base_Class
14 14
 {
15 15
 
16
-    /**
17
-     * This is an array of the original properties and values provided during construction
18
-     * of this model object. (keys are model field names, values are their values).
19
-     * This list is important to remember so that when we are merging data from the db, we know
20
-     * which values to override and which to not override.
21
-     *
22
-     * @var array
23
-     */
24
-    protected $_props_n_values_provided_in_constructor;
25
-
26
-    /**
27
-     * Timezone
28
-     * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in.
29
-     * This can also be used before a get to set what timezone you want strings coming out of the object to be in.  NOT
30
-     * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have
31
-     * access to it.
32
-     *
33
-     * @var string
34
-     */
35
-    protected $_timezone;
36
-
37
-    /**
38
-     * date format
39
-     * pattern or format for displaying dates
40
-     *
41
-     * @var string $_dt_frmt
42
-     */
43
-    protected $_dt_frmt;
44
-
45
-    /**
46
-     * time format
47
-     * pattern or format for displaying time
48
-     *
49
-     * @var string $_tm_frmt
50
-     */
51
-    protected $_tm_frmt;
52
-
53
-    /**
54
-     * This property is for holding a cached array of object properties indexed by property name as the key.
55
-     * The purpose of this is for setting a cache on properties that may have calculated values after a
56
-     * prepare_for_get.  That way the cache can be checked first and the calculated property returned instead of having
57
-     * to recalculate. Used by _set_cached_property() and _get_cached_property() methods.
58
-     *
59
-     * @var array
60
-     */
61
-    protected $_cached_properties = array();
62
-
63
-    /**
64
-     * An array containing keys of the related model, and values are either an array of related mode objects or a
65
-     * single
66
-     * related model object. see the model's _model_relations. The keys should match those specified. And if the
67
-     * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object,
68
-     * all others have an array)
69
-     *
70
-     * @var array
71
-     */
72
-    protected $_model_relations = array();
73
-
74
-    /**
75
-     * Array where keys are field names (see the model's _fields property) and values are their values. To see what
76
-     * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods)
77
-     *
78
-     * @var array
79
-     */
80
-    protected $_fields = array();
81
-
82
-    /**
83
-     * @var boolean indicating whether or not this model object is intended to ever be saved
84
-     * For example, we might create model objects intended to only be used for the duration
85
-     * of this request and to be thrown away, and if they were accidentally saved
86
-     * it would be a bug.
87
-     */
88
-    protected $_allow_persist = true;
89
-
90
-    /**
91
-     * @var boolean indicating whether or not this model object's properties have changed since construction
92
-     */
93
-    protected $_has_changes = false;
94
-
95
-    /**
96
-     * @var EEM_Base
97
-     */
98
-    protected $_model;
99
-
100
-    /**
101
-     * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose
102
-     * for these values is for retrieval of the results, they are not further queryable and they are not persisted to
103
-     * the db.  They also do not automatically update if there are any changes to the data that produced their results.
104
-     * The format is a simple array of field_alias => field_value.  So for instance if a custom select was something
105
-     * like,  "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this
106
-     * array as:
107
-     * array(
108
-     *  'Registration_Count' => 24
109
-     * );
110
-     * Note: if the custom select configuration for the query included a data type, the value will be in the data type
111
-     * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more
112
-     * info)
113
-     *
114
-     * @var array
115
-     */
116
-    protected $custom_selection_results = array();
117
-
118
-
119
-    /**
120
-     * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children
121
-     * play nice
122
-     *
123
-     * @param array   $fieldValues                             where each key is a field (ie, array key in the 2nd
124
-     *                                                         layer of the model's _fields array, (eg, EVT_ID,
125
-     *                                                         TXN_amount, QST_name, etc) and values are their values
126
-     * @param boolean $bydb                                    a flag for setting if the class is instantiated by the
127
-     *                                                         corresponding db model or not.
128
-     * @param string  $timezone                                indicate what timezone you want any datetime fields to
129
-     *                                                         be in when instantiating a EE_Base_Class object.
130
-     * @param array   $date_formats                            An array of date formats to set on construct where first
131
-     *                                                         value is the date_format and second value is the time
132
-     *                                                         format.
133
-     * @throws InvalidArgumentException
134
-     * @throws InvalidInterfaceException
135
-     * @throws InvalidDataTypeException
136
-     * @throws EE_Error
137
-     * @throws ReflectionException
138
-     */
139
-    protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array())
140
-    {
141
-        $className = get_class($this);
142
-        do_action("AHEE__{$className}__construct", $this, $fieldValues);
143
-        $model = $this->get_model();
144
-        $model_fields = $model->field_settings(false);
145
-        // ensure $fieldValues is an array
146
-        $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues);
147
-        // verify client code has not passed any invalid field names
148
-        foreach ($fieldValues as $field_name => $field_value) {
149
-            if (! isset($model_fields[ $field_name ])) {
150
-                throw new EE_Error(
151
-                    sprintf(
152
-                        esc_html__(
153
-                            'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s',
154
-                            'event_espresso'
155
-                        ),
156
-                        $field_name,
157
-                        get_class($this),
158
-                        implode(', ', array_keys($model_fields))
159
-                    )
160
-                );
161
-            }
162
-        }
163
-        $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
164
-        if (! empty($date_formats) && is_array($date_formats)) {
165
-            list($this->_dt_frmt, $this->_tm_frmt) = $date_formats;
166
-        } else {
167
-            // set default formats for date and time
168
-            $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d');
169
-            $this->_tm_frmt = (string) get_option('time_format', 'g:i a');
170
-        }
171
-        // if db model is instantiating
172
-        if ($bydb) {
173
-            // client code has indicated these field values are from the database
174
-            foreach ($model_fields as $fieldName => $field) {
175
-                $this->set_from_db(
176
-                    $fieldName,
177
-                    isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null
178
-                );
179
-            }
180
-        } else {
181
-            // we're constructing a brand
182
-            // new instance of the model object. Generally, this means we'll need to do more field validation
183
-            foreach ($model_fields as $fieldName => $field) {
184
-                $this->set(
185
-                    $fieldName,
186
-                    isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null,
187
-                    true
188
-                );
189
-            }
190
-        }
191
-        // remember what values were passed to this constructor
192
-        $this->_props_n_values_provided_in_constructor = $fieldValues;
193
-        // remember in entity mapper
194
-        if (! $bydb && $model->has_primary_key_field() && $this->ID()) {
195
-            $model->add_to_entity_map($this);
196
-        }
197
-        // setup all the relations
198
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
199
-            if ($relation_obj instanceof EE_Belongs_To_Relation) {
200
-                $this->_model_relations[ $relation_name ] = null;
201
-            } else {
202
-                $this->_model_relations[ $relation_name ] = array();
203
-            }
204
-        }
205
-        /**
206
-         * Action done at the end of each model object construction
207
-         *
208
-         * @param EE_Base_Class $this the model object just created
209
-         */
210
-        do_action('AHEE__EE_Base_Class__construct__finished', $this);
211
-    }
212
-
213
-
214
-    /**
215
-     * Gets whether or not this model object is allowed to persist/be saved to the database.
216
-     *
217
-     * @return boolean
218
-     */
219
-    public function allow_persist()
220
-    {
221
-        return $this->_allow_persist;
222
-    }
223
-
224
-
225
-    /**
226
-     * Sets whether or not this model object should be allowed to be saved to the DB.
227
-     * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless
228
-     * you got new information that somehow made you change your mind.
229
-     *
230
-     * @param boolean $allow_persist
231
-     * @return boolean
232
-     */
233
-    public function set_allow_persist($allow_persist)
234
-    {
235
-        return $this->_allow_persist = $allow_persist;
236
-    }
237
-
238
-
239
-    /**
240
-     * Gets the field's original value when this object was constructed during this request.
241
-     * This can be helpful when determining if a model object has changed or not
242
-     *
243
-     * @param string $field_name
244
-     * @return mixed|null
245
-     * @throws ReflectionException
246
-     * @throws InvalidArgumentException
247
-     * @throws InvalidInterfaceException
248
-     * @throws InvalidDataTypeException
249
-     * @throws EE_Error
250
-     */
251
-    public function get_original($field_name)
252
-    {
253
-        if (isset($this->_props_n_values_provided_in_constructor[ $field_name ])
254
-            && $field_settings = $this->get_model()->field_settings_for($field_name)
255
-        ) {
256
-            return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]);
257
-        }
258
-        return null;
259
-    }
260
-
261
-
262
-    /**
263
-     * @param EE_Base_Class $obj
264
-     * @return string
265
-     */
266
-    public function get_class($obj)
267
-    {
268
-        return get_class($obj);
269
-    }
270
-
271
-
272
-    /**
273
-     * Overrides parent because parent expects old models.
274
-     * This also doesn't do any validation, and won't work for serialized arrays
275
-     *
276
-     * @param    string $field_name
277
-     * @param    mixed  $field_value
278
-     * @param bool      $use_default
279
-     * @throws InvalidArgumentException
280
-     * @throws InvalidInterfaceException
281
-     * @throws InvalidDataTypeException
282
-     * @throws EE_Error
283
-     * @throws ReflectionException
284
-     * @throws ReflectionException
285
-     * @throws ReflectionException
286
-     */
287
-    public function set($field_name, $field_value, $use_default = false)
288
-    {
289
-        // if not using default and nothing has changed, and object has already been setup (has ID),
290
-        // then don't do anything
291
-        if (! $use_default
292
-            && $this->_fields[ $field_name ] === $field_value
293
-            && $this->ID()
294
-        ) {
295
-            return;
296
-        }
297
-        $model = $this->get_model();
298
-        $this->_has_changes = true;
299
-        $field_obj = $model->field_settings_for($field_name);
300
-        if ($field_obj instanceof EE_Model_Field_Base) {
301
-            // if ( method_exists( $field_obj, 'set_timezone' )) {
302
-            if ($field_obj instanceof EE_Datetime_Field) {
303
-                $field_obj->set_timezone($this->_timezone);
304
-                $field_obj->set_date_format($this->_dt_frmt);
305
-                $field_obj->set_time_format($this->_tm_frmt);
306
-            }
307
-            $holder_of_value = $field_obj->prepare_for_set($field_value);
308
-            // should the value be null?
309
-            if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) {
310
-                $this->_fields[ $field_name ] = $field_obj->get_default_value();
311
-                /**
312
-                 * To save having to refactor all the models, if a default value is used for a
313
-                 * EE_Datetime_Field, and that value is not null nor is it a DateTime
314
-                 * object.  Then let's do a set again to ensure that it becomes a DateTime
315
-                 * object.
316
-                 *
317
-                 * @since 4.6.10+
318
-                 */
319
-                if ($field_obj instanceof EE_Datetime_Field
320
-                    && $this->_fields[ $field_name ] !== null
321
-                    && ! $this->_fields[ $field_name ] instanceof DateTime
322
-                ) {
323
-                    empty($this->_fields[ $field_name ])
324
-                        ? $this->set($field_name, time())
325
-                        : $this->set($field_name, $this->_fields[ $field_name ]);
326
-                }
327
-            } else {
328
-                $this->_fields[ $field_name ] = $holder_of_value;
329
-            }
330
-            // if we're not in the constructor...
331
-            // now check if what we set was a primary key
332
-            if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor
333
-                $this->_props_n_values_provided_in_constructor
334
-                && $field_value
335
-                && $field_name === $model->primary_key_name()
336
-            ) {
337
-                // if so, we want all this object's fields to be filled either with
338
-                // what we've explicitly set on this model
339
-                // or what we have in the db
340
-                // echo "setting primary key!";
341
-                $fields_on_model = self::_get_model(get_class($this))->field_settings();
342
-                $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value);
343
-                foreach ($fields_on_model as $field_obj) {
344
-                    if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor)
345
-                        && $field_obj->get_name() !== $field_name
346
-                    ) {
347
-                        $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name()));
348
-                    }
349
-                }
350
-                // oh this model object has an ID? well make sure its in the entity mapper
351
-                $model->add_to_entity_map($this);
352
-            }
353
-            // let's unset any cache for this field_name from the $_cached_properties property.
354
-            $this->_clear_cached_property($field_name);
355
-        } else {
356
-            throw new EE_Error(
357
-                sprintf(
358
-                    esc_html__(
359
-                        'A valid EE_Model_Field_Base could not be found for the given field name: %s',
360
-                        'event_espresso'
361
-                    ),
362
-                    $field_name
363
-                )
364
-            );
365
-        }
366
-    }
367
-
368
-
369
-    /**
370
-     * Set custom select values for model.
371
-     *
372
-     * @param array $custom_select_values
373
-     */
374
-    public function setCustomSelectsValues(array $custom_select_values)
375
-    {
376
-        $this->custom_selection_results = $custom_select_values;
377
-    }
378
-
379
-
380
-    /**
381
-     * Returns the custom select value for the provided alias if its set.
382
-     * If not set, returns null.
383
-     *
384
-     * @param string $alias
385
-     * @return string|int|float|null
386
-     */
387
-    public function getCustomSelect($alias)
388
-    {
389
-        return isset($this->custom_selection_results[ $alias ])
390
-            ? $this->custom_selection_results[ $alias ]
391
-            : null;
392
-    }
393
-
394
-
395
-    /**
396
-     * This sets the field value on the db column if it exists for the given $column_name or
397
-     * saves it to EE_Extra_Meta if the given $column_name does not match a db column.
398
-     *
399
-     * @see EE_message::get_column_value for related documentation on the necessity of this method.
400
-     * @param string $field_name  Must be the exact column name.
401
-     * @param mixed  $field_value The value to set.
402
-     * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs.
403
-     * @throws InvalidArgumentException
404
-     * @throws InvalidInterfaceException
405
-     * @throws InvalidDataTypeException
406
-     * @throws EE_Error
407
-     * @throws ReflectionException
408
-     */
409
-    public function set_field_or_extra_meta($field_name, $field_value)
410
-    {
411
-        if ($this->get_model()->has_field($field_name)) {
412
-            $this->set($field_name, $field_value);
413
-            return true;
414
-        }
415
-        // ensure this object is saved first so that extra meta can be properly related.
416
-        $this->save();
417
-        return $this->update_extra_meta($field_name, $field_value);
418
-    }
419
-
420
-
421
-    /**
422
-     * This retrieves the value of the db column set on this class or if that's not present
423
-     * it will attempt to retrieve from extra_meta if found.
424
-     * Example Usage:
425
-     * Via EE_Message child class:
426
-     * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to",
427
-     * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may
428
-     * also have additional main fields specific to the messenger.  The system accommodates those extra
429
-     * fields through the EE_Extra_Meta table.  This method allows for EE_messengers to retrieve the
430
-     * value for those extra fields dynamically via the EE_message object.
431
-     *
432
-     * @param  string $field_name expecting the fully qualified field name.
433
-     * @return mixed|null  value for the field if found.  null if not found.
434
-     * @throws ReflectionException
435
-     * @throws InvalidArgumentException
436
-     * @throws InvalidInterfaceException
437
-     * @throws InvalidDataTypeException
438
-     * @throws EE_Error
439
-     */
440
-    public function get_field_or_extra_meta($field_name)
441
-    {
442
-        if ($this->get_model()->has_field($field_name)) {
443
-            $column_value = $this->get($field_name);
444
-        } else {
445
-            // This isn't a column in the main table, let's see if it is in the extra meta.
446
-            $column_value = $this->get_extra_meta($field_name, true, null);
447
-        }
448
-        return $column_value;
449
-    }
450
-
451
-
452
-    /**
453
-     * See $_timezone property for description of what the timezone property is for.  This SETS the timezone internally
454
-     * for being able to reference what timezone we are running conversions on when converting TO the internal timezone
455
-     * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is
456
-     * available to all child classes that may be using the EE_Datetime_Field for a field data type.
457
-     *
458
-     * @access public
459
-     * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php
460
-     * @return void
461
-     * @throws InvalidArgumentException
462
-     * @throws InvalidInterfaceException
463
-     * @throws InvalidDataTypeException
464
-     * @throws EE_Error
465
-     * @throws ReflectionException
466
-     */
467
-    public function set_timezone($timezone = '')
468
-    {
469
-        $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
470
-        // make sure we clear all cached properties because they won't be relevant now
471
-        $this->_clear_cached_properties();
472
-        // make sure we update field settings and the date for all EE_Datetime_Fields
473
-        $model_fields = $this->get_model()->field_settings(false);
474
-        foreach ($model_fields as $field_name => $field_obj) {
475
-            if ($field_obj instanceof EE_Datetime_Field) {
476
-                $field_obj->set_timezone($this->_timezone);
477
-                if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) {
478
-                    EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone));
479
-                }
480
-            }
481
-        }
482
-    }
483
-
484
-
485
-    /**
486
-     * This just returns whatever is set for the current timezone.
487
-     *
488
-     * @access public
489
-     * @return string timezone string
490
-     */
491
-    public function get_timezone()
492
-    {
493
-        return $this->_timezone;
494
-    }
495
-
496
-
497
-    /**
498
-     * This sets the internal date format to what is sent in to be used as the new default for the class
499
-     * internally instead of wp set date format options
500
-     *
501
-     * @since 4.6
502
-     * @param string $format should be a format recognizable by PHP date() functions.
503
-     */
504
-    public function set_date_format($format)
505
-    {
506
-        $this->_dt_frmt = $format;
507
-        // clear cached_properties because they won't be relevant now.
508
-        $this->_clear_cached_properties();
509
-    }
510
-
511
-
512
-    /**
513
-     * This sets the internal time format string to what is sent in to be used as the new default for the
514
-     * class internally instead of wp set time format options.
515
-     *
516
-     * @since 4.6
517
-     * @param string $format should be a format recognizable by PHP date() functions.
518
-     */
519
-    public function set_time_format($format)
520
-    {
521
-        $this->_tm_frmt = $format;
522
-        // clear cached_properties because they won't be relevant now.
523
-        $this->_clear_cached_properties();
524
-    }
525
-
526
-
527
-    /**
528
-     * This returns the current internal set format for the date and time formats.
529
-     *
530
-     * @param bool $full           if true (default), then return the full format.  Otherwise will return an array
531
-     *                             where the first value is the date format and the second value is the time format.
532
-     * @return mixed string|array
533
-     */
534
-    public function get_format($full = true)
535
-    {
536
-        return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
537
-    }
538
-
539
-
540
-    /**
541
-     * cache
542
-     * stores the passed model object on the current model object.
543
-     * In certain circumstances, we can use this cached model object instead of querying for another one entirely.
544
-     *
545
-     * @param string        $relationName    one of the keys in the _model_relations array on the model. Eg
546
-     *                                       'Registration' associated with this model object
547
-     * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction,
548
-     *                                       that could be a payment or a registration)
549
-     * @param null          $cache_id        a string or number that will be used as the key for any Belongs_To_Many
550
-     *                                       items which will be stored in an array on this object
551
-     * @throws ReflectionException
552
-     * @throws InvalidArgumentException
553
-     * @throws InvalidInterfaceException
554
-     * @throws InvalidDataTypeException
555
-     * @throws EE_Error
556
-     * @return mixed    index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one
557
-     *                                       related thing, no array)
558
-     */
559
-    public function cache($relationName = '', $object_to_cache = null, $cache_id = null)
560
-    {
561
-        // its entirely possible that there IS no related object yet in which case there is nothing to cache.
562
-        if (! $object_to_cache instanceof EE_Base_Class) {
563
-            return false;
564
-        }
565
-        // also get "how" the object is related, or throw an error
566
-        if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) {
567
-            throw new EE_Error(
568
-                sprintf(
569
-                    esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'),
570
-                    $relationName,
571
-                    get_class($this)
572
-                )
573
-            );
574
-        }
575
-        // how many things are related ?
576
-        if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
577
-            // if it's a "belongs to" relationship, then there's only one related model object
578
-            // eg, if this is a registration, there's only 1 attendee for it
579
-            // so for these model objects just set it to be cached
580
-            $this->_model_relations[ $relationName ] = $object_to_cache;
581
-            $return = true;
582
-        } else {
583
-            // otherwise, this is the "many" side of a one to many relationship,
584
-            // so we'll add the object to the array of related objects for that type.
585
-            // eg: if this is an event, there are many registrations for that event,
586
-            // so we cache the registrations in an array
587
-            if (! is_array($this->_model_relations[ $relationName ])) {
588
-                // if for some reason, the cached item is a model object,
589
-                // then stick that in the array, otherwise start with an empty array
590
-                $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ]
591
-                                                           instanceof
592
-                                                           EE_Base_Class
593
-                    ? array($this->_model_relations[ $relationName ]) : array();
594
-            }
595
-            // first check for a cache_id which is normally empty
596
-            if (! empty($cache_id)) {
597
-                // if the cache_id exists, then it means we are purposely trying to cache this
598
-                // with a known key that can then be used to retrieve the object later on
599
-                $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache;
600
-                $return = $cache_id;
601
-            } elseif ($object_to_cache->ID()) {
602
-                // OR the cached object originally came from the db, so let's just use it's PK for an ID
603
-                $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache;
604
-                $return = $object_to_cache->ID();
605
-            } else {
606
-                // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID
607
-                $this->_model_relations[ $relationName ][] = $object_to_cache;
608
-                // move the internal pointer to the end of the array
609
-                end($this->_model_relations[ $relationName ]);
610
-                // and grab the key so that we can return it
611
-                $return = key($this->_model_relations[ $relationName ]);
612
-            }
613
-        }
614
-        return $return;
615
-    }
616
-
617
-
618
-    /**
619
-     * For adding an item to the cached_properties property.
620
-     *
621
-     * @access protected
622
-     * @param string      $fieldname the property item the corresponding value is for.
623
-     * @param mixed       $value     The value we are caching.
624
-     * @param string|null $cache_type
625
-     * @return void
626
-     * @throws ReflectionException
627
-     * @throws InvalidArgumentException
628
-     * @throws InvalidInterfaceException
629
-     * @throws InvalidDataTypeException
630
-     * @throws EE_Error
631
-     */
632
-    protected function _set_cached_property($fieldname, $value, $cache_type = null)
633
-    {
634
-        // first make sure this property exists
635
-        $this->get_model()->field_settings_for($fieldname);
636
-        $cache_type = empty($cache_type) ? 'standard' : $cache_type;
637
-        $this->_cached_properties[ $fieldname ][ $cache_type ] = $value;
638
-    }
639
-
640
-
641
-    /**
642
-     * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist.
643
-     * This also SETS the cache if we return the actual property!
644
-     *
645
-     * @param string $fieldname        the name of the property we're trying to retrieve
646
-     * @param bool   $pretty
647
-     * @param string $extra_cache_ref  This allows the user to specify an extra cache ref for the given property
648
-     *                                 (in cases where the same property may be used for different outputs
649
-     *                                 - i.e. datetime, money etc.)
650
-     *                                 It can also accept certain pre-defined "schema" strings
651
-     *                                 to define how to output the property.
652
-     *                                 see the field's prepare_for_pretty_echoing for what strings can be used
653
-     * @return mixed                   whatever the value for the property is we're retrieving
654
-     * @throws ReflectionException
655
-     * @throws InvalidArgumentException
656
-     * @throws InvalidInterfaceException
657
-     * @throws InvalidDataTypeException
658
-     * @throws EE_Error
659
-     */
660
-    protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null)
661
-    {
662
-        // verify the field exists
663
-        $model = $this->get_model();
664
-        $model->field_settings_for($fieldname);
665
-        $cache_type = $pretty ? 'pretty' : 'standard';
666
-        $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : '';
667
-        if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) {
668
-            return $this->_cached_properties[ $fieldname ][ $cache_type ];
669
-        }
670
-        $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref);
671
-        $this->_set_cached_property($fieldname, $value, $cache_type);
672
-        return $value;
673
-    }
674
-
675
-
676
-    /**
677
-     * If the cache didn't fetch the needed item, this fetches it.
678
-     *
679
-     * @param string $fieldname
680
-     * @param bool   $pretty
681
-     * @param string $extra_cache_ref
682
-     * @return mixed
683
-     * @throws InvalidArgumentException
684
-     * @throws InvalidInterfaceException
685
-     * @throws InvalidDataTypeException
686
-     * @throws EE_Error
687
-     * @throws ReflectionException
688
-     */
689
-    protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null)
690
-    {
691
-        $field_obj = $this->get_model()->field_settings_for($fieldname);
692
-        // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct
693
-        if ($field_obj instanceof EE_Datetime_Field) {
694
-            $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref);
695
-        }
696
-        if (! isset($this->_fields[ $fieldname ])) {
697
-            $this->_fields[ $fieldname ] = null;
698
-        }
699
-        $value = $pretty
700
-            ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref)
701
-            : $field_obj->prepare_for_get($this->_fields[ $fieldname ]);
702
-        return $value;
703
-    }
704
-
705
-
706
-    /**
707
-     * set timezone, formats, and output for EE_Datetime_Field objects
708
-     *
709
-     * @param \EE_Datetime_Field $datetime_field
710
-     * @param bool               $pretty
711
-     * @param null               $date_or_time
712
-     * @return void
713
-     * @throws InvalidArgumentException
714
-     * @throws InvalidInterfaceException
715
-     * @throws InvalidDataTypeException
716
-     * @throws EE_Error
717
-     */
718
-    protected function _prepare_datetime_field(
719
-        EE_Datetime_Field $datetime_field,
720
-        $pretty = false,
721
-        $date_or_time = null
722
-    ) {
723
-        $datetime_field->set_timezone($this->_timezone);
724
-        $datetime_field->set_date_format($this->_dt_frmt, $pretty);
725
-        $datetime_field->set_time_format($this->_tm_frmt, $pretty);
726
-        // set the output returned
727
-        switch ($date_or_time) {
728
-            case 'D':
729
-                $datetime_field->set_date_time_output('date');
730
-                break;
731
-            case 'T':
732
-                $datetime_field->set_date_time_output('time');
733
-                break;
734
-            default:
735
-                $datetime_field->set_date_time_output();
736
-        }
737
-    }
738
-
739
-
740
-    /**
741
-     * This just takes care of clearing out the cached_properties
742
-     *
743
-     * @return void
744
-     */
745
-    protected function _clear_cached_properties()
746
-    {
747
-        $this->_cached_properties = array();
748
-    }
749
-
750
-
751
-    /**
752
-     * This just clears out ONE property if it exists in the cache
753
-     *
754
-     * @param  string $property_name the property to remove if it exists (from the _cached_properties array)
755
-     * @return void
756
-     */
757
-    protected function _clear_cached_property($property_name)
758
-    {
759
-        if (isset($this->_cached_properties[ $property_name ])) {
760
-            unset($this->_cached_properties[ $property_name ]);
761
-        }
762
-    }
763
-
764
-
765
-    /**
766
-     * Ensures that this related thing is a model object.
767
-     *
768
-     * @param mixed  $object_or_id EE_base_Class/int/string either a related model object, or its ID
769
-     * @param string $model_name   name of the related thing, eg 'Attendee',
770
-     * @return EE_Base_Class
771
-     * @throws ReflectionException
772
-     * @throws InvalidArgumentException
773
-     * @throws InvalidInterfaceException
774
-     * @throws InvalidDataTypeException
775
-     * @throws EE_Error
776
-     */
777
-    protected function ensure_related_thing_is_model_obj($object_or_id, $model_name)
778
-    {
779
-        $other_model_instance = self::_get_model_instance_with_name(
780
-            self::_get_model_classname($model_name),
781
-            $this->_timezone
782
-        );
783
-        return $other_model_instance->ensure_is_obj($object_or_id);
784
-    }
785
-
786
-
787
-    /**
788
-     * Forgets the cached model of the given relation Name. So the next time we request it,
789
-     * we will fetch it again from the database. (Handy if you know it's changed somehow).
790
-     * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM,
791
-     * then only remove that one object from our cached array. Otherwise, clear the entire list
792
-     *
793
-     * @param string $relationName                         one of the keys in the _model_relations array on the model.
794
-     *                                                     Eg 'Registration'
795
-     * @param mixed  $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL
796
-     *                                                     if you intend to use $clear_all = TRUE, or the relation only
797
-     *                                                     has 1 object anyways (ie, it's a BelongsToRelation)
798
-     * @param bool   $clear_all                            This flags clearing the entire cache relation property if
799
-     *                                                     this is HasMany or HABTM.
800
-     * @throws ReflectionException
801
-     * @throws InvalidArgumentException
802
-     * @throws InvalidInterfaceException
803
-     * @throws InvalidDataTypeException
804
-     * @throws EE_Error
805
-     * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a
806
-     *                                                     relation from all
807
-     */
808
-    public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false)
809
-    {
810
-        $relationship_to_model = $this->get_model()->related_settings_for($relationName);
811
-        $index_in_cache = '';
812
-        if (! $relationship_to_model) {
813
-            throw new EE_Error(
814
-                sprintf(
815
-                    esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'),
816
-                    $relationName,
817
-                    get_class($this)
818
-                )
819
-            );
820
-        }
821
-        if ($clear_all) {
822
-            $obj_removed = true;
823
-            $this->_model_relations[ $relationName ] = null;
824
-        } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) {
825
-            $obj_removed = $this->_model_relations[ $relationName ];
826
-            $this->_model_relations[ $relationName ] = null;
827
-        } else {
828
-            if ($object_to_remove_or_index_into_array instanceof EE_Base_Class
829
-                && $object_to_remove_or_index_into_array->ID()
830
-            ) {
831
-                $index_in_cache = $object_to_remove_or_index_into_array->ID();
832
-                if (is_array($this->_model_relations[ $relationName ])
833
-                    && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ])
834
-                ) {
835
-                    $index_found_at = null;
836
-                    // find this object in the array even though it has a different key
837
-                    foreach ($this->_model_relations[ $relationName ] as $index => $obj) {
838
-                        /** @noinspection TypeUnsafeComparisonInspection */
839
-                        if ($obj instanceof EE_Base_Class
840
-                            && (
841
-                                $obj == $object_to_remove_or_index_into_array
842
-                                || $obj->ID() === $object_to_remove_or_index_into_array->ID()
843
-                            )
844
-                        ) {
845
-                            $index_found_at = $index;
846
-                            break;
847
-                        }
848
-                    }
849
-                    if ($index_found_at) {
850
-                        $index_in_cache = $index_found_at;
851
-                    } else {
852
-                        // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache
853
-                        // if it wasn't in it to begin with. So we're done
854
-                        return $object_to_remove_or_index_into_array;
855
-                    }
856
-                }
857
-            } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) {
858
-                // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it!
859
-                foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) {
860
-                    /** @noinspection TypeUnsafeComparisonInspection */
861
-                    if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) {
862
-                        $index_in_cache = $index;
863
-                    }
864
-                }
865
-            } else {
866
-                $index_in_cache = $object_to_remove_or_index_into_array;
867
-            }
868
-            // supposedly we've found it. But it could just be that the client code
869
-            // provided a bad index/object
870
-            if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) {
871
-                $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ];
872
-                unset($this->_model_relations[ $relationName ][ $index_in_cache ]);
873
-            } else {
874
-                // that thing was never cached anyways.
875
-                $obj_removed = null;
876
-            }
877
-        }
878
-        return $obj_removed;
879
-    }
880
-
881
-
882
-    /**
883
-     * update_cache_after_object_save
884
-     * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has
885
-     * obtained after being saved to the db
886
-     *
887
-     * @param string        $relationName       - the type of object that is cached
888
-     * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached
889
-     * @param string        $current_cache_id   - the ID that was used when originally caching the object
890
-     * @return boolean TRUE on success, FALSE on fail
891
-     * @throws ReflectionException
892
-     * @throws InvalidArgumentException
893
-     * @throws InvalidInterfaceException
894
-     * @throws InvalidDataTypeException
895
-     * @throws EE_Error
896
-     */
897
-    public function update_cache_after_object_save(
898
-        $relationName,
899
-        EE_Base_Class $newly_saved_object,
900
-        $current_cache_id = ''
901
-    ) {
902
-        // verify that incoming object is of the correct type
903
-        $obj_class = 'EE_' . $relationName;
904
-        if ($newly_saved_object instanceof $obj_class) {
905
-            /* @type EE_Base_Class $newly_saved_object */
906
-            // now get the type of relation
907
-            $relationship_to_model = $this->get_model()->related_settings_for($relationName);
908
-            // if this is a 1:1 relationship
909
-            if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
910
-                // then just replace the cached object with the newly saved object
911
-                $this->_model_relations[ $relationName ] = $newly_saved_object;
912
-                return true;
913
-                // or if it's some kind of sordid feral polyamorous relationship...
914
-            }
915
-            if (is_array($this->_model_relations[ $relationName ])
916
-                && isset($this->_model_relations[ $relationName ][ $current_cache_id ])
917
-            ) {
918
-                // then remove the current cached item
919
-                unset($this->_model_relations[ $relationName ][ $current_cache_id ]);
920
-                // and cache the newly saved object using it's new ID
921
-                $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object;
922
-                return true;
923
-            }
924
-        }
925
-        return false;
926
-    }
927
-
928
-
929
-    /**
930
-     * Fetches a single EE_Base_Class on that relation. (If the relation is of type
931
-     * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
932
-     *
933
-     * @param string $relationName
934
-     * @return EE_Base_Class
935
-     */
936
-    public function get_one_from_cache($relationName)
937
-    {
938
-        $cached_array_or_object = isset($this->_model_relations[ $relationName ])
939
-            ? $this->_model_relations[ $relationName ]
940
-            : null;
941
-        if (is_array($cached_array_or_object)) {
942
-            return array_shift($cached_array_or_object);
943
-        }
944
-        return $cached_array_or_object;
945
-    }
946
-
947
-
948
-    /**
949
-     * Fetches a single EE_Base_Class on that relation. (If the relation is of type
950
-     * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
951
-     *
952
-     * @param string $relationName
953
-     * @throws ReflectionException
954
-     * @throws InvalidArgumentException
955
-     * @throws InvalidInterfaceException
956
-     * @throws InvalidDataTypeException
957
-     * @throws EE_Error
958
-     * @return EE_Base_Class[] NOT necessarily indexed by primary keys
959
-     */
960
-    public function get_all_from_cache($relationName)
961
-    {
962
-        $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array();
963
-        // if the result is not an array, but exists, make it an array
964
-        $objects = is_array($objects) ? $objects : array($objects);
965
-        // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143
966
-        // basically, if this model object was stored in the session, and these cached model objects
967
-        // already have IDs, let's make sure they're in their model's entity mapper
968
-        // otherwise we will have duplicates next time we call
969
-        // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() );
970
-        $model = EE_Registry::instance()->load_model($relationName);
971
-        foreach ($objects as $model_object) {
972
-            if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) {
973
-                // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved
974
-                if ($model_object->ID()) {
975
-                    $model->add_to_entity_map($model_object);
976
-                }
977
-            } else {
978
-                throw new EE_Error(
979
-                    sprintf(
980
-                        esc_html__(
981
-                            'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object',
982
-                            'event_espresso'
983
-                        ),
984
-                        $relationName,
985
-                        gettype($model_object)
986
-                    )
987
-                );
988
-            }
989
-        }
990
-        return $objects;
991
-    }
992
-
993
-
994
-    /**
995
-     * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database
996
-     * matching the given query conditions.
997
-     *
998
-     * @param null  $field_to_order_by  What field is being used as the reference point.
999
-     * @param int   $limit              How many objects to return.
1000
-     * @param array $query_params       Any additional conditions on the query.
1001
-     * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
1002
-     *                                  you can indicate just the columns you want returned
1003
-     * @return array|EE_Base_Class[]
1004
-     * @throws ReflectionException
1005
-     * @throws InvalidArgumentException
1006
-     * @throws InvalidInterfaceException
1007
-     * @throws InvalidDataTypeException
1008
-     * @throws EE_Error
1009
-     */
1010
-    public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null)
1011
-    {
1012
-        $model = $this->get_model();
1013
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
1014
-            ? $model->get_primary_key_field()->get_name()
1015
-            : $field_to_order_by;
1016
-        $current_value = ! empty($field) ? $this->get($field) : null;
1017
-        if (empty($field) || empty($current_value)) {
1018
-            return array();
1019
-        }
1020
-        return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select);
1021
-    }
1022
-
1023
-
1024
-    /**
1025
-     * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database
1026
-     * matching the given query conditions.
1027
-     *
1028
-     * @param null  $field_to_order_by  What field is being used as the reference point.
1029
-     * @param int   $limit              How many objects to return.
1030
-     * @param array $query_params       Any additional conditions on the query.
1031
-     * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
1032
-     *                                  you can indicate just the columns you want returned
1033
-     * @return array|EE_Base_Class[]
1034
-     * @throws ReflectionException
1035
-     * @throws InvalidArgumentException
1036
-     * @throws InvalidInterfaceException
1037
-     * @throws InvalidDataTypeException
1038
-     * @throws EE_Error
1039
-     */
1040
-    public function previous_x(
1041
-        $field_to_order_by = null,
1042
-        $limit = 1,
1043
-        $query_params = array(),
1044
-        $columns_to_select = null
1045
-    ) {
1046
-        $model = $this->get_model();
1047
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
1048
-            ? $model->get_primary_key_field()->get_name()
1049
-            : $field_to_order_by;
1050
-        $current_value = ! empty($field) ? $this->get($field) : null;
1051
-        if (empty($field) || empty($current_value)) {
1052
-            return array();
1053
-        }
1054
-        return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select);
1055
-    }
1056
-
1057
-
1058
-    /**
1059
-     * Returns the next EE_Base_Class object in sequence from this object as found in the database
1060
-     * matching the given query conditions.
1061
-     *
1062
-     * @param null  $field_to_order_by  What field is being used as the reference point.
1063
-     * @param array $query_params       Any additional conditions on the query.
1064
-     * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
1065
-     *                                  you can indicate just the columns you want returned
1066
-     * @return array|EE_Base_Class
1067
-     * @throws ReflectionException
1068
-     * @throws InvalidArgumentException
1069
-     * @throws InvalidInterfaceException
1070
-     * @throws InvalidDataTypeException
1071
-     * @throws EE_Error
1072
-     */
1073
-    public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
1074
-    {
1075
-        $model = $this->get_model();
1076
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
1077
-            ? $model->get_primary_key_field()->get_name()
1078
-            : $field_to_order_by;
1079
-        $current_value = ! empty($field) ? $this->get($field) : null;
1080
-        if (empty($field) || empty($current_value)) {
1081
-            return array();
1082
-        }
1083
-        return $model->next($current_value, $field, $query_params, $columns_to_select);
1084
-    }
1085
-
1086
-
1087
-    /**
1088
-     * Returns the previous EE_Base_Class object in sequence from this object as found in the database
1089
-     * matching the given query conditions.
1090
-     *
1091
-     * @param null  $field_to_order_by  What field is being used as the reference point.
1092
-     * @param array $query_params       Any additional conditions on the query.
1093
-     * @param null  $columns_to_select  If left null, then an EE_Base_Class object is returned, otherwise
1094
-     *                                  you can indicate just the column you want returned
1095
-     * @return array|EE_Base_Class
1096
-     * @throws ReflectionException
1097
-     * @throws InvalidArgumentException
1098
-     * @throws InvalidInterfaceException
1099
-     * @throws InvalidDataTypeException
1100
-     * @throws EE_Error
1101
-     */
1102
-    public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
1103
-    {
1104
-        $model = $this->get_model();
1105
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
1106
-            ? $model->get_primary_key_field()->get_name()
1107
-            : $field_to_order_by;
1108
-        $current_value = ! empty($field) ? $this->get($field) : null;
1109
-        if (empty($field) || empty($current_value)) {
1110
-            return array();
1111
-        }
1112
-        return $model->previous($current_value, $field, $query_params, $columns_to_select);
1113
-    }
1114
-
1115
-
1116
-    /**
1117
-     * Overrides parent because parent expects old models.
1118
-     * This also doesn't do any validation, and won't work for serialized arrays
1119
-     *
1120
-     * @param string $field_name
1121
-     * @param mixed  $field_value_from_db
1122
-     * @throws ReflectionException
1123
-     * @throws InvalidArgumentException
1124
-     * @throws InvalidInterfaceException
1125
-     * @throws InvalidDataTypeException
1126
-     * @throws EE_Error
1127
-     */
1128
-    public function set_from_db($field_name, $field_value_from_db)
1129
-    {
1130
-        $field_obj = $this->get_model()->field_settings_for($field_name);
1131
-        if ($field_obj instanceof EE_Model_Field_Base) {
1132
-            // you would think the DB has no NULLs for non-null label fields right? wrong!
1133
-            // eg, a CPT model object could have an entry in the posts table, but no
1134
-            // entry in the meta table. Meaning that all its columns in the meta table
1135
-            // are null! yikes! so when we find one like that, use defaults for its meta columns
1136
-            if ($field_value_from_db === null) {
1137
-                if ($field_obj->is_nullable()) {
1138
-                    // if the field allows nulls, then let it be null
1139
-                    $field_value = null;
1140
-                } else {
1141
-                    $field_value = $field_obj->get_default_value();
1142
-                }
1143
-            } else {
1144
-                $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db);
1145
-            }
1146
-            $this->_fields[ $field_name ] = $field_value;
1147
-            $this->_clear_cached_property($field_name);
1148
-        }
1149
-    }
1150
-
1151
-
1152
-    /**
1153
-     * verifies that the specified field is of the correct type
1154
-     *
1155
-     * @param string $field_name
1156
-     * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1157
-     *                                (in cases where the same property may be used for different outputs
1158
-     *                                - i.e. datetime, money etc.)
1159
-     * @return mixed
1160
-     * @throws ReflectionException
1161
-     * @throws InvalidArgumentException
1162
-     * @throws InvalidInterfaceException
1163
-     * @throws InvalidDataTypeException
1164
-     * @throws EE_Error
1165
-     */
1166
-    public function get($field_name, $extra_cache_ref = null)
1167
-    {
1168
-        return $this->_get_cached_property($field_name, false, $extra_cache_ref);
1169
-    }
1170
-
1171
-
1172
-    /**
1173
-     * This method simply returns the RAW unprocessed value for the given property in this class
1174
-     *
1175
-     * @param  string $field_name A valid fieldname
1176
-     * @return mixed              Whatever the raw value stored on the property is.
1177
-     * @throws ReflectionException
1178
-     * @throws InvalidArgumentException
1179
-     * @throws InvalidInterfaceException
1180
-     * @throws InvalidDataTypeException
1181
-     * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist.
1182
-     */
1183
-    public function get_raw($field_name)
1184
-    {
1185
-        $field_settings = $this->get_model()->field_settings_for($field_name);
1186
-        return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime
1187
-            ? $this->_fields[ $field_name ]->format('U')
1188
-            : $this->_fields[ $field_name ];
1189
-    }
1190
-
1191
-
1192
-    /**
1193
-     * This is used to return the internal DateTime object used for a field that is a
1194
-     * EE_Datetime_Field.
1195
-     *
1196
-     * @param string $field_name               The field name retrieving the DateTime object.
1197
-     * @return mixed null | false | DateTime  If the requested field is NOT a EE_Datetime_Field then
1198
-     * @throws EE_Error an error is set and false returned.  If the field IS an
1199
-     *                                         EE_Datetime_Field and but the field value is null, then
1200
-     *                                         just null is returned (because that indicates that likely
1201
-     *                                         this field is nullable).
1202
-     * @throws InvalidArgumentException
1203
-     * @throws InvalidDataTypeException
1204
-     * @throws InvalidInterfaceException
1205
-     * @throws ReflectionException
1206
-     */
1207
-    public function get_DateTime_object($field_name)
1208
-    {
1209
-        $field_settings = $this->get_model()->field_settings_for($field_name);
1210
-        if (! $field_settings instanceof EE_Datetime_Field) {
1211
-            EE_Error::add_error(
1212
-                sprintf(
1213
-                    esc_html__(
1214
-                        'The field %s is not an EE_Datetime_Field field.  There is no DateTime object stored on this field type.',
1215
-                        'event_espresso'
1216
-                    ),
1217
-                    $field_name
1218
-                ),
1219
-                __FILE__,
1220
-                __FUNCTION__,
1221
-                __LINE__
1222
-            );
1223
-            return false;
1224
-        }
1225
-        return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime
1226
-            ? clone $this->_fields[ $field_name ]
1227
-            : null;
1228
-    }
1229
-
1230
-
1231
-    /**
1232
-     * To be used in template to immediately echo out the value, and format it for output.
1233
-     * Eg, should call stripslashes and whatnot before echoing
1234
-     *
1235
-     * @param string $field_name      the name of the field as it appears in the DB
1236
-     * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1237
-     *                                (in cases where the same property may be used for different outputs
1238
-     *                                - i.e. datetime, money etc.)
1239
-     * @return void
1240
-     * @throws ReflectionException
1241
-     * @throws InvalidArgumentException
1242
-     * @throws InvalidInterfaceException
1243
-     * @throws InvalidDataTypeException
1244
-     * @throws EE_Error
1245
-     */
1246
-    public function e($field_name, $extra_cache_ref = null)
1247
-    {
1248
-        echo $this->get_pretty($field_name, $extra_cache_ref);
1249
-    }
1250
-
1251
-
1252
-    /**
1253
-     * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it
1254
-     * can be easily used as the value of form input.
1255
-     *
1256
-     * @param string $field_name
1257
-     * @return void
1258
-     * @throws ReflectionException
1259
-     * @throws InvalidArgumentException
1260
-     * @throws InvalidInterfaceException
1261
-     * @throws InvalidDataTypeException
1262
-     * @throws EE_Error
1263
-     */
1264
-    public function f($field_name)
1265
-    {
1266
-        $this->e($field_name, 'form_input');
1267
-    }
1268
-
1269
-
1270
-    /**
1271
-     * Same as `f()` but just returns the value instead of echoing it
1272
-     *
1273
-     * @param string $field_name
1274
-     * @return string
1275
-     * @throws ReflectionException
1276
-     * @throws InvalidArgumentException
1277
-     * @throws InvalidInterfaceException
1278
-     * @throws InvalidDataTypeException
1279
-     * @throws EE_Error
1280
-     */
1281
-    public function get_f($field_name)
1282
-    {
1283
-        return (string) $this->get_pretty($field_name, 'form_input');
1284
-    }
1285
-
1286
-
1287
-    /**
1288
-     * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this.
1289
-     * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class
1290
-     * to see what options are available.
1291
-     *
1292
-     * @param string $field_name
1293
-     * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1294
-     *                                (in cases where the same property may be used for different outputs
1295
-     *                                - i.e. datetime, money etc.)
1296
-     * @return mixed
1297
-     * @throws ReflectionException
1298
-     * @throws InvalidArgumentException
1299
-     * @throws InvalidInterfaceException
1300
-     * @throws InvalidDataTypeException
1301
-     * @throws EE_Error
1302
-     */
1303
-    public function get_pretty($field_name, $extra_cache_ref = null)
1304
-    {
1305
-        return $this->_get_cached_property($field_name, true, $extra_cache_ref);
1306
-    }
1307
-
1308
-
1309
-    /**
1310
-     * This simply returns the datetime for the given field name
1311
-     * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions
1312
-     * (and the equivalent e_date, e_time, e_datetime).
1313
-     *
1314
-     * @access   protected
1315
-     * @param string   $field_name   Field on the instantiated EE_Base_Class child object
1316
-     * @param string   $dt_frmt      valid datetime format used for date
1317
-     *                               (if '' then we just use the default on the field,
1318
-     *                               if NULL we use the last-used format)
1319
-     * @param string   $tm_frmt      Same as above except this is for time format
1320
-     * @param string   $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time.
1321
-     * @param  boolean $echo         Whether the dtt is echoing using pretty echoing or just returned using vanilla get
1322
-     * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown
1323
-     *                               if field is not a valid dtt field, or void if echoing
1324
-     * @throws ReflectionException
1325
-     * @throws InvalidArgumentException
1326
-     * @throws InvalidInterfaceException
1327
-     * @throws InvalidDataTypeException
1328
-     * @throws EE_Error
1329
-     */
1330
-    protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false)
1331
-    {
1332
-        // clear cached property
1333
-        $this->_clear_cached_property($field_name);
1334
-        // reset format properties because they are used in get()
1335
-        $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt;
1336
-        $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt;
1337
-        if ($echo) {
1338
-            $this->e($field_name, $date_or_time);
1339
-            return '';
1340
-        }
1341
-        return $this->get($field_name, $date_or_time);
1342
-    }
1343
-
1344
-
1345
-    /**
1346
-     * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date
1347
-     * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1348
-     * other echoes the pretty value for dtt)
1349
-     *
1350
-     * @param  string $field_name name of model object datetime field holding the value
1351
-     * @param  string $format     format for the date returned (if NULL we use default in dt_frmt property)
1352
-     * @return string            datetime value formatted
1353
-     * @throws ReflectionException
1354
-     * @throws InvalidArgumentException
1355
-     * @throws InvalidInterfaceException
1356
-     * @throws InvalidDataTypeException
1357
-     * @throws EE_Error
1358
-     */
1359
-    public function get_date($field_name, $format = '')
1360
-    {
1361
-        return $this->_get_datetime($field_name, $format, null, 'D');
1362
-    }
1363
-
1364
-
1365
-    /**
1366
-     * @param        $field_name
1367
-     * @param string $format
1368
-     * @throws ReflectionException
1369
-     * @throws InvalidArgumentException
1370
-     * @throws InvalidInterfaceException
1371
-     * @throws InvalidDataTypeException
1372
-     * @throws EE_Error
1373
-     */
1374
-    public function e_date($field_name, $format = '')
1375
-    {
1376
-        $this->_get_datetime($field_name, $format, null, 'D', true);
1377
-    }
1378
-
1379
-
1380
-    /**
1381
-     * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time
1382
-     * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1383
-     * other echoes the pretty value for dtt)
1384
-     *
1385
-     * @param  string $field_name name of model object datetime field holding the value
1386
-     * @param  string $format     format for the time returned ( if NULL we use default in tm_frmt property)
1387
-     * @return string             datetime value formatted
1388
-     * @throws ReflectionException
1389
-     * @throws InvalidArgumentException
1390
-     * @throws InvalidInterfaceException
1391
-     * @throws InvalidDataTypeException
1392
-     * @throws EE_Error
1393
-     */
1394
-    public function get_time($field_name, $format = '')
1395
-    {
1396
-        return $this->_get_datetime($field_name, null, $format, 'T');
1397
-    }
1398
-
1399
-
1400
-    /**
1401
-     * @param        $field_name
1402
-     * @param string $format
1403
-     * @throws ReflectionException
1404
-     * @throws InvalidArgumentException
1405
-     * @throws InvalidInterfaceException
1406
-     * @throws InvalidDataTypeException
1407
-     * @throws EE_Error
1408
-     */
1409
-    public function e_time($field_name, $format = '')
1410
-    {
1411
-        $this->_get_datetime($field_name, null, $format, 'T', true);
1412
-    }
1413
-
1414
-
1415
-    /**
1416
-     * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND
1417
-     * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1418
-     * other echoes the pretty value for dtt)
1419
-     *
1420
-     * @param  string $field_name name of model object datetime field holding the value
1421
-     * @param  string $dt_frmt    format for the date returned (if NULL we use default in dt_frmt property)
1422
-     * @param  string $tm_frmt    format for the time returned (if NULL we use default in tm_frmt property)
1423
-     * @return string             datetime value formatted
1424
-     * @throws ReflectionException
1425
-     * @throws InvalidArgumentException
1426
-     * @throws InvalidInterfaceException
1427
-     * @throws InvalidDataTypeException
1428
-     * @throws EE_Error
1429
-     */
1430
-    public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1431
-    {
1432
-        return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt);
1433
-    }
1434
-
1435
-
1436
-    /**
1437
-     * @param string $field_name
1438
-     * @param string $dt_frmt
1439
-     * @param string $tm_frmt
1440
-     * @throws ReflectionException
1441
-     * @throws InvalidArgumentException
1442
-     * @throws InvalidInterfaceException
1443
-     * @throws InvalidDataTypeException
1444
-     * @throws EE_Error
1445
-     */
1446
-    public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1447
-    {
1448
-        $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true);
1449
-    }
1450
-
1451
-
1452
-    /**
1453
-     * Get the i8ln value for a date using the WordPress @see date_i18n function.
1454
-     *
1455
-     * @param string $field_name The EE_Datetime_Field reference for the date being retrieved.
1456
-     * @param string $format     PHP valid date/time string format.  If none is provided then the internal set format
1457
-     *                           on the object will be used.
1458
-     * @return string Date and time string in set locale or false if no field exists for the given
1459
-     * @throws ReflectionException
1460
-     * @throws InvalidArgumentException
1461
-     * @throws InvalidInterfaceException
1462
-     * @throws InvalidDataTypeException
1463
-     * @throws EE_Error
1464
-     *                           field name.
1465
-     */
1466
-    public function get_i18n_datetime($field_name, $format = '')
1467
-    {
1468
-        $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format;
1469
-        return date_i18n(
1470
-            $format,
1471
-            EEH_DTT_Helper::get_timestamp_with_offset(
1472
-                $this->get_raw($field_name),
1473
-                $this->_timezone
1474
-            )
1475
-        );
1476
-    }
1477
-
1478
-
1479
-    /**
1480
-     * This method validates whether the given field name is a valid field on the model object as well as it is of a
1481
-     * type EE_Datetime_Field.  On success there will be returned the field settings.  On fail an EE_Error exception is
1482
-     * thrown.
1483
-     *
1484
-     * @param  string $field_name The field name being checked
1485
-     * @throws ReflectionException
1486
-     * @throws InvalidArgumentException
1487
-     * @throws InvalidInterfaceException
1488
-     * @throws InvalidDataTypeException
1489
-     * @throws EE_Error
1490
-     * @return EE_Datetime_Field
1491
-     */
1492
-    protected function _get_dtt_field_settings($field_name)
1493
-    {
1494
-        $field = $this->get_model()->field_settings_for($field_name);
1495
-        // check if field is dtt
1496
-        if ($field instanceof EE_Datetime_Field) {
1497
-            return $field;
1498
-        }
1499
-        throw new EE_Error(
1500
-            sprintf(
1501
-                esc_html__(
1502
-                    'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field.  Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor',
1503
-                    'event_espresso'
1504
-                ),
1505
-                $field_name,
1506
-                self::_get_model_classname(get_class($this))
1507
-            )
1508
-        );
1509
-    }
1510
-
1511
-
1512
-
1513
-
1514
-    /**
1515
-     * NOTE ABOUT BELOW:
1516
-     * These convenience date and time setters are for setting date and time independently.  In other words you might
1517
-     * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand
1518
-     * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value)
1519
-     * method and make sure you send the entire datetime value for setting.
1520
-     */
1521
-    /**
1522
-     * sets the time on a datetime property
1523
-     *
1524
-     * @access protected
1525
-     * @param string|Datetime $time      a valid time string for php datetime functions (or DateTime object)
1526
-     * @param string          $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field)
1527
-     * @throws ReflectionException
1528
-     * @throws InvalidArgumentException
1529
-     * @throws InvalidInterfaceException
1530
-     * @throws InvalidDataTypeException
1531
-     * @throws EE_Error
1532
-     */
1533
-    protected function _set_time_for($time, $fieldname)
1534
-    {
1535
-        $this->_set_date_time('T', $time, $fieldname);
1536
-    }
1537
-
1538
-
1539
-    /**
1540
-     * sets the date on a datetime property
1541
-     *
1542
-     * @access protected
1543
-     * @param string|DateTime $date      a valid date string for php datetime functions ( or DateTime object)
1544
-     * @param string          $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field)
1545
-     * @throws ReflectionException
1546
-     * @throws InvalidArgumentException
1547
-     * @throws InvalidInterfaceException
1548
-     * @throws InvalidDataTypeException
1549
-     * @throws EE_Error
1550
-     */
1551
-    protected function _set_date_for($date, $fieldname)
1552
-    {
1553
-        $this->_set_date_time('D', $date, $fieldname);
1554
-    }
1555
-
1556
-
1557
-    /**
1558
-     * This takes care of setting a date or time independently on a given model object property. This method also
1559
-     * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field
1560
-     *
1561
-     * @access protected
1562
-     * @param string          $what           "T" for time, 'B' for both, 'D' for Date.
1563
-     * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object)
1564
-     * @param string          $fieldname      the name of the field the date OR time is being set on (must match a
1565
-     *                                        EE_Datetime_Field property)
1566
-     * @throws ReflectionException
1567
-     * @throws InvalidArgumentException
1568
-     * @throws InvalidInterfaceException
1569
-     * @throws InvalidDataTypeException
1570
-     * @throws EE_Error
1571
-     */
1572
-    protected function _set_date_time($what = 'T', $datetime_value, $fieldname)
1573
-    {
1574
-        $field = $this->_get_dtt_field_settings($fieldname);
1575
-        $field->set_timezone($this->_timezone);
1576
-        $field->set_date_format($this->_dt_frmt);
1577
-        $field->set_time_format($this->_tm_frmt);
1578
-        switch ($what) {
1579
-            case 'T':
1580
-                $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time(
1581
-                    $datetime_value,
1582
-                    $this->_fields[ $fieldname ]
1583
-                );
1584
-                break;
1585
-            case 'D':
1586
-                $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date(
1587
-                    $datetime_value,
1588
-                    $this->_fields[ $fieldname ]
1589
-                );
1590
-                break;
1591
-            case 'B':
1592
-                $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value);
1593
-                break;
1594
-        }
1595
-        $this->_clear_cached_property($fieldname);
1596
-    }
1597
-
1598
-
1599
-    /**
1600
-     * This will return a timestamp for the website timezone but ONLY when the current website timezone is different
1601
-     * than the timezone set for the website. NOTE, this currently only works well with methods that return values.  If
1602
-     * you use it with methods that echo values the $_timestamp property may not get reset to its original value and
1603
-     * that could lead to some unexpected results!
1604
-     *
1605
-     * @access public
1606
-     * @param string $field_name               This is the name of the field on the object that contains the date/time
1607
-     *                                         value being returned.
1608
-     * @param string $callback                 must match a valid method in this class (defaults to get_datetime)
1609
-     * @param mixed (array|string) $args       This is the arguments that will be passed to the callback.
1610
-     * @param string $prepend                  You can include something to prepend on the timestamp
1611
-     * @param string $append                   You can include something to append on the timestamp
1612
-     * @throws ReflectionException
1613
-     * @throws InvalidArgumentException
1614
-     * @throws InvalidInterfaceException
1615
-     * @throws InvalidDataTypeException
1616
-     * @throws EE_Error
1617
-     * @return string timestamp
1618
-     */
1619
-    public function display_in_my_timezone(
1620
-        $field_name,
1621
-        $callback = 'get_datetime',
1622
-        $args = null,
1623
-        $prepend = '',
1624
-        $append = ''
1625
-    ) {
1626
-        $timezone = EEH_DTT_Helper::get_timezone();
1627
-        if ($timezone === $this->_timezone) {
1628
-            return '';
1629
-        }
1630
-        $original_timezone = $this->_timezone;
1631
-        $this->set_timezone($timezone);
1632
-        $fn = (array) $field_name;
1633
-        $args = array_merge($fn, (array) $args);
1634
-        if (! method_exists($this, $callback)) {
1635
-            throw new EE_Error(
1636
-                sprintf(
1637
-                    esc_html__(
1638
-                        'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist.  Please check your spelling',
1639
-                        'event_espresso'
1640
-                    ),
1641
-                    $callback
1642
-                )
1643
-            );
1644
-        }
1645
-        $args = (array) $args;
1646
-        $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append;
1647
-        $this->set_timezone($original_timezone);
1648
-        return $return;
1649
-    }
1650
-
1651
-
1652
-    /**
1653
-     * Deletes this model object.
1654
-     * This calls the `EE_Base_Class::_delete` method.  Child classes wishing to change default behaviour should
1655
-     * override
1656
-     * `EE_Base_Class::_delete` NOT this class.
1657
-     *
1658
-     * @return boolean | int
1659
-     * @throws ReflectionException
1660
-     * @throws InvalidArgumentException
1661
-     * @throws InvalidInterfaceException
1662
-     * @throws InvalidDataTypeException
1663
-     * @throws EE_Error
1664
-     */
1665
-    public function delete()
1666
-    {
1667
-        /**
1668
-         * Called just before the `EE_Base_Class::_delete` method call.
1669
-         * Note:
1670
-         * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1671
-         * should be aware that `_delete` may not always result in a permanent delete.
1672
-         * For example, `EE_Soft_Delete_Base_Class::_delete`
1673
-         * soft deletes (trash) the object and does not permanently delete it.
1674
-         *
1675
-         * @param EE_Base_Class $model_object about to be 'deleted'
1676
-         */
1677
-        do_action('AHEE__EE_Base_Class__delete__before', $this);
1678
-        $result = $this->_delete();
1679
-        /**
1680
-         * Called just after the `EE_Base_Class::_delete` method call.
1681
-         * Note:
1682
-         * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1683
-         * should be aware that `_delete` may not always result in a permanent delete.
1684
-         * For example `EE_Soft_Base_Class::_delete`
1685
-         * soft deletes (trash) the object and does not permanently delete it.
1686
-         *
1687
-         * @param EE_Base_Class $model_object that was just 'deleted'
1688
-         * @param boolean       $result
1689
-         */
1690
-        do_action('AHEE__EE_Base_Class__delete__end', $this, $result);
1691
-        return $result;
1692
-    }
1693
-
1694
-
1695
-    /**
1696
-     * Calls the specific delete method for the instantiated class.
1697
-     * This method is called by the public `EE_Base_Class::delete` method.  Any child classes desiring to override
1698
-     * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT
1699
-     * `EE_Base_Class::delete`
1700
-     *
1701
-     * @return bool|int
1702
-     * @throws ReflectionException
1703
-     * @throws InvalidArgumentException
1704
-     * @throws InvalidInterfaceException
1705
-     * @throws InvalidDataTypeException
1706
-     * @throws EE_Error
1707
-     */
1708
-    protected function _delete()
1709
-    {
1710
-        return $this->delete_permanently();
1711
-    }
1712
-
1713
-
1714
-    /**
1715
-     * Deletes this model object permanently from db
1716
-     * (but keep in mind related models may block the delete and return an error)
1717
-     *
1718
-     * @return bool | int
1719
-     * @throws ReflectionException
1720
-     * @throws InvalidArgumentException
1721
-     * @throws InvalidInterfaceException
1722
-     * @throws InvalidDataTypeException
1723
-     * @throws EE_Error
1724
-     */
1725
-    public function delete_permanently()
1726
-    {
1727
-        /**
1728
-         * Called just before HARD deleting a model object
1729
-         *
1730
-         * @param EE_Base_Class $model_object about to be 'deleted'
1731
-         */
1732
-        do_action('AHEE__EE_Base_Class__delete_permanently__before', $this);
1733
-        $model = $this->get_model();
1734
-        $result = $model->delete_permanently_by_ID($this->ID());
1735
-        $this->refresh_cache_of_related_objects();
1736
-        /**
1737
-         * Called just after HARD deleting a model object
1738
-         *
1739
-         * @param EE_Base_Class $model_object that was just 'deleted'
1740
-         * @param boolean       $result
1741
-         */
1742
-        do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result);
1743
-        return $result;
1744
-    }
1745
-
1746
-
1747
-    /**
1748
-     * When this model object is deleted, it may still be cached on related model objects. This clears the cache of
1749
-     * related model objects
1750
-     *
1751
-     * @throws ReflectionException
1752
-     * @throws InvalidArgumentException
1753
-     * @throws InvalidInterfaceException
1754
-     * @throws InvalidDataTypeException
1755
-     * @throws EE_Error
1756
-     */
1757
-    public function refresh_cache_of_related_objects()
1758
-    {
1759
-        $model = $this->get_model();
1760
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1761
-            if (! empty($this->_model_relations[ $relation_name ])) {
1762
-                $related_objects = $this->_model_relations[ $relation_name ];
1763
-                if ($relation_obj instanceof EE_Belongs_To_Relation) {
1764
-                    // this relation only stores a single model object, not an array
1765
-                    // but let's make it consistent
1766
-                    $related_objects = array($related_objects);
1767
-                }
1768
-                foreach ($related_objects as $related_object) {
1769
-                    // only refresh their cache if they're in memory
1770
-                    if ($related_object instanceof EE_Base_Class) {
1771
-                        $related_object->clear_cache(
1772
-                            $model->get_this_model_name(),
1773
-                            $this
1774
-                        );
1775
-                    }
1776
-                }
1777
-            }
1778
-        }
1779
-    }
1780
-
1781
-
1782
-    /**
1783
-     *        Saves this object to the database. An array may be supplied to set some values on this
1784
-     * object just before saving.
1785
-     *
1786
-     * @access public
1787
-     * @param array $set_cols_n_values keys are field names, values are their new values,
1788
-     *                                 if provided during the save() method (often client code will change the fields'
1789
-     *                                 values before calling save)
1790
-     * @throws InvalidArgumentException
1791
-     * @throws InvalidInterfaceException
1792
-     * @throws InvalidDataTypeException
1793
-     * @throws EE_Error
1794
-     * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object
1795
-     *                                 isn't allowed to persist (as determined by EE_Base_Class::allow_persist())
1796
-     * @throws ReflectionException
1797
-     * @throws ReflectionException
1798
-     * @throws ReflectionException
1799
-     */
1800
-    public function save($set_cols_n_values = array())
1801
-    {
1802
-        $model = $this->get_model();
1803
-        /**
1804
-         * Filters the fields we're about to save on the model object
1805
-         *
1806
-         * @param array         $set_cols_n_values
1807
-         * @param EE_Base_Class $model_object
1808
-         */
1809
-        $set_cols_n_values = (array) apply_filters(
1810
-            'FHEE__EE_Base_Class__save__set_cols_n_values',
1811
-            $set_cols_n_values,
1812
-            $this
1813
-        );
1814
-        // set attributes as provided in $set_cols_n_values
1815
-        foreach ($set_cols_n_values as $column => $value) {
1816
-            $this->set($column, $value);
1817
-        }
1818
-        // no changes ? then don't do anything
1819
-        if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1820
-            return 0;
1821
-        }
1822
-        /**
1823
-         * Saving a model object.
1824
-         * Before we perform a save, this action is fired.
1825
-         *
1826
-         * @param EE_Base_Class $model_object the model object about to be saved.
1827
-         */
1828
-        do_action('AHEE__EE_Base_Class__save__begin', $this);
1829
-        if (! $this->allow_persist()) {
1830
-            return 0;
1831
-        }
1832
-        // now get current attribute values
1833
-        $save_cols_n_values = $this->_fields;
1834
-        // if the object already has an ID, update it. Otherwise, insert it
1835
-        // also: change the assumption about values passed to the model NOT being prepare dby the model object.
1836
-        // They have been
1837
-        $old_assumption_concerning_value_preparation = $model
1838
-            ->get_assumption_concerning_values_already_prepared_by_model_object();
1839
-        $model->assume_values_already_prepared_by_model_object(true);
1840
-        // does this model have an autoincrement PK?
1841
-        if ($model->has_primary_key_field()) {
1842
-            if ($model->get_primary_key_field()->is_auto_increment()) {
1843
-                // ok check if it's set, if so: update; if not, insert
1844
-                if (! empty($save_cols_n_values[ $model->primary_key_name() ])) {
1845
-                    $results = $model->update_by_ID($save_cols_n_values, $this->ID());
1846
-                } else {
1847
-                    unset($save_cols_n_values[ $model->primary_key_name() ]);
1848
-                    $results = $model->insert($save_cols_n_values);
1849
-                    if ($results) {
1850
-                        // if successful, set the primary key
1851
-                        // but don't use the normal SET method, because it will check if
1852
-                        // an item with the same ID exists in the mapper & db, then
1853
-                        // will find it in the db (because we just added it) and THAT object
1854
-                        // will get added to the mapper before we can add this one!
1855
-                        // but if we just avoid using the SET method, all that headache can be avoided
1856
-                        $pk_field_name = $model->primary_key_name();
1857
-                        $this->_fields[ $pk_field_name ] = $results;
1858
-                        $this->_clear_cached_property($pk_field_name);
1859
-                        $model->add_to_entity_map($this);
1860
-                        $this->_update_cached_related_model_objs_fks();
1861
-                    }
1862
-                }
1863
-            } else {// PK is NOT auto-increment
1864
-                // so check if one like it already exists in the db
1865
-                if ($model->exists_by_ID($this->ID())) {
1866
-                    if (WP_DEBUG && ! $this->in_entity_map()) {
1867
-                        throw new EE_Error(
1868
-                            sprintf(
1869
-                                esc_html__(
1870
-                                    'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s',
1871
-                                    'event_espresso'
1872
-                                ),
1873
-                                get_class($this),
1874
-                                get_class($model) . '::instance()->add_to_entity_map()',
1875
-                                get_class($model) . '::instance()->get_one_by_ID()',
1876
-                                '<br />'
1877
-                            )
1878
-                        );
1879
-                    }
1880
-                    $results = $model->update_by_ID($save_cols_n_values, $this->ID());
1881
-                } else {
1882
-                    $results = $model->insert($save_cols_n_values);
1883
-                    $this->_update_cached_related_model_objs_fks();
1884
-                }
1885
-            }
1886
-        } else {// there is NO primary key
1887
-            $already_in_db = false;
1888
-            foreach ($model->unique_indexes() as $index) {
1889
-                $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields());
1890
-                if ($model->exists(array($uniqueness_where_params))) {
1891
-                    $already_in_db = true;
1892
-                }
1893
-            }
1894
-            if ($already_in_db) {
1895
-                $combined_pk_fields_n_values = array_intersect_key(
1896
-                    $save_cols_n_values,
1897
-                    $model->get_combined_primary_key_fields()
1898
-                );
1899
-                $results = $model->update(
1900
-                    $save_cols_n_values,
1901
-                    $combined_pk_fields_n_values
1902
-                );
1903
-            } else {
1904
-                $results = $model->insert($save_cols_n_values);
1905
-            }
1906
-        }
1907
-        // restore the old assumption about values being prepared by the model object
1908
-        $model->assume_values_already_prepared_by_model_object(
1909
-            $old_assumption_concerning_value_preparation
1910
-        );
1911
-        /**
1912
-         * After saving the model object this action is called
1913
-         *
1914
-         * @param EE_Base_Class $model_object which was just saved
1915
-         * @param boolean|int   $results      if it were updated, TRUE or FALSE; if it were newly inserted
1916
-         *                                    the new ID (or 0 if an error occurred and it wasn't updated)
1917
-         */
1918
-        do_action('AHEE__EE_Base_Class__save__end', $this, $results);
1919
-        $this->_has_changes = false;
1920
-        return $results;
1921
-    }
1922
-
1923
-
1924
-    /**
1925
-     * Updates the foreign key on related models objects pointing to this to have this model object's ID
1926
-     * as their foreign key.  If the cached related model objects already exist in the db, saves them (so that the DB
1927
-     * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its
1928
-     * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't
1929
-     * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the
1930
-     * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether
1931
-     * or not they exist in the DB (if they do, their DB records will be automatically updated)
1932
-     *
1933
-     * @return void
1934
-     * @throws ReflectionException
1935
-     * @throws InvalidArgumentException
1936
-     * @throws InvalidInterfaceException
1937
-     * @throws InvalidDataTypeException
1938
-     * @throws EE_Error
1939
-     */
1940
-    protected function _update_cached_related_model_objs_fks()
1941
-    {
1942
-        $model = $this->get_model();
1943
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1944
-            if ($relation_obj instanceof EE_Has_Many_Relation) {
1945
-                foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) {
1946
-                    $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to(
1947
-                        $model->get_this_model_name()
1948
-                    );
1949
-                    $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID());
1950
-                    if ($related_model_obj_in_cache->ID()) {
1951
-                        $related_model_obj_in_cache->save();
1952
-                    }
1953
-                }
1954
-            }
1955
-        }
1956
-    }
1957
-
1958
-
1959
-    /**
1960
-     * Saves this model object and its NEW cached relations to the database.
1961
-     * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB.
1962
-     * In order for that to work, we would need to mark model objects as dirty/clean...
1963
-     * because otherwise, there's a potential for infinite looping of saving
1964
-     * Saves the cached related model objects, and ensures the relation between them
1965
-     * and this object and properly setup
1966
-     *
1967
-     * @return int ID of new model object on save; 0 on failure+
1968
-     * @throws ReflectionException
1969
-     * @throws InvalidArgumentException
1970
-     * @throws InvalidInterfaceException
1971
-     * @throws InvalidDataTypeException
1972
-     * @throws EE_Error
1973
-     */
1974
-    public function save_new_cached_related_model_objs()
1975
-    {
1976
-        // make sure this has been saved
1977
-        if (! $this->ID()) {
1978
-            $id = $this->save();
1979
-        } else {
1980
-            $id = $this->ID();
1981
-        }
1982
-        // now save all the NEW cached model objects  (ie they don't exist in the DB)
1983
-        foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) {
1984
-            if ($this->_model_relations[ $relationName ]) {
1985
-                // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation)
1986
-                // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)?
1987
-                /* @var $related_model_obj EE_Base_Class */
1988
-                if ($relationObj instanceof EE_Belongs_To_Relation) {
1989
-                    // add a relation to that relation type (which saves the appropriate thing in the process)
1990
-                    // but ONLY if it DOES NOT exist in the DB
1991
-                    $related_model_obj = $this->_model_relations[ $relationName ];
1992
-                    // if( ! $related_model_obj->ID()){
1993
-                    $this->_add_relation_to($related_model_obj, $relationName);
1994
-                    $related_model_obj->save_new_cached_related_model_objs();
1995
-                    // }
1996
-                } else {
1997
-                    foreach ($this->_model_relations[ $relationName ] as $related_model_obj) {
1998
-                        // add a relation to that relation type (which saves the appropriate thing in the process)
1999
-                        // but ONLY if it DOES NOT exist in the DB
2000
-                        // if( ! $related_model_obj->ID()){
2001
-                        $this->_add_relation_to($related_model_obj, $relationName);
2002
-                        $related_model_obj->save_new_cached_related_model_objs();
2003
-                        // }
2004
-                    }
2005
-                }
2006
-            }
2007
-        }
2008
-        return $id;
2009
-    }
2010
-
2011
-
2012
-    /**
2013
-     * for getting a model while instantiated.
2014
-     *
2015
-     * @return EEM_Base | EEM_CPT_Base
2016
-     * @throws ReflectionException
2017
-     * @throws InvalidArgumentException
2018
-     * @throws InvalidInterfaceException
2019
-     * @throws InvalidDataTypeException
2020
-     * @throws EE_Error
2021
-     */
2022
-    public function get_model()
2023
-    {
2024
-        if (! $this->_model) {
2025
-            $modelName = self::_get_model_classname(get_class($this));
2026
-            $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone);
2027
-        } else {
2028
-            $this->_model->set_timezone($this->_timezone);
2029
-        }
2030
-        return $this->_model;
2031
-    }
2032
-
2033
-
2034
-    /**
2035
-     * @param $props_n_values
2036
-     * @param $classname
2037
-     * @return mixed bool|EE_Base_Class|EEM_CPT_Base
2038
-     * @throws ReflectionException
2039
-     * @throws InvalidArgumentException
2040
-     * @throws InvalidInterfaceException
2041
-     * @throws InvalidDataTypeException
2042
-     * @throws EE_Error
2043
-     */
2044
-    protected static function _get_object_from_entity_mapper($props_n_values, $classname)
2045
-    {
2046
-        // TODO: will not work for Term_Relationships because they have no PK!
2047
-        $primary_id_ref = self::_get_primary_key_name($classname);
2048
-        if (array_key_exists($primary_id_ref, $props_n_values)
2049
-            && ! empty($props_n_values[ $primary_id_ref ])
2050
-        ) {
2051
-            $id = $props_n_values[ $primary_id_ref ];
2052
-            return self::_get_model($classname)->get_from_entity_map($id);
2053
-        }
2054
-        return false;
2055
-    }
2056
-
2057
-
2058
-    /**
2059
-     * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for
2060
-     * the primary key (if present in incoming values). If there is a key in the incoming array that matches the
2061
-     * primary key for the model AND it is not null, then we check the db. If there's a an object we return it.  If not
2062
-     * we return false.
2063
-     *
2064
-     * @param  array  $props_n_values   incoming array of properties and their values
2065
-     * @param  string $classname        the classname of the child class
2066
-     * @param null    $timezone
2067
-     * @param array   $date_formats     incoming date_formats in an array where the first value is the
2068
-     *                                  date_format and the second value is the time format
2069
-     * @return mixed (EE_Base_Class|bool)
2070
-     * @throws InvalidArgumentException
2071
-     * @throws InvalidInterfaceException
2072
-     * @throws InvalidDataTypeException
2073
-     * @throws EE_Error
2074
-     * @throws ReflectionException
2075
-     * @throws ReflectionException
2076
-     * @throws ReflectionException
2077
-     */
2078
-    protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array())
2079
-    {
2080
-        $existing = null;
2081
-        $model = self::_get_model($classname, $timezone);
2082
-        if ($model->has_primary_key_field()) {
2083
-            $primary_id_ref = self::_get_primary_key_name($classname);
2084
-            if (array_key_exists($primary_id_ref, $props_n_values)
2085
-                && ! empty($props_n_values[ $primary_id_ref ])
2086
-            ) {
2087
-                $existing = $model->get_one_by_ID(
2088
-                    $props_n_values[ $primary_id_ref ]
2089
-                );
2090
-            }
2091
-        } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) {
2092
-            // no primary key on this model, but there's still a matching item in the DB
2093
-            $existing = self::_get_model($classname, $timezone)->get_one_by_ID(
2094
-                self::_get_model($classname, $timezone)
2095
-                    ->get_index_primary_key_string($props_n_values)
2096
-            );
2097
-        }
2098
-        if ($existing) {
2099
-            // set date formats if present before setting values
2100
-            if (! empty($date_formats) && is_array($date_formats)) {
2101
-                $existing->set_date_format($date_formats[0]);
2102
-                $existing->set_time_format($date_formats[1]);
2103
-            } else {
2104
-                // set default formats for date and time
2105
-                $existing->set_date_format(get_option('date_format'));
2106
-                $existing->set_time_format(get_option('time_format'));
2107
-            }
2108
-            foreach ($props_n_values as $property => $field_value) {
2109
-                $existing->set($property, $field_value);
2110
-            }
2111
-            return $existing;
2112
-        }
2113
-        return false;
2114
-    }
2115
-
2116
-
2117
-    /**
2118
-     * Gets the EEM_*_Model for this class
2119
-     *
2120
-     * @access public now, as this is more convenient
2121
-     * @param      $classname
2122
-     * @param null $timezone
2123
-     * @throws ReflectionException
2124
-     * @throws InvalidArgumentException
2125
-     * @throws InvalidInterfaceException
2126
-     * @throws InvalidDataTypeException
2127
-     * @throws EE_Error
2128
-     * @return EEM_Base
2129
-     */
2130
-    protected static function _get_model($classname, $timezone = null)
2131
-    {
2132
-        // find model for this class
2133
-        if (! $classname) {
2134
-            throw new EE_Error(
2135
-                sprintf(
2136
-                    esc_html__(
2137
-                        'What were you thinking calling _get_model(%s)?? You need to specify the class name',
2138
-                        'event_espresso'
2139
-                    ),
2140
-                    $classname
2141
-                )
2142
-            );
2143
-        }
2144
-        $modelName = self::_get_model_classname($classname);
2145
-        return self::_get_model_instance_with_name($modelName, $timezone);
2146
-    }
2147
-
2148
-
2149
-    /**
2150
-     * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee)
2151
-     *
2152
-     * @param string $model_classname
2153
-     * @param null   $timezone
2154
-     * @return EEM_Base
2155
-     * @throws ReflectionException
2156
-     * @throws InvalidArgumentException
2157
-     * @throws InvalidInterfaceException
2158
-     * @throws InvalidDataTypeException
2159
-     * @throws EE_Error
2160
-     */
2161
-    protected static function _get_model_instance_with_name($model_classname, $timezone = null)
2162
-    {
2163
-        $model_classname = str_replace('EEM_', '', $model_classname);
2164
-        $model = EE_Registry::instance()->load_model($model_classname);
2165
-        $model->set_timezone($timezone);
2166
-        return $model;
2167
-    }
2168
-
2169
-
2170
-    /**
2171
-     * If a model name is provided (eg Registration), gets the model classname for that model.
2172
-     * Also works if a model class's classname is provided (eg EE_Registration).
2173
-     *
2174
-     * @param null $model_name
2175
-     * @return string like EEM_Attendee
2176
-     */
2177
-    private static function _get_model_classname($model_name = null)
2178
-    {
2179
-        if (strpos($model_name, 'EE_') === 0) {
2180
-            $model_classname = str_replace('EE_', 'EEM_', $model_name);
2181
-        } else {
2182
-            $model_classname = 'EEM_' . $model_name;
2183
-        }
2184
-        return $model_classname;
2185
-    }
2186
-
2187
-
2188
-    /**
2189
-     * returns the name of the primary key attribute
2190
-     *
2191
-     * @param null $classname
2192
-     * @throws ReflectionException
2193
-     * @throws InvalidArgumentException
2194
-     * @throws InvalidInterfaceException
2195
-     * @throws InvalidDataTypeException
2196
-     * @throws EE_Error
2197
-     * @return string
2198
-     */
2199
-    protected static function _get_primary_key_name($classname = null)
2200
-    {
2201
-        if (! $classname) {
2202
-            throw new EE_Error(
2203
-                sprintf(
2204
-                    esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'),
2205
-                    $classname
2206
-                )
2207
-            );
2208
-        }
2209
-        return self::_get_model($classname)->get_primary_key_field()->get_name();
2210
-    }
2211
-
2212
-
2213
-    /**
2214
-     * Gets the value of the primary key.
2215
-     * If the object hasn't yet been saved, it should be whatever the model field's default was
2216
-     * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value
2217
-     * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL).
2218
-     *
2219
-     * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string
2220
-     * @throws ReflectionException
2221
-     * @throws InvalidArgumentException
2222
-     * @throws InvalidInterfaceException
2223
-     * @throws InvalidDataTypeException
2224
-     * @throws EE_Error
2225
-     */
2226
-    public function ID()
2227
-    {
2228
-        $model = $this->get_model();
2229
-        // now that we know the name of the variable, use a variable variable to get its value and return its
2230
-        if ($model->has_primary_key_field()) {
2231
-            return $this->_fields[ $model->primary_key_name() ];
2232
-        }
2233
-        return $model->get_index_primary_key_string($this->_fields);
2234
-    }
2235
-
2236
-
2237
-    /**
2238
-     * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current
2239
-     * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE
2240
-     * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing
2241
-     *
2242
-     * @param mixed  $otherObjectModelObjectOrID       EE_Base_Class or the ID of the other object
2243
-     * @param string $relationName                     eg 'Events','Question',etc.
2244
-     *                                                 an attendee to a group, you also want to specify which role they
2245
-     *                                                 will have in that group. So you would use this parameter to
2246
-     *                                                 specify array('role-column-name'=>'role-id')
2247
-     * @param array  $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that
2248
-     *                                                 allow you to further constrict the relation to being added.
2249
-     *                                                 However, keep in mind that the columns (keys) given must match a
2250
-     *                                                 column on the JOIN table and currently only the HABTM models
2251
-     *                                                 accept these additional conditions.  Also remember that if an
2252
-     *                                                 exact match isn't found for these extra cols/val pairs, then a
2253
-     *                                                 NEW row is created in the join table.
2254
-     * @param null   $cache_id
2255
-     * @throws ReflectionException
2256
-     * @throws InvalidArgumentException
2257
-     * @throws InvalidInterfaceException
2258
-     * @throws InvalidDataTypeException
2259
-     * @throws EE_Error
2260
-     * @return EE_Base_Class the object the relation was added to
2261
-     */
2262
-    public function _add_relation_to(
2263
-        $otherObjectModelObjectOrID,
2264
-        $relationName,
2265
-        $extra_join_model_fields_n_values = array(),
2266
-        $cache_id = null
2267
-    ) {
2268
-        $model = $this->get_model();
2269
-        // if this thing exists in the DB, save the relation to the DB
2270
-        if ($this->ID()) {
2271
-            $otherObject = $model->add_relationship_to(
2272
-                $this,
2273
-                $otherObjectModelObjectOrID,
2274
-                $relationName,
2275
-                $extra_join_model_fields_n_values
2276
-            );
2277
-            // clear cache so future get_many_related and get_first_related() return new results.
2278
-            $this->clear_cache($relationName, $otherObject, true);
2279
-            if ($otherObject instanceof EE_Base_Class) {
2280
-                $otherObject->clear_cache($model->get_this_model_name(), $this);
2281
-            }
2282
-        } else {
2283
-            // this thing doesn't exist in the DB,  so just cache it
2284
-            if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) {
2285
-                throw new EE_Error(
2286
-                    sprintf(
2287
-                        esc_html__(
2288
-                            'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s',
2289
-                            'event_espresso'
2290
-                        ),
2291
-                        $otherObjectModelObjectOrID,
2292
-                        get_class($this)
2293
-                    )
2294
-                );
2295
-            }
2296
-            $otherObject = $otherObjectModelObjectOrID;
2297
-            $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id);
2298
-        }
2299
-        if ($otherObject instanceof EE_Base_Class) {
2300
-            // fix the reciprocal relation too
2301
-            if ($otherObject->ID()) {
2302
-                // its saved so assumed relations exist in the DB, so we can just
2303
-                // clear the cache so future queries use the updated info in the DB
2304
-                $otherObject->clear_cache(
2305
-                    $model->get_this_model_name(),
2306
-                    null,
2307
-                    true
2308
-                );
2309
-            } else {
2310
-                // it's not saved, so it caches relations like this
2311
-                $otherObject->cache($model->get_this_model_name(), $this);
2312
-            }
2313
-        }
2314
-        return $otherObject;
2315
-    }
2316
-
2317
-
2318
-    /**
2319
-     * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current
2320
-     * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE
2321
-     * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing
2322
-     * from the cache
2323
-     *
2324
-     * @param mixed  $otherObjectModelObjectOrID
2325
-     *                EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved
2326
-     *                to the DB yet
2327
-     * @param string $relationName
2328
-     * @param array  $where_query
2329
-     *                You can optionally include an array of key=>value pairs that allow you to further constrict the
2330
-     *                relation to being added. However, keep in mind that the columns (keys) given must match a column
2331
-     *                on the JOIN table and currently only the HABTM models accept these additional conditions. Also
2332
-     *                remember that if an exact match isn't found for these extra cols/val pairs, then no row is
2333
-     *                deleted.
2334
-     * @return EE_Base_Class the relation was removed from
2335
-     * @throws ReflectionException
2336
-     * @throws InvalidArgumentException
2337
-     * @throws InvalidInterfaceException
2338
-     * @throws InvalidDataTypeException
2339
-     * @throws EE_Error
2340
-     */
2341
-    public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array())
2342
-    {
2343
-        if ($this->ID()) {
2344
-            // if this exists in the DB, save the relation change to the DB too
2345
-            $otherObject = $this->get_model()->remove_relationship_to(
2346
-                $this,
2347
-                $otherObjectModelObjectOrID,
2348
-                $relationName,
2349
-                $where_query
2350
-            );
2351
-            $this->clear_cache(
2352
-                $relationName,
2353
-                $otherObject
2354
-            );
2355
-        } else {
2356
-            // this doesn't exist in the DB, just remove it from the cache
2357
-            $otherObject = $this->clear_cache(
2358
-                $relationName,
2359
-                $otherObjectModelObjectOrID
2360
-            );
2361
-        }
2362
-        if ($otherObject instanceof EE_Base_Class) {
2363
-            $otherObject->clear_cache(
2364
-                $this->get_model()->get_this_model_name(),
2365
-                $this
2366
-            );
2367
-        }
2368
-        return $otherObject;
2369
-    }
2370
-
2371
-
2372
-    /**
2373
-     * Removes ALL the related things for the $relationName.
2374
-     *
2375
-     * @param string $relationName
2376
-     * @param array  $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
2377
-     * @return EE_Base_Class
2378
-     * @throws ReflectionException
2379
-     * @throws InvalidArgumentException
2380
-     * @throws InvalidInterfaceException
2381
-     * @throws InvalidDataTypeException
2382
-     * @throws EE_Error
2383
-     */
2384
-    public function _remove_relations($relationName, $where_query_params = array())
2385
-    {
2386
-        if ($this->ID()) {
2387
-            // if this exists in the DB, save the relation change to the DB too
2388
-            $otherObjects = $this->get_model()->remove_relations(
2389
-                $this,
2390
-                $relationName,
2391
-                $where_query_params
2392
-            );
2393
-            $this->clear_cache(
2394
-                $relationName,
2395
-                null,
2396
-                true
2397
-            );
2398
-        } else {
2399
-            // this doesn't exist in the DB, just remove it from the cache
2400
-            $otherObjects = $this->clear_cache(
2401
-                $relationName,
2402
-                null,
2403
-                true
2404
-            );
2405
-        }
2406
-        if (is_array($otherObjects)) {
2407
-            foreach ($otherObjects as $otherObject) {
2408
-                $otherObject->clear_cache(
2409
-                    $this->get_model()->get_this_model_name(),
2410
-                    $this
2411
-                );
2412
-            }
2413
-        }
2414
-        return $otherObjects;
2415
-    }
2416
-
2417
-
2418
-    /**
2419
-     * Gets all the related model objects of the specified type. Eg, if the current class if
2420
-     * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the
2421
-     * EE_Registration objects which related to this event. Note: by default, we remove the "default query params"
2422
-     * because we want to get even deleted items etc.
2423
-     *
2424
-     * @param string $relationName key in the model's _model_relations array
2425
-     * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
2426
-     * @return EE_Base_Class[]     Results not necessarily indexed by IDs, because some results might not have primary
2427
-     *                             keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these
2428
-     *                             results if you want IDs
2429
-     * @throws ReflectionException
2430
-     * @throws InvalidArgumentException
2431
-     * @throws InvalidInterfaceException
2432
-     * @throws InvalidDataTypeException
2433
-     * @throws EE_Error
2434
-     */
2435
-    public function get_many_related($relationName, $query_params = array())
2436
-    {
2437
-        if ($this->ID()) {
2438
-            // this exists in the DB, so get the related things from either the cache or the DB
2439
-            // if there are query parameters, forget about caching the related model objects.
2440
-            if ($query_params) {
2441
-                $related_model_objects = $this->get_model()->get_all_related(
2442
-                    $this,
2443
-                    $relationName,
2444
-                    $query_params
2445
-                );
2446
-            } else {
2447
-                // did we already cache the result of this query?
2448
-                $cached_results = $this->get_all_from_cache($relationName);
2449
-                if (! $cached_results) {
2450
-                    $related_model_objects = $this->get_model()->get_all_related(
2451
-                        $this,
2452
-                        $relationName,
2453
-                        $query_params
2454
-                    );
2455
-                    // if no query parameters were passed, then we got all the related model objects
2456
-                    // for that relation. We can cache them then.
2457
-                    foreach ($related_model_objects as $related_model_object) {
2458
-                        $this->cache($relationName, $related_model_object);
2459
-                    }
2460
-                } else {
2461
-                    $related_model_objects = $cached_results;
2462
-                }
2463
-            }
2464
-        } else {
2465
-            // this doesn't exist in the DB, so just get the related things from the cache
2466
-            $related_model_objects = $this->get_all_from_cache($relationName);
2467
-        }
2468
-        return $related_model_objects;
2469
-    }
2470
-
2471
-
2472
-    /**
2473
-     * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default,
2474
-     * unless otherwise specified in the $query_params
2475
-     *
2476
-     * @param string $relation_name  model_name like 'Event', or 'Registration'
2477
-     * @param array  $query_params   @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2478
-     * @param string $field_to_count name of field to count by. By default, uses primary key
2479
-     * @param bool   $distinct       if we want to only count the distinct values for the column then you can trigger
2480
-     *                               that by the setting $distinct to TRUE;
2481
-     * @return int
2482
-     * @throws ReflectionException
2483
-     * @throws InvalidArgumentException
2484
-     * @throws InvalidInterfaceException
2485
-     * @throws InvalidDataTypeException
2486
-     * @throws EE_Error
2487
-     */
2488
-    public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false)
2489
-    {
2490
-        return $this->get_model()->count_related(
2491
-            $this,
2492
-            $relation_name,
2493
-            $query_params,
2494
-            $field_to_count,
2495
-            $distinct
2496
-        );
2497
-    }
2498
-
2499
-
2500
-    /**
2501
-     * Instead of getting the related model objects, simply sums up the values of the specified field.
2502
-     * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params
2503
-     *
2504
-     * @param string $relation_name model_name like 'Event', or 'Registration'
2505
-     * @param array  $query_params  @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2506
-     * @param string $field_to_sum  name of field to count by.
2507
-     *                              By default, uses primary key
2508
-     *                              (which doesn't make much sense, so you should probably change it)
2509
-     * @return int
2510
-     * @throws ReflectionException
2511
-     * @throws InvalidArgumentException
2512
-     * @throws InvalidInterfaceException
2513
-     * @throws InvalidDataTypeException
2514
-     * @throws EE_Error
2515
-     */
2516
-    public function sum_related($relation_name, $query_params = array(), $field_to_sum = null)
2517
-    {
2518
-        return $this->get_model()->sum_related(
2519
-            $this,
2520
-            $relation_name,
2521
-            $query_params,
2522
-            $field_to_sum
2523
-        );
2524
-    }
2525
-
2526
-
2527
-    /**
2528
-     * Gets the first (ie, one) related model object of the specified type.
2529
-     *
2530
-     * @param string $relationName key in the model's _model_relations array
2531
-     * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2532
-     * @return EE_Base_Class (not an array, a single object)
2533
-     * @throws ReflectionException
2534
-     * @throws InvalidArgumentException
2535
-     * @throws InvalidInterfaceException
2536
-     * @throws InvalidDataTypeException
2537
-     * @throws EE_Error
2538
-     */
2539
-    public function get_first_related($relationName, $query_params = array())
2540
-    {
2541
-        $model = $this->get_model();
2542
-        if ($this->ID()) {// this exists in the DB, get from the cache OR the DB
2543
-            // if they've provided some query parameters, don't bother trying to cache the result
2544
-            // also make sure we're not caching the result of get_first_related
2545
-            // on a relation which should have an array of objects (because the cache might have an array of objects)
2546
-            if ($query_params
2547
-                || ! $model->related_settings_for($relationName)
2548
-                     instanceof
2549
-                     EE_Belongs_To_Relation
2550
-            ) {
2551
-                $related_model_object = $model->get_first_related(
2552
-                    $this,
2553
-                    $relationName,
2554
-                    $query_params
2555
-                );
2556
-            } else {
2557
-                // first, check if we've already cached the result of this query
2558
-                $cached_result = $this->get_one_from_cache($relationName);
2559
-                if (! $cached_result) {
2560
-                    $related_model_object = $model->get_first_related(
2561
-                        $this,
2562
-                        $relationName,
2563
-                        $query_params
2564
-                    );
2565
-                    $this->cache($relationName, $related_model_object);
2566
-                } else {
2567
-                    $related_model_object = $cached_result;
2568
-                }
2569
-            }
2570
-        } else {
2571
-            $related_model_object = null;
2572
-            // this doesn't exist in the Db,
2573
-            // but maybe the relation is of type belongs to, and so the related thing might
2574
-            if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) {
2575
-                $related_model_object = $model->get_first_related(
2576
-                    $this,
2577
-                    $relationName,
2578
-                    $query_params
2579
-                );
2580
-            }
2581
-            // this doesn't exist in the DB and apparently the thing it belongs to doesn't either,
2582
-            // just get what's cached on this object
2583
-            if (! $related_model_object) {
2584
-                $related_model_object = $this->get_one_from_cache($relationName);
2585
-            }
2586
-        }
2587
-        return $related_model_object;
2588
-    }
2589
-
2590
-
2591
-    /**
2592
-     * Does a delete on all related objects of type $relationName and removes
2593
-     * the current model object's relation to them. If they can't be deleted (because
2594
-     * of blocking related model objects) does nothing. If the related model objects are
2595
-     * soft-deletable, they will be soft-deleted regardless of related blocking model objects.
2596
-     * If this model object doesn't exist yet in the DB, just removes its related things
2597
-     *
2598
-     * @param string $relationName
2599
-     * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2600
-     * @return int how many deleted
2601
-     * @throws ReflectionException
2602
-     * @throws InvalidArgumentException
2603
-     * @throws InvalidInterfaceException
2604
-     * @throws InvalidDataTypeException
2605
-     * @throws EE_Error
2606
-     */
2607
-    public function delete_related($relationName, $query_params = array())
2608
-    {
2609
-        if ($this->ID()) {
2610
-            $count = $this->get_model()->delete_related(
2611
-                $this,
2612
-                $relationName,
2613
-                $query_params
2614
-            );
2615
-        } else {
2616
-            $count = count($this->get_all_from_cache($relationName));
2617
-            $this->clear_cache($relationName, null, true);
2618
-        }
2619
-        return $count;
2620
-    }
2621
-
2622
-
2623
-    /**
2624
-     * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes
2625
-     * the current model object's relation to them. If they can't be deleted (because
2626
-     * of blocking related model objects) just does a soft delete on it instead, if possible.
2627
-     * If the related thing isn't a soft-deletable model object, this function is identical
2628
-     * to delete_related(). If this model object doesn't exist in the DB, just remove its related things
2629
-     *
2630
-     * @param string $relationName
2631
-     * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2632
-     * @return int how many deleted (including those soft deleted)
2633
-     * @throws ReflectionException
2634
-     * @throws InvalidArgumentException
2635
-     * @throws InvalidInterfaceException
2636
-     * @throws InvalidDataTypeException
2637
-     * @throws EE_Error
2638
-     */
2639
-    public function delete_related_permanently($relationName, $query_params = array())
2640
-    {
2641
-        if ($this->ID()) {
2642
-            $count = $this->get_model()->delete_related_permanently(
2643
-                $this,
2644
-                $relationName,
2645
-                $query_params
2646
-            );
2647
-        } else {
2648
-            $count = count($this->get_all_from_cache($relationName));
2649
-        }
2650
-        $this->clear_cache($relationName, null, true);
2651
-        return $count;
2652
-    }
2653
-
2654
-
2655
-    /**
2656
-     * is_set
2657
-     * Just a simple utility function children can use for checking if property exists
2658
-     *
2659
-     * @access  public
2660
-     * @param  string $field_name property to check
2661
-     * @return bool                              TRUE if existing,FALSE if not.
2662
-     */
2663
-    public function is_set($field_name)
2664
-    {
2665
-        return isset($this->_fields[ $field_name ]);
2666
-    }
2667
-
2668
-
2669
-    /**
2670
-     * Just a simple utility function children can use for checking if property (or properties) exists and throwing an
2671
-     * EE_Error exception if they don't
2672
-     *
2673
-     * @param  mixed (string|array) $properties properties to check
2674
-     * @throws EE_Error
2675
-     * @return bool                              TRUE if existing, throw EE_Error if not.
2676
-     */
2677
-    protected function _property_exists($properties)
2678
-    {
2679
-        foreach ((array) $properties as $property_name) {
2680
-            // first make sure this property exists
2681
-            if (! $this->_fields[ $property_name ]) {
2682
-                throw new EE_Error(
2683
-                    sprintf(
2684
-                        esc_html__(
2685
-                            'Trying to retrieve a non-existent property (%s).  Double check the spelling please',
2686
-                            'event_espresso'
2687
-                        ),
2688
-                        $property_name
2689
-                    )
2690
-                );
2691
-            }
2692
-        }
2693
-        return true;
2694
-    }
2695
-
2696
-
2697
-    /**
2698
-     * This simply returns an array of model fields for this object
2699
-     *
2700
-     * @return array
2701
-     * @throws ReflectionException
2702
-     * @throws InvalidArgumentException
2703
-     * @throws InvalidInterfaceException
2704
-     * @throws InvalidDataTypeException
2705
-     * @throws EE_Error
2706
-     */
2707
-    public function model_field_array()
2708
-    {
2709
-        $fields = $this->get_model()->field_settings(false);
2710
-        $properties = array();
2711
-        // remove prepended underscore
2712
-        foreach ($fields as $field_name => $settings) {
2713
-            $properties[ $field_name ] = $this->get($field_name);
2714
-        }
2715
-        return $properties;
2716
-    }
2717
-
2718
-
2719
-    /**
2720
-     * Very handy general function to allow for plugins to extend any child of EE_Base_Class.
2721
-     * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called
2722
-     * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments.
2723
-     * Instead of requiring a plugin to extend the EE_Base_Class
2724
-     * (which works fine is there's only 1 plugin, but when will that happen?)
2725
-     * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}'
2726
-     * (eg, filters_hook_espresso__EE_Answer__my_great_function)
2727
-     * and accepts 2 arguments: the object on which the function was called,
2728
-     * and an array of the original arguments passed to the function.
2729
-     * Whatever their callback function returns will be returned by this function.
2730
-     * Example: in functions.php (or in a plugin):
2731
-     *      add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3);
2732
-     *      function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){
2733
-     *          $returnString= "you called my_callback! and passed args:".implode(",",$argsArray);
2734
-     *          return $previousReturnValue.$returnString;
2735
-     *      }
2736
-     * require('EE_Answer.class.php');
2737
-     * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42'));
2738
-     * echo $answer->my_callback('monkeys',100);
2739
-     * //will output "you called my_callback! and passed args:monkeys,100"
2740
-     *
2741
-     * @param string $methodName name of method which was called on a child of EE_Base_Class, but which
2742
-     * @param array  $args       array of original arguments passed to the function
2743
-     * @throws EE_Error
2744
-     * @return mixed whatever the plugin which calls add_filter decides
2745
-     */
2746
-    public function __call($methodName, $args)
2747
-    {
2748
-        $className = get_class($this);
2749
-        $tagName = "FHEE__{$className}__{$methodName}";
2750
-        if (! has_filter($tagName)) {
2751
-            throw new EE_Error(
2752
-                sprintf(
2753
-                    esc_html__(
2754
-                        "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}",
2755
-                        'event_espresso'
2756
-                    ),
2757
-                    $methodName,
2758
-                    $className,
2759
-                    $tagName
2760
-                )
2761
-            );
2762
-        }
2763
-        return apply_filters($tagName, null, $this, $args);
2764
-    }
2765
-
2766
-
2767
-    /**
2768
-     * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value.
2769
-     * A $previous_value can be specified in case there are many meta rows with the same key
2770
-     *
2771
-     * @param string $meta_key
2772
-     * @param mixed  $meta_value
2773
-     * @param mixed  $previous_value
2774
-     * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row)
2775
-     *                  NOTE: if the values haven't changed, returns 0
2776
-     * @throws InvalidArgumentException
2777
-     * @throws InvalidInterfaceException
2778
-     * @throws InvalidDataTypeException
2779
-     * @throws EE_Error
2780
-     * @throws ReflectionException
2781
-     */
2782
-    public function update_extra_meta($meta_key, $meta_value, $previous_value = null)
2783
-    {
2784
-        $query_params = array(
2785
-            array(
2786
-                'EXM_key'  => $meta_key,
2787
-                'OBJ_ID'   => $this->ID(),
2788
-                'EXM_type' => $this->get_model()->get_this_model_name(),
2789
-            ),
2790
-        );
2791
-        if ($previous_value !== null) {
2792
-            $query_params[0]['EXM_value'] = $meta_value;
2793
-        }
2794
-        $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params);
2795
-        if (! $existing_rows_like_that) {
2796
-            return $this->add_extra_meta($meta_key, $meta_value);
2797
-        }
2798
-        foreach ($existing_rows_like_that as $existing_row) {
2799
-            $existing_row->save(array('EXM_value' => $meta_value));
2800
-        }
2801
-        return count($existing_rows_like_that);
2802
-    }
2803
-
2804
-
2805
-    /**
2806
-     * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check
2807
-     * no other extra meta for this model object have the same key. Returns TRUE if the
2808
-     * extra meta row was entered, false if not
2809
-     *
2810
-     * @param string  $meta_key
2811
-     * @param mixed   $meta_value
2812
-     * @param boolean $unique
2813
-     * @return boolean
2814
-     * @throws InvalidArgumentException
2815
-     * @throws InvalidInterfaceException
2816
-     * @throws InvalidDataTypeException
2817
-     * @throws EE_Error
2818
-     * @throws ReflectionException
2819
-     * @throws ReflectionException
2820
-     */
2821
-    public function add_extra_meta($meta_key, $meta_value, $unique = false)
2822
-    {
2823
-        if ($unique) {
2824
-            $existing_extra_meta = EEM_Extra_Meta::instance()->get_one(
2825
-                array(
2826
-                    array(
2827
-                        'EXM_key'  => $meta_key,
2828
-                        'OBJ_ID'   => $this->ID(),
2829
-                        'EXM_type' => $this->get_model()->get_this_model_name(),
2830
-                    ),
2831
-                )
2832
-            );
2833
-            if ($existing_extra_meta) {
2834
-                return false;
2835
-            }
2836
-        }
2837
-        $new_extra_meta = EE_Extra_Meta::new_instance(
2838
-            array(
2839
-                'EXM_key'   => $meta_key,
2840
-                'EXM_value' => $meta_value,
2841
-                'OBJ_ID'    => $this->ID(),
2842
-                'EXM_type'  => $this->get_model()->get_this_model_name(),
2843
-            )
2844
-        );
2845
-        $new_extra_meta->save();
2846
-        return true;
2847
-    }
2848
-
2849
-
2850
-    /**
2851
-     * Deletes all the extra meta rows for this record as specified by key. If $meta_value
2852
-     * is specified, only deletes extra meta records with that value.
2853
-     *
2854
-     * @param string $meta_key
2855
-     * @param mixed  $meta_value
2856
-     * @return int number of extra meta rows deleted
2857
-     * @throws InvalidArgumentException
2858
-     * @throws InvalidInterfaceException
2859
-     * @throws InvalidDataTypeException
2860
-     * @throws EE_Error
2861
-     * @throws ReflectionException
2862
-     */
2863
-    public function delete_extra_meta($meta_key, $meta_value = null)
2864
-    {
2865
-        $query_params = array(
2866
-            array(
2867
-                'EXM_key'  => $meta_key,
2868
-                'OBJ_ID'   => $this->ID(),
2869
-                'EXM_type' => $this->get_model()->get_this_model_name(),
2870
-            ),
2871
-        );
2872
-        if ($meta_value !== null) {
2873
-            $query_params[0]['EXM_value'] = $meta_value;
2874
-        }
2875
-        return EEM_Extra_Meta::instance()->delete($query_params);
2876
-    }
2877
-
2878
-
2879
-    /**
2880
-     * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise
2881
-     * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation.
2882
-     * You can specify $default is case you haven't found the extra meta
2883
-     *
2884
-     * @param string  $meta_key
2885
-     * @param boolean $single
2886
-     * @param mixed   $default if we don't find anything, what should we return?
2887
-     * @return mixed single value if $single; array if ! $single
2888
-     * @throws ReflectionException
2889
-     * @throws InvalidArgumentException
2890
-     * @throws InvalidInterfaceException
2891
-     * @throws InvalidDataTypeException
2892
-     * @throws EE_Error
2893
-     */
2894
-    public function get_extra_meta($meta_key, $single = false, $default = null)
2895
-    {
2896
-        if ($single) {
2897
-            $result = $this->get_first_related(
2898
-                'Extra_Meta',
2899
-                array(array('EXM_key' => $meta_key))
2900
-            );
2901
-            if ($result instanceof EE_Extra_Meta) {
2902
-                return $result->value();
2903
-            }
2904
-        } else {
2905
-            $results = $this->get_many_related(
2906
-                'Extra_Meta',
2907
-                array(array('EXM_key' => $meta_key))
2908
-            );
2909
-            if ($results) {
2910
-                $values = array();
2911
-                foreach ($results as $result) {
2912
-                    if ($result instanceof EE_Extra_Meta) {
2913
-                        $values[ $result->ID() ] = $result->value();
2914
-                    }
2915
-                }
2916
-                return $values;
2917
-            }
2918
-        }
2919
-        // if nothing discovered yet return default.
2920
-        return apply_filters(
2921
-            'FHEE__EE_Base_Class__get_extra_meta__default_value',
2922
-            $default,
2923
-            $meta_key,
2924
-            $single,
2925
-            $this
2926
-        );
2927
-    }
2928
-
2929
-
2930
-    /**
2931
-     * Returns a simple array of all the extra meta associated with this model object.
2932
-     * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the
2933
-     * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with
2934
-     * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123))
2935
-     * If $one_of_each_key is false, it will return an array with the top-level keys being
2936
-     * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and
2937
-     * finally the extra meta's value as each sub-value. (eg
2938
-     * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123)))
2939
-     *
2940
-     * @param boolean $one_of_each_key
2941
-     * @return array
2942
-     * @throws ReflectionException
2943
-     * @throws InvalidArgumentException
2944
-     * @throws InvalidInterfaceException
2945
-     * @throws InvalidDataTypeException
2946
-     * @throws EE_Error
2947
-     */
2948
-    public function all_extra_meta_array($one_of_each_key = true)
2949
-    {
2950
-        $return_array = array();
2951
-        if ($one_of_each_key) {
2952
-            $extra_meta_objs = $this->get_many_related(
2953
-                'Extra_Meta',
2954
-                array('group_by' => 'EXM_key')
2955
-            );
2956
-            foreach ($extra_meta_objs as $extra_meta_obj) {
2957
-                if ($extra_meta_obj instanceof EE_Extra_Meta) {
2958
-                    $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value();
2959
-                }
2960
-            }
2961
-        } else {
2962
-            $extra_meta_objs = $this->get_many_related('Extra_Meta');
2963
-            foreach ($extra_meta_objs as $extra_meta_obj) {
2964
-                if ($extra_meta_obj instanceof EE_Extra_Meta) {
2965
-                    if (! isset($return_array[ $extra_meta_obj->key() ])) {
2966
-                        $return_array[ $extra_meta_obj->key() ] = array();
2967
-                    }
2968
-                    $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value();
2969
-                }
2970
-            }
2971
-        }
2972
-        return $return_array;
2973
-    }
2974
-
2975
-
2976
-    /**
2977
-     * Gets a pretty nice displayable nice for this model object. Often overridden
2978
-     *
2979
-     * @return string
2980
-     * @throws ReflectionException
2981
-     * @throws InvalidArgumentException
2982
-     * @throws InvalidInterfaceException
2983
-     * @throws InvalidDataTypeException
2984
-     * @throws EE_Error
2985
-     */
2986
-    public function name()
2987
-    {
2988
-        // find a field that's not a text field
2989
-        $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base');
2990
-        if ($field_we_can_use) {
2991
-            return $this->get($field_we_can_use->get_name());
2992
-        }
2993
-        $first_few_properties = $this->model_field_array();
2994
-        $first_few_properties = array_slice($first_few_properties, 0, 3);
2995
-        $name_parts = array();
2996
-        foreach ($first_few_properties as $name => $value) {
2997
-            $name_parts[] = "$name:$value";
2998
-        }
2999
-        return implode(',', $name_parts);
3000
-    }
3001
-
3002
-
3003
-    /**
3004
-     * in_entity_map
3005
-     * Checks if this model object has been proven to already be in the entity map
3006
-     *
3007
-     * @return boolean
3008
-     * @throws ReflectionException
3009
-     * @throws InvalidArgumentException
3010
-     * @throws InvalidInterfaceException
3011
-     * @throws InvalidDataTypeException
3012
-     * @throws EE_Error
3013
-     */
3014
-    public function in_entity_map()
3015
-    {
3016
-        // well, if we looked, did we find it in the entity map?
3017
-        return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this;
3018
-    }
3019
-
3020
-
3021
-    /**
3022
-     * refresh_from_db
3023
-     * Makes sure the fields and values on this model object are in-sync with what's in the database.
3024
-     *
3025
-     * @throws ReflectionException
3026
-     * @throws InvalidArgumentException
3027
-     * @throws InvalidInterfaceException
3028
-     * @throws InvalidDataTypeException
3029
-     * @throws EE_Error if this model object isn't in the entity mapper (because then you should
3030
-     * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE
3031
-     */
3032
-    public function refresh_from_db()
3033
-    {
3034
-        if ($this->ID() && $this->in_entity_map()) {
3035
-            $this->get_model()->refresh_entity_map_from_db($this->ID());
3036
-        } else {
3037
-            // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database)
3038
-            // if it has an ID but it's not in the map, and you're asking me to refresh it
3039
-            // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's
3040
-            // absolutely nothing in it for this ID
3041
-            if (WP_DEBUG) {
3042
-                throw new EE_Error(
3043
-                    sprintf(
3044
-                        esc_html__(
3045
-                            'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.',
3046
-                            'event_espresso'
3047
-                        ),
3048
-                        $this->ID(),
3049
-                        get_class($this->get_model()) . '::instance()->add_to_entity_map()',
3050
-                        get_class($this->get_model()) . '::instance()->refresh_entity_map()'
3051
-                    )
3052
-                );
3053
-            }
3054
-        }
3055
-    }
3056
-
3057
-
3058
-    /**
3059
-     * Change $fields' values to $new_value_sql (which is a string of raw SQL)
3060
-     *
3061
-     * @since $VID:$
3062
-     * @param EE_Model_Field_Base[] $fields
3063
-     * @param string $new_value_sql
3064
-     *      example: 'column_name=123',
3065
-     *      or 'column_name=column_name+1',
3066
-     *      or 'column_name= CASE
3067
-     *          WHEN (`column_name` + `other_column` + 5) <= `yet_another_column`
3068
-     *          THEN `column_name` + 5
3069
-     *          ELSE `column_name`
3070
-     *      END'
3071
-     *      Also updates $field on this model object with the latest value from the database.
3072
-     * @return bool
3073
-     * @throws EE_Error
3074
-     * @throws InvalidArgumentException
3075
-     * @throws InvalidDataTypeException
3076
-     * @throws InvalidInterfaceException
3077
-     * @throws ReflectionException
3078
-     */
3079
-    protected function updateFieldsInDB($fields, $new_value_sql)
3080
-    {
3081
-        // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB
3082
-        // if it wasn't even there to start off.
3083
-        if (! $this->ID()) {
3084
-            $this->save();
3085
-        }
3086
-        global $wpdb;
3087
-        if (empty($fields)) {
3088
-            throw new InvalidArgumentException(
3089
-                esc_html__(
3090
-                    'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.',
3091
-                    'event_espresso'
3092
-                )
3093
-            );
3094
-        }
3095
-        $first_field = reset($fields);
3096
-        $table_alias = $first_field->get_table_alias();
3097
-        foreach ($fields as $field) {
3098
-            if ($table_alias !== $field->get_table_alias()) {
3099
-                throw new InvalidArgumentException(
3100
-                    sprintf(
3101
-                        esc_html__(
3102
-                            // @codingStandardsIgnoreStart
3103
-                            'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.',
3104
-                            // @codingStandardsIgnoreEnd
3105
-                            'event_espresso'
3106
-                        ),
3107
-                        $table_alias,
3108
-                        $field->get_table_alias()
3109
-                    )
3110
-                );
3111
-            }
3112
-        }
3113
-        // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it.
3114
-        $table_obj = $this->get_model()->get_table_obj_by_alias($table_alias);
3115
-        $table_pk_value = $this->ID();
3116
-        $table_name = $table_obj->get_table_name();
3117
-        if ($table_obj instanceof EE_Secondary_Table) {
3118
-            $table_pk_field_name = $table_obj->get_fk_on_table();
3119
-        } else {
3120
-            $table_pk_field_name = $table_obj->get_pk_column();
3121
-        }
3122
-
3123
-        $query =
3124
-            "UPDATE `{$table_name}`
16
+	/**
17
+	 * This is an array of the original properties and values provided during construction
18
+	 * of this model object. (keys are model field names, values are their values).
19
+	 * This list is important to remember so that when we are merging data from the db, we know
20
+	 * which values to override and which to not override.
21
+	 *
22
+	 * @var array
23
+	 */
24
+	protected $_props_n_values_provided_in_constructor;
25
+
26
+	/**
27
+	 * Timezone
28
+	 * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in.
29
+	 * This can also be used before a get to set what timezone you want strings coming out of the object to be in.  NOT
30
+	 * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have
31
+	 * access to it.
32
+	 *
33
+	 * @var string
34
+	 */
35
+	protected $_timezone;
36
+
37
+	/**
38
+	 * date format
39
+	 * pattern or format for displaying dates
40
+	 *
41
+	 * @var string $_dt_frmt
42
+	 */
43
+	protected $_dt_frmt;
44
+
45
+	/**
46
+	 * time format
47
+	 * pattern or format for displaying time
48
+	 *
49
+	 * @var string $_tm_frmt
50
+	 */
51
+	protected $_tm_frmt;
52
+
53
+	/**
54
+	 * This property is for holding a cached array of object properties indexed by property name as the key.
55
+	 * The purpose of this is for setting a cache on properties that may have calculated values after a
56
+	 * prepare_for_get.  That way the cache can be checked first and the calculated property returned instead of having
57
+	 * to recalculate. Used by _set_cached_property() and _get_cached_property() methods.
58
+	 *
59
+	 * @var array
60
+	 */
61
+	protected $_cached_properties = array();
62
+
63
+	/**
64
+	 * An array containing keys of the related model, and values are either an array of related mode objects or a
65
+	 * single
66
+	 * related model object. see the model's _model_relations. The keys should match those specified. And if the
67
+	 * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object,
68
+	 * all others have an array)
69
+	 *
70
+	 * @var array
71
+	 */
72
+	protected $_model_relations = array();
73
+
74
+	/**
75
+	 * Array where keys are field names (see the model's _fields property) and values are their values. To see what
76
+	 * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods)
77
+	 *
78
+	 * @var array
79
+	 */
80
+	protected $_fields = array();
81
+
82
+	/**
83
+	 * @var boolean indicating whether or not this model object is intended to ever be saved
84
+	 * For example, we might create model objects intended to only be used for the duration
85
+	 * of this request and to be thrown away, and if they were accidentally saved
86
+	 * it would be a bug.
87
+	 */
88
+	protected $_allow_persist = true;
89
+
90
+	/**
91
+	 * @var boolean indicating whether or not this model object's properties have changed since construction
92
+	 */
93
+	protected $_has_changes = false;
94
+
95
+	/**
96
+	 * @var EEM_Base
97
+	 */
98
+	protected $_model;
99
+
100
+	/**
101
+	 * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose
102
+	 * for these values is for retrieval of the results, they are not further queryable and they are not persisted to
103
+	 * the db.  They also do not automatically update if there are any changes to the data that produced their results.
104
+	 * The format is a simple array of field_alias => field_value.  So for instance if a custom select was something
105
+	 * like,  "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this
106
+	 * array as:
107
+	 * array(
108
+	 *  'Registration_Count' => 24
109
+	 * );
110
+	 * Note: if the custom select configuration for the query included a data type, the value will be in the data type
111
+	 * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more
112
+	 * info)
113
+	 *
114
+	 * @var array
115
+	 */
116
+	protected $custom_selection_results = array();
117
+
118
+
119
+	/**
120
+	 * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children
121
+	 * play nice
122
+	 *
123
+	 * @param array   $fieldValues                             where each key is a field (ie, array key in the 2nd
124
+	 *                                                         layer of the model's _fields array, (eg, EVT_ID,
125
+	 *                                                         TXN_amount, QST_name, etc) and values are their values
126
+	 * @param boolean $bydb                                    a flag for setting if the class is instantiated by the
127
+	 *                                                         corresponding db model or not.
128
+	 * @param string  $timezone                                indicate what timezone you want any datetime fields to
129
+	 *                                                         be in when instantiating a EE_Base_Class object.
130
+	 * @param array   $date_formats                            An array of date formats to set on construct where first
131
+	 *                                                         value is the date_format and second value is the time
132
+	 *                                                         format.
133
+	 * @throws InvalidArgumentException
134
+	 * @throws InvalidInterfaceException
135
+	 * @throws InvalidDataTypeException
136
+	 * @throws EE_Error
137
+	 * @throws ReflectionException
138
+	 */
139
+	protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array())
140
+	{
141
+		$className = get_class($this);
142
+		do_action("AHEE__{$className}__construct", $this, $fieldValues);
143
+		$model = $this->get_model();
144
+		$model_fields = $model->field_settings(false);
145
+		// ensure $fieldValues is an array
146
+		$fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues);
147
+		// verify client code has not passed any invalid field names
148
+		foreach ($fieldValues as $field_name => $field_value) {
149
+			if (! isset($model_fields[ $field_name ])) {
150
+				throw new EE_Error(
151
+					sprintf(
152
+						esc_html__(
153
+							'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s',
154
+							'event_espresso'
155
+						),
156
+						$field_name,
157
+						get_class($this),
158
+						implode(', ', array_keys($model_fields))
159
+					)
160
+				);
161
+			}
162
+		}
163
+		$this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
164
+		if (! empty($date_formats) && is_array($date_formats)) {
165
+			list($this->_dt_frmt, $this->_tm_frmt) = $date_formats;
166
+		} else {
167
+			// set default formats for date and time
168
+			$this->_dt_frmt = (string) get_option('date_format', 'Y-m-d');
169
+			$this->_tm_frmt = (string) get_option('time_format', 'g:i a');
170
+		}
171
+		// if db model is instantiating
172
+		if ($bydb) {
173
+			// client code has indicated these field values are from the database
174
+			foreach ($model_fields as $fieldName => $field) {
175
+				$this->set_from_db(
176
+					$fieldName,
177
+					isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null
178
+				);
179
+			}
180
+		} else {
181
+			// we're constructing a brand
182
+			// new instance of the model object. Generally, this means we'll need to do more field validation
183
+			foreach ($model_fields as $fieldName => $field) {
184
+				$this->set(
185
+					$fieldName,
186
+					isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null,
187
+					true
188
+				);
189
+			}
190
+		}
191
+		// remember what values were passed to this constructor
192
+		$this->_props_n_values_provided_in_constructor = $fieldValues;
193
+		// remember in entity mapper
194
+		if (! $bydb && $model->has_primary_key_field() && $this->ID()) {
195
+			$model->add_to_entity_map($this);
196
+		}
197
+		// setup all the relations
198
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
199
+			if ($relation_obj instanceof EE_Belongs_To_Relation) {
200
+				$this->_model_relations[ $relation_name ] = null;
201
+			} else {
202
+				$this->_model_relations[ $relation_name ] = array();
203
+			}
204
+		}
205
+		/**
206
+		 * Action done at the end of each model object construction
207
+		 *
208
+		 * @param EE_Base_Class $this the model object just created
209
+		 */
210
+		do_action('AHEE__EE_Base_Class__construct__finished', $this);
211
+	}
212
+
213
+
214
+	/**
215
+	 * Gets whether or not this model object is allowed to persist/be saved to the database.
216
+	 *
217
+	 * @return boolean
218
+	 */
219
+	public function allow_persist()
220
+	{
221
+		return $this->_allow_persist;
222
+	}
223
+
224
+
225
+	/**
226
+	 * Sets whether or not this model object should be allowed to be saved to the DB.
227
+	 * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless
228
+	 * you got new information that somehow made you change your mind.
229
+	 *
230
+	 * @param boolean $allow_persist
231
+	 * @return boolean
232
+	 */
233
+	public function set_allow_persist($allow_persist)
234
+	{
235
+		return $this->_allow_persist = $allow_persist;
236
+	}
237
+
238
+
239
+	/**
240
+	 * Gets the field's original value when this object was constructed during this request.
241
+	 * This can be helpful when determining if a model object has changed or not
242
+	 *
243
+	 * @param string $field_name
244
+	 * @return mixed|null
245
+	 * @throws ReflectionException
246
+	 * @throws InvalidArgumentException
247
+	 * @throws InvalidInterfaceException
248
+	 * @throws InvalidDataTypeException
249
+	 * @throws EE_Error
250
+	 */
251
+	public function get_original($field_name)
252
+	{
253
+		if (isset($this->_props_n_values_provided_in_constructor[ $field_name ])
254
+			&& $field_settings = $this->get_model()->field_settings_for($field_name)
255
+		) {
256
+			return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]);
257
+		}
258
+		return null;
259
+	}
260
+
261
+
262
+	/**
263
+	 * @param EE_Base_Class $obj
264
+	 * @return string
265
+	 */
266
+	public function get_class($obj)
267
+	{
268
+		return get_class($obj);
269
+	}
270
+
271
+
272
+	/**
273
+	 * Overrides parent because parent expects old models.
274
+	 * This also doesn't do any validation, and won't work for serialized arrays
275
+	 *
276
+	 * @param    string $field_name
277
+	 * @param    mixed  $field_value
278
+	 * @param bool      $use_default
279
+	 * @throws InvalidArgumentException
280
+	 * @throws InvalidInterfaceException
281
+	 * @throws InvalidDataTypeException
282
+	 * @throws EE_Error
283
+	 * @throws ReflectionException
284
+	 * @throws ReflectionException
285
+	 * @throws ReflectionException
286
+	 */
287
+	public function set($field_name, $field_value, $use_default = false)
288
+	{
289
+		// if not using default and nothing has changed, and object has already been setup (has ID),
290
+		// then don't do anything
291
+		if (! $use_default
292
+			&& $this->_fields[ $field_name ] === $field_value
293
+			&& $this->ID()
294
+		) {
295
+			return;
296
+		}
297
+		$model = $this->get_model();
298
+		$this->_has_changes = true;
299
+		$field_obj = $model->field_settings_for($field_name);
300
+		if ($field_obj instanceof EE_Model_Field_Base) {
301
+			// if ( method_exists( $field_obj, 'set_timezone' )) {
302
+			if ($field_obj instanceof EE_Datetime_Field) {
303
+				$field_obj->set_timezone($this->_timezone);
304
+				$field_obj->set_date_format($this->_dt_frmt);
305
+				$field_obj->set_time_format($this->_tm_frmt);
306
+			}
307
+			$holder_of_value = $field_obj->prepare_for_set($field_value);
308
+			// should the value be null?
309
+			if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) {
310
+				$this->_fields[ $field_name ] = $field_obj->get_default_value();
311
+				/**
312
+				 * To save having to refactor all the models, if a default value is used for a
313
+				 * EE_Datetime_Field, and that value is not null nor is it a DateTime
314
+				 * object.  Then let's do a set again to ensure that it becomes a DateTime
315
+				 * object.
316
+				 *
317
+				 * @since 4.6.10+
318
+				 */
319
+				if ($field_obj instanceof EE_Datetime_Field
320
+					&& $this->_fields[ $field_name ] !== null
321
+					&& ! $this->_fields[ $field_name ] instanceof DateTime
322
+				) {
323
+					empty($this->_fields[ $field_name ])
324
+						? $this->set($field_name, time())
325
+						: $this->set($field_name, $this->_fields[ $field_name ]);
326
+				}
327
+			} else {
328
+				$this->_fields[ $field_name ] = $holder_of_value;
329
+			}
330
+			// if we're not in the constructor...
331
+			// now check if what we set was a primary key
332
+			if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor
333
+				$this->_props_n_values_provided_in_constructor
334
+				&& $field_value
335
+				&& $field_name === $model->primary_key_name()
336
+			) {
337
+				// if so, we want all this object's fields to be filled either with
338
+				// what we've explicitly set on this model
339
+				// or what we have in the db
340
+				// echo "setting primary key!";
341
+				$fields_on_model = self::_get_model(get_class($this))->field_settings();
342
+				$obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value);
343
+				foreach ($fields_on_model as $field_obj) {
344
+					if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor)
345
+						&& $field_obj->get_name() !== $field_name
346
+					) {
347
+						$this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name()));
348
+					}
349
+				}
350
+				// oh this model object has an ID? well make sure its in the entity mapper
351
+				$model->add_to_entity_map($this);
352
+			}
353
+			// let's unset any cache for this field_name from the $_cached_properties property.
354
+			$this->_clear_cached_property($field_name);
355
+		} else {
356
+			throw new EE_Error(
357
+				sprintf(
358
+					esc_html__(
359
+						'A valid EE_Model_Field_Base could not be found for the given field name: %s',
360
+						'event_espresso'
361
+					),
362
+					$field_name
363
+				)
364
+			);
365
+		}
366
+	}
367
+
368
+
369
+	/**
370
+	 * Set custom select values for model.
371
+	 *
372
+	 * @param array $custom_select_values
373
+	 */
374
+	public function setCustomSelectsValues(array $custom_select_values)
375
+	{
376
+		$this->custom_selection_results = $custom_select_values;
377
+	}
378
+
379
+
380
+	/**
381
+	 * Returns the custom select value for the provided alias if its set.
382
+	 * If not set, returns null.
383
+	 *
384
+	 * @param string $alias
385
+	 * @return string|int|float|null
386
+	 */
387
+	public function getCustomSelect($alias)
388
+	{
389
+		return isset($this->custom_selection_results[ $alias ])
390
+			? $this->custom_selection_results[ $alias ]
391
+			: null;
392
+	}
393
+
394
+
395
+	/**
396
+	 * This sets the field value on the db column if it exists for the given $column_name or
397
+	 * saves it to EE_Extra_Meta if the given $column_name does not match a db column.
398
+	 *
399
+	 * @see EE_message::get_column_value for related documentation on the necessity of this method.
400
+	 * @param string $field_name  Must be the exact column name.
401
+	 * @param mixed  $field_value The value to set.
402
+	 * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs.
403
+	 * @throws InvalidArgumentException
404
+	 * @throws InvalidInterfaceException
405
+	 * @throws InvalidDataTypeException
406
+	 * @throws EE_Error
407
+	 * @throws ReflectionException
408
+	 */
409
+	public function set_field_or_extra_meta($field_name, $field_value)
410
+	{
411
+		if ($this->get_model()->has_field($field_name)) {
412
+			$this->set($field_name, $field_value);
413
+			return true;
414
+		}
415
+		// ensure this object is saved first so that extra meta can be properly related.
416
+		$this->save();
417
+		return $this->update_extra_meta($field_name, $field_value);
418
+	}
419
+
420
+
421
+	/**
422
+	 * This retrieves the value of the db column set on this class or if that's not present
423
+	 * it will attempt to retrieve from extra_meta if found.
424
+	 * Example Usage:
425
+	 * Via EE_Message child class:
426
+	 * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to",
427
+	 * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may
428
+	 * also have additional main fields specific to the messenger.  The system accommodates those extra
429
+	 * fields through the EE_Extra_Meta table.  This method allows for EE_messengers to retrieve the
430
+	 * value for those extra fields dynamically via the EE_message object.
431
+	 *
432
+	 * @param  string $field_name expecting the fully qualified field name.
433
+	 * @return mixed|null  value for the field if found.  null if not found.
434
+	 * @throws ReflectionException
435
+	 * @throws InvalidArgumentException
436
+	 * @throws InvalidInterfaceException
437
+	 * @throws InvalidDataTypeException
438
+	 * @throws EE_Error
439
+	 */
440
+	public function get_field_or_extra_meta($field_name)
441
+	{
442
+		if ($this->get_model()->has_field($field_name)) {
443
+			$column_value = $this->get($field_name);
444
+		} else {
445
+			// This isn't a column in the main table, let's see if it is in the extra meta.
446
+			$column_value = $this->get_extra_meta($field_name, true, null);
447
+		}
448
+		return $column_value;
449
+	}
450
+
451
+
452
+	/**
453
+	 * See $_timezone property for description of what the timezone property is for.  This SETS the timezone internally
454
+	 * for being able to reference what timezone we are running conversions on when converting TO the internal timezone
455
+	 * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is
456
+	 * available to all child classes that may be using the EE_Datetime_Field for a field data type.
457
+	 *
458
+	 * @access public
459
+	 * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php
460
+	 * @return void
461
+	 * @throws InvalidArgumentException
462
+	 * @throws InvalidInterfaceException
463
+	 * @throws InvalidDataTypeException
464
+	 * @throws EE_Error
465
+	 * @throws ReflectionException
466
+	 */
467
+	public function set_timezone($timezone = '')
468
+	{
469
+		$this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
470
+		// make sure we clear all cached properties because they won't be relevant now
471
+		$this->_clear_cached_properties();
472
+		// make sure we update field settings and the date for all EE_Datetime_Fields
473
+		$model_fields = $this->get_model()->field_settings(false);
474
+		foreach ($model_fields as $field_name => $field_obj) {
475
+			if ($field_obj instanceof EE_Datetime_Field) {
476
+				$field_obj->set_timezone($this->_timezone);
477
+				if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) {
478
+					EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone));
479
+				}
480
+			}
481
+		}
482
+	}
483
+
484
+
485
+	/**
486
+	 * This just returns whatever is set for the current timezone.
487
+	 *
488
+	 * @access public
489
+	 * @return string timezone string
490
+	 */
491
+	public function get_timezone()
492
+	{
493
+		return $this->_timezone;
494
+	}
495
+
496
+
497
+	/**
498
+	 * This sets the internal date format to what is sent in to be used as the new default for the class
499
+	 * internally instead of wp set date format options
500
+	 *
501
+	 * @since 4.6
502
+	 * @param string $format should be a format recognizable by PHP date() functions.
503
+	 */
504
+	public function set_date_format($format)
505
+	{
506
+		$this->_dt_frmt = $format;
507
+		// clear cached_properties because they won't be relevant now.
508
+		$this->_clear_cached_properties();
509
+	}
510
+
511
+
512
+	/**
513
+	 * This sets the internal time format string to what is sent in to be used as the new default for the
514
+	 * class internally instead of wp set time format options.
515
+	 *
516
+	 * @since 4.6
517
+	 * @param string $format should be a format recognizable by PHP date() functions.
518
+	 */
519
+	public function set_time_format($format)
520
+	{
521
+		$this->_tm_frmt = $format;
522
+		// clear cached_properties because they won't be relevant now.
523
+		$this->_clear_cached_properties();
524
+	}
525
+
526
+
527
+	/**
528
+	 * This returns the current internal set format for the date and time formats.
529
+	 *
530
+	 * @param bool $full           if true (default), then return the full format.  Otherwise will return an array
531
+	 *                             where the first value is the date format and the second value is the time format.
532
+	 * @return mixed string|array
533
+	 */
534
+	public function get_format($full = true)
535
+	{
536
+		return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
537
+	}
538
+
539
+
540
+	/**
541
+	 * cache
542
+	 * stores the passed model object on the current model object.
543
+	 * In certain circumstances, we can use this cached model object instead of querying for another one entirely.
544
+	 *
545
+	 * @param string        $relationName    one of the keys in the _model_relations array on the model. Eg
546
+	 *                                       'Registration' associated with this model object
547
+	 * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction,
548
+	 *                                       that could be a payment or a registration)
549
+	 * @param null          $cache_id        a string or number that will be used as the key for any Belongs_To_Many
550
+	 *                                       items which will be stored in an array on this object
551
+	 * @throws ReflectionException
552
+	 * @throws InvalidArgumentException
553
+	 * @throws InvalidInterfaceException
554
+	 * @throws InvalidDataTypeException
555
+	 * @throws EE_Error
556
+	 * @return mixed    index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one
557
+	 *                                       related thing, no array)
558
+	 */
559
+	public function cache($relationName = '', $object_to_cache = null, $cache_id = null)
560
+	{
561
+		// its entirely possible that there IS no related object yet in which case there is nothing to cache.
562
+		if (! $object_to_cache instanceof EE_Base_Class) {
563
+			return false;
564
+		}
565
+		// also get "how" the object is related, or throw an error
566
+		if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) {
567
+			throw new EE_Error(
568
+				sprintf(
569
+					esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'),
570
+					$relationName,
571
+					get_class($this)
572
+				)
573
+			);
574
+		}
575
+		// how many things are related ?
576
+		if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
577
+			// if it's a "belongs to" relationship, then there's only one related model object
578
+			// eg, if this is a registration, there's only 1 attendee for it
579
+			// so for these model objects just set it to be cached
580
+			$this->_model_relations[ $relationName ] = $object_to_cache;
581
+			$return = true;
582
+		} else {
583
+			// otherwise, this is the "many" side of a one to many relationship,
584
+			// so we'll add the object to the array of related objects for that type.
585
+			// eg: if this is an event, there are many registrations for that event,
586
+			// so we cache the registrations in an array
587
+			if (! is_array($this->_model_relations[ $relationName ])) {
588
+				// if for some reason, the cached item is a model object,
589
+				// then stick that in the array, otherwise start with an empty array
590
+				$this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ]
591
+														   instanceof
592
+														   EE_Base_Class
593
+					? array($this->_model_relations[ $relationName ]) : array();
594
+			}
595
+			// first check for a cache_id which is normally empty
596
+			if (! empty($cache_id)) {
597
+				// if the cache_id exists, then it means we are purposely trying to cache this
598
+				// with a known key that can then be used to retrieve the object later on
599
+				$this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache;
600
+				$return = $cache_id;
601
+			} elseif ($object_to_cache->ID()) {
602
+				// OR the cached object originally came from the db, so let's just use it's PK for an ID
603
+				$this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache;
604
+				$return = $object_to_cache->ID();
605
+			} else {
606
+				// OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID
607
+				$this->_model_relations[ $relationName ][] = $object_to_cache;
608
+				// move the internal pointer to the end of the array
609
+				end($this->_model_relations[ $relationName ]);
610
+				// and grab the key so that we can return it
611
+				$return = key($this->_model_relations[ $relationName ]);
612
+			}
613
+		}
614
+		return $return;
615
+	}
616
+
617
+
618
+	/**
619
+	 * For adding an item to the cached_properties property.
620
+	 *
621
+	 * @access protected
622
+	 * @param string      $fieldname the property item the corresponding value is for.
623
+	 * @param mixed       $value     The value we are caching.
624
+	 * @param string|null $cache_type
625
+	 * @return void
626
+	 * @throws ReflectionException
627
+	 * @throws InvalidArgumentException
628
+	 * @throws InvalidInterfaceException
629
+	 * @throws InvalidDataTypeException
630
+	 * @throws EE_Error
631
+	 */
632
+	protected function _set_cached_property($fieldname, $value, $cache_type = null)
633
+	{
634
+		// first make sure this property exists
635
+		$this->get_model()->field_settings_for($fieldname);
636
+		$cache_type = empty($cache_type) ? 'standard' : $cache_type;
637
+		$this->_cached_properties[ $fieldname ][ $cache_type ] = $value;
638
+	}
639
+
640
+
641
+	/**
642
+	 * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist.
643
+	 * This also SETS the cache if we return the actual property!
644
+	 *
645
+	 * @param string $fieldname        the name of the property we're trying to retrieve
646
+	 * @param bool   $pretty
647
+	 * @param string $extra_cache_ref  This allows the user to specify an extra cache ref for the given property
648
+	 *                                 (in cases where the same property may be used for different outputs
649
+	 *                                 - i.e. datetime, money etc.)
650
+	 *                                 It can also accept certain pre-defined "schema" strings
651
+	 *                                 to define how to output the property.
652
+	 *                                 see the field's prepare_for_pretty_echoing for what strings can be used
653
+	 * @return mixed                   whatever the value for the property is we're retrieving
654
+	 * @throws ReflectionException
655
+	 * @throws InvalidArgumentException
656
+	 * @throws InvalidInterfaceException
657
+	 * @throws InvalidDataTypeException
658
+	 * @throws EE_Error
659
+	 */
660
+	protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null)
661
+	{
662
+		// verify the field exists
663
+		$model = $this->get_model();
664
+		$model->field_settings_for($fieldname);
665
+		$cache_type = $pretty ? 'pretty' : 'standard';
666
+		$cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : '';
667
+		if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) {
668
+			return $this->_cached_properties[ $fieldname ][ $cache_type ];
669
+		}
670
+		$value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref);
671
+		$this->_set_cached_property($fieldname, $value, $cache_type);
672
+		return $value;
673
+	}
674
+
675
+
676
+	/**
677
+	 * If the cache didn't fetch the needed item, this fetches it.
678
+	 *
679
+	 * @param string $fieldname
680
+	 * @param bool   $pretty
681
+	 * @param string $extra_cache_ref
682
+	 * @return mixed
683
+	 * @throws InvalidArgumentException
684
+	 * @throws InvalidInterfaceException
685
+	 * @throws InvalidDataTypeException
686
+	 * @throws EE_Error
687
+	 * @throws ReflectionException
688
+	 */
689
+	protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null)
690
+	{
691
+		$field_obj = $this->get_model()->field_settings_for($fieldname);
692
+		// If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct
693
+		if ($field_obj instanceof EE_Datetime_Field) {
694
+			$this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref);
695
+		}
696
+		if (! isset($this->_fields[ $fieldname ])) {
697
+			$this->_fields[ $fieldname ] = null;
698
+		}
699
+		$value = $pretty
700
+			? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref)
701
+			: $field_obj->prepare_for_get($this->_fields[ $fieldname ]);
702
+		return $value;
703
+	}
704
+
705
+
706
+	/**
707
+	 * set timezone, formats, and output for EE_Datetime_Field objects
708
+	 *
709
+	 * @param \EE_Datetime_Field $datetime_field
710
+	 * @param bool               $pretty
711
+	 * @param null               $date_or_time
712
+	 * @return void
713
+	 * @throws InvalidArgumentException
714
+	 * @throws InvalidInterfaceException
715
+	 * @throws InvalidDataTypeException
716
+	 * @throws EE_Error
717
+	 */
718
+	protected function _prepare_datetime_field(
719
+		EE_Datetime_Field $datetime_field,
720
+		$pretty = false,
721
+		$date_or_time = null
722
+	) {
723
+		$datetime_field->set_timezone($this->_timezone);
724
+		$datetime_field->set_date_format($this->_dt_frmt, $pretty);
725
+		$datetime_field->set_time_format($this->_tm_frmt, $pretty);
726
+		// set the output returned
727
+		switch ($date_or_time) {
728
+			case 'D':
729
+				$datetime_field->set_date_time_output('date');
730
+				break;
731
+			case 'T':
732
+				$datetime_field->set_date_time_output('time');
733
+				break;
734
+			default:
735
+				$datetime_field->set_date_time_output();
736
+		}
737
+	}
738
+
739
+
740
+	/**
741
+	 * This just takes care of clearing out the cached_properties
742
+	 *
743
+	 * @return void
744
+	 */
745
+	protected function _clear_cached_properties()
746
+	{
747
+		$this->_cached_properties = array();
748
+	}
749
+
750
+
751
+	/**
752
+	 * This just clears out ONE property if it exists in the cache
753
+	 *
754
+	 * @param  string $property_name the property to remove if it exists (from the _cached_properties array)
755
+	 * @return void
756
+	 */
757
+	protected function _clear_cached_property($property_name)
758
+	{
759
+		if (isset($this->_cached_properties[ $property_name ])) {
760
+			unset($this->_cached_properties[ $property_name ]);
761
+		}
762
+	}
763
+
764
+
765
+	/**
766
+	 * Ensures that this related thing is a model object.
767
+	 *
768
+	 * @param mixed  $object_or_id EE_base_Class/int/string either a related model object, or its ID
769
+	 * @param string $model_name   name of the related thing, eg 'Attendee',
770
+	 * @return EE_Base_Class
771
+	 * @throws ReflectionException
772
+	 * @throws InvalidArgumentException
773
+	 * @throws InvalidInterfaceException
774
+	 * @throws InvalidDataTypeException
775
+	 * @throws EE_Error
776
+	 */
777
+	protected function ensure_related_thing_is_model_obj($object_or_id, $model_name)
778
+	{
779
+		$other_model_instance = self::_get_model_instance_with_name(
780
+			self::_get_model_classname($model_name),
781
+			$this->_timezone
782
+		);
783
+		return $other_model_instance->ensure_is_obj($object_or_id);
784
+	}
785
+
786
+
787
+	/**
788
+	 * Forgets the cached model of the given relation Name. So the next time we request it,
789
+	 * we will fetch it again from the database. (Handy if you know it's changed somehow).
790
+	 * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM,
791
+	 * then only remove that one object from our cached array. Otherwise, clear the entire list
792
+	 *
793
+	 * @param string $relationName                         one of the keys in the _model_relations array on the model.
794
+	 *                                                     Eg 'Registration'
795
+	 * @param mixed  $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL
796
+	 *                                                     if you intend to use $clear_all = TRUE, or the relation only
797
+	 *                                                     has 1 object anyways (ie, it's a BelongsToRelation)
798
+	 * @param bool   $clear_all                            This flags clearing the entire cache relation property if
799
+	 *                                                     this is HasMany or HABTM.
800
+	 * @throws ReflectionException
801
+	 * @throws InvalidArgumentException
802
+	 * @throws InvalidInterfaceException
803
+	 * @throws InvalidDataTypeException
804
+	 * @throws EE_Error
805
+	 * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a
806
+	 *                                                     relation from all
807
+	 */
808
+	public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false)
809
+	{
810
+		$relationship_to_model = $this->get_model()->related_settings_for($relationName);
811
+		$index_in_cache = '';
812
+		if (! $relationship_to_model) {
813
+			throw new EE_Error(
814
+				sprintf(
815
+					esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'),
816
+					$relationName,
817
+					get_class($this)
818
+				)
819
+			);
820
+		}
821
+		if ($clear_all) {
822
+			$obj_removed = true;
823
+			$this->_model_relations[ $relationName ] = null;
824
+		} elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) {
825
+			$obj_removed = $this->_model_relations[ $relationName ];
826
+			$this->_model_relations[ $relationName ] = null;
827
+		} else {
828
+			if ($object_to_remove_or_index_into_array instanceof EE_Base_Class
829
+				&& $object_to_remove_or_index_into_array->ID()
830
+			) {
831
+				$index_in_cache = $object_to_remove_or_index_into_array->ID();
832
+				if (is_array($this->_model_relations[ $relationName ])
833
+					&& ! isset($this->_model_relations[ $relationName ][ $index_in_cache ])
834
+				) {
835
+					$index_found_at = null;
836
+					// find this object in the array even though it has a different key
837
+					foreach ($this->_model_relations[ $relationName ] as $index => $obj) {
838
+						/** @noinspection TypeUnsafeComparisonInspection */
839
+						if ($obj instanceof EE_Base_Class
840
+							&& (
841
+								$obj == $object_to_remove_or_index_into_array
842
+								|| $obj->ID() === $object_to_remove_or_index_into_array->ID()
843
+							)
844
+						) {
845
+							$index_found_at = $index;
846
+							break;
847
+						}
848
+					}
849
+					if ($index_found_at) {
850
+						$index_in_cache = $index_found_at;
851
+					} else {
852
+						// it wasn't found. huh. well obviously it doesn't need to be removed from teh cache
853
+						// if it wasn't in it to begin with. So we're done
854
+						return $object_to_remove_or_index_into_array;
855
+					}
856
+				}
857
+			} elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) {
858
+				// so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it!
859
+				foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) {
860
+					/** @noinspection TypeUnsafeComparisonInspection */
861
+					if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) {
862
+						$index_in_cache = $index;
863
+					}
864
+				}
865
+			} else {
866
+				$index_in_cache = $object_to_remove_or_index_into_array;
867
+			}
868
+			// supposedly we've found it. But it could just be that the client code
869
+			// provided a bad index/object
870
+			if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) {
871
+				$obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ];
872
+				unset($this->_model_relations[ $relationName ][ $index_in_cache ]);
873
+			} else {
874
+				// that thing was never cached anyways.
875
+				$obj_removed = null;
876
+			}
877
+		}
878
+		return $obj_removed;
879
+	}
880
+
881
+
882
+	/**
883
+	 * update_cache_after_object_save
884
+	 * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has
885
+	 * obtained after being saved to the db
886
+	 *
887
+	 * @param string        $relationName       - the type of object that is cached
888
+	 * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached
889
+	 * @param string        $current_cache_id   - the ID that was used when originally caching the object
890
+	 * @return boolean TRUE on success, FALSE on fail
891
+	 * @throws ReflectionException
892
+	 * @throws InvalidArgumentException
893
+	 * @throws InvalidInterfaceException
894
+	 * @throws InvalidDataTypeException
895
+	 * @throws EE_Error
896
+	 */
897
+	public function update_cache_after_object_save(
898
+		$relationName,
899
+		EE_Base_Class $newly_saved_object,
900
+		$current_cache_id = ''
901
+	) {
902
+		// verify that incoming object is of the correct type
903
+		$obj_class = 'EE_' . $relationName;
904
+		if ($newly_saved_object instanceof $obj_class) {
905
+			/* @type EE_Base_Class $newly_saved_object */
906
+			// now get the type of relation
907
+			$relationship_to_model = $this->get_model()->related_settings_for($relationName);
908
+			// if this is a 1:1 relationship
909
+			if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
910
+				// then just replace the cached object with the newly saved object
911
+				$this->_model_relations[ $relationName ] = $newly_saved_object;
912
+				return true;
913
+				// or if it's some kind of sordid feral polyamorous relationship...
914
+			}
915
+			if (is_array($this->_model_relations[ $relationName ])
916
+				&& isset($this->_model_relations[ $relationName ][ $current_cache_id ])
917
+			) {
918
+				// then remove the current cached item
919
+				unset($this->_model_relations[ $relationName ][ $current_cache_id ]);
920
+				// and cache the newly saved object using it's new ID
921
+				$this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object;
922
+				return true;
923
+			}
924
+		}
925
+		return false;
926
+	}
927
+
928
+
929
+	/**
930
+	 * Fetches a single EE_Base_Class on that relation. (If the relation is of type
931
+	 * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
932
+	 *
933
+	 * @param string $relationName
934
+	 * @return EE_Base_Class
935
+	 */
936
+	public function get_one_from_cache($relationName)
937
+	{
938
+		$cached_array_or_object = isset($this->_model_relations[ $relationName ])
939
+			? $this->_model_relations[ $relationName ]
940
+			: null;
941
+		if (is_array($cached_array_or_object)) {
942
+			return array_shift($cached_array_or_object);
943
+		}
944
+		return $cached_array_or_object;
945
+	}
946
+
947
+
948
+	/**
949
+	 * Fetches a single EE_Base_Class on that relation. (If the relation is of type
950
+	 * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
951
+	 *
952
+	 * @param string $relationName
953
+	 * @throws ReflectionException
954
+	 * @throws InvalidArgumentException
955
+	 * @throws InvalidInterfaceException
956
+	 * @throws InvalidDataTypeException
957
+	 * @throws EE_Error
958
+	 * @return EE_Base_Class[] NOT necessarily indexed by primary keys
959
+	 */
960
+	public function get_all_from_cache($relationName)
961
+	{
962
+		$objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array();
963
+		// if the result is not an array, but exists, make it an array
964
+		$objects = is_array($objects) ? $objects : array($objects);
965
+		// bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143
966
+		// basically, if this model object was stored in the session, and these cached model objects
967
+		// already have IDs, let's make sure they're in their model's entity mapper
968
+		// otherwise we will have duplicates next time we call
969
+		// EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() );
970
+		$model = EE_Registry::instance()->load_model($relationName);
971
+		foreach ($objects as $model_object) {
972
+			if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) {
973
+				// ensure its in the map if it has an ID; otherwise it will be added to the map when its saved
974
+				if ($model_object->ID()) {
975
+					$model->add_to_entity_map($model_object);
976
+				}
977
+			} else {
978
+				throw new EE_Error(
979
+					sprintf(
980
+						esc_html__(
981
+							'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object',
982
+							'event_espresso'
983
+						),
984
+						$relationName,
985
+						gettype($model_object)
986
+					)
987
+				);
988
+			}
989
+		}
990
+		return $objects;
991
+	}
992
+
993
+
994
+	/**
995
+	 * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database
996
+	 * matching the given query conditions.
997
+	 *
998
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
999
+	 * @param int   $limit              How many objects to return.
1000
+	 * @param array $query_params       Any additional conditions on the query.
1001
+	 * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
1002
+	 *                                  you can indicate just the columns you want returned
1003
+	 * @return array|EE_Base_Class[]
1004
+	 * @throws ReflectionException
1005
+	 * @throws InvalidArgumentException
1006
+	 * @throws InvalidInterfaceException
1007
+	 * @throws InvalidDataTypeException
1008
+	 * @throws EE_Error
1009
+	 */
1010
+	public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null)
1011
+	{
1012
+		$model = $this->get_model();
1013
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
1014
+			? $model->get_primary_key_field()->get_name()
1015
+			: $field_to_order_by;
1016
+		$current_value = ! empty($field) ? $this->get($field) : null;
1017
+		if (empty($field) || empty($current_value)) {
1018
+			return array();
1019
+		}
1020
+		return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select);
1021
+	}
1022
+
1023
+
1024
+	/**
1025
+	 * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database
1026
+	 * matching the given query conditions.
1027
+	 *
1028
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
1029
+	 * @param int   $limit              How many objects to return.
1030
+	 * @param array $query_params       Any additional conditions on the query.
1031
+	 * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
1032
+	 *                                  you can indicate just the columns you want returned
1033
+	 * @return array|EE_Base_Class[]
1034
+	 * @throws ReflectionException
1035
+	 * @throws InvalidArgumentException
1036
+	 * @throws InvalidInterfaceException
1037
+	 * @throws InvalidDataTypeException
1038
+	 * @throws EE_Error
1039
+	 */
1040
+	public function previous_x(
1041
+		$field_to_order_by = null,
1042
+		$limit = 1,
1043
+		$query_params = array(),
1044
+		$columns_to_select = null
1045
+	) {
1046
+		$model = $this->get_model();
1047
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
1048
+			? $model->get_primary_key_field()->get_name()
1049
+			: $field_to_order_by;
1050
+		$current_value = ! empty($field) ? $this->get($field) : null;
1051
+		if (empty($field) || empty($current_value)) {
1052
+			return array();
1053
+		}
1054
+		return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select);
1055
+	}
1056
+
1057
+
1058
+	/**
1059
+	 * Returns the next EE_Base_Class object in sequence from this object as found in the database
1060
+	 * matching the given query conditions.
1061
+	 *
1062
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
1063
+	 * @param array $query_params       Any additional conditions on the query.
1064
+	 * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
1065
+	 *                                  you can indicate just the columns you want returned
1066
+	 * @return array|EE_Base_Class
1067
+	 * @throws ReflectionException
1068
+	 * @throws InvalidArgumentException
1069
+	 * @throws InvalidInterfaceException
1070
+	 * @throws InvalidDataTypeException
1071
+	 * @throws EE_Error
1072
+	 */
1073
+	public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
1074
+	{
1075
+		$model = $this->get_model();
1076
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
1077
+			? $model->get_primary_key_field()->get_name()
1078
+			: $field_to_order_by;
1079
+		$current_value = ! empty($field) ? $this->get($field) : null;
1080
+		if (empty($field) || empty($current_value)) {
1081
+			return array();
1082
+		}
1083
+		return $model->next($current_value, $field, $query_params, $columns_to_select);
1084
+	}
1085
+
1086
+
1087
+	/**
1088
+	 * Returns the previous EE_Base_Class object in sequence from this object as found in the database
1089
+	 * matching the given query conditions.
1090
+	 *
1091
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
1092
+	 * @param array $query_params       Any additional conditions on the query.
1093
+	 * @param null  $columns_to_select  If left null, then an EE_Base_Class object is returned, otherwise
1094
+	 *                                  you can indicate just the column you want returned
1095
+	 * @return array|EE_Base_Class
1096
+	 * @throws ReflectionException
1097
+	 * @throws InvalidArgumentException
1098
+	 * @throws InvalidInterfaceException
1099
+	 * @throws InvalidDataTypeException
1100
+	 * @throws EE_Error
1101
+	 */
1102
+	public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
1103
+	{
1104
+		$model = $this->get_model();
1105
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
1106
+			? $model->get_primary_key_field()->get_name()
1107
+			: $field_to_order_by;
1108
+		$current_value = ! empty($field) ? $this->get($field) : null;
1109
+		if (empty($field) || empty($current_value)) {
1110
+			return array();
1111
+		}
1112
+		return $model->previous($current_value, $field, $query_params, $columns_to_select);
1113
+	}
1114
+
1115
+
1116
+	/**
1117
+	 * Overrides parent because parent expects old models.
1118
+	 * This also doesn't do any validation, and won't work for serialized arrays
1119
+	 *
1120
+	 * @param string $field_name
1121
+	 * @param mixed  $field_value_from_db
1122
+	 * @throws ReflectionException
1123
+	 * @throws InvalidArgumentException
1124
+	 * @throws InvalidInterfaceException
1125
+	 * @throws InvalidDataTypeException
1126
+	 * @throws EE_Error
1127
+	 */
1128
+	public function set_from_db($field_name, $field_value_from_db)
1129
+	{
1130
+		$field_obj = $this->get_model()->field_settings_for($field_name);
1131
+		if ($field_obj instanceof EE_Model_Field_Base) {
1132
+			// you would think the DB has no NULLs for non-null label fields right? wrong!
1133
+			// eg, a CPT model object could have an entry in the posts table, but no
1134
+			// entry in the meta table. Meaning that all its columns in the meta table
1135
+			// are null! yikes! so when we find one like that, use defaults for its meta columns
1136
+			if ($field_value_from_db === null) {
1137
+				if ($field_obj->is_nullable()) {
1138
+					// if the field allows nulls, then let it be null
1139
+					$field_value = null;
1140
+				} else {
1141
+					$field_value = $field_obj->get_default_value();
1142
+				}
1143
+			} else {
1144
+				$field_value = $field_obj->prepare_for_set_from_db($field_value_from_db);
1145
+			}
1146
+			$this->_fields[ $field_name ] = $field_value;
1147
+			$this->_clear_cached_property($field_name);
1148
+		}
1149
+	}
1150
+
1151
+
1152
+	/**
1153
+	 * verifies that the specified field is of the correct type
1154
+	 *
1155
+	 * @param string $field_name
1156
+	 * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1157
+	 *                                (in cases where the same property may be used for different outputs
1158
+	 *                                - i.e. datetime, money etc.)
1159
+	 * @return mixed
1160
+	 * @throws ReflectionException
1161
+	 * @throws InvalidArgumentException
1162
+	 * @throws InvalidInterfaceException
1163
+	 * @throws InvalidDataTypeException
1164
+	 * @throws EE_Error
1165
+	 */
1166
+	public function get($field_name, $extra_cache_ref = null)
1167
+	{
1168
+		return $this->_get_cached_property($field_name, false, $extra_cache_ref);
1169
+	}
1170
+
1171
+
1172
+	/**
1173
+	 * This method simply returns the RAW unprocessed value for the given property in this class
1174
+	 *
1175
+	 * @param  string $field_name A valid fieldname
1176
+	 * @return mixed              Whatever the raw value stored on the property is.
1177
+	 * @throws ReflectionException
1178
+	 * @throws InvalidArgumentException
1179
+	 * @throws InvalidInterfaceException
1180
+	 * @throws InvalidDataTypeException
1181
+	 * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist.
1182
+	 */
1183
+	public function get_raw($field_name)
1184
+	{
1185
+		$field_settings = $this->get_model()->field_settings_for($field_name);
1186
+		return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime
1187
+			? $this->_fields[ $field_name ]->format('U')
1188
+			: $this->_fields[ $field_name ];
1189
+	}
1190
+
1191
+
1192
+	/**
1193
+	 * This is used to return the internal DateTime object used for a field that is a
1194
+	 * EE_Datetime_Field.
1195
+	 *
1196
+	 * @param string $field_name               The field name retrieving the DateTime object.
1197
+	 * @return mixed null | false | DateTime  If the requested field is NOT a EE_Datetime_Field then
1198
+	 * @throws EE_Error an error is set and false returned.  If the field IS an
1199
+	 *                                         EE_Datetime_Field and but the field value is null, then
1200
+	 *                                         just null is returned (because that indicates that likely
1201
+	 *                                         this field is nullable).
1202
+	 * @throws InvalidArgumentException
1203
+	 * @throws InvalidDataTypeException
1204
+	 * @throws InvalidInterfaceException
1205
+	 * @throws ReflectionException
1206
+	 */
1207
+	public function get_DateTime_object($field_name)
1208
+	{
1209
+		$field_settings = $this->get_model()->field_settings_for($field_name);
1210
+		if (! $field_settings instanceof EE_Datetime_Field) {
1211
+			EE_Error::add_error(
1212
+				sprintf(
1213
+					esc_html__(
1214
+						'The field %s is not an EE_Datetime_Field field.  There is no DateTime object stored on this field type.',
1215
+						'event_espresso'
1216
+					),
1217
+					$field_name
1218
+				),
1219
+				__FILE__,
1220
+				__FUNCTION__,
1221
+				__LINE__
1222
+			);
1223
+			return false;
1224
+		}
1225
+		return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime
1226
+			? clone $this->_fields[ $field_name ]
1227
+			: null;
1228
+	}
1229
+
1230
+
1231
+	/**
1232
+	 * To be used in template to immediately echo out the value, and format it for output.
1233
+	 * Eg, should call stripslashes and whatnot before echoing
1234
+	 *
1235
+	 * @param string $field_name      the name of the field as it appears in the DB
1236
+	 * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1237
+	 *                                (in cases where the same property may be used for different outputs
1238
+	 *                                - i.e. datetime, money etc.)
1239
+	 * @return void
1240
+	 * @throws ReflectionException
1241
+	 * @throws InvalidArgumentException
1242
+	 * @throws InvalidInterfaceException
1243
+	 * @throws InvalidDataTypeException
1244
+	 * @throws EE_Error
1245
+	 */
1246
+	public function e($field_name, $extra_cache_ref = null)
1247
+	{
1248
+		echo $this->get_pretty($field_name, $extra_cache_ref);
1249
+	}
1250
+
1251
+
1252
+	/**
1253
+	 * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it
1254
+	 * can be easily used as the value of form input.
1255
+	 *
1256
+	 * @param string $field_name
1257
+	 * @return void
1258
+	 * @throws ReflectionException
1259
+	 * @throws InvalidArgumentException
1260
+	 * @throws InvalidInterfaceException
1261
+	 * @throws InvalidDataTypeException
1262
+	 * @throws EE_Error
1263
+	 */
1264
+	public function f($field_name)
1265
+	{
1266
+		$this->e($field_name, 'form_input');
1267
+	}
1268
+
1269
+
1270
+	/**
1271
+	 * Same as `f()` but just returns the value instead of echoing it
1272
+	 *
1273
+	 * @param string $field_name
1274
+	 * @return string
1275
+	 * @throws ReflectionException
1276
+	 * @throws InvalidArgumentException
1277
+	 * @throws InvalidInterfaceException
1278
+	 * @throws InvalidDataTypeException
1279
+	 * @throws EE_Error
1280
+	 */
1281
+	public function get_f($field_name)
1282
+	{
1283
+		return (string) $this->get_pretty($field_name, 'form_input');
1284
+	}
1285
+
1286
+
1287
+	/**
1288
+	 * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this.
1289
+	 * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class
1290
+	 * to see what options are available.
1291
+	 *
1292
+	 * @param string $field_name
1293
+	 * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1294
+	 *                                (in cases where the same property may be used for different outputs
1295
+	 *                                - i.e. datetime, money etc.)
1296
+	 * @return mixed
1297
+	 * @throws ReflectionException
1298
+	 * @throws InvalidArgumentException
1299
+	 * @throws InvalidInterfaceException
1300
+	 * @throws InvalidDataTypeException
1301
+	 * @throws EE_Error
1302
+	 */
1303
+	public function get_pretty($field_name, $extra_cache_ref = null)
1304
+	{
1305
+		return $this->_get_cached_property($field_name, true, $extra_cache_ref);
1306
+	}
1307
+
1308
+
1309
+	/**
1310
+	 * This simply returns the datetime for the given field name
1311
+	 * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions
1312
+	 * (and the equivalent e_date, e_time, e_datetime).
1313
+	 *
1314
+	 * @access   protected
1315
+	 * @param string   $field_name   Field on the instantiated EE_Base_Class child object
1316
+	 * @param string   $dt_frmt      valid datetime format used for date
1317
+	 *                               (if '' then we just use the default on the field,
1318
+	 *                               if NULL we use the last-used format)
1319
+	 * @param string   $tm_frmt      Same as above except this is for time format
1320
+	 * @param string   $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time.
1321
+	 * @param  boolean $echo         Whether the dtt is echoing using pretty echoing or just returned using vanilla get
1322
+	 * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown
1323
+	 *                               if field is not a valid dtt field, or void if echoing
1324
+	 * @throws ReflectionException
1325
+	 * @throws InvalidArgumentException
1326
+	 * @throws InvalidInterfaceException
1327
+	 * @throws InvalidDataTypeException
1328
+	 * @throws EE_Error
1329
+	 */
1330
+	protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false)
1331
+	{
1332
+		// clear cached property
1333
+		$this->_clear_cached_property($field_name);
1334
+		// reset format properties because they are used in get()
1335
+		$this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt;
1336
+		$this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt;
1337
+		if ($echo) {
1338
+			$this->e($field_name, $date_or_time);
1339
+			return '';
1340
+		}
1341
+		return $this->get($field_name, $date_or_time);
1342
+	}
1343
+
1344
+
1345
+	/**
1346
+	 * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date
1347
+	 * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1348
+	 * other echoes the pretty value for dtt)
1349
+	 *
1350
+	 * @param  string $field_name name of model object datetime field holding the value
1351
+	 * @param  string $format     format for the date returned (if NULL we use default in dt_frmt property)
1352
+	 * @return string            datetime value formatted
1353
+	 * @throws ReflectionException
1354
+	 * @throws InvalidArgumentException
1355
+	 * @throws InvalidInterfaceException
1356
+	 * @throws InvalidDataTypeException
1357
+	 * @throws EE_Error
1358
+	 */
1359
+	public function get_date($field_name, $format = '')
1360
+	{
1361
+		return $this->_get_datetime($field_name, $format, null, 'D');
1362
+	}
1363
+
1364
+
1365
+	/**
1366
+	 * @param        $field_name
1367
+	 * @param string $format
1368
+	 * @throws ReflectionException
1369
+	 * @throws InvalidArgumentException
1370
+	 * @throws InvalidInterfaceException
1371
+	 * @throws InvalidDataTypeException
1372
+	 * @throws EE_Error
1373
+	 */
1374
+	public function e_date($field_name, $format = '')
1375
+	{
1376
+		$this->_get_datetime($field_name, $format, null, 'D', true);
1377
+	}
1378
+
1379
+
1380
+	/**
1381
+	 * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time
1382
+	 * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1383
+	 * other echoes the pretty value for dtt)
1384
+	 *
1385
+	 * @param  string $field_name name of model object datetime field holding the value
1386
+	 * @param  string $format     format for the time returned ( if NULL we use default in tm_frmt property)
1387
+	 * @return string             datetime value formatted
1388
+	 * @throws ReflectionException
1389
+	 * @throws InvalidArgumentException
1390
+	 * @throws InvalidInterfaceException
1391
+	 * @throws InvalidDataTypeException
1392
+	 * @throws EE_Error
1393
+	 */
1394
+	public function get_time($field_name, $format = '')
1395
+	{
1396
+		return $this->_get_datetime($field_name, null, $format, 'T');
1397
+	}
1398
+
1399
+
1400
+	/**
1401
+	 * @param        $field_name
1402
+	 * @param string $format
1403
+	 * @throws ReflectionException
1404
+	 * @throws InvalidArgumentException
1405
+	 * @throws InvalidInterfaceException
1406
+	 * @throws InvalidDataTypeException
1407
+	 * @throws EE_Error
1408
+	 */
1409
+	public function e_time($field_name, $format = '')
1410
+	{
1411
+		$this->_get_datetime($field_name, null, $format, 'T', true);
1412
+	}
1413
+
1414
+
1415
+	/**
1416
+	 * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND
1417
+	 * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1418
+	 * other echoes the pretty value for dtt)
1419
+	 *
1420
+	 * @param  string $field_name name of model object datetime field holding the value
1421
+	 * @param  string $dt_frmt    format for the date returned (if NULL we use default in dt_frmt property)
1422
+	 * @param  string $tm_frmt    format for the time returned (if NULL we use default in tm_frmt property)
1423
+	 * @return string             datetime value formatted
1424
+	 * @throws ReflectionException
1425
+	 * @throws InvalidArgumentException
1426
+	 * @throws InvalidInterfaceException
1427
+	 * @throws InvalidDataTypeException
1428
+	 * @throws EE_Error
1429
+	 */
1430
+	public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1431
+	{
1432
+		return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt);
1433
+	}
1434
+
1435
+
1436
+	/**
1437
+	 * @param string $field_name
1438
+	 * @param string $dt_frmt
1439
+	 * @param string $tm_frmt
1440
+	 * @throws ReflectionException
1441
+	 * @throws InvalidArgumentException
1442
+	 * @throws InvalidInterfaceException
1443
+	 * @throws InvalidDataTypeException
1444
+	 * @throws EE_Error
1445
+	 */
1446
+	public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1447
+	{
1448
+		$this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true);
1449
+	}
1450
+
1451
+
1452
+	/**
1453
+	 * Get the i8ln value for a date using the WordPress @see date_i18n function.
1454
+	 *
1455
+	 * @param string $field_name The EE_Datetime_Field reference for the date being retrieved.
1456
+	 * @param string $format     PHP valid date/time string format.  If none is provided then the internal set format
1457
+	 *                           on the object will be used.
1458
+	 * @return string Date and time string in set locale or false if no field exists for the given
1459
+	 * @throws ReflectionException
1460
+	 * @throws InvalidArgumentException
1461
+	 * @throws InvalidInterfaceException
1462
+	 * @throws InvalidDataTypeException
1463
+	 * @throws EE_Error
1464
+	 *                           field name.
1465
+	 */
1466
+	public function get_i18n_datetime($field_name, $format = '')
1467
+	{
1468
+		$format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format;
1469
+		return date_i18n(
1470
+			$format,
1471
+			EEH_DTT_Helper::get_timestamp_with_offset(
1472
+				$this->get_raw($field_name),
1473
+				$this->_timezone
1474
+			)
1475
+		);
1476
+	}
1477
+
1478
+
1479
+	/**
1480
+	 * This method validates whether the given field name is a valid field on the model object as well as it is of a
1481
+	 * type EE_Datetime_Field.  On success there will be returned the field settings.  On fail an EE_Error exception is
1482
+	 * thrown.
1483
+	 *
1484
+	 * @param  string $field_name The field name being checked
1485
+	 * @throws ReflectionException
1486
+	 * @throws InvalidArgumentException
1487
+	 * @throws InvalidInterfaceException
1488
+	 * @throws InvalidDataTypeException
1489
+	 * @throws EE_Error
1490
+	 * @return EE_Datetime_Field
1491
+	 */
1492
+	protected function _get_dtt_field_settings($field_name)
1493
+	{
1494
+		$field = $this->get_model()->field_settings_for($field_name);
1495
+		// check if field is dtt
1496
+		if ($field instanceof EE_Datetime_Field) {
1497
+			return $field;
1498
+		}
1499
+		throw new EE_Error(
1500
+			sprintf(
1501
+				esc_html__(
1502
+					'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field.  Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor',
1503
+					'event_espresso'
1504
+				),
1505
+				$field_name,
1506
+				self::_get_model_classname(get_class($this))
1507
+			)
1508
+		);
1509
+	}
1510
+
1511
+
1512
+
1513
+
1514
+	/**
1515
+	 * NOTE ABOUT BELOW:
1516
+	 * These convenience date and time setters are for setting date and time independently.  In other words you might
1517
+	 * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand
1518
+	 * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value)
1519
+	 * method and make sure you send the entire datetime value for setting.
1520
+	 */
1521
+	/**
1522
+	 * sets the time on a datetime property
1523
+	 *
1524
+	 * @access protected
1525
+	 * @param string|Datetime $time      a valid time string for php datetime functions (or DateTime object)
1526
+	 * @param string          $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field)
1527
+	 * @throws ReflectionException
1528
+	 * @throws InvalidArgumentException
1529
+	 * @throws InvalidInterfaceException
1530
+	 * @throws InvalidDataTypeException
1531
+	 * @throws EE_Error
1532
+	 */
1533
+	protected function _set_time_for($time, $fieldname)
1534
+	{
1535
+		$this->_set_date_time('T', $time, $fieldname);
1536
+	}
1537
+
1538
+
1539
+	/**
1540
+	 * sets the date on a datetime property
1541
+	 *
1542
+	 * @access protected
1543
+	 * @param string|DateTime $date      a valid date string for php datetime functions ( or DateTime object)
1544
+	 * @param string          $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field)
1545
+	 * @throws ReflectionException
1546
+	 * @throws InvalidArgumentException
1547
+	 * @throws InvalidInterfaceException
1548
+	 * @throws InvalidDataTypeException
1549
+	 * @throws EE_Error
1550
+	 */
1551
+	protected function _set_date_for($date, $fieldname)
1552
+	{
1553
+		$this->_set_date_time('D', $date, $fieldname);
1554
+	}
1555
+
1556
+
1557
+	/**
1558
+	 * This takes care of setting a date or time independently on a given model object property. This method also
1559
+	 * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field
1560
+	 *
1561
+	 * @access protected
1562
+	 * @param string          $what           "T" for time, 'B' for both, 'D' for Date.
1563
+	 * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object)
1564
+	 * @param string          $fieldname      the name of the field the date OR time is being set on (must match a
1565
+	 *                                        EE_Datetime_Field property)
1566
+	 * @throws ReflectionException
1567
+	 * @throws InvalidArgumentException
1568
+	 * @throws InvalidInterfaceException
1569
+	 * @throws InvalidDataTypeException
1570
+	 * @throws EE_Error
1571
+	 */
1572
+	protected function _set_date_time($what = 'T', $datetime_value, $fieldname)
1573
+	{
1574
+		$field = $this->_get_dtt_field_settings($fieldname);
1575
+		$field->set_timezone($this->_timezone);
1576
+		$field->set_date_format($this->_dt_frmt);
1577
+		$field->set_time_format($this->_tm_frmt);
1578
+		switch ($what) {
1579
+			case 'T':
1580
+				$this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time(
1581
+					$datetime_value,
1582
+					$this->_fields[ $fieldname ]
1583
+				);
1584
+				break;
1585
+			case 'D':
1586
+				$this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date(
1587
+					$datetime_value,
1588
+					$this->_fields[ $fieldname ]
1589
+				);
1590
+				break;
1591
+			case 'B':
1592
+				$this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value);
1593
+				break;
1594
+		}
1595
+		$this->_clear_cached_property($fieldname);
1596
+	}
1597
+
1598
+
1599
+	/**
1600
+	 * This will return a timestamp for the website timezone but ONLY when the current website timezone is different
1601
+	 * than the timezone set for the website. NOTE, this currently only works well with methods that return values.  If
1602
+	 * you use it with methods that echo values the $_timestamp property may not get reset to its original value and
1603
+	 * that could lead to some unexpected results!
1604
+	 *
1605
+	 * @access public
1606
+	 * @param string $field_name               This is the name of the field on the object that contains the date/time
1607
+	 *                                         value being returned.
1608
+	 * @param string $callback                 must match a valid method in this class (defaults to get_datetime)
1609
+	 * @param mixed (array|string) $args       This is the arguments that will be passed to the callback.
1610
+	 * @param string $prepend                  You can include something to prepend on the timestamp
1611
+	 * @param string $append                   You can include something to append on the timestamp
1612
+	 * @throws ReflectionException
1613
+	 * @throws InvalidArgumentException
1614
+	 * @throws InvalidInterfaceException
1615
+	 * @throws InvalidDataTypeException
1616
+	 * @throws EE_Error
1617
+	 * @return string timestamp
1618
+	 */
1619
+	public function display_in_my_timezone(
1620
+		$field_name,
1621
+		$callback = 'get_datetime',
1622
+		$args = null,
1623
+		$prepend = '',
1624
+		$append = ''
1625
+	) {
1626
+		$timezone = EEH_DTT_Helper::get_timezone();
1627
+		if ($timezone === $this->_timezone) {
1628
+			return '';
1629
+		}
1630
+		$original_timezone = $this->_timezone;
1631
+		$this->set_timezone($timezone);
1632
+		$fn = (array) $field_name;
1633
+		$args = array_merge($fn, (array) $args);
1634
+		if (! method_exists($this, $callback)) {
1635
+			throw new EE_Error(
1636
+				sprintf(
1637
+					esc_html__(
1638
+						'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist.  Please check your spelling',
1639
+						'event_espresso'
1640
+					),
1641
+					$callback
1642
+				)
1643
+			);
1644
+		}
1645
+		$args = (array) $args;
1646
+		$return = $prepend . call_user_func_array(array($this, $callback), $args) . $append;
1647
+		$this->set_timezone($original_timezone);
1648
+		return $return;
1649
+	}
1650
+
1651
+
1652
+	/**
1653
+	 * Deletes this model object.
1654
+	 * This calls the `EE_Base_Class::_delete` method.  Child classes wishing to change default behaviour should
1655
+	 * override
1656
+	 * `EE_Base_Class::_delete` NOT this class.
1657
+	 *
1658
+	 * @return boolean | int
1659
+	 * @throws ReflectionException
1660
+	 * @throws InvalidArgumentException
1661
+	 * @throws InvalidInterfaceException
1662
+	 * @throws InvalidDataTypeException
1663
+	 * @throws EE_Error
1664
+	 */
1665
+	public function delete()
1666
+	{
1667
+		/**
1668
+		 * Called just before the `EE_Base_Class::_delete` method call.
1669
+		 * Note:
1670
+		 * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1671
+		 * should be aware that `_delete` may not always result in a permanent delete.
1672
+		 * For example, `EE_Soft_Delete_Base_Class::_delete`
1673
+		 * soft deletes (trash) the object and does not permanently delete it.
1674
+		 *
1675
+		 * @param EE_Base_Class $model_object about to be 'deleted'
1676
+		 */
1677
+		do_action('AHEE__EE_Base_Class__delete__before', $this);
1678
+		$result = $this->_delete();
1679
+		/**
1680
+		 * Called just after the `EE_Base_Class::_delete` method call.
1681
+		 * Note:
1682
+		 * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1683
+		 * should be aware that `_delete` may not always result in a permanent delete.
1684
+		 * For example `EE_Soft_Base_Class::_delete`
1685
+		 * soft deletes (trash) the object and does not permanently delete it.
1686
+		 *
1687
+		 * @param EE_Base_Class $model_object that was just 'deleted'
1688
+		 * @param boolean       $result
1689
+		 */
1690
+		do_action('AHEE__EE_Base_Class__delete__end', $this, $result);
1691
+		return $result;
1692
+	}
1693
+
1694
+
1695
+	/**
1696
+	 * Calls the specific delete method for the instantiated class.
1697
+	 * This method is called by the public `EE_Base_Class::delete` method.  Any child classes desiring to override
1698
+	 * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT
1699
+	 * `EE_Base_Class::delete`
1700
+	 *
1701
+	 * @return bool|int
1702
+	 * @throws ReflectionException
1703
+	 * @throws InvalidArgumentException
1704
+	 * @throws InvalidInterfaceException
1705
+	 * @throws InvalidDataTypeException
1706
+	 * @throws EE_Error
1707
+	 */
1708
+	protected function _delete()
1709
+	{
1710
+		return $this->delete_permanently();
1711
+	}
1712
+
1713
+
1714
+	/**
1715
+	 * Deletes this model object permanently from db
1716
+	 * (but keep in mind related models may block the delete and return an error)
1717
+	 *
1718
+	 * @return bool | int
1719
+	 * @throws ReflectionException
1720
+	 * @throws InvalidArgumentException
1721
+	 * @throws InvalidInterfaceException
1722
+	 * @throws InvalidDataTypeException
1723
+	 * @throws EE_Error
1724
+	 */
1725
+	public function delete_permanently()
1726
+	{
1727
+		/**
1728
+		 * Called just before HARD deleting a model object
1729
+		 *
1730
+		 * @param EE_Base_Class $model_object about to be 'deleted'
1731
+		 */
1732
+		do_action('AHEE__EE_Base_Class__delete_permanently__before', $this);
1733
+		$model = $this->get_model();
1734
+		$result = $model->delete_permanently_by_ID($this->ID());
1735
+		$this->refresh_cache_of_related_objects();
1736
+		/**
1737
+		 * Called just after HARD deleting a model object
1738
+		 *
1739
+		 * @param EE_Base_Class $model_object that was just 'deleted'
1740
+		 * @param boolean       $result
1741
+		 */
1742
+		do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result);
1743
+		return $result;
1744
+	}
1745
+
1746
+
1747
+	/**
1748
+	 * When this model object is deleted, it may still be cached on related model objects. This clears the cache of
1749
+	 * related model objects
1750
+	 *
1751
+	 * @throws ReflectionException
1752
+	 * @throws InvalidArgumentException
1753
+	 * @throws InvalidInterfaceException
1754
+	 * @throws InvalidDataTypeException
1755
+	 * @throws EE_Error
1756
+	 */
1757
+	public function refresh_cache_of_related_objects()
1758
+	{
1759
+		$model = $this->get_model();
1760
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1761
+			if (! empty($this->_model_relations[ $relation_name ])) {
1762
+				$related_objects = $this->_model_relations[ $relation_name ];
1763
+				if ($relation_obj instanceof EE_Belongs_To_Relation) {
1764
+					// this relation only stores a single model object, not an array
1765
+					// but let's make it consistent
1766
+					$related_objects = array($related_objects);
1767
+				}
1768
+				foreach ($related_objects as $related_object) {
1769
+					// only refresh their cache if they're in memory
1770
+					if ($related_object instanceof EE_Base_Class) {
1771
+						$related_object->clear_cache(
1772
+							$model->get_this_model_name(),
1773
+							$this
1774
+						);
1775
+					}
1776
+				}
1777
+			}
1778
+		}
1779
+	}
1780
+
1781
+
1782
+	/**
1783
+	 *        Saves this object to the database. An array may be supplied to set some values on this
1784
+	 * object just before saving.
1785
+	 *
1786
+	 * @access public
1787
+	 * @param array $set_cols_n_values keys are field names, values are their new values,
1788
+	 *                                 if provided during the save() method (often client code will change the fields'
1789
+	 *                                 values before calling save)
1790
+	 * @throws InvalidArgumentException
1791
+	 * @throws InvalidInterfaceException
1792
+	 * @throws InvalidDataTypeException
1793
+	 * @throws EE_Error
1794
+	 * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object
1795
+	 *                                 isn't allowed to persist (as determined by EE_Base_Class::allow_persist())
1796
+	 * @throws ReflectionException
1797
+	 * @throws ReflectionException
1798
+	 * @throws ReflectionException
1799
+	 */
1800
+	public function save($set_cols_n_values = array())
1801
+	{
1802
+		$model = $this->get_model();
1803
+		/**
1804
+		 * Filters the fields we're about to save on the model object
1805
+		 *
1806
+		 * @param array         $set_cols_n_values
1807
+		 * @param EE_Base_Class $model_object
1808
+		 */
1809
+		$set_cols_n_values = (array) apply_filters(
1810
+			'FHEE__EE_Base_Class__save__set_cols_n_values',
1811
+			$set_cols_n_values,
1812
+			$this
1813
+		);
1814
+		// set attributes as provided in $set_cols_n_values
1815
+		foreach ($set_cols_n_values as $column => $value) {
1816
+			$this->set($column, $value);
1817
+		}
1818
+		// no changes ? then don't do anything
1819
+		if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1820
+			return 0;
1821
+		}
1822
+		/**
1823
+		 * Saving a model object.
1824
+		 * Before we perform a save, this action is fired.
1825
+		 *
1826
+		 * @param EE_Base_Class $model_object the model object about to be saved.
1827
+		 */
1828
+		do_action('AHEE__EE_Base_Class__save__begin', $this);
1829
+		if (! $this->allow_persist()) {
1830
+			return 0;
1831
+		}
1832
+		// now get current attribute values
1833
+		$save_cols_n_values = $this->_fields;
1834
+		// if the object already has an ID, update it. Otherwise, insert it
1835
+		// also: change the assumption about values passed to the model NOT being prepare dby the model object.
1836
+		// They have been
1837
+		$old_assumption_concerning_value_preparation = $model
1838
+			->get_assumption_concerning_values_already_prepared_by_model_object();
1839
+		$model->assume_values_already_prepared_by_model_object(true);
1840
+		// does this model have an autoincrement PK?
1841
+		if ($model->has_primary_key_field()) {
1842
+			if ($model->get_primary_key_field()->is_auto_increment()) {
1843
+				// ok check if it's set, if so: update; if not, insert
1844
+				if (! empty($save_cols_n_values[ $model->primary_key_name() ])) {
1845
+					$results = $model->update_by_ID($save_cols_n_values, $this->ID());
1846
+				} else {
1847
+					unset($save_cols_n_values[ $model->primary_key_name() ]);
1848
+					$results = $model->insert($save_cols_n_values);
1849
+					if ($results) {
1850
+						// if successful, set the primary key
1851
+						// but don't use the normal SET method, because it will check if
1852
+						// an item with the same ID exists in the mapper & db, then
1853
+						// will find it in the db (because we just added it) and THAT object
1854
+						// will get added to the mapper before we can add this one!
1855
+						// but if we just avoid using the SET method, all that headache can be avoided
1856
+						$pk_field_name = $model->primary_key_name();
1857
+						$this->_fields[ $pk_field_name ] = $results;
1858
+						$this->_clear_cached_property($pk_field_name);
1859
+						$model->add_to_entity_map($this);
1860
+						$this->_update_cached_related_model_objs_fks();
1861
+					}
1862
+				}
1863
+			} else {// PK is NOT auto-increment
1864
+				// so check if one like it already exists in the db
1865
+				if ($model->exists_by_ID($this->ID())) {
1866
+					if (WP_DEBUG && ! $this->in_entity_map()) {
1867
+						throw new EE_Error(
1868
+							sprintf(
1869
+								esc_html__(
1870
+									'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s',
1871
+									'event_espresso'
1872
+								),
1873
+								get_class($this),
1874
+								get_class($model) . '::instance()->add_to_entity_map()',
1875
+								get_class($model) . '::instance()->get_one_by_ID()',
1876
+								'<br />'
1877
+							)
1878
+						);
1879
+					}
1880
+					$results = $model->update_by_ID($save_cols_n_values, $this->ID());
1881
+				} else {
1882
+					$results = $model->insert($save_cols_n_values);
1883
+					$this->_update_cached_related_model_objs_fks();
1884
+				}
1885
+			}
1886
+		} else {// there is NO primary key
1887
+			$already_in_db = false;
1888
+			foreach ($model->unique_indexes() as $index) {
1889
+				$uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields());
1890
+				if ($model->exists(array($uniqueness_where_params))) {
1891
+					$already_in_db = true;
1892
+				}
1893
+			}
1894
+			if ($already_in_db) {
1895
+				$combined_pk_fields_n_values = array_intersect_key(
1896
+					$save_cols_n_values,
1897
+					$model->get_combined_primary_key_fields()
1898
+				);
1899
+				$results = $model->update(
1900
+					$save_cols_n_values,
1901
+					$combined_pk_fields_n_values
1902
+				);
1903
+			} else {
1904
+				$results = $model->insert($save_cols_n_values);
1905
+			}
1906
+		}
1907
+		// restore the old assumption about values being prepared by the model object
1908
+		$model->assume_values_already_prepared_by_model_object(
1909
+			$old_assumption_concerning_value_preparation
1910
+		);
1911
+		/**
1912
+		 * After saving the model object this action is called
1913
+		 *
1914
+		 * @param EE_Base_Class $model_object which was just saved
1915
+		 * @param boolean|int   $results      if it were updated, TRUE or FALSE; if it were newly inserted
1916
+		 *                                    the new ID (or 0 if an error occurred and it wasn't updated)
1917
+		 */
1918
+		do_action('AHEE__EE_Base_Class__save__end', $this, $results);
1919
+		$this->_has_changes = false;
1920
+		return $results;
1921
+	}
1922
+
1923
+
1924
+	/**
1925
+	 * Updates the foreign key on related models objects pointing to this to have this model object's ID
1926
+	 * as their foreign key.  If the cached related model objects already exist in the db, saves them (so that the DB
1927
+	 * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its
1928
+	 * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't
1929
+	 * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the
1930
+	 * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether
1931
+	 * or not they exist in the DB (if they do, their DB records will be automatically updated)
1932
+	 *
1933
+	 * @return void
1934
+	 * @throws ReflectionException
1935
+	 * @throws InvalidArgumentException
1936
+	 * @throws InvalidInterfaceException
1937
+	 * @throws InvalidDataTypeException
1938
+	 * @throws EE_Error
1939
+	 */
1940
+	protected function _update_cached_related_model_objs_fks()
1941
+	{
1942
+		$model = $this->get_model();
1943
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1944
+			if ($relation_obj instanceof EE_Has_Many_Relation) {
1945
+				foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) {
1946
+					$fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to(
1947
+						$model->get_this_model_name()
1948
+					);
1949
+					$related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID());
1950
+					if ($related_model_obj_in_cache->ID()) {
1951
+						$related_model_obj_in_cache->save();
1952
+					}
1953
+				}
1954
+			}
1955
+		}
1956
+	}
1957
+
1958
+
1959
+	/**
1960
+	 * Saves this model object and its NEW cached relations to the database.
1961
+	 * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB.
1962
+	 * In order for that to work, we would need to mark model objects as dirty/clean...
1963
+	 * because otherwise, there's a potential for infinite looping of saving
1964
+	 * Saves the cached related model objects, and ensures the relation between them
1965
+	 * and this object and properly setup
1966
+	 *
1967
+	 * @return int ID of new model object on save; 0 on failure+
1968
+	 * @throws ReflectionException
1969
+	 * @throws InvalidArgumentException
1970
+	 * @throws InvalidInterfaceException
1971
+	 * @throws InvalidDataTypeException
1972
+	 * @throws EE_Error
1973
+	 */
1974
+	public function save_new_cached_related_model_objs()
1975
+	{
1976
+		// make sure this has been saved
1977
+		if (! $this->ID()) {
1978
+			$id = $this->save();
1979
+		} else {
1980
+			$id = $this->ID();
1981
+		}
1982
+		// now save all the NEW cached model objects  (ie they don't exist in the DB)
1983
+		foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) {
1984
+			if ($this->_model_relations[ $relationName ]) {
1985
+				// is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation)
1986
+				// or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)?
1987
+				/* @var $related_model_obj EE_Base_Class */
1988
+				if ($relationObj instanceof EE_Belongs_To_Relation) {
1989
+					// add a relation to that relation type (which saves the appropriate thing in the process)
1990
+					// but ONLY if it DOES NOT exist in the DB
1991
+					$related_model_obj = $this->_model_relations[ $relationName ];
1992
+					// if( ! $related_model_obj->ID()){
1993
+					$this->_add_relation_to($related_model_obj, $relationName);
1994
+					$related_model_obj->save_new_cached_related_model_objs();
1995
+					// }
1996
+				} else {
1997
+					foreach ($this->_model_relations[ $relationName ] as $related_model_obj) {
1998
+						// add a relation to that relation type (which saves the appropriate thing in the process)
1999
+						// but ONLY if it DOES NOT exist in the DB
2000
+						// if( ! $related_model_obj->ID()){
2001
+						$this->_add_relation_to($related_model_obj, $relationName);
2002
+						$related_model_obj->save_new_cached_related_model_objs();
2003
+						// }
2004
+					}
2005
+				}
2006
+			}
2007
+		}
2008
+		return $id;
2009
+	}
2010
+
2011
+
2012
+	/**
2013
+	 * for getting a model while instantiated.
2014
+	 *
2015
+	 * @return EEM_Base | EEM_CPT_Base
2016
+	 * @throws ReflectionException
2017
+	 * @throws InvalidArgumentException
2018
+	 * @throws InvalidInterfaceException
2019
+	 * @throws InvalidDataTypeException
2020
+	 * @throws EE_Error
2021
+	 */
2022
+	public function get_model()
2023
+	{
2024
+		if (! $this->_model) {
2025
+			$modelName = self::_get_model_classname(get_class($this));
2026
+			$this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone);
2027
+		} else {
2028
+			$this->_model->set_timezone($this->_timezone);
2029
+		}
2030
+		return $this->_model;
2031
+	}
2032
+
2033
+
2034
+	/**
2035
+	 * @param $props_n_values
2036
+	 * @param $classname
2037
+	 * @return mixed bool|EE_Base_Class|EEM_CPT_Base
2038
+	 * @throws ReflectionException
2039
+	 * @throws InvalidArgumentException
2040
+	 * @throws InvalidInterfaceException
2041
+	 * @throws InvalidDataTypeException
2042
+	 * @throws EE_Error
2043
+	 */
2044
+	protected static function _get_object_from_entity_mapper($props_n_values, $classname)
2045
+	{
2046
+		// TODO: will not work for Term_Relationships because they have no PK!
2047
+		$primary_id_ref = self::_get_primary_key_name($classname);
2048
+		if (array_key_exists($primary_id_ref, $props_n_values)
2049
+			&& ! empty($props_n_values[ $primary_id_ref ])
2050
+		) {
2051
+			$id = $props_n_values[ $primary_id_ref ];
2052
+			return self::_get_model($classname)->get_from_entity_map($id);
2053
+		}
2054
+		return false;
2055
+	}
2056
+
2057
+
2058
+	/**
2059
+	 * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for
2060
+	 * the primary key (if present in incoming values). If there is a key in the incoming array that matches the
2061
+	 * primary key for the model AND it is not null, then we check the db. If there's a an object we return it.  If not
2062
+	 * we return false.
2063
+	 *
2064
+	 * @param  array  $props_n_values   incoming array of properties and their values
2065
+	 * @param  string $classname        the classname of the child class
2066
+	 * @param null    $timezone
2067
+	 * @param array   $date_formats     incoming date_formats in an array where the first value is the
2068
+	 *                                  date_format and the second value is the time format
2069
+	 * @return mixed (EE_Base_Class|bool)
2070
+	 * @throws InvalidArgumentException
2071
+	 * @throws InvalidInterfaceException
2072
+	 * @throws InvalidDataTypeException
2073
+	 * @throws EE_Error
2074
+	 * @throws ReflectionException
2075
+	 * @throws ReflectionException
2076
+	 * @throws ReflectionException
2077
+	 */
2078
+	protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array())
2079
+	{
2080
+		$existing = null;
2081
+		$model = self::_get_model($classname, $timezone);
2082
+		if ($model->has_primary_key_field()) {
2083
+			$primary_id_ref = self::_get_primary_key_name($classname);
2084
+			if (array_key_exists($primary_id_ref, $props_n_values)
2085
+				&& ! empty($props_n_values[ $primary_id_ref ])
2086
+			) {
2087
+				$existing = $model->get_one_by_ID(
2088
+					$props_n_values[ $primary_id_ref ]
2089
+				);
2090
+			}
2091
+		} elseif ($model->has_all_combined_primary_key_fields($props_n_values)) {
2092
+			// no primary key on this model, but there's still a matching item in the DB
2093
+			$existing = self::_get_model($classname, $timezone)->get_one_by_ID(
2094
+				self::_get_model($classname, $timezone)
2095
+					->get_index_primary_key_string($props_n_values)
2096
+			);
2097
+		}
2098
+		if ($existing) {
2099
+			// set date formats if present before setting values
2100
+			if (! empty($date_formats) && is_array($date_formats)) {
2101
+				$existing->set_date_format($date_formats[0]);
2102
+				$existing->set_time_format($date_formats[1]);
2103
+			} else {
2104
+				// set default formats for date and time
2105
+				$existing->set_date_format(get_option('date_format'));
2106
+				$existing->set_time_format(get_option('time_format'));
2107
+			}
2108
+			foreach ($props_n_values as $property => $field_value) {
2109
+				$existing->set($property, $field_value);
2110
+			}
2111
+			return $existing;
2112
+		}
2113
+		return false;
2114
+	}
2115
+
2116
+
2117
+	/**
2118
+	 * Gets the EEM_*_Model for this class
2119
+	 *
2120
+	 * @access public now, as this is more convenient
2121
+	 * @param      $classname
2122
+	 * @param null $timezone
2123
+	 * @throws ReflectionException
2124
+	 * @throws InvalidArgumentException
2125
+	 * @throws InvalidInterfaceException
2126
+	 * @throws InvalidDataTypeException
2127
+	 * @throws EE_Error
2128
+	 * @return EEM_Base
2129
+	 */
2130
+	protected static function _get_model($classname, $timezone = null)
2131
+	{
2132
+		// find model for this class
2133
+		if (! $classname) {
2134
+			throw new EE_Error(
2135
+				sprintf(
2136
+					esc_html__(
2137
+						'What were you thinking calling _get_model(%s)?? You need to specify the class name',
2138
+						'event_espresso'
2139
+					),
2140
+					$classname
2141
+				)
2142
+			);
2143
+		}
2144
+		$modelName = self::_get_model_classname($classname);
2145
+		return self::_get_model_instance_with_name($modelName, $timezone);
2146
+	}
2147
+
2148
+
2149
+	/**
2150
+	 * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee)
2151
+	 *
2152
+	 * @param string $model_classname
2153
+	 * @param null   $timezone
2154
+	 * @return EEM_Base
2155
+	 * @throws ReflectionException
2156
+	 * @throws InvalidArgumentException
2157
+	 * @throws InvalidInterfaceException
2158
+	 * @throws InvalidDataTypeException
2159
+	 * @throws EE_Error
2160
+	 */
2161
+	protected static function _get_model_instance_with_name($model_classname, $timezone = null)
2162
+	{
2163
+		$model_classname = str_replace('EEM_', '', $model_classname);
2164
+		$model = EE_Registry::instance()->load_model($model_classname);
2165
+		$model->set_timezone($timezone);
2166
+		return $model;
2167
+	}
2168
+
2169
+
2170
+	/**
2171
+	 * If a model name is provided (eg Registration), gets the model classname for that model.
2172
+	 * Also works if a model class's classname is provided (eg EE_Registration).
2173
+	 *
2174
+	 * @param null $model_name
2175
+	 * @return string like EEM_Attendee
2176
+	 */
2177
+	private static function _get_model_classname($model_name = null)
2178
+	{
2179
+		if (strpos($model_name, 'EE_') === 0) {
2180
+			$model_classname = str_replace('EE_', 'EEM_', $model_name);
2181
+		} else {
2182
+			$model_classname = 'EEM_' . $model_name;
2183
+		}
2184
+		return $model_classname;
2185
+	}
2186
+
2187
+
2188
+	/**
2189
+	 * returns the name of the primary key attribute
2190
+	 *
2191
+	 * @param null $classname
2192
+	 * @throws ReflectionException
2193
+	 * @throws InvalidArgumentException
2194
+	 * @throws InvalidInterfaceException
2195
+	 * @throws InvalidDataTypeException
2196
+	 * @throws EE_Error
2197
+	 * @return string
2198
+	 */
2199
+	protected static function _get_primary_key_name($classname = null)
2200
+	{
2201
+		if (! $classname) {
2202
+			throw new EE_Error(
2203
+				sprintf(
2204
+					esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'),
2205
+					$classname
2206
+				)
2207
+			);
2208
+		}
2209
+		return self::_get_model($classname)->get_primary_key_field()->get_name();
2210
+	}
2211
+
2212
+
2213
+	/**
2214
+	 * Gets the value of the primary key.
2215
+	 * If the object hasn't yet been saved, it should be whatever the model field's default was
2216
+	 * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value
2217
+	 * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL).
2218
+	 *
2219
+	 * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string
2220
+	 * @throws ReflectionException
2221
+	 * @throws InvalidArgumentException
2222
+	 * @throws InvalidInterfaceException
2223
+	 * @throws InvalidDataTypeException
2224
+	 * @throws EE_Error
2225
+	 */
2226
+	public function ID()
2227
+	{
2228
+		$model = $this->get_model();
2229
+		// now that we know the name of the variable, use a variable variable to get its value and return its
2230
+		if ($model->has_primary_key_field()) {
2231
+			return $this->_fields[ $model->primary_key_name() ];
2232
+		}
2233
+		return $model->get_index_primary_key_string($this->_fields);
2234
+	}
2235
+
2236
+
2237
+	/**
2238
+	 * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current
2239
+	 * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE
2240
+	 * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing
2241
+	 *
2242
+	 * @param mixed  $otherObjectModelObjectOrID       EE_Base_Class or the ID of the other object
2243
+	 * @param string $relationName                     eg 'Events','Question',etc.
2244
+	 *                                                 an attendee to a group, you also want to specify which role they
2245
+	 *                                                 will have in that group. So you would use this parameter to
2246
+	 *                                                 specify array('role-column-name'=>'role-id')
2247
+	 * @param array  $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that
2248
+	 *                                                 allow you to further constrict the relation to being added.
2249
+	 *                                                 However, keep in mind that the columns (keys) given must match a
2250
+	 *                                                 column on the JOIN table and currently only the HABTM models
2251
+	 *                                                 accept these additional conditions.  Also remember that if an
2252
+	 *                                                 exact match isn't found for these extra cols/val pairs, then a
2253
+	 *                                                 NEW row is created in the join table.
2254
+	 * @param null   $cache_id
2255
+	 * @throws ReflectionException
2256
+	 * @throws InvalidArgumentException
2257
+	 * @throws InvalidInterfaceException
2258
+	 * @throws InvalidDataTypeException
2259
+	 * @throws EE_Error
2260
+	 * @return EE_Base_Class the object the relation was added to
2261
+	 */
2262
+	public function _add_relation_to(
2263
+		$otherObjectModelObjectOrID,
2264
+		$relationName,
2265
+		$extra_join_model_fields_n_values = array(),
2266
+		$cache_id = null
2267
+	) {
2268
+		$model = $this->get_model();
2269
+		// if this thing exists in the DB, save the relation to the DB
2270
+		if ($this->ID()) {
2271
+			$otherObject = $model->add_relationship_to(
2272
+				$this,
2273
+				$otherObjectModelObjectOrID,
2274
+				$relationName,
2275
+				$extra_join_model_fields_n_values
2276
+			);
2277
+			// clear cache so future get_many_related and get_first_related() return new results.
2278
+			$this->clear_cache($relationName, $otherObject, true);
2279
+			if ($otherObject instanceof EE_Base_Class) {
2280
+				$otherObject->clear_cache($model->get_this_model_name(), $this);
2281
+			}
2282
+		} else {
2283
+			// this thing doesn't exist in the DB,  so just cache it
2284
+			if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) {
2285
+				throw new EE_Error(
2286
+					sprintf(
2287
+						esc_html__(
2288
+							'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s',
2289
+							'event_espresso'
2290
+						),
2291
+						$otherObjectModelObjectOrID,
2292
+						get_class($this)
2293
+					)
2294
+				);
2295
+			}
2296
+			$otherObject = $otherObjectModelObjectOrID;
2297
+			$this->cache($relationName, $otherObjectModelObjectOrID, $cache_id);
2298
+		}
2299
+		if ($otherObject instanceof EE_Base_Class) {
2300
+			// fix the reciprocal relation too
2301
+			if ($otherObject->ID()) {
2302
+				// its saved so assumed relations exist in the DB, so we can just
2303
+				// clear the cache so future queries use the updated info in the DB
2304
+				$otherObject->clear_cache(
2305
+					$model->get_this_model_name(),
2306
+					null,
2307
+					true
2308
+				);
2309
+			} else {
2310
+				// it's not saved, so it caches relations like this
2311
+				$otherObject->cache($model->get_this_model_name(), $this);
2312
+			}
2313
+		}
2314
+		return $otherObject;
2315
+	}
2316
+
2317
+
2318
+	/**
2319
+	 * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current
2320
+	 * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE
2321
+	 * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing
2322
+	 * from the cache
2323
+	 *
2324
+	 * @param mixed  $otherObjectModelObjectOrID
2325
+	 *                EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved
2326
+	 *                to the DB yet
2327
+	 * @param string $relationName
2328
+	 * @param array  $where_query
2329
+	 *                You can optionally include an array of key=>value pairs that allow you to further constrict the
2330
+	 *                relation to being added. However, keep in mind that the columns (keys) given must match a column
2331
+	 *                on the JOIN table and currently only the HABTM models accept these additional conditions. Also
2332
+	 *                remember that if an exact match isn't found for these extra cols/val pairs, then no row is
2333
+	 *                deleted.
2334
+	 * @return EE_Base_Class the relation was removed from
2335
+	 * @throws ReflectionException
2336
+	 * @throws InvalidArgumentException
2337
+	 * @throws InvalidInterfaceException
2338
+	 * @throws InvalidDataTypeException
2339
+	 * @throws EE_Error
2340
+	 */
2341
+	public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array())
2342
+	{
2343
+		if ($this->ID()) {
2344
+			// if this exists in the DB, save the relation change to the DB too
2345
+			$otherObject = $this->get_model()->remove_relationship_to(
2346
+				$this,
2347
+				$otherObjectModelObjectOrID,
2348
+				$relationName,
2349
+				$where_query
2350
+			);
2351
+			$this->clear_cache(
2352
+				$relationName,
2353
+				$otherObject
2354
+			);
2355
+		} else {
2356
+			// this doesn't exist in the DB, just remove it from the cache
2357
+			$otherObject = $this->clear_cache(
2358
+				$relationName,
2359
+				$otherObjectModelObjectOrID
2360
+			);
2361
+		}
2362
+		if ($otherObject instanceof EE_Base_Class) {
2363
+			$otherObject->clear_cache(
2364
+				$this->get_model()->get_this_model_name(),
2365
+				$this
2366
+			);
2367
+		}
2368
+		return $otherObject;
2369
+	}
2370
+
2371
+
2372
+	/**
2373
+	 * Removes ALL the related things for the $relationName.
2374
+	 *
2375
+	 * @param string $relationName
2376
+	 * @param array  $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
2377
+	 * @return EE_Base_Class
2378
+	 * @throws ReflectionException
2379
+	 * @throws InvalidArgumentException
2380
+	 * @throws InvalidInterfaceException
2381
+	 * @throws InvalidDataTypeException
2382
+	 * @throws EE_Error
2383
+	 */
2384
+	public function _remove_relations($relationName, $where_query_params = array())
2385
+	{
2386
+		if ($this->ID()) {
2387
+			// if this exists in the DB, save the relation change to the DB too
2388
+			$otherObjects = $this->get_model()->remove_relations(
2389
+				$this,
2390
+				$relationName,
2391
+				$where_query_params
2392
+			);
2393
+			$this->clear_cache(
2394
+				$relationName,
2395
+				null,
2396
+				true
2397
+			);
2398
+		} else {
2399
+			// this doesn't exist in the DB, just remove it from the cache
2400
+			$otherObjects = $this->clear_cache(
2401
+				$relationName,
2402
+				null,
2403
+				true
2404
+			);
2405
+		}
2406
+		if (is_array($otherObjects)) {
2407
+			foreach ($otherObjects as $otherObject) {
2408
+				$otherObject->clear_cache(
2409
+					$this->get_model()->get_this_model_name(),
2410
+					$this
2411
+				);
2412
+			}
2413
+		}
2414
+		return $otherObjects;
2415
+	}
2416
+
2417
+
2418
+	/**
2419
+	 * Gets all the related model objects of the specified type. Eg, if the current class if
2420
+	 * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the
2421
+	 * EE_Registration objects which related to this event. Note: by default, we remove the "default query params"
2422
+	 * because we want to get even deleted items etc.
2423
+	 *
2424
+	 * @param string $relationName key in the model's _model_relations array
2425
+	 * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
2426
+	 * @return EE_Base_Class[]     Results not necessarily indexed by IDs, because some results might not have primary
2427
+	 *                             keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these
2428
+	 *                             results if you want IDs
2429
+	 * @throws ReflectionException
2430
+	 * @throws InvalidArgumentException
2431
+	 * @throws InvalidInterfaceException
2432
+	 * @throws InvalidDataTypeException
2433
+	 * @throws EE_Error
2434
+	 */
2435
+	public function get_many_related($relationName, $query_params = array())
2436
+	{
2437
+		if ($this->ID()) {
2438
+			// this exists in the DB, so get the related things from either the cache or the DB
2439
+			// if there are query parameters, forget about caching the related model objects.
2440
+			if ($query_params) {
2441
+				$related_model_objects = $this->get_model()->get_all_related(
2442
+					$this,
2443
+					$relationName,
2444
+					$query_params
2445
+				);
2446
+			} else {
2447
+				// did we already cache the result of this query?
2448
+				$cached_results = $this->get_all_from_cache($relationName);
2449
+				if (! $cached_results) {
2450
+					$related_model_objects = $this->get_model()->get_all_related(
2451
+						$this,
2452
+						$relationName,
2453
+						$query_params
2454
+					);
2455
+					// if no query parameters were passed, then we got all the related model objects
2456
+					// for that relation. We can cache them then.
2457
+					foreach ($related_model_objects as $related_model_object) {
2458
+						$this->cache($relationName, $related_model_object);
2459
+					}
2460
+				} else {
2461
+					$related_model_objects = $cached_results;
2462
+				}
2463
+			}
2464
+		} else {
2465
+			// this doesn't exist in the DB, so just get the related things from the cache
2466
+			$related_model_objects = $this->get_all_from_cache($relationName);
2467
+		}
2468
+		return $related_model_objects;
2469
+	}
2470
+
2471
+
2472
+	/**
2473
+	 * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default,
2474
+	 * unless otherwise specified in the $query_params
2475
+	 *
2476
+	 * @param string $relation_name  model_name like 'Event', or 'Registration'
2477
+	 * @param array  $query_params   @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2478
+	 * @param string $field_to_count name of field to count by. By default, uses primary key
2479
+	 * @param bool   $distinct       if we want to only count the distinct values for the column then you can trigger
2480
+	 *                               that by the setting $distinct to TRUE;
2481
+	 * @return int
2482
+	 * @throws ReflectionException
2483
+	 * @throws InvalidArgumentException
2484
+	 * @throws InvalidInterfaceException
2485
+	 * @throws InvalidDataTypeException
2486
+	 * @throws EE_Error
2487
+	 */
2488
+	public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false)
2489
+	{
2490
+		return $this->get_model()->count_related(
2491
+			$this,
2492
+			$relation_name,
2493
+			$query_params,
2494
+			$field_to_count,
2495
+			$distinct
2496
+		);
2497
+	}
2498
+
2499
+
2500
+	/**
2501
+	 * Instead of getting the related model objects, simply sums up the values of the specified field.
2502
+	 * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params
2503
+	 *
2504
+	 * @param string $relation_name model_name like 'Event', or 'Registration'
2505
+	 * @param array  $query_params  @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2506
+	 * @param string $field_to_sum  name of field to count by.
2507
+	 *                              By default, uses primary key
2508
+	 *                              (which doesn't make much sense, so you should probably change it)
2509
+	 * @return int
2510
+	 * @throws ReflectionException
2511
+	 * @throws InvalidArgumentException
2512
+	 * @throws InvalidInterfaceException
2513
+	 * @throws InvalidDataTypeException
2514
+	 * @throws EE_Error
2515
+	 */
2516
+	public function sum_related($relation_name, $query_params = array(), $field_to_sum = null)
2517
+	{
2518
+		return $this->get_model()->sum_related(
2519
+			$this,
2520
+			$relation_name,
2521
+			$query_params,
2522
+			$field_to_sum
2523
+		);
2524
+	}
2525
+
2526
+
2527
+	/**
2528
+	 * Gets the first (ie, one) related model object of the specified type.
2529
+	 *
2530
+	 * @param string $relationName key in the model's _model_relations array
2531
+	 * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2532
+	 * @return EE_Base_Class (not an array, a single object)
2533
+	 * @throws ReflectionException
2534
+	 * @throws InvalidArgumentException
2535
+	 * @throws InvalidInterfaceException
2536
+	 * @throws InvalidDataTypeException
2537
+	 * @throws EE_Error
2538
+	 */
2539
+	public function get_first_related($relationName, $query_params = array())
2540
+	{
2541
+		$model = $this->get_model();
2542
+		if ($this->ID()) {// this exists in the DB, get from the cache OR the DB
2543
+			// if they've provided some query parameters, don't bother trying to cache the result
2544
+			// also make sure we're not caching the result of get_first_related
2545
+			// on a relation which should have an array of objects (because the cache might have an array of objects)
2546
+			if ($query_params
2547
+				|| ! $model->related_settings_for($relationName)
2548
+					 instanceof
2549
+					 EE_Belongs_To_Relation
2550
+			) {
2551
+				$related_model_object = $model->get_first_related(
2552
+					$this,
2553
+					$relationName,
2554
+					$query_params
2555
+				);
2556
+			} else {
2557
+				// first, check if we've already cached the result of this query
2558
+				$cached_result = $this->get_one_from_cache($relationName);
2559
+				if (! $cached_result) {
2560
+					$related_model_object = $model->get_first_related(
2561
+						$this,
2562
+						$relationName,
2563
+						$query_params
2564
+					);
2565
+					$this->cache($relationName, $related_model_object);
2566
+				} else {
2567
+					$related_model_object = $cached_result;
2568
+				}
2569
+			}
2570
+		} else {
2571
+			$related_model_object = null;
2572
+			// this doesn't exist in the Db,
2573
+			// but maybe the relation is of type belongs to, and so the related thing might
2574
+			if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) {
2575
+				$related_model_object = $model->get_first_related(
2576
+					$this,
2577
+					$relationName,
2578
+					$query_params
2579
+				);
2580
+			}
2581
+			// this doesn't exist in the DB and apparently the thing it belongs to doesn't either,
2582
+			// just get what's cached on this object
2583
+			if (! $related_model_object) {
2584
+				$related_model_object = $this->get_one_from_cache($relationName);
2585
+			}
2586
+		}
2587
+		return $related_model_object;
2588
+	}
2589
+
2590
+
2591
+	/**
2592
+	 * Does a delete on all related objects of type $relationName and removes
2593
+	 * the current model object's relation to them. If they can't be deleted (because
2594
+	 * of blocking related model objects) does nothing. If the related model objects are
2595
+	 * soft-deletable, they will be soft-deleted regardless of related blocking model objects.
2596
+	 * If this model object doesn't exist yet in the DB, just removes its related things
2597
+	 *
2598
+	 * @param string $relationName
2599
+	 * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2600
+	 * @return int how many deleted
2601
+	 * @throws ReflectionException
2602
+	 * @throws InvalidArgumentException
2603
+	 * @throws InvalidInterfaceException
2604
+	 * @throws InvalidDataTypeException
2605
+	 * @throws EE_Error
2606
+	 */
2607
+	public function delete_related($relationName, $query_params = array())
2608
+	{
2609
+		if ($this->ID()) {
2610
+			$count = $this->get_model()->delete_related(
2611
+				$this,
2612
+				$relationName,
2613
+				$query_params
2614
+			);
2615
+		} else {
2616
+			$count = count($this->get_all_from_cache($relationName));
2617
+			$this->clear_cache($relationName, null, true);
2618
+		}
2619
+		return $count;
2620
+	}
2621
+
2622
+
2623
+	/**
2624
+	 * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes
2625
+	 * the current model object's relation to them. If they can't be deleted (because
2626
+	 * of blocking related model objects) just does a soft delete on it instead, if possible.
2627
+	 * If the related thing isn't a soft-deletable model object, this function is identical
2628
+	 * to delete_related(). If this model object doesn't exist in the DB, just remove its related things
2629
+	 *
2630
+	 * @param string $relationName
2631
+	 * @param array  $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
2632
+	 * @return int how many deleted (including those soft deleted)
2633
+	 * @throws ReflectionException
2634
+	 * @throws InvalidArgumentException
2635
+	 * @throws InvalidInterfaceException
2636
+	 * @throws InvalidDataTypeException
2637
+	 * @throws EE_Error
2638
+	 */
2639
+	public function delete_related_permanently($relationName, $query_params = array())
2640
+	{
2641
+		if ($this->ID()) {
2642
+			$count = $this->get_model()->delete_related_permanently(
2643
+				$this,
2644
+				$relationName,
2645
+				$query_params
2646
+			);
2647
+		} else {
2648
+			$count = count($this->get_all_from_cache($relationName));
2649
+		}
2650
+		$this->clear_cache($relationName, null, true);
2651
+		return $count;
2652
+	}
2653
+
2654
+
2655
+	/**
2656
+	 * is_set
2657
+	 * Just a simple utility function children can use for checking if property exists
2658
+	 *
2659
+	 * @access  public
2660
+	 * @param  string $field_name property to check
2661
+	 * @return bool                              TRUE if existing,FALSE if not.
2662
+	 */
2663
+	public function is_set($field_name)
2664
+	{
2665
+		return isset($this->_fields[ $field_name ]);
2666
+	}
2667
+
2668
+
2669
+	/**
2670
+	 * Just a simple utility function children can use for checking if property (or properties) exists and throwing an
2671
+	 * EE_Error exception if they don't
2672
+	 *
2673
+	 * @param  mixed (string|array) $properties properties to check
2674
+	 * @throws EE_Error
2675
+	 * @return bool                              TRUE if existing, throw EE_Error if not.
2676
+	 */
2677
+	protected function _property_exists($properties)
2678
+	{
2679
+		foreach ((array) $properties as $property_name) {
2680
+			// first make sure this property exists
2681
+			if (! $this->_fields[ $property_name ]) {
2682
+				throw new EE_Error(
2683
+					sprintf(
2684
+						esc_html__(
2685
+							'Trying to retrieve a non-existent property (%s).  Double check the spelling please',
2686
+							'event_espresso'
2687
+						),
2688
+						$property_name
2689
+					)
2690
+				);
2691
+			}
2692
+		}
2693
+		return true;
2694
+	}
2695
+
2696
+
2697
+	/**
2698
+	 * This simply returns an array of model fields for this object
2699
+	 *
2700
+	 * @return array
2701
+	 * @throws ReflectionException
2702
+	 * @throws InvalidArgumentException
2703
+	 * @throws InvalidInterfaceException
2704
+	 * @throws InvalidDataTypeException
2705
+	 * @throws EE_Error
2706
+	 */
2707
+	public function model_field_array()
2708
+	{
2709
+		$fields = $this->get_model()->field_settings(false);
2710
+		$properties = array();
2711
+		// remove prepended underscore
2712
+		foreach ($fields as $field_name => $settings) {
2713
+			$properties[ $field_name ] = $this->get($field_name);
2714
+		}
2715
+		return $properties;
2716
+	}
2717
+
2718
+
2719
+	/**
2720
+	 * Very handy general function to allow for plugins to extend any child of EE_Base_Class.
2721
+	 * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called
2722
+	 * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments.
2723
+	 * Instead of requiring a plugin to extend the EE_Base_Class
2724
+	 * (which works fine is there's only 1 plugin, but when will that happen?)
2725
+	 * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}'
2726
+	 * (eg, filters_hook_espresso__EE_Answer__my_great_function)
2727
+	 * and accepts 2 arguments: the object on which the function was called,
2728
+	 * and an array of the original arguments passed to the function.
2729
+	 * Whatever their callback function returns will be returned by this function.
2730
+	 * Example: in functions.php (or in a plugin):
2731
+	 *      add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3);
2732
+	 *      function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){
2733
+	 *          $returnString= "you called my_callback! and passed args:".implode(",",$argsArray);
2734
+	 *          return $previousReturnValue.$returnString;
2735
+	 *      }
2736
+	 * require('EE_Answer.class.php');
2737
+	 * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42'));
2738
+	 * echo $answer->my_callback('monkeys',100);
2739
+	 * //will output "you called my_callback! and passed args:monkeys,100"
2740
+	 *
2741
+	 * @param string $methodName name of method which was called on a child of EE_Base_Class, but which
2742
+	 * @param array  $args       array of original arguments passed to the function
2743
+	 * @throws EE_Error
2744
+	 * @return mixed whatever the plugin which calls add_filter decides
2745
+	 */
2746
+	public function __call($methodName, $args)
2747
+	{
2748
+		$className = get_class($this);
2749
+		$tagName = "FHEE__{$className}__{$methodName}";
2750
+		if (! has_filter($tagName)) {
2751
+			throw new EE_Error(
2752
+				sprintf(
2753
+					esc_html__(
2754
+						"Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}",
2755
+						'event_espresso'
2756
+					),
2757
+					$methodName,
2758
+					$className,
2759
+					$tagName
2760
+				)
2761
+			);
2762
+		}
2763
+		return apply_filters($tagName, null, $this, $args);
2764
+	}
2765
+
2766
+
2767
+	/**
2768
+	 * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value.
2769
+	 * A $previous_value can be specified in case there are many meta rows with the same key
2770
+	 *
2771
+	 * @param string $meta_key
2772
+	 * @param mixed  $meta_value
2773
+	 * @param mixed  $previous_value
2774
+	 * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row)
2775
+	 *                  NOTE: if the values haven't changed, returns 0
2776
+	 * @throws InvalidArgumentException
2777
+	 * @throws InvalidInterfaceException
2778
+	 * @throws InvalidDataTypeException
2779
+	 * @throws EE_Error
2780
+	 * @throws ReflectionException
2781
+	 */
2782
+	public function update_extra_meta($meta_key, $meta_value, $previous_value = null)
2783
+	{
2784
+		$query_params = array(
2785
+			array(
2786
+				'EXM_key'  => $meta_key,
2787
+				'OBJ_ID'   => $this->ID(),
2788
+				'EXM_type' => $this->get_model()->get_this_model_name(),
2789
+			),
2790
+		);
2791
+		if ($previous_value !== null) {
2792
+			$query_params[0]['EXM_value'] = $meta_value;
2793
+		}
2794
+		$existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params);
2795
+		if (! $existing_rows_like_that) {
2796
+			return $this->add_extra_meta($meta_key, $meta_value);
2797
+		}
2798
+		foreach ($existing_rows_like_that as $existing_row) {
2799
+			$existing_row->save(array('EXM_value' => $meta_value));
2800
+		}
2801
+		return count($existing_rows_like_that);
2802
+	}
2803
+
2804
+
2805
+	/**
2806
+	 * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check
2807
+	 * no other extra meta for this model object have the same key. Returns TRUE if the
2808
+	 * extra meta row was entered, false if not
2809
+	 *
2810
+	 * @param string  $meta_key
2811
+	 * @param mixed   $meta_value
2812
+	 * @param boolean $unique
2813
+	 * @return boolean
2814
+	 * @throws InvalidArgumentException
2815
+	 * @throws InvalidInterfaceException
2816
+	 * @throws InvalidDataTypeException
2817
+	 * @throws EE_Error
2818
+	 * @throws ReflectionException
2819
+	 * @throws ReflectionException
2820
+	 */
2821
+	public function add_extra_meta($meta_key, $meta_value, $unique = false)
2822
+	{
2823
+		if ($unique) {
2824
+			$existing_extra_meta = EEM_Extra_Meta::instance()->get_one(
2825
+				array(
2826
+					array(
2827
+						'EXM_key'  => $meta_key,
2828
+						'OBJ_ID'   => $this->ID(),
2829
+						'EXM_type' => $this->get_model()->get_this_model_name(),
2830
+					),
2831
+				)
2832
+			);
2833
+			if ($existing_extra_meta) {
2834
+				return false;
2835
+			}
2836
+		}
2837
+		$new_extra_meta = EE_Extra_Meta::new_instance(
2838
+			array(
2839
+				'EXM_key'   => $meta_key,
2840
+				'EXM_value' => $meta_value,
2841
+				'OBJ_ID'    => $this->ID(),
2842
+				'EXM_type'  => $this->get_model()->get_this_model_name(),
2843
+			)
2844
+		);
2845
+		$new_extra_meta->save();
2846
+		return true;
2847
+	}
2848
+
2849
+
2850
+	/**
2851
+	 * Deletes all the extra meta rows for this record as specified by key. If $meta_value
2852
+	 * is specified, only deletes extra meta records with that value.
2853
+	 *
2854
+	 * @param string $meta_key
2855
+	 * @param mixed  $meta_value
2856
+	 * @return int number of extra meta rows deleted
2857
+	 * @throws InvalidArgumentException
2858
+	 * @throws InvalidInterfaceException
2859
+	 * @throws InvalidDataTypeException
2860
+	 * @throws EE_Error
2861
+	 * @throws ReflectionException
2862
+	 */
2863
+	public function delete_extra_meta($meta_key, $meta_value = null)
2864
+	{
2865
+		$query_params = array(
2866
+			array(
2867
+				'EXM_key'  => $meta_key,
2868
+				'OBJ_ID'   => $this->ID(),
2869
+				'EXM_type' => $this->get_model()->get_this_model_name(),
2870
+			),
2871
+		);
2872
+		if ($meta_value !== null) {
2873
+			$query_params[0]['EXM_value'] = $meta_value;
2874
+		}
2875
+		return EEM_Extra_Meta::instance()->delete($query_params);
2876
+	}
2877
+
2878
+
2879
+	/**
2880
+	 * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise
2881
+	 * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation.
2882
+	 * You can specify $default is case you haven't found the extra meta
2883
+	 *
2884
+	 * @param string  $meta_key
2885
+	 * @param boolean $single
2886
+	 * @param mixed   $default if we don't find anything, what should we return?
2887
+	 * @return mixed single value if $single; array if ! $single
2888
+	 * @throws ReflectionException
2889
+	 * @throws InvalidArgumentException
2890
+	 * @throws InvalidInterfaceException
2891
+	 * @throws InvalidDataTypeException
2892
+	 * @throws EE_Error
2893
+	 */
2894
+	public function get_extra_meta($meta_key, $single = false, $default = null)
2895
+	{
2896
+		if ($single) {
2897
+			$result = $this->get_first_related(
2898
+				'Extra_Meta',
2899
+				array(array('EXM_key' => $meta_key))
2900
+			);
2901
+			if ($result instanceof EE_Extra_Meta) {
2902
+				return $result->value();
2903
+			}
2904
+		} else {
2905
+			$results = $this->get_many_related(
2906
+				'Extra_Meta',
2907
+				array(array('EXM_key' => $meta_key))
2908
+			);
2909
+			if ($results) {
2910
+				$values = array();
2911
+				foreach ($results as $result) {
2912
+					if ($result instanceof EE_Extra_Meta) {
2913
+						$values[ $result->ID() ] = $result->value();
2914
+					}
2915
+				}
2916
+				return $values;
2917
+			}
2918
+		}
2919
+		// if nothing discovered yet return default.
2920
+		return apply_filters(
2921
+			'FHEE__EE_Base_Class__get_extra_meta__default_value',
2922
+			$default,
2923
+			$meta_key,
2924
+			$single,
2925
+			$this
2926
+		);
2927
+	}
2928
+
2929
+
2930
+	/**
2931
+	 * Returns a simple array of all the extra meta associated with this model object.
2932
+	 * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the
2933
+	 * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with
2934
+	 * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123))
2935
+	 * If $one_of_each_key is false, it will return an array with the top-level keys being
2936
+	 * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and
2937
+	 * finally the extra meta's value as each sub-value. (eg
2938
+	 * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123)))
2939
+	 *
2940
+	 * @param boolean $one_of_each_key
2941
+	 * @return array
2942
+	 * @throws ReflectionException
2943
+	 * @throws InvalidArgumentException
2944
+	 * @throws InvalidInterfaceException
2945
+	 * @throws InvalidDataTypeException
2946
+	 * @throws EE_Error
2947
+	 */
2948
+	public function all_extra_meta_array($one_of_each_key = true)
2949
+	{
2950
+		$return_array = array();
2951
+		if ($one_of_each_key) {
2952
+			$extra_meta_objs = $this->get_many_related(
2953
+				'Extra_Meta',
2954
+				array('group_by' => 'EXM_key')
2955
+			);
2956
+			foreach ($extra_meta_objs as $extra_meta_obj) {
2957
+				if ($extra_meta_obj instanceof EE_Extra_Meta) {
2958
+					$return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value();
2959
+				}
2960
+			}
2961
+		} else {
2962
+			$extra_meta_objs = $this->get_many_related('Extra_Meta');
2963
+			foreach ($extra_meta_objs as $extra_meta_obj) {
2964
+				if ($extra_meta_obj instanceof EE_Extra_Meta) {
2965
+					if (! isset($return_array[ $extra_meta_obj->key() ])) {
2966
+						$return_array[ $extra_meta_obj->key() ] = array();
2967
+					}
2968
+					$return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value();
2969
+				}
2970
+			}
2971
+		}
2972
+		return $return_array;
2973
+	}
2974
+
2975
+
2976
+	/**
2977
+	 * Gets a pretty nice displayable nice for this model object. Often overridden
2978
+	 *
2979
+	 * @return string
2980
+	 * @throws ReflectionException
2981
+	 * @throws InvalidArgumentException
2982
+	 * @throws InvalidInterfaceException
2983
+	 * @throws InvalidDataTypeException
2984
+	 * @throws EE_Error
2985
+	 */
2986
+	public function name()
2987
+	{
2988
+		// find a field that's not a text field
2989
+		$field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base');
2990
+		if ($field_we_can_use) {
2991
+			return $this->get($field_we_can_use->get_name());
2992
+		}
2993
+		$first_few_properties = $this->model_field_array();
2994
+		$first_few_properties = array_slice($first_few_properties, 0, 3);
2995
+		$name_parts = array();
2996
+		foreach ($first_few_properties as $name => $value) {
2997
+			$name_parts[] = "$name:$value";
2998
+		}
2999
+		return implode(',', $name_parts);
3000
+	}
3001
+
3002
+
3003
+	/**
3004
+	 * in_entity_map
3005
+	 * Checks if this model object has been proven to already be in the entity map
3006
+	 *
3007
+	 * @return boolean
3008
+	 * @throws ReflectionException
3009
+	 * @throws InvalidArgumentException
3010
+	 * @throws InvalidInterfaceException
3011
+	 * @throws InvalidDataTypeException
3012
+	 * @throws EE_Error
3013
+	 */
3014
+	public function in_entity_map()
3015
+	{
3016
+		// well, if we looked, did we find it in the entity map?
3017
+		return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this;
3018
+	}
3019
+
3020
+
3021
+	/**
3022
+	 * refresh_from_db
3023
+	 * Makes sure the fields and values on this model object are in-sync with what's in the database.
3024
+	 *
3025
+	 * @throws ReflectionException
3026
+	 * @throws InvalidArgumentException
3027
+	 * @throws InvalidInterfaceException
3028
+	 * @throws InvalidDataTypeException
3029
+	 * @throws EE_Error if this model object isn't in the entity mapper (because then you should
3030
+	 * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE
3031
+	 */
3032
+	public function refresh_from_db()
3033
+	{
3034
+		if ($this->ID() && $this->in_entity_map()) {
3035
+			$this->get_model()->refresh_entity_map_from_db($this->ID());
3036
+		} else {
3037
+			// if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database)
3038
+			// if it has an ID but it's not in the map, and you're asking me to refresh it
3039
+			// that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's
3040
+			// absolutely nothing in it for this ID
3041
+			if (WP_DEBUG) {
3042
+				throw new EE_Error(
3043
+					sprintf(
3044
+						esc_html__(
3045
+							'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.',
3046
+							'event_espresso'
3047
+						),
3048
+						$this->ID(),
3049
+						get_class($this->get_model()) . '::instance()->add_to_entity_map()',
3050
+						get_class($this->get_model()) . '::instance()->refresh_entity_map()'
3051
+					)
3052
+				);
3053
+			}
3054
+		}
3055
+	}
3056
+
3057
+
3058
+	/**
3059
+	 * Change $fields' values to $new_value_sql (which is a string of raw SQL)
3060
+	 *
3061
+	 * @since $VID:$
3062
+	 * @param EE_Model_Field_Base[] $fields
3063
+	 * @param string $new_value_sql
3064
+	 *      example: 'column_name=123',
3065
+	 *      or 'column_name=column_name+1',
3066
+	 *      or 'column_name= CASE
3067
+	 *          WHEN (`column_name` + `other_column` + 5) <= `yet_another_column`
3068
+	 *          THEN `column_name` + 5
3069
+	 *          ELSE `column_name`
3070
+	 *      END'
3071
+	 *      Also updates $field on this model object with the latest value from the database.
3072
+	 * @return bool
3073
+	 * @throws EE_Error
3074
+	 * @throws InvalidArgumentException
3075
+	 * @throws InvalidDataTypeException
3076
+	 * @throws InvalidInterfaceException
3077
+	 * @throws ReflectionException
3078
+	 */
3079
+	protected function updateFieldsInDB($fields, $new_value_sql)
3080
+	{
3081
+		// First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB
3082
+		// if it wasn't even there to start off.
3083
+		if (! $this->ID()) {
3084
+			$this->save();
3085
+		}
3086
+		global $wpdb;
3087
+		if (empty($fields)) {
3088
+			throw new InvalidArgumentException(
3089
+				esc_html__(
3090
+					'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.',
3091
+					'event_espresso'
3092
+				)
3093
+			);
3094
+		}
3095
+		$first_field = reset($fields);
3096
+		$table_alias = $first_field->get_table_alias();
3097
+		foreach ($fields as $field) {
3098
+			if ($table_alias !== $field->get_table_alias()) {
3099
+				throw new InvalidArgumentException(
3100
+					sprintf(
3101
+						esc_html__(
3102
+							// @codingStandardsIgnoreStart
3103
+							'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.',
3104
+							// @codingStandardsIgnoreEnd
3105
+							'event_espresso'
3106
+						),
3107
+						$table_alias,
3108
+						$field->get_table_alias()
3109
+					)
3110
+				);
3111
+			}
3112
+		}
3113
+		// Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it.
3114
+		$table_obj = $this->get_model()->get_table_obj_by_alias($table_alias);
3115
+		$table_pk_value = $this->ID();
3116
+		$table_name = $table_obj->get_table_name();
3117
+		if ($table_obj instanceof EE_Secondary_Table) {
3118
+			$table_pk_field_name = $table_obj->get_fk_on_table();
3119
+		} else {
3120
+			$table_pk_field_name = $table_obj->get_pk_column();
3121
+		}
3122
+
3123
+		$query =
3124
+			"UPDATE `{$table_name}`
3125 3125
             SET "
3126
-            . $new_value_sql
3127
-            . $wpdb->prepare(
3128
-                "
3126
+			. $new_value_sql
3127
+			. $wpdb->prepare(
3128
+				"
3129 3129
             WHERE `{$table_pk_field_name}` = %d;",
3130
-                $table_pk_value
3131
-            );
3132
-        $result = $wpdb->query($query);
3133
-        foreach ($fields as $field) {
3134
-            // If it was successful, we'd like to know the new value.
3135
-            // If it failed, we'd also like to know the new value.
3136
-            $new_value = $this->get_model()->get_var(
3137
-                $this->get_model()->alter_query_params_to_restrict_by_ID(
3138
-                    $this->get_model()->get_index_primary_key_string(
3139
-                        $this->model_field_array()
3140
-                    ),
3141
-                    array(
3142
-                        'default_where_conditions' => 'minimum',
3143
-                    )
3144
-                ),
3145
-                $field->get_name()
3146
-            );
3147
-            $this->set_from_db(
3148
-                $field->get_name(),
3149
-                $new_value
3150
-            );
3151
-        }
3152
-        return (bool) $result;
3153
-    }
3154
-
3155
-
3156
-    /**
3157
-     * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()).
3158
-     * Does not allow negative values, however.
3159
-     *
3160
-     * @since $VID:$
3161
-     * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them
3162
-     *                                   (positive or negative). One important gotcha: all these values must be
3163
-     *                                   on the same table (eg don't pass in one field for the posts table and
3164
-     *                                   another for the event meta table.)
3165
-     * @return bool
3166
-     * @throws EE_Error
3167
-     * @throws InvalidArgumentException
3168
-     * @throws InvalidDataTypeException
3169
-     * @throws InvalidInterfaceException
3170
-     * @throws ReflectionException
3171
-     */
3172
-    public function adjustNumericFieldsInDb(array $fields_n_quantities)
3173
-    {
3174
-        global $wpdb;
3175
-        if (empty($fields_n_quantities)) {
3176
-            // No fields to update? Well sure, we updated them to that value just fine.
3177
-            return true;
3178
-        }
3179
-        $fields = [];
3180
-        $set_sql_statements = [];
3181
-        foreach ($fields_n_quantities as $field_name => $quantity) {
3182
-            $field = $this->get_model()->field_settings_for($field_name, true);
3183
-            $fields[] = $field;
3184
-            $column_name = $field->get_table_column();
3185
-
3186
-            $abs_qty = absint($quantity);
3187
-            if ($quantity > 0) {
3188
-                // don't let the value be negative as often these fields are unsigned
3189
-                $set_sql_statements[] = $wpdb->prepare(
3190
-                    "`{$column_name}` = `{$column_name}` + %d",
3191
-                    $abs_qty
3192
-                );
3193
-            } else {
3194
-                $set_sql_statements[] = $wpdb->prepare(
3195
-                    "`{$column_name}` = CASE
3130
+				$table_pk_value
3131
+			);
3132
+		$result = $wpdb->query($query);
3133
+		foreach ($fields as $field) {
3134
+			// If it was successful, we'd like to know the new value.
3135
+			// If it failed, we'd also like to know the new value.
3136
+			$new_value = $this->get_model()->get_var(
3137
+				$this->get_model()->alter_query_params_to_restrict_by_ID(
3138
+					$this->get_model()->get_index_primary_key_string(
3139
+						$this->model_field_array()
3140
+					),
3141
+					array(
3142
+						'default_where_conditions' => 'minimum',
3143
+					)
3144
+				),
3145
+				$field->get_name()
3146
+			);
3147
+			$this->set_from_db(
3148
+				$field->get_name(),
3149
+				$new_value
3150
+			);
3151
+		}
3152
+		return (bool) $result;
3153
+	}
3154
+
3155
+
3156
+	/**
3157
+	 * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()).
3158
+	 * Does not allow negative values, however.
3159
+	 *
3160
+	 * @since $VID:$
3161
+	 * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them
3162
+	 *                                   (positive or negative). One important gotcha: all these values must be
3163
+	 *                                   on the same table (eg don't pass in one field for the posts table and
3164
+	 *                                   another for the event meta table.)
3165
+	 * @return bool
3166
+	 * @throws EE_Error
3167
+	 * @throws InvalidArgumentException
3168
+	 * @throws InvalidDataTypeException
3169
+	 * @throws InvalidInterfaceException
3170
+	 * @throws ReflectionException
3171
+	 */
3172
+	public function adjustNumericFieldsInDb(array $fields_n_quantities)
3173
+	{
3174
+		global $wpdb;
3175
+		if (empty($fields_n_quantities)) {
3176
+			// No fields to update? Well sure, we updated them to that value just fine.
3177
+			return true;
3178
+		}
3179
+		$fields = [];
3180
+		$set_sql_statements = [];
3181
+		foreach ($fields_n_quantities as $field_name => $quantity) {
3182
+			$field = $this->get_model()->field_settings_for($field_name, true);
3183
+			$fields[] = $field;
3184
+			$column_name = $field->get_table_column();
3185
+
3186
+			$abs_qty = absint($quantity);
3187
+			if ($quantity > 0) {
3188
+				// don't let the value be negative as often these fields are unsigned
3189
+				$set_sql_statements[] = $wpdb->prepare(
3190
+					"`{$column_name}` = `{$column_name}` + %d",
3191
+					$abs_qty
3192
+				);
3193
+			} else {
3194
+				$set_sql_statements[] = $wpdb->prepare(
3195
+					"`{$column_name}` = CASE
3196 3196
                        WHEN (`{$column_name}` >= %d)
3197 3197
                        THEN `{$column_name}` - %d
3198 3198
                        ELSE 0
3199 3199
                     END",
3200
-                    $abs_qty,
3201
-                    $abs_qty
3202
-                );
3203
-            }
3204
-        }
3205
-        return $this->updateFieldsInDB(
3206
-            $fields,
3207
-            implode(', ', $set_sql_statements)
3208
-        );
3209
-    }
3210
-
3211
-
3212
-    /**
3213
-     * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of
3214
-     * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value.
3215
-     * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold.
3216
-     * Returns true if the value was successfully bumped, and updates the value on this model object.
3217
-     * Otherwise returns false.
3218
-     *
3219
-     * @since $VID:$
3220
-     * @param string $field_name_to_bump
3221
-     * @param string $field_name_affecting_total
3222
-     * @param string $limit_field_name
3223
-     * @param int    $quantity
3224
-     * @return bool
3225
-     * @throws EE_Error
3226
-     * @throws InvalidArgumentException
3227
-     * @throws InvalidDataTypeException
3228
-     * @throws InvalidInterfaceException
3229
-     * @throws ReflectionException
3230
-     */
3231
-    public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity)
3232
-    {
3233
-        global $wpdb;
3234
-        $field = $this->get_model()->field_settings_for($field_name_to_bump, true);
3235
-        $column_name = $field->get_table_column();
3236
-
3237
-        $field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true);
3238
-        $column_affecting_total = $field_affecting_total->get_table_column();
3239
-
3240
-        $limiting_field = $this->get_model()->field_settings_for($limit_field_name, true);
3241
-        $limiting_column = $limiting_field->get_table_column();
3242
-        return $this->updateFieldsInDB(
3243
-            [$field],
3244
-            $wpdb->prepare(
3245
-                "`{$column_name}` =
3200
+					$abs_qty,
3201
+					$abs_qty
3202
+				);
3203
+			}
3204
+		}
3205
+		return $this->updateFieldsInDB(
3206
+			$fields,
3207
+			implode(', ', $set_sql_statements)
3208
+		);
3209
+	}
3210
+
3211
+
3212
+	/**
3213
+	 * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of
3214
+	 * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value.
3215
+	 * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold.
3216
+	 * Returns true if the value was successfully bumped, and updates the value on this model object.
3217
+	 * Otherwise returns false.
3218
+	 *
3219
+	 * @since $VID:$
3220
+	 * @param string $field_name_to_bump
3221
+	 * @param string $field_name_affecting_total
3222
+	 * @param string $limit_field_name
3223
+	 * @param int    $quantity
3224
+	 * @return bool
3225
+	 * @throws EE_Error
3226
+	 * @throws InvalidArgumentException
3227
+	 * @throws InvalidDataTypeException
3228
+	 * @throws InvalidInterfaceException
3229
+	 * @throws ReflectionException
3230
+	 */
3231
+	public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity)
3232
+	{
3233
+		global $wpdb;
3234
+		$field = $this->get_model()->field_settings_for($field_name_to_bump, true);
3235
+		$column_name = $field->get_table_column();
3236
+
3237
+		$field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true);
3238
+		$column_affecting_total = $field_affecting_total->get_table_column();
3239
+
3240
+		$limiting_field = $this->get_model()->field_settings_for($limit_field_name, true);
3241
+		$limiting_column = $limiting_field->get_table_column();
3242
+		return $this->updateFieldsInDB(
3243
+			[$field],
3244
+			$wpdb->prepare(
3245
+				"`{$column_name}` =
3246 3246
             CASE
3247 3247
                WHEN ((`{$column_name}` + `{$column_affecting_total}` + %d) <= `{$limiting_column}`) OR `{$limiting_column}` = %d
3248 3248
                THEN `{$column_name}` + %d
3249 3249
                ELSE `{$column_name}`
3250 3250
             END",
3251
-                $quantity,
3252
-                EE_INF_IN_DB,
3253
-                $quantity
3254
-            )
3255
-        );
3256
-    }
3257
-
3258
-
3259
-    /**
3260
-     * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method
3261
-     * (probably a bad assumption they have made, oh well)
3262
-     *
3263
-     * @return string
3264
-     */
3265
-    public function __toString()
3266
-    {
3267
-        try {
3268
-            return sprintf('%s (%s)', $this->name(), $this->ID());
3269
-        } catch (Exception $e) {
3270
-            EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
3271
-            return '';
3272
-        }
3273
-    }
3274
-
3275
-
3276
-    /**
3277
-     * Clear related model objects if they're already in the DB, because otherwise when we
3278
-     * UN-serialize this model object we'll need to be careful to add them to the entity map.
3279
-     * This means if we have made changes to those related model objects, and want to unserialize
3280
-     * the this model object on a subsequent request, changes to those related model objects will be lost.
3281
-     * Instead, those related model objects should be directly serialized and stored.
3282
-     * Eg, the following won't work:
3283
-     * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
3284
-     * $att = $reg->attendee();
3285
-     * $att->set( 'ATT_fname', 'Dirk' );
3286
-     * update_option( 'my_option', serialize( $reg ) );
3287
-     * //END REQUEST
3288
-     * //START NEXT REQUEST
3289
-     * $reg = get_option( 'my_option' );
3290
-     * $reg->attendee()->save();
3291
-     * And would need to be replace with:
3292
-     * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
3293
-     * $att = $reg->attendee();
3294
-     * $att->set( 'ATT_fname', 'Dirk' );
3295
-     * update_option( 'my_option', serialize( $reg ) );
3296
-     * //END REQUEST
3297
-     * //START NEXT REQUEST
3298
-     * $att = get_option( 'my_option' );
3299
-     * $att->save();
3300
-     *
3301
-     * @return array
3302
-     * @throws ReflectionException
3303
-     * @throws InvalidArgumentException
3304
-     * @throws InvalidInterfaceException
3305
-     * @throws InvalidDataTypeException
3306
-     * @throws EE_Error
3307
-     */
3308
-    public function __sleep()
3309
-    {
3310
-        $model = $this->get_model();
3311
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
3312
-            if ($relation_obj instanceof EE_Belongs_To_Relation) {
3313
-                $classname = 'EE_' . $model->get_this_model_name();
3314
-                if ($this->get_one_from_cache($relation_name) instanceof $classname
3315
-                    && $this->get_one_from_cache($relation_name)->ID()
3316
-                ) {
3317
-                    $this->clear_cache(
3318
-                        $relation_name,
3319
-                        $this->get_one_from_cache($relation_name)->ID()
3320
-                    );
3321
-                }
3322
-            }
3323
-        }
3324
-        $this->_props_n_values_provided_in_constructor = array();
3325
-        $properties_to_serialize = get_object_vars($this);
3326
-        // don't serialize the model. It's big and that risks recursion
3327
-        unset($properties_to_serialize['_model']);
3328
-        return array_keys($properties_to_serialize);
3329
-    }
3330
-
3331
-
3332
-    /**
3333
-     * restore _props_n_values_provided_in_constructor
3334
-     * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization,
3335
-     * and therefore should NOT be used to determine if state change has occurred since initial construction.
3336
-     * At best, you would only be able to detect if state change has occurred during THIS request.
3337
-     */
3338
-    public function __wakeup()
3339
-    {
3340
-        $this->_props_n_values_provided_in_constructor = $this->_fields;
3341
-    }
3342
-
3343
-
3344
-    /**
3345
-     * Usage of this magic method is to ensure any internally cached references to object instances that must remain
3346
-     * distinct with the clone host instance are also cloned.
3347
-     */
3348
-    public function __clone()
3349
-    {
3350
-        // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes).
3351
-        foreach ($this->_fields as $field => $value) {
3352
-            if ($value instanceof DateTime) {
3353
-                $this->_fields[ $field ] = clone $value;
3354
-            }
3355
-        }
3356
-    }
3251
+				$quantity,
3252
+				EE_INF_IN_DB,
3253
+				$quantity
3254
+			)
3255
+		);
3256
+	}
3257
+
3258
+
3259
+	/**
3260
+	 * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method
3261
+	 * (probably a bad assumption they have made, oh well)
3262
+	 *
3263
+	 * @return string
3264
+	 */
3265
+	public function __toString()
3266
+	{
3267
+		try {
3268
+			return sprintf('%s (%s)', $this->name(), $this->ID());
3269
+		} catch (Exception $e) {
3270
+			EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
3271
+			return '';
3272
+		}
3273
+	}
3274
+
3275
+
3276
+	/**
3277
+	 * Clear related model objects if they're already in the DB, because otherwise when we
3278
+	 * UN-serialize this model object we'll need to be careful to add them to the entity map.
3279
+	 * This means if we have made changes to those related model objects, and want to unserialize
3280
+	 * the this model object on a subsequent request, changes to those related model objects will be lost.
3281
+	 * Instead, those related model objects should be directly serialized and stored.
3282
+	 * Eg, the following won't work:
3283
+	 * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
3284
+	 * $att = $reg->attendee();
3285
+	 * $att->set( 'ATT_fname', 'Dirk' );
3286
+	 * update_option( 'my_option', serialize( $reg ) );
3287
+	 * //END REQUEST
3288
+	 * //START NEXT REQUEST
3289
+	 * $reg = get_option( 'my_option' );
3290
+	 * $reg->attendee()->save();
3291
+	 * And would need to be replace with:
3292
+	 * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
3293
+	 * $att = $reg->attendee();
3294
+	 * $att->set( 'ATT_fname', 'Dirk' );
3295
+	 * update_option( 'my_option', serialize( $reg ) );
3296
+	 * //END REQUEST
3297
+	 * //START NEXT REQUEST
3298
+	 * $att = get_option( 'my_option' );
3299
+	 * $att->save();
3300
+	 *
3301
+	 * @return array
3302
+	 * @throws ReflectionException
3303
+	 * @throws InvalidArgumentException
3304
+	 * @throws InvalidInterfaceException
3305
+	 * @throws InvalidDataTypeException
3306
+	 * @throws EE_Error
3307
+	 */
3308
+	public function __sleep()
3309
+	{
3310
+		$model = $this->get_model();
3311
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
3312
+			if ($relation_obj instanceof EE_Belongs_To_Relation) {
3313
+				$classname = 'EE_' . $model->get_this_model_name();
3314
+				if ($this->get_one_from_cache($relation_name) instanceof $classname
3315
+					&& $this->get_one_from_cache($relation_name)->ID()
3316
+				) {
3317
+					$this->clear_cache(
3318
+						$relation_name,
3319
+						$this->get_one_from_cache($relation_name)->ID()
3320
+					);
3321
+				}
3322
+			}
3323
+		}
3324
+		$this->_props_n_values_provided_in_constructor = array();
3325
+		$properties_to_serialize = get_object_vars($this);
3326
+		// don't serialize the model. It's big and that risks recursion
3327
+		unset($properties_to_serialize['_model']);
3328
+		return array_keys($properties_to_serialize);
3329
+	}
3330
+
3331
+
3332
+	/**
3333
+	 * restore _props_n_values_provided_in_constructor
3334
+	 * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization,
3335
+	 * and therefore should NOT be used to determine if state change has occurred since initial construction.
3336
+	 * At best, you would only be able to detect if state change has occurred during THIS request.
3337
+	 */
3338
+	public function __wakeup()
3339
+	{
3340
+		$this->_props_n_values_provided_in_constructor = $this->_fields;
3341
+	}
3342
+
3343
+
3344
+	/**
3345
+	 * Usage of this magic method is to ensure any internally cached references to object instances that must remain
3346
+	 * distinct with the clone host instance are also cloned.
3347
+	 */
3348
+	public function __clone()
3349
+	{
3350
+		// handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes).
3351
+		foreach ($this->_fields as $field => $value) {
3352
+			if ($value instanceof DateTime) {
3353
+				$this->_fields[ $field ] = clone $value;
3354
+			}
3355
+		}
3356
+	}
3357 3357
 }
Please login to merge, or discard this patch.
caffeinated/admin/new/pricing/espresso_events_Pricing_Hooks.class.php 2 patches
Indentation   +2134 added lines, -2134 removed lines patch added patch discarded remove patch
@@ -15,2194 +15,2194 @@
 block discarded – undo
15 15
 class espresso_events_Pricing_Hooks extends EE_Admin_Hooks
16 16
 {
17 17
 
18
-    /**
19
-     * This property is just used to hold the status of whether an event is currently being
20
-     * created (true) or edited (false)
21
-     *
22
-     * @access protected
23
-     * @var bool
24
-     */
25
-    protected $_is_creating_event;
18
+	/**
19
+	 * This property is just used to hold the status of whether an event is currently being
20
+	 * created (true) or edited (false)
21
+	 *
22
+	 * @access protected
23
+	 * @var bool
24
+	 */
25
+	protected $_is_creating_event;
26 26
 
27
-    /**
28
-     * Used to contain the format strings for date and time that will be used for php date and
29
-     * time.
30
-     * Is set in the _set_hooks_properties() method.
31
-     *
32
-     * @var array
33
-     */
34
-    protected $_date_format_strings;
27
+	/**
28
+	 * Used to contain the format strings for date and time that will be used for php date and
29
+	 * time.
30
+	 * Is set in the _set_hooks_properties() method.
31
+	 *
32
+	 * @var array
33
+	 */
34
+	protected $_date_format_strings;
35 35
 
36
-    /**
37
-     * @var string $_date_time_format
38
-     */
39
-    protected $_date_time_format;
36
+	/**
37
+	 * @var string $_date_time_format
38
+	 */
39
+	protected $_date_time_format;
40 40
 
41 41
 
42
-    /**
43
-     * @throws InvalidArgumentException
44
-     * @throws InvalidInterfaceException
45
-     * @throws InvalidDataTypeException
46
-     */
47
-    protected function _set_hooks_properties()
48
-    {
49
-        $this->_name = 'pricing';
50
-        // capability check
51
-        if (! EE_Registry::instance()->CAP->current_user_can(
52
-            'ee_read_default_prices',
53
-            'advanced_ticket_datetime_metabox'
54
-        )) {
55
-            return;
56
-        }
57
-        $this->_setup_metaboxes();
58
-        $this->_set_date_time_formats();
59
-        $this->_validate_format_strings();
60
-        $this->_set_scripts_styles();
61
-        // commented out temporarily until logic is implemented in callback
62
-        // add_action(
63
-        //     'AHEE__EE_Admin_Page_CPT__do_extra_autosave_stuff__after_Extend_Events_Admin_Page',
64
-        //     array($this, 'autosave_handling')
65
-        // );
66
-        add_filter(
67
-            'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
68
-            array($this, 'caf_updates')
69
-        );
70
-    }
42
+	/**
43
+	 * @throws InvalidArgumentException
44
+	 * @throws InvalidInterfaceException
45
+	 * @throws InvalidDataTypeException
46
+	 */
47
+	protected function _set_hooks_properties()
48
+	{
49
+		$this->_name = 'pricing';
50
+		// capability check
51
+		if (! EE_Registry::instance()->CAP->current_user_can(
52
+			'ee_read_default_prices',
53
+			'advanced_ticket_datetime_metabox'
54
+		)) {
55
+			return;
56
+		}
57
+		$this->_setup_metaboxes();
58
+		$this->_set_date_time_formats();
59
+		$this->_validate_format_strings();
60
+		$this->_set_scripts_styles();
61
+		// commented out temporarily until logic is implemented in callback
62
+		// add_action(
63
+		//     'AHEE__EE_Admin_Page_CPT__do_extra_autosave_stuff__after_Extend_Events_Admin_Page',
64
+		//     array($this, 'autosave_handling')
65
+		// );
66
+		add_filter(
67
+			'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
68
+			array($this, 'caf_updates')
69
+		);
70
+	}
71 71
 
72 72
 
73
-    /**
74
-     * @return void
75
-     */
76
-    protected function _setup_metaboxes()
77
-    {
78
-        // if we were going to add our own metaboxes we'd use the below.
79
-        $this->_metaboxes = array(
80
-            0 => array(
81
-                'page_route' => array('edit', 'create_new'),
82
-                'func'       => 'pricing_metabox',
83
-                'label'      => esc_html__('Event Tickets & Datetimes', 'event_espresso'),
84
-                'priority'   => 'high',
85
-                'context'    => 'normal',
86
-            ),
87
-        );
88
-        $this->_remove_metaboxes = array(
89
-            0 => array(
90
-                'page_route' => array('edit', 'create_new'),
91
-                'id'         => 'espresso_event_editor_tickets',
92
-                'context'    => 'normal',
93
-            ),
94
-        );
95
-    }
73
+	/**
74
+	 * @return void
75
+	 */
76
+	protected function _setup_metaboxes()
77
+	{
78
+		// if we were going to add our own metaboxes we'd use the below.
79
+		$this->_metaboxes = array(
80
+			0 => array(
81
+				'page_route' => array('edit', 'create_new'),
82
+				'func'       => 'pricing_metabox',
83
+				'label'      => esc_html__('Event Tickets & Datetimes', 'event_espresso'),
84
+				'priority'   => 'high',
85
+				'context'    => 'normal',
86
+			),
87
+		);
88
+		$this->_remove_metaboxes = array(
89
+			0 => array(
90
+				'page_route' => array('edit', 'create_new'),
91
+				'id'         => 'espresso_event_editor_tickets',
92
+				'context'    => 'normal',
93
+			),
94
+		);
95
+	}
96 96
 
97 97
 
98
-    /**
99
-     * @return void
100
-     */
101
-    protected function _set_date_time_formats()
102
-    {
103
-        /**
104
-         * Format strings for date and time.  Defaults are existing behaviour from 4.1.
105
-         * Note, that if you return null as the value for 'date', and 'time' in the array, then
106
-         * EE will automatically use the set wp_options, 'date_format', and 'time_format'.
107
-         *
108
-         * @since 4.6.7
109
-         * @var array  Expected an array returned with 'date' and 'time' keys.
110
-         */
111
-        $this->_date_format_strings = apply_filters(
112
-            'FHEE__espresso_events_Pricing_Hooks___set_hooks_properties__date_format_strings',
113
-            array(
114
-                'date' => 'Y-m-d',
115
-                'time' => 'h:i a',
116
-            )
117
-        );
118
-        // validate
119
-        $this->_date_format_strings['date'] = isset($this->_date_format_strings['date'])
120
-            ? $this->_date_format_strings['date']
121
-            : null;
122
-        $this->_date_format_strings['time'] = isset($this->_date_format_strings['time'])
123
-            ? $this->_date_format_strings['time']
124
-            : null;
125
-        $this->_date_time_format = $this->_date_format_strings['date']
126
-                                   . ' '
127
-                                   . $this->_date_format_strings['time'];
128
-    }
98
+	/**
99
+	 * @return void
100
+	 */
101
+	protected function _set_date_time_formats()
102
+	{
103
+		/**
104
+		 * Format strings for date and time.  Defaults are existing behaviour from 4.1.
105
+		 * Note, that if you return null as the value for 'date', and 'time' in the array, then
106
+		 * EE will automatically use the set wp_options, 'date_format', and 'time_format'.
107
+		 *
108
+		 * @since 4.6.7
109
+		 * @var array  Expected an array returned with 'date' and 'time' keys.
110
+		 */
111
+		$this->_date_format_strings = apply_filters(
112
+			'FHEE__espresso_events_Pricing_Hooks___set_hooks_properties__date_format_strings',
113
+			array(
114
+				'date' => 'Y-m-d',
115
+				'time' => 'h:i a',
116
+			)
117
+		);
118
+		// validate
119
+		$this->_date_format_strings['date'] = isset($this->_date_format_strings['date'])
120
+			? $this->_date_format_strings['date']
121
+			: null;
122
+		$this->_date_format_strings['time'] = isset($this->_date_format_strings['time'])
123
+			? $this->_date_format_strings['time']
124
+			: null;
125
+		$this->_date_time_format = $this->_date_format_strings['date']
126
+								   . ' '
127
+								   . $this->_date_format_strings['time'];
128
+	}
129 129
 
130 130
 
131
-    /**
132
-     * @return void
133
-     */
134
-    protected function _validate_format_strings()
135
-    {
136
-        // validate format strings
137
-        $format_validation = EEH_DTT_Helper::validate_format_string(
138
-            $this->_date_time_format
139
-        );
140
-        if (is_array($format_validation)) {
141
-            $msg = '<p>';
142
-            $msg .= sprintf(
143
-                esc_html__(
144
-                    'The format "%s" was likely added via a filter and is invalid for the following reasons:',
145
-                    'event_espresso'
146
-                ),
147
-                $this->_date_time_format
148
-            );
149
-            $msg .= '</p><ul>';
150
-            foreach ($format_validation as $error) {
151
-                $msg .= '<li>' . $error . '</li>';
152
-            }
153
-            $msg .= '</ul><p>';
154
-            $msg .= sprintf(
155
-                esc_html__(
156
-                    '%sPlease note that your date and time formats have been reset to "Y-m-d" and "h:i a" respectively.%s',
157
-                    'event_espresso'
158
-                ),
159
-                '<span style="color:#D54E21;">',
160
-                '</span>'
161
-            );
162
-            $msg .= '</p>';
163
-            EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__);
164
-            $this->_date_format_strings = array(
165
-                'date' => 'Y-m-d',
166
-                'time' => 'h:i a',
167
-            );
168
-        }
169
-    }
131
+	/**
132
+	 * @return void
133
+	 */
134
+	protected function _validate_format_strings()
135
+	{
136
+		// validate format strings
137
+		$format_validation = EEH_DTT_Helper::validate_format_string(
138
+			$this->_date_time_format
139
+		);
140
+		if (is_array($format_validation)) {
141
+			$msg = '<p>';
142
+			$msg .= sprintf(
143
+				esc_html__(
144
+					'The format "%s" was likely added via a filter and is invalid for the following reasons:',
145
+					'event_espresso'
146
+				),
147
+				$this->_date_time_format
148
+			);
149
+			$msg .= '</p><ul>';
150
+			foreach ($format_validation as $error) {
151
+				$msg .= '<li>' . $error . '</li>';
152
+			}
153
+			$msg .= '</ul><p>';
154
+			$msg .= sprintf(
155
+				esc_html__(
156
+					'%sPlease note that your date and time formats have been reset to "Y-m-d" and "h:i a" respectively.%s',
157
+					'event_espresso'
158
+				),
159
+				'<span style="color:#D54E21;">',
160
+				'</span>'
161
+			);
162
+			$msg .= '</p>';
163
+			EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__);
164
+			$this->_date_format_strings = array(
165
+				'date' => 'Y-m-d',
166
+				'time' => 'h:i a',
167
+			);
168
+		}
169
+	}
170 170
 
171 171
 
172
-    /**
173
-     * @return void
174
-     */
175
-    protected function _set_scripts_styles()
176
-    {
177
-        $this->_scripts_styles = array(
178
-            'registers'   => array(
179
-                'ee-tickets-datetimes-css' => array(
180
-                    'url'  => PRICING_ASSETS_URL . 'event-tickets-datetimes.css',
181
-                    'type' => 'css',
182
-                ),
183
-                'ee-dtt-ticket-metabox'    => array(
184
-                    'url'     => PRICING_ASSETS_URL . 'ee-datetime-ticket-metabox.js',
185
-                    'depends' => array('ee-datepicker', 'ee-dialog', 'underscore'),
186
-                ),
187
-            ),
188
-            'deregisters' => array(
189
-                'event-editor-css'       => array('type' => 'css'),
190
-                'event-datetime-metabox' => array('type' => 'js'),
191
-            ),
192
-            'enqueues'    => array(
193
-                'ee-tickets-datetimes-css' => array('edit', 'create_new'),
194
-                'ee-dtt-ticket-metabox'    => array('edit', 'create_new'),
195
-            ),
196
-            'localize'    => array(
197
-                'ee-dtt-ticket-metabox' => array(
198
-                    'DTT_TRASH_BLOCK'       => array(
199
-                        'main_warning'            => esc_html__(
200
-                            'The Datetime you are attempting to trash is the only datetime selected for the following ticket(s):',
201
-                            'event_espresso'
202
-                        ),
203
-                        'after_warning'           => esc_html__(
204
-                            'In order to trash this datetime you must first make sure the above ticket(s) are assigned to other datetimes.',
205
-                            'event_espresso'
206
-                        ),
207
-                        'cancel_button'           => '<button class="button-secondary ee-modal-cancel">'
208
-                                                     . esc_html__('Cancel', 'event_espresso') . '</button>',
209
-                        'close_button'            => '<button class="button-secondary ee-modal-cancel">'
210
-                                                     . esc_html__('Close', 'event_espresso') . '</button>',
211
-                        'single_warning_from_tkt' => esc_html__(
212
-                            'The Datetime you are attempting to unassign from this ticket is the only remaining datetime for this ticket. Tickets must always have at least one datetime assigned to them.',
213
-                            'event_espresso'
214
-                        ),
215
-                        'single_warning_from_dtt' => esc_html__(
216
-                            'The ticket you are attempting to unassign from this datetime cannot be unassigned because the datetime is the only remaining datetime for the ticket.  Tickets must always have at least one datetime assigned to them.',
217
-                            'event_espresso'
218
-                        ),
219
-                        'dismiss_button'          => '<button class="button-secondary ee-modal-cancel">'
220
-                                                     . esc_html__('Dismiss', 'event_espresso') . '</button>',
221
-                    ),
222
-                    'DTT_ERROR_MSG'         => array(
223
-                        'no_ticket_name' => esc_html__('General Admission', 'event_espresso'),
224
-                        'dismiss_button' => '<div class="save-cancel-button-container">'
225
-                                            . '<button class="button-secondary ee-modal-cancel">'
226
-                                            . esc_html__('Dismiss', 'event_espresso')
227
-                                            . '</button></div>',
228
-                    ),
229
-                    'DTT_OVERSELL_WARNING'  => array(
230
-                        'datetime_ticket' => esc_html__(
231
-                            'You cannot add this ticket to this datetime because it has a sold amount that is greater than the amount of spots remaining for this datetime.',
232
-                            'event_espresso'
233
-                        ),
234
-                        'ticket_datetime' => esc_html__(
235
-                            'You cannot add this datetime to this ticket because the ticket has a sold amount that is greater than the amount of spots remaining on the datetime.',
236
-                            'event_espresso'
237
-                        ),
238
-                    ),
239
-                    'DTT_CONVERTED_FORMATS' => EEH_DTT_Helper::convert_php_to_js_and_moment_date_formats(
240
-                        $this->_date_format_strings['date'],
241
-                        $this->_date_format_strings['time']
242
-                    ),
243
-                    'DTT_START_OF_WEEK'     => array('dayValue' => (int) get_option('start_of_week')),
244
-                ),
245
-            ),
246
-        );
247
-    }
172
+	/**
173
+	 * @return void
174
+	 */
175
+	protected function _set_scripts_styles()
176
+	{
177
+		$this->_scripts_styles = array(
178
+			'registers'   => array(
179
+				'ee-tickets-datetimes-css' => array(
180
+					'url'  => PRICING_ASSETS_URL . 'event-tickets-datetimes.css',
181
+					'type' => 'css',
182
+				),
183
+				'ee-dtt-ticket-metabox'    => array(
184
+					'url'     => PRICING_ASSETS_URL . 'ee-datetime-ticket-metabox.js',
185
+					'depends' => array('ee-datepicker', 'ee-dialog', 'underscore'),
186
+				),
187
+			),
188
+			'deregisters' => array(
189
+				'event-editor-css'       => array('type' => 'css'),
190
+				'event-datetime-metabox' => array('type' => 'js'),
191
+			),
192
+			'enqueues'    => array(
193
+				'ee-tickets-datetimes-css' => array('edit', 'create_new'),
194
+				'ee-dtt-ticket-metabox'    => array('edit', 'create_new'),
195
+			),
196
+			'localize'    => array(
197
+				'ee-dtt-ticket-metabox' => array(
198
+					'DTT_TRASH_BLOCK'       => array(
199
+						'main_warning'            => esc_html__(
200
+							'The Datetime you are attempting to trash is the only datetime selected for the following ticket(s):',
201
+							'event_espresso'
202
+						),
203
+						'after_warning'           => esc_html__(
204
+							'In order to trash this datetime you must first make sure the above ticket(s) are assigned to other datetimes.',
205
+							'event_espresso'
206
+						),
207
+						'cancel_button'           => '<button class="button-secondary ee-modal-cancel">'
208
+													 . esc_html__('Cancel', 'event_espresso') . '</button>',
209
+						'close_button'            => '<button class="button-secondary ee-modal-cancel">'
210
+													 . esc_html__('Close', 'event_espresso') . '</button>',
211
+						'single_warning_from_tkt' => esc_html__(
212
+							'The Datetime you are attempting to unassign from this ticket is the only remaining datetime for this ticket. Tickets must always have at least one datetime assigned to them.',
213
+							'event_espresso'
214
+						),
215
+						'single_warning_from_dtt' => esc_html__(
216
+							'The ticket you are attempting to unassign from this datetime cannot be unassigned because the datetime is the only remaining datetime for the ticket.  Tickets must always have at least one datetime assigned to them.',
217
+							'event_espresso'
218
+						),
219
+						'dismiss_button'          => '<button class="button-secondary ee-modal-cancel">'
220
+													 . esc_html__('Dismiss', 'event_espresso') . '</button>',
221
+					),
222
+					'DTT_ERROR_MSG'         => array(
223
+						'no_ticket_name' => esc_html__('General Admission', 'event_espresso'),
224
+						'dismiss_button' => '<div class="save-cancel-button-container">'
225
+											. '<button class="button-secondary ee-modal-cancel">'
226
+											. esc_html__('Dismiss', 'event_espresso')
227
+											. '</button></div>',
228
+					),
229
+					'DTT_OVERSELL_WARNING'  => array(
230
+						'datetime_ticket' => esc_html__(
231
+							'You cannot add this ticket to this datetime because it has a sold amount that is greater than the amount of spots remaining for this datetime.',
232
+							'event_espresso'
233
+						),
234
+						'ticket_datetime' => esc_html__(
235
+							'You cannot add this datetime to this ticket because the ticket has a sold amount that is greater than the amount of spots remaining on the datetime.',
236
+							'event_espresso'
237
+						),
238
+					),
239
+					'DTT_CONVERTED_FORMATS' => EEH_DTT_Helper::convert_php_to_js_and_moment_date_formats(
240
+						$this->_date_format_strings['date'],
241
+						$this->_date_format_strings['time']
242
+					),
243
+					'DTT_START_OF_WEEK'     => array('dayValue' => (int) get_option('start_of_week')),
244
+				),
245
+			),
246
+		);
247
+	}
248 248
 
249 249
 
250
-    /**
251
-     * @param array $update_callbacks
252
-     * @return array
253
-     */
254
-    public function caf_updates(array $update_callbacks)
255
-    {
256
-        foreach ($update_callbacks as $key => $callback) {
257
-            if ($callback[1] === '_default_tickets_update') {
258
-                unset($update_callbacks[ $key ]);
259
-            }
260
-        }
261
-        $update_callbacks[] = array($this, 'datetime_and_tickets_caf_update');
262
-        return $update_callbacks;
263
-    }
250
+	/**
251
+	 * @param array $update_callbacks
252
+	 * @return array
253
+	 */
254
+	public function caf_updates(array $update_callbacks)
255
+	{
256
+		foreach ($update_callbacks as $key => $callback) {
257
+			if ($callback[1] === '_default_tickets_update') {
258
+				unset($update_callbacks[ $key ]);
259
+			}
260
+		}
261
+		$update_callbacks[] = array($this, 'datetime_and_tickets_caf_update');
262
+		return $update_callbacks;
263
+	}
264 264
 
265 265
 
266
-    /**
267
-     * Handles saving everything related to Tickets (datetimes, tickets, prices)
268
-     *
269
-     * @param  EE_Event $event The Event object we're attaching data to
270
-     * @param  array    $data  The request data from the form
271
-     * @throws ReflectionException
272
-     * @throws Exception
273
-     * @throws InvalidInterfaceException
274
-     * @throws InvalidDataTypeException
275
-     * @throws EE_Error
276
-     * @throws InvalidArgumentException
277
-     */
278
-    public function datetime_and_tickets_caf_update($event, $data)
279
-    {
280
-        // first we need to start with datetimes cause they are the "root" items attached to events.
281
-        $saved_datetimes = $this->_update_datetimes($event, $data);
282
-        // next tackle the tickets (and prices?)
283
-        $this->_update_tickets($event, $saved_datetimes, $data);
284
-    }
266
+	/**
267
+	 * Handles saving everything related to Tickets (datetimes, tickets, prices)
268
+	 *
269
+	 * @param  EE_Event $event The Event object we're attaching data to
270
+	 * @param  array    $data  The request data from the form
271
+	 * @throws ReflectionException
272
+	 * @throws Exception
273
+	 * @throws InvalidInterfaceException
274
+	 * @throws InvalidDataTypeException
275
+	 * @throws EE_Error
276
+	 * @throws InvalidArgumentException
277
+	 */
278
+	public function datetime_and_tickets_caf_update($event, $data)
279
+	{
280
+		// first we need to start with datetimes cause they are the "root" items attached to events.
281
+		$saved_datetimes = $this->_update_datetimes($event, $data);
282
+		// next tackle the tickets (and prices?)
283
+		$this->_update_tickets($event, $saved_datetimes, $data);
284
+	}
285 285
 
286 286
 
287
-    /**
288
-     * update event_datetimes
289
-     *
290
-     * @param  EE_Event $event Event being updated
291
-     * @param  array    $data  the request data from the form
292
-     * @return EE_Datetime[]
293
-     * @throws Exception
294
-     * @throws ReflectionException
295
-     * @throws InvalidInterfaceException
296
-     * @throws InvalidDataTypeException
297
-     * @throws InvalidArgumentException
298
-     * @throws EE_Error
299
-     */
300
-    protected function _update_datetimes($event, $data)
301
-    {
302
-        $timezone = isset($data['timezone_string']) ? $data['timezone_string'] : null;
303
-        $saved_dtt_ids = array();
304
-        $saved_dtt_objs = array();
305
-        if (empty($data['edit_event_datetimes']) || ! is_array($data['edit_event_datetimes'])) {
306
-            throw new InvalidArgumentException(
307
-                esc_html__(
308
-                    'The "edit_event_datetimes" array is invalid therefore the event can not be updated.',
309
-                    'event_espresso'
310
-                )
311
-            );
312
-        }
313
-        foreach ($data['edit_event_datetimes'] as $row => $datetime_data) {
314
-            // trim all values to ensure any excess whitespace is removed.
315
-            $datetime_data = array_map(
316
-                function ($datetime_data) {
317
-                    return is_array($datetime_data) ? $datetime_data : trim($datetime_data);
318
-                },
319
-                $datetime_data
320
-            );
321
-            $datetime_data['DTT_EVT_end'] = isset($datetime_data['DTT_EVT_end'])
322
-                                            && ! empty($datetime_data['DTT_EVT_end'])
323
-                ? $datetime_data['DTT_EVT_end']
324
-                : $datetime_data['DTT_EVT_start'];
325
-            $datetime_values = array(
326
-                'DTT_ID'          => ! empty($datetime_data['DTT_ID'])
327
-                    ? $datetime_data['DTT_ID']
328
-                    : null,
329
-                'DTT_name'        => ! empty($datetime_data['DTT_name'])
330
-                    ? $datetime_data['DTT_name']
331
-                    : '',
332
-                'DTT_description' => ! empty($datetime_data['DTT_description'])
333
-                    ? $datetime_data['DTT_description']
334
-                    : '',
335
-                'DTT_EVT_start'   => $datetime_data['DTT_EVT_start'],
336
-                'DTT_EVT_end'     => $datetime_data['DTT_EVT_end'],
337
-                'DTT_reg_limit'   => empty($datetime_data['DTT_reg_limit'])
338
-                    ? EE_INF
339
-                    : $datetime_data['DTT_reg_limit'],
340
-                'DTT_order'       => ! isset($datetime_data['DTT_order'])
341
-                    ? $row
342
-                    : $datetime_data['DTT_order'],
343
-            );
344
-            // if we have an id then let's get existing object first and then set the new values.
345
-            // Otherwise we instantiate a new object for save.
346
-            if (! empty($datetime_data['DTT_ID'])) {
347
-                $datetime = EE_Registry::instance()
348
-                                       ->load_model('Datetime', array($timezone))
349
-                                       ->get_one_by_ID($datetime_data['DTT_ID']);
350
-                // set date and time format according to what is set in this class.
351
-                $datetime->set_date_format($this->_date_format_strings['date']);
352
-                $datetime->set_time_format($this->_date_format_strings['time']);
353
-                foreach ($datetime_values as $field => $value) {
354
-                    $datetime->set($field, $value);
355
-                }
356
-                // make sure the $dtt_id here is saved just in case
357
-                // after the add_relation_to() the autosave replaces it.
358
-                // We need to do this so we dont' TRASH the parent DTT.
359
-                // (save the ID for both key and value to avoid duplications)
360
-                $saved_dtt_ids[ $datetime->ID() ] = $datetime->ID();
361
-            } else {
362
-                $datetime = EE_Registry::instance()->load_class(
363
-                    'Datetime',
364
-                    array(
365
-                        $datetime_values,
366
-                        $timezone,
367
-                        array($this->_date_format_strings['date'], $this->_date_format_strings['time']),
368
-                    ),
369
-                    false,
370
-                    false
371
-                );
372
-                foreach ($datetime_values as $field => $value) {
373
-                    $datetime->set($field, $value);
374
-                }
375
-            }
376
-            $datetime->save();
377
-            $datetime = $event->_add_relation_to($datetime, 'Datetime');
378
-            // before going any further make sure our dates are setup correctly
379
-            // so that the end date is always equal or greater than the start date.
380
-            if ($datetime->get_raw('DTT_EVT_start') > $datetime->get_raw('DTT_EVT_end')) {
381
-                $datetime->set('DTT_EVT_end', $datetime->get('DTT_EVT_start'));
382
-                $datetime = EEH_DTT_Helper::date_time_add($datetime, 'DTT_EVT_end', 'days');
383
-                $datetime->save();
384
-            }
385
-            // now we have to make sure we add the new DTT_ID to the $saved_dtt_ids array
386
-            // because it is possible there was a new one created for the autosave.
387
-            // (save the ID for both key and value to avoid duplications)
388
-            $DTT_ID = $datetime->ID();
389
-            $saved_dtt_ids[ $DTT_ID ] = $DTT_ID;
390
-            $saved_dtt_objs[ $row ] = $datetime;
391
-            // @todo if ANY of these updates fail then we want the appropriate global error message.
392
-        }
393
-        $event->save();
394
-        // now we need to REMOVE any datetimes that got deleted.
395
-        // Keep in mind that this process will only kick in for datetimes that don't have any DTT_sold on them.
396
-        // So its safe to permanently delete at this point.
397
-        $old_datetimes = explode(',', $data['datetime_IDs']);
398
-        $old_datetimes = $old_datetimes[0] === '' ? array() : $old_datetimes;
399
-        if (is_array($old_datetimes)) {
400
-            $datetimes_to_delete = array_diff($old_datetimes, $saved_dtt_ids);
401
-            foreach ($datetimes_to_delete as $id) {
402
-                $id = absint($id);
403
-                if (empty($id)) {
404
-                    continue;
405
-                }
406
-                $dtt_to_remove = EE_Registry::instance()->load_model('Datetime')->get_one_by_ID($id);
407
-                // remove tkt relationships.
408
-                $related_tickets = $dtt_to_remove->get_many_related('Ticket');
409
-                foreach ($related_tickets as $tkt) {
410
-                    $dtt_to_remove->_remove_relation_to($tkt, 'Ticket');
411
-                }
412
-                $event->_remove_relation_to($id, 'Datetime');
413
-                $dtt_to_remove->refresh_cache_of_related_objects();
414
-            }
415
-        }
416
-        return $saved_dtt_objs;
417
-    }
287
+	/**
288
+	 * update event_datetimes
289
+	 *
290
+	 * @param  EE_Event $event Event being updated
291
+	 * @param  array    $data  the request data from the form
292
+	 * @return EE_Datetime[]
293
+	 * @throws Exception
294
+	 * @throws ReflectionException
295
+	 * @throws InvalidInterfaceException
296
+	 * @throws InvalidDataTypeException
297
+	 * @throws InvalidArgumentException
298
+	 * @throws EE_Error
299
+	 */
300
+	protected function _update_datetimes($event, $data)
301
+	{
302
+		$timezone = isset($data['timezone_string']) ? $data['timezone_string'] : null;
303
+		$saved_dtt_ids = array();
304
+		$saved_dtt_objs = array();
305
+		if (empty($data['edit_event_datetimes']) || ! is_array($data['edit_event_datetimes'])) {
306
+			throw new InvalidArgumentException(
307
+				esc_html__(
308
+					'The "edit_event_datetimes" array is invalid therefore the event can not be updated.',
309
+					'event_espresso'
310
+				)
311
+			);
312
+		}
313
+		foreach ($data['edit_event_datetimes'] as $row => $datetime_data) {
314
+			// trim all values to ensure any excess whitespace is removed.
315
+			$datetime_data = array_map(
316
+				function ($datetime_data) {
317
+					return is_array($datetime_data) ? $datetime_data : trim($datetime_data);
318
+				},
319
+				$datetime_data
320
+			);
321
+			$datetime_data['DTT_EVT_end'] = isset($datetime_data['DTT_EVT_end'])
322
+											&& ! empty($datetime_data['DTT_EVT_end'])
323
+				? $datetime_data['DTT_EVT_end']
324
+				: $datetime_data['DTT_EVT_start'];
325
+			$datetime_values = array(
326
+				'DTT_ID'          => ! empty($datetime_data['DTT_ID'])
327
+					? $datetime_data['DTT_ID']
328
+					: null,
329
+				'DTT_name'        => ! empty($datetime_data['DTT_name'])
330
+					? $datetime_data['DTT_name']
331
+					: '',
332
+				'DTT_description' => ! empty($datetime_data['DTT_description'])
333
+					? $datetime_data['DTT_description']
334
+					: '',
335
+				'DTT_EVT_start'   => $datetime_data['DTT_EVT_start'],
336
+				'DTT_EVT_end'     => $datetime_data['DTT_EVT_end'],
337
+				'DTT_reg_limit'   => empty($datetime_data['DTT_reg_limit'])
338
+					? EE_INF
339
+					: $datetime_data['DTT_reg_limit'],
340
+				'DTT_order'       => ! isset($datetime_data['DTT_order'])
341
+					? $row
342
+					: $datetime_data['DTT_order'],
343
+			);
344
+			// if we have an id then let's get existing object first and then set the new values.
345
+			// Otherwise we instantiate a new object for save.
346
+			if (! empty($datetime_data['DTT_ID'])) {
347
+				$datetime = EE_Registry::instance()
348
+									   ->load_model('Datetime', array($timezone))
349
+									   ->get_one_by_ID($datetime_data['DTT_ID']);
350
+				// set date and time format according to what is set in this class.
351
+				$datetime->set_date_format($this->_date_format_strings['date']);
352
+				$datetime->set_time_format($this->_date_format_strings['time']);
353
+				foreach ($datetime_values as $field => $value) {
354
+					$datetime->set($field, $value);
355
+				}
356
+				// make sure the $dtt_id here is saved just in case
357
+				// after the add_relation_to() the autosave replaces it.
358
+				// We need to do this so we dont' TRASH the parent DTT.
359
+				// (save the ID for both key and value to avoid duplications)
360
+				$saved_dtt_ids[ $datetime->ID() ] = $datetime->ID();
361
+			} else {
362
+				$datetime = EE_Registry::instance()->load_class(
363
+					'Datetime',
364
+					array(
365
+						$datetime_values,
366
+						$timezone,
367
+						array($this->_date_format_strings['date'], $this->_date_format_strings['time']),
368
+					),
369
+					false,
370
+					false
371
+				);
372
+				foreach ($datetime_values as $field => $value) {
373
+					$datetime->set($field, $value);
374
+				}
375
+			}
376
+			$datetime->save();
377
+			$datetime = $event->_add_relation_to($datetime, 'Datetime');
378
+			// before going any further make sure our dates are setup correctly
379
+			// so that the end date is always equal or greater than the start date.
380
+			if ($datetime->get_raw('DTT_EVT_start') > $datetime->get_raw('DTT_EVT_end')) {
381
+				$datetime->set('DTT_EVT_end', $datetime->get('DTT_EVT_start'));
382
+				$datetime = EEH_DTT_Helper::date_time_add($datetime, 'DTT_EVT_end', 'days');
383
+				$datetime->save();
384
+			}
385
+			// now we have to make sure we add the new DTT_ID to the $saved_dtt_ids array
386
+			// because it is possible there was a new one created for the autosave.
387
+			// (save the ID for both key and value to avoid duplications)
388
+			$DTT_ID = $datetime->ID();
389
+			$saved_dtt_ids[ $DTT_ID ] = $DTT_ID;
390
+			$saved_dtt_objs[ $row ] = $datetime;
391
+			// @todo if ANY of these updates fail then we want the appropriate global error message.
392
+		}
393
+		$event->save();
394
+		// now we need to REMOVE any datetimes that got deleted.
395
+		// Keep in mind that this process will only kick in for datetimes that don't have any DTT_sold on them.
396
+		// So its safe to permanently delete at this point.
397
+		$old_datetimes = explode(',', $data['datetime_IDs']);
398
+		$old_datetimes = $old_datetimes[0] === '' ? array() : $old_datetimes;
399
+		if (is_array($old_datetimes)) {
400
+			$datetimes_to_delete = array_diff($old_datetimes, $saved_dtt_ids);
401
+			foreach ($datetimes_to_delete as $id) {
402
+				$id = absint($id);
403
+				if (empty($id)) {
404
+					continue;
405
+				}
406
+				$dtt_to_remove = EE_Registry::instance()->load_model('Datetime')->get_one_by_ID($id);
407
+				// remove tkt relationships.
408
+				$related_tickets = $dtt_to_remove->get_many_related('Ticket');
409
+				foreach ($related_tickets as $tkt) {
410
+					$dtt_to_remove->_remove_relation_to($tkt, 'Ticket');
411
+				}
412
+				$event->_remove_relation_to($id, 'Datetime');
413
+				$dtt_to_remove->refresh_cache_of_related_objects();
414
+			}
415
+		}
416
+		return $saved_dtt_objs;
417
+	}
418 418
 
419 419
 
420
-    /**
421
-     * update tickets
422
-     *
423
-     * @param  EE_Event      $event           Event object being updated
424
-     * @param  EE_Datetime[] $saved_datetimes an array of datetime ids being updated
425
-     * @param  array         $data            incoming request data
426
-     * @return EE_Ticket[]
427
-     * @throws Exception
428
-     * @throws ReflectionException
429
-     * @throws InvalidInterfaceException
430
-     * @throws InvalidDataTypeException
431
-     * @throws InvalidArgumentException
432
-     * @throws EE_Error
433
-     */
434
-    protected function _update_tickets($event, $saved_datetimes, $data)
435
-    {
436
-        $new_tkt = null;
437
-        $new_default = null;
438
-        // stripslashes because WP filtered the $_POST ($data) array to add slashes
439
-        $data = stripslashes_deep($data);
440
-        $timezone = isset($data['timezone_string']) ? $data['timezone_string'] : null;
441
-        $saved_tickets = $datetimes_on_existing = array();
442
-        $old_tickets = isset($data['ticket_IDs']) ? explode(',', $data['ticket_IDs']) : array();
443
-        if (empty($data['edit_tickets']) || ! is_array($data['edit_tickets'])) {
444
-            throw new InvalidArgumentException(
445
-                esc_html__(
446
-                    'The "edit_tickets" array is invalid therefore the event can not be updated.',
447
-                    'event_espresso'
448
-                )
449
-            );
450
-        }
451
-        foreach ($data['edit_tickets'] as $row => $tkt) {
452
-            $update_prices = $create_new_TKT = false;
453
-            // figure out what datetimes were added to the ticket
454
-            // and what datetimes were removed from the ticket in the session.
455
-            $starting_tkt_dtt_rows = explode(',', $data['starting_ticket_datetime_rows'][ $row ]);
456
-            $tkt_dtt_rows = explode(',', $data['ticket_datetime_rows'][ $row ]);
457
-            $datetimes_added = array_diff($tkt_dtt_rows, $starting_tkt_dtt_rows);
458
-            $datetimes_removed = array_diff($starting_tkt_dtt_rows, $tkt_dtt_rows);
459
-            // trim inputs to ensure any excess whitespace is removed.
460
-            $tkt = array_map(
461
-                function ($ticket_data) {
462
-                    return is_array($ticket_data) ? $ticket_data : trim($ticket_data);
463
-                },
464
-                $tkt
465
-            );
466
-            // note we are doing conversions to floats here instead of allowing EE_Money_Field to handle
467
-            // because we're doing calculations prior to using the models.
468
-            // note incoming ['TKT_price'] value is already in standard notation (via js).
469
-            $ticket_price = isset($tkt['TKT_price'])
470
-                ? round((float) $tkt['TKT_price'], 3)
471
-                : 0;
472
-            // note incoming base price needs converted from localized value.
473
-            $base_price = isset($tkt['TKT_base_price'])
474
-                ? EEH_Money::convert_to_float_from_localized_money($tkt['TKT_base_price'])
475
-                : 0;
476
-            // if ticket price == 0 and $base_price != 0 then ticket price == base_price
477
-            $ticket_price = $ticket_price === 0 && $base_price !== 0
478
-                ? $base_price
479
-                : $ticket_price;
480
-            $base_price_id = isset($tkt['TKT_base_price_ID'])
481
-                ? $tkt['TKT_base_price_ID']
482
-                : 0;
483
-            $price_rows = is_array($data['edit_prices']) && isset($data['edit_prices'][ $row ])
484
-                ? $data['edit_prices'][ $row ]
485
-                : array();
486
-            $now = null;
487
-            if (empty($tkt['TKT_start_date'])) {
488
-                // lets' use now in the set timezone.
489
-                $now = new DateTime('now', new DateTimeZone($event->get_timezone()));
490
-                $tkt['TKT_start_date'] = $now->format($this->_date_time_format);
491
-            }
492
-            if (empty($tkt['TKT_end_date'])) {
493
-                /**
494
-                 * set the TKT_end_date to the first datetime attached to the ticket.
495
-                 */
496
-                $first_dtt = $saved_datetimes[ reset($tkt_dtt_rows) ];
497
-                $tkt['TKT_end_date'] = $first_dtt->start_date_and_time($this->_date_time_format);
498
-            }
499
-            $TKT_values = array(
500
-                'TKT_ID'          => ! empty($tkt['TKT_ID']) ? $tkt['TKT_ID'] : null,
501
-                'TTM_ID'          => ! empty($tkt['TTM_ID']) ? $tkt['TTM_ID'] : 0,
502
-                'TKT_name'        => ! empty($tkt['TKT_name']) ? $tkt['TKT_name'] : '',
503
-                'TKT_description' => ! empty($tkt['TKT_description'])
504
-                                     && $tkt['TKT_description'] !== esc_html__(
505
-                                         'You can modify this description',
506
-                                         'event_espresso'
507
-                                     )
508
-                    ? $tkt['TKT_description']
509
-                    : '',
510
-                'TKT_start_date'  => $tkt['TKT_start_date'],
511
-                'TKT_end_date'    => $tkt['TKT_end_date'],
512
-                'TKT_qty'         => ! isset($tkt['TKT_qty']) || $tkt['TKT_qty'] === ''
513
-                    ? EE_INF
514
-                    : $tkt['TKT_qty'],
515
-                'TKT_uses'        => ! isset($tkt['TKT_uses']) || $tkt['TKT_uses'] === ''
516
-                    ? EE_INF
517
-                    : $tkt['TKT_uses'],
518
-                'TKT_min'         => empty($tkt['TKT_min']) ? 0 : $tkt['TKT_min'],
519
-                'TKT_max'         => empty($tkt['TKT_max']) ? EE_INF : $tkt['TKT_max'],
520
-                'TKT_row'         => $row,
521
-                'TKT_order'       => isset($tkt['TKT_order']) ? $tkt['TKT_order'] : 0,
522
-                'TKT_taxable'     => ! empty($tkt['TKT_taxable']) ? 1 : 0,
523
-                'TKT_required'    => ! empty($tkt['TKT_required']) ? 1 : 0,
524
-                'TKT_price'       => $ticket_price,
525
-            );
526
-            // if this is a default TKT, then we need to set the TKT_ID to 0 and update accordingly,
527
-            // which means in turn that the prices will become new prices as well.
528
-            if (isset($tkt['TKT_is_default']) && $tkt['TKT_is_default']) {
529
-                $TKT_values['TKT_ID'] = 0;
530
-                $TKT_values['TKT_is_default'] = 0;
531
-                $update_prices = true;
532
-            }
533
-            // if we have a TKT_ID then we need to get that existing TKT_obj and update it
534
-            // we actually do our saves ahead of doing any add_relations to
535
-            // because its entirely possible that this ticket wasn't removed or added to any datetime in the session
536
-            // but DID have it's items modified.
537
-            // keep in mind that if the TKT has been sold (and we have changed pricing information),
538
-            // then we won't be updating the tkt but instead a new tkt will be created and the old one archived.
539
-            if (absint($TKT_values['TKT_ID'])) {
540
-                $ticket = EE_Registry::instance()
541
-                                     ->load_model('Ticket', array($timezone))
542
-                                     ->get_one_by_ID($tkt['TKT_ID']);
543
-                if ($ticket instanceof EE_Ticket) {
544
-                    $ticket = $this->_update_ticket_datetimes(
545
-                        $ticket,
546
-                        $saved_datetimes,
547
-                        $datetimes_added,
548
-                        $datetimes_removed
549
-                    );
550
-                    // are there any registrations using this ticket ?
551
-                    $tickets_sold = $ticket->count_related(
552
-                        'Registration',
553
-                        array(
554
-                            array(
555
-                                'STS_ID' => array('NOT IN', array(EEM_Registration::status_id_incomplete)),
556
-                            ),
557
-                        )
558
-                    );
559
-                    // set ticket formats
560
-                    $ticket->set_date_format($this->_date_format_strings['date']);
561
-                    $ticket->set_time_format($this->_date_format_strings['time']);
562
-                    // let's just check the total price for the existing ticket
563
-                    // and determine if it matches the new total price.
564
-                    // if they are different then we create a new ticket (if tickets sold)
565
-                    // if they aren't different then we go ahead and modify existing ticket.
566
-                    $create_new_TKT = $tickets_sold > 0 && $ticket_price !== $ticket->price() && ! $ticket->deleted();
567
-                    // set new values
568
-                    foreach ($TKT_values as $field => $value) {
569
-                        if ($field === 'TKT_qty') {
570
-                            $ticket->set_qty($value);
571
-                        } else {
572
-                            $ticket->set($field, $value);
573
-                        }
574
-                    }
575
-                    // if $create_new_TKT is false then we can safely update the existing ticket.
576
-                    // Otherwise we have to create a new ticket.
577
-                    if ($create_new_TKT) {
578
-                        $new_tkt = $this->_duplicate_ticket(
579
-                            $ticket,
580
-                            $price_rows,
581
-                            $ticket_price,
582
-                            $base_price,
583
-                            $base_price_id
584
-                        );
585
-                    }
586
-                }
587
-            } else {
588
-                // no TKT_id so a new TKT
589
-                $ticket = EE_Ticket::new_instance(
590
-                    $TKT_values,
591
-                    $timezone,
592
-                    array($this->_date_format_strings['date'], $this->_date_format_strings['time'])
593
-                );
594
-                if ($ticket instanceof EE_Ticket) {
595
-                    // make sure ticket has an ID of setting relations won't work
596
-                    $ticket->save();
597
-                    $ticket = $this->_update_ticket_datetimes(
598
-                        $ticket,
599
-                        $saved_datetimes,
600
-                        $datetimes_added,
601
-                        $datetimes_removed
602
-                    );
603
-                    $update_prices = true;
604
-                }
605
-            }
606
-            // make sure any current values have been saved.
607
-            // $ticket->save();
608
-            // before going any further make sure our dates are setup correctly
609
-            // so that the end date is always equal or greater than the start date.
610
-            if ($ticket->get_raw('TKT_start_date') > $ticket->get_raw('TKT_end_date')) {
611
-                $ticket->set('TKT_end_date', $ticket->get('TKT_start_date'));
612
-                $ticket = EEH_DTT_Helper::date_time_add($ticket, 'TKT_end_date', 'days');
613
-            }
614
-            // let's make sure the base price is handled
615
-            $ticket = ! $create_new_TKT
616
-                ? $this->_add_prices_to_ticket(
617
-                    array(),
618
-                    $ticket,
619
-                    $update_prices,
620
-                    $base_price,
621
-                    $base_price_id
622
-                )
623
-                : $ticket;
624
-            // add/update price_modifiers
625
-            $ticket = ! $create_new_TKT
626
-                ? $this->_add_prices_to_ticket($price_rows, $ticket, $update_prices)
627
-                : $ticket;
628
-            // need to make sue that the TKT_price is accurate after saving the prices.
629
-            $ticket->ensure_TKT_Price_correct();
630
-            // handle CREATING a default tkt from the incoming tkt but ONLY if this isn't an autosave.
631
-            if (! defined('DOING_AUTOSAVE') && ! empty($tkt['TKT_is_default_selector'])) {
632
-                $update_prices = true;
633
-                $new_default = clone $ticket;
634
-                $new_default->set('TKT_ID', 0);
635
-                $new_default->set('TKT_is_default', 1);
636
-                $new_default->set('TKT_row', 1);
637
-                $new_default->set('TKT_price', $ticket_price);
638
-                // remove any dtt relations cause we DON'T want dtt relations attached
639
-                // (note this is just removing the cached relations in the object)
640
-                $new_default->_remove_relations('Datetime');
641
-                // @todo we need to add the current attached prices as new prices to the new default ticket.
642
-                $new_default = $this->_add_prices_to_ticket(
643
-                    $price_rows,
644
-                    $new_default,
645
-                    $update_prices
646
-                );
647
-                // don't forget the base price!
648
-                $new_default = $this->_add_prices_to_ticket(
649
-                    array(),
650
-                    $new_default,
651
-                    $update_prices,
652
-                    $base_price,
653
-                    $base_price_id
654
-                );
655
-                $new_default->save();
656
-                do_action(
657
-                    'AHEE__espresso_events_Pricing_Hooks___update_tkts_new_default_ticket',
658
-                    $new_default,
659
-                    $row,
660
-                    $ticket,
661
-                    $data
662
-                );
663
-            }
664
-            // DO ALL dtt relationships for both current tickets and any archived tickets
665
-            // for the given dtt that are related to the current ticket.
666
-            // TODO... not sure exactly how we're going to do this considering we don't know
667
-            // what current ticket the archived tickets are related to
668
-            // (and TKT_parent is used for autosaves so that's not a field we can reliably use).
669
-            // let's assign any tickets that have been setup to the saved_tickets tracker
670
-            // save existing TKT
671
-            $ticket->save();
672
-            if ($create_new_TKT && $new_tkt instanceof EE_Ticket) {
673
-                // save new TKT
674
-                $new_tkt->save();
675
-                // add new ticket to array
676
-                $saved_tickets[ $new_tkt->ID() ] = $new_tkt;
677
-                do_action(
678
-                    'AHEE__espresso_events_Pricing_Hooks___update_tkts_new_ticket',
679
-                    $new_tkt,
680
-                    $row,
681
-                    $tkt,
682
-                    $data
683
-                );
684
-            } else {
685
-                // add tkt to saved tkts
686
-                $saved_tickets[ $ticket->ID() ] = $ticket;
687
-                do_action(
688
-                    'AHEE__espresso_events_Pricing_Hooks___update_tkts_update_ticket',
689
-                    $ticket,
690
-                    $row,
691
-                    $tkt,
692
-                    $data
693
-                );
694
-            }
695
-        }
696
-        // now we need to handle tickets actually "deleted permanently".
697
-        // There are cases where we'd want this to happen
698
-        // (i.e. autosaves are happening and then in between autosaves the user trashes a ticket).
699
-        // Or a draft event was saved and in the process of editing a ticket is trashed.
700
-        // No sense in keeping all the related data in the db!
701
-        $old_tickets = isset($old_tickets[0]) && $old_tickets[0] === '' ? array() : $old_tickets;
702
-        $tickets_removed = array_diff($old_tickets, array_keys($saved_tickets));
703
-        foreach ($tickets_removed as $id) {
704
-            $id = absint($id);
705
-            // get the ticket for this id
706
-            $tkt_to_remove = EE_Registry::instance()->load_model('Ticket')->get_one_by_ID($id);
707
-            // if this tkt is a default tkt we leave it alone cause it won't be attached to the datetime
708
-            if ($tkt_to_remove->get('TKT_is_default')) {
709
-                continue;
710
-            }
711
-            // if this tkt has any registrations attached so then we just ARCHIVE
712
-            // because we don't actually permanently delete these tickets.
713
-            if ($tkt_to_remove->count_related('Registration') > 0) {
714
-                $tkt_to_remove->delete();
715
-                continue;
716
-            }
717
-            // need to get all the related datetimes on this ticket and remove from every single one of them
718
-            // (remember this process can ONLY kick off if there are NO tkts_sold)
719
-            $datetimes = $tkt_to_remove->get_many_related('Datetime');
720
-            foreach ($datetimes as $datetime) {
721
-                $tkt_to_remove->_remove_relation_to($datetime, 'Datetime');
722
-            }
723
-            // need to do the same for prices (except these prices can also be deleted because again,
724
-            // tickets can only be trashed if they don't have any TKTs sold (otherwise they are just archived))
725
-            $tkt_to_remove->delete_related_permanently('Price');
726
-            do_action('AHEE__espresso_events_Pricing_Hooks___update_tkts_delete_ticket', $tkt_to_remove);
727
-            // finally let's delete this ticket
728
-            // (which should not be blocked at this point b/c we've removed all our relationships)
729
-            $tkt_to_remove->delete_permanently();
730
-        }
731
-        return $saved_tickets;
732
-    }
420
+	/**
421
+	 * update tickets
422
+	 *
423
+	 * @param  EE_Event      $event           Event object being updated
424
+	 * @param  EE_Datetime[] $saved_datetimes an array of datetime ids being updated
425
+	 * @param  array         $data            incoming request data
426
+	 * @return EE_Ticket[]
427
+	 * @throws Exception
428
+	 * @throws ReflectionException
429
+	 * @throws InvalidInterfaceException
430
+	 * @throws InvalidDataTypeException
431
+	 * @throws InvalidArgumentException
432
+	 * @throws EE_Error
433
+	 */
434
+	protected function _update_tickets($event, $saved_datetimes, $data)
435
+	{
436
+		$new_tkt = null;
437
+		$new_default = null;
438
+		// stripslashes because WP filtered the $_POST ($data) array to add slashes
439
+		$data = stripslashes_deep($data);
440
+		$timezone = isset($data['timezone_string']) ? $data['timezone_string'] : null;
441
+		$saved_tickets = $datetimes_on_existing = array();
442
+		$old_tickets = isset($data['ticket_IDs']) ? explode(',', $data['ticket_IDs']) : array();
443
+		if (empty($data['edit_tickets']) || ! is_array($data['edit_tickets'])) {
444
+			throw new InvalidArgumentException(
445
+				esc_html__(
446
+					'The "edit_tickets" array is invalid therefore the event can not be updated.',
447
+					'event_espresso'
448
+				)
449
+			);
450
+		}
451
+		foreach ($data['edit_tickets'] as $row => $tkt) {
452
+			$update_prices = $create_new_TKT = false;
453
+			// figure out what datetimes were added to the ticket
454
+			// and what datetimes were removed from the ticket in the session.
455
+			$starting_tkt_dtt_rows = explode(',', $data['starting_ticket_datetime_rows'][ $row ]);
456
+			$tkt_dtt_rows = explode(',', $data['ticket_datetime_rows'][ $row ]);
457
+			$datetimes_added = array_diff($tkt_dtt_rows, $starting_tkt_dtt_rows);
458
+			$datetimes_removed = array_diff($starting_tkt_dtt_rows, $tkt_dtt_rows);
459
+			// trim inputs to ensure any excess whitespace is removed.
460
+			$tkt = array_map(
461
+				function ($ticket_data) {
462
+					return is_array($ticket_data) ? $ticket_data : trim($ticket_data);
463
+				},
464
+				$tkt
465
+			);
466
+			// note we are doing conversions to floats here instead of allowing EE_Money_Field to handle
467
+			// because we're doing calculations prior to using the models.
468
+			// note incoming ['TKT_price'] value is already in standard notation (via js).
469
+			$ticket_price = isset($tkt['TKT_price'])
470
+				? round((float) $tkt['TKT_price'], 3)
471
+				: 0;
472
+			// note incoming base price needs converted from localized value.
473
+			$base_price = isset($tkt['TKT_base_price'])
474
+				? EEH_Money::convert_to_float_from_localized_money($tkt['TKT_base_price'])
475
+				: 0;
476
+			// if ticket price == 0 and $base_price != 0 then ticket price == base_price
477
+			$ticket_price = $ticket_price === 0 && $base_price !== 0
478
+				? $base_price
479
+				: $ticket_price;
480
+			$base_price_id = isset($tkt['TKT_base_price_ID'])
481
+				? $tkt['TKT_base_price_ID']
482
+				: 0;
483
+			$price_rows = is_array($data['edit_prices']) && isset($data['edit_prices'][ $row ])
484
+				? $data['edit_prices'][ $row ]
485
+				: array();
486
+			$now = null;
487
+			if (empty($tkt['TKT_start_date'])) {
488
+				// lets' use now in the set timezone.
489
+				$now = new DateTime('now', new DateTimeZone($event->get_timezone()));
490
+				$tkt['TKT_start_date'] = $now->format($this->_date_time_format);
491
+			}
492
+			if (empty($tkt['TKT_end_date'])) {
493
+				/**
494
+				 * set the TKT_end_date to the first datetime attached to the ticket.
495
+				 */
496
+				$first_dtt = $saved_datetimes[ reset($tkt_dtt_rows) ];
497
+				$tkt['TKT_end_date'] = $first_dtt->start_date_and_time($this->_date_time_format);
498
+			}
499
+			$TKT_values = array(
500
+				'TKT_ID'          => ! empty($tkt['TKT_ID']) ? $tkt['TKT_ID'] : null,
501
+				'TTM_ID'          => ! empty($tkt['TTM_ID']) ? $tkt['TTM_ID'] : 0,
502
+				'TKT_name'        => ! empty($tkt['TKT_name']) ? $tkt['TKT_name'] : '',
503
+				'TKT_description' => ! empty($tkt['TKT_description'])
504
+									 && $tkt['TKT_description'] !== esc_html__(
505
+										 'You can modify this description',
506
+										 'event_espresso'
507
+									 )
508
+					? $tkt['TKT_description']
509
+					: '',
510
+				'TKT_start_date'  => $tkt['TKT_start_date'],
511
+				'TKT_end_date'    => $tkt['TKT_end_date'],
512
+				'TKT_qty'         => ! isset($tkt['TKT_qty']) || $tkt['TKT_qty'] === ''
513
+					? EE_INF
514
+					: $tkt['TKT_qty'],
515
+				'TKT_uses'        => ! isset($tkt['TKT_uses']) || $tkt['TKT_uses'] === ''
516
+					? EE_INF
517
+					: $tkt['TKT_uses'],
518
+				'TKT_min'         => empty($tkt['TKT_min']) ? 0 : $tkt['TKT_min'],
519
+				'TKT_max'         => empty($tkt['TKT_max']) ? EE_INF : $tkt['TKT_max'],
520
+				'TKT_row'         => $row,
521
+				'TKT_order'       => isset($tkt['TKT_order']) ? $tkt['TKT_order'] : 0,
522
+				'TKT_taxable'     => ! empty($tkt['TKT_taxable']) ? 1 : 0,
523
+				'TKT_required'    => ! empty($tkt['TKT_required']) ? 1 : 0,
524
+				'TKT_price'       => $ticket_price,
525
+			);
526
+			// if this is a default TKT, then we need to set the TKT_ID to 0 and update accordingly,
527
+			// which means in turn that the prices will become new prices as well.
528
+			if (isset($tkt['TKT_is_default']) && $tkt['TKT_is_default']) {
529
+				$TKT_values['TKT_ID'] = 0;
530
+				$TKT_values['TKT_is_default'] = 0;
531
+				$update_prices = true;
532
+			}
533
+			// if we have a TKT_ID then we need to get that existing TKT_obj and update it
534
+			// we actually do our saves ahead of doing any add_relations to
535
+			// because its entirely possible that this ticket wasn't removed or added to any datetime in the session
536
+			// but DID have it's items modified.
537
+			// keep in mind that if the TKT has been sold (and we have changed pricing information),
538
+			// then we won't be updating the tkt but instead a new tkt will be created and the old one archived.
539
+			if (absint($TKT_values['TKT_ID'])) {
540
+				$ticket = EE_Registry::instance()
541
+									 ->load_model('Ticket', array($timezone))
542
+									 ->get_one_by_ID($tkt['TKT_ID']);
543
+				if ($ticket instanceof EE_Ticket) {
544
+					$ticket = $this->_update_ticket_datetimes(
545
+						$ticket,
546
+						$saved_datetimes,
547
+						$datetimes_added,
548
+						$datetimes_removed
549
+					);
550
+					// are there any registrations using this ticket ?
551
+					$tickets_sold = $ticket->count_related(
552
+						'Registration',
553
+						array(
554
+							array(
555
+								'STS_ID' => array('NOT IN', array(EEM_Registration::status_id_incomplete)),
556
+							),
557
+						)
558
+					);
559
+					// set ticket formats
560
+					$ticket->set_date_format($this->_date_format_strings['date']);
561
+					$ticket->set_time_format($this->_date_format_strings['time']);
562
+					// let's just check the total price for the existing ticket
563
+					// and determine if it matches the new total price.
564
+					// if they are different then we create a new ticket (if tickets sold)
565
+					// if they aren't different then we go ahead and modify existing ticket.
566
+					$create_new_TKT = $tickets_sold > 0 && $ticket_price !== $ticket->price() && ! $ticket->deleted();
567
+					// set new values
568
+					foreach ($TKT_values as $field => $value) {
569
+						if ($field === 'TKT_qty') {
570
+							$ticket->set_qty($value);
571
+						} else {
572
+							$ticket->set($field, $value);
573
+						}
574
+					}
575
+					// if $create_new_TKT is false then we can safely update the existing ticket.
576
+					// Otherwise we have to create a new ticket.
577
+					if ($create_new_TKT) {
578
+						$new_tkt = $this->_duplicate_ticket(
579
+							$ticket,
580
+							$price_rows,
581
+							$ticket_price,
582
+							$base_price,
583
+							$base_price_id
584
+						);
585
+					}
586
+				}
587
+			} else {
588
+				// no TKT_id so a new TKT
589
+				$ticket = EE_Ticket::new_instance(
590
+					$TKT_values,
591
+					$timezone,
592
+					array($this->_date_format_strings['date'], $this->_date_format_strings['time'])
593
+				);
594
+				if ($ticket instanceof EE_Ticket) {
595
+					// make sure ticket has an ID of setting relations won't work
596
+					$ticket->save();
597
+					$ticket = $this->_update_ticket_datetimes(
598
+						$ticket,
599
+						$saved_datetimes,
600
+						$datetimes_added,
601
+						$datetimes_removed
602
+					);
603
+					$update_prices = true;
604
+				}
605
+			}
606
+			// make sure any current values have been saved.
607
+			// $ticket->save();
608
+			// before going any further make sure our dates are setup correctly
609
+			// so that the end date is always equal or greater than the start date.
610
+			if ($ticket->get_raw('TKT_start_date') > $ticket->get_raw('TKT_end_date')) {
611
+				$ticket->set('TKT_end_date', $ticket->get('TKT_start_date'));
612
+				$ticket = EEH_DTT_Helper::date_time_add($ticket, 'TKT_end_date', 'days');
613
+			}
614
+			// let's make sure the base price is handled
615
+			$ticket = ! $create_new_TKT
616
+				? $this->_add_prices_to_ticket(
617
+					array(),
618
+					$ticket,
619
+					$update_prices,
620
+					$base_price,
621
+					$base_price_id
622
+				)
623
+				: $ticket;
624
+			// add/update price_modifiers
625
+			$ticket = ! $create_new_TKT
626
+				? $this->_add_prices_to_ticket($price_rows, $ticket, $update_prices)
627
+				: $ticket;
628
+			// need to make sue that the TKT_price is accurate after saving the prices.
629
+			$ticket->ensure_TKT_Price_correct();
630
+			// handle CREATING a default tkt from the incoming tkt but ONLY if this isn't an autosave.
631
+			if (! defined('DOING_AUTOSAVE') && ! empty($tkt['TKT_is_default_selector'])) {
632
+				$update_prices = true;
633
+				$new_default = clone $ticket;
634
+				$new_default->set('TKT_ID', 0);
635
+				$new_default->set('TKT_is_default', 1);
636
+				$new_default->set('TKT_row', 1);
637
+				$new_default->set('TKT_price', $ticket_price);
638
+				// remove any dtt relations cause we DON'T want dtt relations attached
639
+				// (note this is just removing the cached relations in the object)
640
+				$new_default->_remove_relations('Datetime');
641
+				// @todo we need to add the current attached prices as new prices to the new default ticket.
642
+				$new_default = $this->_add_prices_to_ticket(
643
+					$price_rows,
644
+					$new_default,
645
+					$update_prices
646
+				);
647
+				// don't forget the base price!
648
+				$new_default = $this->_add_prices_to_ticket(
649
+					array(),
650
+					$new_default,
651
+					$update_prices,
652
+					$base_price,
653
+					$base_price_id
654
+				);
655
+				$new_default->save();
656
+				do_action(
657
+					'AHEE__espresso_events_Pricing_Hooks___update_tkts_new_default_ticket',
658
+					$new_default,
659
+					$row,
660
+					$ticket,
661
+					$data
662
+				);
663
+			}
664
+			// DO ALL dtt relationships for both current tickets and any archived tickets
665
+			// for the given dtt that are related to the current ticket.
666
+			// TODO... not sure exactly how we're going to do this considering we don't know
667
+			// what current ticket the archived tickets are related to
668
+			// (and TKT_parent is used for autosaves so that's not a field we can reliably use).
669
+			// let's assign any tickets that have been setup to the saved_tickets tracker
670
+			// save existing TKT
671
+			$ticket->save();
672
+			if ($create_new_TKT && $new_tkt instanceof EE_Ticket) {
673
+				// save new TKT
674
+				$new_tkt->save();
675
+				// add new ticket to array
676
+				$saved_tickets[ $new_tkt->ID() ] = $new_tkt;
677
+				do_action(
678
+					'AHEE__espresso_events_Pricing_Hooks___update_tkts_new_ticket',
679
+					$new_tkt,
680
+					$row,
681
+					$tkt,
682
+					$data
683
+				);
684
+			} else {
685
+				// add tkt to saved tkts
686
+				$saved_tickets[ $ticket->ID() ] = $ticket;
687
+				do_action(
688
+					'AHEE__espresso_events_Pricing_Hooks___update_tkts_update_ticket',
689
+					$ticket,
690
+					$row,
691
+					$tkt,
692
+					$data
693
+				);
694
+			}
695
+		}
696
+		// now we need to handle tickets actually "deleted permanently".
697
+		// There are cases where we'd want this to happen
698
+		// (i.e. autosaves are happening and then in between autosaves the user trashes a ticket).
699
+		// Or a draft event was saved and in the process of editing a ticket is trashed.
700
+		// No sense in keeping all the related data in the db!
701
+		$old_tickets = isset($old_tickets[0]) && $old_tickets[0] === '' ? array() : $old_tickets;
702
+		$tickets_removed = array_diff($old_tickets, array_keys($saved_tickets));
703
+		foreach ($tickets_removed as $id) {
704
+			$id = absint($id);
705
+			// get the ticket for this id
706
+			$tkt_to_remove = EE_Registry::instance()->load_model('Ticket')->get_one_by_ID($id);
707
+			// if this tkt is a default tkt we leave it alone cause it won't be attached to the datetime
708
+			if ($tkt_to_remove->get('TKT_is_default')) {
709
+				continue;
710
+			}
711
+			// if this tkt has any registrations attached so then we just ARCHIVE
712
+			// because we don't actually permanently delete these tickets.
713
+			if ($tkt_to_remove->count_related('Registration') > 0) {
714
+				$tkt_to_remove->delete();
715
+				continue;
716
+			}
717
+			// need to get all the related datetimes on this ticket and remove from every single one of them
718
+			// (remember this process can ONLY kick off if there are NO tkts_sold)
719
+			$datetimes = $tkt_to_remove->get_many_related('Datetime');
720
+			foreach ($datetimes as $datetime) {
721
+				$tkt_to_remove->_remove_relation_to($datetime, 'Datetime');
722
+			}
723
+			// need to do the same for prices (except these prices can also be deleted because again,
724
+			// tickets can only be trashed if they don't have any TKTs sold (otherwise they are just archived))
725
+			$tkt_to_remove->delete_related_permanently('Price');
726
+			do_action('AHEE__espresso_events_Pricing_Hooks___update_tkts_delete_ticket', $tkt_to_remove);
727
+			// finally let's delete this ticket
728
+			// (which should not be blocked at this point b/c we've removed all our relationships)
729
+			$tkt_to_remove->delete_permanently();
730
+		}
731
+		return $saved_tickets;
732
+	}
733 733
 
734 734
 
735
-    /**
736
-     * @access  protected
737
-     * @param EE_Ticket      $ticket
738
-     * @param \EE_Datetime[] $saved_datetimes
739
-     * @param \EE_Datetime[] $added_datetimes
740
-     * @param \EE_Datetime[] $removed_datetimes
741
-     * @return EE_Ticket
742
-     * @throws EE_Error
743
-     */
744
-    protected function _update_ticket_datetimes(
745
-        EE_Ticket $ticket,
746
-        $saved_datetimes = array(),
747
-        $added_datetimes = array(),
748
-        $removed_datetimes = array()
749
-    ) {
750
-        // to start we have to add the ticket to all the datetimes its supposed to be with,
751
-        // and removing the ticket from datetimes it got removed from.
752
-        // first let's add datetimes
753
-        if (! empty($added_datetimes) && is_array($added_datetimes)) {
754
-            foreach ($added_datetimes as $row_id) {
755
-                $row_id = (int) $row_id;
756
-                if (isset($saved_datetimes[ $row_id ]) && $saved_datetimes[ $row_id ] instanceof EE_Datetime) {
757
-                    $ticket->_add_relation_to($saved_datetimes[ $row_id ], 'Datetime');
758
-                    // Is this an existing ticket (has an ID) and does it have any sold?
759
-                    // If so, then we need to add that to the DTT sold because this DTT is getting added.
760
-                    if ($ticket->ID() && $ticket->sold() > 0) {
761
-                        $saved_datetimes[ $row_id ]->increaseSold($ticket->sold(), false);
762
-                    }
763
-                }
764
-            }
765
-        }
766
-        // then remove datetimes
767
-        if (! empty($removed_datetimes) && is_array($removed_datetimes)) {
768
-            foreach ($removed_datetimes as $row_id) {
769
-                $row_id = (int) $row_id;
770
-                // its entirely possible that a datetime got deleted (instead of just removed from relationship.
771
-                // So make sure we skip over this if the dtt isn't in the $saved_datetimes array)
772
-                if (isset($saved_datetimes[ $row_id ]) && $saved_datetimes[ $row_id ] instanceof EE_Datetime) {
773
-                    $ticket->_remove_relation_to($saved_datetimes[ $row_id ], 'Datetime');
774
-                    // Is this an existing ticket (has an ID) and does it have any sold?
775
-                    // If so, then we need to remove it's sold from the DTT_sold.
776
-                    if ($ticket->ID() && $ticket->sold() > 0) {
777
-                        $saved_datetimes[ $row_id ]->decreaseSold($ticket->sold());
778
-                    }
779
-                }
780
-            }
781
-        }
782
-        // cap ticket qty by datetime reg limits
783
-        $ticket->set_qty(min($ticket->qty(), $ticket->qty('reg_limit')));
784
-        return $ticket;
785
-    }
735
+	/**
736
+	 * @access  protected
737
+	 * @param EE_Ticket      $ticket
738
+	 * @param \EE_Datetime[] $saved_datetimes
739
+	 * @param \EE_Datetime[] $added_datetimes
740
+	 * @param \EE_Datetime[] $removed_datetimes
741
+	 * @return EE_Ticket
742
+	 * @throws EE_Error
743
+	 */
744
+	protected function _update_ticket_datetimes(
745
+		EE_Ticket $ticket,
746
+		$saved_datetimes = array(),
747
+		$added_datetimes = array(),
748
+		$removed_datetimes = array()
749
+	) {
750
+		// to start we have to add the ticket to all the datetimes its supposed to be with,
751
+		// and removing the ticket from datetimes it got removed from.
752
+		// first let's add datetimes
753
+		if (! empty($added_datetimes) && is_array($added_datetimes)) {
754
+			foreach ($added_datetimes as $row_id) {
755
+				$row_id = (int) $row_id;
756
+				if (isset($saved_datetimes[ $row_id ]) && $saved_datetimes[ $row_id ] instanceof EE_Datetime) {
757
+					$ticket->_add_relation_to($saved_datetimes[ $row_id ], 'Datetime');
758
+					// Is this an existing ticket (has an ID) and does it have any sold?
759
+					// If so, then we need to add that to the DTT sold because this DTT is getting added.
760
+					if ($ticket->ID() && $ticket->sold() > 0) {
761
+						$saved_datetimes[ $row_id ]->increaseSold($ticket->sold(), false);
762
+					}
763
+				}
764
+			}
765
+		}
766
+		// then remove datetimes
767
+		if (! empty($removed_datetimes) && is_array($removed_datetimes)) {
768
+			foreach ($removed_datetimes as $row_id) {
769
+				$row_id = (int) $row_id;
770
+				// its entirely possible that a datetime got deleted (instead of just removed from relationship.
771
+				// So make sure we skip over this if the dtt isn't in the $saved_datetimes array)
772
+				if (isset($saved_datetimes[ $row_id ]) && $saved_datetimes[ $row_id ] instanceof EE_Datetime) {
773
+					$ticket->_remove_relation_to($saved_datetimes[ $row_id ], 'Datetime');
774
+					// Is this an existing ticket (has an ID) and does it have any sold?
775
+					// If so, then we need to remove it's sold from the DTT_sold.
776
+					if ($ticket->ID() && $ticket->sold() > 0) {
777
+						$saved_datetimes[ $row_id ]->decreaseSold($ticket->sold());
778
+					}
779
+				}
780
+			}
781
+		}
782
+		// cap ticket qty by datetime reg limits
783
+		$ticket->set_qty(min($ticket->qty(), $ticket->qty('reg_limit')));
784
+		return $ticket;
785
+	}
786 786
 
787 787
 
788
-    /**
789
-     * @access  protected
790
-     * @param EE_Ticket $ticket
791
-     * @param array     $price_rows
792
-     * @param int       $ticket_price
793
-     * @param int       $base_price
794
-     * @param int       $base_price_id
795
-     * @return EE_Ticket
796
-     * @throws ReflectionException
797
-     * @throws InvalidArgumentException
798
-     * @throws InvalidInterfaceException
799
-     * @throws InvalidDataTypeException
800
-     * @throws EE_Error
801
-     */
802
-    protected function _duplicate_ticket(
803
-        EE_Ticket $ticket,
804
-        $price_rows = array(),
805
-        $ticket_price = 0,
806
-        $base_price = 0,
807
-        $base_price_id = 0
808
-    ) {
809
-        // create new ticket that's a copy of the existing
810
-        // except a new id of course (and not archived)
811
-        // AND has the new TKT_price associated with it.
812
-        $new_ticket = clone $ticket;
813
-        $new_ticket->set('TKT_ID', 0);
814
-        $new_ticket->set_deleted(0);
815
-        $new_ticket->set_price($ticket_price);
816
-        $new_ticket->set_sold(0);
817
-        // let's get a new ID for this ticket
818
-        $new_ticket->save();
819
-        // we also need to make sure this new ticket gets the same datetime attachments as the archived ticket
820
-        $datetimes_on_existing = $ticket->datetimes();
821
-        $new_ticket = $this->_update_ticket_datetimes(
822
-            $new_ticket,
823
-            $datetimes_on_existing,
824
-            array_keys($datetimes_on_existing)
825
-        );
826
-        // $ticket will get archived later b/c we are NOT adding it to the saved_tickets array.
827
-        // if existing $ticket has sold amount, then we need to adjust the qty for the new TKT to = the remaining
828
-        // available.
829
-        if ($ticket->sold() > 0) {
830
-            $new_qty = $ticket->qty() - $ticket->sold();
831
-            $new_ticket->set_qty($new_qty);
832
-        }
833
-        // now we update the prices just for this ticket
834
-        $new_ticket = $this->_add_prices_to_ticket($price_rows, $new_ticket, true);
835
-        // and we update the base price
836
-        $new_ticket = $this->_add_prices_to_ticket(
837
-            array(),
838
-            $new_ticket,
839
-            true,
840
-            $base_price,
841
-            $base_price_id
842
-        );
843
-        return $new_ticket;
844
-    }
788
+	/**
789
+	 * @access  protected
790
+	 * @param EE_Ticket $ticket
791
+	 * @param array     $price_rows
792
+	 * @param int       $ticket_price
793
+	 * @param int       $base_price
794
+	 * @param int       $base_price_id
795
+	 * @return EE_Ticket
796
+	 * @throws ReflectionException
797
+	 * @throws InvalidArgumentException
798
+	 * @throws InvalidInterfaceException
799
+	 * @throws InvalidDataTypeException
800
+	 * @throws EE_Error
801
+	 */
802
+	protected function _duplicate_ticket(
803
+		EE_Ticket $ticket,
804
+		$price_rows = array(),
805
+		$ticket_price = 0,
806
+		$base_price = 0,
807
+		$base_price_id = 0
808
+	) {
809
+		// create new ticket that's a copy of the existing
810
+		// except a new id of course (and not archived)
811
+		// AND has the new TKT_price associated with it.
812
+		$new_ticket = clone $ticket;
813
+		$new_ticket->set('TKT_ID', 0);
814
+		$new_ticket->set_deleted(0);
815
+		$new_ticket->set_price($ticket_price);
816
+		$new_ticket->set_sold(0);
817
+		// let's get a new ID for this ticket
818
+		$new_ticket->save();
819
+		// we also need to make sure this new ticket gets the same datetime attachments as the archived ticket
820
+		$datetimes_on_existing = $ticket->datetimes();
821
+		$new_ticket = $this->_update_ticket_datetimes(
822
+			$new_ticket,
823
+			$datetimes_on_existing,
824
+			array_keys($datetimes_on_existing)
825
+		);
826
+		// $ticket will get archived later b/c we are NOT adding it to the saved_tickets array.
827
+		// if existing $ticket has sold amount, then we need to adjust the qty for the new TKT to = the remaining
828
+		// available.
829
+		if ($ticket->sold() > 0) {
830
+			$new_qty = $ticket->qty() - $ticket->sold();
831
+			$new_ticket->set_qty($new_qty);
832
+		}
833
+		// now we update the prices just for this ticket
834
+		$new_ticket = $this->_add_prices_to_ticket($price_rows, $new_ticket, true);
835
+		// and we update the base price
836
+		$new_ticket = $this->_add_prices_to_ticket(
837
+			array(),
838
+			$new_ticket,
839
+			true,
840
+			$base_price,
841
+			$base_price_id
842
+		);
843
+		return $new_ticket;
844
+	}
845 845
 
846 846
 
847
-    /**
848
-     * This attaches a list of given prices to a ticket.
849
-     * Note we dont' have to worry about ever removing relationships (or archiving prices) because if there is a change
850
-     * in price information on a ticket, a new ticket is created anyways so the archived ticket will retain the old
851
-     * price info and prices are automatically "archived" via the ticket.
852
-     *
853
-     * @access  private
854
-     * @param array     $prices        Array of prices from the form.
855
-     * @param EE_Ticket $ticket        EE_Ticket object that prices are being attached to.
856
-     * @param bool      $new_prices    Whether attach existing incoming prices or create new ones.
857
-     * @param int|bool  $base_price    if FALSE then NOT doing a base price add.
858
-     * @param int|bool  $base_price_id if present then this is the base_price_id being updated.
859
-     * @return EE_Ticket
860
-     * @throws ReflectionException
861
-     * @throws InvalidArgumentException
862
-     * @throws InvalidInterfaceException
863
-     * @throws InvalidDataTypeException
864
-     * @throws EE_Error
865
-     */
866
-    protected function _add_prices_to_ticket(
867
-        $prices = array(),
868
-        EE_Ticket $ticket,
869
-        $new_prices = false,
870
-        $base_price = false,
871
-        $base_price_id = false
872
-    ) {
873
-        // let's just get any current prices that may exist on the given ticket
874
-        // so we can remove any prices that got trashed in this session.
875
-        $current_prices_on_ticket = $base_price !== false
876
-            ? $ticket->base_price(true)
877
-            : $ticket->price_modifiers();
878
-        $updated_prices = array();
879
-        // if $base_price ! FALSE then updating a base price.
880
-        if ($base_price !== false) {
881
-            $prices[1] = array(
882
-                'PRC_ID'     => $new_prices || $base_price_id === 1 ? null : $base_price_id,
883
-                'PRT_ID'     => 1,
884
-                'PRC_amount' => $base_price,
885
-                'PRC_name'   => $ticket->get('TKT_name'),
886
-                'PRC_desc'   => $ticket->get('TKT_description'),
887
-            );
888
-        }
889
-        // possibly need to save tkt
890
-        if (! $ticket->ID()) {
891
-            $ticket->save();
892
-        }
893
-        foreach ($prices as $row => $prc) {
894
-            $prt_id = ! empty($prc['PRT_ID']) ? $prc['PRT_ID'] : null;
895
-            if (empty($prt_id)) {
896
-                continue;
897
-            } //prices MUST have a price type id.
898
-            $PRC_values = array(
899
-                'PRC_ID'         => ! empty($prc['PRC_ID']) ? $prc['PRC_ID'] : null,
900
-                'PRT_ID'         => $prt_id,
901
-                'PRC_amount'     => ! empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0,
902
-                'PRC_name'       => ! empty($prc['PRC_name']) ? $prc['PRC_name'] : '',
903
-                'PRC_desc'       => ! empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '',
904
-                'PRC_is_default' => false,
905
-                // make sure we set PRC_is_default to false for all ticket saves from event_editor
906
-                'PRC_order'      => $row,
907
-            );
908
-            if ($new_prices || empty($PRC_values['PRC_ID'])) {
909
-                $PRC_values['PRC_ID'] = 0;
910
-                $price = EE_Registry::instance()->load_class(
911
-                    'Price',
912
-                    array($PRC_values),
913
-                    false,
914
-                    false
915
-                );
916
-            } else {
917
-                $price = EE_Registry::instance()->load_model('Price')->get_one_by_ID($prc['PRC_ID']);
918
-                // update this price with new values
919
-                foreach ($PRC_values as $field => $value) {
920
-                    $price->set($field, $value);
921
-                }
922
-            }
923
-            $price->save();
924
-            $updated_prices[ $price->ID() ] = $price;
925
-            $ticket->_add_relation_to($price, 'Price');
926
-        }
927
-        // now let's remove any prices that got removed from the ticket
928
-        if (! empty($current_prices_on_ticket)) {
929
-            $current = array_keys($current_prices_on_ticket);
930
-            $updated = array_keys($updated_prices);
931
-            $prices_to_remove = array_diff($current, $updated);
932
-            if (! empty($prices_to_remove)) {
933
-                foreach ($prices_to_remove as $prc_id) {
934
-                    $p = $current_prices_on_ticket[ $prc_id ];
935
-                    $ticket->_remove_relation_to($p, 'Price');
936
-                    // delete permanently the price
937
-                    $p->delete_permanently();
938
-                }
939
-            }
940
-        }
941
-        return $ticket;
942
-    }
847
+	/**
848
+	 * This attaches a list of given prices to a ticket.
849
+	 * Note we dont' have to worry about ever removing relationships (or archiving prices) because if there is a change
850
+	 * in price information on a ticket, a new ticket is created anyways so the archived ticket will retain the old
851
+	 * price info and prices are automatically "archived" via the ticket.
852
+	 *
853
+	 * @access  private
854
+	 * @param array     $prices        Array of prices from the form.
855
+	 * @param EE_Ticket $ticket        EE_Ticket object that prices are being attached to.
856
+	 * @param bool      $new_prices    Whether attach existing incoming prices or create new ones.
857
+	 * @param int|bool  $base_price    if FALSE then NOT doing a base price add.
858
+	 * @param int|bool  $base_price_id if present then this is the base_price_id being updated.
859
+	 * @return EE_Ticket
860
+	 * @throws ReflectionException
861
+	 * @throws InvalidArgumentException
862
+	 * @throws InvalidInterfaceException
863
+	 * @throws InvalidDataTypeException
864
+	 * @throws EE_Error
865
+	 */
866
+	protected function _add_prices_to_ticket(
867
+		$prices = array(),
868
+		EE_Ticket $ticket,
869
+		$new_prices = false,
870
+		$base_price = false,
871
+		$base_price_id = false
872
+	) {
873
+		// let's just get any current prices that may exist on the given ticket
874
+		// so we can remove any prices that got trashed in this session.
875
+		$current_prices_on_ticket = $base_price !== false
876
+			? $ticket->base_price(true)
877
+			: $ticket->price_modifiers();
878
+		$updated_prices = array();
879
+		// if $base_price ! FALSE then updating a base price.
880
+		if ($base_price !== false) {
881
+			$prices[1] = array(
882
+				'PRC_ID'     => $new_prices || $base_price_id === 1 ? null : $base_price_id,
883
+				'PRT_ID'     => 1,
884
+				'PRC_amount' => $base_price,
885
+				'PRC_name'   => $ticket->get('TKT_name'),
886
+				'PRC_desc'   => $ticket->get('TKT_description'),
887
+			);
888
+		}
889
+		// possibly need to save tkt
890
+		if (! $ticket->ID()) {
891
+			$ticket->save();
892
+		}
893
+		foreach ($prices as $row => $prc) {
894
+			$prt_id = ! empty($prc['PRT_ID']) ? $prc['PRT_ID'] : null;
895
+			if (empty($prt_id)) {
896
+				continue;
897
+			} //prices MUST have a price type id.
898
+			$PRC_values = array(
899
+				'PRC_ID'         => ! empty($prc['PRC_ID']) ? $prc['PRC_ID'] : null,
900
+				'PRT_ID'         => $prt_id,
901
+				'PRC_amount'     => ! empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0,
902
+				'PRC_name'       => ! empty($prc['PRC_name']) ? $prc['PRC_name'] : '',
903
+				'PRC_desc'       => ! empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '',
904
+				'PRC_is_default' => false,
905
+				// make sure we set PRC_is_default to false for all ticket saves from event_editor
906
+				'PRC_order'      => $row,
907
+			);
908
+			if ($new_prices || empty($PRC_values['PRC_ID'])) {
909
+				$PRC_values['PRC_ID'] = 0;
910
+				$price = EE_Registry::instance()->load_class(
911
+					'Price',
912
+					array($PRC_values),
913
+					false,
914
+					false
915
+				);
916
+			} else {
917
+				$price = EE_Registry::instance()->load_model('Price')->get_one_by_ID($prc['PRC_ID']);
918
+				// update this price with new values
919
+				foreach ($PRC_values as $field => $value) {
920
+					$price->set($field, $value);
921
+				}
922
+			}
923
+			$price->save();
924
+			$updated_prices[ $price->ID() ] = $price;
925
+			$ticket->_add_relation_to($price, 'Price');
926
+		}
927
+		// now let's remove any prices that got removed from the ticket
928
+		if (! empty($current_prices_on_ticket)) {
929
+			$current = array_keys($current_prices_on_ticket);
930
+			$updated = array_keys($updated_prices);
931
+			$prices_to_remove = array_diff($current, $updated);
932
+			if (! empty($prices_to_remove)) {
933
+				foreach ($prices_to_remove as $prc_id) {
934
+					$p = $current_prices_on_ticket[ $prc_id ];
935
+					$ticket->_remove_relation_to($p, 'Price');
936
+					// delete permanently the price
937
+					$p->delete_permanently();
938
+				}
939
+			}
940
+		}
941
+		return $ticket;
942
+	}
943 943
 
944 944
 
945
-    /**
946
-     * @param Events_Admin_Page $event_admin_obj
947
-     * @return Events_Admin_Page
948
-     */
949
-    public function autosave_handling(Events_Admin_Page $event_admin_obj)
950
-    {
951
-        return $event_admin_obj;
952
-        // doing nothing for the moment.
953
-        // todo when I get to this remember that I need to set the template args on the $event_admin_obj
954
-        // (use the set_template_args() method)
955
-        /**
956
-         * need to remember to handle TICKET DEFAULT saves correctly:  I've got two input fields in the dom:
957
-         * 1. TKT_is_default_selector (visible)
958
-         * 2. TKT_is_default (hidden)
959
-         * I think we'll use the TKT_is_default for recording whether the ticket displayed IS a default ticket
960
-         * (on new event creations). Whereas the TKT_is_default_selector is for the user to indicate they want
961
-         * this ticket to be saved as a default.
962
-         * The tricky part is, on an initial display on create or edit (or after manually updating),
963
-         * the TKT_is_default_selector will always be unselected and the TKT_is_default will only be true
964
-         * if this is a create.  However, after an autosave, users will want some sort of indicator that
965
-         * the TKT HAS been saved as a default..
966
-         * in other words we don't want to remove the check on TKT_is_default_selector. So here's what I'm thinking.
967
-         * On Autosave:
968
-         * 1. If TKT_is_default is true: we create a new TKT, send back the new id and add id to related elements,
969
-         * then set the TKT_is_default to false.
970
-         * 2. If TKT_is_default_selector is true: we create/edit existing ticket (following conditions above as well).
971
-         *  We do NOT create a new default ticket.  The checkbox stays selected after autosave.
972
-         * 3. only on MANUAL update do we check for the selection and if selected create the new default ticket.
973
-         */
974
-    }
945
+	/**
946
+	 * @param Events_Admin_Page $event_admin_obj
947
+	 * @return Events_Admin_Page
948
+	 */
949
+	public function autosave_handling(Events_Admin_Page $event_admin_obj)
950
+	{
951
+		return $event_admin_obj;
952
+		// doing nothing for the moment.
953
+		// todo when I get to this remember that I need to set the template args on the $event_admin_obj
954
+		// (use the set_template_args() method)
955
+		/**
956
+		 * need to remember to handle TICKET DEFAULT saves correctly:  I've got two input fields in the dom:
957
+		 * 1. TKT_is_default_selector (visible)
958
+		 * 2. TKT_is_default (hidden)
959
+		 * I think we'll use the TKT_is_default for recording whether the ticket displayed IS a default ticket
960
+		 * (on new event creations). Whereas the TKT_is_default_selector is for the user to indicate they want
961
+		 * this ticket to be saved as a default.
962
+		 * The tricky part is, on an initial display on create or edit (or after manually updating),
963
+		 * the TKT_is_default_selector will always be unselected and the TKT_is_default will only be true
964
+		 * if this is a create.  However, after an autosave, users will want some sort of indicator that
965
+		 * the TKT HAS been saved as a default..
966
+		 * in other words we don't want to remove the check on TKT_is_default_selector. So here's what I'm thinking.
967
+		 * On Autosave:
968
+		 * 1. If TKT_is_default is true: we create a new TKT, send back the new id and add id to related elements,
969
+		 * then set the TKT_is_default to false.
970
+		 * 2. If TKT_is_default_selector is true: we create/edit existing ticket (following conditions above as well).
971
+		 *  We do NOT create a new default ticket.  The checkbox stays selected after autosave.
972
+		 * 3. only on MANUAL update do we check for the selection and if selected create the new default ticket.
973
+		 */
974
+	}
975 975
 
976 976
 
977
-    /**
978
-     * @throws ReflectionException
979
-     * @throws InvalidArgumentException
980
-     * @throws InvalidInterfaceException
981
-     * @throws InvalidDataTypeException
982
-     * @throws DomainException
983
-     * @throws EE_Error
984
-     */
985
-    public function pricing_metabox()
986
-    {
987
-        $existing_datetime_ids = $existing_ticket_ids = $datetime_tickets = $ticket_datetimes = array();
988
-        $event = $this->_adminpage_obj->get_cpt_model_obj();
989
-        // set is_creating_event property.
990
-        $EVT_ID = $event->ID();
991
-        $this->_is_creating_event = empty($this->_req_data['post']);
992
-        // default main template args
993
-        $main_template_args = array(
994
-            'event_datetime_help_link' => EEH_Template::get_help_tab_link(
995
-                'event_editor_event_datetimes_help_tab',
996
-                $this->_adminpage_obj->page_slug,
997
-                $this->_adminpage_obj->get_req_action(),
998
-                false,
999
-                false
1000
-            ),
1001
-            // todo need to add a filter to the template for the help text
1002
-            // in the Events_Admin_Page core file so we can add further help
1003
-            'existing_datetime_ids'    => '',
1004
-            'total_dtt_rows'           => 1,
1005
-            'add_new_dtt_help_link'    => EEH_Template::get_help_tab_link(
1006
-                'add_new_dtt_info',
1007
-                $this->_adminpage_obj->page_slug,
1008
-                $this->_adminpage_obj->get_req_action(),
1009
-                false,
1010
-                false
1011
-            ),
1012
-            // todo need to add this help info id to the Events_Admin_Page core file so we can access it here.
1013
-            'datetime_rows'            => '',
1014
-            'show_tickets_container'   => '',
1015
-            // $this->_adminpage_obj->get_cpt_model_obj()->ID() > 1 ? ' style="display:none;"' : '',
1016
-            'ticket_rows'              => '',
1017
-            'existing_ticket_ids'      => '',
1018
-            'total_ticket_rows'        => 1,
1019
-            'ticket_js_structure'      => '',
1020
-            'ee_collapsible_status'    => ' ee-collapsible-open'
1021
-            // $this->_adminpage_obj->get_cpt_model_obj()->ID() > 0 ? ' ee-collapsible-closed' : ' ee-collapsible-open'
1022
-        );
1023
-        $timezone = $event instanceof EE_Event ? $event->timezone_string() : null;
1024
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1025
-        /**
1026
-         * 1. Start with retrieving Datetimes
1027
-         * 2. For each datetime get related tickets
1028
-         * 3. For each ticket get related prices
1029
-         */
1030
-        /** @var EEM_Datetime $datetime_model */
1031
-        $datetime_model = EE_Registry::instance()->load_model('Datetime', array($timezone));
1032
-        $datetimes = $datetime_model->get_all_event_dates($EVT_ID);
1033
-        $main_template_args['total_dtt_rows'] = count($datetimes);
1034
-        /**
1035
-         * @see https://events.codebasehq.com/projects/event-espresso/tickets/9486
1036
-         * for why we are counting $datetime_row and then setting that on the Datetime object
1037
-         */
1038
-        $datetime_row = 1;
1039
-        foreach ($datetimes as $datetime) {
1040
-            $DTT_ID = $datetime->get('DTT_ID');
1041
-            $datetime->set('DTT_order', $datetime_row);
1042
-            $existing_datetime_ids[] = $DTT_ID;
1043
-            // tickets attached
1044
-            $related_tickets = $datetime->ID() > 0
1045
-                ? $datetime->get_many_related(
1046
-                    'Ticket',
1047
-                    array(
1048
-                        array(
1049
-                            'OR' => array('TKT_deleted' => 1, 'TKT_deleted*' => 0),
1050
-                        ),
1051
-                        'default_where_conditions' => 'none',
1052
-                        'order_by'                 => array('TKT_order' => 'ASC'),
1053
-                    )
1054
-                )
1055
-                : array();
1056
-            // if there are no related tickets this is likely a new event OR autodraft
1057
-            // event so we need to generate the default tickets because datetimes
1058
-            // ALWAYS have at least one related ticket!!.  EXCEPT, we dont' do this if there is already more than one
1059
-            // datetime on the event.
1060
-            if (empty($related_tickets) && count($datetimes) < 2) {
1061
-                /** @var EEM_Ticket $ticket_model */
1062
-                $ticket_model = EE_Registry::instance()->load_model('Ticket');
1063
-                $related_tickets = $ticket_model->get_all_default_tickets();
1064
-                // this should be ordered by TKT_ID, so let's grab the first default ticket
1065
-                // (which will be the main default) and ensure it has any default prices added to it (but do NOT save).
1066
-                $default_prices = EEM_Price::instance()->get_all_default_prices();
1067
-                $main_default_ticket = reset($related_tickets);
1068
-                if ($main_default_ticket instanceof EE_Ticket) {
1069
-                    foreach ($default_prices as $default_price) {
1070
-                        if ($default_price instanceof EE_Price && $default_price->is_base_price()) {
1071
-                            continue;
1072
-                        }
1073
-                        $main_default_ticket->cache('Price', $default_price);
1074
-                    }
1075
-                }
1076
-            }
1077
-            // we can't actually setup rows in this loop yet cause we don't know all
1078
-            // the unique tickets for this event yet (tickets are linked through all datetimes).
1079
-            // So we're going to temporarily cache some of that information.
1080
-            // loop through and setup the ticket rows and make sure the order is set.
1081
-            foreach ($related_tickets as $ticket) {
1082
-                $TKT_ID = $ticket->get('TKT_ID');
1083
-                $ticket_row = $ticket->get('TKT_row');
1084
-                // we only want unique tickets in our final display!!
1085
-                if (! in_array($TKT_ID, $existing_ticket_ids, true)) {
1086
-                    $existing_ticket_ids[] = $TKT_ID;
1087
-                    $all_tickets[] = $ticket;
1088
-                }
1089
-                // temporary cache of this ticket info for this datetime for later processing of datetime rows.
1090
-                $datetime_tickets[ $DTT_ID ][] = $ticket_row;
1091
-                // temporary cache of this datetime info for this ticket for later processing of ticket rows.
1092
-                if (! isset($ticket_datetimes[ $TKT_ID ])
1093
-                    || ! in_array($datetime_row, $ticket_datetimes[ $TKT_ID ], true)
1094
-                ) {
1095
-                    $ticket_datetimes[ $TKT_ID ][] = $datetime_row;
1096
-                }
1097
-            }
1098
-            $datetime_row++;
1099
-        }
1100
-        $main_template_args['total_ticket_rows'] = count($existing_ticket_ids);
1101
-        $main_template_args['existing_ticket_ids'] = implode(',', $existing_ticket_ids);
1102
-        $main_template_args['existing_datetime_ids'] = implode(',', $existing_datetime_ids);
1103
-        // sort $all_tickets by order
1104
-        usort(
1105
-            $all_tickets,
1106
-            function (EE_Ticket $a, EE_Ticket $b) {
1107
-                $a_order = (int) $a->get('TKT_order');
1108
-                $b_order = (int) $b->get('TKT_order');
1109
-                if ($a_order === $b_order) {
1110
-                    return 0;
1111
-                }
1112
-                return ($a_order < $b_order) ? -1 : 1;
1113
-            }
1114
-        );
1115
-        // k NOW we have all the data we need for setting up the dtt rows
1116
-        // and ticket rows so we start our dtt loop again.
1117
-        $datetime_row = 1;
1118
-        foreach ($datetimes as $datetime) {
1119
-            $main_template_args['datetime_rows'] .= $this->_get_datetime_row(
1120
-                $datetime_row,
1121
-                $datetime,
1122
-                $datetime_tickets,
1123
-                $all_tickets,
1124
-                false,
1125
-                $datetimes
1126
-            );
1127
-            $datetime_row++;
1128
-        }
1129
-        // then loop through all tickets for the ticket rows.
1130
-        $ticket_row = 1;
1131
-        foreach ($all_tickets as $ticket) {
1132
-            $main_template_args['ticket_rows'] .= $this->_get_ticket_row(
1133
-                $ticket_row,
1134
-                $ticket,
1135
-                $ticket_datetimes,
1136
-                $datetimes,
1137
-                false,
1138
-                $all_tickets
1139
-            );
1140
-            $ticket_row++;
1141
-        }
1142
-        $main_template_args['ticket_js_structure'] = $this->_get_ticket_js_structure($datetimes, $all_tickets);
1143
-        EEH_Template::display_template(
1144
-            PRICING_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php',
1145
-            $main_template_args
1146
-        );
1147
-    }
977
+	/**
978
+	 * @throws ReflectionException
979
+	 * @throws InvalidArgumentException
980
+	 * @throws InvalidInterfaceException
981
+	 * @throws InvalidDataTypeException
982
+	 * @throws DomainException
983
+	 * @throws EE_Error
984
+	 */
985
+	public function pricing_metabox()
986
+	{
987
+		$existing_datetime_ids = $existing_ticket_ids = $datetime_tickets = $ticket_datetimes = array();
988
+		$event = $this->_adminpage_obj->get_cpt_model_obj();
989
+		// set is_creating_event property.
990
+		$EVT_ID = $event->ID();
991
+		$this->_is_creating_event = empty($this->_req_data['post']);
992
+		// default main template args
993
+		$main_template_args = array(
994
+			'event_datetime_help_link' => EEH_Template::get_help_tab_link(
995
+				'event_editor_event_datetimes_help_tab',
996
+				$this->_adminpage_obj->page_slug,
997
+				$this->_adminpage_obj->get_req_action(),
998
+				false,
999
+				false
1000
+			),
1001
+			// todo need to add a filter to the template for the help text
1002
+			// in the Events_Admin_Page core file so we can add further help
1003
+			'existing_datetime_ids'    => '',
1004
+			'total_dtt_rows'           => 1,
1005
+			'add_new_dtt_help_link'    => EEH_Template::get_help_tab_link(
1006
+				'add_new_dtt_info',
1007
+				$this->_adminpage_obj->page_slug,
1008
+				$this->_adminpage_obj->get_req_action(),
1009
+				false,
1010
+				false
1011
+			),
1012
+			// todo need to add this help info id to the Events_Admin_Page core file so we can access it here.
1013
+			'datetime_rows'            => '',
1014
+			'show_tickets_container'   => '',
1015
+			// $this->_adminpage_obj->get_cpt_model_obj()->ID() > 1 ? ' style="display:none;"' : '',
1016
+			'ticket_rows'              => '',
1017
+			'existing_ticket_ids'      => '',
1018
+			'total_ticket_rows'        => 1,
1019
+			'ticket_js_structure'      => '',
1020
+			'ee_collapsible_status'    => ' ee-collapsible-open'
1021
+			// $this->_adminpage_obj->get_cpt_model_obj()->ID() > 0 ? ' ee-collapsible-closed' : ' ee-collapsible-open'
1022
+		);
1023
+		$timezone = $event instanceof EE_Event ? $event->timezone_string() : null;
1024
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1025
+		/**
1026
+		 * 1. Start with retrieving Datetimes
1027
+		 * 2. For each datetime get related tickets
1028
+		 * 3. For each ticket get related prices
1029
+		 */
1030
+		/** @var EEM_Datetime $datetime_model */
1031
+		$datetime_model = EE_Registry::instance()->load_model('Datetime', array($timezone));
1032
+		$datetimes = $datetime_model->get_all_event_dates($EVT_ID);
1033
+		$main_template_args['total_dtt_rows'] = count($datetimes);
1034
+		/**
1035
+		 * @see https://events.codebasehq.com/projects/event-espresso/tickets/9486
1036
+		 * for why we are counting $datetime_row and then setting that on the Datetime object
1037
+		 */
1038
+		$datetime_row = 1;
1039
+		foreach ($datetimes as $datetime) {
1040
+			$DTT_ID = $datetime->get('DTT_ID');
1041
+			$datetime->set('DTT_order', $datetime_row);
1042
+			$existing_datetime_ids[] = $DTT_ID;
1043
+			// tickets attached
1044
+			$related_tickets = $datetime->ID() > 0
1045
+				? $datetime->get_many_related(
1046
+					'Ticket',
1047
+					array(
1048
+						array(
1049
+							'OR' => array('TKT_deleted' => 1, 'TKT_deleted*' => 0),
1050
+						),
1051
+						'default_where_conditions' => 'none',
1052
+						'order_by'                 => array('TKT_order' => 'ASC'),
1053
+					)
1054
+				)
1055
+				: array();
1056
+			// if there are no related tickets this is likely a new event OR autodraft
1057
+			// event so we need to generate the default tickets because datetimes
1058
+			// ALWAYS have at least one related ticket!!.  EXCEPT, we dont' do this if there is already more than one
1059
+			// datetime on the event.
1060
+			if (empty($related_tickets) && count($datetimes) < 2) {
1061
+				/** @var EEM_Ticket $ticket_model */
1062
+				$ticket_model = EE_Registry::instance()->load_model('Ticket');
1063
+				$related_tickets = $ticket_model->get_all_default_tickets();
1064
+				// this should be ordered by TKT_ID, so let's grab the first default ticket
1065
+				// (which will be the main default) and ensure it has any default prices added to it (but do NOT save).
1066
+				$default_prices = EEM_Price::instance()->get_all_default_prices();
1067
+				$main_default_ticket = reset($related_tickets);
1068
+				if ($main_default_ticket instanceof EE_Ticket) {
1069
+					foreach ($default_prices as $default_price) {
1070
+						if ($default_price instanceof EE_Price && $default_price->is_base_price()) {
1071
+							continue;
1072
+						}
1073
+						$main_default_ticket->cache('Price', $default_price);
1074
+					}
1075
+				}
1076
+			}
1077
+			// we can't actually setup rows in this loop yet cause we don't know all
1078
+			// the unique tickets for this event yet (tickets are linked through all datetimes).
1079
+			// So we're going to temporarily cache some of that information.
1080
+			// loop through and setup the ticket rows and make sure the order is set.
1081
+			foreach ($related_tickets as $ticket) {
1082
+				$TKT_ID = $ticket->get('TKT_ID');
1083
+				$ticket_row = $ticket->get('TKT_row');
1084
+				// we only want unique tickets in our final display!!
1085
+				if (! in_array($TKT_ID, $existing_ticket_ids, true)) {
1086
+					$existing_ticket_ids[] = $TKT_ID;
1087
+					$all_tickets[] = $ticket;
1088
+				}
1089
+				// temporary cache of this ticket info for this datetime for later processing of datetime rows.
1090
+				$datetime_tickets[ $DTT_ID ][] = $ticket_row;
1091
+				// temporary cache of this datetime info for this ticket for later processing of ticket rows.
1092
+				if (! isset($ticket_datetimes[ $TKT_ID ])
1093
+					|| ! in_array($datetime_row, $ticket_datetimes[ $TKT_ID ], true)
1094
+				) {
1095
+					$ticket_datetimes[ $TKT_ID ][] = $datetime_row;
1096
+				}
1097
+			}
1098
+			$datetime_row++;
1099
+		}
1100
+		$main_template_args['total_ticket_rows'] = count($existing_ticket_ids);
1101
+		$main_template_args['existing_ticket_ids'] = implode(',', $existing_ticket_ids);
1102
+		$main_template_args['existing_datetime_ids'] = implode(',', $existing_datetime_ids);
1103
+		// sort $all_tickets by order
1104
+		usort(
1105
+			$all_tickets,
1106
+			function (EE_Ticket $a, EE_Ticket $b) {
1107
+				$a_order = (int) $a->get('TKT_order');
1108
+				$b_order = (int) $b->get('TKT_order');
1109
+				if ($a_order === $b_order) {
1110
+					return 0;
1111
+				}
1112
+				return ($a_order < $b_order) ? -1 : 1;
1113
+			}
1114
+		);
1115
+		// k NOW we have all the data we need for setting up the dtt rows
1116
+		// and ticket rows so we start our dtt loop again.
1117
+		$datetime_row = 1;
1118
+		foreach ($datetimes as $datetime) {
1119
+			$main_template_args['datetime_rows'] .= $this->_get_datetime_row(
1120
+				$datetime_row,
1121
+				$datetime,
1122
+				$datetime_tickets,
1123
+				$all_tickets,
1124
+				false,
1125
+				$datetimes
1126
+			);
1127
+			$datetime_row++;
1128
+		}
1129
+		// then loop through all tickets for the ticket rows.
1130
+		$ticket_row = 1;
1131
+		foreach ($all_tickets as $ticket) {
1132
+			$main_template_args['ticket_rows'] .= $this->_get_ticket_row(
1133
+				$ticket_row,
1134
+				$ticket,
1135
+				$ticket_datetimes,
1136
+				$datetimes,
1137
+				false,
1138
+				$all_tickets
1139
+			);
1140
+			$ticket_row++;
1141
+		}
1142
+		$main_template_args['ticket_js_structure'] = $this->_get_ticket_js_structure($datetimes, $all_tickets);
1143
+		EEH_Template::display_template(
1144
+			PRICING_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php',
1145
+			$main_template_args
1146
+		);
1147
+	}
1148 1148
 
1149 1149
 
1150
-    /**
1151
-     * @param int         $datetime_row
1152
-     * @param EE_Datetime $datetime
1153
-     * @param array       $datetime_tickets
1154
-     * @param array       $all_tickets
1155
-     * @param bool        $default
1156
-     * @param array       $all_datetimes
1157
-     * @return mixed
1158
-     * @throws DomainException
1159
-     * @throws EE_Error
1160
-     */
1161
-    protected function _get_datetime_row(
1162
-        $datetime_row,
1163
-        EE_Datetime $datetime,
1164
-        $datetime_tickets = array(),
1165
-        $all_tickets = array(),
1166
-        $default = false,
1167
-        $all_datetimes = array()
1168
-    ) {
1169
-        $dtt_display_template_args = array(
1170
-            'dtt_edit_row'             => $this->_get_dtt_edit_row(
1171
-                $datetime_row,
1172
-                $datetime,
1173
-                $default,
1174
-                $all_datetimes
1175
-            ),
1176
-            'dtt_attached_tickets_row' => $this->_get_dtt_attached_tickets_row(
1177
-                $datetime_row,
1178
-                $datetime,
1179
-                $datetime_tickets,
1180
-                $all_tickets,
1181
-                $default
1182
-            ),
1183
-            'dtt_row'                  => $default ? 'DTTNUM' : $datetime_row,
1184
-        );
1185
-        return EEH_Template::display_template(
1186
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_row_wrapper.template.php',
1187
-            $dtt_display_template_args,
1188
-            true
1189
-        );
1190
-    }
1150
+	/**
1151
+	 * @param int         $datetime_row
1152
+	 * @param EE_Datetime $datetime
1153
+	 * @param array       $datetime_tickets
1154
+	 * @param array       $all_tickets
1155
+	 * @param bool        $default
1156
+	 * @param array       $all_datetimes
1157
+	 * @return mixed
1158
+	 * @throws DomainException
1159
+	 * @throws EE_Error
1160
+	 */
1161
+	protected function _get_datetime_row(
1162
+		$datetime_row,
1163
+		EE_Datetime $datetime,
1164
+		$datetime_tickets = array(),
1165
+		$all_tickets = array(),
1166
+		$default = false,
1167
+		$all_datetimes = array()
1168
+	) {
1169
+		$dtt_display_template_args = array(
1170
+			'dtt_edit_row'             => $this->_get_dtt_edit_row(
1171
+				$datetime_row,
1172
+				$datetime,
1173
+				$default,
1174
+				$all_datetimes
1175
+			),
1176
+			'dtt_attached_tickets_row' => $this->_get_dtt_attached_tickets_row(
1177
+				$datetime_row,
1178
+				$datetime,
1179
+				$datetime_tickets,
1180
+				$all_tickets,
1181
+				$default
1182
+			),
1183
+			'dtt_row'                  => $default ? 'DTTNUM' : $datetime_row,
1184
+		);
1185
+		return EEH_Template::display_template(
1186
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_row_wrapper.template.php',
1187
+			$dtt_display_template_args,
1188
+			true
1189
+		);
1190
+	}
1191 1191
 
1192 1192
 
1193
-    /**
1194
-     * This method is used to generate a dtt fields  edit row.
1195
-     * The same row is used to generate a row with valid DTT objects
1196
-     * and the default row that is used as the skeleton by the js.
1197
-     *
1198
-     * @param int           $datetime_row  The row number for the row being generated.
1199
-     * @param EE_Datetime   $datetime
1200
-     * @param bool          $default       Whether a default row is being generated or not.
1201
-     * @param EE_Datetime[] $all_datetimes This is the array of all datetimes used in the editor.
1202
-     * @return string
1203
-     * @throws DomainException
1204
-     * @throws EE_Error
1205
-     */
1206
-    protected function _get_dtt_edit_row($datetime_row, $datetime, $default, $all_datetimes)
1207
-    {
1208
-        // if the incoming $datetime object is NOT an instance of EE_Datetime then force default to true.
1209
-        $default = ! $datetime instanceof EE_Datetime ? true : $default;
1210
-        $template_args = array(
1211
-            'dtt_row'              => $default ? 'DTTNUM' : $datetime_row,
1212
-            'event_datetimes_name' => $default ? 'DTTNAMEATTR' : 'edit_event_datetimes',
1213
-            'edit_dtt_expanded'    => '',
1214
-            'DTT_ID'               => $default ? '' : $datetime->ID(),
1215
-            'DTT_name'             => $default ? '' : $datetime->get_f('DTT_name'),
1216
-            'DTT_description'      => $default ? '' : $datetime->get_f('DTT_description'),
1217
-            'DTT_EVT_start'        => $default ? '' : $datetime->start_date($this->_date_time_format),
1218
-            'DTT_EVT_end'          => $default ? '' : $datetime->end_date($this->_date_time_format),
1219
-            'DTT_reg_limit'        => $default
1220
-                ? ''
1221
-                : $datetime->get_pretty(
1222
-                    'DTT_reg_limit',
1223
-                    'input'
1224
-                ),
1225
-            'DTT_order'            => $default ? 'DTTNUM' : $datetime_row,
1226
-            'dtt_sold'             => $default ? '0' : $datetime->get('DTT_sold'),
1227
-            'dtt_reserved'         => $default ? '0' : $datetime->reserved(),
1228
-            'clone_icon'           => ! empty($datetime) && $datetime->get('DTT_sold') > 0
1229
-                ? ''
1230
-                : 'clone-icon ee-icon ee-icon-clone clickable',
1231
-            'trash_icon'           => ! empty($datetime) && $datetime->get('DTT_sold') > 0
1232
-                ? 'ee-lock-icon'
1233
-                : 'trash-icon dashicons dashicons-post-trash clickable',
1234
-            'reg_list_url'         => $default || ! $datetime->event() instanceof \EE_Event
1235
-                ? ''
1236
-                : EE_Admin_Page::add_query_args_and_nonce(
1237
-                    array('event_id' => $datetime->event()->ID(), 'datetime_id' => $datetime->ID()),
1238
-                    REG_ADMIN_URL
1239
-                ),
1240
-        );
1241
-        $template_args['show_trash'] = count($all_datetimes) === 1 && $template_args['trash_icon'] !== 'ee-lock-icon'
1242
-            ? ' style="display:none"'
1243
-            : '';
1244
-        // allow filtering of template args at this point.
1245
-        $template_args = apply_filters(
1246
-            'FHEE__espresso_events_Pricing_Hooks___get_dtt_edit_row__template_args',
1247
-            $template_args,
1248
-            $datetime_row,
1249
-            $datetime,
1250
-            $default,
1251
-            $all_datetimes,
1252
-            $this->_is_creating_event
1253
-        );
1254
-        return EEH_Template::display_template(
1255
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_edit_row.template.php',
1256
-            $template_args,
1257
-            true
1258
-        );
1259
-    }
1193
+	/**
1194
+	 * This method is used to generate a dtt fields  edit row.
1195
+	 * The same row is used to generate a row with valid DTT objects
1196
+	 * and the default row that is used as the skeleton by the js.
1197
+	 *
1198
+	 * @param int           $datetime_row  The row number for the row being generated.
1199
+	 * @param EE_Datetime   $datetime
1200
+	 * @param bool          $default       Whether a default row is being generated or not.
1201
+	 * @param EE_Datetime[] $all_datetimes This is the array of all datetimes used in the editor.
1202
+	 * @return string
1203
+	 * @throws DomainException
1204
+	 * @throws EE_Error
1205
+	 */
1206
+	protected function _get_dtt_edit_row($datetime_row, $datetime, $default, $all_datetimes)
1207
+	{
1208
+		// if the incoming $datetime object is NOT an instance of EE_Datetime then force default to true.
1209
+		$default = ! $datetime instanceof EE_Datetime ? true : $default;
1210
+		$template_args = array(
1211
+			'dtt_row'              => $default ? 'DTTNUM' : $datetime_row,
1212
+			'event_datetimes_name' => $default ? 'DTTNAMEATTR' : 'edit_event_datetimes',
1213
+			'edit_dtt_expanded'    => '',
1214
+			'DTT_ID'               => $default ? '' : $datetime->ID(),
1215
+			'DTT_name'             => $default ? '' : $datetime->get_f('DTT_name'),
1216
+			'DTT_description'      => $default ? '' : $datetime->get_f('DTT_description'),
1217
+			'DTT_EVT_start'        => $default ? '' : $datetime->start_date($this->_date_time_format),
1218
+			'DTT_EVT_end'          => $default ? '' : $datetime->end_date($this->_date_time_format),
1219
+			'DTT_reg_limit'        => $default
1220
+				? ''
1221
+				: $datetime->get_pretty(
1222
+					'DTT_reg_limit',
1223
+					'input'
1224
+				),
1225
+			'DTT_order'            => $default ? 'DTTNUM' : $datetime_row,
1226
+			'dtt_sold'             => $default ? '0' : $datetime->get('DTT_sold'),
1227
+			'dtt_reserved'         => $default ? '0' : $datetime->reserved(),
1228
+			'clone_icon'           => ! empty($datetime) && $datetime->get('DTT_sold') > 0
1229
+				? ''
1230
+				: 'clone-icon ee-icon ee-icon-clone clickable',
1231
+			'trash_icon'           => ! empty($datetime) && $datetime->get('DTT_sold') > 0
1232
+				? 'ee-lock-icon'
1233
+				: 'trash-icon dashicons dashicons-post-trash clickable',
1234
+			'reg_list_url'         => $default || ! $datetime->event() instanceof \EE_Event
1235
+				? ''
1236
+				: EE_Admin_Page::add_query_args_and_nonce(
1237
+					array('event_id' => $datetime->event()->ID(), 'datetime_id' => $datetime->ID()),
1238
+					REG_ADMIN_URL
1239
+				),
1240
+		);
1241
+		$template_args['show_trash'] = count($all_datetimes) === 1 && $template_args['trash_icon'] !== 'ee-lock-icon'
1242
+			? ' style="display:none"'
1243
+			: '';
1244
+		// allow filtering of template args at this point.
1245
+		$template_args = apply_filters(
1246
+			'FHEE__espresso_events_Pricing_Hooks___get_dtt_edit_row__template_args',
1247
+			$template_args,
1248
+			$datetime_row,
1249
+			$datetime,
1250
+			$default,
1251
+			$all_datetimes,
1252
+			$this->_is_creating_event
1253
+		);
1254
+		return EEH_Template::display_template(
1255
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_edit_row.template.php',
1256
+			$template_args,
1257
+			true
1258
+		);
1259
+	}
1260 1260
 
1261 1261
 
1262
-    /**
1263
-     * @param int         $datetime_row
1264
-     * @param EE_Datetime $datetime
1265
-     * @param array       $datetime_tickets
1266
-     * @param array       $all_tickets
1267
-     * @param bool        $default
1268
-     * @return mixed
1269
-     * @throws DomainException
1270
-     * @throws EE_Error
1271
-     */
1272
-    protected function _get_dtt_attached_tickets_row(
1273
-        $datetime_row,
1274
-        $datetime,
1275
-        $datetime_tickets = array(),
1276
-        $all_tickets = array(),
1277
-        $default
1278
-    ) {
1279
-        $template_args = array(
1280
-            'dtt_row'                           => $default ? 'DTTNUM' : $datetime_row,
1281
-            'event_datetimes_name'              => $default ? 'DTTNAMEATTR' : 'edit_event_datetimes',
1282
-            'DTT_description'                   => $default ? '' : $datetime->get_f('DTT_description'),
1283
-            'datetime_tickets_list'             => $default ? '<li class="hidden"></li>' : '',
1284
-            'show_tickets_row'                  => ' style="display:none;"',
1285
-            'add_new_datetime_ticket_help_link' => EEH_Template::get_help_tab_link(
1286
-                'add_new_ticket_via_datetime',
1287
-                $this->_adminpage_obj->page_slug,
1288
-                $this->_adminpage_obj->get_req_action(),
1289
-                false,
1290
-                false
1291
-            ),
1292
-            // todo need to add this help info id to the Events_Admin_Page core file so we can access it here.
1293
-            'DTT_ID'                            => $default ? '' : $datetime->ID(),
1294
-        );
1295
-        // need to setup the list items (but only if this isn't a default skeleton setup)
1296
-        if (! $default) {
1297
-            $ticket_row = 1;
1298
-            foreach ($all_tickets as $ticket) {
1299
-                $template_args['datetime_tickets_list'] .= $this->_get_datetime_tickets_list_item(
1300
-                    $datetime_row,
1301
-                    $ticket_row,
1302
-                    $datetime,
1303
-                    $ticket,
1304
-                    $datetime_tickets,
1305
-                    $default
1306
-                );
1307
-                $ticket_row++;
1308
-            }
1309
-        }
1310
-        // filter template args at this point
1311
-        $template_args = apply_filters(
1312
-            'FHEE__espresso_events_Pricing_Hooks___get_dtt_attached_ticket_row__template_args',
1313
-            $template_args,
1314
-            $datetime_row,
1315
-            $datetime,
1316
-            $datetime_tickets,
1317
-            $all_tickets,
1318
-            $default,
1319
-            $this->_is_creating_event
1320
-        );
1321
-        return EEH_Template::display_template(
1322
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_attached_tickets_row.template.php',
1323
-            $template_args,
1324
-            true
1325
-        );
1326
-    }
1262
+	/**
1263
+	 * @param int         $datetime_row
1264
+	 * @param EE_Datetime $datetime
1265
+	 * @param array       $datetime_tickets
1266
+	 * @param array       $all_tickets
1267
+	 * @param bool        $default
1268
+	 * @return mixed
1269
+	 * @throws DomainException
1270
+	 * @throws EE_Error
1271
+	 */
1272
+	protected function _get_dtt_attached_tickets_row(
1273
+		$datetime_row,
1274
+		$datetime,
1275
+		$datetime_tickets = array(),
1276
+		$all_tickets = array(),
1277
+		$default
1278
+	) {
1279
+		$template_args = array(
1280
+			'dtt_row'                           => $default ? 'DTTNUM' : $datetime_row,
1281
+			'event_datetimes_name'              => $default ? 'DTTNAMEATTR' : 'edit_event_datetimes',
1282
+			'DTT_description'                   => $default ? '' : $datetime->get_f('DTT_description'),
1283
+			'datetime_tickets_list'             => $default ? '<li class="hidden"></li>' : '',
1284
+			'show_tickets_row'                  => ' style="display:none;"',
1285
+			'add_new_datetime_ticket_help_link' => EEH_Template::get_help_tab_link(
1286
+				'add_new_ticket_via_datetime',
1287
+				$this->_adminpage_obj->page_slug,
1288
+				$this->_adminpage_obj->get_req_action(),
1289
+				false,
1290
+				false
1291
+			),
1292
+			// todo need to add this help info id to the Events_Admin_Page core file so we can access it here.
1293
+			'DTT_ID'                            => $default ? '' : $datetime->ID(),
1294
+		);
1295
+		// need to setup the list items (but only if this isn't a default skeleton setup)
1296
+		if (! $default) {
1297
+			$ticket_row = 1;
1298
+			foreach ($all_tickets as $ticket) {
1299
+				$template_args['datetime_tickets_list'] .= $this->_get_datetime_tickets_list_item(
1300
+					$datetime_row,
1301
+					$ticket_row,
1302
+					$datetime,
1303
+					$ticket,
1304
+					$datetime_tickets,
1305
+					$default
1306
+				);
1307
+				$ticket_row++;
1308
+			}
1309
+		}
1310
+		// filter template args at this point
1311
+		$template_args = apply_filters(
1312
+			'FHEE__espresso_events_Pricing_Hooks___get_dtt_attached_ticket_row__template_args',
1313
+			$template_args,
1314
+			$datetime_row,
1315
+			$datetime,
1316
+			$datetime_tickets,
1317
+			$all_tickets,
1318
+			$default,
1319
+			$this->_is_creating_event
1320
+		);
1321
+		return EEH_Template::display_template(
1322
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_attached_tickets_row.template.php',
1323
+			$template_args,
1324
+			true
1325
+		);
1326
+	}
1327 1327
 
1328 1328
 
1329
-    /**
1330
-     * @param int         $datetime_row
1331
-     * @param int         $ticket_row
1332
-     * @param EE_Datetime $datetime
1333
-     * @param EE_Ticket   $ticket
1334
-     * @param array       $datetime_tickets
1335
-     * @param bool        $default
1336
-     * @return mixed
1337
-     * @throws DomainException
1338
-     * @throws EE_Error
1339
-     */
1340
-    protected function _get_datetime_tickets_list_item(
1341
-        $datetime_row,
1342
-        $ticket_row,
1343
-        $datetime,
1344
-        $ticket,
1345
-        $datetime_tickets = array(),
1346
-        $default
1347
-    ) {
1348
-        $dtt_tkts = $datetime instanceof EE_Datetime && isset($datetime_tickets[ $datetime->ID() ])
1349
-            ? $datetime_tickets[ $datetime->ID() ]
1350
-            : array();
1351
-        $display_row = $ticket instanceof EE_Ticket ? $ticket->get('TKT_row') : 0;
1352
-        $no_ticket = $default && empty($ticket);
1353
-        $template_args = array(
1354
-            'dtt_row'                 => $default
1355
-                ? 'DTTNUM'
1356
-                : $datetime_row,
1357
-            'tkt_row'                 => $no_ticket
1358
-                ? 'TICKETNUM'
1359
-                : $ticket_row,
1360
-            'datetime_ticket_checked' => in_array($display_row, $dtt_tkts, true)
1361
-                ? ' checked="checked"'
1362
-                : '',
1363
-            'ticket_selected'         => in_array($display_row, $dtt_tkts, true)
1364
-                ? ' ticket-selected'
1365
-                : '',
1366
-            'TKT_name'                => $no_ticket
1367
-                ? 'TKTNAME'
1368
-                : $ticket->get('TKT_name'),
1369
-            'tkt_status_class'        => $no_ticket || $this->_is_creating_event
1370
-                ? ' tkt-status-' . EE_Ticket::onsale
1371
-                : ' tkt-status-' . $ticket->ticket_status(),
1372
-        );
1373
-        // filter template args
1374
-        $template_args = apply_filters(
1375
-            'FHEE__espresso_events_Pricing_Hooks___get_datetime_tickets_list_item__template_args',
1376
-            $template_args,
1377
-            $datetime_row,
1378
-            $ticket_row,
1379
-            $datetime,
1380
-            $ticket,
1381
-            $datetime_tickets,
1382
-            $default,
1383
-            $this->_is_creating_event
1384
-        );
1385
-        return EEH_Template::display_template(
1386
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_dtt_tickets_list.template.php',
1387
-            $template_args,
1388
-            true
1389
-        );
1390
-    }
1329
+	/**
1330
+	 * @param int         $datetime_row
1331
+	 * @param int         $ticket_row
1332
+	 * @param EE_Datetime $datetime
1333
+	 * @param EE_Ticket   $ticket
1334
+	 * @param array       $datetime_tickets
1335
+	 * @param bool        $default
1336
+	 * @return mixed
1337
+	 * @throws DomainException
1338
+	 * @throws EE_Error
1339
+	 */
1340
+	protected function _get_datetime_tickets_list_item(
1341
+		$datetime_row,
1342
+		$ticket_row,
1343
+		$datetime,
1344
+		$ticket,
1345
+		$datetime_tickets = array(),
1346
+		$default
1347
+	) {
1348
+		$dtt_tkts = $datetime instanceof EE_Datetime && isset($datetime_tickets[ $datetime->ID() ])
1349
+			? $datetime_tickets[ $datetime->ID() ]
1350
+			: array();
1351
+		$display_row = $ticket instanceof EE_Ticket ? $ticket->get('TKT_row') : 0;
1352
+		$no_ticket = $default && empty($ticket);
1353
+		$template_args = array(
1354
+			'dtt_row'                 => $default
1355
+				? 'DTTNUM'
1356
+				: $datetime_row,
1357
+			'tkt_row'                 => $no_ticket
1358
+				? 'TICKETNUM'
1359
+				: $ticket_row,
1360
+			'datetime_ticket_checked' => in_array($display_row, $dtt_tkts, true)
1361
+				? ' checked="checked"'
1362
+				: '',
1363
+			'ticket_selected'         => in_array($display_row, $dtt_tkts, true)
1364
+				? ' ticket-selected'
1365
+				: '',
1366
+			'TKT_name'                => $no_ticket
1367
+				? 'TKTNAME'
1368
+				: $ticket->get('TKT_name'),
1369
+			'tkt_status_class'        => $no_ticket || $this->_is_creating_event
1370
+				? ' tkt-status-' . EE_Ticket::onsale
1371
+				: ' tkt-status-' . $ticket->ticket_status(),
1372
+		);
1373
+		// filter template args
1374
+		$template_args = apply_filters(
1375
+			'FHEE__espresso_events_Pricing_Hooks___get_datetime_tickets_list_item__template_args',
1376
+			$template_args,
1377
+			$datetime_row,
1378
+			$ticket_row,
1379
+			$datetime,
1380
+			$ticket,
1381
+			$datetime_tickets,
1382
+			$default,
1383
+			$this->_is_creating_event
1384
+		);
1385
+		return EEH_Template::display_template(
1386
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_dtt_tickets_list.template.php',
1387
+			$template_args,
1388
+			true
1389
+		);
1390
+	}
1391 1391
 
1392 1392
 
1393
-    /**
1394
-     * This generates the ticket row for tickets.
1395
-     * This same method is used to generate both the actual rows and the js skeleton row
1396
-     * (when default === true)
1397
-     *
1398
-     * @param int           $ticket_row       Represents the row number being generated.
1399
-     * @param               $ticket
1400
-     * @param EE_Datetime[] $ticket_datetimes Either an array of all datetimes on all tickets indexed by each ticket
1401
-     *                                        or empty for default
1402
-     * @param EE_Datetime[] $all_datetimes    All Datetimes on the event or empty for default.
1403
-     * @param bool          $default          Whether default row being generated or not.
1404
-     * @param EE_Ticket[]   $all_tickets      This is an array of all tickets attached to the event
1405
-     *                                        (or empty in the case of defaults)
1406
-     * @return mixed
1407
-     * @throws InvalidArgumentException
1408
-     * @throws InvalidInterfaceException
1409
-     * @throws InvalidDataTypeException
1410
-     * @throws DomainException
1411
-     * @throws EE_Error
1412
-     * @throws ReflectionException
1413
-     */
1414
-    protected function _get_ticket_row(
1415
-        $ticket_row,
1416
-        $ticket,
1417
-        $ticket_datetimes,
1418
-        $all_datetimes,
1419
-        $default = false,
1420
-        $all_tickets = array()
1421
-    ) {
1422
-        // if $ticket is not an instance of EE_Ticket then force default to true.
1423
-        $default = ! $ticket instanceof EE_Ticket ? true : $default;
1424
-        $prices = ! empty($ticket) && ! $default
1425
-            ? $ticket->get_many_related(
1426
-                'Price',
1427
-                array('default_where_conditions' => 'none', 'order_by' => array('PRC_order' => 'ASC'))
1428
-            )
1429
-            : array();
1430
-        // if there is only one price (which would be the base price)
1431
-        // or NO prices and this ticket is a default ticket,
1432
-        // let's just make sure there are no cached default prices on the object.
1433
-        // This is done by not including any query_params.
1434
-        if ($ticket instanceof EE_Ticket && $ticket->is_default() && (count($prices) === 1 || empty($prices))) {
1435
-            $prices = $ticket->prices();
1436
-        }
1437
-        // check if we're dealing with a default ticket in which case
1438
-        // we don't want any starting_ticket_datetime_row values set
1439
-        // (otherwise there won't be any new relationships created for tickets based off of the default ticket).
1440
-        // This will future proof in case there is ever any behaviour change between what the primary_key defaults to.
1441
-        $default_dtt = $default || ($ticket instanceof EE_Ticket && $ticket->is_default());
1442
-        $tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[ $ticket->ID() ])
1443
-            ? $ticket_datetimes[ $ticket->ID() ]
1444
-            : array();
1445
-        $ticket_subtotal = $default ? 0 : $ticket->get_ticket_subtotal();
1446
-        $base_price = $default ? null : $ticket->base_price();
1447
-        $count_price_mods = EEM_Price::instance()->get_all_default_prices(true);
1448
-        // breaking out complicated condition for ticket_status
1449
-        if ($default) {
1450
-            $ticket_status_class = ' tkt-status-' . EE_Ticket::onsale;
1451
-        } else {
1452
-            $ticket_status_class = $ticket->is_default()
1453
-                ? ' tkt-status-' . EE_Ticket::onsale
1454
-                : ' tkt-status-' . $ticket->ticket_status();
1455
-        }
1456
-        // breaking out complicated condition for TKT_taxable
1457
-        if ($default) {
1458
-            $TKT_taxable = '';
1459
-        } else {
1460
-            $TKT_taxable = $ticket->taxable()
1461
-                ? ' checked="checked"'
1462
-                : '';
1463
-        }
1464
-        if ($default) {
1465
-            $TKT_status = EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence');
1466
-        } elseif ($ticket->is_default()) {
1467
-            $TKT_status = EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence');
1468
-        } else {
1469
-            $TKT_status = $ticket->ticket_status(true);
1470
-        }
1471
-        if ($default) {
1472
-            $TKT_min = '';
1473
-        } else {
1474
-            $TKT_min = $ticket->min();
1475
-            if ($TKT_min === -1 || $TKT_min === 0) {
1476
-                $TKT_min = '';
1477
-            }
1478
-        }
1479
-        $template_args = array(
1480
-            'tkt_row'                       => $default ? 'TICKETNUM' : $ticket_row,
1481
-            'TKT_order'                     => $default ? 'TICKETNUM' : $ticket_row,
1482
-            // on initial page load this will always be the correct order.
1483
-            'tkt_status_class'              => $ticket_status_class,
1484
-            'display_edit_tkt_row'          => ' style="display:none;"',
1485
-            'edit_tkt_expanded'             => '',
1486
-            'edit_tickets_name'             => $default ? 'TICKETNAMEATTR' : 'edit_tickets',
1487
-            'TKT_name'                      => $default ? '' : $ticket->get_f('TKT_name'),
1488
-            'TKT_start_date'                => $default
1489
-                ? ''
1490
-                : $ticket->get_date('TKT_start_date', $this->_date_time_format),
1491
-            'TKT_end_date'                  => $default
1492
-                ? ''
1493
-                : $ticket->get_date('TKT_end_date', $this->_date_time_format),
1494
-            'TKT_status'                    => $TKT_status,
1495
-            'TKT_price'                     => $default
1496
-                ? ''
1497
-                : EEH_Template::format_currency(
1498
-                    $ticket->get_ticket_total_with_taxes(),
1499
-                    false,
1500
-                    false
1501
-                ),
1502
-            'TKT_price_code'                => EE_Registry::instance()->CFG->currency->code,
1503
-            'TKT_price_amount'              => $default ? 0 : $ticket_subtotal,
1504
-            'TKT_qty'                       => $default
1505
-                ? ''
1506
-                : $ticket->get_pretty('TKT_qty', 'symbol'),
1507
-            'TKT_qty_for_input'             => $default
1508
-                ? ''
1509
-                : $ticket->get_pretty('TKT_qty', 'input'),
1510
-            'TKT_uses'                      => $default
1511
-                ? ''
1512
-                : $ticket->get_pretty('TKT_uses', 'input'),
1513
-            'TKT_min'                       => $TKT_min,
1514
-            'TKT_max'                       => $default
1515
-                ? ''
1516
-                : $ticket->get_pretty('TKT_max', 'input'),
1517
-            'TKT_sold'                      => $default ? 0 : $ticket->tickets_sold('ticket'),
1518
-            'TKT_reserved'                  => $default ? 0 : $ticket->reserved(),
1519
-            'TKT_registrations'             => $default
1520
-                ? 0
1521
-                : $ticket->count_registrations(
1522
-                    array(
1523
-                        array(
1524
-                            'STS_ID' => array(
1525
-                                '!=',
1526
-                                EEM_Registration::status_id_incomplete,
1527
-                            ),
1528
-                        ),
1529
-                    )
1530
-                ),
1531
-            'TKT_ID'                        => $default ? 0 : $ticket->ID(),
1532
-            'TKT_description'               => $default ? '' : $ticket->get_f('TKT_description'),
1533
-            'TKT_is_default'                => $default ? 0 : $ticket->is_default(),
1534
-            'TKT_required'                  => $default ? 0 : $ticket->required(),
1535
-            'TKT_is_default_selector'       => '',
1536
-            'ticket_price_rows'             => '',
1537
-            'TKT_base_price'                => $default || ! $base_price instanceof EE_Price
1538
-                ? ''
1539
-                : $base_price->get_pretty('PRC_amount', 'localized_float'),
1540
-            'TKT_base_price_ID'             => $default || ! $base_price instanceof EE_Price ? 0 : $base_price->ID(),
1541
-            'show_price_modifier'           => count($prices) > 1 || ($default && $count_price_mods > 0)
1542
-                ? ''
1543
-                : ' style="display:none;"',
1544
-            'show_price_mod_button'         => count($prices) > 1
1545
-                                               || ($default && $count_price_mods > 0)
1546
-                                               || (! $default && $ticket->deleted())
1547
-                ? ' style="display:none;"'
1548
-                : '',
1549
-            'total_price_rows'              => count($prices) > 1 ? count($prices) : 1,
1550
-            'ticket_datetimes_list'         => $default ? '<li class="hidden"></li>' : '',
1551
-            'starting_ticket_datetime_rows' => $default || $default_dtt ? '' : implode(',', $tkt_datetimes),
1552
-            'ticket_datetime_rows'          => $default ? '' : implode(',', $tkt_datetimes),
1553
-            'existing_ticket_price_ids'     => $default ? '' : implode(',', array_keys($prices)),
1554
-            'ticket_template_id'            => $default ? 0 : $ticket->get('TTM_ID'),
1555
-            'TKT_taxable'                   => $TKT_taxable,
1556
-            'display_subtotal'              => $ticket instanceof EE_Ticket && $ticket->taxable()
1557
-                ? ''
1558
-                : ' style="display:none"',
1559
-            'price_currency_symbol'         => EE_Registry::instance()->CFG->currency->sign,
1560
-            'TKT_subtotal_amount_display'   => EEH_Template::format_currency(
1561
-                $ticket_subtotal,
1562
-                false,
1563
-                false
1564
-            ),
1565
-            'TKT_subtotal_amount'           => $ticket_subtotal,
1566
-            'tax_rows'                      => $this->_get_tax_rows($ticket_row, $ticket),
1567
-            'disabled'                      => $ticket instanceof EE_Ticket && $ticket->deleted(),
1568
-            'ticket_archive_class'          => $ticket instanceof EE_Ticket && $ticket->deleted()
1569
-                ? ' ticket-archived'
1570
-                : '',
1571
-            'trash_icon'                    => $ticket instanceof EE_Ticket
1572
-                                               && $ticket->deleted()
1573
-                                               && ! $ticket->is_permanently_deleteable()
1574
-                ? 'ee-lock-icon '
1575
-                : 'trash-icon dashicons dashicons-post-trash clickable',
1576
-            'clone_icon'                    => $ticket instanceof EE_Ticket && $ticket->deleted()
1577
-                ? ''
1578
-                : 'clone-icon ee-icon ee-icon-clone clickable',
1579
-        );
1580
-        $template_args['trash_hidden'] = count($all_tickets) === 1 && $template_args['trash_icon'] !== 'ee-lock-icon'
1581
-            ? ' style="display:none"'
1582
-            : '';
1583
-        // handle rows that should NOT be empty
1584
-        if (empty($template_args['TKT_start_date'])) {
1585
-            // if empty then the start date will be now.
1586
-            $template_args['TKT_start_date'] = date(
1587
-                $this->_date_time_format,
1588
-                current_time('timestamp')
1589
-            );
1590
-            $template_args['tkt_status_class'] = ' tkt-status-' . EE_Ticket::onsale;
1591
-        }
1592
-        if (empty($template_args['TKT_end_date'])) {
1593
-            // get the earliest datetime (if present);
1594
-            $earliest_dtt = $this->_adminpage_obj->get_cpt_model_obj()->ID() > 0
1595
-                ? $this->_adminpage_obj->get_cpt_model_obj()->get_first_related(
1596
-                    'Datetime',
1597
-                    array('order_by' => array('DTT_EVT_start' => 'ASC'))
1598
-                )
1599
-                : null;
1600
-            if (! empty($earliest_dtt)) {
1601
-                $template_args['TKT_end_date'] = $earliest_dtt->get_datetime(
1602
-                    'DTT_EVT_start',
1603
-                    $this->_date_time_format
1604
-                );
1605
-            } else {
1606
-                // default so let's just use what's been set for the default date-time which is 30 days from now.
1607
-                $template_args['TKT_end_date'] = date(
1608
-                    $this->_date_time_format,
1609
-                    mktime(
1610
-                        24,
1611
-                        0,
1612
-                        0,
1613
-                        date('m'),
1614
-                        date('d') + 29,
1615
-                        date('Y')
1616
-                    )
1617
-                );
1618
-            }
1619
-            $template_args['tkt_status_class'] = ' tkt-status-' . EE_Ticket::onsale;
1620
-        }
1621
-        // generate ticket_datetime items
1622
-        if (! $default) {
1623
-            $datetime_row = 1;
1624
-            foreach ($all_datetimes as $datetime) {
1625
-                $template_args['ticket_datetimes_list'] .= $this->_get_ticket_datetime_list_item(
1626
-                    $datetime_row,
1627
-                    $ticket_row,
1628
-                    $datetime,
1629
-                    $ticket,
1630
-                    $ticket_datetimes,
1631
-                    $default
1632
-                );
1633
-                $datetime_row++;
1634
-            }
1635
-        }
1636
-        $price_row = 1;
1637
-        foreach ($prices as $price) {
1638
-            if (! $price instanceof EE_Price) {
1639
-                continue;
1640
-            }
1641
-            if ($price->is_base_price()) {
1642
-                $price_row++;
1643
-                continue;
1644
-            }
1645
-            $show_trash = ! ((count($prices) > 1 && $price_row === 1) || count($prices) === 1);
1646
-            $show_create = ! (count($prices) > 1 && count($prices) !== $price_row);
1647
-            $template_args['ticket_price_rows'] .= $this->_get_ticket_price_row(
1648
-                $ticket_row,
1649
-                $price_row,
1650
-                $price,
1651
-                $default,
1652
-                $ticket,
1653
-                $show_trash,
1654
-                $show_create
1655
-            );
1656
-            $price_row++;
1657
-        }
1658
-        // filter $template_args
1659
-        $template_args = apply_filters(
1660
-            'FHEE__espresso_events_Pricing_Hooks___get_ticket_row__template_args',
1661
-            $template_args,
1662
-            $ticket_row,
1663
-            $ticket,
1664
-            $ticket_datetimes,
1665
-            $all_datetimes,
1666
-            $default,
1667
-            $all_tickets,
1668
-            $this->_is_creating_event
1669
-        );
1670
-        return EEH_Template::display_template(
1671
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_row.template.php',
1672
-            $template_args,
1673
-            true
1674
-        );
1675
-    }
1393
+	/**
1394
+	 * This generates the ticket row for tickets.
1395
+	 * This same method is used to generate both the actual rows and the js skeleton row
1396
+	 * (when default === true)
1397
+	 *
1398
+	 * @param int           $ticket_row       Represents the row number being generated.
1399
+	 * @param               $ticket
1400
+	 * @param EE_Datetime[] $ticket_datetimes Either an array of all datetimes on all tickets indexed by each ticket
1401
+	 *                                        or empty for default
1402
+	 * @param EE_Datetime[] $all_datetimes    All Datetimes on the event or empty for default.
1403
+	 * @param bool          $default          Whether default row being generated or not.
1404
+	 * @param EE_Ticket[]   $all_tickets      This is an array of all tickets attached to the event
1405
+	 *                                        (or empty in the case of defaults)
1406
+	 * @return mixed
1407
+	 * @throws InvalidArgumentException
1408
+	 * @throws InvalidInterfaceException
1409
+	 * @throws InvalidDataTypeException
1410
+	 * @throws DomainException
1411
+	 * @throws EE_Error
1412
+	 * @throws ReflectionException
1413
+	 */
1414
+	protected function _get_ticket_row(
1415
+		$ticket_row,
1416
+		$ticket,
1417
+		$ticket_datetimes,
1418
+		$all_datetimes,
1419
+		$default = false,
1420
+		$all_tickets = array()
1421
+	) {
1422
+		// if $ticket is not an instance of EE_Ticket then force default to true.
1423
+		$default = ! $ticket instanceof EE_Ticket ? true : $default;
1424
+		$prices = ! empty($ticket) && ! $default
1425
+			? $ticket->get_many_related(
1426
+				'Price',
1427
+				array('default_where_conditions' => 'none', 'order_by' => array('PRC_order' => 'ASC'))
1428
+			)
1429
+			: array();
1430
+		// if there is only one price (which would be the base price)
1431
+		// or NO prices and this ticket is a default ticket,
1432
+		// let's just make sure there are no cached default prices on the object.
1433
+		// This is done by not including any query_params.
1434
+		if ($ticket instanceof EE_Ticket && $ticket->is_default() && (count($prices) === 1 || empty($prices))) {
1435
+			$prices = $ticket->prices();
1436
+		}
1437
+		// check if we're dealing with a default ticket in which case
1438
+		// we don't want any starting_ticket_datetime_row values set
1439
+		// (otherwise there won't be any new relationships created for tickets based off of the default ticket).
1440
+		// This will future proof in case there is ever any behaviour change between what the primary_key defaults to.
1441
+		$default_dtt = $default || ($ticket instanceof EE_Ticket && $ticket->is_default());
1442
+		$tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[ $ticket->ID() ])
1443
+			? $ticket_datetimes[ $ticket->ID() ]
1444
+			: array();
1445
+		$ticket_subtotal = $default ? 0 : $ticket->get_ticket_subtotal();
1446
+		$base_price = $default ? null : $ticket->base_price();
1447
+		$count_price_mods = EEM_Price::instance()->get_all_default_prices(true);
1448
+		// breaking out complicated condition for ticket_status
1449
+		if ($default) {
1450
+			$ticket_status_class = ' tkt-status-' . EE_Ticket::onsale;
1451
+		} else {
1452
+			$ticket_status_class = $ticket->is_default()
1453
+				? ' tkt-status-' . EE_Ticket::onsale
1454
+				: ' tkt-status-' . $ticket->ticket_status();
1455
+		}
1456
+		// breaking out complicated condition for TKT_taxable
1457
+		if ($default) {
1458
+			$TKT_taxable = '';
1459
+		} else {
1460
+			$TKT_taxable = $ticket->taxable()
1461
+				? ' checked="checked"'
1462
+				: '';
1463
+		}
1464
+		if ($default) {
1465
+			$TKT_status = EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence');
1466
+		} elseif ($ticket->is_default()) {
1467
+			$TKT_status = EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence');
1468
+		} else {
1469
+			$TKT_status = $ticket->ticket_status(true);
1470
+		}
1471
+		if ($default) {
1472
+			$TKT_min = '';
1473
+		} else {
1474
+			$TKT_min = $ticket->min();
1475
+			if ($TKT_min === -1 || $TKT_min === 0) {
1476
+				$TKT_min = '';
1477
+			}
1478
+		}
1479
+		$template_args = array(
1480
+			'tkt_row'                       => $default ? 'TICKETNUM' : $ticket_row,
1481
+			'TKT_order'                     => $default ? 'TICKETNUM' : $ticket_row,
1482
+			// on initial page load this will always be the correct order.
1483
+			'tkt_status_class'              => $ticket_status_class,
1484
+			'display_edit_tkt_row'          => ' style="display:none;"',
1485
+			'edit_tkt_expanded'             => '',
1486
+			'edit_tickets_name'             => $default ? 'TICKETNAMEATTR' : 'edit_tickets',
1487
+			'TKT_name'                      => $default ? '' : $ticket->get_f('TKT_name'),
1488
+			'TKT_start_date'                => $default
1489
+				? ''
1490
+				: $ticket->get_date('TKT_start_date', $this->_date_time_format),
1491
+			'TKT_end_date'                  => $default
1492
+				? ''
1493
+				: $ticket->get_date('TKT_end_date', $this->_date_time_format),
1494
+			'TKT_status'                    => $TKT_status,
1495
+			'TKT_price'                     => $default
1496
+				? ''
1497
+				: EEH_Template::format_currency(
1498
+					$ticket->get_ticket_total_with_taxes(),
1499
+					false,
1500
+					false
1501
+				),
1502
+			'TKT_price_code'                => EE_Registry::instance()->CFG->currency->code,
1503
+			'TKT_price_amount'              => $default ? 0 : $ticket_subtotal,
1504
+			'TKT_qty'                       => $default
1505
+				? ''
1506
+				: $ticket->get_pretty('TKT_qty', 'symbol'),
1507
+			'TKT_qty_for_input'             => $default
1508
+				? ''
1509
+				: $ticket->get_pretty('TKT_qty', 'input'),
1510
+			'TKT_uses'                      => $default
1511
+				? ''
1512
+				: $ticket->get_pretty('TKT_uses', 'input'),
1513
+			'TKT_min'                       => $TKT_min,
1514
+			'TKT_max'                       => $default
1515
+				? ''
1516
+				: $ticket->get_pretty('TKT_max', 'input'),
1517
+			'TKT_sold'                      => $default ? 0 : $ticket->tickets_sold('ticket'),
1518
+			'TKT_reserved'                  => $default ? 0 : $ticket->reserved(),
1519
+			'TKT_registrations'             => $default
1520
+				? 0
1521
+				: $ticket->count_registrations(
1522
+					array(
1523
+						array(
1524
+							'STS_ID' => array(
1525
+								'!=',
1526
+								EEM_Registration::status_id_incomplete,
1527
+							),
1528
+						),
1529
+					)
1530
+				),
1531
+			'TKT_ID'                        => $default ? 0 : $ticket->ID(),
1532
+			'TKT_description'               => $default ? '' : $ticket->get_f('TKT_description'),
1533
+			'TKT_is_default'                => $default ? 0 : $ticket->is_default(),
1534
+			'TKT_required'                  => $default ? 0 : $ticket->required(),
1535
+			'TKT_is_default_selector'       => '',
1536
+			'ticket_price_rows'             => '',
1537
+			'TKT_base_price'                => $default || ! $base_price instanceof EE_Price
1538
+				? ''
1539
+				: $base_price->get_pretty('PRC_amount', 'localized_float'),
1540
+			'TKT_base_price_ID'             => $default || ! $base_price instanceof EE_Price ? 0 : $base_price->ID(),
1541
+			'show_price_modifier'           => count($prices) > 1 || ($default && $count_price_mods > 0)
1542
+				? ''
1543
+				: ' style="display:none;"',
1544
+			'show_price_mod_button'         => count($prices) > 1
1545
+											   || ($default && $count_price_mods > 0)
1546
+											   || (! $default && $ticket->deleted())
1547
+				? ' style="display:none;"'
1548
+				: '',
1549
+			'total_price_rows'              => count($prices) > 1 ? count($prices) : 1,
1550
+			'ticket_datetimes_list'         => $default ? '<li class="hidden"></li>' : '',
1551
+			'starting_ticket_datetime_rows' => $default || $default_dtt ? '' : implode(',', $tkt_datetimes),
1552
+			'ticket_datetime_rows'          => $default ? '' : implode(',', $tkt_datetimes),
1553
+			'existing_ticket_price_ids'     => $default ? '' : implode(',', array_keys($prices)),
1554
+			'ticket_template_id'            => $default ? 0 : $ticket->get('TTM_ID'),
1555
+			'TKT_taxable'                   => $TKT_taxable,
1556
+			'display_subtotal'              => $ticket instanceof EE_Ticket && $ticket->taxable()
1557
+				? ''
1558
+				: ' style="display:none"',
1559
+			'price_currency_symbol'         => EE_Registry::instance()->CFG->currency->sign,
1560
+			'TKT_subtotal_amount_display'   => EEH_Template::format_currency(
1561
+				$ticket_subtotal,
1562
+				false,
1563
+				false
1564
+			),
1565
+			'TKT_subtotal_amount'           => $ticket_subtotal,
1566
+			'tax_rows'                      => $this->_get_tax_rows($ticket_row, $ticket),
1567
+			'disabled'                      => $ticket instanceof EE_Ticket && $ticket->deleted(),
1568
+			'ticket_archive_class'          => $ticket instanceof EE_Ticket && $ticket->deleted()
1569
+				? ' ticket-archived'
1570
+				: '',
1571
+			'trash_icon'                    => $ticket instanceof EE_Ticket
1572
+											   && $ticket->deleted()
1573
+											   && ! $ticket->is_permanently_deleteable()
1574
+				? 'ee-lock-icon '
1575
+				: 'trash-icon dashicons dashicons-post-trash clickable',
1576
+			'clone_icon'                    => $ticket instanceof EE_Ticket && $ticket->deleted()
1577
+				? ''
1578
+				: 'clone-icon ee-icon ee-icon-clone clickable',
1579
+		);
1580
+		$template_args['trash_hidden'] = count($all_tickets) === 1 && $template_args['trash_icon'] !== 'ee-lock-icon'
1581
+			? ' style="display:none"'
1582
+			: '';
1583
+		// handle rows that should NOT be empty
1584
+		if (empty($template_args['TKT_start_date'])) {
1585
+			// if empty then the start date will be now.
1586
+			$template_args['TKT_start_date'] = date(
1587
+				$this->_date_time_format,
1588
+				current_time('timestamp')
1589
+			);
1590
+			$template_args['tkt_status_class'] = ' tkt-status-' . EE_Ticket::onsale;
1591
+		}
1592
+		if (empty($template_args['TKT_end_date'])) {
1593
+			// get the earliest datetime (if present);
1594
+			$earliest_dtt = $this->_adminpage_obj->get_cpt_model_obj()->ID() > 0
1595
+				? $this->_adminpage_obj->get_cpt_model_obj()->get_first_related(
1596
+					'Datetime',
1597
+					array('order_by' => array('DTT_EVT_start' => 'ASC'))
1598
+				)
1599
+				: null;
1600
+			if (! empty($earliest_dtt)) {
1601
+				$template_args['TKT_end_date'] = $earliest_dtt->get_datetime(
1602
+					'DTT_EVT_start',
1603
+					$this->_date_time_format
1604
+				);
1605
+			} else {
1606
+				// default so let's just use what's been set for the default date-time which is 30 days from now.
1607
+				$template_args['TKT_end_date'] = date(
1608
+					$this->_date_time_format,
1609
+					mktime(
1610
+						24,
1611
+						0,
1612
+						0,
1613
+						date('m'),
1614
+						date('d') + 29,
1615
+						date('Y')
1616
+					)
1617
+				);
1618
+			}
1619
+			$template_args['tkt_status_class'] = ' tkt-status-' . EE_Ticket::onsale;
1620
+		}
1621
+		// generate ticket_datetime items
1622
+		if (! $default) {
1623
+			$datetime_row = 1;
1624
+			foreach ($all_datetimes as $datetime) {
1625
+				$template_args['ticket_datetimes_list'] .= $this->_get_ticket_datetime_list_item(
1626
+					$datetime_row,
1627
+					$ticket_row,
1628
+					$datetime,
1629
+					$ticket,
1630
+					$ticket_datetimes,
1631
+					$default
1632
+				);
1633
+				$datetime_row++;
1634
+			}
1635
+		}
1636
+		$price_row = 1;
1637
+		foreach ($prices as $price) {
1638
+			if (! $price instanceof EE_Price) {
1639
+				continue;
1640
+			}
1641
+			if ($price->is_base_price()) {
1642
+				$price_row++;
1643
+				continue;
1644
+			}
1645
+			$show_trash = ! ((count($prices) > 1 && $price_row === 1) || count($prices) === 1);
1646
+			$show_create = ! (count($prices) > 1 && count($prices) !== $price_row);
1647
+			$template_args['ticket_price_rows'] .= $this->_get_ticket_price_row(
1648
+				$ticket_row,
1649
+				$price_row,
1650
+				$price,
1651
+				$default,
1652
+				$ticket,
1653
+				$show_trash,
1654
+				$show_create
1655
+			);
1656
+			$price_row++;
1657
+		}
1658
+		// filter $template_args
1659
+		$template_args = apply_filters(
1660
+			'FHEE__espresso_events_Pricing_Hooks___get_ticket_row__template_args',
1661
+			$template_args,
1662
+			$ticket_row,
1663
+			$ticket,
1664
+			$ticket_datetimes,
1665
+			$all_datetimes,
1666
+			$default,
1667
+			$all_tickets,
1668
+			$this->_is_creating_event
1669
+		);
1670
+		return EEH_Template::display_template(
1671
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_row.template.php',
1672
+			$template_args,
1673
+			true
1674
+		);
1675
+	}
1676 1676
 
1677 1677
 
1678
-    /**
1679
-     * @param int            $ticket_row
1680
-     * @param EE_Ticket|null $ticket
1681
-     * @return string
1682
-     * @throws DomainException
1683
-     * @throws EE_Error
1684
-     */
1685
-    protected function _get_tax_rows($ticket_row, $ticket)
1686
-    {
1687
-        $tax_rows = '';
1688
-        /** @var EE_Price[] $taxes */
1689
-        $taxes = empty($ticket) ? EE_Taxes::get_taxes_for_admin() : $ticket->get_ticket_taxes_for_admin();
1690
-        foreach ($taxes as $tax) {
1691
-            $tax_added = $this->_get_tax_added($tax, $ticket);
1692
-            $template_args = array(
1693
-                'display_tax'       => ! empty($ticket) && $ticket->get('TKT_taxable')
1694
-                    ? ''
1695
-                    : ' style="display:none;"',
1696
-                'tax_id'            => $tax->ID(),
1697
-                'tkt_row'           => $ticket_row,
1698
-                'tax_label'         => $tax->get('PRC_name'),
1699
-                'tax_added'         => $tax_added,
1700
-                'tax_added_display' => EEH_Template::format_currency($tax_added, false, false),
1701
-                'tax_amount'        => $tax->get('PRC_amount'),
1702
-            );
1703
-            $template_args = apply_filters(
1704
-                'FHEE__espresso_events_Pricing_Hooks___get_tax_rows__template_args',
1705
-                $template_args,
1706
-                $ticket_row,
1707
-                $ticket,
1708
-                $this->_is_creating_event
1709
-            );
1710
-            $tax_rows .= EEH_Template::display_template(
1711
-                PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_tax_row.template.php',
1712
-                $template_args,
1713
-                true
1714
-            );
1715
-        }
1716
-        return $tax_rows;
1717
-    }
1678
+	/**
1679
+	 * @param int            $ticket_row
1680
+	 * @param EE_Ticket|null $ticket
1681
+	 * @return string
1682
+	 * @throws DomainException
1683
+	 * @throws EE_Error
1684
+	 */
1685
+	protected function _get_tax_rows($ticket_row, $ticket)
1686
+	{
1687
+		$tax_rows = '';
1688
+		/** @var EE_Price[] $taxes */
1689
+		$taxes = empty($ticket) ? EE_Taxes::get_taxes_for_admin() : $ticket->get_ticket_taxes_for_admin();
1690
+		foreach ($taxes as $tax) {
1691
+			$tax_added = $this->_get_tax_added($tax, $ticket);
1692
+			$template_args = array(
1693
+				'display_tax'       => ! empty($ticket) && $ticket->get('TKT_taxable')
1694
+					? ''
1695
+					: ' style="display:none;"',
1696
+				'tax_id'            => $tax->ID(),
1697
+				'tkt_row'           => $ticket_row,
1698
+				'tax_label'         => $tax->get('PRC_name'),
1699
+				'tax_added'         => $tax_added,
1700
+				'tax_added_display' => EEH_Template::format_currency($tax_added, false, false),
1701
+				'tax_amount'        => $tax->get('PRC_amount'),
1702
+			);
1703
+			$template_args = apply_filters(
1704
+				'FHEE__espresso_events_Pricing_Hooks___get_tax_rows__template_args',
1705
+				$template_args,
1706
+				$ticket_row,
1707
+				$ticket,
1708
+				$this->_is_creating_event
1709
+			);
1710
+			$tax_rows .= EEH_Template::display_template(
1711
+				PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_tax_row.template.php',
1712
+				$template_args,
1713
+				true
1714
+			);
1715
+		}
1716
+		return $tax_rows;
1717
+	}
1718 1718
 
1719 1719
 
1720
-    /**
1721
-     * @param EE_Price       $tax
1722
-     * @param EE_Ticket|null $ticket
1723
-     * @return float|int
1724
-     * @throws EE_Error
1725
-     */
1726
-    protected function _get_tax_added(EE_Price $tax, $ticket)
1727
-    {
1728
-        $subtotal = empty($ticket) ? 0 : $ticket->get_ticket_subtotal();
1729
-        return $subtotal * $tax->get('PRC_amount') / 100;
1730
-    }
1720
+	/**
1721
+	 * @param EE_Price       $tax
1722
+	 * @param EE_Ticket|null $ticket
1723
+	 * @return float|int
1724
+	 * @throws EE_Error
1725
+	 */
1726
+	protected function _get_tax_added(EE_Price $tax, $ticket)
1727
+	{
1728
+		$subtotal = empty($ticket) ? 0 : $ticket->get_ticket_subtotal();
1729
+		return $subtotal * $tax->get('PRC_amount') / 100;
1730
+	}
1731 1731
 
1732 1732
 
1733
-    /**
1734
-     * @param int            $ticket_row
1735
-     * @param int            $price_row
1736
-     * @param EE_Price|null  $price
1737
-     * @param bool           $default
1738
-     * @param EE_Ticket|null $ticket
1739
-     * @param bool           $show_trash
1740
-     * @param bool           $show_create
1741
-     * @return mixed
1742
-     * @throws InvalidArgumentException
1743
-     * @throws InvalidInterfaceException
1744
-     * @throws InvalidDataTypeException
1745
-     * @throws DomainException
1746
-     * @throws EE_Error
1747
-     * @throws ReflectionException
1748
-     */
1749
-    protected function _get_ticket_price_row(
1750
-        $ticket_row,
1751
-        $price_row,
1752
-        $price,
1753
-        $default,
1754
-        $ticket,
1755
-        $show_trash = true,
1756
-        $show_create = true
1757
-    ) {
1758
-        $send_disabled = ! empty($ticket) && $ticket->get('TKT_deleted');
1759
-        $template_args = array(
1760
-            'tkt_row'               => $default && empty($ticket)
1761
-                ? 'TICKETNUM'
1762
-                : $ticket_row,
1763
-            'PRC_order'             => $default && empty($price)
1764
-                ? 'PRICENUM'
1765
-                : $price_row,
1766
-            'edit_prices_name'      => $default && empty($price)
1767
-                ? 'PRICENAMEATTR'
1768
-                : 'edit_prices',
1769
-            'price_type_selector'   => $default && empty($price)
1770
-                ? $this->_get_base_price_template($ticket_row, $price_row, $price, $default)
1771
-                : $this->_get_price_type_selector(
1772
-                    $ticket_row,
1773
-                    $price_row,
1774
-                    $price,
1775
-                    $default,
1776
-                    $send_disabled
1777
-                ),
1778
-            'PRC_ID'                => $default && empty($price)
1779
-                ? 0
1780
-                : $price->ID(),
1781
-            'PRC_is_default'        => $default && empty($price)
1782
-                ? 0
1783
-                : $price->get('PRC_is_default'),
1784
-            'PRC_name'              => $default && empty($price)
1785
-                ? ''
1786
-                : $price->get('PRC_name'),
1787
-            'price_currency_symbol' => EE_Registry::instance()->CFG->currency->sign,
1788
-            'show_plus_or_minus'    => $default && empty($price)
1789
-                ? ''
1790
-                : ' style="display:none;"',
1791
-            'show_plus'             => ($default && empty($price)) || ($price->is_discount() || $price->is_base_price())
1792
-                ? ' style="display:none;"'
1793
-                : '',
1794
-            'show_minus'            => ($default && empty($price)) || ! $price->is_discount()
1795
-                ? ' style="display:none;"'
1796
-                : '',
1797
-            'show_currency_symbol'  => ($default && empty($price)) || $price->is_percent()
1798
-                ? ' style="display:none"'
1799
-                : '',
1800
-            'PRC_amount'            => $default && empty($price)
1801
-                ? 0
1802
-                : $price->get_pretty('PRC_amount', 'localized_float'),
1803
-            'show_percentage'       => ($default && empty($price)) || ! $price->is_percent()
1804
-                ? ' style="display:none;"'
1805
-                : '',
1806
-            'show_trash_icon'       => $show_trash
1807
-                ? ''
1808
-                : ' style="display:none;"',
1809
-            'show_create_button'    => $show_create
1810
-                ? ''
1811
-                : ' style="display:none;"',
1812
-            'PRC_desc'              => $default && empty($price)
1813
-                ? ''
1814
-                : $price->get('PRC_desc'),
1815
-            'disabled'              => ! empty($ticket) && $ticket->get('TKT_deleted'),
1816
-        );
1817
-        $template_args = apply_filters(
1818
-            'FHEE__espresso_events_Pricing_Hooks___get_ticket_price_row__template_args',
1819
-            $template_args,
1820
-            $ticket_row,
1821
-            $price_row,
1822
-            $price,
1823
-            $default,
1824
-            $ticket,
1825
-            $show_trash,
1826
-            $show_create,
1827
-            $this->_is_creating_event
1828
-        );
1829
-        return EEH_Template::display_template(
1830
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_price_row.template.php',
1831
-            $template_args,
1832
-            true
1833
-        );
1834
-    }
1733
+	/**
1734
+	 * @param int            $ticket_row
1735
+	 * @param int            $price_row
1736
+	 * @param EE_Price|null  $price
1737
+	 * @param bool           $default
1738
+	 * @param EE_Ticket|null $ticket
1739
+	 * @param bool           $show_trash
1740
+	 * @param bool           $show_create
1741
+	 * @return mixed
1742
+	 * @throws InvalidArgumentException
1743
+	 * @throws InvalidInterfaceException
1744
+	 * @throws InvalidDataTypeException
1745
+	 * @throws DomainException
1746
+	 * @throws EE_Error
1747
+	 * @throws ReflectionException
1748
+	 */
1749
+	protected function _get_ticket_price_row(
1750
+		$ticket_row,
1751
+		$price_row,
1752
+		$price,
1753
+		$default,
1754
+		$ticket,
1755
+		$show_trash = true,
1756
+		$show_create = true
1757
+	) {
1758
+		$send_disabled = ! empty($ticket) && $ticket->get('TKT_deleted');
1759
+		$template_args = array(
1760
+			'tkt_row'               => $default && empty($ticket)
1761
+				? 'TICKETNUM'
1762
+				: $ticket_row,
1763
+			'PRC_order'             => $default && empty($price)
1764
+				? 'PRICENUM'
1765
+				: $price_row,
1766
+			'edit_prices_name'      => $default && empty($price)
1767
+				? 'PRICENAMEATTR'
1768
+				: 'edit_prices',
1769
+			'price_type_selector'   => $default && empty($price)
1770
+				? $this->_get_base_price_template($ticket_row, $price_row, $price, $default)
1771
+				: $this->_get_price_type_selector(
1772
+					$ticket_row,
1773
+					$price_row,
1774
+					$price,
1775
+					$default,
1776
+					$send_disabled
1777
+				),
1778
+			'PRC_ID'                => $default && empty($price)
1779
+				? 0
1780
+				: $price->ID(),
1781
+			'PRC_is_default'        => $default && empty($price)
1782
+				? 0
1783
+				: $price->get('PRC_is_default'),
1784
+			'PRC_name'              => $default && empty($price)
1785
+				? ''
1786
+				: $price->get('PRC_name'),
1787
+			'price_currency_symbol' => EE_Registry::instance()->CFG->currency->sign,
1788
+			'show_plus_or_minus'    => $default && empty($price)
1789
+				? ''
1790
+				: ' style="display:none;"',
1791
+			'show_plus'             => ($default && empty($price)) || ($price->is_discount() || $price->is_base_price())
1792
+				? ' style="display:none;"'
1793
+				: '',
1794
+			'show_minus'            => ($default && empty($price)) || ! $price->is_discount()
1795
+				? ' style="display:none;"'
1796
+				: '',
1797
+			'show_currency_symbol'  => ($default && empty($price)) || $price->is_percent()
1798
+				? ' style="display:none"'
1799
+				: '',
1800
+			'PRC_amount'            => $default && empty($price)
1801
+				? 0
1802
+				: $price->get_pretty('PRC_amount', 'localized_float'),
1803
+			'show_percentage'       => ($default && empty($price)) || ! $price->is_percent()
1804
+				? ' style="display:none;"'
1805
+				: '',
1806
+			'show_trash_icon'       => $show_trash
1807
+				? ''
1808
+				: ' style="display:none;"',
1809
+			'show_create_button'    => $show_create
1810
+				? ''
1811
+				: ' style="display:none;"',
1812
+			'PRC_desc'              => $default && empty($price)
1813
+				? ''
1814
+				: $price->get('PRC_desc'),
1815
+			'disabled'              => ! empty($ticket) && $ticket->get('TKT_deleted'),
1816
+		);
1817
+		$template_args = apply_filters(
1818
+			'FHEE__espresso_events_Pricing_Hooks___get_ticket_price_row__template_args',
1819
+			$template_args,
1820
+			$ticket_row,
1821
+			$price_row,
1822
+			$price,
1823
+			$default,
1824
+			$ticket,
1825
+			$show_trash,
1826
+			$show_create,
1827
+			$this->_is_creating_event
1828
+		);
1829
+		return EEH_Template::display_template(
1830
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_price_row.template.php',
1831
+			$template_args,
1832
+			true
1833
+		);
1834
+	}
1835 1835
 
1836 1836
 
1837
-    /**
1838
-     * @param int      $ticket_row
1839
-     * @param int      $price_row
1840
-     * @param EE_Price $price
1841
-     * @param bool     $default
1842
-     * @param bool     $disabled
1843
-     * @return mixed
1844
-     * @throws ReflectionException
1845
-     * @throws InvalidArgumentException
1846
-     * @throws InvalidInterfaceException
1847
-     * @throws InvalidDataTypeException
1848
-     * @throws DomainException
1849
-     * @throws EE_Error
1850
-     */
1851
-    protected function _get_price_type_selector($ticket_row, $price_row, $price, $default, $disabled = false)
1852
-    {
1853
-        if ($price->is_base_price()) {
1854
-            return $this->_get_base_price_template(
1855
-                $ticket_row,
1856
-                $price_row,
1857
-                $price,
1858
-                $default
1859
-            );
1860
-        }
1861
-        return $this->_get_price_modifier_template(
1862
-            $ticket_row,
1863
-            $price_row,
1864
-            $price,
1865
-            $default,
1866
-            $disabled
1867
-        );
1868
-    }
1837
+	/**
1838
+	 * @param int      $ticket_row
1839
+	 * @param int      $price_row
1840
+	 * @param EE_Price $price
1841
+	 * @param bool     $default
1842
+	 * @param bool     $disabled
1843
+	 * @return mixed
1844
+	 * @throws ReflectionException
1845
+	 * @throws InvalidArgumentException
1846
+	 * @throws InvalidInterfaceException
1847
+	 * @throws InvalidDataTypeException
1848
+	 * @throws DomainException
1849
+	 * @throws EE_Error
1850
+	 */
1851
+	protected function _get_price_type_selector($ticket_row, $price_row, $price, $default, $disabled = false)
1852
+	{
1853
+		if ($price->is_base_price()) {
1854
+			return $this->_get_base_price_template(
1855
+				$ticket_row,
1856
+				$price_row,
1857
+				$price,
1858
+				$default
1859
+			);
1860
+		}
1861
+		return $this->_get_price_modifier_template(
1862
+			$ticket_row,
1863
+			$price_row,
1864
+			$price,
1865
+			$default,
1866
+			$disabled
1867
+		);
1868
+	}
1869 1869
 
1870 1870
 
1871
-    /**
1872
-     * @param int      $ticket_row
1873
-     * @param int      $price_row
1874
-     * @param EE_Price $price
1875
-     * @param bool     $default
1876
-     * @return mixed
1877
-     * @throws DomainException
1878
-     * @throws EE_Error
1879
-     */
1880
-    protected function _get_base_price_template($ticket_row, $price_row, $price, $default)
1881
-    {
1882
-        $template_args = array(
1883
-            'tkt_row'                   => $default ? 'TICKETNUM' : $ticket_row,
1884
-            'PRC_order'                 => $default && empty($price) ? 'PRICENUM' : $price_row,
1885
-            'PRT_ID'                    => $default && empty($price) ? 1 : $price->get('PRT_ID'),
1886
-            'PRT_name'                  => esc_html__('Price', 'event_espresso'),
1887
-            'price_selected_operator'   => '+',
1888
-            'price_selected_is_percent' => 0,
1889
-        );
1890
-        $template_args = apply_filters(
1891
-            'FHEE__espresso_events_Pricing_Hooks___get_base_price_template__template_args',
1892
-            $template_args,
1893
-            $ticket_row,
1894
-            $price_row,
1895
-            $price,
1896
-            $default,
1897
-            $this->_is_creating_event
1898
-        );
1899
-        return EEH_Template::display_template(
1900
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_type_base.template.php',
1901
-            $template_args,
1902
-            true
1903
-        );
1904
-    }
1871
+	/**
1872
+	 * @param int      $ticket_row
1873
+	 * @param int      $price_row
1874
+	 * @param EE_Price $price
1875
+	 * @param bool     $default
1876
+	 * @return mixed
1877
+	 * @throws DomainException
1878
+	 * @throws EE_Error
1879
+	 */
1880
+	protected function _get_base_price_template($ticket_row, $price_row, $price, $default)
1881
+	{
1882
+		$template_args = array(
1883
+			'tkt_row'                   => $default ? 'TICKETNUM' : $ticket_row,
1884
+			'PRC_order'                 => $default && empty($price) ? 'PRICENUM' : $price_row,
1885
+			'PRT_ID'                    => $default && empty($price) ? 1 : $price->get('PRT_ID'),
1886
+			'PRT_name'                  => esc_html__('Price', 'event_espresso'),
1887
+			'price_selected_operator'   => '+',
1888
+			'price_selected_is_percent' => 0,
1889
+		);
1890
+		$template_args = apply_filters(
1891
+			'FHEE__espresso_events_Pricing_Hooks___get_base_price_template__template_args',
1892
+			$template_args,
1893
+			$ticket_row,
1894
+			$price_row,
1895
+			$price,
1896
+			$default,
1897
+			$this->_is_creating_event
1898
+		);
1899
+		return EEH_Template::display_template(
1900
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_type_base.template.php',
1901
+			$template_args,
1902
+			true
1903
+		);
1904
+	}
1905 1905
 
1906 1906
 
1907
-    /**
1908
-     * @param int      $ticket_row
1909
-     * @param int      $price_row
1910
-     * @param EE_Price $price
1911
-     * @param bool     $default
1912
-     * @param bool     $disabled
1913
-     * @return mixed
1914
-     * @throws ReflectionException
1915
-     * @throws InvalidArgumentException
1916
-     * @throws InvalidInterfaceException
1917
-     * @throws InvalidDataTypeException
1918
-     * @throws DomainException
1919
-     * @throws EE_Error
1920
-     */
1921
-    protected function _get_price_modifier_template(
1922
-        $ticket_row,
1923
-        $price_row,
1924
-        $price,
1925
-        $default,
1926
-        $disabled = false
1927
-    ) {
1928
-        $select_name = $default && ! $price instanceof EE_Price
1929
-            ? 'edit_prices[TICKETNUM][PRICENUM][PRT_ID]'
1930
-            : 'edit_prices[' . $ticket_row . '][' . $price_row . '][PRT_ID]';
1931
-        /** @var EEM_Price_Type $price_type_model */
1932
-        $price_type_model = EE_Registry::instance()->load_model('Price_Type');
1933
-        $price_types = $price_type_model->get_all(array(
1934
-            array(
1935
-                'OR' => array(
1936
-                    'PBT_ID'  => '2',
1937
-                    'PBT_ID*' => '3',
1938
-                ),
1939
-            ),
1940
-        ));
1941
-        $all_price_types = $default && ! $price instanceof EE_Price
1942
-            ? array(esc_html__('Select Modifier', 'event_espresso'))
1943
-            : array();
1944
-        $selected_price_type_id = $default && ! $price instanceof EE_Price ? 0 : $price->type();
1945
-        $price_option_spans = '';
1946
-        // setup price types for selector
1947
-        foreach ($price_types as $price_type) {
1948
-            if (! $price_type instanceof EE_Price_Type) {
1949
-                continue;
1950
-            }
1951
-            $all_price_types[ $price_type->ID() ] = $price_type->get('PRT_name');
1952
-            // while we're in the loop let's setup the option spans used by js
1953
-            $span_args = array(
1954
-                'PRT_ID'         => $price_type->ID(),
1955
-                'PRT_operator'   => $price_type->is_discount() ? '-' : '+',
1956
-                'PRT_is_percent' => $price_type->get('PRT_is_percent') ? 1 : 0,
1957
-            );
1958
-            $price_option_spans .= EEH_Template::display_template(
1959
-                PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_option_span.template.php',
1960
-                $span_args,
1961
-                true
1962
-            );
1963
-        }
1964
-        $select_name = $disabled ? 'archive_price[' . $ticket_row . '][' . $price_row . '][PRT_ID]'
1965
-            : $select_name;
1966
-        $select_input = new EE_Select_Input(
1967
-            $all_price_types,
1968
-            array(
1969
-                'default'               => $selected_price_type_id,
1970
-                'html_name'             => $select_name,
1971
-                'html_class'            => 'edit-price-PRT_ID',
1972
-                'html_other_attributes' => $disabled ? 'style="width:auto;" disabled' : 'style="width:auto;"',
1973
-            )
1974
-        );
1975
-        $price_selected_operator = $price instanceof EE_Price && $price->is_discount() ? '-' : '+';
1976
-        $price_selected_operator = $default && ! $price instanceof EE_Price ? '' : $price_selected_operator;
1977
-        $price_selected_is_percent = $price instanceof EE_Price && $price->is_percent() ? 1 : 0;
1978
-        $price_selected_is_percent = $default && ! $price instanceof EE_Price ? '' : $price_selected_is_percent;
1979
-        $template_args = array(
1980
-            'tkt_row'                   => $default ? 'TICKETNUM' : $ticket_row,
1981
-            'PRC_order'                 => $default && ! $price instanceof EE_Price ? 'PRICENUM' : $price_row,
1982
-            'price_modifier_selector'   => $select_input->get_html_for_input(),
1983
-            'main_name'                 => $select_name,
1984
-            'selected_price_type_id'    => $selected_price_type_id,
1985
-            'price_option_spans'        => $price_option_spans,
1986
-            'price_selected_operator'   => $price_selected_operator,
1987
-            'price_selected_is_percent' => $price_selected_is_percent,
1988
-            'disabled'                  => $disabled,
1989
-        );
1990
-        $template_args = apply_filters(
1991
-            'FHEE__espresso_events_Pricing_Hooks___get_price_modifier_template__template_args',
1992
-            $template_args,
1993
-            $ticket_row,
1994
-            $price_row,
1995
-            $price,
1996
-            $default,
1997
-            $disabled,
1998
-            $this->_is_creating_event
1999
-        );
2000
-        return EEH_Template::display_template(
2001
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_modifier_selector.template.php',
2002
-            $template_args,
2003
-            true
2004
-        );
2005
-    }
1907
+	/**
1908
+	 * @param int      $ticket_row
1909
+	 * @param int      $price_row
1910
+	 * @param EE_Price $price
1911
+	 * @param bool     $default
1912
+	 * @param bool     $disabled
1913
+	 * @return mixed
1914
+	 * @throws ReflectionException
1915
+	 * @throws InvalidArgumentException
1916
+	 * @throws InvalidInterfaceException
1917
+	 * @throws InvalidDataTypeException
1918
+	 * @throws DomainException
1919
+	 * @throws EE_Error
1920
+	 */
1921
+	protected function _get_price_modifier_template(
1922
+		$ticket_row,
1923
+		$price_row,
1924
+		$price,
1925
+		$default,
1926
+		$disabled = false
1927
+	) {
1928
+		$select_name = $default && ! $price instanceof EE_Price
1929
+			? 'edit_prices[TICKETNUM][PRICENUM][PRT_ID]'
1930
+			: 'edit_prices[' . $ticket_row . '][' . $price_row . '][PRT_ID]';
1931
+		/** @var EEM_Price_Type $price_type_model */
1932
+		$price_type_model = EE_Registry::instance()->load_model('Price_Type');
1933
+		$price_types = $price_type_model->get_all(array(
1934
+			array(
1935
+				'OR' => array(
1936
+					'PBT_ID'  => '2',
1937
+					'PBT_ID*' => '3',
1938
+				),
1939
+			),
1940
+		));
1941
+		$all_price_types = $default && ! $price instanceof EE_Price
1942
+			? array(esc_html__('Select Modifier', 'event_espresso'))
1943
+			: array();
1944
+		$selected_price_type_id = $default && ! $price instanceof EE_Price ? 0 : $price->type();
1945
+		$price_option_spans = '';
1946
+		// setup price types for selector
1947
+		foreach ($price_types as $price_type) {
1948
+			if (! $price_type instanceof EE_Price_Type) {
1949
+				continue;
1950
+			}
1951
+			$all_price_types[ $price_type->ID() ] = $price_type->get('PRT_name');
1952
+			// while we're in the loop let's setup the option spans used by js
1953
+			$span_args = array(
1954
+				'PRT_ID'         => $price_type->ID(),
1955
+				'PRT_operator'   => $price_type->is_discount() ? '-' : '+',
1956
+				'PRT_is_percent' => $price_type->get('PRT_is_percent') ? 1 : 0,
1957
+			);
1958
+			$price_option_spans .= EEH_Template::display_template(
1959
+				PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_option_span.template.php',
1960
+				$span_args,
1961
+				true
1962
+			);
1963
+		}
1964
+		$select_name = $disabled ? 'archive_price[' . $ticket_row . '][' . $price_row . '][PRT_ID]'
1965
+			: $select_name;
1966
+		$select_input = new EE_Select_Input(
1967
+			$all_price_types,
1968
+			array(
1969
+				'default'               => $selected_price_type_id,
1970
+				'html_name'             => $select_name,
1971
+				'html_class'            => 'edit-price-PRT_ID',
1972
+				'html_other_attributes' => $disabled ? 'style="width:auto;" disabled' : 'style="width:auto;"',
1973
+			)
1974
+		);
1975
+		$price_selected_operator = $price instanceof EE_Price && $price->is_discount() ? '-' : '+';
1976
+		$price_selected_operator = $default && ! $price instanceof EE_Price ? '' : $price_selected_operator;
1977
+		$price_selected_is_percent = $price instanceof EE_Price && $price->is_percent() ? 1 : 0;
1978
+		$price_selected_is_percent = $default && ! $price instanceof EE_Price ? '' : $price_selected_is_percent;
1979
+		$template_args = array(
1980
+			'tkt_row'                   => $default ? 'TICKETNUM' : $ticket_row,
1981
+			'PRC_order'                 => $default && ! $price instanceof EE_Price ? 'PRICENUM' : $price_row,
1982
+			'price_modifier_selector'   => $select_input->get_html_for_input(),
1983
+			'main_name'                 => $select_name,
1984
+			'selected_price_type_id'    => $selected_price_type_id,
1985
+			'price_option_spans'        => $price_option_spans,
1986
+			'price_selected_operator'   => $price_selected_operator,
1987
+			'price_selected_is_percent' => $price_selected_is_percent,
1988
+			'disabled'                  => $disabled,
1989
+		);
1990
+		$template_args = apply_filters(
1991
+			'FHEE__espresso_events_Pricing_Hooks___get_price_modifier_template__template_args',
1992
+			$template_args,
1993
+			$ticket_row,
1994
+			$price_row,
1995
+			$price,
1996
+			$default,
1997
+			$disabled,
1998
+			$this->_is_creating_event
1999
+		);
2000
+		return EEH_Template::display_template(
2001
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_modifier_selector.template.php',
2002
+			$template_args,
2003
+			true
2004
+		);
2005
+	}
2006 2006
 
2007 2007
 
2008
-    /**
2009
-     * @param int              $datetime_row
2010
-     * @param int              $ticket_row
2011
-     * @param EE_Datetime|null $datetime
2012
-     * @param EE_Ticket|null   $ticket
2013
-     * @param array            $ticket_datetimes
2014
-     * @param bool             $default
2015
-     * @return mixed
2016
-     * @throws DomainException
2017
-     * @throws EE_Error
2018
-     */
2019
-    protected function _get_ticket_datetime_list_item(
2020
-        $datetime_row,
2021
-        $ticket_row,
2022
-        $datetime,
2023
-        $ticket,
2024
-        $ticket_datetimes = array(),
2025
-        $default
2026
-    ) {
2027
-        $tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[ $ticket->ID() ])
2028
-            ? $ticket_datetimes[ $ticket->ID() ]
2029
-            : array();
2030
-        $template_args = array(
2031
-            'dtt_row'                  => $default && ! $datetime instanceof EE_Datetime
2032
-                ? 'DTTNUM'
2033
-                : $datetime_row,
2034
-            'tkt_row'                  => $default
2035
-                ? 'TICKETNUM'
2036
-                : $ticket_row,
2037
-            'ticket_datetime_selected' => in_array($datetime_row, $tkt_datetimes, true)
2038
-                ? ' ticket-selected'
2039
-                : '',
2040
-            'ticket_datetime_checked'  => in_array($datetime_row, $tkt_datetimes, true)
2041
-                ? ' checked="checked"'
2042
-                : '',
2043
-            'DTT_name'                 => $default && empty($datetime)
2044
-                ? 'DTTNAME'
2045
-                : $datetime->get_dtt_display_name(true),
2046
-            'tkt_status_class'         => '',
2047
-        );
2048
-        $template_args = apply_filters(
2049
-            'FHEE__espresso_events_Pricing_Hooks___get_ticket_datetime_list_item__template_args',
2050
-            $template_args,
2051
-            $datetime_row,
2052
-            $ticket_row,
2053
-            $datetime,
2054
-            $ticket,
2055
-            $ticket_datetimes,
2056
-            $default,
2057
-            $this->_is_creating_event
2058
-        );
2059
-        return EEH_Template::display_template(
2060
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_datetimes_list_item.template.php',
2061
-            $template_args,
2062
-            true
2063
-        );
2064
-    }
2008
+	/**
2009
+	 * @param int              $datetime_row
2010
+	 * @param int              $ticket_row
2011
+	 * @param EE_Datetime|null $datetime
2012
+	 * @param EE_Ticket|null   $ticket
2013
+	 * @param array            $ticket_datetimes
2014
+	 * @param bool             $default
2015
+	 * @return mixed
2016
+	 * @throws DomainException
2017
+	 * @throws EE_Error
2018
+	 */
2019
+	protected function _get_ticket_datetime_list_item(
2020
+		$datetime_row,
2021
+		$ticket_row,
2022
+		$datetime,
2023
+		$ticket,
2024
+		$ticket_datetimes = array(),
2025
+		$default
2026
+	) {
2027
+		$tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[ $ticket->ID() ])
2028
+			? $ticket_datetimes[ $ticket->ID() ]
2029
+			: array();
2030
+		$template_args = array(
2031
+			'dtt_row'                  => $default && ! $datetime instanceof EE_Datetime
2032
+				? 'DTTNUM'
2033
+				: $datetime_row,
2034
+			'tkt_row'                  => $default
2035
+				? 'TICKETNUM'
2036
+				: $ticket_row,
2037
+			'ticket_datetime_selected' => in_array($datetime_row, $tkt_datetimes, true)
2038
+				? ' ticket-selected'
2039
+				: '',
2040
+			'ticket_datetime_checked'  => in_array($datetime_row, $tkt_datetimes, true)
2041
+				? ' checked="checked"'
2042
+				: '',
2043
+			'DTT_name'                 => $default && empty($datetime)
2044
+				? 'DTTNAME'
2045
+				: $datetime->get_dtt_display_name(true),
2046
+			'tkt_status_class'         => '',
2047
+		);
2048
+		$template_args = apply_filters(
2049
+			'FHEE__espresso_events_Pricing_Hooks___get_ticket_datetime_list_item__template_args',
2050
+			$template_args,
2051
+			$datetime_row,
2052
+			$ticket_row,
2053
+			$datetime,
2054
+			$ticket,
2055
+			$ticket_datetimes,
2056
+			$default,
2057
+			$this->_is_creating_event
2058
+		);
2059
+		return EEH_Template::display_template(
2060
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_datetimes_list_item.template.php',
2061
+			$template_args,
2062
+			true
2063
+		);
2064
+	}
2065 2065
 
2066 2066
 
2067
-    /**
2068
-     * @param array $all_datetimes
2069
-     * @param array $all_tickets
2070
-     * @return mixed
2071
-     * @throws ReflectionException
2072
-     * @throws InvalidArgumentException
2073
-     * @throws InvalidInterfaceException
2074
-     * @throws InvalidDataTypeException
2075
-     * @throws DomainException
2076
-     * @throws EE_Error
2077
-     */
2078
-    protected function _get_ticket_js_structure($all_datetimes = array(), $all_tickets = array())
2079
-    {
2080
-        $template_args = array(
2081
-            'default_datetime_edit_row'                => $this->_get_dtt_edit_row(
2082
-                'DTTNUM',
2083
-                null,
2084
-                true,
2085
-                $all_datetimes
2086
-            ),
2087
-            'default_ticket_row'                       => $this->_get_ticket_row(
2088
-                'TICKETNUM',
2089
-                null,
2090
-                array(),
2091
-                array(),
2092
-                true
2093
-            ),
2094
-            'default_price_row'                        => $this->_get_ticket_price_row(
2095
-                'TICKETNUM',
2096
-                'PRICENUM',
2097
-                null,
2098
-                true,
2099
-                null
2100
-            ),
2101
-            'default_price_rows'                       => '',
2102
-            'default_base_price_amount'                => 0,
2103
-            'default_base_price_name'                  => '',
2104
-            'default_base_price_description'           => '',
2105
-            'default_price_modifier_selector_row'      => $this->_get_price_modifier_template(
2106
-                'TICKETNUM',
2107
-                'PRICENUM',
2108
-                null,
2109
-                true
2110
-            ),
2111
-            'default_available_tickets_for_datetime'   => $this->_get_dtt_attached_tickets_row(
2112
-                'DTTNUM',
2113
-                null,
2114
-                array(),
2115
-                array(),
2116
-                true
2117
-            ),
2118
-            'existing_available_datetime_tickets_list' => '',
2119
-            'existing_available_ticket_datetimes_list' => '',
2120
-            'new_available_datetime_ticket_list_item'  => $this->_get_datetime_tickets_list_item(
2121
-                'DTTNUM',
2122
-                'TICKETNUM',
2123
-                null,
2124
-                null,
2125
-                array(),
2126
-                true
2127
-            ),
2128
-            'new_available_ticket_datetime_list_item'  => $this->_get_ticket_datetime_list_item(
2129
-                'DTTNUM',
2130
-                'TICKETNUM',
2131
-                null,
2132
-                null,
2133
-                array(),
2134
-                true
2135
-            ),
2136
-        );
2137
-        $ticket_row = 1;
2138
-        foreach ($all_tickets as $ticket) {
2139
-            $template_args['existing_available_datetime_tickets_list'] .= $this->_get_datetime_tickets_list_item(
2140
-                'DTTNUM',
2141
-                $ticket_row,
2142
-                null,
2143
-                $ticket,
2144
-                array(),
2145
-                true
2146
-            );
2147
-            $ticket_row++;
2148
-        }
2149
-        $datetime_row = 1;
2150
-        foreach ($all_datetimes as $datetime) {
2151
-            $template_args['existing_available_ticket_datetimes_list'] .= $this->_get_ticket_datetime_list_item(
2152
-                $datetime_row,
2153
-                'TICKETNUM',
2154
-                $datetime,
2155
-                null,
2156
-                array(),
2157
-                true
2158
-            );
2159
-            $datetime_row++;
2160
-        }
2161
-        /** @var EEM_Price $price_model */
2162
-        $price_model = EE_Registry::instance()->load_model('Price');
2163
-        $default_prices = $price_model->get_all_default_prices();
2164
-        $price_row = 1;
2165
-        foreach ($default_prices as $price) {
2166
-            if (! $price instanceof EE_Price) {
2167
-                continue;
2168
-            }
2169
-            if ($price->is_base_price()) {
2170
-                $template_args['default_base_price_amount'] = $price->get_pretty(
2171
-                    'PRC_amount',
2172
-                    'localized_float'
2173
-                );
2174
-                $template_args['default_base_price_name'] = $price->get('PRC_name');
2175
-                $template_args['default_base_price_description'] = $price->get('PRC_desc');
2176
-                $price_row++;
2177
-                continue;
2178
-            }
2179
-            $show_trash = ! ((count($default_prices) > 1 && $price_row === 1)
2180
-                             || count($default_prices) === 1);
2181
-            $show_create = ! (count($default_prices) > 1
2182
-                              && count($default_prices)
2183
-                                 !== $price_row);
2184
-            $template_args['default_price_rows'] .= $this->_get_ticket_price_row(
2185
-                'TICKETNUM',
2186
-                $price_row,
2187
-                $price,
2188
-                true,
2189
-                null,
2190
-                $show_trash,
2191
-                $show_create
2192
-            );
2193
-            $price_row++;
2194
-        }
2195
-        $template_args = apply_filters(
2196
-            'FHEE__espresso_events_Pricing_Hooks___get_ticket_js_structure__template_args',
2197
-            $template_args,
2198
-            $all_datetimes,
2199
-            $all_tickets,
2200
-            $this->_is_creating_event
2201
-        );
2202
-        return EEH_Template::display_template(
2203
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_js_structure.template.php',
2204
-            $template_args,
2205
-            true
2206
-        );
2207
-    }
2067
+	/**
2068
+	 * @param array $all_datetimes
2069
+	 * @param array $all_tickets
2070
+	 * @return mixed
2071
+	 * @throws ReflectionException
2072
+	 * @throws InvalidArgumentException
2073
+	 * @throws InvalidInterfaceException
2074
+	 * @throws InvalidDataTypeException
2075
+	 * @throws DomainException
2076
+	 * @throws EE_Error
2077
+	 */
2078
+	protected function _get_ticket_js_structure($all_datetimes = array(), $all_tickets = array())
2079
+	{
2080
+		$template_args = array(
2081
+			'default_datetime_edit_row'                => $this->_get_dtt_edit_row(
2082
+				'DTTNUM',
2083
+				null,
2084
+				true,
2085
+				$all_datetimes
2086
+			),
2087
+			'default_ticket_row'                       => $this->_get_ticket_row(
2088
+				'TICKETNUM',
2089
+				null,
2090
+				array(),
2091
+				array(),
2092
+				true
2093
+			),
2094
+			'default_price_row'                        => $this->_get_ticket_price_row(
2095
+				'TICKETNUM',
2096
+				'PRICENUM',
2097
+				null,
2098
+				true,
2099
+				null
2100
+			),
2101
+			'default_price_rows'                       => '',
2102
+			'default_base_price_amount'                => 0,
2103
+			'default_base_price_name'                  => '',
2104
+			'default_base_price_description'           => '',
2105
+			'default_price_modifier_selector_row'      => $this->_get_price_modifier_template(
2106
+				'TICKETNUM',
2107
+				'PRICENUM',
2108
+				null,
2109
+				true
2110
+			),
2111
+			'default_available_tickets_for_datetime'   => $this->_get_dtt_attached_tickets_row(
2112
+				'DTTNUM',
2113
+				null,
2114
+				array(),
2115
+				array(),
2116
+				true
2117
+			),
2118
+			'existing_available_datetime_tickets_list' => '',
2119
+			'existing_available_ticket_datetimes_list' => '',
2120
+			'new_available_datetime_ticket_list_item'  => $this->_get_datetime_tickets_list_item(
2121
+				'DTTNUM',
2122
+				'TICKETNUM',
2123
+				null,
2124
+				null,
2125
+				array(),
2126
+				true
2127
+			),
2128
+			'new_available_ticket_datetime_list_item'  => $this->_get_ticket_datetime_list_item(
2129
+				'DTTNUM',
2130
+				'TICKETNUM',
2131
+				null,
2132
+				null,
2133
+				array(),
2134
+				true
2135
+			),
2136
+		);
2137
+		$ticket_row = 1;
2138
+		foreach ($all_tickets as $ticket) {
2139
+			$template_args['existing_available_datetime_tickets_list'] .= $this->_get_datetime_tickets_list_item(
2140
+				'DTTNUM',
2141
+				$ticket_row,
2142
+				null,
2143
+				$ticket,
2144
+				array(),
2145
+				true
2146
+			);
2147
+			$ticket_row++;
2148
+		}
2149
+		$datetime_row = 1;
2150
+		foreach ($all_datetimes as $datetime) {
2151
+			$template_args['existing_available_ticket_datetimes_list'] .= $this->_get_ticket_datetime_list_item(
2152
+				$datetime_row,
2153
+				'TICKETNUM',
2154
+				$datetime,
2155
+				null,
2156
+				array(),
2157
+				true
2158
+			);
2159
+			$datetime_row++;
2160
+		}
2161
+		/** @var EEM_Price $price_model */
2162
+		$price_model = EE_Registry::instance()->load_model('Price');
2163
+		$default_prices = $price_model->get_all_default_prices();
2164
+		$price_row = 1;
2165
+		foreach ($default_prices as $price) {
2166
+			if (! $price instanceof EE_Price) {
2167
+				continue;
2168
+			}
2169
+			if ($price->is_base_price()) {
2170
+				$template_args['default_base_price_amount'] = $price->get_pretty(
2171
+					'PRC_amount',
2172
+					'localized_float'
2173
+				);
2174
+				$template_args['default_base_price_name'] = $price->get('PRC_name');
2175
+				$template_args['default_base_price_description'] = $price->get('PRC_desc');
2176
+				$price_row++;
2177
+				continue;
2178
+			}
2179
+			$show_trash = ! ((count($default_prices) > 1 && $price_row === 1)
2180
+							 || count($default_prices) === 1);
2181
+			$show_create = ! (count($default_prices) > 1
2182
+							  && count($default_prices)
2183
+								 !== $price_row);
2184
+			$template_args['default_price_rows'] .= $this->_get_ticket_price_row(
2185
+				'TICKETNUM',
2186
+				$price_row,
2187
+				$price,
2188
+				true,
2189
+				null,
2190
+				$show_trash,
2191
+				$show_create
2192
+			);
2193
+			$price_row++;
2194
+		}
2195
+		$template_args = apply_filters(
2196
+			'FHEE__espresso_events_Pricing_Hooks___get_ticket_js_structure__template_args',
2197
+			$template_args,
2198
+			$all_datetimes,
2199
+			$all_tickets,
2200
+			$this->_is_creating_event
2201
+		);
2202
+		return EEH_Template::display_template(
2203
+			PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_js_structure.template.php',
2204
+			$template_args,
2205
+			true
2206
+		);
2207
+	}
2208 2208
 }
Please login to merge, or discard this patch.
Spacing   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
     {
49 49
         $this->_name = 'pricing';
50 50
         // capability check
51
-        if (! EE_Registry::instance()->CAP->current_user_can(
51
+        if ( ! EE_Registry::instance()->CAP->current_user_can(
52 52
             'ee_read_default_prices',
53 53
             'advanced_ticket_datetime_metabox'
54 54
         )) {
@@ -148,7 +148,7 @@  discard block
 block discarded – undo
148 148
             );
149 149
             $msg .= '</p><ul>';
150 150
             foreach ($format_validation as $error) {
151
-                $msg .= '<li>' . $error . '</li>';
151
+                $msg .= '<li>'.$error.'</li>';
152 152
             }
153 153
             $msg .= '</ul><p>';
154 154
             $msg .= sprintf(
@@ -177,11 +177,11 @@  discard block
 block discarded – undo
177 177
         $this->_scripts_styles = array(
178 178
             'registers'   => array(
179 179
                 'ee-tickets-datetimes-css' => array(
180
-                    'url'  => PRICING_ASSETS_URL . 'event-tickets-datetimes.css',
180
+                    'url'  => PRICING_ASSETS_URL.'event-tickets-datetimes.css',
181 181
                     'type' => 'css',
182 182
                 ),
183 183
                 'ee-dtt-ticket-metabox'    => array(
184
-                    'url'     => PRICING_ASSETS_URL . 'ee-datetime-ticket-metabox.js',
184
+                    'url'     => PRICING_ASSETS_URL.'ee-datetime-ticket-metabox.js',
185 185
                     'depends' => array('ee-datepicker', 'ee-dialog', 'underscore'),
186 186
                 ),
187 187
             ),
@@ -205,9 +205,9 @@  discard block
 block discarded – undo
205 205
                             'event_espresso'
206 206
                         ),
207 207
                         'cancel_button'           => '<button class="button-secondary ee-modal-cancel">'
208
-                                                     . esc_html__('Cancel', 'event_espresso') . '</button>',
208
+                                                     . esc_html__('Cancel', 'event_espresso').'</button>',
209 209
                         'close_button'            => '<button class="button-secondary ee-modal-cancel">'
210
-                                                     . esc_html__('Close', 'event_espresso') . '</button>',
210
+                                                     . esc_html__('Close', 'event_espresso').'</button>',
211 211
                         'single_warning_from_tkt' => esc_html__(
212 212
                             'The Datetime you are attempting to unassign from this ticket is the only remaining datetime for this ticket. Tickets must always have at least one datetime assigned to them.',
213 213
                             'event_espresso'
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
                             'event_espresso'
218 218
                         ),
219 219
                         'dismiss_button'          => '<button class="button-secondary ee-modal-cancel">'
220
-                                                     . esc_html__('Dismiss', 'event_espresso') . '</button>',
220
+                                                     . esc_html__('Dismiss', 'event_espresso').'</button>',
221 221
                     ),
222 222
                     'DTT_ERROR_MSG'         => array(
223 223
                         'no_ticket_name' => esc_html__('General Admission', 'event_espresso'),
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
     {
256 256
         foreach ($update_callbacks as $key => $callback) {
257 257
             if ($callback[1] === '_default_tickets_update') {
258
-                unset($update_callbacks[ $key ]);
258
+                unset($update_callbacks[$key]);
259 259
             }
260 260
         }
261 261
         $update_callbacks[] = array($this, 'datetime_and_tickets_caf_update');
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
         foreach ($data['edit_event_datetimes'] as $row => $datetime_data) {
314 314
             // trim all values to ensure any excess whitespace is removed.
315 315
             $datetime_data = array_map(
316
-                function ($datetime_data) {
316
+                function($datetime_data) {
317 317
                     return is_array($datetime_data) ? $datetime_data : trim($datetime_data);
318 318
                 },
319 319
                 $datetime_data
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
             );
344 344
             // if we have an id then let's get existing object first and then set the new values.
345 345
             // Otherwise we instantiate a new object for save.
346
-            if (! empty($datetime_data['DTT_ID'])) {
346
+            if ( ! empty($datetime_data['DTT_ID'])) {
347 347
                 $datetime = EE_Registry::instance()
348 348
                                        ->load_model('Datetime', array($timezone))
349 349
                                        ->get_one_by_ID($datetime_data['DTT_ID']);
@@ -357,7 +357,7 @@  discard block
 block discarded – undo
357 357
                 // after the add_relation_to() the autosave replaces it.
358 358
                 // We need to do this so we dont' TRASH the parent DTT.
359 359
                 // (save the ID for both key and value to avoid duplications)
360
-                $saved_dtt_ids[ $datetime->ID() ] = $datetime->ID();
360
+                $saved_dtt_ids[$datetime->ID()] = $datetime->ID();
361 361
             } else {
362 362
                 $datetime = EE_Registry::instance()->load_class(
363 363
                     'Datetime',
@@ -386,8 +386,8 @@  discard block
 block discarded – undo
386 386
             // because it is possible there was a new one created for the autosave.
387 387
             // (save the ID for both key and value to avoid duplications)
388 388
             $DTT_ID = $datetime->ID();
389
-            $saved_dtt_ids[ $DTT_ID ] = $DTT_ID;
390
-            $saved_dtt_objs[ $row ] = $datetime;
389
+            $saved_dtt_ids[$DTT_ID] = $DTT_ID;
390
+            $saved_dtt_objs[$row] = $datetime;
391 391
             // @todo if ANY of these updates fail then we want the appropriate global error message.
392 392
         }
393 393
         $event->save();
@@ -452,13 +452,13 @@  discard block
 block discarded – undo
452 452
             $update_prices = $create_new_TKT = false;
453 453
             // figure out what datetimes were added to the ticket
454 454
             // and what datetimes were removed from the ticket in the session.
455
-            $starting_tkt_dtt_rows = explode(',', $data['starting_ticket_datetime_rows'][ $row ]);
456
-            $tkt_dtt_rows = explode(',', $data['ticket_datetime_rows'][ $row ]);
455
+            $starting_tkt_dtt_rows = explode(',', $data['starting_ticket_datetime_rows'][$row]);
456
+            $tkt_dtt_rows = explode(',', $data['ticket_datetime_rows'][$row]);
457 457
             $datetimes_added = array_diff($tkt_dtt_rows, $starting_tkt_dtt_rows);
458 458
             $datetimes_removed = array_diff($starting_tkt_dtt_rows, $tkt_dtt_rows);
459 459
             // trim inputs to ensure any excess whitespace is removed.
460 460
             $tkt = array_map(
461
-                function ($ticket_data) {
461
+                function($ticket_data) {
462 462
                     return is_array($ticket_data) ? $ticket_data : trim($ticket_data);
463 463
                 },
464 464
                 $tkt
@@ -480,8 +480,8 @@  discard block
 block discarded – undo
480 480
             $base_price_id = isset($tkt['TKT_base_price_ID'])
481 481
                 ? $tkt['TKT_base_price_ID']
482 482
                 : 0;
483
-            $price_rows = is_array($data['edit_prices']) && isset($data['edit_prices'][ $row ])
484
-                ? $data['edit_prices'][ $row ]
483
+            $price_rows = is_array($data['edit_prices']) && isset($data['edit_prices'][$row])
484
+                ? $data['edit_prices'][$row]
485 485
                 : array();
486 486
             $now = null;
487 487
             if (empty($tkt['TKT_start_date'])) {
@@ -493,7 +493,7 @@  discard block
 block discarded – undo
493 493
                 /**
494 494
                  * set the TKT_end_date to the first datetime attached to the ticket.
495 495
                  */
496
-                $first_dtt = $saved_datetimes[ reset($tkt_dtt_rows) ];
496
+                $first_dtt = $saved_datetimes[reset($tkt_dtt_rows)];
497 497
                 $tkt['TKT_end_date'] = $first_dtt->start_date_and_time($this->_date_time_format);
498 498
             }
499 499
             $TKT_values = array(
@@ -628,7 +628,7 @@  discard block
 block discarded – undo
628 628
             // need to make sue that the TKT_price is accurate after saving the prices.
629 629
             $ticket->ensure_TKT_Price_correct();
630 630
             // handle CREATING a default tkt from the incoming tkt but ONLY if this isn't an autosave.
631
-            if (! defined('DOING_AUTOSAVE') && ! empty($tkt['TKT_is_default_selector'])) {
631
+            if ( ! defined('DOING_AUTOSAVE') && ! empty($tkt['TKT_is_default_selector'])) {
632 632
                 $update_prices = true;
633 633
                 $new_default = clone $ticket;
634 634
                 $new_default->set('TKT_ID', 0);
@@ -673,7 +673,7 @@  discard block
 block discarded – undo
673 673
                 // save new TKT
674 674
                 $new_tkt->save();
675 675
                 // add new ticket to array
676
-                $saved_tickets[ $new_tkt->ID() ] = $new_tkt;
676
+                $saved_tickets[$new_tkt->ID()] = $new_tkt;
677 677
                 do_action(
678 678
                     'AHEE__espresso_events_Pricing_Hooks___update_tkts_new_ticket',
679 679
                     $new_tkt,
@@ -683,7 +683,7 @@  discard block
 block discarded – undo
683 683
                 );
684 684
             } else {
685 685
                 // add tkt to saved tkts
686
-                $saved_tickets[ $ticket->ID() ] = $ticket;
686
+                $saved_tickets[$ticket->ID()] = $ticket;
687 687
                 do_action(
688 688
                     'AHEE__espresso_events_Pricing_Hooks___update_tkts_update_ticket',
689 689
                     $ticket,
@@ -750,31 +750,31 @@  discard block
 block discarded – undo
750 750
         // to start we have to add the ticket to all the datetimes its supposed to be with,
751 751
         // and removing the ticket from datetimes it got removed from.
752 752
         // first let's add datetimes
753
-        if (! empty($added_datetimes) && is_array($added_datetimes)) {
753
+        if ( ! empty($added_datetimes) && is_array($added_datetimes)) {
754 754
             foreach ($added_datetimes as $row_id) {
755 755
                 $row_id = (int) $row_id;
756
-                if (isset($saved_datetimes[ $row_id ]) && $saved_datetimes[ $row_id ] instanceof EE_Datetime) {
757
-                    $ticket->_add_relation_to($saved_datetimes[ $row_id ], 'Datetime');
756
+                if (isset($saved_datetimes[$row_id]) && $saved_datetimes[$row_id] instanceof EE_Datetime) {
757
+                    $ticket->_add_relation_to($saved_datetimes[$row_id], 'Datetime');
758 758
                     // Is this an existing ticket (has an ID) and does it have any sold?
759 759
                     // If so, then we need to add that to the DTT sold because this DTT is getting added.
760 760
                     if ($ticket->ID() && $ticket->sold() > 0) {
761
-                        $saved_datetimes[ $row_id ]->increaseSold($ticket->sold(), false);
761
+                        $saved_datetimes[$row_id]->increaseSold($ticket->sold(), false);
762 762
                     }
763 763
                 }
764 764
             }
765 765
         }
766 766
         // then remove datetimes
767
-        if (! empty($removed_datetimes) && is_array($removed_datetimes)) {
767
+        if ( ! empty($removed_datetimes) && is_array($removed_datetimes)) {
768 768
             foreach ($removed_datetimes as $row_id) {
769 769
                 $row_id = (int) $row_id;
770 770
                 // its entirely possible that a datetime got deleted (instead of just removed from relationship.
771 771
                 // So make sure we skip over this if the dtt isn't in the $saved_datetimes array)
772
-                if (isset($saved_datetimes[ $row_id ]) && $saved_datetimes[ $row_id ] instanceof EE_Datetime) {
773
-                    $ticket->_remove_relation_to($saved_datetimes[ $row_id ], 'Datetime');
772
+                if (isset($saved_datetimes[$row_id]) && $saved_datetimes[$row_id] instanceof EE_Datetime) {
773
+                    $ticket->_remove_relation_to($saved_datetimes[$row_id], 'Datetime');
774 774
                     // Is this an existing ticket (has an ID) and does it have any sold?
775 775
                     // If so, then we need to remove it's sold from the DTT_sold.
776 776
                     if ($ticket->ID() && $ticket->sold() > 0) {
777
-                        $saved_datetimes[ $row_id ]->decreaseSold($ticket->sold());
777
+                        $saved_datetimes[$row_id]->decreaseSold($ticket->sold());
778 778
                     }
779 779
                 }
780 780
             }
@@ -887,7 +887,7 @@  discard block
 block discarded – undo
887 887
             );
888 888
         }
889 889
         // possibly need to save tkt
890
-        if (! $ticket->ID()) {
890
+        if ( ! $ticket->ID()) {
891 891
             $ticket->save();
892 892
         }
893 893
         foreach ($prices as $row => $prc) {
@@ -921,17 +921,17 @@  discard block
 block discarded – undo
921 921
                 }
922 922
             }
923 923
             $price->save();
924
-            $updated_prices[ $price->ID() ] = $price;
924
+            $updated_prices[$price->ID()] = $price;
925 925
             $ticket->_add_relation_to($price, 'Price');
926 926
         }
927 927
         // now let's remove any prices that got removed from the ticket
928
-        if (! empty($current_prices_on_ticket)) {
928
+        if ( ! empty($current_prices_on_ticket)) {
929 929
             $current = array_keys($current_prices_on_ticket);
930 930
             $updated = array_keys($updated_prices);
931 931
             $prices_to_remove = array_diff($current, $updated);
932
-            if (! empty($prices_to_remove)) {
932
+            if ( ! empty($prices_to_remove)) {
933 933
                 foreach ($prices_to_remove as $prc_id) {
934
-                    $p = $current_prices_on_ticket[ $prc_id ];
934
+                    $p = $current_prices_on_ticket[$prc_id];
935 935
                     $ticket->_remove_relation_to($p, 'Price');
936 936
                     // delete permanently the price
937 937
                     $p->delete_permanently();
@@ -1082,17 +1082,17 @@  discard block
 block discarded – undo
1082 1082
                 $TKT_ID = $ticket->get('TKT_ID');
1083 1083
                 $ticket_row = $ticket->get('TKT_row');
1084 1084
                 // we only want unique tickets in our final display!!
1085
-                if (! in_array($TKT_ID, $existing_ticket_ids, true)) {
1085
+                if ( ! in_array($TKT_ID, $existing_ticket_ids, true)) {
1086 1086
                     $existing_ticket_ids[] = $TKT_ID;
1087 1087
                     $all_tickets[] = $ticket;
1088 1088
                 }
1089 1089
                 // temporary cache of this ticket info for this datetime for later processing of datetime rows.
1090
-                $datetime_tickets[ $DTT_ID ][] = $ticket_row;
1090
+                $datetime_tickets[$DTT_ID][] = $ticket_row;
1091 1091
                 // temporary cache of this datetime info for this ticket for later processing of ticket rows.
1092
-                if (! isset($ticket_datetimes[ $TKT_ID ])
1093
-                    || ! in_array($datetime_row, $ticket_datetimes[ $TKT_ID ], true)
1092
+                if ( ! isset($ticket_datetimes[$TKT_ID])
1093
+                    || ! in_array($datetime_row, $ticket_datetimes[$TKT_ID], true)
1094 1094
                 ) {
1095
-                    $ticket_datetimes[ $TKT_ID ][] = $datetime_row;
1095
+                    $ticket_datetimes[$TKT_ID][] = $datetime_row;
1096 1096
                 }
1097 1097
             }
1098 1098
             $datetime_row++;
@@ -1103,7 +1103,7 @@  discard block
 block discarded – undo
1103 1103
         // sort $all_tickets by order
1104 1104
         usort(
1105 1105
             $all_tickets,
1106
-            function (EE_Ticket $a, EE_Ticket $b) {
1106
+            function(EE_Ticket $a, EE_Ticket $b) {
1107 1107
                 $a_order = (int) $a->get('TKT_order');
1108 1108
                 $b_order = (int) $b->get('TKT_order');
1109 1109
                 if ($a_order === $b_order) {
@@ -1141,7 +1141,7 @@  discard block
 block discarded – undo
1141 1141
         }
1142 1142
         $main_template_args['ticket_js_structure'] = $this->_get_ticket_js_structure($datetimes, $all_tickets);
1143 1143
         EEH_Template::display_template(
1144
-            PRICING_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php',
1144
+            PRICING_TEMPLATE_PATH.'event_tickets_metabox_main.template.php',
1145 1145
             $main_template_args
1146 1146
         );
1147 1147
     }
@@ -1183,7 +1183,7 @@  discard block
 block discarded – undo
1183 1183
             'dtt_row'                  => $default ? 'DTTNUM' : $datetime_row,
1184 1184
         );
1185 1185
         return EEH_Template::display_template(
1186
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_row_wrapper.template.php',
1186
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_row_wrapper.template.php',
1187 1187
             $dtt_display_template_args,
1188 1188
             true
1189 1189
         );
@@ -1252,7 +1252,7 @@  discard block
 block discarded – undo
1252 1252
             $this->_is_creating_event
1253 1253
         );
1254 1254
         return EEH_Template::display_template(
1255
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_edit_row.template.php',
1255
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_edit_row.template.php',
1256 1256
             $template_args,
1257 1257
             true
1258 1258
         );
@@ -1293,7 +1293,7 @@  discard block
 block discarded – undo
1293 1293
             'DTT_ID'                            => $default ? '' : $datetime->ID(),
1294 1294
         );
1295 1295
         // need to setup the list items (but only if this isn't a default skeleton setup)
1296
-        if (! $default) {
1296
+        if ( ! $default) {
1297 1297
             $ticket_row = 1;
1298 1298
             foreach ($all_tickets as $ticket) {
1299 1299
                 $template_args['datetime_tickets_list'] .= $this->_get_datetime_tickets_list_item(
@@ -1319,7 +1319,7 @@  discard block
 block discarded – undo
1319 1319
             $this->_is_creating_event
1320 1320
         );
1321 1321
         return EEH_Template::display_template(
1322
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_attached_tickets_row.template.php',
1322
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_attached_tickets_row.template.php',
1323 1323
             $template_args,
1324 1324
             true
1325 1325
         );
@@ -1345,8 +1345,8 @@  discard block
 block discarded – undo
1345 1345
         $datetime_tickets = array(),
1346 1346
         $default
1347 1347
     ) {
1348
-        $dtt_tkts = $datetime instanceof EE_Datetime && isset($datetime_tickets[ $datetime->ID() ])
1349
-            ? $datetime_tickets[ $datetime->ID() ]
1348
+        $dtt_tkts = $datetime instanceof EE_Datetime && isset($datetime_tickets[$datetime->ID()])
1349
+            ? $datetime_tickets[$datetime->ID()]
1350 1350
             : array();
1351 1351
         $display_row = $ticket instanceof EE_Ticket ? $ticket->get('TKT_row') : 0;
1352 1352
         $no_ticket = $default && empty($ticket);
@@ -1367,8 +1367,8 @@  discard block
 block discarded – undo
1367 1367
                 ? 'TKTNAME'
1368 1368
                 : $ticket->get('TKT_name'),
1369 1369
             'tkt_status_class'        => $no_ticket || $this->_is_creating_event
1370
-                ? ' tkt-status-' . EE_Ticket::onsale
1371
-                : ' tkt-status-' . $ticket->ticket_status(),
1370
+                ? ' tkt-status-'.EE_Ticket::onsale
1371
+                : ' tkt-status-'.$ticket->ticket_status(),
1372 1372
         );
1373 1373
         // filter template args
1374 1374
         $template_args = apply_filters(
@@ -1383,7 +1383,7 @@  discard block
 block discarded – undo
1383 1383
             $this->_is_creating_event
1384 1384
         );
1385 1385
         return EEH_Template::display_template(
1386
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_dtt_tickets_list.template.php',
1386
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_dtt_tickets_list.template.php',
1387 1387
             $template_args,
1388 1388
             true
1389 1389
         );
@@ -1439,19 +1439,19 @@  discard block
 block discarded – undo
1439 1439
         // (otherwise there won't be any new relationships created for tickets based off of the default ticket).
1440 1440
         // This will future proof in case there is ever any behaviour change between what the primary_key defaults to.
1441 1441
         $default_dtt = $default || ($ticket instanceof EE_Ticket && $ticket->is_default());
1442
-        $tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[ $ticket->ID() ])
1443
-            ? $ticket_datetimes[ $ticket->ID() ]
1442
+        $tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[$ticket->ID()])
1443
+            ? $ticket_datetimes[$ticket->ID()]
1444 1444
             : array();
1445 1445
         $ticket_subtotal = $default ? 0 : $ticket->get_ticket_subtotal();
1446 1446
         $base_price = $default ? null : $ticket->base_price();
1447 1447
         $count_price_mods = EEM_Price::instance()->get_all_default_prices(true);
1448 1448
         // breaking out complicated condition for ticket_status
1449 1449
         if ($default) {
1450
-            $ticket_status_class = ' tkt-status-' . EE_Ticket::onsale;
1450
+            $ticket_status_class = ' tkt-status-'.EE_Ticket::onsale;
1451 1451
         } else {
1452 1452
             $ticket_status_class = $ticket->is_default()
1453
-                ? ' tkt-status-' . EE_Ticket::onsale
1454
-                : ' tkt-status-' . $ticket->ticket_status();
1453
+                ? ' tkt-status-'.EE_Ticket::onsale
1454
+                : ' tkt-status-'.$ticket->ticket_status();
1455 1455
         }
1456 1456
         // breaking out complicated condition for TKT_taxable
1457 1457
         if ($default) {
@@ -1543,7 +1543,7 @@  discard block
 block discarded – undo
1543 1543
                 : ' style="display:none;"',
1544 1544
             'show_price_mod_button'         => count($prices) > 1
1545 1545
                                                || ($default && $count_price_mods > 0)
1546
-                                               || (! $default && $ticket->deleted())
1546
+                                               || ( ! $default && $ticket->deleted())
1547 1547
                 ? ' style="display:none;"'
1548 1548
                 : '',
1549 1549
             'total_price_rows'              => count($prices) > 1 ? count($prices) : 1,
@@ -1587,7 +1587,7 @@  discard block
 block discarded – undo
1587 1587
                 $this->_date_time_format,
1588 1588
                 current_time('timestamp')
1589 1589
             );
1590
-            $template_args['tkt_status_class'] = ' tkt-status-' . EE_Ticket::onsale;
1590
+            $template_args['tkt_status_class'] = ' tkt-status-'.EE_Ticket::onsale;
1591 1591
         }
1592 1592
         if (empty($template_args['TKT_end_date'])) {
1593 1593
             // get the earliest datetime (if present);
@@ -1597,7 +1597,7 @@  discard block
 block discarded – undo
1597 1597
                     array('order_by' => array('DTT_EVT_start' => 'ASC'))
1598 1598
                 )
1599 1599
                 : null;
1600
-            if (! empty($earliest_dtt)) {
1600
+            if ( ! empty($earliest_dtt)) {
1601 1601
                 $template_args['TKT_end_date'] = $earliest_dtt->get_datetime(
1602 1602
                     'DTT_EVT_start',
1603 1603
                     $this->_date_time_format
@@ -1616,10 +1616,10 @@  discard block
 block discarded – undo
1616 1616
                     )
1617 1617
                 );
1618 1618
             }
1619
-            $template_args['tkt_status_class'] = ' tkt-status-' . EE_Ticket::onsale;
1619
+            $template_args['tkt_status_class'] = ' tkt-status-'.EE_Ticket::onsale;
1620 1620
         }
1621 1621
         // generate ticket_datetime items
1622
-        if (! $default) {
1622
+        if ( ! $default) {
1623 1623
             $datetime_row = 1;
1624 1624
             foreach ($all_datetimes as $datetime) {
1625 1625
                 $template_args['ticket_datetimes_list'] .= $this->_get_ticket_datetime_list_item(
@@ -1635,7 +1635,7 @@  discard block
 block discarded – undo
1635 1635
         }
1636 1636
         $price_row = 1;
1637 1637
         foreach ($prices as $price) {
1638
-            if (! $price instanceof EE_Price) {
1638
+            if ( ! $price instanceof EE_Price) {
1639 1639
                 continue;
1640 1640
             }
1641 1641
             if ($price->is_base_price()) {
@@ -1668,7 +1668,7 @@  discard block
 block discarded – undo
1668 1668
             $this->_is_creating_event
1669 1669
         );
1670 1670
         return EEH_Template::display_template(
1671
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_row.template.php',
1671
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_ticket_row.template.php',
1672 1672
             $template_args,
1673 1673
             true
1674 1674
         );
@@ -1708,7 +1708,7 @@  discard block
 block discarded – undo
1708 1708
                 $this->_is_creating_event
1709 1709
             );
1710 1710
             $tax_rows .= EEH_Template::display_template(
1711
-                PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_tax_row.template.php',
1711
+                PRICING_TEMPLATE_PATH.'event_tickets_datetime_ticket_tax_row.template.php',
1712 1712
                 $template_args,
1713 1713
                 true
1714 1714
             );
@@ -1827,7 +1827,7 @@  discard block
 block discarded – undo
1827 1827
             $this->_is_creating_event
1828 1828
         );
1829 1829
         return EEH_Template::display_template(
1830
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_price_row.template.php',
1830
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_ticket_price_row.template.php',
1831 1831
             $template_args,
1832 1832
             true
1833 1833
         );
@@ -1897,7 +1897,7 @@  discard block
 block discarded – undo
1897 1897
             $this->_is_creating_event
1898 1898
         );
1899 1899
         return EEH_Template::display_template(
1900
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_type_base.template.php',
1900
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_price_type_base.template.php',
1901 1901
             $template_args,
1902 1902
             true
1903 1903
         );
@@ -1927,7 +1927,7 @@  discard block
 block discarded – undo
1927 1927
     ) {
1928 1928
         $select_name = $default && ! $price instanceof EE_Price
1929 1929
             ? 'edit_prices[TICKETNUM][PRICENUM][PRT_ID]'
1930
-            : 'edit_prices[' . $ticket_row . '][' . $price_row . '][PRT_ID]';
1930
+            : 'edit_prices['.$ticket_row.']['.$price_row.'][PRT_ID]';
1931 1931
         /** @var EEM_Price_Type $price_type_model */
1932 1932
         $price_type_model = EE_Registry::instance()->load_model('Price_Type');
1933 1933
         $price_types = $price_type_model->get_all(array(
@@ -1945,10 +1945,10 @@  discard block
 block discarded – undo
1945 1945
         $price_option_spans = '';
1946 1946
         // setup price types for selector
1947 1947
         foreach ($price_types as $price_type) {
1948
-            if (! $price_type instanceof EE_Price_Type) {
1948
+            if ( ! $price_type instanceof EE_Price_Type) {
1949 1949
                 continue;
1950 1950
             }
1951
-            $all_price_types[ $price_type->ID() ] = $price_type->get('PRT_name');
1951
+            $all_price_types[$price_type->ID()] = $price_type->get('PRT_name');
1952 1952
             // while we're in the loop let's setup the option spans used by js
1953 1953
             $span_args = array(
1954 1954
                 'PRT_ID'         => $price_type->ID(),
@@ -1956,12 +1956,12 @@  discard block
 block discarded – undo
1956 1956
                 'PRT_is_percent' => $price_type->get('PRT_is_percent') ? 1 : 0,
1957 1957
             );
1958 1958
             $price_option_spans .= EEH_Template::display_template(
1959
-                PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_option_span.template.php',
1959
+                PRICING_TEMPLATE_PATH.'event_tickets_datetime_price_option_span.template.php',
1960 1960
                 $span_args,
1961 1961
                 true
1962 1962
             );
1963 1963
         }
1964
-        $select_name = $disabled ? 'archive_price[' . $ticket_row . '][' . $price_row . '][PRT_ID]'
1964
+        $select_name = $disabled ? 'archive_price['.$ticket_row.']['.$price_row.'][PRT_ID]'
1965 1965
             : $select_name;
1966 1966
         $select_input = new EE_Select_Input(
1967 1967
             $all_price_types,
@@ -1998,7 +1998,7 @@  discard block
 block discarded – undo
1998 1998
             $this->_is_creating_event
1999 1999
         );
2000 2000
         return EEH_Template::display_template(
2001
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_price_modifier_selector.template.php',
2001
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_price_modifier_selector.template.php',
2002 2002
             $template_args,
2003 2003
             true
2004 2004
         );
@@ -2024,8 +2024,8 @@  discard block
 block discarded – undo
2024 2024
         $ticket_datetimes = array(),
2025 2025
         $default
2026 2026
     ) {
2027
-        $tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[ $ticket->ID() ])
2028
-            ? $ticket_datetimes[ $ticket->ID() ]
2027
+        $tkt_datetimes = $ticket instanceof EE_Ticket && isset($ticket_datetimes[$ticket->ID()])
2028
+            ? $ticket_datetimes[$ticket->ID()]
2029 2029
             : array();
2030 2030
         $template_args = array(
2031 2031
             'dtt_row'                  => $default && ! $datetime instanceof EE_Datetime
@@ -2057,7 +2057,7 @@  discard block
 block discarded – undo
2057 2057
             $this->_is_creating_event
2058 2058
         );
2059 2059
         return EEH_Template::display_template(
2060
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_datetimes_list_item.template.php',
2060
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_ticket_datetimes_list_item.template.php',
2061 2061
             $template_args,
2062 2062
             true
2063 2063
         );
@@ -2163,7 +2163,7 @@  discard block
 block discarded – undo
2163 2163
         $default_prices = $price_model->get_all_default_prices();
2164 2164
         $price_row = 1;
2165 2165
         foreach ($default_prices as $price) {
2166
-            if (! $price instanceof EE_Price) {
2166
+            if ( ! $price instanceof EE_Price) {
2167 2167
                 continue;
2168 2168
             }
2169 2169
             if ($price->is_base_price()) {
@@ -2200,7 +2200,7 @@  discard block
 block discarded – undo
2200 2200
             $this->_is_creating_event
2201 2201
         );
2202 2202
         return EEH_Template::display_template(
2203
-            PRICING_TEMPLATE_PATH . 'event_tickets_datetime_ticket_js_structure.template.php',
2203
+            PRICING_TEMPLATE_PATH.'event_tickets_datetime_ticket_js_structure.template.php',
2204 2204
             $template_args,
2205 2205
             true
2206 2206
         );
Please login to merge, or discard this patch.
core/db_classes/EE_Line_Item.class.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -781,7 +781,7 @@
 block discarded – undo
781 781
     /**
782 782
      * Gets the event that's related to the ticket, if this line item represents a ticket.
783 783
      *
784
-     * @return EE_Event|null
784
+     * @return EE_Base_Class|null
785 785
      * @throws EE_Error
786 786
      * @throws InvalidArgumentException
787 787
      * @throws InvalidDataTypeException
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
     protected function __construct($fieldValues = array(), $bydb = false, $timezone = '')
89 89
     {
90 90
         parent::__construct($fieldValues, $bydb, $timezone);
91
-        if (! $this->get('LIN_code')) {
91
+        if ( ! $this->get('LIN_code')) {
92 92
             $this->set_code($this->generate_code());
93 93
         }
94 94
     }
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
     public function name()
156 156
     {
157 157
         $name = $this->get('LIN_name');
158
-        if (! $name) {
158
+        if ( ! $name) {
159 159
             $name = ucwords(str_replace('-', ' ', $this->type()));
160 160
         }
161 161
         return $name;
@@ -615,7 +615,7 @@  discard block
 block discarded – undo
615 615
                 )
616 616
             );
617 617
         }
618
-        if (! is_array($this->_children)) {
618
+        if ( ! is_array($this->_children)) {
619 619
             $this->_children = array();
620 620
         }
621 621
         return $this->_children;
@@ -856,7 +856,7 @@  discard block
 block discarded – undo
856 856
             }
857 857
             return $line_item->save();
858 858
         }
859
-        $this->_children[ $line_item->code() ] = $line_item;
859
+        $this->_children[$line_item->code()] = $line_item;
860 860
         if ($line_item->parent() !== $this) {
861 861
             $line_item->set_parent($this);
862 862
         }
@@ -880,7 +880,7 @@  discard block
 block discarded – undo
880 880
     public function set_parent($line_item)
881 881
     {
882 882
         if ($this->ID()) {
883
-            if (! $line_item->ID()) {
883
+            if ( ! $line_item->ID()) {
884 884
                 $line_item->save();
885 885
             }
886 886
             $this->set_parent_ID($line_item->ID());
@@ -912,8 +912,8 @@  discard block
 block discarded – undo
912 912
                 array(array('LIN_parent' => $this->ID(), 'LIN_code' => $code))
913 913
             );
914 914
         }
915
-        return isset($this->_children[ $code ])
916
-            ? $this->_children[ $code ]
915
+        return isset($this->_children[$code])
916
+            ? $this->_children[$code]
917 917
             : null;
918 918
     }
919 919
 
@@ -973,8 +973,8 @@  discard block
 block discarded – undo
973 973
             }
974 974
             return $items_deleted;
975 975
         }
976
-        if (isset($this->_children[ $code ])) {
977
-            unset($this->_children[ $code ]);
976
+        if (isset($this->_children[$code])) {
977
+            unset($this->_children[$code]);
978 978
             return 1;
979 979
         }
980 980
         return 0;
@@ -1015,7 +1015,7 @@  discard block
 block discarded – undo
1015 1015
     public function generate_code()
1016 1016
     {
1017 1017
         // each line item in the cart requires a unique identifier
1018
-        return md5($this->get('OBJ_type') . $this->get('OBJ_ID') . microtime());
1018
+        return md5($this->get('OBJ_type').$this->get('OBJ_ID').microtime());
1019 1019
     }
1020 1020
 
1021 1021
 
@@ -1220,7 +1220,7 @@  discard block
 block discarded – undo
1220 1220
         $has_children = ! empty($my_children);
1221 1221
         if ($has_children && $this->is_line_item()) {
1222 1222
             $total = $this->_recalculate_pretax_total_for_line_item($total, $my_children);
1223
-        } elseif (! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1223
+        } elseif ( ! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1224 1224
             $total = $this->unit_price() * $this->quantity();
1225 1225
         } elseif ($this->is_sub_total() || $this->is_total()) {
1226 1226
             $total = $this->_recalculate_pretax_total_for_subtotal($total, $my_children);
@@ -1229,18 +1229,18 @@  discard block
 block discarded – undo
1229 1229
             return 0;
1230 1230
         }
1231 1231
         // ensure all non-line items and non-sub-line-items have a quantity of 1 (except for Events)
1232
-        if (! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1232
+        if ( ! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1233 1233
         ) {
1234 1234
             if ($this->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_EVENT) {
1235 1235
                 $this->set_quantity(1);
1236 1236
             }
1237
-            if (! $this->is_percent()) {
1237
+            if ( ! $this->is_percent()) {
1238 1238
                 $this->set_unit_price($total);
1239 1239
             }
1240 1240
         }
1241 1241
         // we don't want to bother saving grand totals, because that needs to factor in taxes anyways
1242 1242
         // so it ought to be
1243
-        if (! $this->is_total()) {
1243
+        if ( ! $this->is_total()) {
1244 1244
             $this->set_total($total);
1245 1245
             // if not a percent line item, make sure we keep the unit price in sync
1246 1246
             if ($has_children
@@ -1587,7 +1587,7 @@  discard block
 block discarded – undo
1587 1587
     public function save_this_and_descendants_to_txn($txn_id = null)
1588 1588
     {
1589 1589
         $count = 0;
1590
-        if (! $txn_id) {
1590
+        if ( ! $txn_id) {
1591 1591
             $txn_id = $this->TXN_ID();
1592 1592
         }
1593 1593
         $this->set_TXN_ID($txn_id);
Please login to merge, or discard this patch.
Indentation   +1735 added lines, -1735 removed lines patch added patch discarded remove patch
@@ -14,1739 +14,1739 @@
 block discarded – undo
14 14
 class EE_Line_Item extends EE_Base_Class implements EEI_Line_Item
15 15
 {
16 16
 
17
-    /**
18
-     * for children line items (currently not a normal relation)
19
-     *
20
-     * @type EE_Line_Item[]
21
-     */
22
-    protected $_children = array();
23
-
24
-    /**
25
-     * for the parent line item
26
-     *
27
-     * @var EE_Line_Item
28
-     */
29
-    protected $_parent;
30
-
31
-
32
-    /**
33
-     * @param array  $props_n_values          incoming values
34
-     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
35
-     *                                        used.)
36
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
37
-     *                                        date_format and the second value is the time format
38
-     * @return EE_Line_Item
39
-     * @throws EE_Error
40
-     * @throws InvalidArgumentException
41
-     * @throws InvalidDataTypeException
42
-     * @throws InvalidInterfaceException
43
-     * @throws ReflectionException
44
-     */
45
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
46
-    {
47
-        $has_object = parent::_check_for_object(
48
-            $props_n_values,
49
-            __CLASS__,
50
-            $timezone,
51
-            $date_formats
52
-        );
53
-        return $has_object
54
-            ? $has_object
55
-            : new self($props_n_values, false, $timezone);
56
-    }
57
-
58
-
59
-    /**
60
-     * @param array  $props_n_values  incoming values from the database
61
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
62
-     *                                the website will be used.
63
-     * @return EE_Line_Item
64
-     * @throws EE_Error
65
-     * @throws InvalidArgumentException
66
-     * @throws InvalidDataTypeException
67
-     * @throws InvalidInterfaceException
68
-     * @throws ReflectionException
69
-     */
70
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
71
-    {
72
-        return new self($props_n_values, true, $timezone);
73
-    }
74
-
75
-
76
-    /**
77
-     * Adds some defaults if they're not specified
78
-     *
79
-     * @param array  $fieldValues
80
-     * @param bool   $bydb
81
-     * @param string $timezone
82
-     * @throws EE_Error
83
-     * @throws InvalidArgumentException
84
-     * @throws InvalidDataTypeException
85
-     * @throws InvalidInterfaceException
86
-     * @throws ReflectionException
87
-     */
88
-    protected function __construct($fieldValues = array(), $bydb = false, $timezone = '')
89
-    {
90
-        parent::__construct($fieldValues, $bydb, $timezone);
91
-        if (! $this->get('LIN_code')) {
92
-            $this->set_code($this->generate_code());
93
-        }
94
-    }
95
-
96
-
97
-    /**
98
-     * Gets ID
99
-     *
100
-     * @return int
101
-     * @throws EE_Error
102
-     * @throws InvalidArgumentException
103
-     * @throws InvalidDataTypeException
104
-     * @throws InvalidInterfaceException
105
-     * @throws ReflectionException
106
-     */
107
-    public function ID()
108
-    {
109
-        return $this->get('LIN_ID');
110
-    }
111
-
112
-
113
-    /**
114
-     * Gets TXN_ID
115
-     *
116
-     * @return int
117
-     * @throws EE_Error
118
-     * @throws InvalidArgumentException
119
-     * @throws InvalidDataTypeException
120
-     * @throws InvalidInterfaceException
121
-     * @throws ReflectionException
122
-     */
123
-    public function TXN_ID()
124
-    {
125
-        return $this->get('TXN_ID');
126
-    }
127
-
128
-
129
-    /**
130
-     * Sets TXN_ID
131
-     *
132
-     * @param int $TXN_ID
133
-     * @throws EE_Error
134
-     * @throws InvalidArgumentException
135
-     * @throws InvalidDataTypeException
136
-     * @throws InvalidInterfaceException
137
-     * @throws ReflectionException
138
-     */
139
-    public function set_TXN_ID($TXN_ID)
140
-    {
141
-        $this->set('TXN_ID', $TXN_ID);
142
-    }
143
-
144
-
145
-    /**
146
-     * Gets name
147
-     *
148
-     * @return string
149
-     * @throws EE_Error
150
-     * @throws InvalidArgumentException
151
-     * @throws InvalidDataTypeException
152
-     * @throws InvalidInterfaceException
153
-     * @throws ReflectionException
154
-     */
155
-    public function name()
156
-    {
157
-        $name = $this->get('LIN_name');
158
-        if (! $name) {
159
-            $name = ucwords(str_replace('-', ' ', $this->type()));
160
-        }
161
-        return $name;
162
-    }
163
-
164
-
165
-    /**
166
-     * Sets name
167
-     *
168
-     * @param string $name
169
-     * @throws EE_Error
170
-     * @throws InvalidArgumentException
171
-     * @throws InvalidDataTypeException
172
-     * @throws InvalidInterfaceException
173
-     * @throws ReflectionException
174
-     */
175
-    public function set_name($name)
176
-    {
177
-        $this->set('LIN_name', $name);
178
-    }
179
-
180
-
181
-    /**
182
-     * Gets desc
183
-     *
184
-     * @return string
185
-     * @throws EE_Error
186
-     * @throws InvalidArgumentException
187
-     * @throws InvalidDataTypeException
188
-     * @throws InvalidInterfaceException
189
-     * @throws ReflectionException
190
-     */
191
-    public function desc()
192
-    {
193
-        return $this->get('LIN_desc');
194
-    }
195
-
196
-
197
-    /**
198
-     * Sets desc
199
-     *
200
-     * @param string $desc
201
-     * @throws EE_Error
202
-     * @throws InvalidArgumentException
203
-     * @throws InvalidDataTypeException
204
-     * @throws InvalidInterfaceException
205
-     * @throws ReflectionException
206
-     */
207
-    public function set_desc($desc)
208
-    {
209
-        $this->set('LIN_desc', $desc);
210
-    }
211
-
212
-
213
-    /**
214
-     * Gets quantity
215
-     *
216
-     * @return int
217
-     * @throws EE_Error
218
-     * @throws InvalidArgumentException
219
-     * @throws InvalidDataTypeException
220
-     * @throws InvalidInterfaceException
221
-     * @throws ReflectionException
222
-     */
223
-    public function quantity()
224
-    {
225
-        return $this->get('LIN_quantity');
226
-    }
227
-
228
-
229
-    /**
230
-     * Sets quantity
231
-     *
232
-     * @param int $quantity
233
-     * @throws EE_Error
234
-     * @throws InvalidArgumentException
235
-     * @throws InvalidDataTypeException
236
-     * @throws InvalidInterfaceException
237
-     * @throws ReflectionException
238
-     */
239
-    public function set_quantity($quantity)
240
-    {
241
-        $this->set('LIN_quantity', max($quantity, 0));
242
-    }
243
-
244
-
245
-    /**
246
-     * Gets item_id
247
-     *
248
-     * @return string
249
-     * @throws EE_Error
250
-     * @throws InvalidArgumentException
251
-     * @throws InvalidDataTypeException
252
-     * @throws InvalidInterfaceException
253
-     * @throws ReflectionException
254
-     */
255
-    public function OBJ_ID()
256
-    {
257
-        return $this->get('OBJ_ID');
258
-    }
259
-
260
-
261
-    /**
262
-     * Sets item_id
263
-     *
264
-     * @param string $item_id
265
-     * @throws EE_Error
266
-     * @throws InvalidArgumentException
267
-     * @throws InvalidDataTypeException
268
-     * @throws InvalidInterfaceException
269
-     * @throws ReflectionException
270
-     */
271
-    public function set_OBJ_ID($item_id)
272
-    {
273
-        $this->set('OBJ_ID', $item_id);
274
-    }
275
-
276
-
277
-    /**
278
-     * Gets item_type
279
-     *
280
-     * @return string
281
-     * @throws EE_Error
282
-     * @throws InvalidArgumentException
283
-     * @throws InvalidDataTypeException
284
-     * @throws InvalidInterfaceException
285
-     * @throws ReflectionException
286
-     */
287
-    public function OBJ_type()
288
-    {
289
-        return $this->get('OBJ_type');
290
-    }
291
-
292
-
293
-    /**
294
-     * Gets item_type
295
-     *
296
-     * @return string
297
-     * @throws EE_Error
298
-     * @throws InvalidArgumentException
299
-     * @throws InvalidDataTypeException
300
-     * @throws InvalidInterfaceException
301
-     * @throws ReflectionException
302
-     */
303
-    public function OBJ_type_i18n()
304
-    {
305
-        $obj_type = $this->OBJ_type();
306
-        switch ($obj_type) {
307
-            case EEM_Line_Item::OBJ_TYPE_EVENT:
308
-                $obj_type = esc_html__('Event', 'event_espresso');
309
-                break;
310
-            case EEM_Line_Item::OBJ_TYPE_PRICE:
311
-                $obj_type = esc_html__('Price', 'event_espresso');
312
-                break;
313
-            case EEM_Line_Item::OBJ_TYPE_PROMOTION:
314
-                $obj_type = esc_html__('Promotion', 'event_espresso');
315
-                break;
316
-            case EEM_Line_Item::OBJ_TYPE_TICKET:
317
-                $obj_type = esc_html__('Ticket', 'event_espresso');
318
-                break;
319
-            case EEM_Line_Item::OBJ_TYPE_TRANSACTION:
320
-                $obj_type = esc_html__('Transaction', 'event_espresso');
321
-                break;
322
-        }
323
-        return apply_filters('FHEE__EE_Line_Item__OBJ_type_i18n', $obj_type, $this);
324
-    }
325
-
326
-
327
-    /**
328
-     * Sets item_type
329
-     *
330
-     * @param string $OBJ_type
331
-     * @throws EE_Error
332
-     * @throws InvalidArgumentException
333
-     * @throws InvalidDataTypeException
334
-     * @throws InvalidInterfaceException
335
-     * @throws ReflectionException
336
-     */
337
-    public function set_OBJ_type($OBJ_type)
338
-    {
339
-        $this->set('OBJ_type', $OBJ_type);
340
-    }
341
-
342
-
343
-    /**
344
-     * Gets unit_price
345
-     *
346
-     * @return float
347
-     * @throws EE_Error
348
-     * @throws InvalidArgumentException
349
-     * @throws InvalidDataTypeException
350
-     * @throws InvalidInterfaceException
351
-     * @throws ReflectionException
352
-     */
353
-    public function unit_price()
354
-    {
355
-        return $this->get('LIN_unit_price');
356
-    }
357
-
358
-
359
-    /**
360
-     * Sets unit_price
361
-     *
362
-     * @param float $unit_price
363
-     * @throws EE_Error
364
-     * @throws InvalidArgumentException
365
-     * @throws InvalidDataTypeException
366
-     * @throws InvalidInterfaceException
367
-     * @throws ReflectionException
368
-     */
369
-    public function set_unit_price($unit_price)
370
-    {
371
-        $this->set('LIN_unit_price', $unit_price);
372
-    }
373
-
374
-
375
-    /**
376
-     * Checks if this item is a percentage modifier or not
377
-     *
378
-     * @return boolean
379
-     * @throws EE_Error
380
-     * @throws InvalidArgumentException
381
-     * @throws InvalidDataTypeException
382
-     * @throws InvalidInterfaceException
383
-     * @throws ReflectionException
384
-     */
385
-    public function is_percent()
386
-    {
387
-        if ($this->is_tax_sub_total()) {
388
-            // tax subtotals HAVE a percent on them, that percentage only applies
389
-            // to taxable items, so its' an exception. Treat it like a flat line item
390
-            return false;
391
-        }
392
-        $unit_price = abs($this->get('LIN_unit_price'));
393
-        $percent = abs($this->get('LIN_percent'));
394
-        if ($unit_price < .001 && $percent) {
395
-            return true;
396
-        }
397
-        if ($unit_price >= .001 && ! $percent) {
398
-            return false;
399
-        }
400
-        if ($unit_price >= .001 && $percent) {
401
-            throw new EE_Error(
402
-                sprintf(
403
-                    esc_html__(
404
-                        'A Line Item can not have a unit price of (%s) AND a percent (%s)!',
405
-                        'event_espresso'
406
-                    ),
407
-                    $unit_price,
408
-                    $percent
409
-                )
410
-            );
411
-        }
412
-        // if they're both 0, assume its not a percent item
413
-        return false;
414
-    }
415
-
416
-
417
-    /**
418
-     * Gets percent (between 100-.001)
419
-     *
420
-     * @return float
421
-     * @throws EE_Error
422
-     * @throws InvalidArgumentException
423
-     * @throws InvalidDataTypeException
424
-     * @throws InvalidInterfaceException
425
-     * @throws ReflectionException
426
-     */
427
-    public function percent()
428
-    {
429
-        return $this->get('LIN_percent');
430
-    }
431
-
432
-
433
-    /**
434
-     * Sets percent (between 100-0.01)
435
-     *
436
-     * @param float $percent
437
-     * @throws EE_Error
438
-     * @throws InvalidArgumentException
439
-     * @throws InvalidDataTypeException
440
-     * @throws InvalidInterfaceException
441
-     * @throws ReflectionException
442
-     */
443
-    public function set_percent($percent)
444
-    {
445
-        $this->set('LIN_percent', $percent);
446
-    }
447
-
448
-
449
-    /**
450
-     * Gets total
451
-     *
452
-     * @return float
453
-     * @throws EE_Error
454
-     * @throws InvalidArgumentException
455
-     * @throws InvalidDataTypeException
456
-     * @throws InvalidInterfaceException
457
-     * @throws ReflectionException
458
-     */
459
-    public function total()
460
-    {
461
-        return $this->get('LIN_total');
462
-    }
463
-
464
-
465
-    /**
466
-     * Sets total
467
-     *
468
-     * @param float $total
469
-     * @throws EE_Error
470
-     * @throws InvalidArgumentException
471
-     * @throws InvalidDataTypeException
472
-     * @throws InvalidInterfaceException
473
-     * @throws ReflectionException
474
-     */
475
-    public function set_total($total)
476
-    {
477
-        $this->set('LIN_total', $total);
478
-    }
479
-
480
-
481
-    /**
482
-     * Gets order
483
-     *
484
-     * @return int
485
-     * @throws EE_Error
486
-     * @throws InvalidArgumentException
487
-     * @throws InvalidDataTypeException
488
-     * @throws InvalidInterfaceException
489
-     * @throws ReflectionException
490
-     */
491
-    public function order()
492
-    {
493
-        return $this->get('LIN_order');
494
-    }
495
-
496
-
497
-    /**
498
-     * Sets order
499
-     *
500
-     * @param int $order
501
-     * @throws EE_Error
502
-     * @throws InvalidArgumentException
503
-     * @throws InvalidDataTypeException
504
-     * @throws InvalidInterfaceException
505
-     * @throws ReflectionException
506
-     */
507
-    public function set_order($order)
508
-    {
509
-        $this->set('LIN_order', $order);
510
-    }
511
-
512
-
513
-    /**
514
-     * Gets parent
515
-     *
516
-     * @return int
517
-     * @throws EE_Error
518
-     * @throws InvalidArgumentException
519
-     * @throws InvalidDataTypeException
520
-     * @throws InvalidInterfaceException
521
-     * @throws ReflectionException
522
-     */
523
-    public function parent_ID()
524
-    {
525
-        return $this->get('LIN_parent');
526
-    }
527
-
528
-
529
-    /**
530
-     * Sets parent
531
-     *
532
-     * @param int $parent
533
-     * @throws EE_Error
534
-     * @throws InvalidArgumentException
535
-     * @throws InvalidDataTypeException
536
-     * @throws InvalidInterfaceException
537
-     * @throws ReflectionException
538
-     */
539
-    public function set_parent_ID($parent)
540
-    {
541
-        $this->set('LIN_parent', $parent);
542
-    }
543
-
544
-
545
-    /**
546
-     * Gets type
547
-     *
548
-     * @return string
549
-     * @throws EE_Error
550
-     * @throws InvalidArgumentException
551
-     * @throws InvalidDataTypeException
552
-     * @throws InvalidInterfaceException
553
-     * @throws ReflectionException
554
-     */
555
-    public function type()
556
-    {
557
-        return $this->get('LIN_type');
558
-    }
559
-
560
-
561
-    /**
562
-     * Sets type
563
-     *
564
-     * @param string $type
565
-     * @throws EE_Error
566
-     * @throws InvalidArgumentException
567
-     * @throws InvalidDataTypeException
568
-     * @throws InvalidInterfaceException
569
-     * @throws ReflectionException
570
-     */
571
-    public function set_type($type)
572
-    {
573
-        $this->set('LIN_type', $type);
574
-    }
575
-
576
-
577
-    /**
578
-     * Gets the line item of which this item is a composite. Eg, if this is a subtotal, the parent might be a total\
579
-     * If this line item is saved to the DB, fetches the parent from the DB. However, if this line item isn't in the DB
580
-     * it uses its cached reference to its parent line item (which would have been set by `EE_Line_Item::set_parent()`
581
-     * or indirectly by `EE_Line_item::add_child_line_item()`)
582
-     *
583
-     * @return EE_Base_Class|EE_Line_Item
584
-     * @throws EE_Error
585
-     * @throws InvalidArgumentException
586
-     * @throws InvalidDataTypeException
587
-     * @throws InvalidInterfaceException
588
-     * @throws ReflectionException
589
-     */
590
-    public function parent()
591
-    {
592
-        return $this->ID()
593
-            ? $this->get_model()->get_one_by_ID($this->parent_ID())
594
-            : $this->_parent;
595
-    }
596
-
597
-
598
-    /**
599
-     * Gets ALL the children of this line item (ie, all the parts that contribute towards this total).
600
-     *
601
-     * @return EE_Base_Class[]|EE_Line_Item[]
602
-     * @throws EE_Error
603
-     * @throws InvalidArgumentException
604
-     * @throws InvalidDataTypeException
605
-     * @throws InvalidInterfaceException
606
-     * @throws ReflectionException
607
-     */
608
-    public function children()
609
-    {
610
-        if ($this->ID()) {
611
-            return $this->get_model()->get_all(
612
-                array(
613
-                    array('LIN_parent' => $this->ID()),
614
-                    'order_by' => array('LIN_order' => 'ASC'),
615
-                )
616
-            );
617
-        }
618
-        if (! is_array($this->_children)) {
619
-            $this->_children = array();
620
-        }
621
-        return $this->_children;
622
-    }
623
-
624
-
625
-    /**
626
-     * Gets code
627
-     *
628
-     * @return string
629
-     * @throws EE_Error
630
-     * @throws InvalidArgumentException
631
-     * @throws InvalidDataTypeException
632
-     * @throws InvalidInterfaceException
633
-     * @throws ReflectionException
634
-     */
635
-    public function code()
636
-    {
637
-        return $this->get('LIN_code');
638
-    }
639
-
640
-
641
-    /**
642
-     * Sets code
643
-     *
644
-     * @param string $code
645
-     * @throws EE_Error
646
-     * @throws InvalidArgumentException
647
-     * @throws InvalidDataTypeException
648
-     * @throws InvalidInterfaceException
649
-     * @throws ReflectionException
650
-     */
651
-    public function set_code($code)
652
-    {
653
-        $this->set('LIN_code', $code);
654
-    }
655
-
656
-
657
-    /**
658
-     * Gets is_taxable
659
-     *
660
-     * @return boolean
661
-     * @throws EE_Error
662
-     * @throws InvalidArgumentException
663
-     * @throws InvalidDataTypeException
664
-     * @throws InvalidInterfaceException
665
-     * @throws ReflectionException
666
-     */
667
-    public function is_taxable()
668
-    {
669
-        return $this->get('LIN_is_taxable');
670
-    }
671
-
672
-
673
-    /**
674
-     * Sets is_taxable
675
-     *
676
-     * @param boolean $is_taxable
677
-     * @throws EE_Error
678
-     * @throws InvalidArgumentException
679
-     * @throws InvalidDataTypeException
680
-     * @throws InvalidInterfaceException
681
-     * @throws ReflectionException
682
-     */
683
-    public function set_is_taxable($is_taxable)
684
-    {
685
-        $this->set('LIN_is_taxable', $is_taxable);
686
-    }
687
-
688
-
689
-    /**
690
-     * Gets the object that this model-joins-to.
691
-     * returns one of the model objects that the field OBJ_ID can point to... see the 'OBJ_ID' field on
692
-     * EEM_Promotion_Object
693
-     *        Eg, if this line item join model object is for a ticket, this will return the EE_Ticket object
694
-     *
695
-     * @return EE_Base_Class | NULL
696
-     * @throws EE_Error
697
-     * @throws InvalidArgumentException
698
-     * @throws InvalidDataTypeException
699
-     * @throws InvalidInterfaceException
700
-     * @throws ReflectionException
701
-     */
702
-    public function get_object()
703
-    {
704
-        $model_name_of_related_obj = $this->OBJ_type();
705
-        return $this->get_model()->has_relation($model_name_of_related_obj)
706
-            ? $this->get_first_related($model_name_of_related_obj)
707
-            : null;
708
-    }
709
-
710
-
711
-    /**
712
-     * Like EE_Line_Item::get_object(), but can only ever actually return an EE_Ticket.
713
-     * (IE, if this line item is for a price or something else, will return NULL)
714
-     *
715
-     * @param array $query_params
716
-     * @return EE_Base_Class|EE_Ticket
717
-     * @throws EE_Error
718
-     * @throws InvalidArgumentException
719
-     * @throws InvalidDataTypeException
720
-     * @throws InvalidInterfaceException
721
-     * @throws ReflectionException
722
-     */
723
-    public function ticket($query_params = array())
724
-    {
725
-        // we're going to assume that when this method is called
726
-        // we always want to receive the attached ticket EVEN if that ticket is archived.
727
-        // This can be overridden via the incoming $query_params argument
728
-        $remove_defaults = array('default_where_conditions' => 'none');
729
-        $query_params = array_merge($remove_defaults, $query_params);
730
-        return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TICKET, $query_params);
731
-    }
732
-
733
-
734
-    /**
735
-     * Gets the EE_Datetime that's related to the ticket, IF this is for a ticket
736
-     *
737
-     * @return EE_Datetime | NULL
738
-     * @throws EE_Error
739
-     * @throws InvalidArgumentException
740
-     * @throws InvalidDataTypeException
741
-     * @throws InvalidInterfaceException
742
-     * @throws ReflectionException
743
-     */
744
-    public function get_ticket_datetime()
745
-    {
746
-        if ($this->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
747
-            $ticket = $this->ticket();
748
-            if ($ticket instanceof EE_Ticket) {
749
-                $datetime = $ticket->first_datetime();
750
-                if ($datetime instanceof EE_Datetime) {
751
-                    return $datetime;
752
-                }
753
-            }
754
-        }
755
-        return null;
756
-    }
757
-
758
-
759
-    /**
760
-     * Gets the event's name that's related to the ticket, if this is for
761
-     * a ticket
762
-     *
763
-     * @return string
764
-     * @throws EE_Error
765
-     * @throws InvalidArgumentException
766
-     * @throws InvalidDataTypeException
767
-     * @throws InvalidInterfaceException
768
-     * @throws ReflectionException
769
-     */
770
-    public function ticket_event_name()
771
-    {
772
-        $event_name = esc_html__('Unknown', 'event_espresso');
773
-        $event = $this->ticket_event();
774
-        if ($event instanceof EE_Event) {
775
-            $event_name = $event->name();
776
-        }
777
-        return $event_name;
778
-    }
779
-
780
-
781
-    /**
782
-     * Gets the event that's related to the ticket, if this line item represents a ticket.
783
-     *
784
-     * @return EE_Event|null
785
-     * @throws EE_Error
786
-     * @throws InvalidArgumentException
787
-     * @throws InvalidDataTypeException
788
-     * @throws InvalidInterfaceException
789
-     * @throws ReflectionException
790
-     */
791
-    public function ticket_event()
792
-    {
793
-        $event = null;
794
-        $ticket = $this->ticket();
795
-        if ($ticket instanceof EE_Ticket) {
796
-            $datetime = $ticket->first_datetime();
797
-            if ($datetime instanceof EE_Datetime) {
798
-                $event = $datetime->event();
799
-            }
800
-        }
801
-        return $event;
802
-    }
803
-
804
-
805
-    /**
806
-     * Gets the first datetime for this lien item, assuming it's for a ticket
807
-     *
808
-     * @param string $date_format
809
-     * @param string $time_format
810
-     * @return string
811
-     * @throws EE_Error
812
-     * @throws InvalidArgumentException
813
-     * @throws InvalidDataTypeException
814
-     * @throws InvalidInterfaceException
815
-     * @throws ReflectionException
816
-     */
817
-    public function ticket_datetime_start($date_format = '', $time_format = '')
818
-    {
819
-        $first_datetime_string = esc_html__('Unknown', 'event_espresso');
820
-        $datetime = $this->get_ticket_datetime();
821
-        if ($datetime) {
822
-            $first_datetime_string = $datetime->start_date_and_time($date_format, $time_format);
823
-        }
824
-        return $first_datetime_string;
825
-    }
826
-
827
-
828
-    /**
829
-     * Adds the line item as a child to this line item. If there is another child line
830
-     * item with the same LIN_code, it is overwritten by this new one
831
-     *
832
-     * @param EEI_Line_Item $line_item
833
-     * @param bool          $set_order
834
-     * @return bool success
835
-     * @throws EE_Error
836
-     * @throws InvalidArgumentException
837
-     * @throws InvalidDataTypeException
838
-     * @throws InvalidInterfaceException
839
-     * @throws ReflectionException
840
-     */
841
-    public function add_child_line_item(EEI_Line_Item $line_item, $set_order = true)
842
-    {
843
-        // should we calculate the LIN_order for this line item ?
844
-        if ($set_order || $line_item->order() === null) {
845
-            $line_item->set_order(count($this->children()));
846
-        }
847
-        if ($this->ID()) {
848
-            // check for any duplicate line items (with the same code), if so, this replaces it
849
-            $line_item_with_same_code = $this->get_child_line_item($line_item->code());
850
-            if ($line_item_with_same_code instanceof EE_Line_Item && $line_item_with_same_code !== $line_item) {
851
-                $this->delete_child_line_item($line_item_with_same_code->code());
852
-            }
853
-            $line_item->set_parent_ID($this->ID());
854
-            if ($this->TXN_ID()) {
855
-                $line_item->set_TXN_ID($this->TXN_ID());
856
-            }
857
-            return $line_item->save();
858
-        }
859
-        $this->_children[ $line_item->code() ] = $line_item;
860
-        if ($line_item->parent() !== $this) {
861
-            $line_item->set_parent($this);
862
-        }
863
-        return true;
864
-    }
865
-
866
-
867
-    /**
868
-     * Similar to EE_Base_Class::_add_relation_to, except this isn't a normal relation.
869
-     * If this line item is saved to the DB, this is just a wrapper for set_parent_ID() and save()
870
-     * However, if this line item is NOT saved to the DB, this just caches the parent on
871
-     * the EE_Line_Item::_parent property.
872
-     *
873
-     * @param EE_Line_Item $line_item
874
-     * @throws EE_Error
875
-     * @throws InvalidArgumentException
876
-     * @throws InvalidDataTypeException
877
-     * @throws InvalidInterfaceException
878
-     * @throws ReflectionException
879
-     */
880
-    public function set_parent($line_item)
881
-    {
882
-        if ($this->ID()) {
883
-            if (! $line_item->ID()) {
884
-                $line_item->save();
885
-            }
886
-            $this->set_parent_ID($line_item->ID());
887
-            $this->save();
888
-        } else {
889
-            $this->_parent = $line_item;
890
-            $this->set_parent_ID($line_item->ID());
891
-        }
892
-    }
893
-
894
-
895
-    /**
896
-     * Gets the child line item as specified by its code. Because this returns an object (by reference)
897
-     * you can modify this child line item and the parent (this object) can know about them
898
-     * because it also has a reference to that line item
899
-     *
900
-     * @param string $code
901
-     * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
902
-     * @throws EE_Error
903
-     * @throws InvalidArgumentException
904
-     * @throws InvalidDataTypeException
905
-     * @throws InvalidInterfaceException
906
-     * @throws ReflectionException
907
-     */
908
-    public function get_child_line_item($code)
909
-    {
910
-        if ($this->ID()) {
911
-            return $this->get_model()->get_one(
912
-                array(array('LIN_parent' => $this->ID(), 'LIN_code' => $code))
913
-            );
914
-        }
915
-        return isset($this->_children[ $code ])
916
-            ? $this->_children[ $code ]
917
-            : null;
918
-    }
919
-
920
-
921
-    /**
922
-     * Returns how many items are deleted (or, if this item has not been saved ot the DB yet, just how many it HAD
923
-     * cached on it)
924
-     *
925
-     * @return int
926
-     * @throws EE_Error
927
-     * @throws InvalidArgumentException
928
-     * @throws InvalidDataTypeException
929
-     * @throws InvalidInterfaceException
930
-     * @throws ReflectionException
931
-     */
932
-    public function delete_children_line_items()
933
-    {
934
-        if ($this->ID()) {
935
-            return $this->get_model()->delete(array(array('LIN_parent' => $this->ID())));
936
-        }
937
-        $count = count($this->_children);
938
-        $this->_children = array();
939
-        return $count;
940
-    }
941
-
942
-
943
-    /**
944
-     * If this line item has been saved to the DB, deletes its child with LIN_code == $code. If this line
945
-     * HAS NOT been saved to the DB, removes the child line item with index $code.
946
-     * Also searches through the child's children for a matching line item. However, once a line item has been found
947
-     * and deleted, stops searching (so if there are line items with duplicate codes, only the first one found will be
948
-     * deleted)
949
-     *
950
-     * @param string $code
951
-     * @param bool   $stop_search_once_found
952
-     * @return int count of items deleted (or simply removed from the line item's cache, if not has not been saved to
953
-     *             the DB yet)
954
-     * @throws EE_Error
955
-     * @throws InvalidArgumentException
956
-     * @throws InvalidDataTypeException
957
-     * @throws InvalidInterfaceException
958
-     * @throws ReflectionException
959
-     */
960
-    public function delete_child_line_item($code, $stop_search_once_found = true)
961
-    {
962
-        if ($this->ID()) {
963
-            $items_deleted = 0;
964
-            if ($this->code() === $code) {
965
-                $items_deleted += EEH_Line_Item::delete_all_child_items($this);
966
-                $items_deleted += (int) $this->delete();
967
-                if ($stop_search_once_found) {
968
-                    return $items_deleted;
969
-                }
970
-            }
971
-            foreach ($this->children() as $child_line_item) {
972
-                $items_deleted += $child_line_item->delete_child_line_item($code, $stop_search_once_found);
973
-            }
974
-            return $items_deleted;
975
-        }
976
-        if (isset($this->_children[ $code ])) {
977
-            unset($this->_children[ $code ]);
978
-            return 1;
979
-        }
980
-        return 0;
981
-    }
982
-
983
-
984
-    /**
985
-     * If this line item is in the database, is of the type subtotal, and
986
-     * has no children, why do we have it? It should be deleted so this function
987
-     * does that
988
-     *
989
-     * @return boolean
990
-     * @throws EE_Error
991
-     * @throws InvalidArgumentException
992
-     * @throws InvalidDataTypeException
993
-     * @throws InvalidInterfaceException
994
-     * @throws ReflectionException
995
-     */
996
-    public function delete_if_childless_subtotal()
997
-    {
998
-        if ($this->ID() && $this->type() === EEM_Line_Item::type_sub_total && ! $this->children()) {
999
-            return $this->delete();
1000
-        }
1001
-        return false;
1002
-    }
1003
-
1004
-
1005
-    /**
1006
-     * Creates a code and returns a string. doesn't assign the code to this model object
1007
-     *
1008
-     * @return string
1009
-     * @throws EE_Error
1010
-     * @throws InvalidArgumentException
1011
-     * @throws InvalidDataTypeException
1012
-     * @throws InvalidInterfaceException
1013
-     * @throws ReflectionException
1014
-     */
1015
-    public function generate_code()
1016
-    {
1017
-        // each line item in the cart requires a unique identifier
1018
-        return md5($this->get('OBJ_type') . $this->get('OBJ_ID') . microtime());
1019
-    }
1020
-
1021
-
1022
-    /**
1023
-     * @return bool
1024
-     * @throws EE_Error
1025
-     * @throws InvalidArgumentException
1026
-     * @throws InvalidDataTypeException
1027
-     * @throws InvalidInterfaceException
1028
-     * @throws ReflectionException
1029
-     */
1030
-    public function is_tax()
1031
-    {
1032
-        return $this->type() === EEM_Line_Item::type_tax;
1033
-    }
1034
-
1035
-
1036
-    /**
1037
-     * @return bool
1038
-     * @throws EE_Error
1039
-     * @throws InvalidArgumentException
1040
-     * @throws InvalidDataTypeException
1041
-     * @throws InvalidInterfaceException
1042
-     * @throws ReflectionException
1043
-     */
1044
-    public function is_tax_sub_total()
1045
-    {
1046
-        return $this->type() === EEM_Line_Item::type_tax_sub_total;
1047
-    }
1048
-
1049
-
1050
-    /**
1051
-     * @return bool
1052
-     * @throws EE_Error
1053
-     * @throws InvalidArgumentException
1054
-     * @throws InvalidDataTypeException
1055
-     * @throws InvalidInterfaceException
1056
-     * @throws ReflectionException
1057
-     */
1058
-    public function is_line_item()
1059
-    {
1060
-        return $this->type() === EEM_Line_Item::type_line_item;
1061
-    }
1062
-
1063
-
1064
-    /**
1065
-     * @return bool
1066
-     * @throws EE_Error
1067
-     * @throws InvalidArgumentException
1068
-     * @throws InvalidDataTypeException
1069
-     * @throws InvalidInterfaceException
1070
-     * @throws ReflectionException
1071
-     */
1072
-    public function is_sub_line_item()
1073
-    {
1074
-        return $this->type() === EEM_Line_Item::type_sub_line_item;
1075
-    }
1076
-
1077
-
1078
-    /**
1079
-     * @return bool
1080
-     * @throws EE_Error
1081
-     * @throws InvalidArgumentException
1082
-     * @throws InvalidDataTypeException
1083
-     * @throws InvalidInterfaceException
1084
-     * @throws ReflectionException
1085
-     */
1086
-    public function is_sub_total()
1087
-    {
1088
-        return $this->type() === EEM_Line_Item::type_sub_total;
1089
-    }
1090
-
1091
-
1092
-    /**
1093
-     * Whether or not this line item is a cancellation line item
1094
-     *
1095
-     * @return boolean
1096
-     * @throws EE_Error
1097
-     * @throws InvalidArgumentException
1098
-     * @throws InvalidDataTypeException
1099
-     * @throws InvalidInterfaceException
1100
-     * @throws ReflectionException
1101
-     */
1102
-    public function is_cancellation()
1103
-    {
1104
-        return EEM_Line_Item::type_cancellation === $this->type();
1105
-    }
1106
-
1107
-
1108
-    /**
1109
-     * @return bool
1110
-     * @throws EE_Error
1111
-     * @throws InvalidArgumentException
1112
-     * @throws InvalidDataTypeException
1113
-     * @throws InvalidInterfaceException
1114
-     * @throws ReflectionException
1115
-     */
1116
-    public function is_total()
1117
-    {
1118
-        return $this->type() === EEM_Line_Item::type_total;
1119
-    }
1120
-
1121
-
1122
-    /**
1123
-     * @return bool
1124
-     * @throws EE_Error
1125
-     * @throws InvalidArgumentException
1126
-     * @throws InvalidDataTypeException
1127
-     * @throws InvalidInterfaceException
1128
-     * @throws ReflectionException
1129
-     */
1130
-    public function is_cancelled()
1131
-    {
1132
-        return $this->type() === EEM_Line_Item::type_cancellation;
1133
-    }
1134
-
1135
-
1136
-    /**
1137
-     * @return string like '2, 004.00', formatted according to the localized currency
1138
-     * @throws EE_Error
1139
-     * @throws InvalidArgumentException
1140
-     * @throws InvalidDataTypeException
1141
-     * @throws InvalidInterfaceException
1142
-     * @throws ReflectionException
1143
-     */
1144
-    public function unit_price_no_code()
1145
-    {
1146
-        return $this->get_pretty('LIN_unit_price', 'no_currency_code');
1147
-    }
1148
-
1149
-
1150
-    /**
1151
-     * @return string like '2, 004.00', formatted according to the localized currency
1152
-     * @throws EE_Error
1153
-     * @throws InvalidArgumentException
1154
-     * @throws InvalidDataTypeException
1155
-     * @throws InvalidInterfaceException
1156
-     * @throws ReflectionException
1157
-     */
1158
-    public function total_no_code()
1159
-    {
1160
-        return $this->get_pretty('LIN_total', 'no_currency_code');
1161
-    }
1162
-
1163
-
1164
-    /**
1165
-     * Gets the final total on this item, taking taxes into account.
1166
-     * Has the side-effect of setting the sub-total as it was just calculated.
1167
-     * If this is used on a grand-total line item, also updates the transaction's
1168
-     * TXN_total (provided this line item is allowed to persist, otherwise we don't
1169
-     * want to change a persistable transaction with info from a non-persistent line item)
1170
-     *
1171
-     * @param bool $update_txn_status
1172
-     * @return float
1173
-     * @throws EE_Error
1174
-     * @throws InvalidArgumentException
1175
-     * @throws InvalidDataTypeException
1176
-     * @throws InvalidInterfaceException
1177
-     * @throws ReflectionException
1178
-     * @throws RuntimeException
1179
-     */
1180
-    public function recalculate_total_including_taxes($update_txn_status = false)
1181
-    {
1182
-        $pre_tax_total = $this->recalculate_pre_tax_total();
1183
-        $tax_total = $this->recalculate_taxes_and_tax_total();
1184
-        $total = $pre_tax_total + $tax_total;
1185
-        // no negative totals plz
1186
-        $total = max($total, 0);
1187
-        $this->set_total($total);
1188
-        // only update the related transaction's total
1189
-        // if we intend to save this line item and its a grand total
1190
-        if ($this->allow_persist() && $this->type() === EEM_Line_Item::type_total
1191
-            && $this->transaction()
1192
-               instanceof
1193
-               EE_Transaction
1194
-        ) {
1195
-            $this->transaction()->set_total($total);
1196
-            if ($update_txn_status) {
1197
-                // don't save the TXN because that will be done below
1198
-                // and the following method only saves if the status changes
1199
-                $this->transaction()->update_status_based_on_total_paid(false);
1200
-            }
1201
-            if ($this->transaction()->ID()) {
1202
-                $this->transaction()->save();
1203
-            }
1204
-        }
1205
-        $this->maybe_save();
1206
-        return $total;
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     * Recursively goes through all the children and recalculates sub-totals EXCEPT for
1212
-     * tax-sub-totals (they're a an odd beast). Updates the 'total' on each line item according to either its
1213
-     * unit price * quantity or the total of all its children EXCEPT when we're only calculating the taxable total and
1214
-     * when this is called on the grand total
1215
-     *
1216
-     * @return float
1217
-     * @throws EE_Error
1218
-     * @throws InvalidArgumentException
1219
-     * @throws InvalidDataTypeException
1220
-     * @throws InvalidInterfaceException
1221
-     * @throws ReflectionException
1222
-     */
1223
-    public function recalculate_pre_tax_total()
1224
-    {
1225
-        $total = 0;
1226
-        $my_children = $this->children();
1227
-        $has_children = ! empty($my_children);
1228
-        if ($has_children && $this->is_line_item()) {
1229
-            $total = $this->_recalculate_pretax_total_for_line_item($total, $my_children);
1230
-        } elseif (! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1231
-            $total = $this->unit_price() * $this->quantity();
1232
-        } elseif ($this->is_sub_total() || $this->is_total()) {
1233
-            $total = $this->_recalculate_pretax_total_for_subtotal($total, $my_children);
1234
-        } elseif ($this->is_tax_sub_total() || $this->is_tax() || $this->is_cancelled()) {
1235
-            // completely ignore tax totals, tax sub-totals, and cancelled line items, when calculating the pre-tax-total
1236
-            return 0;
1237
-        }
1238
-        // ensure all non-line items and non-sub-line-items have a quantity of 1 (except for Events)
1239
-        if (! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1240
-        ) {
1241
-            if ($this->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_EVENT) {
1242
-                $this->set_quantity(1);
1243
-            }
1244
-            if (! $this->is_percent()) {
1245
-                $this->set_unit_price($total);
1246
-            }
1247
-        }
1248
-        // we don't want to bother saving grand totals, because that needs to factor in taxes anyways
1249
-        // so it ought to be
1250
-        if (! $this->is_total()) {
1251
-            $this->set_total($total);
1252
-            // if not a percent line item, make sure we keep the unit price in sync
1253
-            if ($has_children
1254
-                && $this->is_line_item()
1255
-                && ! $this->is_percent()
1256
-            ) {
1257
-                if ($this->quantity() === 0) {
1258
-                    $new_unit_price = 0;
1259
-                } else {
1260
-                    $new_unit_price = $this->total() / $this->quantity();
1261
-                }
1262
-                $this->set_unit_price($new_unit_price);
1263
-            }
1264
-            $this->maybe_save();
1265
-        }
1266
-        return $total;
1267
-    }
1268
-
1269
-
1270
-    /**
1271
-     * Calculates the pretax total when this line item is a subtotal or total line item.
1272
-     * Basically does a sum-then-round approach (ie, any percent line item that are children
1273
-     * will calculate their total based on the un-rounded total we're working with so far, and
1274
-     * THEN round the result; instead of rounding as we go like with sub-line-items)
1275
-     *
1276
-     * @param float          $calculated_total_so_far
1277
-     * @param EE_Line_Item[] $my_children
1278
-     * @return float
1279
-     * @throws EE_Error
1280
-     * @throws InvalidArgumentException
1281
-     * @throws InvalidDataTypeException
1282
-     * @throws InvalidInterfaceException
1283
-     * @throws ReflectionException
1284
-     */
1285
-    protected function _recalculate_pretax_total_for_subtotal($calculated_total_so_far, $my_children = null)
1286
-    {
1287
-        if ($my_children === null) {
1288
-            $my_children = $this->children();
1289
-        }
1290
-        $subtotal_quantity = 0;
1291
-        // get the total of all its children
1292
-        foreach ($my_children as $child_line_item) {
1293
-            if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1294
-                // percentage line items are based on total so far
1295
-                if ($child_line_item->is_percent()) {
1296
-                    // round as we go so that the line items add up ok
1297
-                    $percent_total = round(
1298
-                        $calculated_total_so_far * $child_line_item->percent() / 100,
1299
-                        EE_Registry::instance()->CFG->currency->dec_plc
1300
-                    );
1301
-                    $child_line_item->set_total($percent_total);
1302
-                    // so far all percent line items should have a quantity of 1
1303
-                    // (ie, no double percent discounts. Although that might be requested someday)
1304
-                    $child_line_item->set_quantity(1);
1305
-                    $child_line_item->maybe_save();
1306
-                    $calculated_total_so_far += $percent_total;
1307
-                } else {
1308
-                    // verify flat sub-line-item quantities match their parent
1309
-                    if ($child_line_item->is_sub_line_item()) {
1310
-                        $child_line_item->set_quantity($this->quantity());
1311
-                    }
1312
-                    $calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1313
-                    $subtotal_quantity += $child_line_item->quantity();
1314
-                }
1315
-            }
1316
-        }
1317
-        if ($this->is_sub_total()) {
1318
-            // no negative totals plz
1319
-            $calculated_total_so_far = max($calculated_total_so_far, 0);
1320
-            $subtotal_quantity = $subtotal_quantity > 0 ? 1 : 0;
1321
-            $this->set_quantity($subtotal_quantity);
1322
-            $this->maybe_save();
1323
-        }
1324
-        return $calculated_total_so_far;
1325
-    }
1326
-
1327
-
1328
-    /**
1329
-     * Calculates the pretax total for a normal line item, in a round-then-sum approach
1330
-     * (where each sub-line-item is applied to the base price for the line item
1331
-     * and the result is immediately rounded, rather than summing all the sub-line-items
1332
-     * then rounding, like we do when recalculating pretax totals on totals and subtotals).
1333
-     *
1334
-     * @param float          $calculated_total_so_far
1335
-     * @param EE_Line_Item[] $my_children
1336
-     * @return float
1337
-     * @throws EE_Error
1338
-     * @throws InvalidArgumentException
1339
-     * @throws InvalidDataTypeException
1340
-     * @throws InvalidInterfaceException
1341
-     * @throws ReflectionException
1342
-     */
1343
-    protected function _recalculate_pretax_total_for_line_item($calculated_total_so_far, $my_children = null)
1344
-    {
1345
-        if ($my_children === null) {
1346
-            $my_children = $this->children();
1347
-        }
1348
-        // we need to keep track of the running total for a single item,
1349
-        // because we need to round as we go
1350
-        $unit_price_for_total = 0;
1351
-        $quantity_for_total = 1;
1352
-        // get the total of all its children
1353
-        foreach ($my_children as $child_line_item) {
1354
-            if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1355
-                if ($child_line_item->is_percent()) {
1356
-                    // it should be the unit-price-so-far multiplied by teh percent multiplied by the quantity
1357
-                    // not total multiplied by percent, because that ignores rounding along-the-way
1358
-                    $percent_unit_price = round(
1359
-                        $unit_price_for_total * $child_line_item->percent() / 100,
1360
-                        EE_Registry::instance()->CFG->currency->dec_plc
1361
-                    );
1362
-                    $percent_total = $percent_unit_price * $quantity_for_total;
1363
-                    $child_line_item->set_total($percent_total);
1364
-                    // so far all percent line items should have a quantity of 1
1365
-                    // (ie, no double percent discounts. Although that might be requested someday)
1366
-                    $child_line_item->set_quantity(1);
1367
-                    $child_line_item->maybe_save();
1368
-                    $calculated_total_so_far += $percent_total;
1369
-                    $unit_price_for_total += $percent_unit_price;
1370
-                } else {
1371
-                    // verify flat sub-line-item quantities match their parent
1372
-                    if ($child_line_item->is_sub_line_item()) {
1373
-                        $child_line_item->set_quantity($this->quantity());
1374
-                    }
1375
-                    $quantity_for_total = $child_line_item->quantity();
1376
-                    $calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1377
-                    $unit_price_for_total += $child_line_item->unit_price();
1378
-                }
1379
-            }
1380
-        }
1381
-        return $calculated_total_so_far;
1382
-    }
1383
-
1384
-
1385
-    /**
1386
-     * Recalculates the total on each individual tax (based on a recalculation of the pre-tax total), sets
1387
-     * the totals on each tax calculated, and returns the final tax total. Re-saves tax line items
1388
-     * and tax sub-total if already in the DB
1389
-     *
1390
-     * @return float
1391
-     * @throws EE_Error
1392
-     * @throws InvalidArgumentException
1393
-     * @throws InvalidDataTypeException
1394
-     * @throws InvalidInterfaceException
1395
-     * @throws ReflectionException
1396
-     */
1397
-    public function recalculate_taxes_and_tax_total()
1398
-    {
1399
-        // get all taxes
1400
-        $taxes = $this->tax_descendants();
1401
-        // calculate the pretax total
1402
-        $taxable_total = $this->taxable_total();
1403
-        $tax_total = 0;
1404
-        foreach ($taxes as $tax) {
1405
-            $total_on_this_tax = $taxable_total * $tax->percent() / 100;
1406
-            // remember the total on this line item
1407
-            $tax->set_total($total_on_this_tax);
1408
-            $tax->maybe_save();
1409
-            $tax_total += $tax->total();
1410
-        }
1411
-        $this->_recalculate_tax_sub_total();
1412
-        return $tax_total;
1413
-    }
1414
-
1415
-
1416
-    /**
1417
-     * Simply forces all the tax-sub-totals to recalculate. Assumes the taxes have been calculated
1418
-     *
1419
-     * @return void
1420
-     * @throws EE_Error
1421
-     * @throws InvalidArgumentException
1422
-     * @throws InvalidDataTypeException
1423
-     * @throws InvalidInterfaceException
1424
-     * @throws ReflectionException
1425
-     */
1426
-    private function _recalculate_tax_sub_total()
1427
-    {
1428
-        if ($this->is_tax_sub_total()) {
1429
-            $total = 0;
1430
-            $total_percent = 0;
1431
-            // simply loop through all its children (which should be taxes) and sum their total
1432
-            foreach ($this->children() as $child_tax) {
1433
-                if ($child_tax instanceof EE_Line_Item) {
1434
-                    $total += $child_tax->total();
1435
-                    $total_percent += $child_tax->percent();
1436
-                }
1437
-            }
1438
-            $this->set_total($total);
1439
-            $this->set_percent($total_percent);
1440
-            $this->maybe_save();
1441
-        } elseif ($this->is_total()) {
1442
-            foreach ($this->children() as $maybe_tax_subtotal) {
1443
-                if ($maybe_tax_subtotal instanceof EE_Line_Item) {
1444
-                    $maybe_tax_subtotal->_recalculate_tax_sub_total();
1445
-                }
1446
-            }
1447
-        }
1448
-    }
1449
-
1450
-
1451
-    /**
1452
-     * Gets the total tax on this line item. Assumes taxes have already been calculated using
1453
-     * recalculate_taxes_and_total
1454
-     *
1455
-     * @return float
1456
-     * @throws EE_Error
1457
-     * @throws InvalidArgumentException
1458
-     * @throws InvalidDataTypeException
1459
-     * @throws InvalidInterfaceException
1460
-     * @throws ReflectionException
1461
-     */
1462
-    public function get_total_tax()
1463
-    {
1464
-        $this->_recalculate_tax_sub_total();
1465
-        $total = 0;
1466
-        foreach ($this->tax_descendants() as $tax_line_item) {
1467
-            if ($tax_line_item instanceof EE_Line_Item) {
1468
-                $total += $tax_line_item->total();
1469
-            }
1470
-        }
1471
-        return $total;
1472
-    }
1473
-
1474
-
1475
-    /**
1476
-     * Gets the total for all the items purchased only
1477
-     *
1478
-     * @return float
1479
-     * @throws EE_Error
1480
-     * @throws InvalidArgumentException
1481
-     * @throws InvalidDataTypeException
1482
-     * @throws InvalidInterfaceException
1483
-     * @throws ReflectionException
1484
-     */
1485
-    public function get_items_total()
1486
-    {
1487
-        // by default, let's make sure we're consistent with the existing line item
1488
-        if ($this->is_total()) {
1489
-            $pretax_subtotal_li = EEH_Line_Item::get_pre_tax_subtotal($this);
1490
-            if ($pretax_subtotal_li instanceof EE_Line_Item) {
1491
-                return $pretax_subtotal_li->total();
1492
-            }
1493
-        }
1494
-        $total = 0;
1495
-        foreach ($this->get_items() as $item) {
1496
-            if ($item instanceof EE_Line_Item) {
1497
-                $total += $item->total();
1498
-            }
1499
-        }
1500
-        return $total;
1501
-    }
1502
-
1503
-
1504
-    /**
1505
-     * Gets all the descendants (ie, children or children of children etc) that
1506
-     * are of the type 'tax'
1507
-     *
1508
-     * @return EE_Line_Item[]
1509
-     * @throws EE_Error
1510
-     */
1511
-    public function tax_descendants()
1512
-    {
1513
-        return EEH_Line_Item::get_tax_descendants($this);
1514
-    }
1515
-
1516
-
1517
-    /**
1518
-     * Gets all the real items purchased which are children of this item
1519
-     *
1520
-     * @return EE_Line_Item[]
1521
-     * @throws EE_Error
1522
-     */
1523
-    public function get_items()
1524
-    {
1525
-        return EEH_Line_Item::get_line_item_descendants($this);
1526
-    }
1527
-
1528
-
1529
-    /**
1530
-     * Returns the amount taxable among this line item's children (or if it has no children,
1531
-     * how much of it is taxable). Does not recalculate totals or subtotals.
1532
-     * If the taxable total is negative, (eg, if none of the tickets were taxable,
1533
-     * but there is a "Taxable" discount), returns 0.
1534
-     *
1535
-     * @return float
1536
-     * @throws EE_Error
1537
-     * @throws InvalidArgumentException
1538
-     * @throws InvalidDataTypeException
1539
-     * @throws InvalidInterfaceException
1540
-     * @throws ReflectionException
1541
-     */
1542
-    public function taxable_total()
1543
-    {
1544
-        $total = 0;
1545
-        if ($this->children()) {
1546
-            foreach ($this->children() as $child_line_item) {
1547
-                if ($child_line_item->type() === EEM_Line_Item::type_line_item && $child_line_item->is_taxable()) {
1548
-                    // if it's a percent item, only take into account the percent
1549
-                    // that's taxable too (the taxable total so far)
1550
-                    if ($child_line_item->is_percent()) {
1551
-                        $total += ($total * $child_line_item->percent() / 100);
1552
-                    } else {
1553
-                        $total += $child_line_item->total();
1554
-                    }
1555
-                } elseif ($child_line_item->type() === EEM_Line_Item::type_sub_total) {
1556
-                    $total += $child_line_item->taxable_total();
1557
-                }
1558
-            }
1559
-        }
1560
-        return max($total, 0);
1561
-    }
1562
-
1563
-
1564
-    /**
1565
-     * Gets the transaction for this line item
1566
-     *
1567
-     * @return EE_Base_Class|EE_Transaction
1568
-     * @throws EE_Error
1569
-     * @throws InvalidArgumentException
1570
-     * @throws InvalidDataTypeException
1571
-     * @throws InvalidInterfaceException
1572
-     * @throws ReflectionException
1573
-     */
1574
-    public function transaction()
1575
-    {
1576
-        return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TRANSACTION);
1577
-    }
1578
-
1579
-
1580
-    /**
1581
-     * Saves this line item to the DB, and recursively saves its descendants.
1582
-     * Because there currently is no proper parent-child relation on the model,
1583
-     * save_this_and_cached() will NOT save the descendants.
1584
-     * Also sets the transaction on this line item and all its descendants before saving
1585
-     *
1586
-     * @param int $txn_id if none is provided, assumes $this->TXN_ID()
1587
-     * @return int count of items saved
1588
-     * @throws EE_Error
1589
-     * @throws InvalidArgumentException
1590
-     * @throws InvalidDataTypeException
1591
-     * @throws InvalidInterfaceException
1592
-     * @throws ReflectionException
1593
-     */
1594
-    public function save_this_and_descendants_to_txn($txn_id = null)
1595
-    {
1596
-        $count = 0;
1597
-        if (! $txn_id) {
1598
-            $txn_id = $this->TXN_ID();
1599
-        }
1600
-        $this->set_TXN_ID($txn_id);
1601
-        $children = $this->children();
1602
-        $count += $this->save()
1603
-            ? 1
1604
-            : 0;
1605
-        foreach ($children as $child_line_item) {
1606
-            if ($child_line_item instanceof EE_Line_Item) {
1607
-                $child_line_item->set_parent_ID($this->ID());
1608
-                $count += $child_line_item->save_this_and_descendants_to_txn($txn_id);
1609
-            }
1610
-        }
1611
-        return $count;
1612
-    }
1613
-
1614
-
1615
-    /**
1616
-     * Saves this line item to the DB, and recursively saves its descendants.
1617
-     *
1618
-     * @return int count of items saved
1619
-     * @throws EE_Error
1620
-     * @throws InvalidArgumentException
1621
-     * @throws InvalidDataTypeException
1622
-     * @throws InvalidInterfaceException
1623
-     * @throws ReflectionException
1624
-     */
1625
-    public function save_this_and_descendants()
1626
-    {
1627
-        $count = 0;
1628
-        $children = $this->children();
1629
-        $count += $this->save()
1630
-            ? 1
1631
-            : 0;
1632
-        foreach ($children as $child_line_item) {
1633
-            if ($child_line_item instanceof EE_Line_Item) {
1634
-                $child_line_item->set_parent_ID($this->ID());
1635
-                $count += $child_line_item->save_this_and_descendants();
1636
-            }
1637
-        }
1638
-        return $count;
1639
-    }
1640
-
1641
-
1642
-    /**
1643
-     * returns the cancellation line item if this item was cancelled
1644
-     *
1645
-     * @return EE_Line_Item[]
1646
-     * @throws InvalidArgumentException
1647
-     * @throws InvalidInterfaceException
1648
-     * @throws InvalidDataTypeException
1649
-     * @throws ReflectionException
1650
-     * @throws EE_Error
1651
-     */
1652
-    public function get_cancellations()
1653
-    {
1654
-        EE_Registry::instance()->load_helper('Line_Item');
1655
-        return EEH_Line_Item::get_descendants_of_type($this, EEM_Line_Item::type_cancellation);
1656
-    }
1657
-
1658
-
1659
-    /**
1660
-     * If this item has an ID, then this saves it again to update the db
1661
-     *
1662
-     * @return int count of items saved
1663
-     * @throws EE_Error
1664
-     * @throws InvalidArgumentException
1665
-     * @throws InvalidDataTypeException
1666
-     * @throws InvalidInterfaceException
1667
-     * @throws ReflectionException
1668
-     */
1669
-    public function maybe_save()
1670
-    {
1671
-        if ($this->ID()) {
1672
-            return $this->save();
1673
-        }
1674
-        return false;
1675
-    }
1676
-
1677
-
1678
-    /**
1679
-     * clears the cached children and parent from the line item
1680
-     *
1681
-     * @return void
1682
-     */
1683
-    public function clear_related_line_item_cache()
1684
-    {
1685
-        $this->_children = array();
1686
-        $this->_parent = null;
1687
-    }
1688
-
1689
-
1690
-    /**
1691
-     * @param bool $raw
1692
-     * @return int
1693
-     * @throws EE_Error
1694
-     * @throws InvalidArgumentException
1695
-     * @throws InvalidDataTypeException
1696
-     * @throws InvalidInterfaceException
1697
-     * @throws ReflectionException
1698
-     */
1699
-    public function timestamp($raw = false)
1700
-    {
1701
-        return $raw
1702
-            ? $this->get_raw('LIN_timestamp')
1703
-            : $this->get('LIN_timestamp');
1704
-    }
1705
-
1706
-
1707
-
1708
-
1709
-    /************************* DEPRECATED *************************/
1710
-    /**
1711
-     * @deprecated 4.6.0
1712
-     * @param string $type one of the constants on EEM_Line_Item
1713
-     * @return EE_Line_Item[]
1714
-     * @throws EE_Error
1715
-     */
1716
-    protected function _get_descendants_of_type($type)
1717
-    {
1718
-        EE_Error::doing_it_wrong(
1719
-            'EE_Line_Item::_get_descendants_of_type()',
1720
-            sprintf(
1721
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1722
-                'EEH_Line_Item::get_descendants_of_type()'
1723
-            ),
1724
-            '4.6.0'
1725
-        );
1726
-        return EEH_Line_Item::get_descendants_of_type($this, $type);
1727
-    }
1728
-
1729
-
1730
-    /**
1731
-     * @deprecated 4.6.0
1732
-     * @param string $type like one of the EEM_Line_Item::type_*
1733
-     * @return EE_Line_Item
1734
-     * @throws EE_Error
1735
-     * @throws InvalidArgumentException
1736
-     * @throws InvalidDataTypeException
1737
-     * @throws InvalidInterfaceException
1738
-     * @throws ReflectionException
1739
-     */
1740
-    public function get_nearest_descendant_of_type($type)
1741
-    {
1742
-        EE_Error::doing_it_wrong(
1743
-            'EE_Line_Item::get_nearest_descendant_of_type()',
1744
-            sprintf(
1745
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1746
-                'EEH_Line_Item::get_nearest_descendant_of_type()'
1747
-            ),
1748
-            '4.6.0'
1749
-        );
1750
-        return EEH_Line_Item::get_nearest_descendant_of_type($this, $type);
1751
-    }
17
+	/**
18
+	 * for children line items (currently not a normal relation)
19
+	 *
20
+	 * @type EE_Line_Item[]
21
+	 */
22
+	protected $_children = array();
23
+
24
+	/**
25
+	 * for the parent line item
26
+	 *
27
+	 * @var EE_Line_Item
28
+	 */
29
+	protected $_parent;
30
+
31
+
32
+	/**
33
+	 * @param array  $props_n_values          incoming values
34
+	 * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
35
+	 *                                        used.)
36
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
37
+	 *                                        date_format and the second value is the time format
38
+	 * @return EE_Line_Item
39
+	 * @throws EE_Error
40
+	 * @throws InvalidArgumentException
41
+	 * @throws InvalidDataTypeException
42
+	 * @throws InvalidInterfaceException
43
+	 * @throws ReflectionException
44
+	 */
45
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
46
+	{
47
+		$has_object = parent::_check_for_object(
48
+			$props_n_values,
49
+			__CLASS__,
50
+			$timezone,
51
+			$date_formats
52
+		);
53
+		return $has_object
54
+			? $has_object
55
+			: new self($props_n_values, false, $timezone);
56
+	}
57
+
58
+
59
+	/**
60
+	 * @param array  $props_n_values  incoming values from the database
61
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
62
+	 *                                the website will be used.
63
+	 * @return EE_Line_Item
64
+	 * @throws EE_Error
65
+	 * @throws InvalidArgumentException
66
+	 * @throws InvalidDataTypeException
67
+	 * @throws InvalidInterfaceException
68
+	 * @throws ReflectionException
69
+	 */
70
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
71
+	{
72
+		return new self($props_n_values, true, $timezone);
73
+	}
74
+
75
+
76
+	/**
77
+	 * Adds some defaults if they're not specified
78
+	 *
79
+	 * @param array  $fieldValues
80
+	 * @param bool   $bydb
81
+	 * @param string $timezone
82
+	 * @throws EE_Error
83
+	 * @throws InvalidArgumentException
84
+	 * @throws InvalidDataTypeException
85
+	 * @throws InvalidInterfaceException
86
+	 * @throws ReflectionException
87
+	 */
88
+	protected function __construct($fieldValues = array(), $bydb = false, $timezone = '')
89
+	{
90
+		parent::__construct($fieldValues, $bydb, $timezone);
91
+		if (! $this->get('LIN_code')) {
92
+			$this->set_code($this->generate_code());
93
+		}
94
+	}
95
+
96
+
97
+	/**
98
+	 * Gets ID
99
+	 *
100
+	 * @return int
101
+	 * @throws EE_Error
102
+	 * @throws InvalidArgumentException
103
+	 * @throws InvalidDataTypeException
104
+	 * @throws InvalidInterfaceException
105
+	 * @throws ReflectionException
106
+	 */
107
+	public function ID()
108
+	{
109
+		return $this->get('LIN_ID');
110
+	}
111
+
112
+
113
+	/**
114
+	 * Gets TXN_ID
115
+	 *
116
+	 * @return int
117
+	 * @throws EE_Error
118
+	 * @throws InvalidArgumentException
119
+	 * @throws InvalidDataTypeException
120
+	 * @throws InvalidInterfaceException
121
+	 * @throws ReflectionException
122
+	 */
123
+	public function TXN_ID()
124
+	{
125
+		return $this->get('TXN_ID');
126
+	}
127
+
128
+
129
+	/**
130
+	 * Sets TXN_ID
131
+	 *
132
+	 * @param int $TXN_ID
133
+	 * @throws EE_Error
134
+	 * @throws InvalidArgumentException
135
+	 * @throws InvalidDataTypeException
136
+	 * @throws InvalidInterfaceException
137
+	 * @throws ReflectionException
138
+	 */
139
+	public function set_TXN_ID($TXN_ID)
140
+	{
141
+		$this->set('TXN_ID', $TXN_ID);
142
+	}
143
+
144
+
145
+	/**
146
+	 * Gets name
147
+	 *
148
+	 * @return string
149
+	 * @throws EE_Error
150
+	 * @throws InvalidArgumentException
151
+	 * @throws InvalidDataTypeException
152
+	 * @throws InvalidInterfaceException
153
+	 * @throws ReflectionException
154
+	 */
155
+	public function name()
156
+	{
157
+		$name = $this->get('LIN_name');
158
+		if (! $name) {
159
+			$name = ucwords(str_replace('-', ' ', $this->type()));
160
+		}
161
+		return $name;
162
+	}
163
+
164
+
165
+	/**
166
+	 * Sets name
167
+	 *
168
+	 * @param string $name
169
+	 * @throws EE_Error
170
+	 * @throws InvalidArgumentException
171
+	 * @throws InvalidDataTypeException
172
+	 * @throws InvalidInterfaceException
173
+	 * @throws ReflectionException
174
+	 */
175
+	public function set_name($name)
176
+	{
177
+		$this->set('LIN_name', $name);
178
+	}
179
+
180
+
181
+	/**
182
+	 * Gets desc
183
+	 *
184
+	 * @return string
185
+	 * @throws EE_Error
186
+	 * @throws InvalidArgumentException
187
+	 * @throws InvalidDataTypeException
188
+	 * @throws InvalidInterfaceException
189
+	 * @throws ReflectionException
190
+	 */
191
+	public function desc()
192
+	{
193
+		return $this->get('LIN_desc');
194
+	}
195
+
196
+
197
+	/**
198
+	 * Sets desc
199
+	 *
200
+	 * @param string $desc
201
+	 * @throws EE_Error
202
+	 * @throws InvalidArgumentException
203
+	 * @throws InvalidDataTypeException
204
+	 * @throws InvalidInterfaceException
205
+	 * @throws ReflectionException
206
+	 */
207
+	public function set_desc($desc)
208
+	{
209
+		$this->set('LIN_desc', $desc);
210
+	}
211
+
212
+
213
+	/**
214
+	 * Gets quantity
215
+	 *
216
+	 * @return int
217
+	 * @throws EE_Error
218
+	 * @throws InvalidArgumentException
219
+	 * @throws InvalidDataTypeException
220
+	 * @throws InvalidInterfaceException
221
+	 * @throws ReflectionException
222
+	 */
223
+	public function quantity()
224
+	{
225
+		return $this->get('LIN_quantity');
226
+	}
227
+
228
+
229
+	/**
230
+	 * Sets quantity
231
+	 *
232
+	 * @param int $quantity
233
+	 * @throws EE_Error
234
+	 * @throws InvalidArgumentException
235
+	 * @throws InvalidDataTypeException
236
+	 * @throws InvalidInterfaceException
237
+	 * @throws ReflectionException
238
+	 */
239
+	public function set_quantity($quantity)
240
+	{
241
+		$this->set('LIN_quantity', max($quantity, 0));
242
+	}
243
+
244
+
245
+	/**
246
+	 * Gets item_id
247
+	 *
248
+	 * @return string
249
+	 * @throws EE_Error
250
+	 * @throws InvalidArgumentException
251
+	 * @throws InvalidDataTypeException
252
+	 * @throws InvalidInterfaceException
253
+	 * @throws ReflectionException
254
+	 */
255
+	public function OBJ_ID()
256
+	{
257
+		return $this->get('OBJ_ID');
258
+	}
259
+
260
+
261
+	/**
262
+	 * Sets item_id
263
+	 *
264
+	 * @param string $item_id
265
+	 * @throws EE_Error
266
+	 * @throws InvalidArgumentException
267
+	 * @throws InvalidDataTypeException
268
+	 * @throws InvalidInterfaceException
269
+	 * @throws ReflectionException
270
+	 */
271
+	public function set_OBJ_ID($item_id)
272
+	{
273
+		$this->set('OBJ_ID', $item_id);
274
+	}
275
+
276
+
277
+	/**
278
+	 * Gets item_type
279
+	 *
280
+	 * @return string
281
+	 * @throws EE_Error
282
+	 * @throws InvalidArgumentException
283
+	 * @throws InvalidDataTypeException
284
+	 * @throws InvalidInterfaceException
285
+	 * @throws ReflectionException
286
+	 */
287
+	public function OBJ_type()
288
+	{
289
+		return $this->get('OBJ_type');
290
+	}
291
+
292
+
293
+	/**
294
+	 * Gets item_type
295
+	 *
296
+	 * @return string
297
+	 * @throws EE_Error
298
+	 * @throws InvalidArgumentException
299
+	 * @throws InvalidDataTypeException
300
+	 * @throws InvalidInterfaceException
301
+	 * @throws ReflectionException
302
+	 */
303
+	public function OBJ_type_i18n()
304
+	{
305
+		$obj_type = $this->OBJ_type();
306
+		switch ($obj_type) {
307
+			case EEM_Line_Item::OBJ_TYPE_EVENT:
308
+				$obj_type = esc_html__('Event', 'event_espresso');
309
+				break;
310
+			case EEM_Line_Item::OBJ_TYPE_PRICE:
311
+				$obj_type = esc_html__('Price', 'event_espresso');
312
+				break;
313
+			case EEM_Line_Item::OBJ_TYPE_PROMOTION:
314
+				$obj_type = esc_html__('Promotion', 'event_espresso');
315
+				break;
316
+			case EEM_Line_Item::OBJ_TYPE_TICKET:
317
+				$obj_type = esc_html__('Ticket', 'event_espresso');
318
+				break;
319
+			case EEM_Line_Item::OBJ_TYPE_TRANSACTION:
320
+				$obj_type = esc_html__('Transaction', 'event_espresso');
321
+				break;
322
+		}
323
+		return apply_filters('FHEE__EE_Line_Item__OBJ_type_i18n', $obj_type, $this);
324
+	}
325
+
326
+
327
+	/**
328
+	 * Sets item_type
329
+	 *
330
+	 * @param string $OBJ_type
331
+	 * @throws EE_Error
332
+	 * @throws InvalidArgumentException
333
+	 * @throws InvalidDataTypeException
334
+	 * @throws InvalidInterfaceException
335
+	 * @throws ReflectionException
336
+	 */
337
+	public function set_OBJ_type($OBJ_type)
338
+	{
339
+		$this->set('OBJ_type', $OBJ_type);
340
+	}
341
+
342
+
343
+	/**
344
+	 * Gets unit_price
345
+	 *
346
+	 * @return float
347
+	 * @throws EE_Error
348
+	 * @throws InvalidArgumentException
349
+	 * @throws InvalidDataTypeException
350
+	 * @throws InvalidInterfaceException
351
+	 * @throws ReflectionException
352
+	 */
353
+	public function unit_price()
354
+	{
355
+		return $this->get('LIN_unit_price');
356
+	}
357
+
358
+
359
+	/**
360
+	 * Sets unit_price
361
+	 *
362
+	 * @param float $unit_price
363
+	 * @throws EE_Error
364
+	 * @throws InvalidArgumentException
365
+	 * @throws InvalidDataTypeException
366
+	 * @throws InvalidInterfaceException
367
+	 * @throws ReflectionException
368
+	 */
369
+	public function set_unit_price($unit_price)
370
+	{
371
+		$this->set('LIN_unit_price', $unit_price);
372
+	}
373
+
374
+
375
+	/**
376
+	 * Checks if this item is a percentage modifier or not
377
+	 *
378
+	 * @return boolean
379
+	 * @throws EE_Error
380
+	 * @throws InvalidArgumentException
381
+	 * @throws InvalidDataTypeException
382
+	 * @throws InvalidInterfaceException
383
+	 * @throws ReflectionException
384
+	 */
385
+	public function is_percent()
386
+	{
387
+		if ($this->is_tax_sub_total()) {
388
+			// tax subtotals HAVE a percent on them, that percentage only applies
389
+			// to taxable items, so its' an exception. Treat it like a flat line item
390
+			return false;
391
+		}
392
+		$unit_price = abs($this->get('LIN_unit_price'));
393
+		$percent = abs($this->get('LIN_percent'));
394
+		if ($unit_price < .001 && $percent) {
395
+			return true;
396
+		}
397
+		if ($unit_price >= .001 && ! $percent) {
398
+			return false;
399
+		}
400
+		if ($unit_price >= .001 && $percent) {
401
+			throw new EE_Error(
402
+				sprintf(
403
+					esc_html__(
404
+						'A Line Item can not have a unit price of (%s) AND a percent (%s)!',
405
+						'event_espresso'
406
+					),
407
+					$unit_price,
408
+					$percent
409
+				)
410
+			);
411
+		}
412
+		// if they're both 0, assume its not a percent item
413
+		return false;
414
+	}
415
+
416
+
417
+	/**
418
+	 * Gets percent (between 100-.001)
419
+	 *
420
+	 * @return float
421
+	 * @throws EE_Error
422
+	 * @throws InvalidArgumentException
423
+	 * @throws InvalidDataTypeException
424
+	 * @throws InvalidInterfaceException
425
+	 * @throws ReflectionException
426
+	 */
427
+	public function percent()
428
+	{
429
+		return $this->get('LIN_percent');
430
+	}
431
+
432
+
433
+	/**
434
+	 * Sets percent (between 100-0.01)
435
+	 *
436
+	 * @param float $percent
437
+	 * @throws EE_Error
438
+	 * @throws InvalidArgumentException
439
+	 * @throws InvalidDataTypeException
440
+	 * @throws InvalidInterfaceException
441
+	 * @throws ReflectionException
442
+	 */
443
+	public function set_percent($percent)
444
+	{
445
+		$this->set('LIN_percent', $percent);
446
+	}
447
+
448
+
449
+	/**
450
+	 * Gets total
451
+	 *
452
+	 * @return float
453
+	 * @throws EE_Error
454
+	 * @throws InvalidArgumentException
455
+	 * @throws InvalidDataTypeException
456
+	 * @throws InvalidInterfaceException
457
+	 * @throws ReflectionException
458
+	 */
459
+	public function total()
460
+	{
461
+		return $this->get('LIN_total');
462
+	}
463
+
464
+
465
+	/**
466
+	 * Sets total
467
+	 *
468
+	 * @param float $total
469
+	 * @throws EE_Error
470
+	 * @throws InvalidArgumentException
471
+	 * @throws InvalidDataTypeException
472
+	 * @throws InvalidInterfaceException
473
+	 * @throws ReflectionException
474
+	 */
475
+	public function set_total($total)
476
+	{
477
+		$this->set('LIN_total', $total);
478
+	}
479
+
480
+
481
+	/**
482
+	 * Gets order
483
+	 *
484
+	 * @return int
485
+	 * @throws EE_Error
486
+	 * @throws InvalidArgumentException
487
+	 * @throws InvalidDataTypeException
488
+	 * @throws InvalidInterfaceException
489
+	 * @throws ReflectionException
490
+	 */
491
+	public function order()
492
+	{
493
+		return $this->get('LIN_order');
494
+	}
495
+
496
+
497
+	/**
498
+	 * Sets order
499
+	 *
500
+	 * @param int $order
501
+	 * @throws EE_Error
502
+	 * @throws InvalidArgumentException
503
+	 * @throws InvalidDataTypeException
504
+	 * @throws InvalidInterfaceException
505
+	 * @throws ReflectionException
506
+	 */
507
+	public function set_order($order)
508
+	{
509
+		$this->set('LIN_order', $order);
510
+	}
511
+
512
+
513
+	/**
514
+	 * Gets parent
515
+	 *
516
+	 * @return int
517
+	 * @throws EE_Error
518
+	 * @throws InvalidArgumentException
519
+	 * @throws InvalidDataTypeException
520
+	 * @throws InvalidInterfaceException
521
+	 * @throws ReflectionException
522
+	 */
523
+	public function parent_ID()
524
+	{
525
+		return $this->get('LIN_parent');
526
+	}
527
+
528
+
529
+	/**
530
+	 * Sets parent
531
+	 *
532
+	 * @param int $parent
533
+	 * @throws EE_Error
534
+	 * @throws InvalidArgumentException
535
+	 * @throws InvalidDataTypeException
536
+	 * @throws InvalidInterfaceException
537
+	 * @throws ReflectionException
538
+	 */
539
+	public function set_parent_ID($parent)
540
+	{
541
+		$this->set('LIN_parent', $parent);
542
+	}
543
+
544
+
545
+	/**
546
+	 * Gets type
547
+	 *
548
+	 * @return string
549
+	 * @throws EE_Error
550
+	 * @throws InvalidArgumentException
551
+	 * @throws InvalidDataTypeException
552
+	 * @throws InvalidInterfaceException
553
+	 * @throws ReflectionException
554
+	 */
555
+	public function type()
556
+	{
557
+		return $this->get('LIN_type');
558
+	}
559
+
560
+
561
+	/**
562
+	 * Sets type
563
+	 *
564
+	 * @param string $type
565
+	 * @throws EE_Error
566
+	 * @throws InvalidArgumentException
567
+	 * @throws InvalidDataTypeException
568
+	 * @throws InvalidInterfaceException
569
+	 * @throws ReflectionException
570
+	 */
571
+	public function set_type($type)
572
+	{
573
+		$this->set('LIN_type', $type);
574
+	}
575
+
576
+
577
+	/**
578
+	 * Gets the line item of which this item is a composite. Eg, if this is a subtotal, the parent might be a total\
579
+	 * If this line item is saved to the DB, fetches the parent from the DB. However, if this line item isn't in the DB
580
+	 * it uses its cached reference to its parent line item (which would have been set by `EE_Line_Item::set_parent()`
581
+	 * or indirectly by `EE_Line_item::add_child_line_item()`)
582
+	 *
583
+	 * @return EE_Base_Class|EE_Line_Item
584
+	 * @throws EE_Error
585
+	 * @throws InvalidArgumentException
586
+	 * @throws InvalidDataTypeException
587
+	 * @throws InvalidInterfaceException
588
+	 * @throws ReflectionException
589
+	 */
590
+	public function parent()
591
+	{
592
+		return $this->ID()
593
+			? $this->get_model()->get_one_by_ID($this->parent_ID())
594
+			: $this->_parent;
595
+	}
596
+
597
+
598
+	/**
599
+	 * Gets ALL the children of this line item (ie, all the parts that contribute towards this total).
600
+	 *
601
+	 * @return EE_Base_Class[]|EE_Line_Item[]
602
+	 * @throws EE_Error
603
+	 * @throws InvalidArgumentException
604
+	 * @throws InvalidDataTypeException
605
+	 * @throws InvalidInterfaceException
606
+	 * @throws ReflectionException
607
+	 */
608
+	public function children()
609
+	{
610
+		if ($this->ID()) {
611
+			return $this->get_model()->get_all(
612
+				array(
613
+					array('LIN_parent' => $this->ID()),
614
+					'order_by' => array('LIN_order' => 'ASC'),
615
+				)
616
+			);
617
+		}
618
+		if (! is_array($this->_children)) {
619
+			$this->_children = array();
620
+		}
621
+		return $this->_children;
622
+	}
623
+
624
+
625
+	/**
626
+	 * Gets code
627
+	 *
628
+	 * @return string
629
+	 * @throws EE_Error
630
+	 * @throws InvalidArgumentException
631
+	 * @throws InvalidDataTypeException
632
+	 * @throws InvalidInterfaceException
633
+	 * @throws ReflectionException
634
+	 */
635
+	public function code()
636
+	{
637
+		return $this->get('LIN_code');
638
+	}
639
+
640
+
641
+	/**
642
+	 * Sets code
643
+	 *
644
+	 * @param string $code
645
+	 * @throws EE_Error
646
+	 * @throws InvalidArgumentException
647
+	 * @throws InvalidDataTypeException
648
+	 * @throws InvalidInterfaceException
649
+	 * @throws ReflectionException
650
+	 */
651
+	public function set_code($code)
652
+	{
653
+		$this->set('LIN_code', $code);
654
+	}
655
+
656
+
657
+	/**
658
+	 * Gets is_taxable
659
+	 *
660
+	 * @return boolean
661
+	 * @throws EE_Error
662
+	 * @throws InvalidArgumentException
663
+	 * @throws InvalidDataTypeException
664
+	 * @throws InvalidInterfaceException
665
+	 * @throws ReflectionException
666
+	 */
667
+	public function is_taxable()
668
+	{
669
+		return $this->get('LIN_is_taxable');
670
+	}
671
+
672
+
673
+	/**
674
+	 * Sets is_taxable
675
+	 *
676
+	 * @param boolean $is_taxable
677
+	 * @throws EE_Error
678
+	 * @throws InvalidArgumentException
679
+	 * @throws InvalidDataTypeException
680
+	 * @throws InvalidInterfaceException
681
+	 * @throws ReflectionException
682
+	 */
683
+	public function set_is_taxable($is_taxable)
684
+	{
685
+		$this->set('LIN_is_taxable', $is_taxable);
686
+	}
687
+
688
+
689
+	/**
690
+	 * Gets the object that this model-joins-to.
691
+	 * returns one of the model objects that the field OBJ_ID can point to... see the 'OBJ_ID' field on
692
+	 * EEM_Promotion_Object
693
+	 *        Eg, if this line item join model object is for a ticket, this will return the EE_Ticket object
694
+	 *
695
+	 * @return EE_Base_Class | NULL
696
+	 * @throws EE_Error
697
+	 * @throws InvalidArgumentException
698
+	 * @throws InvalidDataTypeException
699
+	 * @throws InvalidInterfaceException
700
+	 * @throws ReflectionException
701
+	 */
702
+	public function get_object()
703
+	{
704
+		$model_name_of_related_obj = $this->OBJ_type();
705
+		return $this->get_model()->has_relation($model_name_of_related_obj)
706
+			? $this->get_first_related($model_name_of_related_obj)
707
+			: null;
708
+	}
709
+
710
+
711
+	/**
712
+	 * Like EE_Line_Item::get_object(), but can only ever actually return an EE_Ticket.
713
+	 * (IE, if this line item is for a price or something else, will return NULL)
714
+	 *
715
+	 * @param array $query_params
716
+	 * @return EE_Base_Class|EE_Ticket
717
+	 * @throws EE_Error
718
+	 * @throws InvalidArgumentException
719
+	 * @throws InvalidDataTypeException
720
+	 * @throws InvalidInterfaceException
721
+	 * @throws ReflectionException
722
+	 */
723
+	public function ticket($query_params = array())
724
+	{
725
+		// we're going to assume that when this method is called
726
+		// we always want to receive the attached ticket EVEN if that ticket is archived.
727
+		// This can be overridden via the incoming $query_params argument
728
+		$remove_defaults = array('default_where_conditions' => 'none');
729
+		$query_params = array_merge($remove_defaults, $query_params);
730
+		return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TICKET, $query_params);
731
+	}
732
+
733
+
734
+	/**
735
+	 * Gets the EE_Datetime that's related to the ticket, IF this is for a ticket
736
+	 *
737
+	 * @return EE_Datetime | NULL
738
+	 * @throws EE_Error
739
+	 * @throws InvalidArgumentException
740
+	 * @throws InvalidDataTypeException
741
+	 * @throws InvalidInterfaceException
742
+	 * @throws ReflectionException
743
+	 */
744
+	public function get_ticket_datetime()
745
+	{
746
+		if ($this->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
747
+			$ticket = $this->ticket();
748
+			if ($ticket instanceof EE_Ticket) {
749
+				$datetime = $ticket->first_datetime();
750
+				if ($datetime instanceof EE_Datetime) {
751
+					return $datetime;
752
+				}
753
+			}
754
+		}
755
+		return null;
756
+	}
757
+
758
+
759
+	/**
760
+	 * Gets the event's name that's related to the ticket, if this is for
761
+	 * a ticket
762
+	 *
763
+	 * @return string
764
+	 * @throws EE_Error
765
+	 * @throws InvalidArgumentException
766
+	 * @throws InvalidDataTypeException
767
+	 * @throws InvalidInterfaceException
768
+	 * @throws ReflectionException
769
+	 */
770
+	public function ticket_event_name()
771
+	{
772
+		$event_name = esc_html__('Unknown', 'event_espresso');
773
+		$event = $this->ticket_event();
774
+		if ($event instanceof EE_Event) {
775
+			$event_name = $event->name();
776
+		}
777
+		return $event_name;
778
+	}
779
+
780
+
781
+	/**
782
+	 * Gets the event that's related to the ticket, if this line item represents a ticket.
783
+	 *
784
+	 * @return EE_Event|null
785
+	 * @throws EE_Error
786
+	 * @throws InvalidArgumentException
787
+	 * @throws InvalidDataTypeException
788
+	 * @throws InvalidInterfaceException
789
+	 * @throws ReflectionException
790
+	 */
791
+	public function ticket_event()
792
+	{
793
+		$event = null;
794
+		$ticket = $this->ticket();
795
+		if ($ticket instanceof EE_Ticket) {
796
+			$datetime = $ticket->first_datetime();
797
+			if ($datetime instanceof EE_Datetime) {
798
+				$event = $datetime->event();
799
+			}
800
+		}
801
+		return $event;
802
+	}
803
+
804
+
805
+	/**
806
+	 * Gets the first datetime for this lien item, assuming it's for a ticket
807
+	 *
808
+	 * @param string $date_format
809
+	 * @param string $time_format
810
+	 * @return string
811
+	 * @throws EE_Error
812
+	 * @throws InvalidArgumentException
813
+	 * @throws InvalidDataTypeException
814
+	 * @throws InvalidInterfaceException
815
+	 * @throws ReflectionException
816
+	 */
817
+	public function ticket_datetime_start($date_format = '', $time_format = '')
818
+	{
819
+		$first_datetime_string = esc_html__('Unknown', 'event_espresso');
820
+		$datetime = $this->get_ticket_datetime();
821
+		if ($datetime) {
822
+			$first_datetime_string = $datetime->start_date_and_time($date_format, $time_format);
823
+		}
824
+		return $first_datetime_string;
825
+	}
826
+
827
+
828
+	/**
829
+	 * Adds the line item as a child to this line item. If there is another child line
830
+	 * item with the same LIN_code, it is overwritten by this new one
831
+	 *
832
+	 * @param EEI_Line_Item $line_item
833
+	 * @param bool          $set_order
834
+	 * @return bool success
835
+	 * @throws EE_Error
836
+	 * @throws InvalidArgumentException
837
+	 * @throws InvalidDataTypeException
838
+	 * @throws InvalidInterfaceException
839
+	 * @throws ReflectionException
840
+	 */
841
+	public function add_child_line_item(EEI_Line_Item $line_item, $set_order = true)
842
+	{
843
+		// should we calculate the LIN_order for this line item ?
844
+		if ($set_order || $line_item->order() === null) {
845
+			$line_item->set_order(count($this->children()));
846
+		}
847
+		if ($this->ID()) {
848
+			// check for any duplicate line items (with the same code), if so, this replaces it
849
+			$line_item_with_same_code = $this->get_child_line_item($line_item->code());
850
+			if ($line_item_with_same_code instanceof EE_Line_Item && $line_item_with_same_code !== $line_item) {
851
+				$this->delete_child_line_item($line_item_with_same_code->code());
852
+			}
853
+			$line_item->set_parent_ID($this->ID());
854
+			if ($this->TXN_ID()) {
855
+				$line_item->set_TXN_ID($this->TXN_ID());
856
+			}
857
+			return $line_item->save();
858
+		}
859
+		$this->_children[ $line_item->code() ] = $line_item;
860
+		if ($line_item->parent() !== $this) {
861
+			$line_item->set_parent($this);
862
+		}
863
+		return true;
864
+	}
865
+
866
+
867
+	/**
868
+	 * Similar to EE_Base_Class::_add_relation_to, except this isn't a normal relation.
869
+	 * If this line item is saved to the DB, this is just a wrapper for set_parent_ID() and save()
870
+	 * However, if this line item is NOT saved to the DB, this just caches the parent on
871
+	 * the EE_Line_Item::_parent property.
872
+	 *
873
+	 * @param EE_Line_Item $line_item
874
+	 * @throws EE_Error
875
+	 * @throws InvalidArgumentException
876
+	 * @throws InvalidDataTypeException
877
+	 * @throws InvalidInterfaceException
878
+	 * @throws ReflectionException
879
+	 */
880
+	public function set_parent($line_item)
881
+	{
882
+		if ($this->ID()) {
883
+			if (! $line_item->ID()) {
884
+				$line_item->save();
885
+			}
886
+			$this->set_parent_ID($line_item->ID());
887
+			$this->save();
888
+		} else {
889
+			$this->_parent = $line_item;
890
+			$this->set_parent_ID($line_item->ID());
891
+		}
892
+	}
893
+
894
+
895
+	/**
896
+	 * Gets the child line item as specified by its code. Because this returns an object (by reference)
897
+	 * you can modify this child line item and the parent (this object) can know about them
898
+	 * because it also has a reference to that line item
899
+	 *
900
+	 * @param string $code
901
+	 * @return EE_Base_Class|EE_Line_Item|EE_Soft_Delete_Base_Class|NULL
902
+	 * @throws EE_Error
903
+	 * @throws InvalidArgumentException
904
+	 * @throws InvalidDataTypeException
905
+	 * @throws InvalidInterfaceException
906
+	 * @throws ReflectionException
907
+	 */
908
+	public function get_child_line_item($code)
909
+	{
910
+		if ($this->ID()) {
911
+			return $this->get_model()->get_one(
912
+				array(array('LIN_parent' => $this->ID(), 'LIN_code' => $code))
913
+			);
914
+		}
915
+		return isset($this->_children[ $code ])
916
+			? $this->_children[ $code ]
917
+			: null;
918
+	}
919
+
920
+
921
+	/**
922
+	 * Returns how many items are deleted (or, if this item has not been saved ot the DB yet, just how many it HAD
923
+	 * cached on it)
924
+	 *
925
+	 * @return int
926
+	 * @throws EE_Error
927
+	 * @throws InvalidArgumentException
928
+	 * @throws InvalidDataTypeException
929
+	 * @throws InvalidInterfaceException
930
+	 * @throws ReflectionException
931
+	 */
932
+	public function delete_children_line_items()
933
+	{
934
+		if ($this->ID()) {
935
+			return $this->get_model()->delete(array(array('LIN_parent' => $this->ID())));
936
+		}
937
+		$count = count($this->_children);
938
+		$this->_children = array();
939
+		return $count;
940
+	}
941
+
942
+
943
+	/**
944
+	 * If this line item has been saved to the DB, deletes its child with LIN_code == $code. If this line
945
+	 * HAS NOT been saved to the DB, removes the child line item with index $code.
946
+	 * Also searches through the child's children for a matching line item. However, once a line item has been found
947
+	 * and deleted, stops searching (so if there are line items with duplicate codes, only the first one found will be
948
+	 * deleted)
949
+	 *
950
+	 * @param string $code
951
+	 * @param bool   $stop_search_once_found
952
+	 * @return int count of items deleted (or simply removed from the line item's cache, if not has not been saved to
953
+	 *             the DB yet)
954
+	 * @throws EE_Error
955
+	 * @throws InvalidArgumentException
956
+	 * @throws InvalidDataTypeException
957
+	 * @throws InvalidInterfaceException
958
+	 * @throws ReflectionException
959
+	 */
960
+	public function delete_child_line_item($code, $stop_search_once_found = true)
961
+	{
962
+		if ($this->ID()) {
963
+			$items_deleted = 0;
964
+			if ($this->code() === $code) {
965
+				$items_deleted += EEH_Line_Item::delete_all_child_items($this);
966
+				$items_deleted += (int) $this->delete();
967
+				if ($stop_search_once_found) {
968
+					return $items_deleted;
969
+				}
970
+			}
971
+			foreach ($this->children() as $child_line_item) {
972
+				$items_deleted += $child_line_item->delete_child_line_item($code, $stop_search_once_found);
973
+			}
974
+			return $items_deleted;
975
+		}
976
+		if (isset($this->_children[ $code ])) {
977
+			unset($this->_children[ $code ]);
978
+			return 1;
979
+		}
980
+		return 0;
981
+	}
982
+
983
+
984
+	/**
985
+	 * If this line item is in the database, is of the type subtotal, and
986
+	 * has no children, why do we have it? It should be deleted so this function
987
+	 * does that
988
+	 *
989
+	 * @return boolean
990
+	 * @throws EE_Error
991
+	 * @throws InvalidArgumentException
992
+	 * @throws InvalidDataTypeException
993
+	 * @throws InvalidInterfaceException
994
+	 * @throws ReflectionException
995
+	 */
996
+	public function delete_if_childless_subtotal()
997
+	{
998
+		if ($this->ID() && $this->type() === EEM_Line_Item::type_sub_total && ! $this->children()) {
999
+			return $this->delete();
1000
+		}
1001
+		return false;
1002
+	}
1003
+
1004
+
1005
+	/**
1006
+	 * Creates a code and returns a string. doesn't assign the code to this model object
1007
+	 *
1008
+	 * @return string
1009
+	 * @throws EE_Error
1010
+	 * @throws InvalidArgumentException
1011
+	 * @throws InvalidDataTypeException
1012
+	 * @throws InvalidInterfaceException
1013
+	 * @throws ReflectionException
1014
+	 */
1015
+	public function generate_code()
1016
+	{
1017
+		// each line item in the cart requires a unique identifier
1018
+		return md5($this->get('OBJ_type') . $this->get('OBJ_ID') . microtime());
1019
+	}
1020
+
1021
+
1022
+	/**
1023
+	 * @return bool
1024
+	 * @throws EE_Error
1025
+	 * @throws InvalidArgumentException
1026
+	 * @throws InvalidDataTypeException
1027
+	 * @throws InvalidInterfaceException
1028
+	 * @throws ReflectionException
1029
+	 */
1030
+	public function is_tax()
1031
+	{
1032
+		return $this->type() === EEM_Line_Item::type_tax;
1033
+	}
1034
+
1035
+
1036
+	/**
1037
+	 * @return bool
1038
+	 * @throws EE_Error
1039
+	 * @throws InvalidArgumentException
1040
+	 * @throws InvalidDataTypeException
1041
+	 * @throws InvalidInterfaceException
1042
+	 * @throws ReflectionException
1043
+	 */
1044
+	public function is_tax_sub_total()
1045
+	{
1046
+		return $this->type() === EEM_Line_Item::type_tax_sub_total;
1047
+	}
1048
+
1049
+
1050
+	/**
1051
+	 * @return bool
1052
+	 * @throws EE_Error
1053
+	 * @throws InvalidArgumentException
1054
+	 * @throws InvalidDataTypeException
1055
+	 * @throws InvalidInterfaceException
1056
+	 * @throws ReflectionException
1057
+	 */
1058
+	public function is_line_item()
1059
+	{
1060
+		return $this->type() === EEM_Line_Item::type_line_item;
1061
+	}
1062
+
1063
+
1064
+	/**
1065
+	 * @return bool
1066
+	 * @throws EE_Error
1067
+	 * @throws InvalidArgumentException
1068
+	 * @throws InvalidDataTypeException
1069
+	 * @throws InvalidInterfaceException
1070
+	 * @throws ReflectionException
1071
+	 */
1072
+	public function is_sub_line_item()
1073
+	{
1074
+		return $this->type() === EEM_Line_Item::type_sub_line_item;
1075
+	}
1076
+
1077
+
1078
+	/**
1079
+	 * @return bool
1080
+	 * @throws EE_Error
1081
+	 * @throws InvalidArgumentException
1082
+	 * @throws InvalidDataTypeException
1083
+	 * @throws InvalidInterfaceException
1084
+	 * @throws ReflectionException
1085
+	 */
1086
+	public function is_sub_total()
1087
+	{
1088
+		return $this->type() === EEM_Line_Item::type_sub_total;
1089
+	}
1090
+
1091
+
1092
+	/**
1093
+	 * Whether or not this line item is a cancellation line item
1094
+	 *
1095
+	 * @return boolean
1096
+	 * @throws EE_Error
1097
+	 * @throws InvalidArgumentException
1098
+	 * @throws InvalidDataTypeException
1099
+	 * @throws InvalidInterfaceException
1100
+	 * @throws ReflectionException
1101
+	 */
1102
+	public function is_cancellation()
1103
+	{
1104
+		return EEM_Line_Item::type_cancellation === $this->type();
1105
+	}
1106
+
1107
+
1108
+	/**
1109
+	 * @return bool
1110
+	 * @throws EE_Error
1111
+	 * @throws InvalidArgumentException
1112
+	 * @throws InvalidDataTypeException
1113
+	 * @throws InvalidInterfaceException
1114
+	 * @throws ReflectionException
1115
+	 */
1116
+	public function is_total()
1117
+	{
1118
+		return $this->type() === EEM_Line_Item::type_total;
1119
+	}
1120
+
1121
+
1122
+	/**
1123
+	 * @return bool
1124
+	 * @throws EE_Error
1125
+	 * @throws InvalidArgumentException
1126
+	 * @throws InvalidDataTypeException
1127
+	 * @throws InvalidInterfaceException
1128
+	 * @throws ReflectionException
1129
+	 */
1130
+	public function is_cancelled()
1131
+	{
1132
+		return $this->type() === EEM_Line_Item::type_cancellation;
1133
+	}
1134
+
1135
+
1136
+	/**
1137
+	 * @return string like '2, 004.00', formatted according to the localized currency
1138
+	 * @throws EE_Error
1139
+	 * @throws InvalidArgumentException
1140
+	 * @throws InvalidDataTypeException
1141
+	 * @throws InvalidInterfaceException
1142
+	 * @throws ReflectionException
1143
+	 */
1144
+	public function unit_price_no_code()
1145
+	{
1146
+		return $this->get_pretty('LIN_unit_price', 'no_currency_code');
1147
+	}
1148
+
1149
+
1150
+	/**
1151
+	 * @return string like '2, 004.00', formatted according to the localized currency
1152
+	 * @throws EE_Error
1153
+	 * @throws InvalidArgumentException
1154
+	 * @throws InvalidDataTypeException
1155
+	 * @throws InvalidInterfaceException
1156
+	 * @throws ReflectionException
1157
+	 */
1158
+	public function total_no_code()
1159
+	{
1160
+		return $this->get_pretty('LIN_total', 'no_currency_code');
1161
+	}
1162
+
1163
+
1164
+	/**
1165
+	 * Gets the final total on this item, taking taxes into account.
1166
+	 * Has the side-effect of setting the sub-total as it was just calculated.
1167
+	 * If this is used on a grand-total line item, also updates the transaction's
1168
+	 * TXN_total (provided this line item is allowed to persist, otherwise we don't
1169
+	 * want to change a persistable transaction with info from a non-persistent line item)
1170
+	 *
1171
+	 * @param bool $update_txn_status
1172
+	 * @return float
1173
+	 * @throws EE_Error
1174
+	 * @throws InvalidArgumentException
1175
+	 * @throws InvalidDataTypeException
1176
+	 * @throws InvalidInterfaceException
1177
+	 * @throws ReflectionException
1178
+	 * @throws RuntimeException
1179
+	 */
1180
+	public function recalculate_total_including_taxes($update_txn_status = false)
1181
+	{
1182
+		$pre_tax_total = $this->recalculate_pre_tax_total();
1183
+		$tax_total = $this->recalculate_taxes_and_tax_total();
1184
+		$total = $pre_tax_total + $tax_total;
1185
+		// no negative totals plz
1186
+		$total = max($total, 0);
1187
+		$this->set_total($total);
1188
+		// only update the related transaction's total
1189
+		// if we intend to save this line item and its a grand total
1190
+		if ($this->allow_persist() && $this->type() === EEM_Line_Item::type_total
1191
+			&& $this->transaction()
1192
+			   instanceof
1193
+			   EE_Transaction
1194
+		) {
1195
+			$this->transaction()->set_total($total);
1196
+			if ($update_txn_status) {
1197
+				// don't save the TXN because that will be done below
1198
+				// and the following method only saves if the status changes
1199
+				$this->transaction()->update_status_based_on_total_paid(false);
1200
+			}
1201
+			if ($this->transaction()->ID()) {
1202
+				$this->transaction()->save();
1203
+			}
1204
+		}
1205
+		$this->maybe_save();
1206
+		return $total;
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 * Recursively goes through all the children and recalculates sub-totals EXCEPT for
1212
+	 * tax-sub-totals (they're a an odd beast). Updates the 'total' on each line item according to either its
1213
+	 * unit price * quantity or the total of all its children EXCEPT when we're only calculating the taxable total and
1214
+	 * when this is called on the grand total
1215
+	 *
1216
+	 * @return float
1217
+	 * @throws EE_Error
1218
+	 * @throws InvalidArgumentException
1219
+	 * @throws InvalidDataTypeException
1220
+	 * @throws InvalidInterfaceException
1221
+	 * @throws ReflectionException
1222
+	 */
1223
+	public function recalculate_pre_tax_total()
1224
+	{
1225
+		$total = 0;
1226
+		$my_children = $this->children();
1227
+		$has_children = ! empty($my_children);
1228
+		if ($has_children && $this->is_line_item()) {
1229
+			$total = $this->_recalculate_pretax_total_for_line_item($total, $my_children);
1230
+		} elseif (! $has_children && ($this->is_sub_line_item() || $this->is_line_item())) {
1231
+			$total = $this->unit_price() * $this->quantity();
1232
+		} elseif ($this->is_sub_total() || $this->is_total()) {
1233
+			$total = $this->_recalculate_pretax_total_for_subtotal($total, $my_children);
1234
+		} elseif ($this->is_tax_sub_total() || $this->is_tax() || $this->is_cancelled()) {
1235
+			// completely ignore tax totals, tax sub-totals, and cancelled line items, when calculating the pre-tax-total
1236
+			return 0;
1237
+		}
1238
+		// ensure all non-line items and non-sub-line-items have a quantity of 1 (except for Events)
1239
+		if (! $this->is_line_item() && ! $this->is_sub_line_item() && ! $this->is_cancellation()
1240
+		) {
1241
+			if ($this->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_EVENT) {
1242
+				$this->set_quantity(1);
1243
+			}
1244
+			if (! $this->is_percent()) {
1245
+				$this->set_unit_price($total);
1246
+			}
1247
+		}
1248
+		// we don't want to bother saving grand totals, because that needs to factor in taxes anyways
1249
+		// so it ought to be
1250
+		if (! $this->is_total()) {
1251
+			$this->set_total($total);
1252
+			// if not a percent line item, make sure we keep the unit price in sync
1253
+			if ($has_children
1254
+				&& $this->is_line_item()
1255
+				&& ! $this->is_percent()
1256
+			) {
1257
+				if ($this->quantity() === 0) {
1258
+					$new_unit_price = 0;
1259
+				} else {
1260
+					$new_unit_price = $this->total() / $this->quantity();
1261
+				}
1262
+				$this->set_unit_price($new_unit_price);
1263
+			}
1264
+			$this->maybe_save();
1265
+		}
1266
+		return $total;
1267
+	}
1268
+
1269
+
1270
+	/**
1271
+	 * Calculates the pretax total when this line item is a subtotal or total line item.
1272
+	 * Basically does a sum-then-round approach (ie, any percent line item that are children
1273
+	 * will calculate their total based on the un-rounded total we're working with so far, and
1274
+	 * THEN round the result; instead of rounding as we go like with sub-line-items)
1275
+	 *
1276
+	 * @param float          $calculated_total_so_far
1277
+	 * @param EE_Line_Item[] $my_children
1278
+	 * @return float
1279
+	 * @throws EE_Error
1280
+	 * @throws InvalidArgumentException
1281
+	 * @throws InvalidDataTypeException
1282
+	 * @throws InvalidInterfaceException
1283
+	 * @throws ReflectionException
1284
+	 */
1285
+	protected function _recalculate_pretax_total_for_subtotal($calculated_total_so_far, $my_children = null)
1286
+	{
1287
+		if ($my_children === null) {
1288
+			$my_children = $this->children();
1289
+		}
1290
+		$subtotal_quantity = 0;
1291
+		// get the total of all its children
1292
+		foreach ($my_children as $child_line_item) {
1293
+			if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1294
+				// percentage line items are based on total so far
1295
+				if ($child_line_item->is_percent()) {
1296
+					// round as we go so that the line items add up ok
1297
+					$percent_total = round(
1298
+						$calculated_total_so_far * $child_line_item->percent() / 100,
1299
+						EE_Registry::instance()->CFG->currency->dec_plc
1300
+					);
1301
+					$child_line_item->set_total($percent_total);
1302
+					// so far all percent line items should have a quantity of 1
1303
+					// (ie, no double percent discounts. Although that might be requested someday)
1304
+					$child_line_item->set_quantity(1);
1305
+					$child_line_item->maybe_save();
1306
+					$calculated_total_so_far += $percent_total;
1307
+				} else {
1308
+					// verify flat sub-line-item quantities match their parent
1309
+					if ($child_line_item->is_sub_line_item()) {
1310
+						$child_line_item->set_quantity($this->quantity());
1311
+					}
1312
+					$calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1313
+					$subtotal_quantity += $child_line_item->quantity();
1314
+				}
1315
+			}
1316
+		}
1317
+		if ($this->is_sub_total()) {
1318
+			// no negative totals plz
1319
+			$calculated_total_so_far = max($calculated_total_so_far, 0);
1320
+			$subtotal_quantity = $subtotal_quantity > 0 ? 1 : 0;
1321
+			$this->set_quantity($subtotal_quantity);
1322
+			$this->maybe_save();
1323
+		}
1324
+		return $calculated_total_so_far;
1325
+	}
1326
+
1327
+
1328
+	/**
1329
+	 * Calculates the pretax total for a normal line item, in a round-then-sum approach
1330
+	 * (where each sub-line-item is applied to the base price for the line item
1331
+	 * and the result is immediately rounded, rather than summing all the sub-line-items
1332
+	 * then rounding, like we do when recalculating pretax totals on totals and subtotals).
1333
+	 *
1334
+	 * @param float          $calculated_total_so_far
1335
+	 * @param EE_Line_Item[] $my_children
1336
+	 * @return float
1337
+	 * @throws EE_Error
1338
+	 * @throws InvalidArgumentException
1339
+	 * @throws InvalidDataTypeException
1340
+	 * @throws InvalidInterfaceException
1341
+	 * @throws ReflectionException
1342
+	 */
1343
+	protected function _recalculate_pretax_total_for_line_item($calculated_total_so_far, $my_children = null)
1344
+	{
1345
+		if ($my_children === null) {
1346
+			$my_children = $this->children();
1347
+		}
1348
+		// we need to keep track of the running total for a single item,
1349
+		// because we need to round as we go
1350
+		$unit_price_for_total = 0;
1351
+		$quantity_for_total = 1;
1352
+		// get the total of all its children
1353
+		foreach ($my_children as $child_line_item) {
1354
+			if ($child_line_item instanceof EE_Line_Item && ! $child_line_item->is_cancellation()) {
1355
+				if ($child_line_item->is_percent()) {
1356
+					// it should be the unit-price-so-far multiplied by teh percent multiplied by the quantity
1357
+					// not total multiplied by percent, because that ignores rounding along-the-way
1358
+					$percent_unit_price = round(
1359
+						$unit_price_for_total * $child_line_item->percent() / 100,
1360
+						EE_Registry::instance()->CFG->currency->dec_plc
1361
+					);
1362
+					$percent_total = $percent_unit_price * $quantity_for_total;
1363
+					$child_line_item->set_total($percent_total);
1364
+					// so far all percent line items should have a quantity of 1
1365
+					// (ie, no double percent discounts. Although that might be requested someday)
1366
+					$child_line_item->set_quantity(1);
1367
+					$child_line_item->maybe_save();
1368
+					$calculated_total_so_far += $percent_total;
1369
+					$unit_price_for_total += $percent_unit_price;
1370
+				} else {
1371
+					// verify flat sub-line-item quantities match their parent
1372
+					if ($child_line_item->is_sub_line_item()) {
1373
+						$child_line_item->set_quantity($this->quantity());
1374
+					}
1375
+					$quantity_for_total = $child_line_item->quantity();
1376
+					$calculated_total_so_far += $child_line_item->recalculate_pre_tax_total();
1377
+					$unit_price_for_total += $child_line_item->unit_price();
1378
+				}
1379
+			}
1380
+		}
1381
+		return $calculated_total_so_far;
1382
+	}
1383
+
1384
+
1385
+	/**
1386
+	 * Recalculates the total on each individual tax (based on a recalculation of the pre-tax total), sets
1387
+	 * the totals on each tax calculated, and returns the final tax total. Re-saves tax line items
1388
+	 * and tax sub-total if already in the DB
1389
+	 *
1390
+	 * @return float
1391
+	 * @throws EE_Error
1392
+	 * @throws InvalidArgumentException
1393
+	 * @throws InvalidDataTypeException
1394
+	 * @throws InvalidInterfaceException
1395
+	 * @throws ReflectionException
1396
+	 */
1397
+	public function recalculate_taxes_and_tax_total()
1398
+	{
1399
+		// get all taxes
1400
+		$taxes = $this->tax_descendants();
1401
+		// calculate the pretax total
1402
+		$taxable_total = $this->taxable_total();
1403
+		$tax_total = 0;
1404
+		foreach ($taxes as $tax) {
1405
+			$total_on_this_tax = $taxable_total * $tax->percent() / 100;
1406
+			// remember the total on this line item
1407
+			$tax->set_total($total_on_this_tax);
1408
+			$tax->maybe_save();
1409
+			$tax_total += $tax->total();
1410
+		}
1411
+		$this->_recalculate_tax_sub_total();
1412
+		return $tax_total;
1413
+	}
1414
+
1415
+
1416
+	/**
1417
+	 * Simply forces all the tax-sub-totals to recalculate. Assumes the taxes have been calculated
1418
+	 *
1419
+	 * @return void
1420
+	 * @throws EE_Error
1421
+	 * @throws InvalidArgumentException
1422
+	 * @throws InvalidDataTypeException
1423
+	 * @throws InvalidInterfaceException
1424
+	 * @throws ReflectionException
1425
+	 */
1426
+	private function _recalculate_tax_sub_total()
1427
+	{
1428
+		if ($this->is_tax_sub_total()) {
1429
+			$total = 0;
1430
+			$total_percent = 0;
1431
+			// simply loop through all its children (which should be taxes) and sum their total
1432
+			foreach ($this->children() as $child_tax) {
1433
+				if ($child_tax instanceof EE_Line_Item) {
1434
+					$total += $child_tax->total();
1435
+					$total_percent += $child_tax->percent();
1436
+				}
1437
+			}
1438
+			$this->set_total($total);
1439
+			$this->set_percent($total_percent);
1440
+			$this->maybe_save();
1441
+		} elseif ($this->is_total()) {
1442
+			foreach ($this->children() as $maybe_tax_subtotal) {
1443
+				if ($maybe_tax_subtotal instanceof EE_Line_Item) {
1444
+					$maybe_tax_subtotal->_recalculate_tax_sub_total();
1445
+				}
1446
+			}
1447
+		}
1448
+	}
1449
+
1450
+
1451
+	/**
1452
+	 * Gets the total tax on this line item. Assumes taxes have already been calculated using
1453
+	 * recalculate_taxes_and_total
1454
+	 *
1455
+	 * @return float
1456
+	 * @throws EE_Error
1457
+	 * @throws InvalidArgumentException
1458
+	 * @throws InvalidDataTypeException
1459
+	 * @throws InvalidInterfaceException
1460
+	 * @throws ReflectionException
1461
+	 */
1462
+	public function get_total_tax()
1463
+	{
1464
+		$this->_recalculate_tax_sub_total();
1465
+		$total = 0;
1466
+		foreach ($this->tax_descendants() as $tax_line_item) {
1467
+			if ($tax_line_item instanceof EE_Line_Item) {
1468
+				$total += $tax_line_item->total();
1469
+			}
1470
+		}
1471
+		return $total;
1472
+	}
1473
+
1474
+
1475
+	/**
1476
+	 * Gets the total for all the items purchased only
1477
+	 *
1478
+	 * @return float
1479
+	 * @throws EE_Error
1480
+	 * @throws InvalidArgumentException
1481
+	 * @throws InvalidDataTypeException
1482
+	 * @throws InvalidInterfaceException
1483
+	 * @throws ReflectionException
1484
+	 */
1485
+	public function get_items_total()
1486
+	{
1487
+		// by default, let's make sure we're consistent with the existing line item
1488
+		if ($this->is_total()) {
1489
+			$pretax_subtotal_li = EEH_Line_Item::get_pre_tax_subtotal($this);
1490
+			if ($pretax_subtotal_li instanceof EE_Line_Item) {
1491
+				return $pretax_subtotal_li->total();
1492
+			}
1493
+		}
1494
+		$total = 0;
1495
+		foreach ($this->get_items() as $item) {
1496
+			if ($item instanceof EE_Line_Item) {
1497
+				$total += $item->total();
1498
+			}
1499
+		}
1500
+		return $total;
1501
+	}
1502
+
1503
+
1504
+	/**
1505
+	 * Gets all the descendants (ie, children or children of children etc) that
1506
+	 * are of the type 'tax'
1507
+	 *
1508
+	 * @return EE_Line_Item[]
1509
+	 * @throws EE_Error
1510
+	 */
1511
+	public function tax_descendants()
1512
+	{
1513
+		return EEH_Line_Item::get_tax_descendants($this);
1514
+	}
1515
+
1516
+
1517
+	/**
1518
+	 * Gets all the real items purchased which are children of this item
1519
+	 *
1520
+	 * @return EE_Line_Item[]
1521
+	 * @throws EE_Error
1522
+	 */
1523
+	public function get_items()
1524
+	{
1525
+		return EEH_Line_Item::get_line_item_descendants($this);
1526
+	}
1527
+
1528
+
1529
+	/**
1530
+	 * Returns the amount taxable among this line item's children (or if it has no children,
1531
+	 * how much of it is taxable). Does not recalculate totals or subtotals.
1532
+	 * If the taxable total is negative, (eg, if none of the tickets were taxable,
1533
+	 * but there is a "Taxable" discount), returns 0.
1534
+	 *
1535
+	 * @return float
1536
+	 * @throws EE_Error
1537
+	 * @throws InvalidArgumentException
1538
+	 * @throws InvalidDataTypeException
1539
+	 * @throws InvalidInterfaceException
1540
+	 * @throws ReflectionException
1541
+	 */
1542
+	public function taxable_total()
1543
+	{
1544
+		$total = 0;
1545
+		if ($this->children()) {
1546
+			foreach ($this->children() as $child_line_item) {
1547
+				if ($child_line_item->type() === EEM_Line_Item::type_line_item && $child_line_item->is_taxable()) {
1548
+					// if it's a percent item, only take into account the percent
1549
+					// that's taxable too (the taxable total so far)
1550
+					if ($child_line_item->is_percent()) {
1551
+						$total += ($total * $child_line_item->percent() / 100);
1552
+					} else {
1553
+						$total += $child_line_item->total();
1554
+					}
1555
+				} elseif ($child_line_item->type() === EEM_Line_Item::type_sub_total) {
1556
+					$total += $child_line_item->taxable_total();
1557
+				}
1558
+			}
1559
+		}
1560
+		return max($total, 0);
1561
+	}
1562
+
1563
+
1564
+	/**
1565
+	 * Gets the transaction for this line item
1566
+	 *
1567
+	 * @return EE_Base_Class|EE_Transaction
1568
+	 * @throws EE_Error
1569
+	 * @throws InvalidArgumentException
1570
+	 * @throws InvalidDataTypeException
1571
+	 * @throws InvalidInterfaceException
1572
+	 * @throws ReflectionException
1573
+	 */
1574
+	public function transaction()
1575
+	{
1576
+		return $this->get_first_related(EEM_Line_Item::OBJ_TYPE_TRANSACTION);
1577
+	}
1578
+
1579
+
1580
+	/**
1581
+	 * Saves this line item to the DB, and recursively saves its descendants.
1582
+	 * Because there currently is no proper parent-child relation on the model,
1583
+	 * save_this_and_cached() will NOT save the descendants.
1584
+	 * Also sets the transaction on this line item and all its descendants before saving
1585
+	 *
1586
+	 * @param int $txn_id if none is provided, assumes $this->TXN_ID()
1587
+	 * @return int count of items saved
1588
+	 * @throws EE_Error
1589
+	 * @throws InvalidArgumentException
1590
+	 * @throws InvalidDataTypeException
1591
+	 * @throws InvalidInterfaceException
1592
+	 * @throws ReflectionException
1593
+	 */
1594
+	public function save_this_and_descendants_to_txn($txn_id = null)
1595
+	{
1596
+		$count = 0;
1597
+		if (! $txn_id) {
1598
+			$txn_id = $this->TXN_ID();
1599
+		}
1600
+		$this->set_TXN_ID($txn_id);
1601
+		$children = $this->children();
1602
+		$count += $this->save()
1603
+			? 1
1604
+			: 0;
1605
+		foreach ($children as $child_line_item) {
1606
+			if ($child_line_item instanceof EE_Line_Item) {
1607
+				$child_line_item->set_parent_ID($this->ID());
1608
+				$count += $child_line_item->save_this_and_descendants_to_txn($txn_id);
1609
+			}
1610
+		}
1611
+		return $count;
1612
+	}
1613
+
1614
+
1615
+	/**
1616
+	 * Saves this line item to the DB, and recursively saves its descendants.
1617
+	 *
1618
+	 * @return int count of items saved
1619
+	 * @throws EE_Error
1620
+	 * @throws InvalidArgumentException
1621
+	 * @throws InvalidDataTypeException
1622
+	 * @throws InvalidInterfaceException
1623
+	 * @throws ReflectionException
1624
+	 */
1625
+	public function save_this_and_descendants()
1626
+	{
1627
+		$count = 0;
1628
+		$children = $this->children();
1629
+		$count += $this->save()
1630
+			? 1
1631
+			: 0;
1632
+		foreach ($children as $child_line_item) {
1633
+			if ($child_line_item instanceof EE_Line_Item) {
1634
+				$child_line_item->set_parent_ID($this->ID());
1635
+				$count += $child_line_item->save_this_and_descendants();
1636
+			}
1637
+		}
1638
+		return $count;
1639
+	}
1640
+
1641
+
1642
+	/**
1643
+	 * returns the cancellation line item if this item was cancelled
1644
+	 *
1645
+	 * @return EE_Line_Item[]
1646
+	 * @throws InvalidArgumentException
1647
+	 * @throws InvalidInterfaceException
1648
+	 * @throws InvalidDataTypeException
1649
+	 * @throws ReflectionException
1650
+	 * @throws EE_Error
1651
+	 */
1652
+	public function get_cancellations()
1653
+	{
1654
+		EE_Registry::instance()->load_helper('Line_Item');
1655
+		return EEH_Line_Item::get_descendants_of_type($this, EEM_Line_Item::type_cancellation);
1656
+	}
1657
+
1658
+
1659
+	/**
1660
+	 * If this item has an ID, then this saves it again to update the db
1661
+	 *
1662
+	 * @return int count of items saved
1663
+	 * @throws EE_Error
1664
+	 * @throws InvalidArgumentException
1665
+	 * @throws InvalidDataTypeException
1666
+	 * @throws InvalidInterfaceException
1667
+	 * @throws ReflectionException
1668
+	 */
1669
+	public function maybe_save()
1670
+	{
1671
+		if ($this->ID()) {
1672
+			return $this->save();
1673
+		}
1674
+		return false;
1675
+	}
1676
+
1677
+
1678
+	/**
1679
+	 * clears the cached children and parent from the line item
1680
+	 *
1681
+	 * @return void
1682
+	 */
1683
+	public function clear_related_line_item_cache()
1684
+	{
1685
+		$this->_children = array();
1686
+		$this->_parent = null;
1687
+	}
1688
+
1689
+
1690
+	/**
1691
+	 * @param bool $raw
1692
+	 * @return int
1693
+	 * @throws EE_Error
1694
+	 * @throws InvalidArgumentException
1695
+	 * @throws InvalidDataTypeException
1696
+	 * @throws InvalidInterfaceException
1697
+	 * @throws ReflectionException
1698
+	 */
1699
+	public function timestamp($raw = false)
1700
+	{
1701
+		return $raw
1702
+			? $this->get_raw('LIN_timestamp')
1703
+			: $this->get('LIN_timestamp');
1704
+	}
1705
+
1706
+
1707
+
1708
+
1709
+	/************************* DEPRECATED *************************/
1710
+	/**
1711
+	 * @deprecated 4.6.0
1712
+	 * @param string $type one of the constants on EEM_Line_Item
1713
+	 * @return EE_Line_Item[]
1714
+	 * @throws EE_Error
1715
+	 */
1716
+	protected function _get_descendants_of_type($type)
1717
+	{
1718
+		EE_Error::doing_it_wrong(
1719
+			'EE_Line_Item::_get_descendants_of_type()',
1720
+			sprintf(
1721
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1722
+				'EEH_Line_Item::get_descendants_of_type()'
1723
+			),
1724
+			'4.6.0'
1725
+		);
1726
+		return EEH_Line_Item::get_descendants_of_type($this, $type);
1727
+	}
1728
+
1729
+
1730
+	/**
1731
+	 * @deprecated 4.6.0
1732
+	 * @param string $type like one of the EEM_Line_Item::type_*
1733
+	 * @return EE_Line_Item
1734
+	 * @throws EE_Error
1735
+	 * @throws InvalidArgumentException
1736
+	 * @throws InvalidDataTypeException
1737
+	 * @throws InvalidInterfaceException
1738
+	 * @throws ReflectionException
1739
+	 */
1740
+	public function get_nearest_descendant_of_type($type)
1741
+	{
1742
+		EE_Error::doing_it_wrong(
1743
+			'EE_Line_Item::get_nearest_descendant_of_type()',
1744
+			sprintf(
1745
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1746
+				'EEH_Line_Item::get_nearest_descendant_of_type()'
1747
+			),
1748
+			'4.6.0'
1749
+		);
1750
+		return EEH_Line_Item::get_nearest_descendant_of_type($this, $type);
1751
+	}
1752 1752
 }
Please login to merge, or discard this patch.
core/helpers/EEH_Line_Item.helper.php 3 patches
Doc Comments   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1027,7 +1027,7 @@  discard block
 block discarded – undo
1027 1027
      * Deletes ALL children of the passed line item
1028 1028
      *
1029 1029
      * @param EE_Line_Item $parent_line_item
1030
-     * @return bool
1030
+     * @return integer
1031 1031
      * @throws EE_Error
1032 1032
      * @throws InvalidArgumentException
1033 1033
      * @throws InvalidDataTypeException
@@ -1333,6 +1333,7 @@  discard block
 block discarded – undo
1333 1333
      * @param string        $line_item_type   one of the EEM_Line_Item constants
1334 1334
      * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1335 1335
      *                                        searching
1336
+     * @param string $obj_type
1336 1337
      * @return EE_Line_Item[]
1337 1338
      * @throws EE_Error
1338 1339
      */
Please login to merge, or discard this patch.
Spacing   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
      */
139 139
     public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140 140
     {
141
-        if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
141
+        if ( ! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142 142
             throw new EE_Error(
143 143
                 sprintf(
144 144
                     esc_html__(
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
         // either increment the qty for an existing ticket
154 154
         $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155 155
         // or add a new one
156
-        if (! $line_item instanceof EE_Line_Item) {
156
+        if ( ! $line_item instanceof EE_Line_Item) {
157 157
             $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158 158
         }
159 159
         $total_line_item->recalculate_total_including_taxes();
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
      */
215 215
     public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216 216
     {
217
-        if (! $line_item->is_percent()) {
217
+        if ( ! $line_item->is_percent()) {
218 218
             $qty += $line_item->quantity();
219 219
             $line_item->set_quantity($qty);
220 220
             $line_item->set_total($line_item->unit_price() * $qty);
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
      */
244 244
     public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245 245
     {
246
-        if (! $line_item->is_percent()) {
246
+        if ( ! $line_item->is_percent()) {
247 247
             $qty = $line_item->quantity() - $qty;
248 248
             $qty = max($qty, 0);
249 249
             $line_item->set_quantity($qty);
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
      */
273 273
     public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274 274
     {
275
-        if (! $line_item->is_percent()) {
275
+        if ( ! $line_item->is_percent()) {
276 276
             $line_item->set_quantity($new_quantity);
277 277
             $line_item->set_total($line_item->unit_price() * $new_quantity);
278 278
             $line_item->save();
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
         // add $ticket to cart
313 313
         $line_item = EE_Line_Item::new_instance(array(
314 314
             'LIN_name'       => $ticket->name(),
315
-            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
315
+            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description().' '.$event : $event,
316 316
             'LIN_unit_price' => $ticket->price(),
317 317
             'LIN_quantity'   => $qty,
318 318
             'LIN_is_taxable' => $ticket->taxable(),
@@ -462,7 +462,7 @@  discard block
 block discarded – undo
462 462
                         'event_espresso'
463 463
                     ),
464 464
                     $ticket_line_item->name(),
465
-                    current_time(get_option('date_format') . ' ' . get_option('time_format'))
465
+                    current_time(get_option('date_format').' '.get_option('time_format'))
466 466
                 ),
467 467
                 'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468 468
                 'LIN_quantity'   => $qty,
@@ -524,7 +524,7 @@  discard block
 block discarded – undo
524 524
         );
525 525
         $cancellation_line_item = reset($cancellation_line_item);
526 526
         // verify that this ticket was indeed previously cancelled
527
-        if (! $cancellation_line_item instanceof EE_Line_Item) {
527
+        if ( ! $cancellation_line_item instanceof EE_Line_Item) {
528 528
             return false;
529 529
         }
530 530
         if ($cancellation_line_item->quantity() > $qty) {
@@ -729,7 +729,7 @@  discard block
 block discarded – undo
729 729
             'LIN_code'  => 'taxes',
730 730
             'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731 731
             'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
-            'LIN_order' => 1000,// this should always come last
732
+            'LIN_order' => 1000, // this should always come last
733 733
         ));
734 734
         $tax_line_item = apply_filters(
735 735
             'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
@@ -785,7 +785,7 @@  discard block
 block discarded – undo
785 785
      */
786 786
     public static function get_event_code($event)
787 787
     {
788
-        return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
788
+        return 'event-'.($event instanceof EE_Event ? $event->ID() : '0');
789 789
     }
790 790
 
791 791
 
@@ -834,7 +834,7 @@  discard block
 block discarded – undo
834 834
     public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835 835
     {
836 836
         $first_datetime = $ticket->first_datetime();
837
-        if (! $first_datetime instanceof EE_Datetime) {
837
+        if ( ! $first_datetime instanceof EE_Datetime) {
838 838
             throw new EE_Error(
839 839
                 sprintf(
840 840
                     __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
@@ -843,7 +843,7 @@  discard block
 block discarded – undo
843 843
             );
844 844
         }
845 845
         $event = $first_datetime->event();
846
-        if (! $event instanceof EE_Event) {
846
+        if ( ! $event instanceof EE_Event) {
847 847
             throw new EE_Error(
848 848
                 sprintf(
849 849
                     esc_html__(
@@ -855,7 +855,7 @@  discard block
 block discarded – undo
855 855
             );
856 856
         }
857 857
         $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
-        if (! $events_sub_total instanceof EE_Line_Item) {
858
+        if ( ! $events_sub_total instanceof EE_Line_Item) {
859 859
             throw new EE_Error(
860 860
                 sprintf(
861 861
                     esc_html__(
@@ -891,7 +891,7 @@  discard block
 block discarded – undo
891 891
         $found = false;
892 892
         foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893 893
             // default event subtotal, we should only ever find this the first time this method is called
894
-            if (! $event_line_item->OBJ_ID()) {
894
+            if ( ! $event_line_item->OBJ_ID()) {
895 895
                 // let's use this! but first... set the event details
896 896
                 EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897 897
                 $found = true;
@@ -903,7 +903,7 @@  discard block
 block discarded – undo
903 903
                 break;
904 904
             }
905 905
         }
906
-        if (! $found) {
906
+        if ( ! $found) {
907 907
             // there is no event sub-total yet, so add it
908 908
             $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909 909
             // create a new "event" subtotal below that
@@ -1018,7 +1018,7 @@  discard block
 block discarded – undo
1018 1018
     public static function ensure_taxes_applied($total_line_item)
1019 1019
     {
1020 1020
         $taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1021
-        if (! $taxes_subtotal->children()) {
1021
+        if ( ! $taxes_subtotal->children()) {
1022 1022
             self::apply_taxes($total_line_item);
1023 1023
         }
1024 1024
         return $taxes_subtotal->total();
@@ -1085,7 +1085,7 @@  discard block
 block discarded – undo
1085 1085
         do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1086 1086
 
1087 1087
         // check if only a single line_item_id was passed
1088
-        if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1088
+        if ( ! empty($line_item_codes) && ! is_array($line_item_codes)) {
1089 1089
             // place single line_item_id in an array to appear as multiple line_item_ids
1090 1090
             $line_item_codes = array($line_item_codes);
1091 1091
         }
@@ -1192,7 +1192,7 @@  discard block
 block discarded – undo
1192 1192
         if ($code_substring_for_whitelist !== null) {
1193 1193
             $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1194 1194
         }
1195
-        if (! $whitelisted && $line_item->is_line_item()) {
1195
+        if ( ! $whitelisted && $line_item->is_line_item()) {
1196 1196
             $line_item->set_is_taxable($taxable);
1197 1197
         }
1198 1198
         foreach ($line_item->children() as $child_line_item) {
@@ -1554,7 +1554,7 @@  discard block
 block discarded – undo
1554 1554
     public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1555 1555
     {
1556 1556
         echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1557
-        if (! $indentation) {
1557
+        if ( ! $indentation) {
1558 1558
             echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559 1559
         }
1560 1560
         for ($i = 0; $i < $indentation; $i++) {
@@ -1565,12 +1565,12 @@  discard block
 block discarded – undo
1565 1565
             if ($line_item->is_percent()) {
1566 1566
                 $breakdown = "{$line_item->percent()}%";
1567 1567
             } else {
1568
-                $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1568
+                $breakdown = '$'."{$line_item->unit_price()} x {$line_item->quantity()}";
1569 1569
             }
1570 1570
         }
1571 1571
         echo $line_item->name();
1572 1572
         echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1573
-        echo '$' . (string) $line_item->total();
1573
+        echo '$'.(string) $line_item->total();
1574 1574
         if ($breakdown) {
1575 1575
             echo " ( {$breakdown} )";
1576 1576
         }
@@ -1658,8 +1658,8 @@  discard block
 block discarded – undo
1658 1658
                         if ($line_item_id === 'taxable') {
1659 1659
                             continue;
1660 1660
                         }
1661
-                        $taxable_total = $running_totals['taxable'][ $line_item_id ];
1662
-                        $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1661
+                        $taxable_total = $running_totals['taxable'][$line_item_id];
1662
+                        $running_totals[$line_item_id] += ($taxable_total * $tax_percent_decimal);
1663 1663
                     }
1664 1664
                     break;
1665 1665
 
@@ -1667,7 +1667,7 @@  discard block
 block discarded – undo
1667 1667
                     // ticket line items or ????
1668 1668
                     if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1669 1669
                         // kk it's a ticket
1670
-                        if (isset($running_totals[ $child_line_item->ID() ])) {
1670
+                        if (isset($running_totals[$child_line_item->ID()])) {
1671 1671
                             // huh? that shouldn't happen.
1672 1672
                             $running_totals['total'] += $child_line_item->total();
1673 1673
                         } else {
@@ -1678,18 +1678,18 @@  discard block
 block discarded – undo
1678 1678
                                 $taxable_amount = 0;
1679 1679
                             }
1680 1680
                             // are we only calculating totals for some tickets?
1681
-                            if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1682
-                                $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1683
-                                $running_totals[ $child_line_item->ID() ] = $quantity
1681
+                            if (isset($billable_ticket_quantities[$child_line_item->OBJ_ID()])) {
1682
+                                $quantity = $billable_ticket_quantities[$child_line_item->OBJ_ID()];
1683
+                                $running_totals[$child_line_item->ID()] = $quantity
1684 1684
                                     ? $child_line_item->unit_price()
1685 1685
                                     : 0;
1686
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1686
+                                $running_totals['taxable'][$child_line_item->ID()] = $quantity
1687 1687
                                     ? $taxable_amount
1688 1688
                                     : 0;
1689 1689
                             } else {
1690 1690
                                 $quantity = $child_line_item->quantity();
1691
-                                $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1692
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1691
+                                $running_totals[$child_line_item->ID()] = $child_line_item->unit_price();
1692
+                                $running_totals['taxable'][$child_line_item->ID()] = $taxable_amount;
1693 1693
                             }
1694 1694
                             $running_totals['taxable']['total'] += $taxable_amount * $quantity;
1695 1695
                             $running_totals['total'] += $child_line_item->unit_price() * $quantity;
@@ -1709,12 +1709,12 @@  discard block
 block discarded – undo
1709 1709
                             }
1710 1710
                             // update the running totals
1711 1711
                             // yes this actually even works for the running grand total!
1712
-                            $running_totals[ $line_item_id ] =
1712
+                            $running_totals[$line_item_id] =
1713 1713
                                 $line_items_percent_of_running_total * $this_running_total;
1714 1714
 
1715 1715
                             if ($child_line_item->is_taxable()) {
1716
-                                $running_totals['taxable'][ $line_item_id ] =
1717
-                                    $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1716
+                                $running_totals['taxable'][$line_item_id] =
1717
+                                    $line_items_percent_of_running_total * $running_totals['taxable'][$line_item_id];
1718 1718
                             }
1719 1719
                         }
1720 1720
                     }
@@ -1747,8 +1747,8 @@  discard block
 block discarded – undo
1747 1747
             );
1748 1748
         }
1749 1749
         // ok now find this new registration's final price
1750
-        if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) {
1751
-            return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ];
1750
+        if (isset($final_prices_per_ticket_line_item[$ticket_line_item->ID()])) {
1751
+            return $final_prices_per_ticket_line_item[$ticket_line_item->ID()];
1752 1752
         }
1753 1753
         $message = sprintf(
1754 1754
             esc_html__(
@@ -1758,7 +1758,7 @@  discard block
 block discarded – undo
1758 1758
             $ticket_line_item->ID()
1759 1759
         );
1760 1760
         if (WP_DEBUG) {
1761
-            $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1761
+            $message .= '<br>'.print_r($final_prices_per_ticket_line_item, true);
1762 1762
             throw new OutOfRangeException($message);
1763 1763
         }
1764 1764
         EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
Please login to merge, or discard this patch.
Indentation   +2041 added lines, -2041 removed lines patch added patch discarded remove patch
@@ -21,2045 +21,2045 @@
 block discarded – undo
21 21
 class EEH_Line_Item
22 22
 {
23 23
 
24
-    /**
25
-     * Adds a simple item (unrelated to any other model object) to the provided PARENT line item.
26
-     * Does NOT automatically re-calculate the line item totals or update the related transaction.
27
-     * You should call recalculate_total_including_taxes() on the grant total line item after this
28
-     * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
29
-     * to keep the registration final prices in-sync with the transaction's total.
30
-     *
31
-     * @param EE_Line_Item $parent_line_item
32
-     * @param string       $name
33
-     * @param float        $unit_price
34
-     * @param string       $description
35
-     * @param int          $quantity
36
-     * @param boolean      $taxable
37
-     * @param boolean      $code if set to a value, ensures there is only one line item with that code
38
-     * @return boolean success
39
-     * @throws EE_Error
40
-     * @throws InvalidArgumentException
41
-     * @throws InvalidDataTypeException
42
-     * @throws InvalidInterfaceException
43
-     * @throws ReflectionException
44
-     */
45
-    public static function add_unrelated_item(
46
-        EE_Line_Item $parent_line_item,
47
-        $name,
48
-        $unit_price,
49
-        $description = '',
50
-        $quantity = 1,
51
-        $taxable = false,
52
-        $code = null
53
-    ) {
54
-        $items_subtotal = self::get_pre_tax_subtotal($parent_line_item);
55
-        $line_item = EE_Line_Item::new_instance(array(
56
-            'LIN_name'       => $name,
57
-            'LIN_desc'       => $description,
58
-            'LIN_unit_price' => $unit_price,
59
-            'LIN_quantity'   => $quantity,
60
-            'LIN_percent'    => null,
61
-            'LIN_is_taxable' => $taxable,
62
-            'LIN_order'      => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0,
63
-            'LIN_total'      => (float) $unit_price * (int) $quantity,
64
-            'LIN_type'       => EEM_Line_Item::type_line_item,
65
-            'LIN_code'       => $code,
66
-        ));
67
-        $line_item = apply_filters(
68
-            'FHEE__EEH_Line_Item__add_unrelated_item__line_item',
69
-            $line_item,
70
-            $parent_line_item
71
-        );
72
-        return self::add_item($parent_line_item, $line_item);
73
-    }
74
-
75
-
76
-    /**
77
-     * Adds a simple item ( unrelated to any other model object) to the total line item,
78
-     * in the correct spot in the line item tree. Does not automatically
79
-     * re-calculate the line item totals, nor update the related transaction, nor upgrade the transaction's
80
-     * registrations' final prices (which should probably change because of this).
81
-     * You should call recalculate_total_including_taxes() on the grand total line item, then
82
-     * update the transaction's total, and EE_Registration_Processor::update_registration_final_prices()
83
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
84
-     *
85
-     * @param EE_Line_Item $parent_line_item
86
-     * @param string       $name
87
-     * @param float        $percentage_amount
88
-     * @param string       $description
89
-     * @param boolean      $taxable
90
-     * @return boolean success
91
-     * @throws EE_Error
92
-     */
93
-    public static function add_percentage_based_item(
94
-        EE_Line_Item $parent_line_item,
95
-        $name,
96
-        $percentage_amount,
97
-        $description = '',
98
-        $taxable = false
99
-    ) {
100
-        $line_item = EE_Line_Item::new_instance(array(
101
-            'LIN_name'       => $name,
102
-            'LIN_desc'       => $description,
103
-            'LIN_unit_price' => 0,
104
-            'LIN_percent'    => $percentage_amount,
105
-            'LIN_quantity'   => 1,
106
-            'LIN_is_taxable' => $taxable,
107
-            'LIN_total'      => (float) ($percentage_amount * ($parent_line_item->total() / 100)),
108
-            'LIN_type'       => EEM_Line_Item::type_line_item,
109
-            'LIN_parent'     => $parent_line_item->ID(),
110
-        ));
111
-        $line_item = apply_filters(
112
-            'FHEE__EEH_Line_Item__add_percentage_based_item__line_item',
113
-            $line_item
114
-        );
115
-        return $parent_line_item->add_child_line_item($line_item, false);
116
-    }
117
-
118
-
119
-    /**
120
-     * Returns the new line item created by adding a purchase of the ticket
121
-     * ensures that ticket line item is saved, and that cart total has been recalculated.
122
-     * If this ticket has already been purchased, just increments its count.
123
-     * Automatically re-calculates the line item totals and updates the related transaction. But
124
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
125
-     * should probably change because of this).
126
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
127
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
128
-     *
129
-     * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total
130
-     * @param EE_Ticket    $ticket
131
-     * @param int          $qty
132
-     * @return EE_Line_Item
133
-     * @throws EE_Error
134
-     * @throws InvalidArgumentException
135
-     * @throws InvalidDataTypeException
136
-     * @throws InvalidInterfaceException
137
-     * @throws ReflectionException
138
-     */
139
-    public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140
-    {
141
-        if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142
-            throw new EE_Error(
143
-                sprintf(
144
-                    esc_html__(
145
-                        'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.',
146
-                        'event_espresso'
147
-                    ),
148
-                    $ticket->ID(),
149
-                    $total_line_item->ID()
150
-                )
151
-            );
152
-        }
153
-        // either increment the qty for an existing ticket
154
-        $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155
-        // or add a new one
156
-        if (! $line_item instanceof EE_Line_Item) {
157
-            $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158
-        }
159
-        $total_line_item->recalculate_total_including_taxes();
160
-        return $line_item;
161
-    }
162
-
163
-
164
-    /**
165
-     * Returns the new line item created by adding a purchase of the ticket
166
-     *
167
-     * @param EE_Line_Item $total_line_item
168
-     * @param EE_Ticket    $ticket
169
-     * @param int          $qty
170
-     * @return EE_Line_Item
171
-     * @throws EE_Error
172
-     * @throws InvalidArgumentException
173
-     * @throws InvalidDataTypeException
174
-     * @throws InvalidInterfaceException
175
-     * @throws ReflectionException
176
-     */
177
-    public static function increment_ticket_qty_if_already_in_cart(
178
-        EE_Line_Item $total_line_item,
179
-        EE_Ticket $ticket,
180
-        $qty = 1
181
-    ) {
182
-        $line_item = null;
183
-        if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) {
184
-            $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
185
-            foreach ((array) $ticket_line_items as $ticket_line_item) {
186
-                if ($ticket_line_item instanceof EE_Line_Item
187
-                    && (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID()
188
-                ) {
189
-                    $line_item = $ticket_line_item;
190
-                    break;
191
-                }
192
-            }
193
-        }
194
-        if ($line_item instanceof EE_Line_Item) {
195
-            EEH_Line_Item::increment_quantity($line_item, $qty);
196
-            return $line_item;
197
-        }
198
-        return null;
199
-    }
200
-
201
-
202
-    /**
203
-     * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected).
204
-     * Does NOT save or recalculate other line items totals
205
-     *
206
-     * @param EE_Line_Item $line_item
207
-     * @param int          $qty
208
-     * @return void
209
-     * @throws EE_Error
210
-     * @throws InvalidArgumentException
211
-     * @throws InvalidDataTypeException
212
-     * @throws InvalidInterfaceException
213
-     * @throws ReflectionException
214
-     */
215
-    public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216
-    {
217
-        if (! $line_item->is_percent()) {
218
-            $qty += $line_item->quantity();
219
-            $line_item->set_quantity($qty);
220
-            $line_item->set_total($line_item->unit_price() * $qty);
221
-            $line_item->save();
222
-        }
223
-        foreach ($line_item->children() as $child) {
224
-            if ($child->is_sub_line_item()) {
225
-                EEH_Line_Item::update_quantity($child, $qty);
226
-            }
227
-        }
228
-    }
229
-
230
-
231
-    /**
232
-     * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected).
233
-     * Does NOT save or recalculate other line items totals
234
-     *
235
-     * @param EE_Line_Item $line_item
236
-     * @param int          $qty
237
-     * @return void
238
-     * @throws EE_Error
239
-     * @throws InvalidArgumentException
240
-     * @throws InvalidDataTypeException
241
-     * @throws InvalidInterfaceException
242
-     * @throws ReflectionException
243
-     */
244
-    public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245
-    {
246
-        if (! $line_item->is_percent()) {
247
-            $qty = $line_item->quantity() - $qty;
248
-            $qty = max($qty, 0);
249
-            $line_item->set_quantity($qty);
250
-            $line_item->set_total($line_item->unit_price() * $qty);
251
-            $line_item->save();
252
-        }
253
-        foreach ($line_item->children() as $child) {
254
-            if ($child->is_sub_line_item()) {
255
-                EEH_Line_Item::update_quantity($child, $qty);
256
-            }
257
-        }
258
-    }
259
-
260
-
261
-    /**
262
-     * Updates the line item and its children's quantities to the specified number.
263
-     * Does NOT save them or recalculate totals.
264
-     *
265
-     * @param EE_Line_Item $line_item
266
-     * @param int          $new_quantity
267
-     * @throws EE_Error
268
-     * @throws InvalidArgumentException
269
-     * @throws InvalidDataTypeException
270
-     * @throws InvalidInterfaceException
271
-     * @throws ReflectionException
272
-     */
273
-    public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274
-    {
275
-        if (! $line_item->is_percent()) {
276
-            $line_item->set_quantity($new_quantity);
277
-            $line_item->set_total($line_item->unit_price() * $new_quantity);
278
-            $line_item->save();
279
-        }
280
-        foreach ($line_item->children() as $child) {
281
-            if ($child->is_sub_line_item()) {
282
-                EEH_Line_Item::update_quantity($child, $new_quantity);
283
-            }
284
-        }
285
-    }
286
-
287
-
288
-    /**
289
-     * Returns the new line item created by adding a purchase of the ticket
290
-     *
291
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
292
-     * @param EE_Ticket    $ticket
293
-     * @param int          $qty
294
-     * @return EE_Line_Item
295
-     * @throws EE_Error
296
-     * @throws InvalidArgumentException
297
-     * @throws InvalidDataTypeException
298
-     * @throws InvalidInterfaceException
299
-     * @throws ReflectionException
300
-     */
301
-    public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
302
-    {
303
-        $datetimes = $ticket->datetimes();
304
-        $first_datetime = reset($datetimes);
305
-        $first_datetime_name = esc_html__('Event', 'event_espresso');
306
-        if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) {
307
-            $first_datetime_name = $first_datetime->event()->name();
308
-        }
309
-        $event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name);
310
-        // get event subtotal line
311
-        $events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket);
312
-        // add $ticket to cart
313
-        $line_item = EE_Line_Item::new_instance(array(
314
-            'LIN_name'       => $ticket->name(),
315
-            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
316
-            'LIN_unit_price' => $ticket->price(),
317
-            'LIN_quantity'   => $qty,
318
-            'LIN_is_taxable' => $ticket->taxable(),
319
-            'LIN_order'      => count($events_sub_total->children()),
320
-            'LIN_total'      => $ticket->price() * $qty,
321
-            'LIN_type'       => EEM_Line_Item::type_line_item,
322
-            'OBJ_ID'         => $ticket->ID(),
323
-            'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_TICKET,
324
-        ));
325
-        $line_item = apply_filters(
326
-            'FHEE__EEH_Line_Item__create_ticket_line_item__line_item',
327
-            $line_item
328
-        );
329
-        $events_sub_total->add_child_line_item($line_item);
330
-        // now add the sub-line items
331
-        $running_total_for_ticket = 0;
332
-        foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
333
-            $sign = $price->is_discount() ? -1 : 1;
334
-            $price_total = $price->is_percent()
335
-                ? $running_total_for_ticket * $price->amount() / 100
336
-                : $price->amount() * $qty;
337
-            $sub_line_item = EE_Line_Item::new_instance(array(
338
-                'LIN_name'       => $price->name(),
339
-                'LIN_desc'       => $price->desc(),
340
-                'LIN_quantity'   => $price->is_percent() ? null : $qty,
341
-                'LIN_is_taxable' => false,
342
-                'LIN_order'      => $price->order(),
343
-                'LIN_total'      => $sign * $price_total,
344
-                'LIN_type'       => EEM_Line_Item::type_sub_line_item,
345
-                'OBJ_ID'         => $price->ID(),
346
-                'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
347
-            ));
348
-            $sub_line_item = apply_filters(
349
-                'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item',
350
-                $sub_line_item
351
-            );
352
-            if ($price->is_percent()) {
353
-                $sub_line_item->set_percent($sign * $price->amount());
354
-            } else {
355
-                $sub_line_item->set_unit_price($sign * $price->amount());
356
-            }
357
-            $running_total_for_ticket += $price_total;
358
-            $line_item->add_child_line_item($sub_line_item);
359
-        }
360
-        return $line_item;
361
-    }
362
-
363
-
364
-    /**
365
-     * Adds the specified item under the pre-tax-sub-total line item. Automatically
366
-     * re-calculates the line item totals and updates the related transaction. But
367
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
368
-     * should probably change because of this).
369
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
370
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
371
-     *
372
-     * @param EE_Line_Item $total_line_item
373
-     * @param EE_Line_Item $item to be added
374
-     * @return boolean
375
-     * @throws EE_Error
376
-     * @throws InvalidArgumentException
377
-     * @throws InvalidDataTypeException
378
-     * @throws InvalidInterfaceException
379
-     * @throws ReflectionException
380
-     */
381
-    public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item)
382
-    {
383
-        $pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item);
384
-        if ($pre_tax_subtotal instanceof EE_Line_Item) {
385
-            $success = $pre_tax_subtotal->add_child_line_item($item);
386
-        } else {
387
-            return false;
388
-        }
389
-        $total_line_item->recalculate_total_including_taxes();
390
-        return $success;
391
-    }
392
-
393
-
394
-    /**
395
-     * cancels an existing ticket line item,
396
-     * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item.
397
-     * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
398
-     *
399
-     * @param EE_Line_Item $ticket_line_item
400
-     * @param int          $qty
401
-     * @return bool success
402
-     * @throws EE_Error
403
-     * @throws InvalidArgumentException
404
-     * @throws InvalidDataTypeException
405
-     * @throws InvalidInterfaceException
406
-     * @throws ReflectionException
407
-     */
408
-    public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
409
-    {
410
-        // validate incoming line_item
411
-        if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
412
-            throw new EE_Error(
413
-                sprintf(
414
-                    esc_html__(
415
-                        'The supplied line item must have an Object Type of "Ticket", not %1$s.',
416
-                        'event_espresso'
417
-                    ),
418
-                    $ticket_line_item->type()
419
-                )
420
-            );
421
-        }
422
-        if ($ticket_line_item->quantity() < $qty) {
423
-            throw new EE_Error(
424
-                sprintf(
425
-                    esc_html__(
426
-                        'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.',
427
-                        'event_espresso'
428
-                    ),
429
-                    $qty,
430
-                    $ticket_line_item->quantity()
431
-                )
432
-            );
433
-        }
434
-        // decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this
435
-        $ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty);
436
-        foreach ($ticket_line_item->children() as $child_line_item) {
437
-            if ($child_line_item->is_sub_line_item()
438
-                && ! $child_line_item->is_percent()
439
-                && ! $child_line_item->is_cancellation()
440
-            ) {
441
-                $child_line_item->set_quantity($child_line_item->quantity() - $qty);
442
-            }
443
-        }
444
-        // get cancellation sub line item
445
-        $cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
446
-            $ticket_line_item,
447
-            EEM_Line_Item::type_cancellation
448
-        );
449
-        $cancellation_line_item = reset($cancellation_line_item);
450
-        // verify that this ticket was indeed previously cancelled
451
-        if ($cancellation_line_item instanceof EE_Line_Item) {
452
-            // increment cancelled quantity
453
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty);
454
-        } else {
455
-            // create cancellation sub line item
456
-            $cancellation_line_item = EE_Line_Item::new_instance(array(
457
-                'LIN_name'       => esc_html__('Cancellation', 'event_espresso'),
458
-                'LIN_desc'       => sprintf(
459
-                    esc_html_x(
460
-                        'Cancelled %1$s : %2$s',
461
-                        'Cancelled Ticket Name : 2015-01-01 11:11',
462
-                        'event_espresso'
463
-                    ),
464
-                    $ticket_line_item->name(),
465
-                    current_time(get_option('date_format') . ' ' . get_option('time_format'))
466
-                ),
467
-                'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468
-                'LIN_quantity'   => $qty,
469
-                'LIN_is_taxable' => $ticket_line_item->is_taxable(),
470
-                'LIN_order'      => count($ticket_line_item->children()),
471
-                'LIN_total'      => 0, // $ticket_line_item->unit_price()
472
-                'LIN_type'       => EEM_Line_Item::type_cancellation,
473
-            ));
474
-            $ticket_line_item->add_child_line_item($cancellation_line_item);
475
-        }
476
-        if ($ticket_line_item->save_this_and_descendants() > 0) {
477
-            // decrement parent line item quantity
478
-            $event_line_item = $ticket_line_item->parent();
479
-            if ($event_line_item instanceof EE_Line_Item
480
-                && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
481
-            ) {
482
-                $event_line_item->set_quantity($event_line_item->quantity() - $qty);
483
-                $event_line_item->save();
484
-            }
485
-            EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
486
-            return true;
487
-        }
488
-        return false;
489
-    }
490
-
491
-
492
-    /**
493
-     * reinstates (un-cancels?) a previously canceled ticket line item,
494
-     * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item.
495
-     * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
496
-     *
497
-     * @param EE_Line_Item $ticket_line_item
498
-     * @param int          $qty
499
-     * @return bool success
500
-     * @throws EE_Error
501
-     * @throws InvalidArgumentException
502
-     * @throws InvalidDataTypeException
503
-     * @throws InvalidInterfaceException
504
-     * @throws ReflectionException
505
-     */
506
-    public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
507
-    {
508
-        // validate incoming line_item
509
-        if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
510
-            throw new EE_Error(
511
-                sprintf(
512
-                    esc_html__(
513
-                        'The supplied line item must have an Object Type of "Ticket", not %1$s.',
514
-                        'event_espresso'
515
-                    ),
516
-                    $ticket_line_item->type()
517
-                )
518
-            );
519
-        }
520
-        // get cancellation sub line item
521
-        $cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
522
-            $ticket_line_item,
523
-            EEM_Line_Item::type_cancellation
524
-        );
525
-        $cancellation_line_item = reset($cancellation_line_item);
526
-        // verify that this ticket was indeed previously cancelled
527
-        if (! $cancellation_line_item instanceof EE_Line_Item) {
528
-            return false;
529
-        }
530
-        if ($cancellation_line_item->quantity() > $qty) {
531
-            // decrement cancelled quantity
532
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
533
-        } elseif ($cancellation_line_item->quantity() === $qty) {
534
-            // decrement cancelled quantity in case anyone still has the object kicking around
535
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
536
-            // delete because quantity will end up as 0
537
-            $cancellation_line_item->delete();
538
-            // and attempt to destroy the object,
539
-            // even though PHP won't actually destroy it until it needs the memory
540
-            unset($cancellation_line_item);
541
-        } else {
542
-            // what ?!?! negative quantity ?!?!
543
-            throw new EE_Error(
544
-                sprintf(
545
-                    esc_html__(
546
-                        'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.',
547
-                        'event_espresso'
548
-                    ),
549
-                    $qty,
550
-                    $cancellation_line_item->quantity()
551
-                )
552
-            );
553
-        }
554
-        // increment ticket quantity
555
-        $ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty);
556
-        if ($ticket_line_item->save_this_and_descendants() > 0) {
557
-            // increment parent line item quantity
558
-            $event_line_item = $ticket_line_item->parent();
559
-            if ($event_line_item instanceof EE_Line_Item
560
-                && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
561
-            ) {
562
-                $event_line_item->set_quantity($event_line_item->quantity() + $qty);
563
-            }
564
-            EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
565
-            return true;
566
-        }
567
-        return false;
568
-    }
569
-
570
-
571
-    /**
572
-     * calls EEH_Line_Item::find_transaction_grand_total_for_line_item()
573
-     * then EE_Line_Item::recalculate_total_including_taxes() on the result
574
-     *
575
-     * @param EE_Line_Item $line_item
576
-     * @return float
577
-     * @throws EE_Error
578
-     * @throws InvalidArgumentException
579
-     * @throws InvalidDataTypeException
580
-     * @throws InvalidInterfaceException
581
-     * @throws ReflectionException
582
-     */
583
-    public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item)
584
-    {
585
-        $grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item);
586
-        return $grand_total_line_item->recalculate_total_including_taxes();
587
-    }
588
-
589
-
590
-    /**
591
-     * Gets the line item which contains the subtotal of all the items
592
-     *
593
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
594
-     * @return EE_Line_Item
595
-     * @throws EE_Error
596
-     * @throws InvalidArgumentException
597
-     * @throws InvalidDataTypeException
598
-     * @throws InvalidInterfaceException
599
-     * @throws ReflectionException
600
-     */
601
-    public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item)
602
-    {
603
-        $pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal');
604
-        return $pre_tax_subtotal instanceof EE_Line_Item
605
-            ? $pre_tax_subtotal
606
-            : self::create_pre_tax_subtotal($total_line_item);
607
-    }
608
-
609
-
610
-    /**
611
-     * Gets the line item for the taxes subtotal
612
-     *
613
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
614
-     * @return EE_Line_Item
615
-     * @throws EE_Error
616
-     * @throws InvalidArgumentException
617
-     * @throws InvalidDataTypeException
618
-     * @throws InvalidInterfaceException
619
-     * @throws ReflectionException
620
-     */
621
-    public static function get_taxes_subtotal(EE_Line_Item $total_line_item)
622
-    {
623
-        $taxes = $total_line_item->get_child_line_item('taxes');
624
-        return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item);
625
-    }
626
-
627
-
628
-    /**
629
-     * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object
630
-     *
631
-     * @param EE_Line_Item   $line_item
632
-     * @param EE_Transaction $transaction
633
-     * @return void
634
-     * @throws EE_Error
635
-     * @throws InvalidArgumentException
636
-     * @throws InvalidDataTypeException
637
-     * @throws InvalidInterfaceException
638
-     * @throws ReflectionException
639
-     */
640
-    public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null)
641
-    {
642
-        if ($transaction) {
643
-            /** @type EEM_Transaction $EEM_Transaction */
644
-            $EEM_Transaction = EE_Registry::instance()->load_model('Transaction');
645
-            $TXN_ID = $EEM_Transaction->ensure_is_ID($transaction);
646
-            $line_item->set_TXN_ID($TXN_ID);
647
-        }
648
-    }
649
-
650
-
651
-    /**
652
-     * Creates a new default total line item for the transaction,
653
-     * and its tickets subtotal and taxes subtotal line items (and adds the
654
-     * existing taxes as children of the taxes subtotal line item)
655
-     *
656
-     * @param EE_Transaction $transaction
657
-     * @return EE_Line_Item of type total
658
-     * @throws EE_Error
659
-     * @throws InvalidArgumentException
660
-     * @throws InvalidDataTypeException
661
-     * @throws InvalidInterfaceException
662
-     * @throws ReflectionException
663
-     */
664
-    public static function create_total_line_item($transaction = null)
665
-    {
666
-        $total_line_item = EE_Line_Item::new_instance(array(
667
-            'LIN_code' => 'total',
668
-            'LIN_name' => esc_html__('Grand Total', 'event_espresso'),
669
-            'LIN_type' => EEM_Line_Item::type_total,
670
-            'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION,
671
-        ));
672
-        $total_line_item = apply_filters(
673
-            'FHEE__EEH_Line_Item__create_total_line_item__total_line_item',
674
-            $total_line_item
675
-        );
676
-        self::set_TXN_ID($total_line_item, $transaction);
677
-        self::create_pre_tax_subtotal($total_line_item, $transaction);
678
-        self::create_taxes_subtotal($total_line_item, $transaction);
679
-        return $total_line_item;
680
-    }
681
-
682
-
683
-    /**
684
-     * Creates a default items subtotal line item
685
-     *
686
-     * @param EE_Line_Item   $total_line_item
687
-     * @param EE_Transaction $transaction
688
-     * @return EE_Line_Item
689
-     * @throws EE_Error
690
-     * @throws InvalidArgumentException
691
-     * @throws InvalidDataTypeException
692
-     * @throws InvalidInterfaceException
693
-     * @throws ReflectionException
694
-     */
695
-    protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null)
696
-    {
697
-        $pre_tax_line_item = EE_Line_Item::new_instance(array(
698
-            'LIN_code' => 'pre-tax-subtotal',
699
-            'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'),
700
-            'LIN_type' => EEM_Line_Item::type_sub_total,
701
-        ));
702
-        $pre_tax_line_item = apply_filters(
703
-            'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item',
704
-            $pre_tax_line_item
705
-        );
706
-        self::set_TXN_ID($pre_tax_line_item, $transaction);
707
-        $total_line_item->add_child_line_item($pre_tax_line_item);
708
-        self::create_event_subtotal($pre_tax_line_item, $transaction);
709
-        return $pre_tax_line_item;
710
-    }
711
-
712
-
713
-    /**
714
-     * Creates a line item for the taxes subtotal and finds all the tax prices
715
-     * and applies taxes to it
716
-     *
717
-     * @param EE_Line_Item   $total_line_item of type EEM_Line_Item::type_total
718
-     * @param EE_Transaction $transaction
719
-     * @return EE_Line_Item
720
-     * @throws EE_Error
721
-     * @throws InvalidArgumentException
722
-     * @throws InvalidDataTypeException
723
-     * @throws InvalidInterfaceException
724
-     * @throws ReflectionException
725
-     */
726
-    protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
727
-    {
728
-        $tax_line_item = EE_Line_Item::new_instance(array(
729
-            'LIN_code'  => 'taxes',
730
-            'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731
-            'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
-            'LIN_order' => 1000,// this should always come last
733
-        ));
734
-        $tax_line_item = apply_filters(
735
-            'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
736
-            $tax_line_item
737
-        );
738
-        self::set_TXN_ID($tax_line_item, $transaction);
739
-        $total_line_item->add_child_line_item($tax_line_item);
740
-        // and lastly, add the actual taxes
741
-        self::apply_taxes($total_line_item);
742
-        return $tax_line_item;
743
-    }
744
-
745
-
746
-    /**
747
-     * Creates a default items subtotal line item
748
-     *
749
-     * @param EE_Line_Item   $pre_tax_line_item
750
-     * @param EE_Transaction $transaction
751
-     * @param EE_Event       $event
752
-     * @return EE_Line_Item
753
-     * @throws EE_Error
754
-     * @throws InvalidArgumentException
755
-     * @throws InvalidDataTypeException
756
-     * @throws InvalidInterfaceException
757
-     * @throws ReflectionException
758
-     */
759
-    public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null)
760
-    {
761
-        $event_line_item = EE_Line_Item::new_instance(array(
762
-            'LIN_code' => self::get_event_code($event),
763
-            'LIN_name' => self::get_event_name($event),
764
-            'LIN_desc' => self::get_event_desc($event),
765
-            'LIN_type' => EEM_Line_Item::type_sub_total,
766
-            'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT,
767
-            'OBJ_ID'   => $event instanceof EE_Event ? $event->ID() : 0,
768
-        ));
769
-        $event_line_item = apply_filters(
770
-            'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item',
771
-            $event_line_item
772
-        );
773
-        self::set_TXN_ID($event_line_item, $transaction);
774
-        $pre_tax_line_item->add_child_line_item($event_line_item);
775
-        return $event_line_item;
776
-    }
777
-
778
-
779
-    /**
780
-     * Gets what the event ticket's code SHOULD be
781
-     *
782
-     * @param EE_Event $event
783
-     * @return string
784
-     * @throws EE_Error
785
-     */
786
-    public static function get_event_code($event)
787
-    {
788
-        return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
789
-    }
790
-
791
-
792
-    /**
793
-     * Gets the event name
794
-     *
795
-     * @param EE_Event $event
796
-     * @return string
797
-     * @throws EE_Error
798
-     */
799
-    public static function get_event_name($event)
800
-    {
801
-        return $event instanceof EE_Event
802
-            ? mb_substr($event->name(), 0, 245)
803
-            : esc_html__('Event', 'event_espresso');
804
-    }
805
-
806
-
807
-    /**
808
-     * Gets the event excerpt
809
-     *
810
-     * @param EE_Event $event
811
-     * @return string
812
-     * @throws EE_Error
813
-     */
814
-    public static function get_event_desc($event)
815
-    {
816
-        return $event instanceof EE_Event ? $event->short_description() : '';
817
-    }
818
-
819
-
820
-    /**
821
-     * Given the grand total line item and a ticket, finds the event sub-total
822
-     * line item the ticket's purchase should be added onto
823
-     *
824
-     * @access public
825
-     * @param EE_Line_Item $grand_total the grand total line item
826
-     * @param EE_Ticket    $ticket
827
-     * @return EE_Line_Item
828
-     * @throws EE_Error
829
-     * @throws InvalidArgumentException
830
-     * @throws InvalidDataTypeException
831
-     * @throws InvalidInterfaceException
832
-     * @throws ReflectionException
833
-     */
834
-    public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835
-    {
836
-        $first_datetime = $ticket->first_datetime();
837
-        if (! $first_datetime instanceof EE_Datetime) {
838
-            throw new EE_Error(
839
-                sprintf(
840
-                    __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
841
-                    $ticket->ID()
842
-                )
843
-            );
844
-        }
845
-        $event = $first_datetime->event();
846
-        if (! $event instanceof EE_Event) {
847
-            throw new EE_Error(
848
-                sprintf(
849
-                    esc_html__(
850
-                        'The supplied ticket (ID %d) has no event data associated with it.',
851
-                        'event_espresso'
852
-                    ),
853
-                    $ticket->ID()
854
-                )
855
-            );
856
-        }
857
-        $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
-        if (! $events_sub_total instanceof EE_Line_Item) {
859
-            throw new EE_Error(
860
-                sprintf(
861
-                    esc_html__(
862
-                        'There is no events sub-total for ticket %s on total line item %d',
863
-                        'event_espresso'
864
-                    ),
865
-                    $ticket->ID(),
866
-                    $grand_total->ID()
867
-                )
868
-            );
869
-        }
870
-        return $events_sub_total;
871
-    }
872
-
873
-
874
-    /**
875
-     * Gets the event line item
876
-     *
877
-     * @param EE_Line_Item $grand_total
878
-     * @param EE_Event     $event
879
-     * @return EE_Line_Item for the event subtotal which is a child of $grand_total
880
-     * @throws EE_Error
881
-     * @throws InvalidArgumentException
882
-     * @throws InvalidDataTypeException
883
-     * @throws InvalidInterfaceException
884
-     * @throws ReflectionException
885
-     */
886
-    public static function get_event_line_item(EE_Line_Item $grand_total, $event)
887
-    {
888
-        /** @type EE_Event $event */
889
-        $event = EEM_Event::instance()->ensure_is_obj($event, true);
890
-        $event_line_item = null;
891
-        $found = false;
892
-        foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893
-            // default event subtotal, we should only ever find this the first time this method is called
894
-            if (! $event_line_item->OBJ_ID()) {
895
-                // let's use this! but first... set the event details
896
-                EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897
-                $found = true;
898
-                break;
899
-            }
900
-            if ($event_line_item->OBJ_ID() === $event->ID()) {
901
-                // found existing line item for this event in the cart, so break out of loop and use this one
902
-                $found = true;
903
-                break;
904
-            }
905
-        }
906
-        if (! $found) {
907
-            // there is no event sub-total yet, so add it
908
-            $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909
-            // create a new "event" subtotal below that
910
-            $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
911
-            // and set the event details
912
-            EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
913
-        }
914
-        return $event_line_item;
915
-    }
916
-
917
-
918
-    /**
919
-     * Creates a default items subtotal line item
920
-     *
921
-     * @param EE_Line_Item   $event_line_item
922
-     * @param EE_Event       $event
923
-     * @param EE_Transaction $transaction
924
-     * @return void
925
-     * @throws EE_Error
926
-     * @throws InvalidArgumentException
927
-     * @throws InvalidDataTypeException
928
-     * @throws InvalidInterfaceException
929
-     * @throws ReflectionException
930
-     */
931
-    public static function set_event_subtotal_details(
932
-        EE_Line_Item $event_line_item,
933
-        EE_Event $event,
934
-        $transaction = null
935
-    ) {
936
-        if ($event instanceof EE_Event) {
937
-            $event_line_item->set_code(self::get_event_code($event));
938
-            $event_line_item->set_name(self::get_event_name($event));
939
-            $event_line_item->set_desc(self::get_event_desc($event));
940
-            $event_line_item->set_OBJ_ID($event->ID());
941
-        }
942
-        self::set_TXN_ID($event_line_item, $transaction);
943
-    }
944
-
945
-
946
-    /**
947
-     * Finds what taxes should apply, adds them as tax line items under the taxes sub-total,
948
-     * and recalculates the taxes sub-total and the grand total. Resets the taxes, so
949
-     * any old taxes are removed
950
-     *
951
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
952
-     * @param bool         $update_txn_status
953
-     * @return bool
954
-     * @throws EE_Error
955
-     * @throws InvalidArgumentException
956
-     * @throws InvalidDataTypeException
957
-     * @throws InvalidInterfaceException
958
-     * @throws ReflectionException
959
-     * @throws RuntimeException
960
-     */
961
-    public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false)
962
-    {
963
-        /** @type EEM_Price $EEM_Price */
964
-        $EEM_Price = EE_Registry::instance()->load_model('Price');
965
-        // get array of taxes via Price Model
966
-        $ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes();
967
-        ksort($ordered_taxes);
968
-        $taxes_line_item = self::get_taxes_subtotal($total_line_item);
969
-        // just to be safe, remove its old tax line items
970
-        $deleted = $taxes_line_item->delete_children_line_items();
971
-        $updates = false;
972
-        // loop thru taxes
973
-        foreach ($ordered_taxes as $order => $taxes) {
974
-            foreach ($taxes as $tax) {
975
-                if ($tax instanceof EE_Price) {
976
-                    $tax_line_item = EE_Line_Item::new_instance(
977
-                        array(
978
-                            'LIN_name'       => $tax->name(),
979
-                            'LIN_desc'       => $tax->desc(),
980
-                            'LIN_percent'    => $tax->amount(),
981
-                            'LIN_is_taxable' => false,
982
-                            'LIN_order'      => $order,
983
-                            'LIN_total'      => 0,
984
-                            'LIN_type'       => EEM_Line_Item::type_tax,
985
-                            'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
986
-                            'OBJ_ID'         => $tax->ID(),
987
-                        )
988
-                    );
989
-                    $tax_line_item = apply_filters(
990
-                        'FHEE__EEH_Line_Item__apply_taxes__tax_line_item',
991
-                        $tax_line_item
992
-                    );
993
-                    $updates = $taxes_line_item->add_child_line_item($tax_line_item) ?
994
-                        true :
995
-                        $updates;
996
-                }
997
-            }
998
-        }
999
-        // only recalculate totals if something changed
1000
-        if ($deleted || $updates) {
1001
-            $total_line_item->recalculate_total_including_taxes($update_txn_status);
1002
-            return true;
1003
-        }
1004
-        return false;
1005
-    }
1006
-
1007
-
1008
-    /**
1009
-     * Ensures that taxes have been applied to the order, if not applies them.
1010
-     * Returns the total amount of tax
1011
-     *
1012
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
1013
-     * @return float
1014
-     * @throws EE_Error
1015
-     * @throws InvalidArgumentException
1016
-     * @throws InvalidDataTypeException
1017
-     * @throws InvalidInterfaceException
1018
-     * @throws ReflectionException
1019
-     */
1020
-    public static function ensure_taxes_applied($total_line_item)
1021
-    {
1022
-        $taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1023
-        if (! $taxes_subtotal->children()) {
1024
-            self::apply_taxes($total_line_item);
1025
-        }
1026
-        return $taxes_subtotal->total();
1027
-    }
1028
-
1029
-
1030
-    /**
1031
-     * Deletes ALL children of the passed line item
1032
-     *
1033
-     * @param EE_Line_Item $parent_line_item
1034
-     * @return bool
1035
-     * @throws EE_Error
1036
-     * @throws InvalidArgumentException
1037
-     * @throws InvalidDataTypeException
1038
-     * @throws InvalidInterfaceException
1039
-     * @throws ReflectionException
1040
-     */
1041
-    public static function delete_all_child_items(EE_Line_Item $parent_line_item)
1042
-    {
1043
-        $deleted = 0;
1044
-        foreach ($parent_line_item->children() as $child_line_item) {
1045
-            if ($child_line_item instanceof EE_Line_Item) {
1046
-                $deleted += EEH_Line_Item::delete_all_child_items($child_line_item);
1047
-                if ($child_line_item->ID()) {
1048
-                    $child_line_item->delete();
1049
-                    unset($child_line_item);
1050
-                } else {
1051
-                    $parent_line_item->delete_child_line_item($child_line_item->code());
1052
-                }
1053
-                $deleted++;
1054
-            }
1055
-        }
1056
-        return $deleted;
1057
-    }
1058
-
1059
-
1060
-    /**
1061
-     * Deletes the line items as indicated by the line item code(s) provided,
1062
-     * regardless of where they're found in the line item tree. Automatically
1063
-     * re-calculates the line item totals and updates the related transaction. But
1064
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
1065
-     * should probably change because of this).
1066
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
1067
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
1068
-     *
1069
-     * @param EE_Line_Item      $total_line_item of type EEM_Line_Item::type_total
1070
-     * @param array|bool|string $line_item_codes
1071
-     * @return int number of items successfully removed
1072
-     * @throws EE_Error
1073
-     */
1074
-    public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false)
1075
-    {
1076
-
1077
-        if ($total_line_item->type() !== EEM_Line_Item::type_total) {
1078
-            EE_Error::doing_it_wrong(
1079
-                'EEH_Line_Item::delete_items',
1080
-                esc_html__(
1081
-                    'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly',
1082
-                    'event_espresso'
1083
-                ),
1084
-                '4.6.18'
1085
-            );
1086
-        }
1087
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1088
-
1089
-        // check if only a single line_item_id was passed
1090
-        if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1091
-            // place single line_item_id in an array to appear as multiple line_item_ids
1092
-            $line_item_codes = array($line_item_codes);
1093
-        }
1094
-        $removals = 0;
1095
-        // cycle thru line_item_ids
1096
-        foreach ($line_item_codes as $line_item_id) {
1097
-            $removals += $total_line_item->delete_child_line_item($line_item_id);
1098
-        }
1099
-
1100
-        if ($removals > 0) {
1101
-            $total_line_item->recalculate_taxes_and_tax_total();
1102
-            return $removals;
1103
-        } else {
1104
-            return false;
1105
-        }
1106
-    }
1107
-
1108
-
1109
-    /**
1110
-     * Overwrites the previous tax by clearing out the old taxes, and creates a new
1111
-     * tax and updates the total line item accordingly
1112
-     *
1113
-     * @param EE_Line_Item $total_line_item
1114
-     * @param float        $amount
1115
-     * @param string       $name
1116
-     * @param string       $description
1117
-     * @param string       $code
1118
-     * @param boolean      $add_to_existing_line_item
1119
-     *                          if true, and a duplicate line item with the same code is found,
1120
-     *                          $amount will be added onto it; otherwise will simply set the taxes to match $amount
1121
-     * @return EE_Line_Item the new tax line item created
1122
-     * @throws EE_Error
1123
-     * @throws InvalidArgumentException
1124
-     * @throws InvalidDataTypeException
1125
-     * @throws InvalidInterfaceException
1126
-     * @throws ReflectionException
1127
-     */
1128
-    public static function set_total_tax_to(
1129
-        EE_Line_Item $total_line_item,
1130
-        $amount,
1131
-        $name = null,
1132
-        $description = null,
1133
-        $code = null,
1134
-        $add_to_existing_line_item = false
1135
-    ) {
1136
-        $tax_subtotal = self::get_taxes_subtotal($total_line_item);
1137
-        $taxable_total = $total_line_item->taxable_total();
1138
-
1139
-        if ($add_to_existing_line_item) {
1140
-            $new_tax = $tax_subtotal->get_child_line_item($code);
1141
-            EEM_Line_Item::instance()->delete(
1142
-                array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID()))
1143
-            );
1144
-        } else {
1145
-            $new_tax = null;
1146
-            $tax_subtotal->delete_children_line_items();
1147
-        }
1148
-        if ($new_tax) {
1149
-            $new_tax->set_total($new_tax->total() + $amount);
1150
-            $new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0);
1151
-        } else {
1152
-            // no existing tax item. Create it
1153
-            $new_tax = EE_Line_Item::new_instance(array(
1154
-                'TXN_ID'      => $total_line_item->TXN_ID(),
1155
-                'LIN_name'    => $name ? $name : esc_html__('Tax', 'event_espresso'),
1156
-                'LIN_desc'    => $description ? $description : '',
1157
-                'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0,
1158
-                'LIN_total'   => $amount,
1159
-                'LIN_parent'  => $tax_subtotal->ID(),
1160
-                'LIN_type'    => EEM_Line_Item::type_tax,
1161
-                'LIN_code'    => $code,
1162
-            ));
1163
-        }
1164
-
1165
-        $new_tax = apply_filters(
1166
-            'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal',
1167
-            $new_tax,
1168
-            $total_line_item
1169
-        );
1170
-        $new_tax->save();
1171
-        $tax_subtotal->set_total($new_tax->total());
1172
-        $tax_subtotal->save();
1173
-        $total_line_item->recalculate_total_including_taxes();
1174
-        return $new_tax;
1175
-    }
1176
-
1177
-
1178
-    /**
1179
-     * Makes all the line items which are children of $line_item taxable (or not).
1180
-     * Does NOT save the line items
1181
-     *
1182
-     * @param EE_Line_Item $line_item
1183
-     * @param boolean      $taxable
1184
-     * @param string       $code_substring_for_whitelist if this string is part of the line item's code
1185
-     *                                                   it will be whitelisted (ie, except from becoming taxable)
1186
-     * @throws EE_Error
1187
-     */
1188
-    public static function set_line_items_taxable(
1189
-        EE_Line_Item $line_item,
1190
-        $taxable = true,
1191
-        $code_substring_for_whitelist = null
1192
-    ) {
1193
-        $whitelisted = false;
1194
-        if ($code_substring_for_whitelist !== null) {
1195
-            $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1196
-        }
1197
-        if (! $whitelisted && $line_item->is_line_item()) {
1198
-            $line_item->set_is_taxable($taxable);
1199
-        }
1200
-        foreach ($line_item->children() as $child_line_item) {
1201
-            EEH_Line_Item::set_line_items_taxable(
1202
-                $child_line_item,
1203
-                $taxable,
1204
-                $code_substring_for_whitelist
1205
-            );
1206
-        }
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     * Gets all descendants that are event subtotals
1212
-     *
1213
-     * @uses  EEH_Line_Item::get_subtotals_of_object_type()
1214
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1215
-     * @return EE_Line_Item[]
1216
-     * @throws EE_Error
1217
-     */
1218
-    public static function get_event_subtotals(EE_Line_Item $parent_line_item)
1219
-    {
1220
-        return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT);
1221
-    }
1222
-
1223
-
1224
-    /**
1225
-     * Gets all descendants subtotals that match the supplied object type
1226
-     *
1227
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1228
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1229
-     * @param string       $obj_type
1230
-     * @return EE_Line_Item[]
1231
-     * @throws EE_Error
1232
-     */
1233
-    public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1234
-    {
1235
-        return self::_get_descendants_by_type_and_object_type(
1236
-            $parent_line_item,
1237
-            EEM_Line_Item::type_sub_total,
1238
-            $obj_type
1239
-        );
1240
-    }
1241
-
1242
-
1243
-    /**
1244
-     * Gets all descendants that are tickets
1245
-     *
1246
-     * @uses  EEH_Line_Item::get_line_items_of_object_type()
1247
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1248
-     * @return EE_Line_Item[]
1249
-     * @throws EE_Error
1250
-     */
1251
-    public static function get_ticket_line_items(EE_Line_Item $parent_line_item)
1252
-    {
1253
-        return self::get_line_items_of_object_type(
1254
-            $parent_line_item,
1255
-            EEM_Line_Item::OBJ_TYPE_TICKET
1256
-        );
1257
-    }
1258
-
1259
-
1260
-    /**
1261
-     * Gets all descendants subtotals that match the supplied object type
1262
-     *
1263
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1264
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1265
-     * @param string       $obj_type
1266
-     * @return EE_Line_Item[]
1267
-     * @throws EE_Error
1268
-     */
1269
-    public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1270
-    {
1271
-        return self::_get_descendants_by_type_and_object_type(
1272
-            $parent_line_item,
1273
-            EEM_Line_Item::type_line_item,
1274
-            $obj_type
1275
-        );
1276
-    }
1277
-
1278
-
1279
-    /**
1280
-     * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax'
1281
-     *
1282
-     * @uses  EEH_Line_Item::get_descendants_of_type()
1283
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1284
-     * @return EE_Line_Item[]
1285
-     * @throws EE_Error
1286
-     */
1287
-    public static function get_tax_descendants(EE_Line_Item $parent_line_item)
1288
-    {
1289
-        return EEH_Line_Item::get_descendants_of_type(
1290
-            $parent_line_item,
1291
-            EEM_Line_Item::type_tax
1292
-        );
1293
-    }
1294
-
1295
-
1296
-    /**
1297
-     * Gets all the real items purchased which are children of this item
1298
-     *
1299
-     * @uses  EEH_Line_Item::get_descendants_of_type()
1300
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1301
-     * @return EE_Line_Item[]
1302
-     * @throws EE_Error
1303
-     */
1304
-    public static function get_line_item_descendants(EE_Line_Item $parent_line_item)
1305
-    {
1306
-        return EEH_Line_Item::get_descendants_of_type(
1307
-            $parent_line_item,
1308
-            EEM_Line_Item::type_line_item
1309
-        );
1310
-    }
1311
-
1312
-
1313
-    /**
1314
-     * Gets all descendants of supplied line item that match the supplied line item type
1315
-     *
1316
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1317
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1318
-     * @param string       $line_item_type   one of the EEM_Line_Item constants
1319
-     * @return EE_Line_Item[]
1320
-     * @throws EE_Error
1321
-     */
1322
-    public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type)
1323
-    {
1324
-        return self::_get_descendants_by_type_and_object_type(
1325
-            $parent_line_item,
1326
-            $line_item_type,
1327
-            null
1328
-        );
1329
-    }
1330
-
1331
-
1332
-    /**
1333
-     * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1334
-     * as well
1335
-     *
1336
-     * @param EE_Line_Item  $parent_line_item - the line item to find descendants of
1337
-     * @param string        $line_item_type   one of the EEM_Line_Item constants
1338
-     * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1339
-     *                                        searching
1340
-     * @return EE_Line_Item[]
1341
-     * @throws EE_Error
1342
-     */
1343
-    protected static function _get_descendants_by_type_and_object_type(
1344
-        EE_Line_Item $parent_line_item,
1345
-        $line_item_type,
1346
-        $obj_type = null
1347
-    ) {
1348
-        $objects = array();
1349
-        foreach ($parent_line_item->children() as $child_line_item) {
1350
-            if ($child_line_item instanceof EE_Line_Item) {
1351
-                if ($child_line_item->type() === $line_item_type
1352
-                    && (
1353
-                        $child_line_item->OBJ_type() === $obj_type || $obj_type === null
1354
-                    )
1355
-                ) {
1356
-                    $objects[] = $child_line_item;
1357
-                } else {
1358
-                    // go-through-all-its children looking for more matches
1359
-                    $objects = array_merge(
1360
-                        $objects,
1361
-                        self::_get_descendants_by_type_and_object_type(
1362
-                            $child_line_item,
1363
-                            $line_item_type,
1364
-                            $obj_type
1365
-                        )
1366
-                    );
1367
-                }
1368
-            }
1369
-        }
1370
-        return $objects;
1371
-    }
1372
-
1373
-
1374
-    /**
1375
-     * Gets all descendants subtotals that match the supplied object type
1376
-     *
1377
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1378
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1379
-     * @param string       $OBJ_type         object type (like Event)
1380
-     * @param array        $OBJ_IDs          array of OBJ_IDs
1381
-     * @return EE_Line_Item[]
1382
-     * @throws EE_Error
1383
-     */
1384
-    public static function get_line_items_by_object_type_and_IDs(
1385
-        EE_Line_Item $parent_line_item,
1386
-        $OBJ_type = '',
1387
-        $OBJ_IDs = array()
1388
-    ) {
1389
-        return self::_get_descendants_by_object_type_and_object_ID(
1390
-            $parent_line_item,
1391
-            $OBJ_type,
1392
-            $OBJ_IDs
1393
-        );
1394
-    }
1395
-
1396
-
1397
-    /**
1398
-     * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1399
-     * as well
1400
-     *
1401
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1402
-     * @param string       $OBJ_type         object type (like Event)
1403
-     * @param array        $OBJ_IDs          array of OBJ_IDs
1404
-     * @return EE_Line_Item[]
1405
-     * @throws EE_Error
1406
-     */
1407
-    protected static function _get_descendants_by_object_type_and_object_ID(
1408
-        EE_Line_Item $parent_line_item,
1409
-        $OBJ_type,
1410
-        $OBJ_IDs
1411
-    ) {
1412
-        $objects = array();
1413
-        foreach ($parent_line_item->children() as $child_line_item) {
1414
-            if ($child_line_item instanceof EE_Line_Item) {
1415
-                if ($child_line_item->OBJ_type() === $OBJ_type
1416
-                    && is_array($OBJ_IDs)
1417
-                    && in_array($child_line_item->OBJ_ID(), $OBJ_IDs)
1418
-                ) {
1419
-                    $objects[] = $child_line_item;
1420
-                } else {
1421
-                    // go-through-all-its children looking for more matches
1422
-                    $objects = array_merge(
1423
-                        $objects,
1424
-                        self::_get_descendants_by_object_type_and_object_ID(
1425
-                            $child_line_item,
1426
-                            $OBJ_type,
1427
-                            $OBJ_IDs
1428
-                        )
1429
-                    );
1430
-                }
1431
-            }
1432
-        }
1433
-        return $objects;
1434
-    }
1435
-
1436
-
1437
-    /**
1438
-     * Uses a breadth-first-search in order to find the nearest descendant of
1439
-     * the specified type and returns it, else NULL
1440
-     *
1441
-     * @uses  EEH_Line_Item::_get_nearest_descendant()
1442
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1443
-     * @param string       $type             like one of the EEM_Line_Item::type_*
1444
-     * @return EE_Line_Item
1445
-     * @throws EE_Error
1446
-     * @throws InvalidArgumentException
1447
-     * @throws InvalidDataTypeException
1448
-     * @throws InvalidInterfaceException
1449
-     * @throws ReflectionException
1450
-     */
1451
-    public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type)
1452
-    {
1453
-        return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type);
1454
-    }
1455
-
1456
-
1457
-    /**
1458
-     * Uses a breadth-first-search in order to find the nearest descendant
1459
-     * having the specified LIN_code and returns it, else NULL
1460
-     *
1461
-     * @uses  EEH_Line_Item::_get_nearest_descendant()
1462
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1463
-     * @param string       $code             any value used for LIN_code
1464
-     * @return EE_Line_Item
1465
-     * @throws EE_Error
1466
-     * @throws InvalidArgumentException
1467
-     * @throws InvalidDataTypeException
1468
-     * @throws InvalidInterfaceException
1469
-     * @throws ReflectionException
1470
-     */
1471
-    public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code)
1472
-    {
1473
-        return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code);
1474
-    }
1475
-
1476
-
1477
-    /**
1478
-     * Uses a breadth-first-search in order to find the nearest descendant
1479
-     * having the specified LIN_code and returns it, else NULL
1480
-     *
1481
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1482
-     * @param string       $search_field     name of EE_Line_Item property
1483
-     * @param string       $value            any value stored in $search_field
1484
-     * @return EE_Line_Item
1485
-     * @throws EE_Error
1486
-     * @throws InvalidArgumentException
1487
-     * @throws InvalidDataTypeException
1488
-     * @throws InvalidInterfaceException
1489
-     * @throws ReflectionException
1490
-     */
1491
-    protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value)
1492
-    {
1493
-        foreach ($parent_line_item->children() as $child) {
1494
-            if ($child->get($search_field) == $value) {
1495
-                return $child;
1496
-            }
1497
-        }
1498
-        foreach ($parent_line_item->children() as $child) {
1499
-            $descendant_found = self::_get_nearest_descendant(
1500
-                $child,
1501
-                $search_field,
1502
-                $value
1503
-            );
1504
-            if ($descendant_found) {
1505
-                return $descendant_found;
1506
-            }
1507
-        }
1508
-        return null;
1509
-    }
1510
-
1511
-
1512
-    /**
1513
-     * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction,
1514
-     * else recursively walks up the line item tree until a parent of type total is found,
1515
-     *
1516
-     * @param EE_Line_Item $line_item
1517
-     * @return EE_Line_Item
1518
-     * @throws EE_Error
1519
-     */
1520
-    public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item)
1521
-    {
1522
-        if ($line_item->TXN_ID()) {
1523
-            $total_line_item = $line_item->transaction()->total_line_item(false);
1524
-            if ($total_line_item instanceof EE_Line_Item) {
1525
-                return $total_line_item;
1526
-            }
1527
-        } else {
1528
-            $line_item_parent = $line_item->parent();
1529
-            if ($line_item_parent instanceof EE_Line_Item) {
1530
-                if ($line_item_parent->is_total()) {
1531
-                    return $line_item_parent;
1532
-                }
1533
-                return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent);
1534
-            }
1535
-        }
1536
-        throw new EE_Error(
1537
-            sprintf(
1538
-                esc_html__(
1539
-                    'A valid grand total for line item %1$d was not found.',
1540
-                    'event_espresso'
1541
-                ),
1542
-                $line_item->ID()
1543
-            )
1544
-        );
1545
-    }
1546
-
1547
-
1548
-    /**
1549
-     * Prints out a representation of the line item tree
1550
-     *
1551
-     * @param EE_Line_Item $line_item
1552
-     * @param int          $indentation
1553
-     * @return void
1554
-     * @throws EE_Error
1555
-     */
1556
-    public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1557
-    {
1558
-        echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559
-        if (! $indentation) {
1560
-            echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1561
-        }
1562
-        for ($i = 0; $i < $indentation; $i++) {
1563
-            echo '. ';
1564
-        }
1565
-        $breakdown = '';
1566
-        if ($line_item->is_line_item()) {
1567
-            if ($line_item->is_percent()) {
1568
-                $breakdown = "{$line_item->percent()}%";
1569
-            } else {
1570
-                $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1571
-            }
1572
-        }
1573
-        echo $line_item->name();
1574
-        echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1575
-        echo '$' . (string) $line_item->total();
1576
-        if ($breakdown) {
1577
-            echo " ( {$breakdown} )";
1578
-        }
1579
-        if ($line_item->is_taxable()) {
1580
-            echo '  * taxable';
1581
-        }
1582
-        if ($line_item->children()) {
1583
-            foreach ($line_item->children() as $child) {
1584
-                self::visualize($child, $indentation + 1);
1585
-            }
1586
-        }
1587
-    }
1588
-
1589
-
1590
-    /**
1591
-     * Calculates the registration's final price, taking into account that they
1592
-     * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes,
1593
-     * and receive a portion of any transaction-wide discounts.
1594
-     * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount
1595
-     * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get
1596
-     * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50
1597
-     * and brent's final price should be $5.50.
1598
-     * In order to do this, we basically need to traverse the line item tree calculating
1599
-     * the running totals (just as if we were recalculating the total), but when we identify
1600
-     * regular line items, we need to keep track of their share of the grand total.
1601
-     * Also, we need to keep track of the TAXABLE total for each ticket purchase, so
1602
-     * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total"
1603
-     * when there are non-taxable items; otherwise they would be the same)
1604
-     *
1605
-     * @param EE_Line_Item $line_item
1606
-     * @param array        $billable_ticket_quantities  array of EE_Ticket IDs and their corresponding quantity that
1607
-     *                                                  can be included in price calculations at this moment
1608
-     * @return array        keys are line items for tickets IDs and values are their share of the running total,
1609
-     *                                                  plus the key 'total', and 'taxable' which also has keys of all
1610
-     *                                                  the ticket IDs.
1611
-     *                                                  Eg array(
1612
-     *                                                      12 => 4.3
1613
-     *                                                      23 => 8.0
1614
-     *                                                      'total' => 16.6,
1615
-     *                                                      'taxable' => array(
1616
-     *                                                          12 => 10,
1617
-     *                                                          23 => 4
1618
-     *                                                      ).
1619
-     *                                                  So to find which registrations have which final price, we need
1620
-     *                                                  to find which line item is theirs, which can be done with
1621
-     *                                                  `EEM_Line_Item::instance()->get_line_item_for_registration(
1622
-     *                                                  $registration );`
1623
-     * @throws EE_Error
1624
-     * @throws InvalidArgumentException
1625
-     * @throws InvalidDataTypeException
1626
-     * @throws InvalidInterfaceException
1627
-     * @throws ReflectionException
1628
-     */
1629
-    public static function calculate_reg_final_prices_per_line_item(
1630
-        EE_Line_Item $line_item,
1631
-        $billable_ticket_quantities = array()
1632
-    ) {
1633
-        $running_totals = [
1634
-            'total'   => 0,
1635
-            'taxable' => ['total' => 0]
1636
-        ];
1637
-        foreach ($line_item->children() as $child_line_item) {
1638
-            switch ($child_line_item->type()) {
1639
-                case EEM_Line_Item::type_sub_total:
1640
-                    $running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1641
-                        $child_line_item,
1642
-                        $billable_ticket_quantities
1643
-                    );
1644
-                    // combine arrays but preserve numeric keys
1645
-                    $running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals);
1646
-                    $running_totals['total'] += $running_totals_from_subtotal['total'];
1647
-                    $running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total'];
1648
-                    break;
1649
-
1650
-                case EEM_Line_Item::type_tax_sub_total:
1651
-                    // find how much the taxes percentage is
1652
-                    if ($child_line_item->percent() !== 0) {
1653
-                        $tax_percent_decimal = $child_line_item->percent() / 100;
1654
-                    } else {
1655
-                        $tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100;
1656
-                    }
1657
-                    // and apply to all the taxable totals, and add to the pretax totals
1658
-                    foreach ($running_totals as $line_item_id => $this_running_total) {
1659
-                        // "total" and "taxable" array key is an exception
1660
-                        if ($line_item_id === 'taxable') {
1661
-                            continue;
1662
-                        }
1663
-                        $taxable_total = $running_totals['taxable'][ $line_item_id ];
1664
-                        $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1665
-                    }
1666
-                    break;
1667
-
1668
-                case EEM_Line_Item::type_line_item:
1669
-                    // ticket line items or ????
1670
-                    if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1671
-                        // kk it's a ticket
1672
-                        if (isset($running_totals[ $child_line_item->ID() ])) {
1673
-                            // huh? that shouldn't happen.
1674
-                            $running_totals['total'] += $child_line_item->total();
1675
-                        } else {
1676
-                            // its not in our running totals yet. great.
1677
-                            if ($child_line_item->is_taxable()) {
1678
-                                $taxable_amount = $child_line_item->unit_price();
1679
-                            } else {
1680
-                                $taxable_amount = 0;
1681
-                            }
1682
-                            // are we only calculating totals for some tickets?
1683
-                            if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1684
-                                $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1685
-                                $running_totals[ $child_line_item->ID() ] = $quantity
1686
-                                    ? $child_line_item->unit_price()
1687
-                                    : 0;
1688
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1689
-                                    ? $taxable_amount
1690
-                                    : 0;
1691
-                            } else {
1692
-                                $quantity = $child_line_item->quantity();
1693
-                                $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1694
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1695
-                            }
1696
-                            $running_totals['taxable']['total'] += $taxable_amount * $quantity;
1697
-                            $running_totals['total'] += $child_line_item->unit_price() * $quantity;
1698
-                        }
1699
-                    } else {
1700
-                        // it's some other type of item added to the cart
1701
-                        // it should affect the running totals
1702
-                        // basically we want to convert it into a PERCENT modifier. Because
1703
-                        // more clearly affect all registration's final price equally
1704
-                        $line_items_percent_of_running_total = $running_totals['total'] > 0
1705
-                            ? ($child_line_item->total() / $running_totals['total']) + 1
1706
-                            : 1;
1707
-                        foreach ($running_totals as $line_item_id => $this_running_total) {
1708
-                            // the "taxable" array key is an exception
1709
-                            if ($line_item_id === 'taxable') {
1710
-                                continue;
1711
-                            }
1712
-                            // update the running totals
1713
-                            // yes this actually even works for the running grand total!
1714
-                            $running_totals[ $line_item_id ] =
1715
-                                $line_items_percent_of_running_total * $this_running_total;
1716
-
1717
-                            if ($child_line_item->is_taxable()) {
1718
-                                $running_totals['taxable'][ $line_item_id ] =
1719
-                                    $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1720
-                            }
1721
-                        }
1722
-                    }
1723
-                    break;
1724
-            }
1725
-        }
1726
-        return $running_totals;
1727
-    }
1728
-
1729
-
1730
-    /**
1731
-     * @param EE_Line_Item $total_line_item
1732
-     * @param EE_Line_Item $ticket_line_item
1733
-     * @return float | null
1734
-     * @throws EE_Error
1735
-     * @throws InvalidArgumentException
1736
-     * @throws InvalidDataTypeException
1737
-     * @throws InvalidInterfaceException
1738
-     * @throws OutOfRangeException
1739
-     * @throws ReflectionException
1740
-     */
1741
-    public static function calculate_final_price_for_ticket_line_item(
1742
-        EE_Line_Item $total_line_item,
1743
-        EE_Line_Item $ticket_line_item
1744
-    ) {
1745
-        static $final_prices_per_ticket_line_item = array();
1746
-        if (empty($final_prices_per_ticket_line_item)) {
1747
-            $final_prices_per_ticket_line_item = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1748
-                $total_line_item
1749
-            );
1750
-        }
1751
-        // ok now find this new registration's final price
1752
-        if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) {
1753
-            return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ];
1754
-        }
1755
-        $message = sprintf(
1756
-            esc_html__(
1757
-                'The final price for the ticket line item (ID:%1$d) could not be calculated.',
1758
-                'event_espresso'
1759
-            ),
1760
-            $ticket_line_item->ID()
1761
-        );
1762
-        if (WP_DEBUG) {
1763
-            $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1764
-            throw new OutOfRangeException($message);
1765
-        }
1766
-        EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
1767
-        return null;
1768
-    }
1769
-
1770
-
1771
-    /**
1772
-     * Creates a duplicate of the line item tree, except only includes billable items
1773
-     * and the portion of line items attributed to billable things
1774
-     *
1775
-     * @param EE_Line_Item      $line_item
1776
-     * @param EE_Registration[] $registrations
1777
-     * @return EE_Line_Item
1778
-     * @throws EE_Error
1779
-     * @throws InvalidArgumentException
1780
-     * @throws InvalidDataTypeException
1781
-     * @throws InvalidInterfaceException
1782
-     * @throws ReflectionException
1783
-     */
1784
-    public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations)
1785
-    {
1786
-        $copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations);
1787
-        foreach ($line_item->children() as $child_li) {
1788
-            $copy_li->add_child_line_item(
1789
-                EEH_Line_Item::billable_line_item_tree($child_li, $registrations)
1790
-            );
1791
-        }
1792
-        // if this is the grand total line item, make sure the totals all add up
1793
-        // (we could have duplicated this logic AS we copied the line items, but
1794
-        // it seems DRYer this way)
1795
-        if ($copy_li->type() === EEM_Line_Item::type_total) {
1796
-            $copy_li->recalculate_total_including_taxes();
1797
-        }
1798
-        return $copy_li;
1799
-    }
1800
-
1801
-
1802
-    /**
1803
-     * Creates a new, unsaved line item from $line_item that factors in the
1804
-     * number of billable registrations on $registrations.
1805
-     *
1806
-     * @param EE_Line_Item      $line_item
1807
-     * @param EE_Registration[] $registrations
1808
-     * @return EE_Line_Item
1809
-     * @throws EE_Error
1810
-     * @throws InvalidArgumentException
1811
-     * @throws InvalidDataTypeException
1812
-     * @throws InvalidInterfaceException
1813
-     * @throws ReflectionException
1814
-     */
1815
-    public static function billable_line_item(EE_Line_Item $line_item, $registrations)
1816
-    {
1817
-        $new_li_fields = $line_item->model_field_array();
1818
-        if ($line_item->type() === EEM_Line_Item::type_line_item &&
1819
-            $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1820
-        ) {
1821
-            $count = 0;
1822
-            foreach ($registrations as $registration) {
1823
-                if ($line_item->OBJ_ID() === $registration->ticket_ID() &&
1824
-                    in_array(
1825
-                        $registration->status_ID(),
1826
-                        EEM_Registration::reg_statuses_that_allow_payment(),
1827
-                        true
1828
-                    )
1829
-                ) {
1830
-                    $count++;
1831
-                }
1832
-            }
1833
-            $new_li_fields['LIN_quantity'] = $count;
1834
-        }
1835
-        // don't set the total. We'll leave that up to the code that calculates it
1836
-        unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']);
1837
-        return EE_Line_Item::new_instance($new_li_fields);
1838
-    }
1839
-
1840
-
1841
-    /**
1842
-     * Returns a modified line item tree where all the subtotals which have a total of 0
1843
-     * are removed, and line items with a quantity of 0
1844
-     *
1845
-     * @param EE_Line_Item $line_item |null
1846
-     * @return EE_Line_Item|null
1847
-     * @throws EE_Error
1848
-     * @throws InvalidArgumentException
1849
-     * @throws InvalidDataTypeException
1850
-     * @throws InvalidInterfaceException
1851
-     * @throws ReflectionException
1852
-     */
1853
-    public static function non_empty_line_items(EE_Line_Item $line_item)
1854
-    {
1855
-        $copied_li = EEH_Line_Item::non_empty_line_item($line_item);
1856
-        if ($copied_li === null) {
1857
-            return null;
1858
-        }
1859
-        // if this is an event subtotal, we want to only include it if it
1860
-        // has a non-zero total and at least one ticket line item child
1861
-        $ticket_children = 0;
1862
-        foreach ($line_item->children() as $child_li) {
1863
-            $child_li_copy = EEH_Line_Item::non_empty_line_items($child_li);
1864
-            if ($child_li_copy !== null) {
1865
-                $copied_li->add_child_line_item($child_li_copy);
1866
-                if ($child_li_copy->type() === EEM_Line_Item::type_line_item &&
1867
-                    $child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1868
-                ) {
1869
-                    $ticket_children++;
1870
-                }
1871
-            }
1872
-        }
1873
-        // if this is an event subtotal with NO ticket children
1874
-        // we basically want to ignore it
1875
-        if ($ticket_children === 0
1876
-            && $line_item->type() === EEM_Line_Item::type_sub_total
1877
-            && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
1878
-            && $line_item->total() === 0
1879
-        ) {
1880
-            return null;
1881
-        }
1882
-        return $copied_li;
1883
-    }
1884
-
1885
-
1886
-    /**
1887
-     * Creates a new, unsaved line item, but if it's a ticket line item
1888
-     * with a total of 0, or a subtotal of 0, returns null instead
1889
-     *
1890
-     * @param EE_Line_Item $line_item
1891
-     * @return EE_Line_Item
1892
-     * @throws EE_Error
1893
-     * @throws InvalidArgumentException
1894
-     * @throws InvalidDataTypeException
1895
-     * @throws InvalidInterfaceException
1896
-     * @throws ReflectionException
1897
-     */
1898
-    public static function non_empty_line_item(EE_Line_Item $line_item)
1899
-    {
1900
-        if ($line_item->type() === EEM_Line_Item::type_line_item
1901
-            && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1902
-            && $line_item->quantity() === 0
1903
-        ) {
1904
-            return null;
1905
-        }
1906
-        $new_li_fields = $line_item->model_field_array();
1907
-        // don't set the total. We'll leave that up to the code that calculates it
1908
-        unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']);
1909
-        return EE_Line_Item::new_instance($new_li_fields);
1910
-    }
1911
-
1912
-
1913
-    /**
1914
-     * Cycles through all of the ticket line items for the supplied total line item
1915
-     * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket
1916
-     *
1917
-     * @param EE_Line_Item $total_line_item
1918
-     * @since 4.9.79.p
1919
-     * @throws EE_Error
1920
-     * @throws InvalidArgumentException
1921
-     * @throws InvalidDataTypeException
1922
-     * @throws InvalidInterfaceException
1923
-     * @throws ReflectionException
1924
-     */
1925
-    public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item)
1926
-    {
1927
-        $ticket_line_items = self::get_ticket_line_items($total_line_item);
1928
-        foreach ($ticket_line_items as $ticket_line_item) {
1929
-            if ($ticket_line_item instanceof EE_Line_Item
1930
-                && $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1931
-            ) {
1932
-                $ticket = $ticket_line_item->ticket();
1933
-                if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) {
1934
-                    $ticket_line_item->set_is_taxable($ticket->taxable());
1935
-                    $ticket_line_item->save();
1936
-                }
1937
-            }
1938
-        }
1939
-    }
1940
-
1941
-
1942
-
1943
-    /**************************************** @DEPRECATED METHODS *************************************** */
1944
-    /**
1945
-     * @deprecated
1946
-     * @param EE_Line_Item $total_line_item
1947
-     * @return EE_Line_Item
1948
-     * @throws EE_Error
1949
-     * @throws InvalidArgumentException
1950
-     * @throws InvalidDataTypeException
1951
-     * @throws InvalidInterfaceException
1952
-     * @throws ReflectionException
1953
-     */
1954
-    public static function get_items_subtotal(EE_Line_Item $total_line_item)
1955
-    {
1956
-        EE_Error::doing_it_wrong(
1957
-            'EEH_Line_Item::get_items_subtotal()',
1958
-            sprintf(
1959
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1960
-                'EEH_Line_Item::get_pre_tax_subtotal()'
1961
-            ),
1962
-            '4.6.0'
1963
-        );
1964
-        return self::get_pre_tax_subtotal($total_line_item);
1965
-    }
1966
-
1967
-
1968
-    /**
1969
-     * @deprecated
1970
-     * @param EE_Transaction $transaction
1971
-     * @return EE_Line_Item
1972
-     * @throws EE_Error
1973
-     * @throws InvalidArgumentException
1974
-     * @throws InvalidDataTypeException
1975
-     * @throws InvalidInterfaceException
1976
-     * @throws ReflectionException
1977
-     */
1978
-    public static function create_default_total_line_item($transaction = null)
1979
-    {
1980
-        EE_Error::doing_it_wrong(
1981
-            'EEH_Line_Item::create_default_total_line_item()',
1982
-            sprintf(
1983
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1984
-                'EEH_Line_Item::create_total_line_item()'
1985
-            ),
1986
-            '4.6.0'
1987
-        );
1988
-        return self::create_total_line_item($transaction);
1989
-    }
1990
-
1991
-
1992
-    /**
1993
-     * @deprecated
1994
-     * @param EE_Line_Item   $total_line_item
1995
-     * @param EE_Transaction $transaction
1996
-     * @return EE_Line_Item
1997
-     * @throws EE_Error
1998
-     * @throws InvalidArgumentException
1999
-     * @throws InvalidDataTypeException
2000
-     * @throws InvalidInterfaceException
2001
-     * @throws ReflectionException
2002
-     */
2003
-    public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2004
-    {
2005
-        EE_Error::doing_it_wrong(
2006
-            'EEH_Line_Item::create_default_tickets_subtotal()',
2007
-            sprintf(
2008
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2009
-                'EEH_Line_Item::create_pre_tax_subtotal()'
2010
-            ),
2011
-            '4.6.0'
2012
-        );
2013
-        return self::create_pre_tax_subtotal($total_line_item, $transaction);
2014
-    }
2015
-
2016
-
2017
-    /**
2018
-     * @deprecated
2019
-     * @param EE_Line_Item   $total_line_item
2020
-     * @param EE_Transaction $transaction
2021
-     * @return EE_Line_Item
2022
-     * @throws EE_Error
2023
-     * @throws InvalidArgumentException
2024
-     * @throws InvalidDataTypeException
2025
-     * @throws InvalidInterfaceException
2026
-     * @throws ReflectionException
2027
-     */
2028
-    public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2029
-    {
2030
-        EE_Error::doing_it_wrong(
2031
-            'EEH_Line_Item::create_default_taxes_subtotal()',
2032
-            sprintf(
2033
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2034
-                'EEH_Line_Item::create_taxes_subtotal()'
2035
-            ),
2036
-            '4.6.0'
2037
-        );
2038
-        return self::create_taxes_subtotal($total_line_item, $transaction);
2039
-    }
2040
-
2041
-
2042
-    /**
2043
-     * @deprecated
2044
-     * @param EE_Line_Item   $total_line_item
2045
-     * @param EE_Transaction $transaction
2046
-     * @return EE_Line_Item
2047
-     * @throws EE_Error
2048
-     * @throws InvalidArgumentException
2049
-     * @throws InvalidDataTypeException
2050
-     * @throws InvalidInterfaceException
2051
-     * @throws ReflectionException
2052
-     */
2053
-    public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2054
-    {
2055
-        EE_Error::doing_it_wrong(
2056
-            'EEH_Line_Item::create_default_event_subtotal()',
2057
-            sprintf(
2058
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2059
-                'EEH_Line_Item::create_event_subtotal()'
2060
-            ),
2061
-            '4.6.0'
2062
-        );
2063
-        return self::create_event_subtotal($total_line_item, $transaction);
2064
-    }
24
+	/**
25
+	 * Adds a simple item (unrelated to any other model object) to the provided PARENT line item.
26
+	 * Does NOT automatically re-calculate the line item totals or update the related transaction.
27
+	 * You should call recalculate_total_including_taxes() on the grant total line item after this
28
+	 * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
29
+	 * to keep the registration final prices in-sync with the transaction's total.
30
+	 *
31
+	 * @param EE_Line_Item $parent_line_item
32
+	 * @param string       $name
33
+	 * @param float        $unit_price
34
+	 * @param string       $description
35
+	 * @param int          $quantity
36
+	 * @param boolean      $taxable
37
+	 * @param boolean      $code if set to a value, ensures there is only one line item with that code
38
+	 * @return boolean success
39
+	 * @throws EE_Error
40
+	 * @throws InvalidArgumentException
41
+	 * @throws InvalidDataTypeException
42
+	 * @throws InvalidInterfaceException
43
+	 * @throws ReflectionException
44
+	 */
45
+	public static function add_unrelated_item(
46
+		EE_Line_Item $parent_line_item,
47
+		$name,
48
+		$unit_price,
49
+		$description = '',
50
+		$quantity = 1,
51
+		$taxable = false,
52
+		$code = null
53
+	) {
54
+		$items_subtotal = self::get_pre_tax_subtotal($parent_line_item);
55
+		$line_item = EE_Line_Item::new_instance(array(
56
+			'LIN_name'       => $name,
57
+			'LIN_desc'       => $description,
58
+			'LIN_unit_price' => $unit_price,
59
+			'LIN_quantity'   => $quantity,
60
+			'LIN_percent'    => null,
61
+			'LIN_is_taxable' => $taxable,
62
+			'LIN_order'      => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0,
63
+			'LIN_total'      => (float) $unit_price * (int) $quantity,
64
+			'LIN_type'       => EEM_Line_Item::type_line_item,
65
+			'LIN_code'       => $code,
66
+		));
67
+		$line_item = apply_filters(
68
+			'FHEE__EEH_Line_Item__add_unrelated_item__line_item',
69
+			$line_item,
70
+			$parent_line_item
71
+		);
72
+		return self::add_item($parent_line_item, $line_item);
73
+	}
74
+
75
+
76
+	/**
77
+	 * Adds a simple item ( unrelated to any other model object) to the total line item,
78
+	 * in the correct spot in the line item tree. Does not automatically
79
+	 * re-calculate the line item totals, nor update the related transaction, nor upgrade the transaction's
80
+	 * registrations' final prices (which should probably change because of this).
81
+	 * You should call recalculate_total_including_taxes() on the grand total line item, then
82
+	 * update the transaction's total, and EE_Registration_Processor::update_registration_final_prices()
83
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
84
+	 *
85
+	 * @param EE_Line_Item $parent_line_item
86
+	 * @param string       $name
87
+	 * @param float        $percentage_amount
88
+	 * @param string       $description
89
+	 * @param boolean      $taxable
90
+	 * @return boolean success
91
+	 * @throws EE_Error
92
+	 */
93
+	public static function add_percentage_based_item(
94
+		EE_Line_Item $parent_line_item,
95
+		$name,
96
+		$percentage_amount,
97
+		$description = '',
98
+		$taxable = false
99
+	) {
100
+		$line_item = EE_Line_Item::new_instance(array(
101
+			'LIN_name'       => $name,
102
+			'LIN_desc'       => $description,
103
+			'LIN_unit_price' => 0,
104
+			'LIN_percent'    => $percentage_amount,
105
+			'LIN_quantity'   => 1,
106
+			'LIN_is_taxable' => $taxable,
107
+			'LIN_total'      => (float) ($percentage_amount * ($parent_line_item->total() / 100)),
108
+			'LIN_type'       => EEM_Line_Item::type_line_item,
109
+			'LIN_parent'     => $parent_line_item->ID(),
110
+		));
111
+		$line_item = apply_filters(
112
+			'FHEE__EEH_Line_Item__add_percentage_based_item__line_item',
113
+			$line_item
114
+		);
115
+		return $parent_line_item->add_child_line_item($line_item, false);
116
+	}
117
+
118
+
119
+	/**
120
+	 * Returns the new line item created by adding a purchase of the ticket
121
+	 * ensures that ticket line item is saved, and that cart total has been recalculated.
122
+	 * If this ticket has already been purchased, just increments its count.
123
+	 * Automatically re-calculates the line item totals and updates the related transaction. But
124
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
125
+	 * should probably change because of this).
126
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
127
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
128
+	 *
129
+	 * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total
130
+	 * @param EE_Ticket    $ticket
131
+	 * @param int          $qty
132
+	 * @return EE_Line_Item
133
+	 * @throws EE_Error
134
+	 * @throws InvalidArgumentException
135
+	 * @throws InvalidDataTypeException
136
+	 * @throws InvalidInterfaceException
137
+	 * @throws ReflectionException
138
+	 */
139
+	public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140
+	{
141
+		if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142
+			throw new EE_Error(
143
+				sprintf(
144
+					esc_html__(
145
+						'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.',
146
+						'event_espresso'
147
+					),
148
+					$ticket->ID(),
149
+					$total_line_item->ID()
150
+				)
151
+			);
152
+		}
153
+		// either increment the qty for an existing ticket
154
+		$line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155
+		// or add a new one
156
+		if (! $line_item instanceof EE_Line_Item) {
157
+			$line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158
+		}
159
+		$total_line_item->recalculate_total_including_taxes();
160
+		return $line_item;
161
+	}
162
+
163
+
164
+	/**
165
+	 * Returns the new line item created by adding a purchase of the ticket
166
+	 *
167
+	 * @param EE_Line_Item $total_line_item
168
+	 * @param EE_Ticket    $ticket
169
+	 * @param int          $qty
170
+	 * @return EE_Line_Item
171
+	 * @throws EE_Error
172
+	 * @throws InvalidArgumentException
173
+	 * @throws InvalidDataTypeException
174
+	 * @throws InvalidInterfaceException
175
+	 * @throws ReflectionException
176
+	 */
177
+	public static function increment_ticket_qty_if_already_in_cart(
178
+		EE_Line_Item $total_line_item,
179
+		EE_Ticket $ticket,
180
+		$qty = 1
181
+	) {
182
+		$line_item = null;
183
+		if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) {
184
+			$ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
185
+			foreach ((array) $ticket_line_items as $ticket_line_item) {
186
+				if ($ticket_line_item instanceof EE_Line_Item
187
+					&& (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID()
188
+				) {
189
+					$line_item = $ticket_line_item;
190
+					break;
191
+				}
192
+			}
193
+		}
194
+		if ($line_item instanceof EE_Line_Item) {
195
+			EEH_Line_Item::increment_quantity($line_item, $qty);
196
+			return $line_item;
197
+		}
198
+		return null;
199
+	}
200
+
201
+
202
+	/**
203
+	 * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected).
204
+	 * Does NOT save or recalculate other line items totals
205
+	 *
206
+	 * @param EE_Line_Item $line_item
207
+	 * @param int          $qty
208
+	 * @return void
209
+	 * @throws EE_Error
210
+	 * @throws InvalidArgumentException
211
+	 * @throws InvalidDataTypeException
212
+	 * @throws InvalidInterfaceException
213
+	 * @throws ReflectionException
214
+	 */
215
+	public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216
+	{
217
+		if (! $line_item->is_percent()) {
218
+			$qty += $line_item->quantity();
219
+			$line_item->set_quantity($qty);
220
+			$line_item->set_total($line_item->unit_price() * $qty);
221
+			$line_item->save();
222
+		}
223
+		foreach ($line_item->children() as $child) {
224
+			if ($child->is_sub_line_item()) {
225
+				EEH_Line_Item::update_quantity($child, $qty);
226
+			}
227
+		}
228
+	}
229
+
230
+
231
+	/**
232
+	 * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected).
233
+	 * Does NOT save or recalculate other line items totals
234
+	 *
235
+	 * @param EE_Line_Item $line_item
236
+	 * @param int          $qty
237
+	 * @return void
238
+	 * @throws EE_Error
239
+	 * @throws InvalidArgumentException
240
+	 * @throws InvalidDataTypeException
241
+	 * @throws InvalidInterfaceException
242
+	 * @throws ReflectionException
243
+	 */
244
+	public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245
+	{
246
+		if (! $line_item->is_percent()) {
247
+			$qty = $line_item->quantity() - $qty;
248
+			$qty = max($qty, 0);
249
+			$line_item->set_quantity($qty);
250
+			$line_item->set_total($line_item->unit_price() * $qty);
251
+			$line_item->save();
252
+		}
253
+		foreach ($line_item->children() as $child) {
254
+			if ($child->is_sub_line_item()) {
255
+				EEH_Line_Item::update_quantity($child, $qty);
256
+			}
257
+		}
258
+	}
259
+
260
+
261
+	/**
262
+	 * Updates the line item and its children's quantities to the specified number.
263
+	 * Does NOT save them or recalculate totals.
264
+	 *
265
+	 * @param EE_Line_Item $line_item
266
+	 * @param int          $new_quantity
267
+	 * @throws EE_Error
268
+	 * @throws InvalidArgumentException
269
+	 * @throws InvalidDataTypeException
270
+	 * @throws InvalidInterfaceException
271
+	 * @throws ReflectionException
272
+	 */
273
+	public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274
+	{
275
+		if (! $line_item->is_percent()) {
276
+			$line_item->set_quantity($new_quantity);
277
+			$line_item->set_total($line_item->unit_price() * $new_quantity);
278
+			$line_item->save();
279
+		}
280
+		foreach ($line_item->children() as $child) {
281
+			if ($child->is_sub_line_item()) {
282
+				EEH_Line_Item::update_quantity($child, $new_quantity);
283
+			}
284
+		}
285
+	}
286
+
287
+
288
+	/**
289
+	 * Returns the new line item created by adding a purchase of the ticket
290
+	 *
291
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
292
+	 * @param EE_Ticket    $ticket
293
+	 * @param int          $qty
294
+	 * @return EE_Line_Item
295
+	 * @throws EE_Error
296
+	 * @throws InvalidArgumentException
297
+	 * @throws InvalidDataTypeException
298
+	 * @throws InvalidInterfaceException
299
+	 * @throws ReflectionException
300
+	 */
301
+	public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
302
+	{
303
+		$datetimes = $ticket->datetimes();
304
+		$first_datetime = reset($datetimes);
305
+		$first_datetime_name = esc_html__('Event', 'event_espresso');
306
+		if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) {
307
+			$first_datetime_name = $first_datetime->event()->name();
308
+		}
309
+		$event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name);
310
+		// get event subtotal line
311
+		$events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket);
312
+		// add $ticket to cart
313
+		$line_item = EE_Line_Item::new_instance(array(
314
+			'LIN_name'       => $ticket->name(),
315
+			'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
316
+			'LIN_unit_price' => $ticket->price(),
317
+			'LIN_quantity'   => $qty,
318
+			'LIN_is_taxable' => $ticket->taxable(),
319
+			'LIN_order'      => count($events_sub_total->children()),
320
+			'LIN_total'      => $ticket->price() * $qty,
321
+			'LIN_type'       => EEM_Line_Item::type_line_item,
322
+			'OBJ_ID'         => $ticket->ID(),
323
+			'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_TICKET,
324
+		));
325
+		$line_item = apply_filters(
326
+			'FHEE__EEH_Line_Item__create_ticket_line_item__line_item',
327
+			$line_item
328
+		);
329
+		$events_sub_total->add_child_line_item($line_item);
330
+		// now add the sub-line items
331
+		$running_total_for_ticket = 0;
332
+		foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
333
+			$sign = $price->is_discount() ? -1 : 1;
334
+			$price_total = $price->is_percent()
335
+				? $running_total_for_ticket * $price->amount() / 100
336
+				: $price->amount() * $qty;
337
+			$sub_line_item = EE_Line_Item::new_instance(array(
338
+				'LIN_name'       => $price->name(),
339
+				'LIN_desc'       => $price->desc(),
340
+				'LIN_quantity'   => $price->is_percent() ? null : $qty,
341
+				'LIN_is_taxable' => false,
342
+				'LIN_order'      => $price->order(),
343
+				'LIN_total'      => $sign * $price_total,
344
+				'LIN_type'       => EEM_Line_Item::type_sub_line_item,
345
+				'OBJ_ID'         => $price->ID(),
346
+				'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
347
+			));
348
+			$sub_line_item = apply_filters(
349
+				'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item',
350
+				$sub_line_item
351
+			);
352
+			if ($price->is_percent()) {
353
+				$sub_line_item->set_percent($sign * $price->amount());
354
+			} else {
355
+				$sub_line_item->set_unit_price($sign * $price->amount());
356
+			}
357
+			$running_total_for_ticket += $price_total;
358
+			$line_item->add_child_line_item($sub_line_item);
359
+		}
360
+		return $line_item;
361
+	}
362
+
363
+
364
+	/**
365
+	 * Adds the specified item under the pre-tax-sub-total line item. Automatically
366
+	 * re-calculates the line item totals and updates the related transaction. But
367
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
368
+	 * should probably change because of this).
369
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
370
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
371
+	 *
372
+	 * @param EE_Line_Item $total_line_item
373
+	 * @param EE_Line_Item $item to be added
374
+	 * @return boolean
375
+	 * @throws EE_Error
376
+	 * @throws InvalidArgumentException
377
+	 * @throws InvalidDataTypeException
378
+	 * @throws InvalidInterfaceException
379
+	 * @throws ReflectionException
380
+	 */
381
+	public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item)
382
+	{
383
+		$pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item);
384
+		if ($pre_tax_subtotal instanceof EE_Line_Item) {
385
+			$success = $pre_tax_subtotal->add_child_line_item($item);
386
+		} else {
387
+			return false;
388
+		}
389
+		$total_line_item->recalculate_total_including_taxes();
390
+		return $success;
391
+	}
392
+
393
+
394
+	/**
395
+	 * cancels an existing ticket line item,
396
+	 * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item.
397
+	 * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
398
+	 *
399
+	 * @param EE_Line_Item $ticket_line_item
400
+	 * @param int          $qty
401
+	 * @return bool success
402
+	 * @throws EE_Error
403
+	 * @throws InvalidArgumentException
404
+	 * @throws InvalidDataTypeException
405
+	 * @throws InvalidInterfaceException
406
+	 * @throws ReflectionException
407
+	 */
408
+	public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
409
+	{
410
+		// validate incoming line_item
411
+		if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
412
+			throw new EE_Error(
413
+				sprintf(
414
+					esc_html__(
415
+						'The supplied line item must have an Object Type of "Ticket", not %1$s.',
416
+						'event_espresso'
417
+					),
418
+					$ticket_line_item->type()
419
+				)
420
+			);
421
+		}
422
+		if ($ticket_line_item->quantity() < $qty) {
423
+			throw new EE_Error(
424
+				sprintf(
425
+					esc_html__(
426
+						'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.',
427
+						'event_espresso'
428
+					),
429
+					$qty,
430
+					$ticket_line_item->quantity()
431
+				)
432
+			);
433
+		}
434
+		// decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this
435
+		$ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty);
436
+		foreach ($ticket_line_item->children() as $child_line_item) {
437
+			if ($child_line_item->is_sub_line_item()
438
+				&& ! $child_line_item->is_percent()
439
+				&& ! $child_line_item->is_cancellation()
440
+			) {
441
+				$child_line_item->set_quantity($child_line_item->quantity() - $qty);
442
+			}
443
+		}
444
+		// get cancellation sub line item
445
+		$cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
446
+			$ticket_line_item,
447
+			EEM_Line_Item::type_cancellation
448
+		);
449
+		$cancellation_line_item = reset($cancellation_line_item);
450
+		// verify that this ticket was indeed previously cancelled
451
+		if ($cancellation_line_item instanceof EE_Line_Item) {
452
+			// increment cancelled quantity
453
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty);
454
+		} else {
455
+			// create cancellation sub line item
456
+			$cancellation_line_item = EE_Line_Item::new_instance(array(
457
+				'LIN_name'       => esc_html__('Cancellation', 'event_espresso'),
458
+				'LIN_desc'       => sprintf(
459
+					esc_html_x(
460
+						'Cancelled %1$s : %2$s',
461
+						'Cancelled Ticket Name : 2015-01-01 11:11',
462
+						'event_espresso'
463
+					),
464
+					$ticket_line_item->name(),
465
+					current_time(get_option('date_format') . ' ' . get_option('time_format'))
466
+				),
467
+				'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468
+				'LIN_quantity'   => $qty,
469
+				'LIN_is_taxable' => $ticket_line_item->is_taxable(),
470
+				'LIN_order'      => count($ticket_line_item->children()),
471
+				'LIN_total'      => 0, // $ticket_line_item->unit_price()
472
+				'LIN_type'       => EEM_Line_Item::type_cancellation,
473
+			));
474
+			$ticket_line_item->add_child_line_item($cancellation_line_item);
475
+		}
476
+		if ($ticket_line_item->save_this_and_descendants() > 0) {
477
+			// decrement parent line item quantity
478
+			$event_line_item = $ticket_line_item->parent();
479
+			if ($event_line_item instanceof EE_Line_Item
480
+				&& $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
481
+			) {
482
+				$event_line_item->set_quantity($event_line_item->quantity() - $qty);
483
+				$event_line_item->save();
484
+			}
485
+			EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
486
+			return true;
487
+		}
488
+		return false;
489
+	}
490
+
491
+
492
+	/**
493
+	 * reinstates (un-cancels?) a previously canceled ticket line item,
494
+	 * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item.
495
+	 * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
496
+	 *
497
+	 * @param EE_Line_Item $ticket_line_item
498
+	 * @param int          $qty
499
+	 * @return bool success
500
+	 * @throws EE_Error
501
+	 * @throws InvalidArgumentException
502
+	 * @throws InvalidDataTypeException
503
+	 * @throws InvalidInterfaceException
504
+	 * @throws ReflectionException
505
+	 */
506
+	public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
507
+	{
508
+		// validate incoming line_item
509
+		if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
510
+			throw new EE_Error(
511
+				sprintf(
512
+					esc_html__(
513
+						'The supplied line item must have an Object Type of "Ticket", not %1$s.',
514
+						'event_espresso'
515
+					),
516
+					$ticket_line_item->type()
517
+				)
518
+			);
519
+		}
520
+		// get cancellation sub line item
521
+		$cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
522
+			$ticket_line_item,
523
+			EEM_Line_Item::type_cancellation
524
+		);
525
+		$cancellation_line_item = reset($cancellation_line_item);
526
+		// verify that this ticket was indeed previously cancelled
527
+		if (! $cancellation_line_item instanceof EE_Line_Item) {
528
+			return false;
529
+		}
530
+		if ($cancellation_line_item->quantity() > $qty) {
531
+			// decrement cancelled quantity
532
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
533
+		} elseif ($cancellation_line_item->quantity() === $qty) {
534
+			// decrement cancelled quantity in case anyone still has the object kicking around
535
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
536
+			// delete because quantity will end up as 0
537
+			$cancellation_line_item->delete();
538
+			// and attempt to destroy the object,
539
+			// even though PHP won't actually destroy it until it needs the memory
540
+			unset($cancellation_line_item);
541
+		} else {
542
+			// what ?!?! negative quantity ?!?!
543
+			throw new EE_Error(
544
+				sprintf(
545
+					esc_html__(
546
+						'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.',
547
+						'event_espresso'
548
+					),
549
+					$qty,
550
+					$cancellation_line_item->quantity()
551
+				)
552
+			);
553
+		}
554
+		// increment ticket quantity
555
+		$ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty);
556
+		if ($ticket_line_item->save_this_and_descendants() > 0) {
557
+			// increment parent line item quantity
558
+			$event_line_item = $ticket_line_item->parent();
559
+			if ($event_line_item instanceof EE_Line_Item
560
+				&& $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
561
+			) {
562
+				$event_line_item->set_quantity($event_line_item->quantity() + $qty);
563
+			}
564
+			EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
565
+			return true;
566
+		}
567
+		return false;
568
+	}
569
+
570
+
571
+	/**
572
+	 * calls EEH_Line_Item::find_transaction_grand_total_for_line_item()
573
+	 * then EE_Line_Item::recalculate_total_including_taxes() on the result
574
+	 *
575
+	 * @param EE_Line_Item $line_item
576
+	 * @return float
577
+	 * @throws EE_Error
578
+	 * @throws InvalidArgumentException
579
+	 * @throws InvalidDataTypeException
580
+	 * @throws InvalidInterfaceException
581
+	 * @throws ReflectionException
582
+	 */
583
+	public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item)
584
+	{
585
+		$grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item);
586
+		return $grand_total_line_item->recalculate_total_including_taxes();
587
+	}
588
+
589
+
590
+	/**
591
+	 * Gets the line item which contains the subtotal of all the items
592
+	 *
593
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
594
+	 * @return EE_Line_Item
595
+	 * @throws EE_Error
596
+	 * @throws InvalidArgumentException
597
+	 * @throws InvalidDataTypeException
598
+	 * @throws InvalidInterfaceException
599
+	 * @throws ReflectionException
600
+	 */
601
+	public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item)
602
+	{
603
+		$pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal');
604
+		return $pre_tax_subtotal instanceof EE_Line_Item
605
+			? $pre_tax_subtotal
606
+			: self::create_pre_tax_subtotal($total_line_item);
607
+	}
608
+
609
+
610
+	/**
611
+	 * Gets the line item for the taxes subtotal
612
+	 *
613
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
614
+	 * @return EE_Line_Item
615
+	 * @throws EE_Error
616
+	 * @throws InvalidArgumentException
617
+	 * @throws InvalidDataTypeException
618
+	 * @throws InvalidInterfaceException
619
+	 * @throws ReflectionException
620
+	 */
621
+	public static function get_taxes_subtotal(EE_Line_Item $total_line_item)
622
+	{
623
+		$taxes = $total_line_item->get_child_line_item('taxes');
624
+		return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item);
625
+	}
626
+
627
+
628
+	/**
629
+	 * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object
630
+	 *
631
+	 * @param EE_Line_Item   $line_item
632
+	 * @param EE_Transaction $transaction
633
+	 * @return void
634
+	 * @throws EE_Error
635
+	 * @throws InvalidArgumentException
636
+	 * @throws InvalidDataTypeException
637
+	 * @throws InvalidInterfaceException
638
+	 * @throws ReflectionException
639
+	 */
640
+	public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null)
641
+	{
642
+		if ($transaction) {
643
+			/** @type EEM_Transaction $EEM_Transaction */
644
+			$EEM_Transaction = EE_Registry::instance()->load_model('Transaction');
645
+			$TXN_ID = $EEM_Transaction->ensure_is_ID($transaction);
646
+			$line_item->set_TXN_ID($TXN_ID);
647
+		}
648
+	}
649
+
650
+
651
+	/**
652
+	 * Creates a new default total line item for the transaction,
653
+	 * and its tickets subtotal and taxes subtotal line items (and adds the
654
+	 * existing taxes as children of the taxes subtotal line item)
655
+	 *
656
+	 * @param EE_Transaction $transaction
657
+	 * @return EE_Line_Item of type total
658
+	 * @throws EE_Error
659
+	 * @throws InvalidArgumentException
660
+	 * @throws InvalidDataTypeException
661
+	 * @throws InvalidInterfaceException
662
+	 * @throws ReflectionException
663
+	 */
664
+	public static function create_total_line_item($transaction = null)
665
+	{
666
+		$total_line_item = EE_Line_Item::new_instance(array(
667
+			'LIN_code' => 'total',
668
+			'LIN_name' => esc_html__('Grand Total', 'event_espresso'),
669
+			'LIN_type' => EEM_Line_Item::type_total,
670
+			'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION,
671
+		));
672
+		$total_line_item = apply_filters(
673
+			'FHEE__EEH_Line_Item__create_total_line_item__total_line_item',
674
+			$total_line_item
675
+		);
676
+		self::set_TXN_ID($total_line_item, $transaction);
677
+		self::create_pre_tax_subtotal($total_line_item, $transaction);
678
+		self::create_taxes_subtotal($total_line_item, $transaction);
679
+		return $total_line_item;
680
+	}
681
+
682
+
683
+	/**
684
+	 * Creates a default items subtotal line item
685
+	 *
686
+	 * @param EE_Line_Item   $total_line_item
687
+	 * @param EE_Transaction $transaction
688
+	 * @return EE_Line_Item
689
+	 * @throws EE_Error
690
+	 * @throws InvalidArgumentException
691
+	 * @throws InvalidDataTypeException
692
+	 * @throws InvalidInterfaceException
693
+	 * @throws ReflectionException
694
+	 */
695
+	protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null)
696
+	{
697
+		$pre_tax_line_item = EE_Line_Item::new_instance(array(
698
+			'LIN_code' => 'pre-tax-subtotal',
699
+			'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'),
700
+			'LIN_type' => EEM_Line_Item::type_sub_total,
701
+		));
702
+		$pre_tax_line_item = apply_filters(
703
+			'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item',
704
+			$pre_tax_line_item
705
+		);
706
+		self::set_TXN_ID($pre_tax_line_item, $transaction);
707
+		$total_line_item->add_child_line_item($pre_tax_line_item);
708
+		self::create_event_subtotal($pre_tax_line_item, $transaction);
709
+		return $pre_tax_line_item;
710
+	}
711
+
712
+
713
+	/**
714
+	 * Creates a line item for the taxes subtotal and finds all the tax prices
715
+	 * and applies taxes to it
716
+	 *
717
+	 * @param EE_Line_Item   $total_line_item of type EEM_Line_Item::type_total
718
+	 * @param EE_Transaction $transaction
719
+	 * @return EE_Line_Item
720
+	 * @throws EE_Error
721
+	 * @throws InvalidArgumentException
722
+	 * @throws InvalidDataTypeException
723
+	 * @throws InvalidInterfaceException
724
+	 * @throws ReflectionException
725
+	 */
726
+	protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
727
+	{
728
+		$tax_line_item = EE_Line_Item::new_instance(array(
729
+			'LIN_code'  => 'taxes',
730
+			'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731
+			'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
+			'LIN_order' => 1000,// this should always come last
733
+		));
734
+		$tax_line_item = apply_filters(
735
+			'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
736
+			$tax_line_item
737
+		);
738
+		self::set_TXN_ID($tax_line_item, $transaction);
739
+		$total_line_item->add_child_line_item($tax_line_item);
740
+		// and lastly, add the actual taxes
741
+		self::apply_taxes($total_line_item);
742
+		return $tax_line_item;
743
+	}
744
+
745
+
746
+	/**
747
+	 * Creates a default items subtotal line item
748
+	 *
749
+	 * @param EE_Line_Item   $pre_tax_line_item
750
+	 * @param EE_Transaction $transaction
751
+	 * @param EE_Event       $event
752
+	 * @return EE_Line_Item
753
+	 * @throws EE_Error
754
+	 * @throws InvalidArgumentException
755
+	 * @throws InvalidDataTypeException
756
+	 * @throws InvalidInterfaceException
757
+	 * @throws ReflectionException
758
+	 */
759
+	public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null)
760
+	{
761
+		$event_line_item = EE_Line_Item::new_instance(array(
762
+			'LIN_code' => self::get_event_code($event),
763
+			'LIN_name' => self::get_event_name($event),
764
+			'LIN_desc' => self::get_event_desc($event),
765
+			'LIN_type' => EEM_Line_Item::type_sub_total,
766
+			'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT,
767
+			'OBJ_ID'   => $event instanceof EE_Event ? $event->ID() : 0,
768
+		));
769
+		$event_line_item = apply_filters(
770
+			'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item',
771
+			$event_line_item
772
+		);
773
+		self::set_TXN_ID($event_line_item, $transaction);
774
+		$pre_tax_line_item->add_child_line_item($event_line_item);
775
+		return $event_line_item;
776
+	}
777
+
778
+
779
+	/**
780
+	 * Gets what the event ticket's code SHOULD be
781
+	 *
782
+	 * @param EE_Event $event
783
+	 * @return string
784
+	 * @throws EE_Error
785
+	 */
786
+	public static function get_event_code($event)
787
+	{
788
+		return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
789
+	}
790
+
791
+
792
+	/**
793
+	 * Gets the event name
794
+	 *
795
+	 * @param EE_Event $event
796
+	 * @return string
797
+	 * @throws EE_Error
798
+	 */
799
+	public static function get_event_name($event)
800
+	{
801
+		return $event instanceof EE_Event
802
+			? mb_substr($event->name(), 0, 245)
803
+			: esc_html__('Event', 'event_espresso');
804
+	}
805
+
806
+
807
+	/**
808
+	 * Gets the event excerpt
809
+	 *
810
+	 * @param EE_Event $event
811
+	 * @return string
812
+	 * @throws EE_Error
813
+	 */
814
+	public static function get_event_desc($event)
815
+	{
816
+		return $event instanceof EE_Event ? $event->short_description() : '';
817
+	}
818
+
819
+
820
+	/**
821
+	 * Given the grand total line item and a ticket, finds the event sub-total
822
+	 * line item the ticket's purchase should be added onto
823
+	 *
824
+	 * @access public
825
+	 * @param EE_Line_Item $grand_total the grand total line item
826
+	 * @param EE_Ticket    $ticket
827
+	 * @return EE_Line_Item
828
+	 * @throws EE_Error
829
+	 * @throws InvalidArgumentException
830
+	 * @throws InvalidDataTypeException
831
+	 * @throws InvalidInterfaceException
832
+	 * @throws ReflectionException
833
+	 */
834
+	public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835
+	{
836
+		$first_datetime = $ticket->first_datetime();
837
+		if (! $first_datetime instanceof EE_Datetime) {
838
+			throw new EE_Error(
839
+				sprintf(
840
+					__('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
841
+					$ticket->ID()
842
+				)
843
+			);
844
+		}
845
+		$event = $first_datetime->event();
846
+		if (! $event instanceof EE_Event) {
847
+			throw new EE_Error(
848
+				sprintf(
849
+					esc_html__(
850
+						'The supplied ticket (ID %d) has no event data associated with it.',
851
+						'event_espresso'
852
+					),
853
+					$ticket->ID()
854
+				)
855
+			);
856
+		}
857
+		$events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
+		if (! $events_sub_total instanceof EE_Line_Item) {
859
+			throw new EE_Error(
860
+				sprintf(
861
+					esc_html__(
862
+						'There is no events sub-total for ticket %s on total line item %d',
863
+						'event_espresso'
864
+					),
865
+					$ticket->ID(),
866
+					$grand_total->ID()
867
+				)
868
+			);
869
+		}
870
+		return $events_sub_total;
871
+	}
872
+
873
+
874
+	/**
875
+	 * Gets the event line item
876
+	 *
877
+	 * @param EE_Line_Item $grand_total
878
+	 * @param EE_Event     $event
879
+	 * @return EE_Line_Item for the event subtotal which is a child of $grand_total
880
+	 * @throws EE_Error
881
+	 * @throws InvalidArgumentException
882
+	 * @throws InvalidDataTypeException
883
+	 * @throws InvalidInterfaceException
884
+	 * @throws ReflectionException
885
+	 */
886
+	public static function get_event_line_item(EE_Line_Item $grand_total, $event)
887
+	{
888
+		/** @type EE_Event $event */
889
+		$event = EEM_Event::instance()->ensure_is_obj($event, true);
890
+		$event_line_item = null;
891
+		$found = false;
892
+		foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893
+			// default event subtotal, we should only ever find this the first time this method is called
894
+			if (! $event_line_item->OBJ_ID()) {
895
+				// let's use this! but first... set the event details
896
+				EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897
+				$found = true;
898
+				break;
899
+			}
900
+			if ($event_line_item->OBJ_ID() === $event->ID()) {
901
+				// found existing line item for this event in the cart, so break out of loop and use this one
902
+				$found = true;
903
+				break;
904
+			}
905
+		}
906
+		if (! $found) {
907
+			// there is no event sub-total yet, so add it
908
+			$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909
+			// create a new "event" subtotal below that
910
+			$event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
911
+			// and set the event details
912
+			EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
913
+		}
914
+		return $event_line_item;
915
+	}
916
+
917
+
918
+	/**
919
+	 * Creates a default items subtotal line item
920
+	 *
921
+	 * @param EE_Line_Item   $event_line_item
922
+	 * @param EE_Event       $event
923
+	 * @param EE_Transaction $transaction
924
+	 * @return void
925
+	 * @throws EE_Error
926
+	 * @throws InvalidArgumentException
927
+	 * @throws InvalidDataTypeException
928
+	 * @throws InvalidInterfaceException
929
+	 * @throws ReflectionException
930
+	 */
931
+	public static function set_event_subtotal_details(
932
+		EE_Line_Item $event_line_item,
933
+		EE_Event $event,
934
+		$transaction = null
935
+	) {
936
+		if ($event instanceof EE_Event) {
937
+			$event_line_item->set_code(self::get_event_code($event));
938
+			$event_line_item->set_name(self::get_event_name($event));
939
+			$event_line_item->set_desc(self::get_event_desc($event));
940
+			$event_line_item->set_OBJ_ID($event->ID());
941
+		}
942
+		self::set_TXN_ID($event_line_item, $transaction);
943
+	}
944
+
945
+
946
+	/**
947
+	 * Finds what taxes should apply, adds them as tax line items under the taxes sub-total,
948
+	 * and recalculates the taxes sub-total and the grand total. Resets the taxes, so
949
+	 * any old taxes are removed
950
+	 *
951
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
952
+	 * @param bool         $update_txn_status
953
+	 * @return bool
954
+	 * @throws EE_Error
955
+	 * @throws InvalidArgumentException
956
+	 * @throws InvalidDataTypeException
957
+	 * @throws InvalidInterfaceException
958
+	 * @throws ReflectionException
959
+	 * @throws RuntimeException
960
+	 */
961
+	public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false)
962
+	{
963
+		/** @type EEM_Price $EEM_Price */
964
+		$EEM_Price = EE_Registry::instance()->load_model('Price');
965
+		// get array of taxes via Price Model
966
+		$ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes();
967
+		ksort($ordered_taxes);
968
+		$taxes_line_item = self::get_taxes_subtotal($total_line_item);
969
+		// just to be safe, remove its old tax line items
970
+		$deleted = $taxes_line_item->delete_children_line_items();
971
+		$updates = false;
972
+		// loop thru taxes
973
+		foreach ($ordered_taxes as $order => $taxes) {
974
+			foreach ($taxes as $tax) {
975
+				if ($tax instanceof EE_Price) {
976
+					$tax_line_item = EE_Line_Item::new_instance(
977
+						array(
978
+							'LIN_name'       => $tax->name(),
979
+							'LIN_desc'       => $tax->desc(),
980
+							'LIN_percent'    => $tax->amount(),
981
+							'LIN_is_taxable' => false,
982
+							'LIN_order'      => $order,
983
+							'LIN_total'      => 0,
984
+							'LIN_type'       => EEM_Line_Item::type_tax,
985
+							'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
986
+							'OBJ_ID'         => $tax->ID(),
987
+						)
988
+					);
989
+					$tax_line_item = apply_filters(
990
+						'FHEE__EEH_Line_Item__apply_taxes__tax_line_item',
991
+						$tax_line_item
992
+					);
993
+					$updates = $taxes_line_item->add_child_line_item($tax_line_item) ?
994
+						true :
995
+						$updates;
996
+				}
997
+			}
998
+		}
999
+		// only recalculate totals if something changed
1000
+		if ($deleted || $updates) {
1001
+			$total_line_item->recalculate_total_including_taxes($update_txn_status);
1002
+			return true;
1003
+		}
1004
+		return false;
1005
+	}
1006
+
1007
+
1008
+	/**
1009
+	 * Ensures that taxes have been applied to the order, if not applies them.
1010
+	 * Returns the total amount of tax
1011
+	 *
1012
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
1013
+	 * @return float
1014
+	 * @throws EE_Error
1015
+	 * @throws InvalidArgumentException
1016
+	 * @throws InvalidDataTypeException
1017
+	 * @throws InvalidInterfaceException
1018
+	 * @throws ReflectionException
1019
+	 */
1020
+	public static function ensure_taxes_applied($total_line_item)
1021
+	{
1022
+		$taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1023
+		if (! $taxes_subtotal->children()) {
1024
+			self::apply_taxes($total_line_item);
1025
+		}
1026
+		return $taxes_subtotal->total();
1027
+	}
1028
+
1029
+
1030
+	/**
1031
+	 * Deletes ALL children of the passed line item
1032
+	 *
1033
+	 * @param EE_Line_Item $parent_line_item
1034
+	 * @return bool
1035
+	 * @throws EE_Error
1036
+	 * @throws InvalidArgumentException
1037
+	 * @throws InvalidDataTypeException
1038
+	 * @throws InvalidInterfaceException
1039
+	 * @throws ReflectionException
1040
+	 */
1041
+	public static function delete_all_child_items(EE_Line_Item $parent_line_item)
1042
+	{
1043
+		$deleted = 0;
1044
+		foreach ($parent_line_item->children() as $child_line_item) {
1045
+			if ($child_line_item instanceof EE_Line_Item) {
1046
+				$deleted += EEH_Line_Item::delete_all_child_items($child_line_item);
1047
+				if ($child_line_item->ID()) {
1048
+					$child_line_item->delete();
1049
+					unset($child_line_item);
1050
+				} else {
1051
+					$parent_line_item->delete_child_line_item($child_line_item->code());
1052
+				}
1053
+				$deleted++;
1054
+			}
1055
+		}
1056
+		return $deleted;
1057
+	}
1058
+
1059
+
1060
+	/**
1061
+	 * Deletes the line items as indicated by the line item code(s) provided,
1062
+	 * regardless of where they're found in the line item tree. Automatically
1063
+	 * re-calculates the line item totals and updates the related transaction. But
1064
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
1065
+	 * should probably change because of this).
1066
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
1067
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
1068
+	 *
1069
+	 * @param EE_Line_Item      $total_line_item of type EEM_Line_Item::type_total
1070
+	 * @param array|bool|string $line_item_codes
1071
+	 * @return int number of items successfully removed
1072
+	 * @throws EE_Error
1073
+	 */
1074
+	public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false)
1075
+	{
1076
+
1077
+		if ($total_line_item->type() !== EEM_Line_Item::type_total) {
1078
+			EE_Error::doing_it_wrong(
1079
+				'EEH_Line_Item::delete_items',
1080
+				esc_html__(
1081
+					'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly',
1082
+					'event_espresso'
1083
+				),
1084
+				'4.6.18'
1085
+			);
1086
+		}
1087
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1088
+
1089
+		// check if only a single line_item_id was passed
1090
+		if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1091
+			// place single line_item_id in an array to appear as multiple line_item_ids
1092
+			$line_item_codes = array($line_item_codes);
1093
+		}
1094
+		$removals = 0;
1095
+		// cycle thru line_item_ids
1096
+		foreach ($line_item_codes as $line_item_id) {
1097
+			$removals += $total_line_item->delete_child_line_item($line_item_id);
1098
+		}
1099
+
1100
+		if ($removals > 0) {
1101
+			$total_line_item->recalculate_taxes_and_tax_total();
1102
+			return $removals;
1103
+		} else {
1104
+			return false;
1105
+		}
1106
+	}
1107
+
1108
+
1109
+	/**
1110
+	 * Overwrites the previous tax by clearing out the old taxes, and creates a new
1111
+	 * tax and updates the total line item accordingly
1112
+	 *
1113
+	 * @param EE_Line_Item $total_line_item
1114
+	 * @param float        $amount
1115
+	 * @param string       $name
1116
+	 * @param string       $description
1117
+	 * @param string       $code
1118
+	 * @param boolean      $add_to_existing_line_item
1119
+	 *                          if true, and a duplicate line item with the same code is found,
1120
+	 *                          $amount will be added onto it; otherwise will simply set the taxes to match $amount
1121
+	 * @return EE_Line_Item the new tax line item created
1122
+	 * @throws EE_Error
1123
+	 * @throws InvalidArgumentException
1124
+	 * @throws InvalidDataTypeException
1125
+	 * @throws InvalidInterfaceException
1126
+	 * @throws ReflectionException
1127
+	 */
1128
+	public static function set_total_tax_to(
1129
+		EE_Line_Item $total_line_item,
1130
+		$amount,
1131
+		$name = null,
1132
+		$description = null,
1133
+		$code = null,
1134
+		$add_to_existing_line_item = false
1135
+	) {
1136
+		$tax_subtotal = self::get_taxes_subtotal($total_line_item);
1137
+		$taxable_total = $total_line_item->taxable_total();
1138
+
1139
+		if ($add_to_existing_line_item) {
1140
+			$new_tax = $tax_subtotal->get_child_line_item($code);
1141
+			EEM_Line_Item::instance()->delete(
1142
+				array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID()))
1143
+			);
1144
+		} else {
1145
+			$new_tax = null;
1146
+			$tax_subtotal->delete_children_line_items();
1147
+		}
1148
+		if ($new_tax) {
1149
+			$new_tax->set_total($new_tax->total() + $amount);
1150
+			$new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0);
1151
+		} else {
1152
+			// no existing tax item. Create it
1153
+			$new_tax = EE_Line_Item::new_instance(array(
1154
+				'TXN_ID'      => $total_line_item->TXN_ID(),
1155
+				'LIN_name'    => $name ? $name : esc_html__('Tax', 'event_espresso'),
1156
+				'LIN_desc'    => $description ? $description : '',
1157
+				'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0,
1158
+				'LIN_total'   => $amount,
1159
+				'LIN_parent'  => $tax_subtotal->ID(),
1160
+				'LIN_type'    => EEM_Line_Item::type_tax,
1161
+				'LIN_code'    => $code,
1162
+			));
1163
+		}
1164
+
1165
+		$new_tax = apply_filters(
1166
+			'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal',
1167
+			$new_tax,
1168
+			$total_line_item
1169
+		);
1170
+		$new_tax->save();
1171
+		$tax_subtotal->set_total($new_tax->total());
1172
+		$tax_subtotal->save();
1173
+		$total_line_item->recalculate_total_including_taxes();
1174
+		return $new_tax;
1175
+	}
1176
+
1177
+
1178
+	/**
1179
+	 * Makes all the line items which are children of $line_item taxable (or not).
1180
+	 * Does NOT save the line items
1181
+	 *
1182
+	 * @param EE_Line_Item $line_item
1183
+	 * @param boolean      $taxable
1184
+	 * @param string       $code_substring_for_whitelist if this string is part of the line item's code
1185
+	 *                                                   it will be whitelisted (ie, except from becoming taxable)
1186
+	 * @throws EE_Error
1187
+	 */
1188
+	public static function set_line_items_taxable(
1189
+		EE_Line_Item $line_item,
1190
+		$taxable = true,
1191
+		$code_substring_for_whitelist = null
1192
+	) {
1193
+		$whitelisted = false;
1194
+		if ($code_substring_for_whitelist !== null) {
1195
+			$whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1196
+		}
1197
+		if (! $whitelisted && $line_item->is_line_item()) {
1198
+			$line_item->set_is_taxable($taxable);
1199
+		}
1200
+		foreach ($line_item->children() as $child_line_item) {
1201
+			EEH_Line_Item::set_line_items_taxable(
1202
+				$child_line_item,
1203
+				$taxable,
1204
+				$code_substring_for_whitelist
1205
+			);
1206
+		}
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 * Gets all descendants that are event subtotals
1212
+	 *
1213
+	 * @uses  EEH_Line_Item::get_subtotals_of_object_type()
1214
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1215
+	 * @return EE_Line_Item[]
1216
+	 * @throws EE_Error
1217
+	 */
1218
+	public static function get_event_subtotals(EE_Line_Item $parent_line_item)
1219
+	{
1220
+		return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT);
1221
+	}
1222
+
1223
+
1224
+	/**
1225
+	 * Gets all descendants subtotals that match the supplied object type
1226
+	 *
1227
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1228
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1229
+	 * @param string       $obj_type
1230
+	 * @return EE_Line_Item[]
1231
+	 * @throws EE_Error
1232
+	 */
1233
+	public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1234
+	{
1235
+		return self::_get_descendants_by_type_and_object_type(
1236
+			$parent_line_item,
1237
+			EEM_Line_Item::type_sub_total,
1238
+			$obj_type
1239
+		);
1240
+	}
1241
+
1242
+
1243
+	/**
1244
+	 * Gets all descendants that are tickets
1245
+	 *
1246
+	 * @uses  EEH_Line_Item::get_line_items_of_object_type()
1247
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1248
+	 * @return EE_Line_Item[]
1249
+	 * @throws EE_Error
1250
+	 */
1251
+	public static function get_ticket_line_items(EE_Line_Item $parent_line_item)
1252
+	{
1253
+		return self::get_line_items_of_object_type(
1254
+			$parent_line_item,
1255
+			EEM_Line_Item::OBJ_TYPE_TICKET
1256
+		);
1257
+	}
1258
+
1259
+
1260
+	/**
1261
+	 * Gets all descendants subtotals that match the supplied object type
1262
+	 *
1263
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1264
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1265
+	 * @param string       $obj_type
1266
+	 * @return EE_Line_Item[]
1267
+	 * @throws EE_Error
1268
+	 */
1269
+	public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1270
+	{
1271
+		return self::_get_descendants_by_type_and_object_type(
1272
+			$parent_line_item,
1273
+			EEM_Line_Item::type_line_item,
1274
+			$obj_type
1275
+		);
1276
+	}
1277
+
1278
+
1279
+	/**
1280
+	 * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax'
1281
+	 *
1282
+	 * @uses  EEH_Line_Item::get_descendants_of_type()
1283
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1284
+	 * @return EE_Line_Item[]
1285
+	 * @throws EE_Error
1286
+	 */
1287
+	public static function get_tax_descendants(EE_Line_Item $parent_line_item)
1288
+	{
1289
+		return EEH_Line_Item::get_descendants_of_type(
1290
+			$parent_line_item,
1291
+			EEM_Line_Item::type_tax
1292
+		);
1293
+	}
1294
+
1295
+
1296
+	/**
1297
+	 * Gets all the real items purchased which are children of this item
1298
+	 *
1299
+	 * @uses  EEH_Line_Item::get_descendants_of_type()
1300
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1301
+	 * @return EE_Line_Item[]
1302
+	 * @throws EE_Error
1303
+	 */
1304
+	public static function get_line_item_descendants(EE_Line_Item $parent_line_item)
1305
+	{
1306
+		return EEH_Line_Item::get_descendants_of_type(
1307
+			$parent_line_item,
1308
+			EEM_Line_Item::type_line_item
1309
+		);
1310
+	}
1311
+
1312
+
1313
+	/**
1314
+	 * Gets all descendants of supplied line item that match the supplied line item type
1315
+	 *
1316
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1317
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1318
+	 * @param string       $line_item_type   one of the EEM_Line_Item constants
1319
+	 * @return EE_Line_Item[]
1320
+	 * @throws EE_Error
1321
+	 */
1322
+	public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type)
1323
+	{
1324
+		return self::_get_descendants_by_type_and_object_type(
1325
+			$parent_line_item,
1326
+			$line_item_type,
1327
+			null
1328
+		);
1329
+	}
1330
+
1331
+
1332
+	/**
1333
+	 * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1334
+	 * as well
1335
+	 *
1336
+	 * @param EE_Line_Item  $parent_line_item - the line item to find descendants of
1337
+	 * @param string        $line_item_type   one of the EEM_Line_Item constants
1338
+	 * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1339
+	 *                                        searching
1340
+	 * @return EE_Line_Item[]
1341
+	 * @throws EE_Error
1342
+	 */
1343
+	protected static function _get_descendants_by_type_and_object_type(
1344
+		EE_Line_Item $parent_line_item,
1345
+		$line_item_type,
1346
+		$obj_type = null
1347
+	) {
1348
+		$objects = array();
1349
+		foreach ($parent_line_item->children() as $child_line_item) {
1350
+			if ($child_line_item instanceof EE_Line_Item) {
1351
+				if ($child_line_item->type() === $line_item_type
1352
+					&& (
1353
+						$child_line_item->OBJ_type() === $obj_type || $obj_type === null
1354
+					)
1355
+				) {
1356
+					$objects[] = $child_line_item;
1357
+				} else {
1358
+					// go-through-all-its children looking for more matches
1359
+					$objects = array_merge(
1360
+						$objects,
1361
+						self::_get_descendants_by_type_and_object_type(
1362
+							$child_line_item,
1363
+							$line_item_type,
1364
+							$obj_type
1365
+						)
1366
+					);
1367
+				}
1368
+			}
1369
+		}
1370
+		return $objects;
1371
+	}
1372
+
1373
+
1374
+	/**
1375
+	 * Gets all descendants subtotals that match the supplied object type
1376
+	 *
1377
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1378
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1379
+	 * @param string       $OBJ_type         object type (like Event)
1380
+	 * @param array        $OBJ_IDs          array of OBJ_IDs
1381
+	 * @return EE_Line_Item[]
1382
+	 * @throws EE_Error
1383
+	 */
1384
+	public static function get_line_items_by_object_type_and_IDs(
1385
+		EE_Line_Item $parent_line_item,
1386
+		$OBJ_type = '',
1387
+		$OBJ_IDs = array()
1388
+	) {
1389
+		return self::_get_descendants_by_object_type_and_object_ID(
1390
+			$parent_line_item,
1391
+			$OBJ_type,
1392
+			$OBJ_IDs
1393
+		);
1394
+	}
1395
+
1396
+
1397
+	/**
1398
+	 * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1399
+	 * as well
1400
+	 *
1401
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1402
+	 * @param string       $OBJ_type         object type (like Event)
1403
+	 * @param array        $OBJ_IDs          array of OBJ_IDs
1404
+	 * @return EE_Line_Item[]
1405
+	 * @throws EE_Error
1406
+	 */
1407
+	protected static function _get_descendants_by_object_type_and_object_ID(
1408
+		EE_Line_Item $parent_line_item,
1409
+		$OBJ_type,
1410
+		$OBJ_IDs
1411
+	) {
1412
+		$objects = array();
1413
+		foreach ($parent_line_item->children() as $child_line_item) {
1414
+			if ($child_line_item instanceof EE_Line_Item) {
1415
+				if ($child_line_item->OBJ_type() === $OBJ_type
1416
+					&& is_array($OBJ_IDs)
1417
+					&& in_array($child_line_item->OBJ_ID(), $OBJ_IDs)
1418
+				) {
1419
+					$objects[] = $child_line_item;
1420
+				} else {
1421
+					// go-through-all-its children looking for more matches
1422
+					$objects = array_merge(
1423
+						$objects,
1424
+						self::_get_descendants_by_object_type_and_object_ID(
1425
+							$child_line_item,
1426
+							$OBJ_type,
1427
+							$OBJ_IDs
1428
+						)
1429
+					);
1430
+				}
1431
+			}
1432
+		}
1433
+		return $objects;
1434
+	}
1435
+
1436
+
1437
+	/**
1438
+	 * Uses a breadth-first-search in order to find the nearest descendant of
1439
+	 * the specified type and returns it, else NULL
1440
+	 *
1441
+	 * @uses  EEH_Line_Item::_get_nearest_descendant()
1442
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1443
+	 * @param string       $type             like one of the EEM_Line_Item::type_*
1444
+	 * @return EE_Line_Item
1445
+	 * @throws EE_Error
1446
+	 * @throws InvalidArgumentException
1447
+	 * @throws InvalidDataTypeException
1448
+	 * @throws InvalidInterfaceException
1449
+	 * @throws ReflectionException
1450
+	 */
1451
+	public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type)
1452
+	{
1453
+		return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type);
1454
+	}
1455
+
1456
+
1457
+	/**
1458
+	 * Uses a breadth-first-search in order to find the nearest descendant
1459
+	 * having the specified LIN_code and returns it, else NULL
1460
+	 *
1461
+	 * @uses  EEH_Line_Item::_get_nearest_descendant()
1462
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1463
+	 * @param string       $code             any value used for LIN_code
1464
+	 * @return EE_Line_Item
1465
+	 * @throws EE_Error
1466
+	 * @throws InvalidArgumentException
1467
+	 * @throws InvalidDataTypeException
1468
+	 * @throws InvalidInterfaceException
1469
+	 * @throws ReflectionException
1470
+	 */
1471
+	public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code)
1472
+	{
1473
+		return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code);
1474
+	}
1475
+
1476
+
1477
+	/**
1478
+	 * Uses a breadth-first-search in order to find the nearest descendant
1479
+	 * having the specified LIN_code and returns it, else NULL
1480
+	 *
1481
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1482
+	 * @param string       $search_field     name of EE_Line_Item property
1483
+	 * @param string       $value            any value stored in $search_field
1484
+	 * @return EE_Line_Item
1485
+	 * @throws EE_Error
1486
+	 * @throws InvalidArgumentException
1487
+	 * @throws InvalidDataTypeException
1488
+	 * @throws InvalidInterfaceException
1489
+	 * @throws ReflectionException
1490
+	 */
1491
+	protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value)
1492
+	{
1493
+		foreach ($parent_line_item->children() as $child) {
1494
+			if ($child->get($search_field) == $value) {
1495
+				return $child;
1496
+			}
1497
+		}
1498
+		foreach ($parent_line_item->children() as $child) {
1499
+			$descendant_found = self::_get_nearest_descendant(
1500
+				$child,
1501
+				$search_field,
1502
+				$value
1503
+			);
1504
+			if ($descendant_found) {
1505
+				return $descendant_found;
1506
+			}
1507
+		}
1508
+		return null;
1509
+	}
1510
+
1511
+
1512
+	/**
1513
+	 * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction,
1514
+	 * else recursively walks up the line item tree until a parent of type total is found,
1515
+	 *
1516
+	 * @param EE_Line_Item $line_item
1517
+	 * @return EE_Line_Item
1518
+	 * @throws EE_Error
1519
+	 */
1520
+	public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item)
1521
+	{
1522
+		if ($line_item->TXN_ID()) {
1523
+			$total_line_item = $line_item->transaction()->total_line_item(false);
1524
+			if ($total_line_item instanceof EE_Line_Item) {
1525
+				return $total_line_item;
1526
+			}
1527
+		} else {
1528
+			$line_item_parent = $line_item->parent();
1529
+			if ($line_item_parent instanceof EE_Line_Item) {
1530
+				if ($line_item_parent->is_total()) {
1531
+					return $line_item_parent;
1532
+				}
1533
+				return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent);
1534
+			}
1535
+		}
1536
+		throw new EE_Error(
1537
+			sprintf(
1538
+				esc_html__(
1539
+					'A valid grand total for line item %1$d was not found.',
1540
+					'event_espresso'
1541
+				),
1542
+				$line_item->ID()
1543
+			)
1544
+		);
1545
+	}
1546
+
1547
+
1548
+	/**
1549
+	 * Prints out a representation of the line item tree
1550
+	 *
1551
+	 * @param EE_Line_Item $line_item
1552
+	 * @param int          $indentation
1553
+	 * @return void
1554
+	 * @throws EE_Error
1555
+	 */
1556
+	public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1557
+	{
1558
+		echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559
+		if (! $indentation) {
1560
+			echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1561
+		}
1562
+		for ($i = 0; $i < $indentation; $i++) {
1563
+			echo '. ';
1564
+		}
1565
+		$breakdown = '';
1566
+		if ($line_item->is_line_item()) {
1567
+			if ($line_item->is_percent()) {
1568
+				$breakdown = "{$line_item->percent()}%";
1569
+			} else {
1570
+				$breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1571
+			}
1572
+		}
1573
+		echo $line_item->name();
1574
+		echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1575
+		echo '$' . (string) $line_item->total();
1576
+		if ($breakdown) {
1577
+			echo " ( {$breakdown} )";
1578
+		}
1579
+		if ($line_item->is_taxable()) {
1580
+			echo '  * taxable';
1581
+		}
1582
+		if ($line_item->children()) {
1583
+			foreach ($line_item->children() as $child) {
1584
+				self::visualize($child, $indentation + 1);
1585
+			}
1586
+		}
1587
+	}
1588
+
1589
+
1590
+	/**
1591
+	 * Calculates the registration's final price, taking into account that they
1592
+	 * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes,
1593
+	 * and receive a portion of any transaction-wide discounts.
1594
+	 * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount
1595
+	 * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get
1596
+	 * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50
1597
+	 * and brent's final price should be $5.50.
1598
+	 * In order to do this, we basically need to traverse the line item tree calculating
1599
+	 * the running totals (just as if we were recalculating the total), but when we identify
1600
+	 * regular line items, we need to keep track of their share of the grand total.
1601
+	 * Also, we need to keep track of the TAXABLE total for each ticket purchase, so
1602
+	 * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total"
1603
+	 * when there are non-taxable items; otherwise they would be the same)
1604
+	 *
1605
+	 * @param EE_Line_Item $line_item
1606
+	 * @param array        $billable_ticket_quantities  array of EE_Ticket IDs and their corresponding quantity that
1607
+	 *                                                  can be included in price calculations at this moment
1608
+	 * @return array        keys are line items for tickets IDs and values are their share of the running total,
1609
+	 *                                                  plus the key 'total', and 'taxable' which also has keys of all
1610
+	 *                                                  the ticket IDs.
1611
+	 *                                                  Eg array(
1612
+	 *                                                      12 => 4.3
1613
+	 *                                                      23 => 8.0
1614
+	 *                                                      'total' => 16.6,
1615
+	 *                                                      'taxable' => array(
1616
+	 *                                                          12 => 10,
1617
+	 *                                                          23 => 4
1618
+	 *                                                      ).
1619
+	 *                                                  So to find which registrations have which final price, we need
1620
+	 *                                                  to find which line item is theirs, which can be done with
1621
+	 *                                                  `EEM_Line_Item::instance()->get_line_item_for_registration(
1622
+	 *                                                  $registration );`
1623
+	 * @throws EE_Error
1624
+	 * @throws InvalidArgumentException
1625
+	 * @throws InvalidDataTypeException
1626
+	 * @throws InvalidInterfaceException
1627
+	 * @throws ReflectionException
1628
+	 */
1629
+	public static function calculate_reg_final_prices_per_line_item(
1630
+		EE_Line_Item $line_item,
1631
+		$billable_ticket_quantities = array()
1632
+	) {
1633
+		$running_totals = [
1634
+			'total'   => 0,
1635
+			'taxable' => ['total' => 0]
1636
+		];
1637
+		foreach ($line_item->children() as $child_line_item) {
1638
+			switch ($child_line_item->type()) {
1639
+				case EEM_Line_Item::type_sub_total:
1640
+					$running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1641
+						$child_line_item,
1642
+						$billable_ticket_quantities
1643
+					);
1644
+					// combine arrays but preserve numeric keys
1645
+					$running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals);
1646
+					$running_totals['total'] += $running_totals_from_subtotal['total'];
1647
+					$running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total'];
1648
+					break;
1649
+
1650
+				case EEM_Line_Item::type_tax_sub_total:
1651
+					// find how much the taxes percentage is
1652
+					if ($child_line_item->percent() !== 0) {
1653
+						$tax_percent_decimal = $child_line_item->percent() / 100;
1654
+					} else {
1655
+						$tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100;
1656
+					}
1657
+					// and apply to all the taxable totals, and add to the pretax totals
1658
+					foreach ($running_totals as $line_item_id => $this_running_total) {
1659
+						// "total" and "taxable" array key is an exception
1660
+						if ($line_item_id === 'taxable') {
1661
+							continue;
1662
+						}
1663
+						$taxable_total = $running_totals['taxable'][ $line_item_id ];
1664
+						$running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1665
+					}
1666
+					break;
1667
+
1668
+				case EEM_Line_Item::type_line_item:
1669
+					// ticket line items or ????
1670
+					if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1671
+						// kk it's a ticket
1672
+						if (isset($running_totals[ $child_line_item->ID() ])) {
1673
+							// huh? that shouldn't happen.
1674
+							$running_totals['total'] += $child_line_item->total();
1675
+						} else {
1676
+							// its not in our running totals yet. great.
1677
+							if ($child_line_item->is_taxable()) {
1678
+								$taxable_amount = $child_line_item->unit_price();
1679
+							} else {
1680
+								$taxable_amount = 0;
1681
+							}
1682
+							// are we only calculating totals for some tickets?
1683
+							if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1684
+								$quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1685
+								$running_totals[ $child_line_item->ID() ] = $quantity
1686
+									? $child_line_item->unit_price()
1687
+									: 0;
1688
+								$running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1689
+									? $taxable_amount
1690
+									: 0;
1691
+							} else {
1692
+								$quantity = $child_line_item->quantity();
1693
+								$running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1694
+								$running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1695
+							}
1696
+							$running_totals['taxable']['total'] += $taxable_amount * $quantity;
1697
+							$running_totals['total'] += $child_line_item->unit_price() * $quantity;
1698
+						}
1699
+					} else {
1700
+						// it's some other type of item added to the cart
1701
+						// it should affect the running totals
1702
+						// basically we want to convert it into a PERCENT modifier. Because
1703
+						// more clearly affect all registration's final price equally
1704
+						$line_items_percent_of_running_total = $running_totals['total'] > 0
1705
+							? ($child_line_item->total() / $running_totals['total']) + 1
1706
+							: 1;
1707
+						foreach ($running_totals as $line_item_id => $this_running_total) {
1708
+							// the "taxable" array key is an exception
1709
+							if ($line_item_id === 'taxable') {
1710
+								continue;
1711
+							}
1712
+							// update the running totals
1713
+							// yes this actually even works for the running grand total!
1714
+							$running_totals[ $line_item_id ] =
1715
+								$line_items_percent_of_running_total * $this_running_total;
1716
+
1717
+							if ($child_line_item->is_taxable()) {
1718
+								$running_totals['taxable'][ $line_item_id ] =
1719
+									$line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1720
+							}
1721
+						}
1722
+					}
1723
+					break;
1724
+			}
1725
+		}
1726
+		return $running_totals;
1727
+	}
1728
+
1729
+
1730
+	/**
1731
+	 * @param EE_Line_Item $total_line_item
1732
+	 * @param EE_Line_Item $ticket_line_item
1733
+	 * @return float | null
1734
+	 * @throws EE_Error
1735
+	 * @throws InvalidArgumentException
1736
+	 * @throws InvalidDataTypeException
1737
+	 * @throws InvalidInterfaceException
1738
+	 * @throws OutOfRangeException
1739
+	 * @throws ReflectionException
1740
+	 */
1741
+	public static function calculate_final_price_for_ticket_line_item(
1742
+		EE_Line_Item $total_line_item,
1743
+		EE_Line_Item $ticket_line_item
1744
+	) {
1745
+		static $final_prices_per_ticket_line_item = array();
1746
+		if (empty($final_prices_per_ticket_line_item)) {
1747
+			$final_prices_per_ticket_line_item = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1748
+				$total_line_item
1749
+			);
1750
+		}
1751
+		// ok now find this new registration's final price
1752
+		if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) {
1753
+			return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ];
1754
+		}
1755
+		$message = sprintf(
1756
+			esc_html__(
1757
+				'The final price for the ticket line item (ID:%1$d) could not be calculated.',
1758
+				'event_espresso'
1759
+			),
1760
+			$ticket_line_item->ID()
1761
+		);
1762
+		if (WP_DEBUG) {
1763
+			$message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1764
+			throw new OutOfRangeException($message);
1765
+		}
1766
+		EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
1767
+		return null;
1768
+	}
1769
+
1770
+
1771
+	/**
1772
+	 * Creates a duplicate of the line item tree, except only includes billable items
1773
+	 * and the portion of line items attributed to billable things
1774
+	 *
1775
+	 * @param EE_Line_Item      $line_item
1776
+	 * @param EE_Registration[] $registrations
1777
+	 * @return EE_Line_Item
1778
+	 * @throws EE_Error
1779
+	 * @throws InvalidArgumentException
1780
+	 * @throws InvalidDataTypeException
1781
+	 * @throws InvalidInterfaceException
1782
+	 * @throws ReflectionException
1783
+	 */
1784
+	public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations)
1785
+	{
1786
+		$copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations);
1787
+		foreach ($line_item->children() as $child_li) {
1788
+			$copy_li->add_child_line_item(
1789
+				EEH_Line_Item::billable_line_item_tree($child_li, $registrations)
1790
+			);
1791
+		}
1792
+		// if this is the grand total line item, make sure the totals all add up
1793
+		// (we could have duplicated this logic AS we copied the line items, but
1794
+		// it seems DRYer this way)
1795
+		if ($copy_li->type() === EEM_Line_Item::type_total) {
1796
+			$copy_li->recalculate_total_including_taxes();
1797
+		}
1798
+		return $copy_li;
1799
+	}
1800
+
1801
+
1802
+	/**
1803
+	 * Creates a new, unsaved line item from $line_item that factors in the
1804
+	 * number of billable registrations on $registrations.
1805
+	 *
1806
+	 * @param EE_Line_Item      $line_item
1807
+	 * @param EE_Registration[] $registrations
1808
+	 * @return EE_Line_Item
1809
+	 * @throws EE_Error
1810
+	 * @throws InvalidArgumentException
1811
+	 * @throws InvalidDataTypeException
1812
+	 * @throws InvalidInterfaceException
1813
+	 * @throws ReflectionException
1814
+	 */
1815
+	public static function billable_line_item(EE_Line_Item $line_item, $registrations)
1816
+	{
1817
+		$new_li_fields = $line_item->model_field_array();
1818
+		if ($line_item->type() === EEM_Line_Item::type_line_item &&
1819
+			$line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1820
+		) {
1821
+			$count = 0;
1822
+			foreach ($registrations as $registration) {
1823
+				if ($line_item->OBJ_ID() === $registration->ticket_ID() &&
1824
+					in_array(
1825
+						$registration->status_ID(),
1826
+						EEM_Registration::reg_statuses_that_allow_payment(),
1827
+						true
1828
+					)
1829
+				) {
1830
+					$count++;
1831
+				}
1832
+			}
1833
+			$new_li_fields['LIN_quantity'] = $count;
1834
+		}
1835
+		// don't set the total. We'll leave that up to the code that calculates it
1836
+		unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']);
1837
+		return EE_Line_Item::new_instance($new_li_fields);
1838
+	}
1839
+
1840
+
1841
+	/**
1842
+	 * Returns a modified line item tree where all the subtotals which have a total of 0
1843
+	 * are removed, and line items with a quantity of 0
1844
+	 *
1845
+	 * @param EE_Line_Item $line_item |null
1846
+	 * @return EE_Line_Item|null
1847
+	 * @throws EE_Error
1848
+	 * @throws InvalidArgumentException
1849
+	 * @throws InvalidDataTypeException
1850
+	 * @throws InvalidInterfaceException
1851
+	 * @throws ReflectionException
1852
+	 */
1853
+	public static function non_empty_line_items(EE_Line_Item $line_item)
1854
+	{
1855
+		$copied_li = EEH_Line_Item::non_empty_line_item($line_item);
1856
+		if ($copied_li === null) {
1857
+			return null;
1858
+		}
1859
+		// if this is an event subtotal, we want to only include it if it
1860
+		// has a non-zero total and at least one ticket line item child
1861
+		$ticket_children = 0;
1862
+		foreach ($line_item->children() as $child_li) {
1863
+			$child_li_copy = EEH_Line_Item::non_empty_line_items($child_li);
1864
+			if ($child_li_copy !== null) {
1865
+				$copied_li->add_child_line_item($child_li_copy);
1866
+				if ($child_li_copy->type() === EEM_Line_Item::type_line_item &&
1867
+					$child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1868
+				) {
1869
+					$ticket_children++;
1870
+				}
1871
+			}
1872
+		}
1873
+		// if this is an event subtotal with NO ticket children
1874
+		// we basically want to ignore it
1875
+		if ($ticket_children === 0
1876
+			&& $line_item->type() === EEM_Line_Item::type_sub_total
1877
+			&& $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
1878
+			&& $line_item->total() === 0
1879
+		) {
1880
+			return null;
1881
+		}
1882
+		return $copied_li;
1883
+	}
1884
+
1885
+
1886
+	/**
1887
+	 * Creates a new, unsaved line item, but if it's a ticket line item
1888
+	 * with a total of 0, or a subtotal of 0, returns null instead
1889
+	 *
1890
+	 * @param EE_Line_Item $line_item
1891
+	 * @return EE_Line_Item
1892
+	 * @throws EE_Error
1893
+	 * @throws InvalidArgumentException
1894
+	 * @throws InvalidDataTypeException
1895
+	 * @throws InvalidInterfaceException
1896
+	 * @throws ReflectionException
1897
+	 */
1898
+	public static function non_empty_line_item(EE_Line_Item $line_item)
1899
+	{
1900
+		if ($line_item->type() === EEM_Line_Item::type_line_item
1901
+			&& $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1902
+			&& $line_item->quantity() === 0
1903
+		) {
1904
+			return null;
1905
+		}
1906
+		$new_li_fields = $line_item->model_field_array();
1907
+		// don't set the total. We'll leave that up to the code that calculates it
1908
+		unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']);
1909
+		return EE_Line_Item::new_instance($new_li_fields);
1910
+	}
1911
+
1912
+
1913
+	/**
1914
+	 * Cycles through all of the ticket line items for the supplied total line item
1915
+	 * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket
1916
+	 *
1917
+	 * @param EE_Line_Item $total_line_item
1918
+	 * @since 4.9.79.p
1919
+	 * @throws EE_Error
1920
+	 * @throws InvalidArgumentException
1921
+	 * @throws InvalidDataTypeException
1922
+	 * @throws InvalidInterfaceException
1923
+	 * @throws ReflectionException
1924
+	 */
1925
+	public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item)
1926
+	{
1927
+		$ticket_line_items = self::get_ticket_line_items($total_line_item);
1928
+		foreach ($ticket_line_items as $ticket_line_item) {
1929
+			if ($ticket_line_item instanceof EE_Line_Item
1930
+				&& $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1931
+			) {
1932
+				$ticket = $ticket_line_item->ticket();
1933
+				if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) {
1934
+					$ticket_line_item->set_is_taxable($ticket->taxable());
1935
+					$ticket_line_item->save();
1936
+				}
1937
+			}
1938
+		}
1939
+	}
1940
+
1941
+
1942
+
1943
+	/**************************************** @DEPRECATED METHODS *************************************** */
1944
+	/**
1945
+	 * @deprecated
1946
+	 * @param EE_Line_Item $total_line_item
1947
+	 * @return EE_Line_Item
1948
+	 * @throws EE_Error
1949
+	 * @throws InvalidArgumentException
1950
+	 * @throws InvalidDataTypeException
1951
+	 * @throws InvalidInterfaceException
1952
+	 * @throws ReflectionException
1953
+	 */
1954
+	public static function get_items_subtotal(EE_Line_Item $total_line_item)
1955
+	{
1956
+		EE_Error::doing_it_wrong(
1957
+			'EEH_Line_Item::get_items_subtotal()',
1958
+			sprintf(
1959
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1960
+				'EEH_Line_Item::get_pre_tax_subtotal()'
1961
+			),
1962
+			'4.6.0'
1963
+		);
1964
+		return self::get_pre_tax_subtotal($total_line_item);
1965
+	}
1966
+
1967
+
1968
+	/**
1969
+	 * @deprecated
1970
+	 * @param EE_Transaction $transaction
1971
+	 * @return EE_Line_Item
1972
+	 * @throws EE_Error
1973
+	 * @throws InvalidArgumentException
1974
+	 * @throws InvalidDataTypeException
1975
+	 * @throws InvalidInterfaceException
1976
+	 * @throws ReflectionException
1977
+	 */
1978
+	public static function create_default_total_line_item($transaction = null)
1979
+	{
1980
+		EE_Error::doing_it_wrong(
1981
+			'EEH_Line_Item::create_default_total_line_item()',
1982
+			sprintf(
1983
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1984
+				'EEH_Line_Item::create_total_line_item()'
1985
+			),
1986
+			'4.6.0'
1987
+		);
1988
+		return self::create_total_line_item($transaction);
1989
+	}
1990
+
1991
+
1992
+	/**
1993
+	 * @deprecated
1994
+	 * @param EE_Line_Item   $total_line_item
1995
+	 * @param EE_Transaction $transaction
1996
+	 * @return EE_Line_Item
1997
+	 * @throws EE_Error
1998
+	 * @throws InvalidArgumentException
1999
+	 * @throws InvalidDataTypeException
2000
+	 * @throws InvalidInterfaceException
2001
+	 * @throws ReflectionException
2002
+	 */
2003
+	public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2004
+	{
2005
+		EE_Error::doing_it_wrong(
2006
+			'EEH_Line_Item::create_default_tickets_subtotal()',
2007
+			sprintf(
2008
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2009
+				'EEH_Line_Item::create_pre_tax_subtotal()'
2010
+			),
2011
+			'4.6.0'
2012
+		);
2013
+		return self::create_pre_tax_subtotal($total_line_item, $transaction);
2014
+	}
2015
+
2016
+
2017
+	/**
2018
+	 * @deprecated
2019
+	 * @param EE_Line_Item   $total_line_item
2020
+	 * @param EE_Transaction $transaction
2021
+	 * @return EE_Line_Item
2022
+	 * @throws EE_Error
2023
+	 * @throws InvalidArgumentException
2024
+	 * @throws InvalidDataTypeException
2025
+	 * @throws InvalidInterfaceException
2026
+	 * @throws ReflectionException
2027
+	 */
2028
+	public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2029
+	{
2030
+		EE_Error::doing_it_wrong(
2031
+			'EEH_Line_Item::create_default_taxes_subtotal()',
2032
+			sprintf(
2033
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2034
+				'EEH_Line_Item::create_taxes_subtotal()'
2035
+			),
2036
+			'4.6.0'
2037
+		);
2038
+		return self::create_taxes_subtotal($total_line_item, $transaction);
2039
+	}
2040
+
2041
+
2042
+	/**
2043
+	 * @deprecated
2044
+	 * @param EE_Line_Item   $total_line_item
2045
+	 * @param EE_Transaction $transaction
2046
+	 * @return EE_Line_Item
2047
+	 * @throws EE_Error
2048
+	 * @throws InvalidArgumentException
2049
+	 * @throws InvalidDataTypeException
2050
+	 * @throws InvalidInterfaceException
2051
+	 * @throws ReflectionException
2052
+	 */
2053
+	public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2054
+	{
2055
+		EE_Error::doing_it_wrong(
2056
+			'EEH_Line_Item::create_default_event_subtotal()',
2057
+			sprintf(
2058
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2059
+				'EEH_Line_Item::create_event_subtotal()'
2060
+			),
2061
+			'4.6.0'
2062
+		);
2063
+		return self::create_event_subtotal($total_line_item, $transaction);
2064
+	}
2065 2065
 }
Please login to merge, or discard this patch.