Completed
Branch BUG-10537-cart-ticket-reservat... (863a8b)
by
unknown
170:28 queued 158:59
created
modules/ticket_sales_monitor/EED_Ticket_Sales_Monitor.module.php 2 patches
Indentation   +946 added lines, -946 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@  discard block
 block discarded – undo
2 2
 use EventEspresso\core\exceptions\UnexpectedEntityException;
3 3
 
4 4
 if ( ! defined('EVENT_ESPRESSO_VERSION')) {
5
-    exit('No direct script access allowed');
5
+	exit('No direct script access allowed');
6 6
 }
7 7
 
8 8
 
@@ -21,952 +21,952 @@  discard block
 block discarded – undo
21 21
 class EED_Ticket_Sales_Monitor extends EED_Module
22 22
 {
23 23
 
24
-    const debug = false;    //	true false
25
-
26
-    /**
27
-     * an array of raw ticket data from EED_Ticket_Selector
28
-     *
29
-     * @var array $ticket_selections
30
-     */
31
-    protected $ticket_selections = array();
32
-
33
-    /**
34
-     * the raw ticket data from EED_Ticket_Selector is organized in rows
35
-     * according to how they are displayed in the actual Ticket_Selector
36
-     * this tracks the current row being processed
37
-     *
38
-     * @var int $current_row
39
-     */
40
-    protected $current_row = 0;
41
-
42
-    /**
43
-     * an array for tracking names of tickets that have sold out
44
-     *
45
-     * @var array $sold_out_tickets
46
-     */
47
-    protected $sold_out_tickets = array();
48
-
49
-    /**
50
-     * an array for tracking names of tickets that have had their quantities reduced
51
-     *
52
-     * @var array $decremented_tickets
53
-     */
54
-    protected $decremented_tickets = array();
55
-
56
-
57
-
58
-    /**
59
-     *    set_hooks - for hooking into EE Core, other modules, etc
60
-     *
61
-     * @access    public
62
-     * @return    void
63
-     */
64
-    public static function set_hooks()
65
-    {
66
-        // release tickets for expired carts
67
-        add_action('EED_Ticket_Selector__process_ticket_selections__before',
68
-            array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'),
69
-            1
70
-        );
71
-        // check ticket reserves AFTER MER does it's check (hence priority 20)
72
-        add_filter('FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty',
73
-            array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'),
74
-            20, 3
75
-        );
76
-        // add notices for sold out tickets
77
-        add_action('AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart',
78
-            array('EED_Ticket_Sales_Monitor', 'post_notices'),
79
-            10
80
-        );
81
-        // handle ticket quantities adjusted in cart
82
-        //add_action(
83
-        //	'FHEE__EED_Multi_Event_Registration__adjust_line_item_quantity__line_item_quantity_updated',
84
-        //	array( 'EED_Ticket_Sales_Monitor', 'ticket_quantity_updated' ),
85
-        //	10, 2
86
-        //);
87
-        // handle tickets deleted from cart
88
-        add_action(
89
-            'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart',
90
-            array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'),
91
-            10, 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, 1
98
-        );
99
-        add_action(
100
-            'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart',
101
-            array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
102
-            10, 1
103
-        );
104
-        // handle cancelled registrations
105
-        add_action(
106
-            'AHEE__EE_Session__reset_checkout__before_reset',
107
-            array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'),
108
-            10, 1
109
-        );
110
-        // cron tasks
111
-        add_action(
112
-            'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions__abandoned_transaction',
113
-            array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
114
-            10, 1
115
-        );
116
-        add_action(
117
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction',
118
-            array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
119
-            10, 1
120
-        );
121
-        add_action(
122
-            'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction',
123
-            array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'),
124
-            10, 1
125
-        );
126
-    }
127
-
128
-
129
-
130
-    /**
131
-     *    set_hooks_admin - for hooking into EE Admin Core, other modules, etc
132
-     *
133
-     * @access    public
134
-     * @return    void
135
-     */
136
-    public static function set_hooks_admin()
137
-    {
138
-        EED_Ticket_Sales_Monitor::set_hooks();
139
-    }
140
-
141
-
142
-
143
-    /**
144
-     * @return EED_Ticket_Sales_Monitor|EED_Module
145
-     */
146
-    public static function instance()
147
-    {
148
-        return parent::get_instance(__CLASS__);
149
-    }
150
-
151
-
152
-
153
-    /**
154
-     *    run
155
-     *
156
-     * @access    public
157
-     * @param WP_Query $WP_Query
158
-     * @return    void
159
-     */
160
-    public function run($WP_Query)
161
-    {
162
-    }
163
-
164
-
165
-
166
-    /********************************** PRE_TICKET_SALES  **********************************/
167
-
168
-
169
-
170
-    /**
171
-     * Retrieves grand totals from the line items that have no TXN ID
172
-     * and timestamps less than the current time minus the session lifespan.
173
-     * These are carts that have been abandoned before the "registrant" even attempted to checkout.
174
-     * We're going to release the tickets for these line items before attempting to add more to the cart.
175
-     *
176
-     * @return void
177
-     * @throws \EE_Error
178
-     */
179
-    public static function release_tickets_for_expired_carts()
180
-    {
181
-        $expired_ticket_IDs = array();
182
-        $valid_ticket_line_items = array();
183
-        $total_line_items = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction();
184
-        if(empty($total_line_items)){
185
-            return;
186
-        }
187
-        $expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan();
188
-        foreach ($total_line_items as $total_line_item) {
189
-            /** @var EE_Line_Item $total_line_item */
190
-            $ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item);
191
-            foreach ($ticket_line_items as $ticket_line_item) {
192
-                if(! $ticket_line_item instanceof EE_Line_Item) {
193
-                    continue;
194
-                }
195
-                if ($total_line_item->timestamp(true) <= $expired ) {
196
-                    $expired_ticket_IDs[$ticket_line_item->OBJ_ID()] = $ticket_line_item->OBJ_ID();
197
-                } else {
198
-                    $valid_ticket_line_items[$ticket_line_item->OBJ_ID()] = $ticket_line_item;
199
-                }
200
-            }
201
-        }
202
-        if (! empty($expired_ticket_IDs)) {
203
-            EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
204
-                \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
205
-                $valid_ticket_line_items
206
-            );
207
-            // let's get rid of expired line items so that they can't interfere with tracking
208
-            add_action(
209
-                'shutdown',
210
-                array('EED_Ticket_Sales_Monitor', 'clear_expired_line_items_with_no_transaction'),
211
-                999
212
-            );
213
-        }
214
-    }
215
-
216
-
217
-
218
-    /********************************** VALIDATE_TICKET_SALE  **********************************/
219
-
220
-
221
-
222
-    /**
223
-     *    validate_ticket_sales
224
-     *    callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data'
225
-     *
226
-     * @access    public
227
-     * @param int $qty
228
-     * @param \EE_Ticket $ticket
229
-     * @return bool
230
-     * @throws UnexpectedEntityException
231
-     * @throws EE_Error
232
-     */
233
-    public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket)
234
-    {
235
-        $qty = absint($qty);
236
-        if ($qty > 0) {
237
-            $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
238
-        }
239
-        if (self::debug) {
240
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()';
241
-            echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>';
242
-        }
243
-        return $qty;
244
-    }
245
-
246
-
247
-
248
-    /**
249
-     *    _validate_ticket_sale
250
-     * checks whether an individual ticket is available for purchase based on datetime, and ticket details
251
-     *
252
-     * @access    protected
253
-     * @param   \EE_Ticket $ticket
254
-     * @param int          $qty
255
-     * @return int
256
-     * @throws UnexpectedEntityException
257
-     * @throws EE_Error
258
-     */
259
-    protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
260
-    {
261
-        if (self::debug) {
262
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
263
-        }
264
-        if ( ! $ticket instanceof EE_Ticket) {
265
-            return 0;
266
-        }
267
-        if (self::debug) {
268
-            echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>';
269
-            echo '<br /> . original ticket->reserved: ' . $ticket->reserved();
270
-        }
271
-        $ticket->refresh_from_db();
272
-        // first let's determine the ticket availability based on sales
273
-        $available = $ticket->qty('saleable');
274
-        if (self::debug) {
275
-            echo '<br /> . . . ticket->qty: ' . $ticket->qty();
276
-            echo '<br /> . . . ticket->sold: ' . $ticket->sold();
277
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
278
-            echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
279
-            echo '<br /> . . . available: ' . $available;
280
-        }
281
-        if ($available < 1) {
282
-            $this->_ticket_sold_out($ticket);
283
-            return 0;
284
-        }
285
-        if (self::debug) {
286
-            echo '<br /> . . . qty: ' . $qty;
287
-        }
288
-        if ($available < $qty) {
289
-            $qty = $available;
290
-            if (self::debug) {
291
-                echo '<br /> . . . QTY ADJUSTED: ' . $qty;
292
-            }
293
-            $this->_ticket_quantity_decremented($ticket);
294
-        }
295
-        $this->_reserve_ticket($ticket, $qty);
296
-        return $qty;
297
-    }
298
-
299
-
300
-
301
-    /**
302
-     *  _reserve_ticket
303
-     *    increments ticket reserved based on quantity passed
304
-     *
305
-     * @access    protected
306
-     * @param    \EE_Ticket $ticket
307
-     * @param int           $quantity
308
-     * @return bool
309
-     * @throws EE_Error
310
-     */
311
-    protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
312
-    {
313
-        if (self::debug) {
314
-            echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity;
315
-        }
316
-        $ticket->increase_reserved($quantity);
317
-        return $ticket->save();
318
-    }
319
-
320
-
321
-
322
-    /**
323
-     * _release_reserved_ticket
324
-     *
325
-     * @access protected
326
-     * @param  EE_Ticket $ticket
327
-     * @param  int       $quantity
328
-     * @return bool
329
-     * @throws EE_Error
330
-     */
331
-    protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
332
-    {
333
-        if (self::debug) {
334
-            echo '<br /> . . . ticket->ID: ' . $ticket->ID();
335
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
336
-        }
337
-        $ticket->decrease_reserved($quantity);
338
-        if (self::debug) {
339
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
340
-        }
341
-        return $ticket->save() ? 1 : 0;
342
-    }
343
-
344
-
345
-
346
-    /**
347
-     *    _ticket_sold_out
348
-     *    removes quantities within the ticket selector based on zero ticket availability
349
-     *
350
-     * @access    protected
351
-     * @param    \EE_Ticket $ticket
352
-     * @return    void
353
-     * @throws UnexpectedEntityException
354
-     * @throws EE_Error
355
-     */
356
-    protected function _ticket_sold_out(EE_Ticket $ticket)
357
-    {
358
-        if (self::debug) {
359
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
360
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
361
-        }
362
-        $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
363
-    }
364
-
365
-
366
-
367
-    /**
368
-     *    _ticket_quantity_decremented
369
-     *    adjusts quantities within the ticket selector based on decreased ticket availability
370
-     *
371
-     * @access    protected
372
-     * @param    \EE_Ticket $ticket
373
-     * @return void
374
-     * @throws UnexpectedEntityException
375
-     * @throws EE_Error
376
-     */
377
-    protected function _ticket_quantity_decremented(EE_Ticket $ticket)
378
-    {
379
-        if (self::debug) {
380
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
381
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
382
-        }
383
-        $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
384
-    }
385
-
386
-
387
-
388
-    /**
389
-     *    _get_ticket_and_event_name
390
-     *    builds string out of ticket and event name
391
-     *
392
-     * @access    protected
393
-     * @param    \EE_Ticket $ticket
394
-     * @return string
395
-     * @throws UnexpectedEntityException
396
-     * @throws EE_Error
397
-     */
398
-    protected function _get_ticket_and_event_name(EE_Ticket $ticket)
399
-    {
400
-        $event = $ticket->get_related_event();
401
-        if ($event instanceof EE_Event) {
402
-            $ticket_name = sprintf(
403
-                _x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'),
404
-                $ticket->name(),
405
-                $event->name()
406
-            );
407
-        } else {
408
-            $ticket_name = $ticket->name();
409
-        }
410
-        return $ticket_name;
411
-    }
412
-
413
-
414
-
415
-    /********************************** EVENT CART  **********************************/
416
-
417
-
418
-
419
-    /**
420
-     * ticket_quantity_updated
421
-     * releases or reserves ticket(s) based on quantity passed
422
-     *
423
-     * @access public
424
-     * @param  EE_Line_Item $line_item
425
-     * @param  int          $quantity
426
-     * @return void
427
-     * @throws EE_Error
428
-     */
429
-    public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1)
430
-    {
431
-        $ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID()));
432
-        if ($ticket instanceof EE_Ticket) {
433
-            if ($quantity > 0) {
434
-                EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
435
-            } else {
436
-                EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
437
-            }
438
-        }
439
-    }
440
-
441
-
442
-
443
-    /**
444
-     * ticket_removed_from_cart
445
-     * releases reserved ticket(s) based on quantity passed
446
-     *
447
-     * @access public
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
-        EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
456
-    }
457
-
458
-
459
-
460
-    /********************************** POST_NOTICES  **********************************/
461
-
462
-
463
-
464
-    /**
465
-     *    post_notices
466
-     *
467
-     * @access    public
468
-     * @return    void
469
-     * @throws EE_Error
470
-     */
471
-    public static function post_notices()
472
-    {
473
-        EED_Ticket_Sales_Monitor::instance()->_post_notices();
474
-    }
475
-
476
-
477
-
478
-    /**
479
-     *    _post_notices
480
-     *
481
-     * @access    protected
482
-     * @return    void
483
-     * @throws EE_Error
484
-     */
485
-    protected function _post_notices()
486
-    {
487
-        if (self::debug) {
488
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
489
-        }
490
-        $refresh_msg = '';
491
-        $none_added_msg = '';
492
-        if (defined('DOING_AJAX') && DOING_AJAX) {
493
-            $refresh_msg = __('Please refresh the page to view updated ticket quantities.',
494
-                'event_espresso');
495
-            $none_added_msg = __('No tickets were added for the event.', 'event_espresso');
496
-        }
497
-        if ( ! empty($this->sold_out_tickets)) {
498
-            EE_Error::add_attention(
499
-                sprintf(
500
-                    apply_filters(
501
-                        'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice',
502
-                        __('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',
503
-                            'event_espresso')
504
-                    ),
505
-                    '<br />',
506
-                    implode('<br />', $this->sold_out_tickets),
507
-                    $none_added_msg,
508
-                    $refresh_msg
509
-                )
510
-            );
511
-            // alter code flow in the Ticket Selector for better UX
512
-            add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true');
513
-            add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false');
514
-            $this->sold_out_tickets = array();
515
-            // and reset the cart
516
-            EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
517
-        }
518
-        if ( ! empty($this->decremented_tickets)) {
519
-            EE_Error::add_attention(
520
-                sprintf(
521
-                    apply_filters(
522
-                        'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice',
523
-                        __('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',
524
-                            'event_espresso')
525
-                    ),
526
-                    '<br />',
527
-                    implode('<br />', $this->decremented_tickets),
528
-                    $none_added_msg,
529
-                    $refresh_msg
530
-                )
531
-            );
532
-            $this->decremented_tickets = array();
533
-        }
534
-    }
535
-
536
-
537
-
538
-    /********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION  **********************************/
539
-
540
-
541
-
542
-    /**
543
-     *    _release_all_reserved_tickets_for_transaction
544
-     *    releases reserved tickets for all registrations of an EE_Transaction
545
-     *    by default, will NOT release tickets for finalized transactions
546
-     *
547
-     * @access    protected
548
-     * @param    EE_Transaction $transaction
549
-     * @return int
550
-     * @throws EE_Error
551
-     */
552
-    protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
553
-    {
554
-        if (self::debug) {
555
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
556
-            echo '<br /> . transaction->ID: ' . $transaction->ID();
557
-        }
558
-        // check if 'finalize_registration' step has been completed...
559
-        $finalized = $transaction->reg_step_completed('finalize_registration');
560
-        if (self::debug) {
561
-            // DEBUG LOG
562
-            EEH_Debug_Tools::log(
563
-                __CLASS__, __FUNCTION__, __LINE__,
564
-                array('finalized' => $finalized),
565
-                false, 'EE_Transaction: ' . $transaction->ID()
566
-            );
567
-        }
568
-        // how many tickets were released
569
-        $count = 0;
570
-        if (self::debug) {
571
-            echo '<br /> . . . finalized: ' . $finalized;
572
-        }
573
-        $release_tickets_with_TXN_status = array(
574
-            EEM_Transaction::failed_status_code,
575
-            EEM_Transaction::abandoned_status_code,
576
-            EEM_Transaction::incomplete_status_code,
577
-        );
578
-        // if the session is getting cleared BEFORE the TXN has been finalized
579
-        if ( ! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
580
-            // let's cancel any reserved tickets
581
-            $registrations = $transaction->registrations();
582
-            if ( ! empty($registrations)) {
583
-                foreach ($registrations as $registration) {
584
-                    if ($registration instanceof EE_Registration) {
585
-                        $count += $this->_release_reserved_ticket_for_registration($registration, $transaction);
586
-                    }
587
-                }
588
-            }
589
-        }
590
-        return $count;
591
-    }
592
-
593
-
594
-
595
-    /**
596
-     *    _release_reserved_ticket_for_registration
597
-     *    releases reserved tickets for an EE_Registration
598
-     *    by default, will NOT release tickets for APPROVED registrations
599
-     *
600
-     * @access    protected
601
-     * @param    EE_Registration $registration
602
-     * @param    EE_Transaction  $transaction
603
-     * @return    int
604
-     * @throws    EE_Error
605
-     */
606
-    protected function _release_reserved_ticket_for_registration(
607
-        EE_Registration $registration,
608
-        EE_Transaction $transaction
609
-    ) {
610
-        $STS_ID = $transaction->status_ID();
611
-        if (self::debug) {
612
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
613
-            echo '<br /> . . registration->ID: ' . $registration->ID();
614
-            echo '<br /> . . registration->status_ID: ' . $registration->status_ID();
615
-            echo '<br /> . . transaction->status_ID(): ' . $STS_ID;
616
-        }
617
-        if (
618
-            // release Tickets for Failed Transactions and Abandoned Transactions
619
-            $STS_ID === EEM_Transaction::failed_status_code
620
-            || $STS_ID === EEM_Transaction::abandoned_status_code
621
-            || (
622
-                // also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved
623
-                $STS_ID === EEM_Transaction::incomplete_status_code
624
-                && $registration->status_ID() !== EEM_Registration::status_id_approved
625
-            )
626
-        ) {
627
-            $ticket = $registration->ticket();
628
-            if ($ticket instanceof EE_Ticket) {
629
-                return $this->_release_reserved_ticket($ticket);
630
-            }
631
-        }
632
-        return 0;
633
-    }
634
-
635
-
636
-
637
-    /********************************** SESSION_CART_RESET  **********************************/
638
-
639
-
640
-
641
-    /**
642
-     *    session_cart_reset
643
-     * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset'
644
-     *
645
-     * @access    public
646
-     * @param    EE_Session $session
647
-     * @return    void
648
-     * @throws EE_Error
649
-     */
650
-    public static function session_cart_reset(EE_Session $session)
651
-    {
652
-        if (self::debug) {
653
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
654
-        }
655
-        $cart = $session->cart();
656
-        if ($cart instanceof EE_Cart) {
657
-            if (self::debug) {
658
-                echo '<br /><br /> cart instance of EE_Cart: ';
659
-            }
660
-            EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart);
661
-        } else {
662
-            if (self::debug) {
663
-                echo '<br /><br /> invalid EE_Cart: ';
664
-                var_dump($cart);
665
-            }
666
-        }
667
-    }
668
-
669
-
670
-
671
-    /**
672
-     *    _session_cart_reset
673
-     * releases reserved tickets in the EE_Cart
674
-     *
675
-     * @access    protected
676
-     * @param    EE_Cart $cart
677
-     * @return    void
678
-     * @throws EE_Error
679
-     */
680
-    protected function _session_cart_reset(EE_Cart $cart)
681
-    {
682
-        if (self::debug) {
683
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
684
-        }
685
-        EE_Registry::instance()->load_helper('Line_Item');
686
-        $ticket_line_items = $cart->get_tickets();
687
-        if (empty($ticket_line_items)) {
688
-            return;
689
-        }
690
-        foreach ($ticket_line_items as $ticket_line_item) {
691
-            if (self::debug) {
692
-                echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID();
693
-            }
694
-            if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
695
-                if (self::debug) {
696
-                    echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
697
-                }
698
-                $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
699
-                if ($ticket instanceof EE_Ticket) {
700
-                    if (self::debug) {
701
-                        echo '<br /> . . ticket->ID(): ' . $ticket->ID();
702
-                        echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
703
-                    }
704
-                    $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
705
-                }
706
-            }
707
-        }
708
-        if (self::debug) {
709
-            echo '<br /><br /> RESET COMPLETED ';
710
-        }
711
-    }
712
-
713
-
714
-
715
-    /********************************** SESSION_CHECKOUT_RESET  **********************************/
716
-
717
-
718
-
719
-    /**
720
-     *    session_checkout_reset
721
-     * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset'
722
-     *
723
-     * @access    public
724
-     * @param    EE_Session $session
725
-     * @return    void
726
-     * @throws EE_Error
727
-     */
728
-    public static function session_checkout_reset(EE_Session $session)
729
-    {
730
-        $checkout = $session->checkout();
731
-        if ($checkout instanceof EE_Checkout) {
732
-            EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
733
-        }
734
-    }
735
-
736
-
737
-
738
-    /**
739
-     *    _session_checkout_reset
740
-     * releases reserved tickets for the EE_Checkout->transaction
741
-     *
742
-     * @access    protected
743
-     * @param    EE_Checkout $checkout
744
-     * @return    void
745
-     * @throws EE_Error
746
-     */
747
-    protected function _session_checkout_reset(EE_Checkout $checkout)
748
-    {
749
-        if (self::debug) {
750
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
751
-        }
752
-        // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
753
-        if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
754
-            return;
755
-        }
756
-        $this->_release_all_reserved_tickets_for_transaction($checkout->transaction);
757
-    }
758
-
759
-
760
-
761
-    /********************************** SESSION_EXPIRED_RESET  **********************************/
762
-
763
-
764
-
765
-    /**
766
-     *    session_expired_reset
767
-     *
768
-     * @access    public
769
-     * @param    EE_Session $session
770
-     * @return    void
771
-     */
772
-    public static function session_expired_reset(EE_Session $session)
773
-    {
774
-    }
775
-
776
-
777
-
778
-    /********************************** PROCESS_ABANDONED_TRANSACTIONS  **********************************/
779
-
780
-
781
-
782
-    /**
783
-     *    process_abandoned_transactions
784
-     *    releases reserved tickets for all registrations of an ABANDONED EE_Transaction
785
-     *    by default, will NOT release tickets for free transactions, or any that have received a payment
786
-     *
787
-     * @access    public
788
-     * @param    EE_Transaction $transaction
789
-     * @return    void
790
-     * @throws EE_Error
791
-     */
792
-    public static function process_abandoned_transactions(EE_Transaction $transaction)
793
-    {
794
-        // is this TXN free or has any money been paid towards this TXN? If so, then leave it alone
795
-        if ($transaction->is_free() || $transaction->paid() > 0) {
796
-            if (self::debug) {
797
-                // DEBUG LOG
798
-                EEH_Debug_Tools::log(
799
-                    __CLASS__, __FUNCTION__, __LINE__,
800
-                    array($transaction),
801
-                    false, 'EE_Transaction: ' . $transaction->ID()
802
-                );
803
-            }
804
-            return;
805
-        }
806
-        // have their been any successful payments made ?
807
-        $payments = $transaction->payments();
808
-        foreach ($payments as $payment) {
809
-            if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) {
810
-                if (self::debug) {
811
-                    // DEBUG LOG
812
-                    EEH_Debug_Tools::log(
813
-                        __CLASS__, __FUNCTION__, __LINE__,
814
-                        array($payment),
815
-                        false, 'EE_Transaction: ' . $transaction->ID()
816
-                    );
817
-                }
818
-                return;
819
-            }
820
-        }
821
-        // since you haven't even attempted to pay for your ticket...
822
-        EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
823
-    }
824
-
825
-
826
-
827
-    /********************************** PROCESS_FAILED_TRANSACTIONS  **********************************/
828
-
829
-
830
-
831
-    /**
832
-     *    process_abandoned_transactions
833
-     *    releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction
834
-     *
835
-     * @access    public
836
-     * @param    EE_Transaction $transaction
837
-     * @return    void
838
-     * @throws EE_Error
839
-     */
840
-    public static function process_failed_transactions(EE_Transaction $transaction)
841
-    {
842
-        // since you haven't even attempted to pay for your ticket...
843
-        EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
844
-    }
845
-
846
-
847
-
848
-    /********************************** RESET RESERVATION COUNTS  *********************************/
849
-
850
-
851
-
852
-    /**
853
-     * Resets all ticket and datetime reserved counts to zero
854
-     * Tickets that are currently associated with a Transaction that is in progress
855
-     *
856
-     * @throws \EE_Error
857
-     * @throws \DomainException
858
-     */
859
-    public static function reset_reservation_counts()
860
-    {
861
-        /** @var EE_Line_Item[] $valid_reserved_tickets */
862
-        $valid_reserved_tickets = array();
863
-        $transactions_in_progress = EEM_Transaction::instance()->get_transactions_in_progress();
864
-        foreach ($transactions_in_progress as $transaction_in_progress) {
865
-            // if this TXN has been fully completed, then skip it
866
-            if ($transaction_in_progress->reg_step_completed('finalize_registration')) {
867
-                continue;
868
-            }
869
-            /** @var EE_Transaction $transaction_in_progress */
870
-            $total_line_item = $transaction_in_progress->total_line_item();
871
-            // $transaction_in_progress->line
872
-            if (! $total_line_item instanceof EE_Line_Item) {
873
-                throw new DomainException(
874
-                    esc_html__('Transaction does not have a valid Total Line Item associated with it.', 'event_espresso')
875
-                );
876
-            }
877
-            $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
878
-                $total_line_item
879
-            );
880
-        }
881
-        $total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts();
882
-        foreach ($total_line_items as $total_line_item) {
883
-            $valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
884
-                $total_line_item
885
-            );
886
-        }
887
-        return EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
888
-            EEM_Ticket::instance()->get_tickets_with_reservations(),
889
-            $valid_reserved_tickets
890
-        );
891
-    }
892
-
893
-
894
-
895
-    /**
896
-     * @param EE_Line_Item $total_line_item
897
-     * @return EE_Line_Item[]
898
-     */
899
-    private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item)
900
-    {
901
-        /** @var EE_Line_Item[] $valid_reserved_tickets */
902
-        $valid_reserved_tickets = array();
903
-        $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
904
-        foreach ($ticket_line_items as $ticket_line_item) {
905
-            if ($ticket_line_item instanceof EE_Line_Item) {
906
-                $valid_reserved_tickets[] = $ticket_line_item;
907
-            }
908
-        }
909
-        return $valid_reserved_tickets;
910
-    }
911
-
912
-
913
-
914
-    /**
915
-     * @param EE_Ticket[] $tickets_with_reservations
916
-     * @param EE_Line_Item[] $valid_reserved_ticket_line_items
917
-     * @return int
918
-     * @throws \EE_Error
919
-     */
920
-    private static function release_reservations_for_tickets(
921
-        array $tickets_with_reservations,
922
-        $valid_reserved_ticket_line_items = array()
923
-    ) {
924
-        $total_tickets_released = 0;
925
-        foreach ($tickets_with_reservations as $ticket_with_reservations) {
926
-            if (! $ticket_with_reservations instanceof EE_Ticket) {
927
-                continue;
928
-            }
929
-            $reserved_qty = $ticket_with_reservations->reserved();
930
-            foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) {
931
-                if (
932
-                    $valid_reserved_ticket_line_item instanceof EE_Line_Item
933
-                    && $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID()
934
-                ) {
935
-                    $reserved_qty -= $valid_reserved_ticket_line_item->quantity();
936
-                }
937
-            }
938
-            if ($reserved_qty > 0) {
939
-                $ticket_with_reservations->decrease_reserved($reserved_qty);
940
-                $ticket_with_reservations->save();
941
-                $total_tickets_released += $reserved_qty;
942
-            }
943
-        }
944
-        return $total_tickets_released;
945
-    }
946
-
947
-
948
-
949
-    /********************************** SHUTDOWN  **********************************/
950
-
951
-
952
-
953
-    /**
954
-     * @return false|int
955
-     * @throws \EE_Error
956
-     */
957
-    public static function clear_expired_line_items_with_no_transaction()
958
-    {
959
-        /** @type WPDB $wpdb */
960
-        global $wpdb;
961
-        return $wpdb->query(
962
-            $wpdb->prepare(
963
-                'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
24
+	const debug = false;    //	true false
25
+
26
+	/**
27
+	 * an array of raw ticket data from EED_Ticket_Selector
28
+	 *
29
+	 * @var array $ticket_selections
30
+	 */
31
+	protected $ticket_selections = array();
32
+
33
+	/**
34
+	 * the raw ticket data from EED_Ticket_Selector is organized in rows
35
+	 * according to how they are displayed in the actual Ticket_Selector
36
+	 * this tracks the current row being processed
37
+	 *
38
+	 * @var int $current_row
39
+	 */
40
+	protected $current_row = 0;
41
+
42
+	/**
43
+	 * an array for tracking names of tickets that have sold out
44
+	 *
45
+	 * @var array $sold_out_tickets
46
+	 */
47
+	protected $sold_out_tickets = array();
48
+
49
+	/**
50
+	 * an array for tracking names of tickets that have had their quantities reduced
51
+	 *
52
+	 * @var array $decremented_tickets
53
+	 */
54
+	protected $decremented_tickets = array();
55
+
56
+
57
+
58
+	/**
59
+	 *    set_hooks - for hooking into EE Core, other modules, etc
60
+	 *
61
+	 * @access    public
62
+	 * @return    void
63
+	 */
64
+	public static function set_hooks()
65
+	{
66
+		// release tickets for expired carts
67
+		add_action('EED_Ticket_Selector__process_ticket_selections__before',
68
+			array('EED_Ticket_Sales_Monitor', 'release_tickets_for_expired_carts'),
69
+			1
70
+		);
71
+		// check ticket reserves AFTER MER does it's check (hence priority 20)
72
+		add_filter('FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty',
73
+			array('EED_Ticket_Sales_Monitor', 'validate_ticket_sale'),
74
+			20, 3
75
+		);
76
+		// add notices for sold out tickets
77
+		add_action('AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart',
78
+			array('EED_Ticket_Sales_Monitor', 'post_notices'),
79
+			10
80
+		);
81
+		// handle ticket quantities adjusted in cart
82
+		//add_action(
83
+		//	'FHEE__EED_Multi_Event_Registration__adjust_line_item_quantity__line_item_quantity_updated',
84
+		//	array( 'EED_Ticket_Sales_Monitor', 'ticket_quantity_updated' ),
85
+		//	10, 2
86
+		//);
87
+		// handle tickets deleted from cart
88
+		add_action(
89
+			'FHEE__EED_Multi_Event_Registration__delete_ticket__ticket_removed_from_cart',
90
+			array('EED_Ticket_Sales_Monitor', 'ticket_removed_from_cart'),
91
+			10, 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, 1
98
+		);
99
+		add_action(
100
+			'AHEE__EED_Multi_Event_Registration__empty_event_cart__before_delete_cart',
101
+			array('EED_Ticket_Sales_Monitor', 'session_cart_reset'),
102
+			10, 1
103
+		);
104
+		// handle cancelled registrations
105
+		add_action(
106
+			'AHEE__EE_Session__reset_checkout__before_reset',
107
+			array('EED_Ticket_Sales_Monitor', 'session_checkout_reset'),
108
+			10, 1
109
+		);
110
+		// cron tasks
111
+		add_action(
112
+			'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions__abandoned_transaction',
113
+			array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
114
+			10, 1
115
+		);
116
+		add_action(
117
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction',
118
+			array('EED_Ticket_Sales_Monitor', 'process_abandoned_transactions'),
119
+			10, 1
120
+		);
121
+		add_action(
122
+			'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction',
123
+			array('EED_Ticket_Sales_Monitor', 'process_failed_transactions'),
124
+			10, 1
125
+		);
126
+	}
127
+
128
+
129
+
130
+	/**
131
+	 *    set_hooks_admin - for hooking into EE Admin Core, other modules, etc
132
+	 *
133
+	 * @access    public
134
+	 * @return    void
135
+	 */
136
+	public static function set_hooks_admin()
137
+	{
138
+		EED_Ticket_Sales_Monitor::set_hooks();
139
+	}
140
+
141
+
142
+
143
+	/**
144
+	 * @return EED_Ticket_Sales_Monitor|EED_Module
145
+	 */
146
+	public static function instance()
147
+	{
148
+		return parent::get_instance(__CLASS__);
149
+	}
150
+
151
+
152
+
153
+	/**
154
+	 *    run
155
+	 *
156
+	 * @access    public
157
+	 * @param WP_Query $WP_Query
158
+	 * @return    void
159
+	 */
160
+	public function run($WP_Query)
161
+	{
162
+	}
163
+
164
+
165
+
166
+	/********************************** PRE_TICKET_SALES  **********************************/
167
+
168
+
169
+
170
+	/**
171
+	 * Retrieves grand totals from the line items that have no TXN ID
172
+	 * and timestamps less than the current time minus the session lifespan.
173
+	 * These are carts that have been abandoned before the "registrant" even attempted to checkout.
174
+	 * We're going to release the tickets for these line items before attempting to add more to the cart.
175
+	 *
176
+	 * @return void
177
+	 * @throws \EE_Error
178
+	 */
179
+	public static function release_tickets_for_expired_carts()
180
+	{
181
+		$expired_ticket_IDs = array();
182
+		$valid_ticket_line_items = array();
183
+		$total_line_items = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction();
184
+		if(empty($total_line_items)){
185
+			return;
186
+		}
187
+		$expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan();
188
+		foreach ($total_line_items as $total_line_item) {
189
+			/** @var EE_Line_Item $total_line_item */
190
+			$ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item);
191
+			foreach ($ticket_line_items as $ticket_line_item) {
192
+				if(! $ticket_line_item instanceof EE_Line_Item) {
193
+					continue;
194
+				}
195
+				if ($total_line_item->timestamp(true) <= $expired ) {
196
+					$expired_ticket_IDs[$ticket_line_item->OBJ_ID()] = $ticket_line_item->OBJ_ID();
197
+				} else {
198
+					$valid_ticket_line_items[$ticket_line_item->OBJ_ID()] = $ticket_line_item;
199
+				}
200
+			}
201
+		}
202
+		if (! empty($expired_ticket_IDs)) {
203
+			EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
204
+				\EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
205
+				$valid_ticket_line_items
206
+			);
207
+			// let's get rid of expired line items so that they can't interfere with tracking
208
+			add_action(
209
+				'shutdown',
210
+				array('EED_Ticket_Sales_Monitor', 'clear_expired_line_items_with_no_transaction'),
211
+				999
212
+			);
213
+		}
214
+	}
215
+
216
+
217
+
218
+	/********************************** VALIDATE_TICKET_SALE  **********************************/
219
+
220
+
221
+
222
+	/**
223
+	 *    validate_ticket_sales
224
+	 *    callback for 'FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data'
225
+	 *
226
+	 * @access    public
227
+	 * @param int $qty
228
+	 * @param \EE_Ticket $ticket
229
+	 * @return bool
230
+	 * @throws UnexpectedEntityException
231
+	 * @throws EE_Error
232
+	 */
233
+	public static function validate_ticket_sale($qty = 1, EE_Ticket $ticket)
234
+	{
235
+		$qty = absint($qty);
236
+		if ($qty > 0) {
237
+			$qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
238
+		}
239
+		if (self::debug) {
240
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()';
241
+			echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>';
242
+		}
243
+		return $qty;
244
+	}
245
+
246
+
247
+
248
+	/**
249
+	 *    _validate_ticket_sale
250
+	 * checks whether an individual ticket is available for purchase based on datetime, and ticket details
251
+	 *
252
+	 * @access    protected
253
+	 * @param   \EE_Ticket $ticket
254
+	 * @param int          $qty
255
+	 * @return int
256
+	 * @throws UnexpectedEntityException
257
+	 * @throws EE_Error
258
+	 */
259
+	protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
260
+	{
261
+		if (self::debug) {
262
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
263
+		}
264
+		if ( ! $ticket instanceof EE_Ticket) {
265
+			return 0;
266
+		}
267
+		if (self::debug) {
268
+			echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>';
269
+			echo '<br /> . original ticket->reserved: ' . $ticket->reserved();
270
+		}
271
+		$ticket->refresh_from_db();
272
+		// first let's determine the ticket availability based on sales
273
+		$available = $ticket->qty('saleable');
274
+		if (self::debug) {
275
+			echo '<br /> . . . ticket->qty: ' . $ticket->qty();
276
+			echo '<br /> . . . ticket->sold: ' . $ticket->sold();
277
+			echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
278
+			echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
279
+			echo '<br /> . . . available: ' . $available;
280
+		}
281
+		if ($available < 1) {
282
+			$this->_ticket_sold_out($ticket);
283
+			return 0;
284
+		}
285
+		if (self::debug) {
286
+			echo '<br /> . . . qty: ' . $qty;
287
+		}
288
+		if ($available < $qty) {
289
+			$qty = $available;
290
+			if (self::debug) {
291
+				echo '<br /> . . . QTY ADJUSTED: ' . $qty;
292
+			}
293
+			$this->_ticket_quantity_decremented($ticket);
294
+		}
295
+		$this->_reserve_ticket($ticket, $qty);
296
+		return $qty;
297
+	}
298
+
299
+
300
+
301
+	/**
302
+	 *  _reserve_ticket
303
+	 *    increments ticket reserved based on quantity passed
304
+	 *
305
+	 * @access    protected
306
+	 * @param    \EE_Ticket $ticket
307
+	 * @param int           $quantity
308
+	 * @return bool
309
+	 * @throws EE_Error
310
+	 */
311
+	protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
312
+	{
313
+		if (self::debug) {
314
+			echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity;
315
+		}
316
+		$ticket->increase_reserved($quantity);
317
+		return $ticket->save();
318
+	}
319
+
320
+
321
+
322
+	/**
323
+	 * _release_reserved_ticket
324
+	 *
325
+	 * @access protected
326
+	 * @param  EE_Ticket $ticket
327
+	 * @param  int       $quantity
328
+	 * @return bool
329
+	 * @throws EE_Error
330
+	 */
331
+	protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
332
+	{
333
+		if (self::debug) {
334
+			echo '<br /> . . . ticket->ID: ' . $ticket->ID();
335
+			echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
336
+		}
337
+		$ticket->decrease_reserved($quantity);
338
+		if (self::debug) {
339
+			echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
340
+		}
341
+		return $ticket->save() ? 1 : 0;
342
+	}
343
+
344
+
345
+
346
+	/**
347
+	 *    _ticket_sold_out
348
+	 *    removes quantities within the ticket selector based on zero ticket availability
349
+	 *
350
+	 * @access    protected
351
+	 * @param    \EE_Ticket $ticket
352
+	 * @return    void
353
+	 * @throws UnexpectedEntityException
354
+	 * @throws EE_Error
355
+	 */
356
+	protected function _ticket_sold_out(EE_Ticket $ticket)
357
+	{
358
+		if (self::debug) {
359
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
360
+			echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
361
+		}
362
+		$this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
363
+	}
364
+
365
+
366
+
367
+	/**
368
+	 *    _ticket_quantity_decremented
369
+	 *    adjusts quantities within the ticket selector based on decreased ticket availability
370
+	 *
371
+	 * @access    protected
372
+	 * @param    \EE_Ticket $ticket
373
+	 * @return void
374
+	 * @throws UnexpectedEntityException
375
+	 * @throws EE_Error
376
+	 */
377
+	protected function _ticket_quantity_decremented(EE_Ticket $ticket)
378
+	{
379
+		if (self::debug) {
380
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
381
+			echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
382
+		}
383
+		$this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
384
+	}
385
+
386
+
387
+
388
+	/**
389
+	 *    _get_ticket_and_event_name
390
+	 *    builds string out of ticket and event name
391
+	 *
392
+	 * @access    protected
393
+	 * @param    \EE_Ticket $ticket
394
+	 * @return string
395
+	 * @throws UnexpectedEntityException
396
+	 * @throws EE_Error
397
+	 */
398
+	protected function _get_ticket_and_event_name(EE_Ticket $ticket)
399
+	{
400
+		$event = $ticket->get_related_event();
401
+		if ($event instanceof EE_Event) {
402
+			$ticket_name = sprintf(
403
+				_x('%1$s for %2$s', 'ticket name for event name', 'event_espresso'),
404
+				$ticket->name(),
405
+				$event->name()
406
+			);
407
+		} else {
408
+			$ticket_name = $ticket->name();
409
+		}
410
+		return $ticket_name;
411
+	}
412
+
413
+
414
+
415
+	/********************************** EVENT CART  **********************************/
416
+
417
+
418
+
419
+	/**
420
+	 * ticket_quantity_updated
421
+	 * releases or reserves ticket(s) based on quantity passed
422
+	 *
423
+	 * @access public
424
+	 * @param  EE_Line_Item $line_item
425
+	 * @param  int          $quantity
426
+	 * @return void
427
+	 * @throws EE_Error
428
+	 */
429
+	public static function ticket_quantity_updated(EE_Line_Item $line_item, $quantity = 1)
430
+	{
431
+		$ticket = EEM_Ticket::instance()->get_one_by_ID(absint($line_item->OBJ_ID()));
432
+		if ($ticket instanceof EE_Ticket) {
433
+			if ($quantity > 0) {
434
+				EED_Ticket_Sales_Monitor::instance()->_reserve_ticket($ticket, $quantity);
435
+			} else {
436
+				EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
437
+			}
438
+		}
439
+	}
440
+
441
+
442
+
443
+	/**
444
+	 * ticket_removed_from_cart
445
+	 * releases reserved ticket(s) based on quantity passed
446
+	 *
447
+	 * @access public
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
+		EED_Ticket_Sales_Monitor::instance()->_release_reserved_ticket($ticket, $quantity);
456
+	}
457
+
458
+
459
+
460
+	/********************************** POST_NOTICES  **********************************/
461
+
462
+
463
+
464
+	/**
465
+	 *    post_notices
466
+	 *
467
+	 * @access    public
468
+	 * @return    void
469
+	 * @throws EE_Error
470
+	 */
471
+	public static function post_notices()
472
+	{
473
+		EED_Ticket_Sales_Monitor::instance()->_post_notices();
474
+	}
475
+
476
+
477
+
478
+	/**
479
+	 *    _post_notices
480
+	 *
481
+	 * @access    protected
482
+	 * @return    void
483
+	 * @throws EE_Error
484
+	 */
485
+	protected function _post_notices()
486
+	{
487
+		if (self::debug) {
488
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
489
+		}
490
+		$refresh_msg = '';
491
+		$none_added_msg = '';
492
+		if (defined('DOING_AJAX') && DOING_AJAX) {
493
+			$refresh_msg = __('Please refresh the page to view updated ticket quantities.',
494
+				'event_espresso');
495
+			$none_added_msg = __('No tickets were added for the event.', 'event_espresso');
496
+		}
497
+		if ( ! empty($this->sold_out_tickets)) {
498
+			EE_Error::add_attention(
499
+				sprintf(
500
+					apply_filters(
501
+						'FHEE__EED_Ticket_Sales_Monitor___post_notices__sold_out_tickets_notice',
502
+						__('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',
503
+							'event_espresso')
504
+					),
505
+					'<br />',
506
+					implode('<br />', $this->sold_out_tickets),
507
+					$none_added_msg,
508
+					$refresh_msg
509
+				)
510
+			);
511
+			// alter code flow in the Ticket Selector for better UX
512
+			add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', '__return_true');
513
+			add_filter('FHEE__EED_Ticket_Selector__process_ticket_selections__success', '__return_false');
514
+			$this->sold_out_tickets = array();
515
+			// and reset the cart
516
+			EED_Ticket_Sales_Monitor::session_cart_reset(EE_Registry::instance()->SSN);
517
+		}
518
+		if ( ! empty($this->decremented_tickets)) {
519
+			EE_Error::add_attention(
520
+				sprintf(
521
+					apply_filters(
522
+						'FHEE__EED_Ticket_Sales_Monitor___ticket_quantity_decremented__notice',
523
+						__('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',
524
+							'event_espresso')
525
+					),
526
+					'<br />',
527
+					implode('<br />', $this->decremented_tickets),
528
+					$none_added_msg,
529
+					$refresh_msg
530
+				)
531
+			);
532
+			$this->decremented_tickets = array();
533
+		}
534
+	}
535
+
536
+
537
+
538
+	/********************************** RELEASE_ALL_RESERVED_TICKETS_FOR_TRANSACTION  **********************************/
539
+
540
+
541
+
542
+	/**
543
+	 *    _release_all_reserved_tickets_for_transaction
544
+	 *    releases reserved tickets for all registrations of an EE_Transaction
545
+	 *    by default, will NOT release tickets for finalized transactions
546
+	 *
547
+	 * @access    protected
548
+	 * @param    EE_Transaction $transaction
549
+	 * @return int
550
+	 * @throws EE_Error
551
+	 */
552
+	protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
553
+	{
554
+		if (self::debug) {
555
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
556
+			echo '<br /> . transaction->ID: ' . $transaction->ID();
557
+		}
558
+		// check if 'finalize_registration' step has been completed...
559
+		$finalized = $transaction->reg_step_completed('finalize_registration');
560
+		if (self::debug) {
561
+			// DEBUG LOG
562
+			EEH_Debug_Tools::log(
563
+				__CLASS__, __FUNCTION__, __LINE__,
564
+				array('finalized' => $finalized),
565
+				false, 'EE_Transaction: ' . $transaction->ID()
566
+			);
567
+		}
568
+		// how many tickets were released
569
+		$count = 0;
570
+		if (self::debug) {
571
+			echo '<br /> . . . finalized: ' . $finalized;
572
+		}
573
+		$release_tickets_with_TXN_status = array(
574
+			EEM_Transaction::failed_status_code,
575
+			EEM_Transaction::abandoned_status_code,
576
+			EEM_Transaction::incomplete_status_code,
577
+		);
578
+		// if the session is getting cleared BEFORE the TXN has been finalized
579
+		if ( ! $finalized || in_array($transaction->status_ID(), $release_tickets_with_TXN_status, true)) {
580
+			// let's cancel any reserved tickets
581
+			$registrations = $transaction->registrations();
582
+			if ( ! empty($registrations)) {
583
+				foreach ($registrations as $registration) {
584
+					if ($registration instanceof EE_Registration) {
585
+						$count += $this->_release_reserved_ticket_for_registration($registration, $transaction);
586
+					}
587
+				}
588
+			}
589
+		}
590
+		return $count;
591
+	}
592
+
593
+
594
+
595
+	/**
596
+	 *    _release_reserved_ticket_for_registration
597
+	 *    releases reserved tickets for an EE_Registration
598
+	 *    by default, will NOT release tickets for APPROVED registrations
599
+	 *
600
+	 * @access    protected
601
+	 * @param    EE_Registration $registration
602
+	 * @param    EE_Transaction  $transaction
603
+	 * @return    int
604
+	 * @throws    EE_Error
605
+	 */
606
+	protected function _release_reserved_ticket_for_registration(
607
+		EE_Registration $registration,
608
+		EE_Transaction $transaction
609
+	) {
610
+		$STS_ID = $transaction->status_ID();
611
+		if (self::debug) {
612
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
613
+			echo '<br /> . . registration->ID: ' . $registration->ID();
614
+			echo '<br /> . . registration->status_ID: ' . $registration->status_ID();
615
+			echo '<br /> . . transaction->status_ID(): ' . $STS_ID;
616
+		}
617
+		if (
618
+			// release Tickets for Failed Transactions and Abandoned Transactions
619
+			$STS_ID === EEM_Transaction::failed_status_code
620
+			|| $STS_ID === EEM_Transaction::abandoned_status_code
621
+			|| (
622
+				// also release Tickets for Incomplete Transactions, but ONLY if the Registrations are NOT Approved
623
+				$STS_ID === EEM_Transaction::incomplete_status_code
624
+				&& $registration->status_ID() !== EEM_Registration::status_id_approved
625
+			)
626
+		) {
627
+			$ticket = $registration->ticket();
628
+			if ($ticket instanceof EE_Ticket) {
629
+				return $this->_release_reserved_ticket($ticket);
630
+			}
631
+		}
632
+		return 0;
633
+	}
634
+
635
+
636
+
637
+	/********************************** SESSION_CART_RESET  **********************************/
638
+
639
+
640
+
641
+	/**
642
+	 *    session_cart_reset
643
+	 * callback hooked into 'AHEE__EE_Session__reset_cart__before_reset'
644
+	 *
645
+	 * @access    public
646
+	 * @param    EE_Session $session
647
+	 * @return    void
648
+	 * @throws EE_Error
649
+	 */
650
+	public static function session_cart_reset(EE_Session $session)
651
+	{
652
+		if (self::debug) {
653
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
654
+		}
655
+		$cart = $session->cart();
656
+		if ($cart instanceof EE_Cart) {
657
+			if (self::debug) {
658
+				echo '<br /><br /> cart instance of EE_Cart: ';
659
+			}
660
+			EED_Ticket_Sales_Monitor::instance()->_session_cart_reset($cart);
661
+		} else {
662
+			if (self::debug) {
663
+				echo '<br /><br /> invalid EE_Cart: ';
664
+				var_dump($cart);
665
+			}
666
+		}
667
+	}
668
+
669
+
670
+
671
+	/**
672
+	 *    _session_cart_reset
673
+	 * releases reserved tickets in the EE_Cart
674
+	 *
675
+	 * @access    protected
676
+	 * @param    EE_Cart $cart
677
+	 * @return    void
678
+	 * @throws EE_Error
679
+	 */
680
+	protected function _session_cart_reset(EE_Cart $cart)
681
+	{
682
+		if (self::debug) {
683
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
684
+		}
685
+		EE_Registry::instance()->load_helper('Line_Item');
686
+		$ticket_line_items = $cart->get_tickets();
687
+		if (empty($ticket_line_items)) {
688
+			return;
689
+		}
690
+		foreach ($ticket_line_items as $ticket_line_item) {
691
+			if (self::debug) {
692
+				echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID();
693
+			}
694
+			if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
695
+				if (self::debug) {
696
+					echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
697
+				}
698
+				$ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
699
+				if ($ticket instanceof EE_Ticket) {
700
+					if (self::debug) {
701
+						echo '<br /> . . ticket->ID(): ' . $ticket->ID();
702
+						echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
703
+					}
704
+					$this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
705
+				}
706
+			}
707
+		}
708
+		if (self::debug) {
709
+			echo '<br /><br /> RESET COMPLETED ';
710
+		}
711
+	}
712
+
713
+
714
+
715
+	/********************************** SESSION_CHECKOUT_RESET  **********************************/
716
+
717
+
718
+
719
+	/**
720
+	 *    session_checkout_reset
721
+	 * callback hooked into 'AHEE__EE_Session__reset_checkout__before_reset'
722
+	 *
723
+	 * @access    public
724
+	 * @param    EE_Session $session
725
+	 * @return    void
726
+	 * @throws EE_Error
727
+	 */
728
+	public static function session_checkout_reset(EE_Session $session)
729
+	{
730
+		$checkout = $session->checkout();
731
+		if ($checkout instanceof EE_Checkout) {
732
+			EED_Ticket_Sales_Monitor::instance()->_session_checkout_reset($checkout);
733
+		}
734
+	}
735
+
736
+
737
+
738
+	/**
739
+	 *    _session_checkout_reset
740
+	 * releases reserved tickets for the EE_Checkout->transaction
741
+	 *
742
+	 * @access    protected
743
+	 * @param    EE_Checkout $checkout
744
+	 * @return    void
745
+	 * @throws EE_Error
746
+	 */
747
+	protected function _session_checkout_reset(EE_Checkout $checkout)
748
+	{
749
+		if (self::debug) {
750
+			echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
751
+		}
752
+		// we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
753
+		if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
754
+			return;
755
+		}
756
+		$this->_release_all_reserved_tickets_for_transaction($checkout->transaction);
757
+	}
758
+
759
+
760
+
761
+	/********************************** SESSION_EXPIRED_RESET  **********************************/
762
+
763
+
764
+
765
+	/**
766
+	 *    session_expired_reset
767
+	 *
768
+	 * @access    public
769
+	 * @param    EE_Session $session
770
+	 * @return    void
771
+	 */
772
+	public static function session_expired_reset(EE_Session $session)
773
+	{
774
+	}
775
+
776
+
777
+
778
+	/********************************** PROCESS_ABANDONED_TRANSACTIONS  **********************************/
779
+
780
+
781
+
782
+	/**
783
+	 *    process_abandoned_transactions
784
+	 *    releases reserved tickets for all registrations of an ABANDONED EE_Transaction
785
+	 *    by default, will NOT release tickets for free transactions, or any that have received a payment
786
+	 *
787
+	 * @access    public
788
+	 * @param    EE_Transaction $transaction
789
+	 * @return    void
790
+	 * @throws EE_Error
791
+	 */
792
+	public static function process_abandoned_transactions(EE_Transaction $transaction)
793
+	{
794
+		// is this TXN free or has any money been paid towards this TXN? If so, then leave it alone
795
+		if ($transaction->is_free() || $transaction->paid() > 0) {
796
+			if (self::debug) {
797
+				// DEBUG LOG
798
+				EEH_Debug_Tools::log(
799
+					__CLASS__, __FUNCTION__, __LINE__,
800
+					array($transaction),
801
+					false, 'EE_Transaction: ' . $transaction->ID()
802
+				);
803
+			}
804
+			return;
805
+		}
806
+		// have their been any successful payments made ?
807
+		$payments = $transaction->payments();
808
+		foreach ($payments as $payment) {
809
+			if ($payment instanceof EE_Payment && $payment->status() === EEM_Payment::status_id_approved) {
810
+				if (self::debug) {
811
+					// DEBUG LOG
812
+					EEH_Debug_Tools::log(
813
+						__CLASS__, __FUNCTION__, __LINE__,
814
+						array($payment),
815
+						false, 'EE_Transaction: ' . $transaction->ID()
816
+					);
817
+				}
818
+				return;
819
+			}
820
+		}
821
+		// since you haven't even attempted to pay for your ticket...
822
+		EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
823
+	}
824
+
825
+
826
+
827
+	/********************************** PROCESS_FAILED_TRANSACTIONS  **********************************/
828
+
829
+
830
+
831
+	/**
832
+	 *    process_abandoned_transactions
833
+	 *    releases reserved tickets for absolutely ALL registrations of a FAILED EE_Transaction
834
+	 *
835
+	 * @access    public
836
+	 * @param    EE_Transaction $transaction
837
+	 * @return    void
838
+	 * @throws EE_Error
839
+	 */
840
+	public static function process_failed_transactions(EE_Transaction $transaction)
841
+	{
842
+		// since you haven't even attempted to pay for your ticket...
843
+		EED_Ticket_Sales_Monitor::instance()->_release_all_reserved_tickets_for_transaction($transaction);
844
+	}
845
+
846
+
847
+
848
+	/********************************** RESET RESERVATION COUNTS  *********************************/
849
+
850
+
851
+
852
+	/**
853
+	 * Resets all ticket and datetime reserved counts to zero
854
+	 * Tickets that are currently associated with a Transaction that is in progress
855
+	 *
856
+	 * @throws \EE_Error
857
+	 * @throws \DomainException
858
+	 */
859
+	public static function reset_reservation_counts()
860
+	{
861
+		/** @var EE_Line_Item[] $valid_reserved_tickets */
862
+		$valid_reserved_tickets = array();
863
+		$transactions_in_progress = EEM_Transaction::instance()->get_transactions_in_progress();
864
+		foreach ($transactions_in_progress as $transaction_in_progress) {
865
+			// if this TXN has been fully completed, then skip it
866
+			if ($transaction_in_progress->reg_step_completed('finalize_registration')) {
867
+				continue;
868
+			}
869
+			/** @var EE_Transaction $transaction_in_progress */
870
+			$total_line_item = $transaction_in_progress->total_line_item();
871
+			// $transaction_in_progress->line
872
+			if (! $total_line_item instanceof EE_Line_Item) {
873
+				throw new DomainException(
874
+					esc_html__('Transaction does not have a valid Total Line Item associated with it.', 'event_espresso')
875
+				);
876
+			}
877
+			$valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
878
+				$total_line_item
879
+			);
880
+		}
881
+		$total_line_items = EEM_Line_Item::instance()->get_total_line_items_for_active_carts();
882
+		foreach ($total_line_items as $total_line_item) {
883
+			$valid_reserved_tickets += EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total(
884
+				$total_line_item
885
+			);
886
+		}
887
+		return EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
888
+			EEM_Ticket::instance()->get_tickets_with_reservations(),
889
+			$valid_reserved_tickets
890
+		);
891
+	}
892
+
893
+
894
+
895
+	/**
896
+	 * @param EE_Line_Item $total_line_item
897
+	 * @return EE_Line_Item[]
898
+	 */
899
+	private static function get_ticket_line_items_for_grand_total(EE_Line_Item $total_line_item)
900
+	{
901
+		/** @var EE_Line_Item[] $valid_reserved_tickets */
902
+		$valid_reserved_tickets = array();
903
+		$ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
904
+		foreach ($ticket_line_items as $ticket_line_item) {
905
+			if ($ticket_line_item instanceof EE_Line_Item) {
906
+				$valid_reserved_tickets[] = $ticket_line_item;
907
+			}
908
+		}
909
+		return $valid_reserved_tickets;
910
+	}
911
+
912
+
913
+
914
+	/**
915
+	 * @param EE_Ticket[] $tickets_with_reservations
916
+	 * @param EE_Line_Item[] $valid_reserved_ticket_line_items
917
+	 * @return int
918
+	 * @throws \EE_Error
919
+	 */
920
+	private static function release_reservations_for_tickets(
921
+		array $tickets_with_reservations,
922
+		$valid_reserved_ticket_line_items = array()
923
+	) {
924
+		$total_tickets_released = 0;
925
+		foreach ($tickets_with_reservations as $ticket_with_reservations) {
926
+			if (! $ticket_with_reservations instanceof EE_Ticket) {
927
+				continue;
928
+			}
929
+			$reserved_qty = $ticket_with_reservations->reserved();
930
+			foreach ($valid_reserved_ticket_line_items as $valid_reserved_ticket_line_item) {
931
+				if (
932
+					$valid_reserved_ticket_line_item instanceof EE_Line_Item
933
+					&& $valid_reserved_ticket_line_item->OBJ_ID() === $ticket_with_reservations->ID()
934
+				) {
935
+					$reserved_qty -= $valid_reserved_ticket_line_item->quantity();
936
+				}
937
+			}
938
+			if ($reserved_qty > 0) {
939
+				$ticket_with_reservations->decrease_reserved($reserved_qty);
940
+				$ticket_with_reservations->save();
941
+				$total_tickets_released += $reserved_qty;
942
+			}
943
+		}
944
+		return $total_tickets_released;
945
+	}
946
+
947
+
948
+
949
+	/********************************** SHUTDOWN  **********************************/
950
+
951
+
952
+
953
+	/**
954
+	 * @return false|int
955
+	 * @throws \EE_Error
956
+	 */
957
+	public static function clear_expired_line_items_with_no_transaction()
958
+	{
959
+		/** @type WPDB $wpdb */
960
+		global $wpdb;
961
+		return $wpdb->query(
962
+			$wpdb->prepare(
963
+				'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
964 964
                 WHERE TXN_ID = 0 AND LIN_timestamp <= %s',
965
-                // use GMT time because that's what LIN_timestamps are in
966
-                date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan())
967
-            )
968
-        );
969
-    }
965
+				// use GMT time because that's what LIN_timestamps are in
966
+				date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan())
967
+			)
968
+		);
969
+	}
970 970
 
