|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\domain\services\ticket; |
|
3
|
|
|
|
|
4
|
|
|
use EventEspresso\core\domain\services\DomainService; |
|
5
|
|
|
use EventEspresso\core\exceptions\EntityNotFoundException; |
|
6
|
|
|
|
|
7
|
|
|
if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
8
|
|
|
exit('No direct script access allowed'); |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class CancelTicketLineItemService |
|
15
|
|
|
* Decrements the quantity for the provided ticket line item, |
|
16
|
|
|
* then if it's new quantity is zero, |
|
17
|
|
|
* either cancels it altogether (by setting its type to EEM_Line_Item::type_cancellation) |
|
18
|
|
|
* or inserts a new line item with a type of EEM_Line_Item::type_cancellation |
|
19
|
|
|
* |
|
20
|
|
|
* @package Event Espresso |
|
21
|
|
|
* @author Brent Christensen |
|
22
|
|
|
* @since 4.9.1 |
|
23
|
|
|
*/ |
|
24
|
|
|
class CancelTicketLineItemService extends DomainService |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param \EE_Registration $registration |
|
30
|
|
|
* @param int $quantity |
|
31
|
|
|
* @return bool|int |
|
32
|
|
|
*/ |
|
33
|
|
|
public function forRegistration(\EE_Registration $registration,$quantity = 1) { |
|
34
|
|
|
return $this->cancel( |
|
35
|
|
|
$registration->transaction(), |
|
36
|
|
|
$registration->ticket(), |
|
37
|
|
|
$quantity, |
|
38
|
|
|
$registration->ticket_line_item() |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param \EE_Transaction $transaction |
|
45
|
|
|
* @param \EE_Ticket $ticket |
|
46
|
|
|
* @param int $quantity |
|
47
|
|
|
* @param \EE_Line_Item $ticket_line_item |
|
48
|
|
|
* @return bool|int |
|
49
|
|
|
*/ |
|
50
|
|
|
public function cancel( |
|
51
|
|
|
\EE_Transaction $transaction, |
|
52
|
|
|
\EE_Ticket $ticket, |
|
53
|
|
|
$quantity = 1, |
|
54
|
|
|
\EE_Line_Item $ticket_line_item = null |
|
55
|
|
|
) { |
|
56
|
|
|
$ticket_line_item = $ticket_line_item instanceof \EE_Line_Item |
|
57
|
|
|
? $ticket_line_item |
|
58
|
|
|
: $this->getTicketLineItem($transaction, $ticket); |
|
59
|
|
|
// first we need to decrement the ticket quantity |
|
60
|
|
|
\EEH_Line_Item::decrement_quantity($ticket_line_item, $quantity); |
|
61
|
|
|
// no tickets left for this line item ? |
|
62
|
|
|
if ((int)$ticket_line_item->quantity() === 0) { |
|
63
|
|
|
// then just set this line item as cancelled, save, and get out |
|
64
|
|
|
$ticket_line_item->set_type(\EEM_Line_Item::type_cancellation); |
|
65
|
|
|
$success = $ticket_line_item->save(); |
|
66
|
|
|
} else { |
|
67
|
|
|
// otherwise create a new cancelled line item, so that we have a record of the cancellation |
|
68
|
|
|
$items_subtotal = \EEH_Line_Item::get_pre_tax_subtotal( |
|
69
|
|
|
\EEH_Line_Item::get_event_line_item_for_ticket( |
|
70
|
|
|
$transaction->total_line_item(), |
|
71
|
|
|
$ticket |
|
72
|
|
|
) |
|
73
|
|
|
); |
|
74
|
|
|
$cancelled_line_item = \EE_Line_Item::new_instance( |
|
75
|
|
|
array( |
|
76
|
|
|
'LIN_name' => $ticket_line_item->name(), |
|
77
|
|
|
'LIN_desc' => sprintf( |
|
78
|
|
|
__('%1$s Cancelled: %2$s', 'event_espresso'), |
|
79
|
|
|
$ticket_line_item->desc(), |
|
80
|
|
|
date('Y-m-d h:i a') |
|
81
|
|
|
), |
|
82
|
|
|
'LIN_unit_price' => (float)$ticket_line_item->unit_price(), |
|
83
|
|
|
'LIN_quantity' => $quantity, |
|
84
|
|
|
'LIN_percent' => null, |
|
85
|
|
|
'LIN_is_taxable' => false, |
|
86
|
|
|
'LIN_order' => $items_subtotal instanceof \EE_Line_Item |
|
87
|
|
|
? count($items_subtotal->children()) |
|
88
|
|
|
: 0, |
|
89
|
|
|
'LIN_total' => (float)$ticket_line_item->unit_price(), |
|
90
|
|
|
'LIN_type' => \EEM_Line_Item::type_cancellation |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
$success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
|
94
|
|
|
} |
|
95
|
|
|
if ( ! $success) { |
|
96
|
|
|
throw new \RuntimeException( |
|
97
|
|
|
sprintf( |
|
98
|
|
|
__('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
|
99
|
|
|
$ticket_line_item->ID() |
|
100
|
|
|
) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
return $success; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param \EE_Transaction $transaction |
|
110
|
|
|
* @param \EE_Ticket $ticket |
|
111
|
|
|
* @return \EE_Line_Item |
|
112
|
|
|
* @throws EntityNotFoundException |
|
113
|
|
|
* @throws \EE_Error |
|
114
|
|
|
*/ |
|
115
|
|
|
protected static function getTicketLineItem(\EE_Transaction $transaction, \EE_Ticket $ticket) |
|
116
|
|
|
{ |
|
117
|
|
|
$line_item = null; |
|
118
|
|
|
$ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs( |
|
119
|
|
|
$transaction->total_line_item(), |
|
120
|
|
|
'Ticket', |
|
121
|
|
|
array($ticket->ID()) |
|
122
|
|
|
); |
|
123
|
|
View Code Duplication |
foreach ($ticket_line_items as $ticket_line_item) { |
|
124
|
|
|
if ( |
|
125
|
|
|
$ticket_line_item instanceof \EE_Line_Item |
|
126
|
|
|
&& $ticket_line_item->OBJ_type() === 'Ticket' |
|
127
|
|
|
&& $ticket_line_item->OBJ_ID() === $ticket->ID() |
|
128
|
|
|
) { |
|
129
|
|
|
$line_item = $ticket_line_item; |
|
130
|
|
|
break; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
View Code Duplication |
if ( ! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
134
|
|
|
throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID()); |
|
135
|
|
|
} |
|
136
|
|
|
return $line_item; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
// End of file CancelTicketLineItemService.php |
|
142
|
|
|
// Location: /CancelTicketLineItemService.php |