Completed
Branch BUG-11108-ticket-reserved-coun... (74c229)
by
unknown
11:53 queued 12s
created

EventSpacesCalculator::setHooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\event;
4
5
use DomainException;
6
use EE_Datetime;
7
use EE_Error;
8
use EE_Event;
9
use EE_Ticket;
10
use EEM_Ticket;
11
use EventEspresso\core\exceptions\InvalidDataTypeException;
12
use EventEspresso\core\exceptions\InvalidInterfaceException;
13
use EventEspresso\core\exceptions\UnexpectedEntityException;
14
use InvalidArgumentException;
15
16
defined('EVENT_ESPRESSO_VERSION') || exit;
17
18
19
20
/**
21
 * Class EventSpacesCalculator
22
 * Calculates total available spaces for an event with no regard for sold tickets,
23
 * or spaces remaining based on "saleable" tickets.
24
 * This is done by looping through all of the tickets and datetimes for the event
25
 * and simulating the sale of available tickets until each datetime reaches its maximum capacity.
26
 *
27
 * @package EventEspresso\core\domain\services\event
28
 * @author  Brent Christensen
29
 * @since   4.9.45
30
 */
31
class EventSpacesCalculator
32
{
33
34
    /**
35
     * @var EE_Event $event
36
     */
37
    private $event;
38
39
    /**
40
     * @var array $datetime_query_params
41
     */
42
    private $datetime_query_params;
43
44
    /**
45
     * @var EE_Ticket[] $active_tickets
46
     */
47
    private $active_tickets = array();
48
49
    /**
50
     * @var EE_Datetime[] $datetimes
51
     */
52
    private $datetimes = array();
53
54
    /**
55
     * Array of Ticket IDs grouped by Datetime
56
     *
57
     * @var array $datetimes
58
     */
59
    private $datetime_tickets = array();
60
61
    /**
62
     * Max spaces for each Datetime (reg limit - previous sold)
63
     *
64
     * @var array $datetime_spaces
65
     */
66
    private $datetime_spaces = array();
67
68
    /**
69
     * Array of Datetime IDs grouped by Ticket
70
     *
71
     * @var array[] $ticket_datetimes
72
     */
73
    private $ticket_datetimes = array();
74
75
    /**
76
     * maximum ticket quantities for each ticket (adjusted for reg limit)
77
     *
78
     * @var array $ticket_quantities
79
     */
80
    private $ticket_quantities = array();
81
82
    /**
83
     * total quantity of sold and reserved for each ticket
84
     *
85
     * @var array $tickets_sold
86
     */
87
    private $tickets_sold = array();
88
89
    /**
90
     * total spaces available across all datetimes
91
     *
92
     * @var array $total_spaces
93
     */
94
    private $total_spaces = array();
95
96
    /**
97
     * @var boolean $debug
98
     */
99
    private $debug = false;
100
101
    /**
102
     * @var null|int $spaces_remaining
103
     */
104
    private $spaces_remaining;
105
106
    /**
107
     * @var null|int $total_spaces_available
108
     */
109
    private $total_spaces_available;
110
111
112
113
    /**
114
     * EventSpacesCalculator constructor.
115
     *
116
     * @param EE_Event $event
117
     * @param array    $datetime_query_params
118
     * @throws EE_Error
119
     */
120
    public function __construct(EE_Event $event, array $datetime_query_params = array())
121
    {
122
        $this->event                 = $event;
123
        $this->datetime_query_params = $datetime_query_params + array('order_by' => array('DTT_reg_limit' => 'ASC'));
124
        $this->setHooks();
125
    }
126
127
128
129
    /**
130
     * @return void
131
     */
132
    private function setHooks()
133
    {
134
        add_action( 'AHEE__EE_Ticket__increase_sold', array($this, 'clearResults'));
135
        add_action( 'AHEE__EE_Ticket__decrease_sold', array($this, 'clearResults'));
136
        add_action( 'AHEE__EE_Datetime__increase_sold', array($this, 'clearResults'));
137
        add_action( 'AHEE__EE_Datetime__decrease_sold', array($this, 'clearResults'));
138
        add_action( 'AHEE__EE_Ticket__increase_reserved', array($this, 'clearResults'));
139
        add_action( 'AHEE__EE_Ticket__decrease_reserved', array($this, 'clearResults'));
140
        add_action( 'AHEE__EE_Datetime__increase_reserved', array($this, 'clearResults'));
141
        add_action( 'AHEE__EE_Datetime__decrease_reserved', array($this, 'clearResults'));
142
    }
143
144
145
146
    /**
147
     * @return void
148
     */
149
    public function clearResults()
150
    {
151
        $this->spaces_remaining = null;
152
        $this->total_spaces_available = null;
153
    }
154
155
156
157
    /**
158
     * @return EE_Ticket[]
159
     * @throws EE_Error
160
     * @throws InvalidDataTypeException
161
     * @throws InvalidInterfaceException
162
     * @throws InvalidArgumentException
163
     */
164
    public function getActiveTickets()
165
    {
166
        if (empty($this->active_tickets)) {
167
            $this->active_tickets = $this->event->tickets(
168
                array(
169
                    array(
170
                        'TKT_end_date' => array('>=', EEM_Ticket::instance()->current_time_for_query('TKT_end_date')),
171
                        'TKT_deleted'  => false,
172
                    ),
173
                    'order_by' => array('TKT_qty' => 'ASC'),
174
                )
175
            );
176
        }
177
        return $this->active_tickets;
178
    }
179
180
181
182
    /**
183
     * @param EE_Ticket[] $active_tickets
184
     * @throws EE_Error
185
     * @throws DomainException
186
     * @throws UnexpectedEntityException
187
     */
188
    public function setActiveTickets(array $active_tickets = array())
189
    {
190
        if ( ! empty($active_tickets)) {
191
            foreach ($active_tickets as $active_ticket) {
192
                $this->validateTicket($active_ticket);
193
            }
194
            // sort incoming array by ticket quantity (asc)
195
            usort(
196
                $active_tickets,
197
                function (EE_Ticket $a, EE_Ticket $b) {
198
                    if ($a->qty() === $b->qty()) {
199
                        return 0;
200
                    }
201
                    return ($a->qty() < $b->qty())
202
                        ? -1
203
                        : 1;
204
                }
205
            );
206
        }
207
        $this->active_tickets = $active_tickets;
208
    }
209
210
211
212
    /**
213
     * @param $ticket
214
     * @throws DomainException
215
     * @throws EE_Error
216
     * @throws UnexpectedEntityException
217
     */
218
    private function validateTicket($ticket)
219
    {
220
        if ( ! $ticket instanceof EE_Ticket) {
221
            throw new DomainException(
222
                esc_html__(
223
                    'Invalid Ticket. Only EE_Ticket objects can be used to calculate event space availability.',
224
                    'event_espresso'
225
                )
226
            );
227
        }
228 View Code Duplication
        if ($ticket->get_event_ID() !== $this->event->ID()) {
229
            throw new DomainException(
230
                sprintf(
231
                    esc_html__(
232
                        'An EE_Ticket for Event %1$d was supplied while calculating event space availability for Event %2$d.',
233
                        'event_espresso'
234
                    ),
235
                    $ticket->get_event_ID(),
236
                    $this->event->ID()
237
                )
238
            );
239
        }
240
    }
241
242
243
244
    /**
245
     * @return EE_Datetime[]
246
     */
247
    public function getDatetimes()
248
    {
249
        return $this->datetimes;
250
    }
251
252
253
254
    /**
255
     * @param EE_Datetime $datetime
256
     * @throws EE_Error
257
     * @throws DomainException
258
     */
259
    public function setDatetime(EE_Datetime $datetime)
260
    {
261 View Code Duplication
        if ($datetime->event()->ID() !== $this->event->ID()) {
262
            throw new DomainException(
263
                sprintf(
264
                    esc_html__(
265
                        'An EE_Datetime for Event %1$d was supplied while calculating event space availability for Event %2$d.',
266
                        'event_espresso'
267
                    ),
268
                    $datetime->event()->ID(),
269
                    $this->event->ID()
270
                )
271
            );
272
        }
273
        $this->datetimes[ $datetime->ID() ] = $datetime;
274
    }
275
276
277
278
    /**
279
     * calculate spaces remaining based on "saleable" tickets
280
     *
281
     * @return float|int
282
     * @throws EE_Error
283
     * @throws DomainException
284
     * @throws UnexpectedEntityException
285
     * @throws InvalidDataTypeException
286
     * @throws InvalidInterfaceException
287
     * @throws InvalidArgumentException
288
     */
289
    public function spacesRemaining()
290
    {
291
        if ($this->spaces_remaining === null) {
292
            $this->initialize();
293
            $this->spaces_remaining = $this->calculate();
294
        }
295
        return $this->spaces_remaining;
296
    }
297
298
299
300
    /**
301
     * calculates total available spaces for an event with no regard for sold tickets
302
     *
303
     * @return int|float
304
     * @throws EE_Error
305
     * @throws DomainException
306
     * @throws UnexpectedEntityException
307
     * @throws InvalidDataTypeException
308
     * @throws InvalidInterfaceException
309
     * @throws InvalidArgumentException
310
     */
311
    public function totalSpacesAvailable()
312
    {
313
        if($this->total_spaces_available === null) {
314
            $this->initialize();
315
            $this->total_spaces_available = $this->calculate(false);
316
        }
317
        return $this->total_spaces_available;
318
    }
319
320
321
322
    /**
323
     * Loops through the active tickets for the event
324
     * and builds a series of data arrays that will be used for calculating
325
     * the total maximum available spaces, as well as the spaces remaining.
326
     * Because ticket quantities affect datetime spaces and vice versa,
327
     * we need to be constantly updating these data arrays as things change,
328
     * which is the entire reason for their existence.
329
     *
330
     * @throws EE_Error
331
     * @throws DomainException
332
     * @throws UnexpectedEntityException
333
     * @throws InvalidDataTypeException
334
     * @throws InvalidInterfaceException
335
     * @throws InvalidArgumentException
336
     */
337
    private function initialize()
338
    {
339
        if ($this->debug) {
340
            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
341
        }
342
        $this->datetime_tickets  = array();
343
        $this->datetime_spaces   = array();
344
        $this->ticket_datetimes  = array();
345
        $this->ticket_quantities = array();
346
        $this->tickets_sold      = array();
347
        $this->total_spaces      = array();
348
        $active_tickets          = $this->getActiveTickets();
349
        if ( ! empty($active_tickets)) {
350
            foreach ($active_tickets as $ticket) {
351
                $this->validateTicket($ticket);
352
                // we need to index our data arrays using strings for the purpose of sorting,
353
                // but we also need them to be unique, so  we'll just prepend a letter T to the ID
354
                $ticket_identifier = "T{$ticket->ID()}";
355
                // to start, we'll just consider the raw qty to be the maximum availability for this ticket
356
                $max_tickets = $ticket->qty();
357
                // but we'll adjust that after looping over each datetime for the ticket and checking reg limits
358
                $ticket_datetimes = $ticket->datetimes($this->datetime_query_params);
359
                foreach ($ticket_datetimes as $datetime) {
360
                    // save all datetimes
361
                    $this->setDatetime($datetime);
362
                    $datetime_identifier = "D{$datetime->ID()}";
363
                    $reg_limit           = $datetime->reg_limit();
364
                    // ticket quantity can not exceed datetime reg limit
365
                    $max_tickets = min($max_tickets, $reg_limit);
366
                    // as described earlier, because we need to be able to constantly adjust numbers for things,
367
                    // we are going to move all of our data into the following arrays:
368
                    // datetime spaces initially represents the reg limit for each datetime,
369
                    // but this will get adjusted as tickets are accounted for
370
                    $this->datetime_spaces[ $datetime_identifier ] = $reg_limit;
371
                    // just an array of ticket IDs grouped by datetime
372
                    $this->datetime_tickets[ $datetime_identifier ][] = $ticket_identifier;
373
                    // and an array of datetime IDs grouped by ticket
374
                    $this->ticket_datetimes[ $ticket_identifier ][] = $datetime_identifier;
375
                }
376
                // total quantity of sold and reserved for each ticket
377
                $this->tickets_sold[ $ticket_identifier ] = $ticket->sold() + $ticket->reserved();
378
                // and the maximum ticket quantities for each ticket (adjusted for reg limit)
379
                $this->ticket_quantities[ $ticket_identifier ] = $max_tickets;
380
            }
381
        }
382
        // sort datetime spaces by reg limit, but maintain our string indexes
383
        asort($this->datetime_spaces, SORT_NUMERIC);
384
        // datetime tickets need to be sorted in the SAME order as the above array...
385
        // so we'll just use array_merge() to take the structure of datetime_spaces
386
        // but overwrite all of the data with that from datetime_tickets
387
        $this->datetime_tickets = array_merge(
388
            $this->datetime_spaces,
389
            $this->datetime_tickets
390
        );
391
        if ($this->debug) {
392
            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
393
            \EEH_Debug_Tools::printr($this->datetime_tickets, 'datetime_tickets', __FILE__, __LINE__);
394
            \EEH_Debug_Tools::printr($this->ticket_quantities, 'ticket_quantities', __FILE__, __LINE__);
395
        }
396
    }
397
398
399
400
    /**
401
     * performs calculations on initialized data
402
     *
403
     * @param bool $consider_sold
404
     * @return int|float
405
     */
406
    private function calculate($consider_sold = true)
407
    {
408
        if ($this->debug) {
409
            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
410
        }
411
        if ($consider_sold) {
412
            // subtract amounts sold from all ticket quantities and datetime spaces
413
            $this->adjustTicketQuantitiesDueToSales();
414
        }
415
        foreach ($this->datetime_tickets as $datetime_identifier => $tickets) {
416
            $this->trackAvailableSpacesForDatetimes($datetime_identifier, $tickets);
417
        }
418
        // total spaces available is just the sum of the spaces available for each datetime
419
        $spaces_remaining = array_sum($this->total_spaces);
420 View Code Duplication
        if ($this->debug) {
421
            \EEH_Debug_Tools::printr($this->total_spaces, '$this->total_spaces', __FILE__, __LINE__);
422
            \EEH_Debug_Tools::printr($this->tickets_sold, '$this->tickets_sold', __FILE__, __LINE__);
423
            \EEH_Debug_Tools::printr($spaces_remaining, '$spaces_remaining', __FILE__, __LINE__);
424
        }
425
        return $spaces_remaining;
426
    }
427
428
429
    /**
430
     * subtracts amount of  tickets sold from ticket quantities and datetime spaces
431
     */
432
    private function adjustTicketQuantitiesDueToSales()
433
    {
434
        if ($this->debug) {
435
            \EEH_Debug_Tools::printr(__FUNCTION__, __CLASS__, __FILE__, __LINE__, 2);
436
        }
437
        foreach ($this->tickets_sold as $ticket_identifier => $tickets_sold) {
438 View Code Duplication
            if (isset($this->ticket_quantities[ $ticket_identifier ])){
439
                $this->ticket_quantities[ $ticket_identifier ] -= $tickets_sold;
440
                if ($this->debug) {
441
                    \EEH_Debug_Tools::printr("{$tickets_sold} sales for ticket {$ticket_identifier} ", 'subtracting', __FILE__, __LINE__);
442
                }
443
            }
444
            if (
445
                isset($this->ticket_datetimes[ $ticket_identifier ])
446
                && is_array($this->ticket_datetimes[ $ticket_identifier ])
447
            ){
448
                foreach ($this->ticket_datetimes[ $ticket_identifier ] as $ticket_datetime) {
449 View Code Duplication
                    if (isset($this->ticket_quantities[ $ticket_identifier ])) {
450
                        $this->datetime_spaces[ $ticket_datetime ] -= $tickets_sold;
451
                        if ($this->debug) {
452
                            \EEH_Debug_Tools::printr("{$tickets_sold} sales for datetime {$ticket_datetime} ",
453
                                'subtracting', __FILE__, __LINE__);
454
                        }
455
                    }
456
                }
457
            }
458
        }
459
    }
460
461
462
463
    /**
464
     * @param string $datetime_identifier
465
     * @param array  $tickets
466
     */
467
    private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets)