971 971
 }
972 972
 // End of file EED_Ticket_Sales_Monitor.module.php
Please login to merge, or discard this patch.
Spacing   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
 class EED_Ticket_Sales_Monitor extends EED_Module
22 22
 {
23 23
 
24
-    const debug = false;    //	true false
24
+    const debug = false; //	true false
25 25
 
26 26
     /**
27 27
      * an array of raw ticket data from EED_Ticket_Selector
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
         $expired_ticket_IDs = array();
182 182
         $valid_ticket_line_items = array();
183 183
         $total_line_items = EEM_Line_Item::instance()->get_total_line_items_with_no_transaction();
184
-        if(empty($total_line_items)){
184
+        if (empty($total_line_items)) {
185 185
             return;
186 186
         }
187 187
         $expired = current_time('timestamp') - EE_Registry::instance()->SSN->lifespan();
@@ -189,17 +189,17 @@  discard block
 block discarded – undo
189 189
             /** @var EE_Line_Item $total_line_item */
190 190
             $ticket_line_items = EED_Ticket_Sales_Monitor::get_ticket_line_items_for_grand_total($total_line_item);
191 191
             foreach ($ticket_line_items as $ticket_line_item) {
192
-                if(! $ticket_line_item instanceof EE_Line_Item) {
192
+                if ( ! $ticket_line_item instanceof EE_Line_Item) {
193 193
                     continue;
194 194
                 }
195
-                if ($total_line_item->timestamp(true) <= $expired ) {
195
+                if ($total_line_item->timestamp(true) <= $expired) {
196 196
                     $expired_ticket_IDs[$ticket_line_item->OBJ_ID()] = $ticket_line_item->OBJ_ID();
197 197
                 } else {
198 198
                     $valid_ticket_line_items[$ticket_line_item->OBJ_ID()] = $ticket_line_item;
199 199
                 }
200 200
             }
201 201
         }
202
-        if (! empty($expired_ticket_IDs)) {
202
+        if ( ! empty($expired_ticket_IDs)) {
203 203
             EED_Ticket_Sales_Monitor::release_reservations_for_tickets(
204 204
                 \EEM_Ticket::instance()->get_tickets_with_IDs($expired_ticket_IDs),
205 205
                 $valid_ticket_line_items
@@ -237,8 +237,8 @@  discard block
 block discarded – undo
237 237
             $qty = EED_Ticket_Sales_Monitor::instance()->_validate_ticket_sale($ticket, $qty);
238 238
         }
239 239
         if (self::debug) {
240
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '()';
241
-            echo '<br /><br /><b> RETURNED QTY: ' . $qty . '</b>';
240
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'()';
241
+            echo '<br /><br /><b> RETURNED QTY: '.$qty.'</b>';
242 242
         }
243 243
         return $qty;
244 244
     }
@@ -259,36 +259,36 @@  discard block
 block discarded – undo
259 259
     protected function _validate_ticket_sale(EE_Ticket $ticket, $qty = 1)
260 260
     {
261 261
         if (self::debug) {
262
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
262
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
263 263
         }
264 264
         if ( ! $ticket instanceof EE_Ticket) {
265 265
             return 0;
266 266
         }
267 267
         if (self::debug) {
268
-            echo '<br /><b> . ticket->ID: ' . $ticket->ID() . '</b>';
269
-            echo '<br /> . original ticket->reserved: ' . $ticket->reserved();
268
+            echo '<br /><b> . ticket->ID: '.$ticket->ID().'</b>';
269
+            echo '<br /> . original ticket->reserved: '.$ticket->reserved();
270 270
         }
271 271
         $ticket->refresh_from_db();
272 272
         // first let's determine the ticket availability based on sales
273 273
         $available = $ticket->qty('saleable');
274 274
         if (self::debug) {
275
-            echo '<br /> . . . ticket->qty: ' . $ticket->qty();
276
-            echo '<br /> . . . ticket->sold: ' . $ticket->sold();
277
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
278
-            echo '<br /> . . . ticket->qty(saleable): ' . $ticket->qty('saleable');
279
-            echo '<br /> . . . available: ' . $available;
275
+            echo '<br /> . . . ticket->qty: '.$ticket->qty();
276
+            echo '<br /> . . . ticket->sold: '.$ticket->sold();
277
+            echo '<br /> . . . ticket->reserved: '.$ticket->reserved();
278
+            echo '<br /> . . . ticket->qty(saleable): '.$ticket->qty('saleable');
279
+            echo '<br /> . . . available: '.$available;
280 280
         }
281 281
         if ($available < 1) {
282 282
             $this->_ticket_sold_out($ticket);
283 283
             return 0;
284 284
         }
285 285
         if (self::debug) {
286
-            echo '<br /> . . . qty: ' . $qty;
286
+            echo '<br /> . . . qty: '.$qty;
287 287
         }
288 288
         if ($available < $qty) {
289 289
             $qty = $available;
290 290
             if (self::debug) {
291
-                echo '<br /> . . . QTY ADJUSTED: ' . $qty;
291
+                echo '<br /> . . . QTY ADJUSTED: '.$qty;
292 292
             }
293 293
             $this->_ticket_quantity_decremented($ticket);
294 294
         }
@@ -311,7 +311,7 @@  discard block
 block discarded – undo
311 311
     protected function _reserve_ticket(EE_Ticket $ticket, $quantity = 1)
312 312
     {
313 313
         if (self::debug) {
314
-            echo '<br /><br /> . . . INCREASE RESERVED: ' . $quantity;
314
+            echo '<br /><br /> . . . INCREASE RESERVED: '.$quantity;
315 315
         }
316 316
         $ticket->increase_reserved($quantity);
317 317
         return $ticket->save();
@@ -331,12 +331,12 @@  discard block
 block discarded – undo
331 331
     protected function _release_reserved_ticket(EE_Ticket $ticket, $quantity = 1)
332 332
     {
333 333
         if (self::debug) {
334
-            echo '<br /> . . . ticket->ID: ' . $ticket->ID();
335
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
334
+            echo '<br /> . . . ticket->ID: '.$ticket->ID();
335
+            echo '<br /> . . . ticket->reserved: '.$ticket->reserved();
336 336
         }
337 337
         $ticket->decrease_reserved($quantity);
338 338
         if (self::debug) {
339
-            echo '<br /> . . . ticket->reserved: ' . $ticket->reserved();
339
+            echo '<br /> . . . ticket->reserved: '.$ticket->reserved();
340 340
         }
341 341
         return $ticket->save() ? 1 : 0;
342 342
     }
@@ -356,8 +356,8 @@  discard block
 block discarded – undo
356 356
     protected function _ticket_sold_out(EE_Ticket $ticket)
357 357
     {
358 358
         if (self::debug) {
359
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
360
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
359
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
360
+            echo '<br /> . . ticket->name: '.$this->_get_ticket_and_event_name($ticket);
361 361
         }
362 362
         $this->sold_out_tickets[] = $this->_get_ticket_and_event_name($ticket);
363 363
     }
@@ -377,8 +377,8 @@  discard block
 block discarded – undo
377 377
     protected function _ticket_quantity_decremented(EE_Ticket $ticket)
378 378
     {
379 379
         if (self::debug) {
380
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
381
-            echo '<br /> . . ticket->name: ' . $this->_get_ticket_and_event_name($ticket);
380
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
381
+            echo '<br /> . . ticket->name: '.$this->_get_ticket_and_event_name($ticket);
382 382
         }
383 383
         $this->decremented_tickets[] = $this->_get_ticket_and_event_name($ticket);
384 384
     }
@@ -485,7 +485,7 @@  discard block
 block discarded – undo
485 485
     protected function _post_notices()
486 486
     {
487 487
         if (self::debug) {
488
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
488
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
489 489
         }
490 490
         $refresh_msg = '';
491 491
         $none_added_msg = '';
@@ -552,8 +552,8 @@  discard block
 block discarded – undo
552 552
     protected function _release_all_reserved_tickets_for_transaction(EE_Transaction $transaction)
553 553
     {
554 554
         if (self::debug) {
555
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
556
-            echo '<br /> . transaction->ID: ' . $transaction->ID();
555
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
556
+            echo '<br /> . transaction->ID: '.$transaction->ID();
557 557
         }
558 558
         // check if 'finalize_registration' step has been completed...
559 559
         $finalized = $transaction->reg_step_completed('finalize_registration');
@@ -562,13 +562,13 @@  discard block
 block discarded – undo
562 562
             EEH_Debug_Tools::log(
563 563
                 __CLASS__, __FUNCTION__, __LINE__,
564 564
                 array('finalized' => $finalized),
565
-                false, 'EE_Transaction: ' . $transaction->ID()
565
+                false, 'EE_Transaction: '.$transaction->ID()
566 566
             );
567 567
         }
568 568
         // how many tickets were released
569 569
         $count = 0;
570 570
         if (self::debug) {
571
-            echo '<br /> . . . finalized: ' . $finalized;
571
+            echo '<br /> . . . finalized: '.$finalized;
572 572
         }
573 573
         $release_tickets_with_TXN_status = array(
574 574
             EEM_Transaction::failed_status_code,
@@ -609,10 +609,10 @@  discard block
 block discarded – undo
609 609
     ) {
610 610
         $STS_ID = $transaction->status_ID();
611 611
         if (self::debug) {
612
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
613
-            echo '<br /> . . registration->ID: ' . $registration->ID();
614
-            echo '<br /> . . registration->status_ID: ' . $registration->status_ID();
615
-            echo '<br /> . . transaction->status_ID(): ' . $STS_ID;
612
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
613
+            echo '<br /> . . registration->ID: '.$registration->ID();
614
+            echo '<br /> . . registration->status_ID: '.$registration->status_ID();
615
+            echo '<br /> . . transaction->status_ID(): '.$STS_ID;
616 616
         }
617 617
         if (
618 618
             // release Tickets for Failed Transactions and Abandoned Transactions
@@ -650,7 +650,7 @@  discard block
 block discarded – undo
650 650
     public static function session_cart_reset(EE_Session $session)
651 651
     {
652 652
         if (self::debug) {
653
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
653
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
654 654
         }
655 655
         $cart = $session->cart();
656 656
         if ($cart instanceof EE_Cart) {
@@ -680,7 +680,7 @@  discard block
 block discarded – undo
680 680
     protected function _session_cart_reset(EE_Cart $cart)
681 681
     {
682 682
         if (self::debug) {
683
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
683
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
684 684
         }
685 685
         EE_Registry::instance()->load_helper('Line_Item');
686 686
         $ticket_line_items = $cart->get_tickets();
@@ -689,17 +689,17 @@  discard block
 block discarded – undo
689 689
         }
690 690
         foreach ($ticket_line_items as $ticket_line_item) {
691 691
             if (self::debug) {
692
-                echo '<br /> . ticket_line_item->ID(): ' . $ticket_line_item->ID();
692
+                echo '<br /> . ticket_line_item->ID(): '.$ticket_line_item->ID();
693 693
             }
694 694
             if ($ticket_line_item instanceof EE_Line_Item && $ticket_line_item->OBJ_type() === 'Ticket') {
695 695
                 if (self::debug) {
696
-                    echo '<br /> . . ticket_line_item->OBJ_ID(): ' . $ticket_line_item->OBJ_ID();
696
+                    echo '<br /> . . ticket_line_item->OBJ_ID(): '.$ticket_line_item->OBJ_ID();
697 697
                 }
698 698
                 $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_line_item->OBJ_ID());
699 699
                 if ($ticket instanceof EE_Ticket) {
700 700
                     if (self::debug) {
701
-                        echo '<br /> . . ticket->ID(): ' . $ticket->ID();
702
-                        echo '<br /> . . ticket_line_item->quantity(): ' . $ticket_line_item->quantity();
701
+                        echo '<br /> . . ticket->ID(): '.$ticket->ID();
702
+                        echo '<br /> . . ticket_line_item->quantity(): '.$ticket_line_item->quantity();
703 703
                     }
704 704
                     $this->_release_reserved_ticket($ticket, $ticket_line_item->quantity());
705 705
                 }
@@ -747,7 +747,7 @@  discard block
 block discarded – undo
747 747
     protected function _session_checkout_reset(EE_Checkout $checkout)
748 748
     {
749 749
         if (self::debug) {
750
-            echo '<br /><br /> ' . __LINE__ . ') ' . __METHOD__ . '() ';
750
+            echo '<br /><br /> '.__LINE__.') '.__METHOD__.'() ';
751 751
         }
752 752
         // we want to release the each registration's reserved tickets if the session was cleared, but not if this is a revisit
753 753
         if ($checkout->revisit || ! $checkout->transaction instanceof EE_Transaction) {
@@ -798,7 +798,7 @@  discard block
 block discarded – undo
798 798
                 EEH_Debug_Tools::log(
799 799
                     __CLASS__, __FUNCTION__, __LINE__,
800 800
                     array($transaction),
801
-                    false, 'EE_Transaction: ' . $transaction->ID()
801
+                    false, 'EE_Transaction: '.$transaction->ID()
802 802
                 );
803 803
             }
804 804
             return;
@@ -812,7 +812,7 @@  discard block
 block discarded – undo
812 812
                     EEH_Debug_Tools::log(
813 813
                         __CLASS__, __FUNCTION__, __LINE__,
814 814
                         array($payment),
815
-                        false, 'EE_Transaction: ' . $transaction->ID()
815
+                        false, 'EE_Transaction: '.$transaction->ID()
816 816
                     );
817 817
                 }
818 818
                 return;
@@ -869,7 +869,7 @@  discard block
 block discarded – undo
869 869
             /** @var EE_Transaction $transaction_in_progress */
870 870
             $total_line_item = $transaction_in_progress->total_line_item();
871 871
             // $transaction_in_progress->line
872
-            if (! $total_line_item instanceof EE_Line_Item) {
872
+            if ( ! $total_line_item instanceof EE_Line_Item) {
873 873
                 throw new DomainException(
874 874
                     esc_html__('Transaction does not have a valid Total Line Item associated with it.', 'event_espresso')
875 875
                 );
@@ -923,7 +923,7 @@  discard block
 block discarded – undo
923 923
     ) {
924 924
         $total_tickets_released = 0;
925 925
         foreach ($tickets_with_reservations as $ticket_with_reservations) {
926
-            if (! $ticket_with_reservations instanceof EE_Ticket) {
926
+            if ( ! $ticket_with_reservations instanceof EE_Ticket) {
927 927
                 continue;
928 928
             }
929 929
             $reserved_qty = $ticket_with_reservations->reserved();
@@ -960,7 +960,7 @@  discard block
 block discarded – undo
960 960
         global $wpdb;
961 961
         return $wpdb->query(
962 962
             $wpdb->prepare(
963
-                'DELETE FROM ' . EEM_Line_Item::instance()->table() . '
963
+                'DELETE FROM '.EEM_Line_Item::instance()->table().'
964 964
                 WHERE TXN_ID = 0 AND LIN_timestamp <= %s',
965 965
                 // use GMT time because that's what LIN_timestamps are in
966 966
                 date('Y-m-d H:i:s', time() - EE_Registry::instance()->SSN->lifespan())
Please login to merge, or discard this patch.