1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\admin\entities; |
4
|
|
|
|
5
|
|
|
use EE_Base_Class; |
6
|
|
|
use EE_Error; |
7
|
|
|
use EE_Price; |
8
|
|
|
use EE_Price_Type; |
9
|
|
|
use EE_Ticket; |
10
|
|
|
use EEM_Price; |
11
|
|
|
use EEM_Price_Type; |
12
|
|
|
use EventEspresso\core\exceptions\InvalidEntityException; |
13
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
14
|
|
|
use ReflectionException; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class DefaultPrices |
19
|
|
|
* Description |
20
|
|
|
* |
21
|
|
|
* @package EventEspresso\core\domain\services\admin\events\editor |
22
|
|
|
* @author Brent Christensen |
23
|
|
|
* @since $VID:$ |
24
|
|
|
*/ |
25
|
|
|
class DefaultPrices implements DefaultEntityGeneratorInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EEM_Price $price_model |
30
|
|
|
*/ |
31
|
|
|
protected $price_model; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var EEM_Price_Type $price_type_model |
35
|
|
|
*/ |
36
|
|
|
protected $price_type_model; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param EEM_Price $price_model |
41
|
|
|
* @param EEM_Price_Type $price_type_model |
42
|
|
|
*/ |
43
|
|
|
public function __construct(EEM_Price $price_model, EEM_Price_Type $price_type_model) |
44
|
|
|
{ |
45
|
|
|
$this->price_model = $price_model; |
46
|
|
|
$this->price_type_model = $price_type_model; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param EE_Ticket|EE_Base_Class $entity |
52
|
|
|
* @return EE_Price[] |
53
|
|
|
* @throws EE_Error |
54
|
|
|
* @throws InvalidInterfaceException |
55
|
|
|
* @throws ReflectionException |
56
|
|
|
* @since $VID:$ |
57
|
|
|
*/ |
58
|
|
|
public function create(EE_Base_Class $entity) |
59
|
|
|
{ |
60
|
|
|
if (! $entity instanceof EE_Ticket) { |
61
|
|
|
throw new InvalidEntityException($entity, 'EE_Ticket'); |
62
|
|
|
} |
63
|
|
|
$taxes = []; |
64
|
|
|
$new_prices = []; |
65
|
|
|
$is_free = true; |
66
|
|
|
$has_base_price = false; |
67
|
|
|
$default_prices = $this->price_model->get_all_default_prices(false, true); |
68
|
|
|
if (is_array($default_prices)) { |
69
|
|
|
foreach ($default_prices as $default_price) { |
70
|
|
|
if (! $default_price instanceof EE_Price) { |
71
|
|
|
throw new InvalidEntityException($default_price, 'EE_Price'); |
72
|
|
|
} |
73
|
|
|
// grab any taxes but don't do anything just yet |
74
|
|
|
if ($default_price->is_tax()) { |
75
|
|
|
$taxes[] = $default_price; |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
// duplicate the default price so that it does not get mutated |
79
|
|
|
/** @var EE_Price $default_price_clone */ |
80
|
|
|
$default_price_clone = clone $default_price; |
81
|
|
|
if (( |
82
|
|
|
// has non-zero base price |
83
|
|
|
$default_price_clone->is_base_price() |
84
|
|
|
&& $default_price_clone->amount() > 0 |
85
|
|
|
) ||( |
86
|
|
|
// or has fixed amount surcharge |
87
|
|
|
$default_price_clone->is_surcharge() |
88
|
|
|
&& ! $default_price_clone->is_percent() |
89
|
|
|
)) { |
90
|
|
|
$is_free = false; |
91
|
|
|
} |
92
|
|
|
$default_price_clone->set('PRC_ID', null); |
93
|
|
|
$default_price_clone->set('PRC_is_default', false); |
94
|
|
|
$default_price_clone->save(); |
95
|
|
|
$default_price_clone->_add_relation_to($entity, 'Ticket'); |
96
|
|
|
// verify that a base price has been set |
97
|
|
|
$has_base_price = $default_price_clone->is_base_price() ? true : $has_base_price; |
98
|
|
|
$new_prices[ $default_price_clone->ID() ] = $default_price_clone; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
if (! $is_free && ! empty($taxes)) { |
102
|
|
|
foreach ($taxes as $tax) { |
103
|
|
|
// assign taxes but don't duplicate them because they operate globally |
104
|
|
|
$entity->set_taxable(true); |
105
|
|
|
$tax->_add_relation_to($entity, 'Ticket'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
if (! $has_base_price) { |
109
|
|
|
$new_base_price = $this->createNewBasePrice($entity); |
110
|
|
|
$new_prices[ $new_base_price->ID() ] = $new_base_price; |
111
|
|
|
} |
112
|
|
|
$ticket_total = $entity->get_ticket_total_with_taxes(true); |
113
|
|
|
if ($ticket_total !== $entity->ticket_price()) { |
114
|
|
|
$entity->set_price($ticket_total); |
115
|
|
|
$entity->save(); |
116
|
|
|
} |
117
|
|
|
return $new_prices; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param EE_Ticket $ticket |
123
|
|
|
* @return EE_Price |
124
|
|
|
* @throws EE_Error |
125
|
|
|
* @throws ReflectionException |
126
|
|
|
* @since $VID:$ |
127
|
|
|
*/ |
128
|
|
|
protected function createNewBasePrice(EE_Ticket $ticket) |
129
|
|
|
{ |
130
|
|
|
$new_base_price = $this->price_model->get_new_price(); |
131
|
|
|
$base_price_type = $this->price_type_model->get_one([ |
132
|
|
|
[ |
133
|
|
|
'PBT_ID' => EEM_Price_Type::base_type_base_price |
134
|
|
|
] |
135
|
|
|
]); |
136
|
|
|
if (! $base_price_type instanceof EE_Price_Type) { |
137
|
|
|
throw new RuntimeException( |
138
|
|
|
esc_html__( |
139
|
|
|
'A valid base price type could not be retrieved from the database.', |
140
|
|
|
'event_espresso' |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
$new_base_price->set('PRT_ID', $base_price_type->ID()); |
145
|
|
|
$new_base_price->set('PRC_is_default', false); |
146
|
|
|
$new_base_price->save(); |
147
|
|
|
$new_base_price->_add_relation_to($ticket, 'Ticket'); |
148
|
|
|
return $new_base_price; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|