468
    {
469
        // make sure a reg limit is set for the datetime
470
        $reg_limit = isset($this->datetime_spaces[ $datetime_identifier ])
471
            ? $this->datetime_spaces[ $datetime_identifier ]
472
            : 0;
473
        // and bail if it is not
474
        if ( ! $reg_limit) {
475
            if ($this->debug) {
476
                \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__);
477
            }
478
            return;
479
        }
480 View Code Duplication
        if ($this->debug) {
481
            \EEH_Debug_Tools::printr($datetime_identifier, '* $datetime_identifier', __FILE__, __LINE__, 1);
482
            \EEH_Debug_Tools::printr("{$reg_limit}", 'REG LIMIT', __FILE__, __LINE__);
483
        }
484
        // number of allocated spaces always starts at zero
485
        $spaces_allocated                           = 0;
486
        $this->total_spaces[ $datetime_identifier ] = 0;
487
        foreach ($tickets as $ticket_identifier) {
488
            $spaces_allocated = $this->calculateAvailableSpacesForTicket(
489
                $datetime_identifier,
490
                $reg_limit,
491
                $ticket_identifier,
492
                $spaces_allocated
493
            );
494
        }
495
        // spaces can't be negative
496
        $spaces_allocated = max($spaces_allocated, 0);
497
        if ($spaces_allocated) {
498
            // track any non-zero values
499
            $this->total_spaces[ $datetime_identifier ] += $spaces_allocated;
500
            if ($this->debug) {
501
                \EEH_Debug_Tools::printr((string)$spaces_allocated, ' . $spaces_allocated: ', __FILE__, __LINE__);
502
            }
503
        } else {
504
            if ($this->debug) {
505
                \EEH_Debug_Tools::printr(' ', ' . NO TICKETS AVAILABLE FOR DATETIME', __FILE__, __LINE__);
506
            }
507
        }
508 View Code Duplication
        if ($this->debug) {
509
            \EEH_Debug_Tools::printr($this->total_spaces[ $datetime_identifier ], '$total_spaces', __FILE__,
510
                __LINE__);
511
            \EEH_Debug_Tools::printr($this->ticket_quantities, '$ticket_quantities', __FILE__, __LINE__);
512
            \EEH_Debug_Tools::printr($this->datetime_spaces, 'datetime_spaces', __FILE__, __LINE__);
513
        }
514
    }
515
516
517
518
    /**
519
     * @param string $datetime_identifier
520
     * @param int    $reg_limit
521
     * @param string $ticket_identifier
522
     * @param int    $spaces_allocated
523
     * @return int
524
     */
525
    private function calculateAvailableSpacesForTicket(
526
        $datetime_identifier,
527
        $reg_limit,
528
        $ticket_identifier,
529
        $spaces_allocated
530
    ) {
531
        // make sure ticket quantity is set
532
        $ticket_quantity = isset($this->ticket_quantities[ $ticket_identifier ])
533
            ? $this->ticket_quantities[ $ticket_identifier ]
534
            : 0;
535
        if ($this->debug) {
536
            \EEH_Debug_Tools::printr("{$spaces_allocated}", '$spaces_allocated', __FILE__, __LINE__);
537
            \EEH_Debug_Tools::printr("{$ticket_quantity}", "ticket $ticket_identifier quantity: ",
538
                __FILE__, __LINE__, 2);
539
        }
540
        if ($ticket_quantity) {
541
            if ($this->debug) {
542
                \EEH_Debug_Tools::printr(
543
                    ($spaces_allocated <= $reg_limit)
544
                        ? 'true'
545
                        : 'false',
546
                    ' . spaces_allocated <= reg_limit = ',
547
                    __FILE__, __LINE__
548
                );
549
            }
550
            // if the datetime is NOT at full capacity yet
551
            if ($spaces_allocated <= $reg_limit) {
552
                // then the maximum ticket quantity we can allocate is the lowest value of either:
553
                //  the number of remaining spaces for the datetime, which is the limit - spaces already taken
554
                //  or the maximum ticket quantity
555
                $ticket_quantity = min($reg_limit - $spaces_allocated, $ticket_quantity);
556
                // adjust the available quantity in our tracking array
557
                $this->ticket_quantities[ $ticket_identifier ] -= $ticket_quantity;
558
                // and increment spaces allocated for this datetime
559
                $spaces_allocated += $ticket_quantity;
560
                $at_capacity = $spaces_allocated >= $reg_limit;
561
                if ($this->debug) {
562
                    \EEH_Debug_Tools::printr("{$ticket_quantity} {$ticket_identifier} tickets", ' > > allocate ',
563
                        __FILE__, __LINE__,   3);
564
                    if ($at_capacity) {
565
                        \EEH_Debug_Tools::printr('AT CAPACITY', " . {$datetime_identifier}", __FILE__, __LINE__, 3);
566
                    }
567
                }
568
                // now adjust all other datetimes that allow access to this ticket
569
                $this->adjustDatetimes(
570
                    $datetime_identifier,
571
                    $ticket_identifier,
572
                    $ticket_quantity,
573
                    $at_capacity
574
                );
575
            }
576
        }
577
        return $spaces_allocated;
578
    }
579
580
581
582
    /**
583
     * subtracts ticket amounts from all datetime reg limits
584
     * that allow access to the ticket specified,
585
     * because that ticket could be used
586
     * to attend any of the datetimes it has access to
587
     *
588
     * @param string $datetime_identifier
589
     * @param string $ticket_identifier
590
     * @param bool   $at_capacity
591
     * @param int    $ticket_quantity
592
     */
593
    private function adjustDatetimes(
594
        $datetime_identifier,
595
        $ticket_identifier,
596
        $ticket_quantity,
597
        $at_capacity
598
    ) {
599
        /** @var array $datetime_tickets */
600
        foreach ($this->datetime_tickets as $datetime_ID => $datetime_tickets) {
601
            if ($datetime_ID !== $datetime_identifier || ! is_array($datetime_tickets)) {
602
                continue;
603
            }
604
            $adjusted = $this->adjustDatetimeSpaces(
605
                $datetime_ID,
606
                $ticket_identifier,
607
                $ticket_quantity
608
            );
609
            // skip to next ticket if nothing changed
610
            if (! ($adjusted || $at_capacity)) {
611
                continue;
612
            }
613
            // then all of it's tickets are now unavailable
614
            foreach ($datetime_tickets as $datetime_ticket) {
615
                if (
616
                    ($ticket_identifier === $datetime_ticket || $at_capacity)
617
                    && isset($this->ticket_quantities[ $datetime_ticket ])
618
                    && $this->ticket_quantities[ $datetime_ticket ] > 0
619
                ) {
620
                    if ($this->debug) {
621
                        \EEH_Debug_Tools::printr($datetime_ticket, ' . . . adjust ticket quantities for', __FILE__,
622
                            __LINE__);
623
                    }
624
                    // if this datetime is at full capacity, set any tracked available quantities to zero
625
                    // otherwise just subtract the ticket quantity
626
                    $new_quantity = $at_capacity
627
                        ? 0
628
                        : $this->ticket_quantities[ $datetime_ticket ] - $ticket_quantity;
629
                    // don't let ticket quantity go below zero
630
                    $this->ticket_quantities[ $datetime_ticket ] = max($new_quantity, 0);
631
                    if ($this->debug) {
632
                        \EEH_Debug_Tools::printr(
633
                            $at_capacity
634
                                ? "0 because Datetime {$datetime_identifier} is at capacity"
635
                                : "{$this->ticket_quantities[ $datetime_ticket ]}",
636
                            " . . . . {$datetime_ticket} quantity set to ",
637
                            __FILE__, __LINE__
638
                        );
639
                    }
640
                }
641
                // but we also need to adjust spaces for any other datetimes this ticket has access to
642
                if ($datetime_ticket === $ticket_identifier) {
643
                    if (isset($this->ticket_datetimes[ $datetime_ticket ])
644
                        && is_array($this->ticket_datetimes[ $datetime_ticket ])
645
                    ) {
646
                        if ($this->debug) {
647
                            \EEH_Debug_Tools::printr($datetime_ticket, ' . . adjust other Datetimes for', __FILE__,
648
                                __LINE__);
649
                        }
650
                        foreach ($this->ticket_datetimes[ $datetime_ticket ] as $datetime) {
651
                            // don't adjust the current datetime twice
652
                            if ($datetime !== $datetime_identifier) {
653
                                $this->adjustDatetimeSpaces(
654
                                    $datetime,
655
                                    $datetime_ticket,
656
                                    $ticket_quantity
657
                                );
658
                            }
659
                        }
660
                    }
661
                }
662
            }
663
        }
664
    }
665
666
    private function adjustDatetimeSpaces($datetime_identifier, $ticket_identifier, $ticket_quantity = 0)
667
    {
668
        // does datetime have spaces available?
669
        // and does the supplied ticket have access to this datetime ?
670
        if (
671
            $this->datetime_spaces[ $datetime_identifier ] > 0
672
            && isset($this->datetime_spaces[ $datetime_identifier ], $this->datetime_tickets[ $datetime_identifier ])
673
            && in_array($ticket_identifier, $this->datetime_tickets[ $datetime_identifier ], true)
674
            ) {
675 View Code Duplication
            if ($this->debug) {
676
                \EEH_Debug_Tools::printr($datetime_identifier, ' . . adjust Datetime Spaces for', __FILE__, __LINE__);
677
                \EEH_Debug_Tools::printr("{$this->datetime_spaces[ $datetime_identifier ]}", " . . current  {$datetime_identifier} spaces available", __FILE__, __LINE__);
678
            }
679
            // then decrement the available spaces for the datetime
680
            $this->datetime_spaces[ $datetime_identifier ] -= $ticket_quantity;
681
            // but don't let quantities go below zero
682
            $this->datetime_spaces[ $datetime_identifier ] = max(
683
                $this->datetime_spaces[ $datetime_identifier ],
684
                0
685
            );
686
            if ($this->debug) {
687
                \EEH_Debug_Tools::printr("{$ticket_quantity}",
688
                    " . . . {$datetime_identifier} capacity reduced by", __FILE__, __LINE__);
689
            }
690
            return true;
691
        }
692
        return false;
693
    }
694
695
}
696
// Location: EventSpacesCalculator.php
697