@@ -13,369 +13,369 @@ |
||
| 13 | 13 | class EEM_Price extends EEM_Soft_Delete_Base |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - // private instance of the EEM_Price object |
|
| 17 | - protected static $_instance; |
|
| 16 | + // private instance of the EEM_Price object |
|
| 17 | + protected static $_instance; |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * private constructor to prevent direct creation |
|
| 22 | - * |
|
| 23 | - * @Constructor |
|
| 24 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
| 25 | - * (and any incoming timezone data that gets saved). |
|
| 26 | - * Note this just sends the timezone info to the date time model field objects. |
|
| 27 | - * Default is NULL |
|
| 28 | - * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 29 | - */ |
|
| 30 | - protected function __construct($timezone) |
|
| 31 | - { |
|
| 32 | - require_once(EE_MODELS . 'EEM_Price_Type.model.php'); |
|
| 33 | - $this->singular_item = __('Price', 'event_espresso'); |
|
| 34 | - $this->plural_item = __('Prices', 'event_espresso'); |
|
| 20 | + /** |
|
| 21 | + * private constructor to prevent direct creation |
|
| 22 | + * |
|
| 23 | + * @Constructor |
|
| 24 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
| 25 | + * (and any incoming timezone data that gets saved). |
|
| 26 | + * Note this just sends the timezone info to the date time model field objects. |
|
| 27 | + * Default is NULL |
|
| 28 | + * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 29 | + */ |
|
| 30 | + protected function __construct($timezone) |
|
| 31 | + { |
|
| 32 | + require_once(EE_MODELS . 'EEM_Price_Type.model.php'); |
|
| 33 | + $this->singular_item = __('Price', 'event_espresso'); |
|
| 34 | + $this->plural_item = __('Prices', 'event_espresso'); |
|
| 35 | 35 | |
| 36 | - $this->_tables = [ |
|
| 37 | - 'Price' => new EE_Primary_Table('esp_price', 'PRC_ID'), |
|
| 38 | - ]; |
|
| 39 | - $this->_fields = [ |
|
| 40 | - 'Price' => [ |
|
| 41 | - 'PRC_ID' => new EE_Primary_Key_Int_Field( |
|
| 42 | - 'PRC_ID', |
|
| 43 | - 'Price ID' |
|
| 44 | - ), |
|
| 45 | - 'PRT_ID' => new EE_Foreign_Key_Int_Field( |
|
| 46 | - 'PRT_ID', |
|
| 47 | - esc_html__('Price type Id', 'event_espresso'), |
|
| 48 | - false, |
|
| 49 | - null, |
|
| 50 | - 'Price_Type' |
|
| 51 | - ), |
|
| 52 | - 'PRC_amount' => new EE_Money_Field( |
|
| 53 | - 'PRC_amount', |
|
| 54 | - esc_html__('Price Amount', 'event_espresso'), |
|
| 55 | - false, |
|
| 56 | - 0 |
|
| 57 | - ), |
|
| 58 | - 'PRC_name' => new EE_Plain_Text_Field( |
|
| 59 | - 'PRC_name', |
|
| 60 | - esc_html__('Name of Price', 'event_espresso'), |
|
| 61 | - false, |
|
| 62 | - '' |
|
| 63 | - ), |
|
| 64 | - 'PRC_desc' => new EE_Post_Content_Field( |
|
| 65 | - 'PRC_desc', |
|
| 66 | - esc_html__('Price Description', 'event_espresso'), |
|
| 67 | - false, |
|
| 68 | - '' |
|
| 69 | - ), |
|
| 70 | - 'PRC_is_default' => new EE_Boolean_Field( |
|
| 71 | - 'PRC_is_default', |
|
| 72 | - esc_html__('Flag indicating whether price is a default price', 'event_espresso'), |
|
| 73 | - false, |
|
| 74 | - false |
|
| 75 | - ), |
|
| 76 | - 'PRC_overrides' => new EE_Integer_Field( |
|
| 77 | - 'PRC_overrides', |
|
| 78 | - esc_html__( |
|
| 79 | - 'Price ID for a global Price that will be overridden by this Price ( for replacing default prices )', |
|
| 80 | - 'event_espresso' |
|
| 81 | - ), |
|
| 82 | - true, |
|
| 83 | - 0 |
|
| 84 | - ), |
|
| 85 | - 'PRC_order' => new EE_Integer_Field( |
|
| 86 | - 'PRC_order', |
|
| 87 | - esc_html__( |
|
| 88 | - 'Order of Application of Price (lower numbers apply first?)', |
|
| 89 | - 'event_espresso' |
|
| 90 | - ), |
|
| 91 | - false, |
|
| 92 | - 1 |
|
| 93 | - ), |
|
| 94 | - 'PRC_deleted' => new EE_Trashed_Flag_Field( |
|
| 95 | - 'PRC_deleted', |
|
| 96 | - esc_html__('Flag Indicating if this has been deleted or not', 'event_espresso'), |
|
| 97 | - false, |
|
| 98 | - false |
|
| 99 | - ), |
|
| 100 | - 'PRC_parent' => new EE_Integer_Field( |
|
| 101 | - 'PRC_parent', |
|
| 102 | - esc_html__('Indicates what PRC_ID is the parent of this PRC_ID', 'event_espresso'), |
|
| 103 | - true, |
|
| 104 | - 0 |
|
| 105 | - ), |
|
| 106 | - 'PRC_wp_user' => new EE_WP_User_Field( |
|
| 107 | - 'PRC_wp_user', |
|
| 108 | - esc_html__('Price Creator ID', 'event_espresso'), |
|
| 109 | - false |
|
| 110 | - ), |
|
| 111 | - ], |
|
| 112 | - ]; |
|
| 113 | - $this->_model_relations = [ |
|
| 114 | - 'Ticket' => new EE_HABTM_Relation('Ticket_Price'), |
|
| 115 | - 'Price_Type' => new EE_Belongs_To_Relation(), |
|
| 116 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
| 117 | - ]; |
|
| 118 | - // this model is generally available for reading |
|
| 119 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = |
|
| 120 | - new EE_Restriction_Generator_Default_Public( |
|
| 121 | - 'PRC_is_default', |
|
| 122 | - 'Ticket.Datetime.Event' |
|
| 123 | - ); |
|
| 124 | - // account for default tickets in the caps |
|
| 125 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
| 126 | - new EE_Restriction_Generator_Default_Protected( |
|
| 127 | - 'PRC_is_default', |
|
| 128 | - 'Ticket.Datetime.Event' |
|
| 129 | - ); |
|
| 130 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
| 131 | - new EE_Restriction_Generator_Default_Protected( |
|
| 132 | - 'PRC_is_default', |
|
| 133 | - 'Ticket.Datetime.Event' |
|
| 134 | - ); |
|
| 135 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
| 136 | - new EE_Restriction_Generator_Default_Protected( |
|
| 137 | - 'PRC_is_default', |
|
| 138 | - 'Ticket.Datetime.Event' |
|
| 139 | - ); |
|
| 140 | - parent::__construct($timezone); |
|
| 141 | - } |
|
| 36 | + $this->_tables = [ |
|
| 37 | + 'Price' => new EE_Primary_Table('esp_price', 'PRC_ID'), |
|
| 38 | + ]; |
|
| 39 | + $this->_fields = [ |
|
| 40 | + 'Price' => [ |
|
| 41 | + 'PRC_ID' => new EE_Primary_Key_Int_Field( |
|
| 42 | + 'PRC_ID', |
|
| 43 | + 'Price ID' |
|
| 44 | + ), |
|
| 45 | + 'PRT_ID' => new EE_Foreign_Key_Int_Field( |
|
| 46 | + 'PRT_ID', |
|
| 47 | + esc_html__('Price type Id', 'event_espresso'), |
|
| 48 | + false, |
|
| 49 | + null, |
|
| 50 | + 'Price_Type' |
|
| 51 | + ), |
|
| 52 | + 'PRC_amount' => new EE_Money_Field( |
|
| 53 | + 'PRC_amount', |
|
| 54 | + esc_html__('Price Amount', 'event_espresso'), |
|
| 55 | + false, |
|
| 56 | + 0 |
|
| 57 | + ), |
|
| 58 | + 'PRC_name' => new EE_Plain_Text_Field( |
|
| 59 | + 'PRC_name', |
|
| 60 | + esc_html__('Name of Price', 'event_espresso'), |
|
| 61 | + false, |
|
| 62 | + '' |
|
| 63 | + ), |
|
| 64 | + 'PRC_desc' => new EE_Post_Content_Field( |
|
| 65 | + 'PRC_desc', |
|
| 66 | + esc_html__('Price Description', 'event_espresso'), |
|
| 67 | + false, |
|
| 68 | + '' |
|
| 69 | + ), |
|
| 70 | + 'PRC_is_default' => new EE_Boolean_Field( |
|
| 71 | + 'PRC_is_default', |
|
| 72 | + esc_html__('Flag indicating whether price is a default price', 'event_espresso'), |
|
| 73 | + false, |
|
| 74 | + false |
|
| 75 | + ), |
|
| 76 | + 'PRC_overrides' => new EE_Integer_Field( |
|
| 77 | + 'PRC_overrides', |
|
| 78 | + esc_html__( |
|
| 79 | + 'Price ID for a global Price that will be overridden by this Price ( for replacing default prices )', |
|
| 80 | + 'event_espresso' |
|
| 81 | + ), |
|
| 82 | + true, |
|
| 83 | + 0 |
|
| 84 | + ), |
|
| 85 | + 'PRC_order' => new EE_Integer_Field( |
|
| 86 | + 'PRC_order', |
|
| 87 | + esc_html__( |
|
| 88 | + 'Order of Application of Price (lower numbers apply first?)', |
|
| 89 | + 'event_espresso' |
|
| 90 | + ), |
|
| 91 | + false, |
|
| 92 | + 1 |
|
| 93 | + ), |
|
| 94 | + 'PRC_deleted' => new EE_Trashed_Flag_Field( |
|
| 95 | + 'PRC_deleted', |
|
| 96 | + esc_html__('Flag Indicating if this has been deleted or not', 'event_espresso'), |
|
| 97 | + false, |
|
| 98 | + false |
|
| 99 | + ), |
|
| 100 | + 'PRC_parent' => new EE_Integer_Field( |
|
| 101 | + 'PRC_parent', |
|
| 102 | + esc_html__('Indicates what PRC_ID is the parent of this PRC_ID', 'event_espresso'), |
|
| 103 | + true, |
|
| 104 | + 0 |
|
| 105 | + ), |
|
| 106 | + 'PRC_wp_user' => new EE_WP_User_Field( |
|
| 107 | + 'PRC_wp_user', |
|
| 108 | + esc_html__('Price Creator ID', 'event_espresso'), |
|
| 109 | + false |
|
| 110 | + ), |
|
| 111 | + ], |
|
| 112 | + ]; |
|
| 113 | + $this->_model_relations = [ |
|
| 114 | + 'Ticket' => new EE_HABTM_Relation('Ticket_Price'), |
|
| 115 | + 'Price_Type' => new EE_Belongs_To_Relation(), |
|
| 116 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
| 117 | + ]; |
|
| 118 | + // this model is generally available for reading |
|
| 119 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = |
|
| 120 | + new EE_Restriction_Generator_Default_Public( |
|
| 121 | + 'PRC_is_default', |
|
| 122 | + 'Ticket.Datetime.Event' |
|
| 123 | + ); |
|
| 124 | + // account for default tickets in the caps |
|
| 125 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
| 126 | + new EE_Restriction_Generator_Default_Protected( |
|
| 127 | + 'PRC_is_default', |
|
| 128 | + 'Ticket.Datetime.Event' |
|
| 129 | + ); |
|
| 130 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
| 131 | + new EE_Restriction_Generator_Default_Protected( |
|
| 132 | + 'PRC_is_default', |
|
| 133 | + 'Ticket.Datetime.Event' |
|
| 134 | + ); |
|
| 135 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
| 136 | + new EE_Restriction_Generator_Default_Protected( |
|
| 137 | + 'PRC_is_default', |
|
| 138 | + 'Ticket.Datetime.Event' |
|
| 139 | + ); |
|
| 140 | + parent::__construct($timezone); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * instantiate a new price object with blank/empty properties |
|
| 146 | - * |
|
| 147 | - * @return mixed array on success, FALSE on fail |
|
| 148 | - */ |
|
| 149 | - public function get_new_price() |
|
| 150 | - { |
|
| 151 | - return $this->create_default_object(); |
|
| 152 | - } |
|
| 144 | + /** |
|
| 145 | + * instantiate a new price object with blank/empty properties |
|
| 146 | + * |
|
| 147 | + * @return mixed array on success, FALSE on fail |
|
| 148 | + */ |
|
| 149 | + public function get_new_price() |
|
| 150 | + { |
|
| 151 | + return $this->create_default_object(); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * retrieve ALL prices from db |
|
| 157 | - * |
|
| 158 | - * @return EE_Base_Class[]|EE_PRice[] |
|
| 159 | - * @throws EE_Error |
|
| 160 | - */ |
|
| 161 | - public function get_all_prices() |
|
| 162 | - { |
|
| 163 | - // retrieve all prices |
|
| 164 | - return $this->get_all(['order_by' => ['PRC_amount' => 'ASC']]); |
|
| 165 | - } |
|
| 155 | + /** |
|
| 156 | + * retrieve ALL prices from db |
|
| 157 | + * |
|
| 158 | + * @return EE_Base_Class[]|EE_PRice[] |
|
| 159 | + * @throws EE_Error |
|
| 160 | + */ |
|
| 161 | + public function get_all_prices() |
|
| 162 | + { |
|
| 163 | + // retrieve all prices |
|
| 164 | + return $this->get_all(['order_by' => ['PRC_amount' => 'ASC']]); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * retrieve all active prices for a particular event |
|
| 170 | - * |
|
| 171 | - * @param int $EVT_ID |
|
| 172 | - * @return array on success |
|
| 173 | - * @throws EE_Error |
|
| 174 | - */ |
|
| 175 | - public function get_all_event_prices($EVT_ID = 0) |
|
| 176 | - { |
|
| 177 | - return $this->get_all( |
|
| 178 | - [ |
|
| 179 | - [ |
|
| 180 | - 'EVT_ID' => $EVT_ID, |
|
| 181 | - 'Price_Type.PBT_ID' => ['!=', EEM_Price_Type::base_type_tax], |
|
| 182 | - ], |
|
| 183 | - 'order_by' => $this->_order_by_array_for_get_all_method(), |
|
| 184 | - ] |
|
| 185 | - ); |
|
| 186 | - } |
|
| 168 | + /** |
|
| 169 | + * retrieve all active prices for a particular event |
|
| 170 | + * |
|
| 171 | + * @param int $EVT_ID |
|
| 172 | + * @return array on success |
|
| 173 | + * @throws EE_Error |
|
| 174 | + */ |
|
| 175 | + public function get_all_event_prices($EVT_ID = 0) |
|
| 176 | + { |
|
| 177 | + return $this->get_all( |
|
| 178 | + [ |
|
| 179 | + [ |
|
| 180 | + 'EVT_ID' => $EVT_ID, |
|
| 181 | + 'Price_Type.PBT_ID' => ['!=', EEM_Price_Type::base_type_tax], |
|
| 182 | + ], |
|
| 183 | + 'order_by' => $this->_order_by_array_for_get_all_method(), |
|
| 184 | + ] |
|
| 185 | + ); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * retrieve all active global prices (that are not taxes (PBT_ID=4)) for a particular event |
|
| 191 | - * |
|
| 192 | - * @param boolean $count return count |
|
| 193 | - * @param bool $include_taxes |
|
| 194 | - * @return bool|EE_Base_Class[]|EE_PRice[] |
|
| 195 | - * @throws EE_Error |
|
| 196 | - */ |
|
| 197 | - public function get_all_default_prices($count = false, $include_taxes = false) |
|
| 198 | - { |
|
| 199 | - $_where = [ |
|
| 200 | - 'PRC_deleted' => 0, |
|
| 201 | - 'PRC_is_default' => 1, |
|
| 202 | - ]; |
|
| 203 | - if (! $include_taxes) { |
|
| 204 | - $_where['Price_Type.PBT_ID'] = ['!=', 4]; |
|
| 205 | - } |
|
| 206 | - $_query_params = [ |
|
| 207 | - $_where, |
|
| 208 | - 'order_by' => $this->_order_by_array_for_get_all_method(), |
|
| 209 | - ]; |
|
| 210 | - return $count ? $this->count([$_where]) : $this->get_all($_query_params); |
|
| 211 | - } |
|
| 189 | + /** |
|
| 190 | + * retrieve all active global prices (that are not taxes (PBT_ID=4)) for a particular event |
|
| 191 | + * |
|
| 192 | + * @param boolean $count return count |
|
| 193 | + * @param bool $include_taxes |
|
| 194 | + * @return bool|EE_Base_Class[]|EE_PRice[] |
|
| 195 | + * @throws EE_Error |
|
| 196 | + */ |
|
| 197 | + public function get_all_default_prices($count = false, $include_taxes = false) |
|
| 198 | + { |
|
| 199 | + $_where = [ |
|
| 200 | + 'PRC_deleted' => 0, |
|
| 201 | + 'PRC_is_default' => 1, |
|
| 202 | + ]; |
|
| 203 | + if (! $include_taxes) { |
|
| 204 | + $_where['Price_Type.PBT_ID'] = ['!=', 4]; |
|
| 205 | + } |
|
| 206 | + $_query_params = [ |
|
| 207 | + $_where, |
|
| 208 | + 'order_by' => $this->_order_by_array_for_get_all_method(), |
|
| 209 | + ]; |
|
| 210 | + return $count ? $this->count([$_where]) : $this->get_all($_query_params); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * retrieve all active global prices that are taxes |
|
| 216 | - * |
|
| 217 | - * @return bool|EE_Base_Class[]|EE_PRice[] |
|
| 218 | - * @throws EE_Error |
|
| 219 | - * @since $VID:$ |
|
| 220 | - */ |
|
| 221 | - public function getAllDefaultTaxes() |
|
| 222 | - { |
|
| 223 | - return $this->get_all( |
|
| 224 | - [ |
|
| 225 | - [ |
|
| 226 | - 'PRC_deleted' => 0, |
|
| 227 | - 'PRC_is_default' => 1, |
|
| 228 | - 'Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax |
|
| 229 | - ], |
|
| 230 | - 'order_by' => [ |
|
| 231 | - 'Price_Type.PRT_order' => 'ASC', |
|
| 232 | - 'PRC_order' => 'ASC' |
|
| 233 | - ], |
|
| 234 | - ] |
|
| 235 | - ); |
|
| 236 | - } |
|
| 214 | + /** |
|
| 215 | + * retrieve all active global prices that are taxes |
|
| 216 | + * |
|
| 217 | + * @return bool|EE_Base_Class[]|EE_PRice[] |
|
| 218 | + * @throws EE_Error |
|
| 219 | + * @since $VID:$ |
|
| 220 | + */ |
|
| 221 | + public function getAllDefaultTaxes() |
|
| 222 | + { |
|
| 223 | + return $this->get_all( |
|
| 224 | + [ |
|
| 225 | + [ |
|
| 226 | + 'PRC_deleted' => 0, |
|
| 227 | + 'PRC_is_default' => 1, |
|
| 228 | + 'Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax |
|
| 229 | + ], |
|
| 230 | + 'order_by' => [ |
|
| 231 | + 'Price_Type.PRT_order' => 'ASC', |
|
| 232 | + 'PRC_order' => 'ASC' |
|
| 233 | + ], |
|
| 234 | + ] |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * retrieve all prices that are taxes |
|
| 241 | - * |
|
| 242 | - * @return EE_Base_Class[]|EE_PRice[] |
|
| 243 | - * @throws EE_Error |
|
| 244 | - * @throws InvalidArgumentException |
|
| 245 | - * @throws ReflectionException |
|
| 246 | - * @throws InvalidDataTypeException |
|
| 247 | - * @throws InvalidInterfaceException |
|
| 248 | - */ |
|
| 249 | - public function get_all_prices_that_are_taxes() |
|
| 250 | - { |
|
| 251 | - $taxes = []; |
|
| 252 | - $all_taxes = $this->get_all( |
|
| 253 | - [ |
|
| 254 | - ['Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax], |
|
| 255 | - 'order_by' => ['Price_Type.PRT_order' => 'ASC', 'PRC_order' => 'ASC'], |
|
| 256 | - ] |
|
| 257 | - ); |
|
| 258 | - foreach ($all_taxes as $tax) { |
|
| 259 | - if ($tax instanceof EE_Price) { |
|
| 260 | - $taxes[ $tax->order() ][ $tax->ID() ] = $tax; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - return $taxes; |
|
| 264 | - } |
|
| 239 | + /** |
|
| 240 | + * retrieve all prices that are taxes |
|
| 241 | + * |
|
| 242 | + * @return EE_Base_Class[]|EE_PRice[] |
|
| 243 | + * @throws EE_Error |
|
| 244 | + * @throws InvalidArgumentException |
|
| 245 | + * @throws ReflectionException |
|
| 246 | + * @throws InvalidDataTypeException |
|
| 247 | + * @throws InvalidInterfaceException |
|
| 248 | + */ |
|
| 249 | + public function get_all_prices_that_are_taxes() |
|
| 250 | + { |
|
| 251 | + $taxes = []; |
|
| 252 | + $all_taxes = $this->get_all( |
|
| 253 | + [ |
|
| 254 | + ['Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax], |
|
| 255 | + 'order_by' => ['Price_Type.PRT_order' => 'ASC', 'PRC_order' => 'ASC'], |
|
| 256 | + ] |
|
| 257 | + ); |
|
| 258 | + foreach ($all_taxes as $tax) { |
|
| 259 | + if ($tax instanceof EE_Price) { |
|
| 260 | + $taxes[ $tax->order() ][ $tax->ID() ] = $tax; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + return $taxes; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * retrieve all prices for an ticket plus default global prices, but not taxes |
|
| 269 | - * |
|
| 270 | - * @param int $TKT_ID the id of the event. If not included then we assume that this is a new ticket. |
|
| 271 | - * @return EE_Base_Class[]|EE_PRice[]|boolean |
|
| 272 | - * @throws EE_Error |
|
| 273 | - */ |
|
| 274 | - public function get_all_ticket_prices_for_admin($TKT_ID = 0) |
|
| 275 | - { |
|
| 276 | - $array_of_price_objects = []; |
|
| 277 | - if (empty($TKT_ID)) { |
|
| 278 | - // if there is no tkt, get prices with no tkt ID, are global, are not a tax, and are active |
|
| 279 | - // return that list |
|
| 280 | - $default_prices = $this->get_all_default_prices(); |
|
| 267 | + /** |
|
| 268 | + * retrieve all prices for an ticket plus default global prices, but not taxes |
|
| 269 | + * |
|
| 270 | + * @param int $TKT_ID the id of the event. If not included then we assume that this is a new ticket. |
|
| 271 | + * @return EE_Base_Class[]|EE_PRice[]|boolean |
|
| 272 | + * @throws EE_Error |
|
| 273 | + */ |
|
| 274 | + public function get_all_ticket_prices_for_admin($TKT_ID = 0) |
|
| 275 | + { |
|
| 276 | + $array_of_price_objects = []; |
|
| 277 | + if (empty($TKT_ID)) { |
|
| 278 | + // if there is no tkt, get prices with no tkt ID, are global, are not a tax, and are active |
|
| 279 | + // return that list |
|
| 280 | + $default_prices = $this->get_all_default_prices(); |
|
| 281 | 281 | |
| 282 | - if ($default_prices) { |
|
| 283 | - foreach ($default_prices as $price) { |
|
| 284 | - if ($price instanceof EE_Price) { |
|
| 285 | - $array_of_price_objects[ $price->type() ][] = $price; |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - return $array_of_price_objects; |
|
| 289 | - } |
|
| 290 | - return []; |
|
| 291 | - } |
|
| 292 | - $ticket_prices = $this->get_all( |
|
| 293 | - [ |
|
| 294 | - [ |
|
| 295 | - 'TKT_ID' => $TKT_ID, |
|
| 296 | - 'PRC_deleted' => 0, |
|
| 297 | - ], |
|
| 298 | - 'order_by' => ['PRC_order' => 'ASC'], |
|
| 299 | - ] |
|
| 300 | - ); |
|
| 282 | + if ($default_prices) { |
|
| 283 | + foreach ($default_prices as $price) { |
|
| 284 | + if ($price instanceof EE_Price) { |
|
| 285 | + $array_of_price_objects[ $price->type() ][] = $price; |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + return $array_of_price_objects; |
|
| 289 | + } |
|
| 290 | + return []; |
|
| 291 | + } |
|
| 292 | + $ticket_prices = $this->get_all( |
|
| 293 | + [ |
|
| 294 | + [ |
|
| 295 | + 'TKT_ID' => $TKT_ID, |
|
| 296 | + 'PRC_deleted' => 0, |
|
| 297 | + ], |
|
| 298 | + 'order_by' => ['PRC_order' => 'ASC'], |
|
| 299 | + ] |
|
| 300 | + ); |
|
| 301 | 301 | |
| 302 | - if (! empty($ticket_prices)) { |
|
| 303 | - foreach ($ticket_prices as $price) { |
|
| 304 | - if ($price instanceof EE_Price) { |
|
| 305 | - $array_of_price_objects[ $price->type() ][] = $price; |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - return $array_of_price_objects; |
|
| 309 | - } |
|
| 310 | - return false; |
|
| 311 | - } |
|
| 302 | + if (! empty($ticket_prices)) { |
|
| 303 | + foreach ($ticket_prices as $price) { |
|
| 304 | + if ($price instanceof EE_Price) { |
|
| 305 | + $array_of_price_objects[ $price->type() ][] = $price; |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + return $array_of_price_objects; |
|
| 309 | + } |
|
| 310 | + return false; |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | 313 | |
| 314 | - /** |
|
| 315 | - * _sort_event_prices_by_type |
|
| 316 | - * |
|
| 317 | - * @param EE_Price $price_a |
|
| 318 | - * @param EE_Price $price_b |
|
| 319 | - * @return bool false on fail |
|
| 320 | - */ |
|
| 321 | - public function _sort_event_prices_by_type(EE_Price $price_a, EE_Price $price_b) |
|
| 322 | - { |
|
| 323 | - if ($price_a->type_obj()->order() === $price_b->type_obj()->order()) { |
|
| 324 | - return $this->_sort_event_prices_by_order($price_a, $price_b); |
|
| 325 | - } |
|
| 326 | - return $price_a->type_obj()->order() < $price_b->type_obj()->order() ? -1 : 1; |
|
| 327 | - } |
|
| 314 | + /** |
|
| 315 | + * _sort_event_prices_by_type |
|
| 316 | + * |
|
| 317 | + * @param EE_Price $price_a |
|
| 318 | + * @param EE_Price $price_b |
|
| 319 | + * @return bool false on fail |
|
| 320 | + */ |
|
| 321 | + public function _sort_event_prices_by_type(EE_Price $price_a, EE_Price $price_b) |
|
| 322 | + { |
|
| 323 | + if ($price_a->type_obj()->order() === $price_b->type_obj()->order()) { |
|
| 324 | + return $this->_sort_event_prices_by_order($price_a, $price_b); |
|
| 325 | + } |
|
| 326 | + return $price_a->type_obj()->order() < $price_b->type_obj()->order() ? -1 : 1; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | 329 | |
| 330 | - /** |
|
| 331 | - * _sort_event_prices_by_order |
|
| 332 | - * |
|
| 333 | - * @param EE_Price $price_a |
|
| 334 | - * @param EE_Price $price_b |
|
| 335 | - * @return bool false on fail |
|
| 336 | - */ |
|
| 337 | - public function _sort_event_prices_by_order(EE_Price $price_a, EE_Price $price_b) |
|
| 338 | - { |
|
| 339 | - if ($price_a->order() === $price_b->order()) { |
|
| 340 | - return 0; |
|
| 341 | - } |
|
| 342 | - return $price_a->order() < $price_b->order() ? -1 : 1; |
|
| 343 | - } |
|
| 330 | + /** |
|
| 331 | + * _sort_event_prices_by_order |
|
| 332 | + * |
|
| 333 | + * @param EE_Price $price_a |
|
| 334 | + * @param EE_Price $price_b |
|
| 335 | + * @return bool false on fail |
|
| 336 | + */ |
|
| 337 | + public function _sort_event_prices_by_order(EE_Price $price_a, EE_Price $price_b) |
|
| 338 | + { |
|
| 339 | + if ($price_a->order() === $price_b->order()) { |
|
| 340 | + return 0; |
|
| 341 | + } |
|
| 342 | + return $price_a->order() < $price_b->order() ? -1 : 1; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | 345 | |
| 346 | - /** |
|
| 347 | - * get all prices of a specific type |
|
| 348 | - * |
|
| 349 | - * @param int $type - PRT_ID |
|
| 350 | - * @return EE_Base_Class[]|EE_PRice[] |
|
| 351 | - * @throws EE_Error |
|
| 352 | - */ |
|
| 353 | - public function get_all_prices_that_are_type($type = 0) |
|
| 354 | - { |
|
| 355 | - return $this->get_all( |
|
| 356 | - [ |
|
| 357 | - [ |
|
| 358 | - 'PRT_ID' => $type, |
|
| 359 | - ], |
|
| 360 | - 'order_by' => $this->_order_by_array_for_get_all_method(), |
|
| 361 | - ] |
|
| 362 | - ); |
|
| 363 | - } |
|
| 346 | + /** |
|
| 347 | + * get all prices of a specific type |
|
| 348 | + * |
|
| 349 | + * @param int $type - PRT_ID |
|
| 350 | + * @return EE_Base_Class[]|EE_PRice[] |
|
| 351 | + * @throws EE_Error |
|
| 352 | + */ |
|
| 353 | + public function get_all_prices_that_are_type($type = 0) |
|
| 354 | + { |
|
| 355 | + return $this->get_all( |
|
| 356 | + [ |
|
| 357 | + [ |
|
| 358 | + 'PRT_ID' => $type, |
|
| 359 | + ], |
|
| 360 | + 'order_by' => $this->_order_by_array_for_get_all_method(), |
|
| 361 | + ] |
|
| 362 | + ); |
|
| 363 | + } |
|
| 364 | 364 | |
| 365 | 365 | |
| 366 | - /** |
|
| 367 | - * Returns an array of the normal 'order_by' query parameter provided to the get_all query. |
|
| 368 | - * Of course you don't have to use it, but this is the order we usually want to sort prices by |
|
| 369 | - * |
|
| 370 | - * @return array which can be used like so: $this->get_all(array(array(...where |
|
| 371 | - * stuff...),'order_by'=>$this->_order_by_array_for_get_all_method())); |
|
| 372 | - */ |
|
| 373 | - public function _order_by_array_for_get_all_method() |
|
| 374 | - { |
|
| 375 | - return [ |
|
| 376 | - 'PRC_order' => 'ASC', |
|
| 377 | - 'Price_Type.PRT_order' => 'ASC', |
|
| 378 | - 'PRC_ID' => 'ASC', |
|
| 379 | - ]; |
|
| 380 | - } |
|
| 366 | + /** |
|
| 367 | + * Returns an array of the normal 'order_by' query parameter provided to the get_all query. |
|
| 368 | + * Of course you don't have to use it, but this is the order we usually want to sort prices by |
|
| 369 | + * |
|
| 370 | + * @return array which can be used like so: $this->get_all(array(array(...where |
|
| 371 | + * stuff...),'order_by'=>$this->_order_by_array_for_get_all_method())); |
|
| 372 | + */ |
|
| 373 | + public function _order_by_array_for_get_all_method() |
|
| 374 | + { |
|
| 375 | + return [ |
|
| 376 | + 'PRC_order' => 'ASC', |
|
| 377 | + 'Price_Type.PRT_order' => 'ASC', |
|
| 378 | + 'PRC_ID' => 'ASC', |
|
| 379 | + ]; |
|
| 380 | + } |
|
| 381 | 381 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | protected function __construct($timezone) |
| 31 | 31 | { |
| 32 | - require_once(EE_MODELS . 'EEM_Price_Type.model.php'); |
|
| 32 | + require_once(EE_MODELS.'EEM_Price_Type.model.php'); |
|
| 33 | 33 | $this->singular_item = __('Price', 'event_espresso'); |
| 34 | 34 | $this->plural_item = __('Prices', 'event_espresso'); |
| 35 | 35 | |
@@ -116,23 +116,23 @@ discard block |
||
| 116 | 116 | 'WP_User' => new EE_Belongs_To_Relation(), |
| 117 | 117 | ]; |
| 118 | 118 | // this model is generally available for reading |
| 119 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = |
|
| 119 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = |
|
| 120 | 120 | new EE_Restriction_Generator_Default_Public( |
| 121 | 121 | 'PRC_is_default', |
| 122 | 122 | 'Ticket.Datetime.Event' |
| 123 | 123 | ); |
| 124 | 124 | // account for default tickets in the caps |
| 125 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
| 125 | + $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = |
|
| 126 | 126 | new EE_Restriction_Generator_Default_Protected( |
| 127 | 127 | 'PRC_is_default', |
| 128 | 128 | 'Ticket.Datetime.Event' |
| 129 | 129 | ); |
| 130 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = |
|
| 130 | + $this->_cap_restriction_generators[EEM_Base::caps_edit] = |
|
| 131 | 131 | new EE_Restriction_Generator_Default_Protected( |
| 132 | 132 | 'PRC_is_default', |
| 133 | 133 | 'Ticket.Datetime.Event' |
| 134 | 134 | ); |
| 135 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = |
|
| 135 | + $this->_cap_restriction_generators[EEM_Base::caps_delete] = |
|
| 136 | 136 | new EE_Restriction_Generator_Default_Protected( |
| 137 | 137 | 'PRC_is_default', |
| 138 | 138 | 'Ticket.Datetime.Event' |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | 'PRC_deleted' => 0, |
| 201 | 201 | 'PRC_is_default' => 1, |
| 202 | 202 | ]; |
| 203 | - if (! $include_taxes) { |
|
| 203 | + if ( ! $include_taxes) { |
|
| 204 | 204 | $_where['Price_Type.PBT_ID'] = ['!=', 4]; |
| 205 | 205 | } |
| 206 | 206 | $_query_params = [ |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | ); |
| 258 | 258 | foreach ($all_taxes as $tax) { |
| 259 | 259 | if ($tax instanceof EE_Price) { |
| 260 | - $taxes[ $tax->order() ][ $tax->ID() ] = $tax; |
|
| 260 | + $taxes[$tax->order()][$tax->ID()] = $tax; |
|
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | return $taxes; |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | if ($default_prices) { |
| 283 | 283 | foreach ($default_prices as $price) { |
| 284 | 284 | if ($price instanceof EE_Price) { |
| 285 | - $array_of_price_objects[ $price->type() ][] = $price; |
|
| 285 | + $array_of_price_objects[$price->type()][] = $price; |
|
| 286 | 286 | } |
| 287 | 287 | } |
| 288 | 288 | return $array_of_price_objects; |
@@ -299,10 +299,10 @@ discard block |
||
| 299 | 299 | ] |
| 300 | 300 | ); |
| 301 | 301 | |
| 302 | - if (! empty($ticket_prices)) { |
|
| 302 | + if ( ! empty($ticket_prices)) { |
|
| 303 | 303 | foreach ($ticket_prices as $price) { |
| 304 | 304 | if ($price instanceof EE_Price) { |
| 305 | - $array_of_price_objects[ $price->type() ][] = $price; |
|
| 305 | + $array_of_price_objects[$price->type()][] = $price; |
|
| 306 | 306 | } |
| 307 | 307 | } |
| 308 | 308 | return $array_of_price_objects; |
@@ -25,211 +25,211 @@ |
||
| 25 | 25 | class DefaultPrices implements DefaultEntityGeneratorInterface |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var EEM_Price |
|
| 30 | - */ |
|
| 31 | - protected $price_model; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var EEM_Price_Type |
|
| 35 | - */ |
|
| 36 | - protected $price_type_model; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var EE_Price[] |
|
| 40 | - */ |
|
| 41 | - protected $new_prices = []; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var EE_Price[] |
|
| 45 | - */ |
|
| 46 | - protected $taxes = []; |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param EEM_Price $price_model |
|
| 51 | - * @param EEM_Price_Type $price_type_model |
|
| 52 | - */ |
|
| 53 | - public function __construct(EEM_Price $price_model, EEM_Price_Type $price_type_model) |
|
| 54 | - { |
|
| 55 | - $this->price_model = $price_model; |
|
| 56 | - $this->price_type_model = $price_type_model; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param EE_Ticket|EE_Base_Class $entity |
|
| 62 | - * @return EE_Price[] |
|
| 63 | - * @throws EE_Error |
|
| 64 | - * @throws InvalidInterfaceException |
|
| 65 | - * @throws ReflectionException |
|
| 66 | - * @since $VID:$ |
|
| 67 | - */ |
|
| 68 | - public function create(EE_Base_Class $entity): array |
|
| 69 | - { |
|
| 70 | - if (! $entity instanceof EE_Ticket) { |
|
| 71 | - throw new InvalidEntityException($entity, 'EE_Ticket'); |
|
| 72 | - } |
|
| 73 | - $is_free = true; |
|
| 74 | - $has_base_price = false; |
|
| 75 | - $remove_existing_relations = true; |
|
| 76 | - // first, let's get all of the default taxes for the site |
|
| 77 | - $this->taxes = $this->price_model->getAllDefaultTaxes(); |
|
| 78 | - // then separate taxes from the other prices for the existing default ticket prices |
|
| 79 | - $default_prices = $this->separateTaxesFromOtherPrices($entity->prices()); |
|
| 80 | - // but if that's empty, then let's get the default global prices |
|
| 81 | - if (empty($default_prices)) { |
|
| 82 | - $default_global_prices = $this->price_model->get_all_default_prices(); |
|
| 83 | - $default_prices = $this->separateTaxesFromOtherPrices($default_global_prices); |
|
| 84 | - $remove_existing_relations = false; |
|
| 85 | - } |
|
| 86 | - // then clone and apply all of the default prices |
|
| 87 | - [$has_base_price, $is_free] = $this->cloneDefaultPrices( |
|
| 88 | - $entity, |
|
| 89 | - $default_prices, |
|
| 90 | - $remove_existing_relations, |
|
| 91 | - $has_base_price, |
|
| 92 | - $is_free |
|
| 93 | - ); |
|
| 94 | - if (! $has_base_price) { |
|
| 95 | - $new_base_price = $this->createNewBasePrice($entity); |
|
| 96 | - $this->new_prices[ $new_base_price->ID() ] = $new_base_price; |
|
| 97 | - } |
|
| 98 | - $this->applyTaxes($entity, $is_free); |
|
| 99 | - $ticket_total = $entity->get_ticket_total_with_taxes(true); |
|
| 100 | - if ($ticket_total !== $entity->ticket_price()) { |
|
| 101 | - $entity->set_price($ticket_total); |
|
| 102 | - $entity->save(); |
|
| 103 | - } |
|
| 104 | - return $this->new_prices; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param EE_Ticket $ticket |
|
| 110 | - * @param bool $is_free |
|
| 111 | - * @throws EE_Error |
|
| 112 | - * @throws ReflectionException |
|
| 113 | - */ |
|
| 114 | - private function applyTaxes(EE_Ticket $ticket, bool $is_free) |
|
| 115 | - { |
|
| 116 | - if (! $is_free && ! empty($this->taxes)) { |
|
| 117 | - foreach ($this->taxes as $tax) { |
|
| 118 | - // assign taxes but don't duplicate them because they operate globally |
|
| 119 | - $ticket->set_taxable(true); |
|
| 120 | - $tax->_add_relation_to($ticket, 'Ticket'); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param EE_Ticket $ticket |
|
| 128 | - * @param EE_Price[] $default_prices |
|
| 129 | - * @param bool $remove_existing_relations |
|
| 130 | - * @param bool $has_base_price |
|
| 131 | - * @param bool $is_free |
|
| 132 | - * @return bool[] |
|
| 133 | - * @throws EE_Error |
|
| 134 | - * @throws ReflectionException |
|
| 135 | - */ |
|
| 136 | - private function cloneDefaultPrices( |
|
| 137 | - EE_Ticket $ticket, |
|
| 138 | - array $default_prices, |
|
| 139 | - bool $remove_existing_relations, |
|
| 140 | - bool $has_base_price, |
|
| 141 | - bool $is_free |
|
| 142 | - ): array { |
|
| 143 | - foreach ($default_prices as $default_price) { |
|
| 144 | - // duplicate the default price so that it does not get mutated |
|
| 145 | - $default_price_clone = clone $default_price; |
|
| 146 | - if ($remove_existing_relations) { |
|
| 147 | - $ticket->_remove_relation_to($default_price, 'Price'); |
|
| 148 | - } |
|
| 149 | - if (( |
|
| 150 | - // has non-zero base price |
|
| 151 | - $default_price_clone->is_base_price() |
|
| 152 | - && $default_price_clone->amount() > 0 |
|
| 153 | - ) |
|
| 154 | - || ( |
|
| 155 | - // or has fixed amount surcharge |
|
| 156 | - $default_price_clone->is_surcharge() |
|
| 157 | - && ! $default_price_clone->is_percent() |
|
| 158 | - ) |
|
| 159 | - ) { |
|
| 160 | - $is_free = false; |
|
| 161 | - } |
|
| 162 | - $is_base_price = $default_price_clone->is_base_price(); |
|
| 163 | - // add this price to ticket if it is a regular price modifier, ie: NOT a base price, |
|
| 164 | - // OR if it IS a base price but this ticket does NOT already have a base price |
|
| 165 | - if (! $is_base_price || ! $has_base_price) { |
|
| 166 | - $default_price_clone->set('PRC_ID', null); |
|
| 167 | - $default_price_clone->set('PRC_is_default', false); |
|
| 168 | - $default_price_clone->save(); |
|
| 169 | - $default_price_clone->_add_relation_to($ticket, 'Ticket'); |
|
| 170 | - $this->new_prices[ $default_price_clone->ID() ] = $default_price_clone; |
|
| 171 | - // then recheck that a base price has been set so that we don't add another one |
|
| 172 | - $has_base_price = $is_base_price ? true : $has_base_price; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - return [$has_base_price, $is_free]; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param EE_Ticket $ticket |
|
| 181 | - * @return EE_Price |
|
| 182 | - * @throws EE_Error |
|
| 183 | - * @throws ReflectionException |
|
| 184 | - */ |
|
| 185 | - private function createNewBasePrice(EE_Ticket $ticket): EE_Price |
|
| 186 | - { |
|
| 187 | - $new_base_price = $this->price_model->get_new_price(); |
|
| 188 | - $base_price_type = $this->price_type_model->get_one( |
|
| 189 | - [ |
|
| 190 | - [ |
|
| 191 | - 'PBT_ID' => EEM_Price_Type::base_type_base_price, |
|
| 192 | - ], |
|
| 193 | - ] |
|
| 194 | - ); |
|
| 195 | - if (! $base_price_type instanceof EE_Price_Type) { |
|
| 196 | - throw new RuntimeException( |
|
| 197 | - esc_html__( |
|
| 198 | - 'A valid base price type could not be retrieved from the database.', |
|
| 199 | - 'event_espresso' |
|
| 200 | - ) |
|
| 201 | - ); |
|
| 202 | - } |
|
| 203 | - $new_base_price->set('PRT_ID', $base_price_type->ID()); |
|
| 204 | - $new_base_price->set('PRC_is_default', false); |
|
| 205 | - $new_base_price->save(); |
|
| 206 | - $new_base_price->_add_relation_to($ticket, 'Ticket'); |
|
| 207 | - return $new_base_price; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param array $prices |
|
| 213 | - * @return array |
|
| 214 | - * @throws EE_Error |
|
| 215 | - * @throws ReflectionException |
|
| 216 | - */ |
|
| 217 | - private function separateTaxesFromOtherPrices(array $prices = []): array |
|
| 218 | - { |
|
| 219 | - $default_prices = []; |
|
| 220 | - if (is_array($prices)) { |
|
| 221 | - foreach ($prices as $key => $price) { |
|
| 222 | - if (! $price instanceof EE_Price) { |
|
| 223 | - throw new InvalidEntityException($price, 'EE_Price'); |
|
| 224 | - } |
|
| 225 | - // grab any taxes but don't do anything just yet |
|
| 226 | - if ($price->is_tax()) { |
|
| 227 | - $this->taxes[ $price->ID() ] = $price; |
|
| 228 | - continue; |
|
| 229 | - } |
|
| 230 | - $default_prices[ $price->ID() ] = $price; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - return $default_prices; |
|
| 234 | - } |
|
| 28 | + /** |
|
| 29 | + * @var EEM_Price |
|
| 30 | + */ |
|
| 31 | + protected $price_model; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var EEM_Price_Type |
|
| 35 | + */ |
|
| 36 | + protected $price_type_model; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var EE_Price[] |
|
| 40 | + */ |
|
| 41 | + protected $new_prices = []; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var EE_Price[] |
|
| 45 | + */ |
|
| 46 | + protected $taxes = []; |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param EEM_Price $price_model |
|
| 51 | + * @param EEM_Price_Type $price_type_model |
|
| 52 | + */ |
|
| 53 | + public function __construct(EEM_Price $price_model, EEM_Price_Type $price_type_model) |
|
| 54 | + { |
|
| 55 | + $this->price_model = $price_model; |
|
| 56 | + $this->price_type_model = $price_type_model; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param EE_Ticket|EE_Base_Class $entity |
|
| 62 | + * @return EE_Price[] |
|
| 63 | + * @throws EE_Error |
|
| 64 | + * @throws InvalidInterfaceException |
|
| 65 | + * @throws ReflectionException |
|
| 66 | + * @since $VID:$ |
|
| 67 | + */ |
|
| 68 | + public function create(EE_Base_Class $entity): array |
|
| 69 | + { |
|
| 70 | + if (! $entity instanceof EE_Ticket) { |
|
| 71 | + throw new InvalidEntityException($entity, 'EE_Ticket'); |
|
| 72 | + } |
|
| 73 | + $is_free = true; |
|
| 74 | + $has_base_price = false; |
|
| 75 | + $remove_existing_relations = true; |
|
| 76 | + // first, let's get all of the default taxes for the site |
|
| 77 | + $this->taxes = $this->price_model->getAllDefaultTaxes(); |
|
| 78 | + // then separate taxes from the other prices for the existing default ticket prices |
|
| 79 | + $default_prices = $this->separateTaxesFromOtherPrices($entity->prices()); |
|
| 80 | + // but if that's empty, then let's get the default global prices |
|
| 81 | + if (empty($default_prices)) { |
|
| 82 | + $default_global_prices = $this->price_model->get_all_default_prices(); |
|
| 83 | + $default_prices = $this->separateTaxesFromOtherPrices($default_global_prices); |
|
| 84 | + $remove_existing_relations = false; |
|
| 85 | + } |
|
| 86 | + // then clone and apply all of the default prices |
|
| 87 | + [$has_base_price, $is_free] = $this->cloneDefaultPrices( |
|
| 88 | + $entity, |
|
| 89 | + $default_prices, |
|
| 90 | + $remove_existing_relations, |
|
| 91 | + $has_base_price, |
|
| 92 | + $is_free |
|
| 93 | + ); |
|
| 94 | + if (! $has_base_price) { |
|
| 95 | + $new_base_price = $this->createNewBasePrice($entity); |
|
| 96 | + $this->new_prices[ $new_base_price->ID() ] = $new_base_price; |
|
| 97 | + } |
|
| 98 | + $this->applyTaxes($entity, $is_free); |
|
| 99 | + $ticket_total = $entity->get_ticket_total_with_taxes(true); |
|
| 100 | + if ($ticket_total !== $entity->ticket_price()) { |
|
| 101 | + $entity->set_price($ticket_total); |
|
| 102 | + $entity->save(); |
|
| 103 | + } |
|
| 104 | + return $this->new_prices; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param EE_Ticket $ticket |
|
| 110 | + * @param bool $is_free |
|
| 111 | + * @throws EE_Error |
|
| 112 | + * @throws ReflectionException |
|
| 113 | + */ |
|
| 114 | + private function applyTaxes(EE_Ticket $ticket, bool $is_free) |
|
| 115 | + { |
|
| 116 | + if (! $is_free && ! empty($this->taxes)) { |
|
| 117 | + foreach ($this->taxes as $tax) { |
|
| 118 | + // assign taxes but don't duplicate them because they operate globally |
|
| 119 | + $ticket->set_taxable(true); |
|
| 120 | + $tax->_add_relation_to($ticket, 'Ticket'); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param EE_Ticket $ticket |
|
| 128 | + * @param EE_Price[] $default_prices |
|
| 129 | + * @param bool $remove_existing_relations |
|
| 130 | + * @param bool $has_base_price |
|
| 131 | + * @param bool $is_free |
|
| 132 | + * @return bool[] |
|
| 133 | + * @throws EE_Error |
|
| 134 | + * @throws ReflectionException |
|
| 135 | + */ |
|
| 136 | + private function cloneDefaultPrices( |
|
| 137 | + EE_Ticket $ticket, |
|
| 138 | + array $default_prices, |
|
| 139 | + bool $remove_existing_relations, |
|
| 140 | + bool $has_base_price, |
|
| 141 | + bool $is_free |
|
| 142 | + ): array { |
|
| 143 | + foreach ($default_prices as $default_price) { |
|
| 144 | + // duplicate the default price so that it does not get mutated |
|
| 145 | + $default_price_clone = clone $default_price; |
|
| 146 | + if ($remove_existing_relations) { |
|
| 147 | + $ticket->_remove_relation_to($default_price, 'Price'); |
|
| 148 | + } |
|
| 149 | + if (( |
|
| 150 | + // has non-zero base price |
|
| 151 | + $default_price_clone->is_base_price() |
|
| 152 | + && $default_price_clone->amount() > 0 |
|
| 153 | + ) |
|
| 154 | + || ( |
|
| 155 | + // or has fixed amount surcharge |
|
| 156 | + $default_price_clone->is_surcharge() |
|
| 157 | + && ! $default_price_clone->is_percent() |
|
| 158 | + ) |
|
| 159 | + ) { |
|
| 160 | + $is_free = false; |
|
| 161 | + } |
|
| 162 | + $is_base_price = $default_price_clone->is_base_price(); |
|
| 163 | + // add this price to ticket if it is a regular price modifier, ie: NOT a base price, |
|
| 164 | + // OR if it IS a base price but this ticket does NOT already have a base price |
|
| 165 | + if (! $is_base_price || ! $has_base_price) { |
|
| 166 | + $default_price_clone->set('PRC_ID', null); |
|
| 167 | + $default_price_clone->set('PRC_is_default', false); |
|
| 168 | + $default_price_clone->save(); |
|
| 169 | + $default_price_clone->_add_relation_to($ticket, 'Ticket'); |
|
| 170 | + $this->new_prices[ $default_price_clone->ID() ] = $default_price_clone; |
|
| 171 | + // then recheck that a base price has been set so that we don't add another one |
|
| 172 | + $has_base_price = $is_base_price ? true : $has_base_price; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + return [$has_base_price, $is_free]; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param EE_Ticket $ticket |
|
| 181 | + * @return EE_Price |
|
| 182 | + * @throws EE_Error |
|
| 183 | + * @throws ReflectionException |
|
| 184 | + */ |
|
| 185 | + private function createNewBasePrice(EE_Ticket $ticket): EE_Price |
|
| 186 | + { |
|
| 187 | + $new_base_price = $this->price_model->get_new_price(); |
|
| 188 | + $base_price_type = $this->price_type_model->get_one( |
|
| 189 | + [ |
|
| 190 | + [ |
|
| 191 | + 'PBT_ID' => EEM_Price_Type::base_type_base_price, |
|
| 192 | + ], |
|
| 193 | + ] |
|
| 194 | + ); |
|
| 195 | + if (! $base_price_type instanceof EE_Price_Type) { |
|
| 196 | + throw new RuntimeException( |
|
| 197 | + esc_html__( |
|
| 198 | + 'A valid base price type could not be retrieved from the database.', |
|
| 199 | + 'event_espresso' |
|
| 200 | + ) |
|
| 201 | + ); |
|
| 202 | + } |
|
| 203 | + $new_base_price->set('PRT_ID', $base_price_type->ID()); |
|
| 204 | + $new_base_price->set('PRC_is_default', false); |
|
| 205 | + $new_base_price->save(); |
|
| 206 | + $new_base_price->_add_relation_to($ticket, 'Ticket'); |
|
| 207 | + return $new_base_price; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param array $prices |
|
| 213 | + * @return array |
|
| 214 | + * @throws EE_Error |
|
| 215 | + * @throws ReflectionException |
|
| 216 | + */ |
|
| 217 | + private function separateTaxesFromOtherPrices(array $prices = []): array |
|
| 218 | + { |
|
| 219 | + $default_prices = []; |
|
| 220 | + if (is_array($prices)) { |
|
| 221 | + foreach ($prices as $key => $price) { |
|
| 222 | + if (! $price instanceof EE_Price) { |
|
| 223 | + throw new InvalidEntityException($price, 'EE_Price'); |
|
| 224 | + } |
|
| 225 | + // grab any taxes but don't do anything just yet |
|
| 226 | + if ($price->is_tax()) { |
|
| 227 | + $this->taxes[ $price->ID() ] = $price; |
|
| 228 | + continue; |
|
| 229 | + } |
|
| 230 | + $default_prices[ $price->ID() ] = $price; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + return $default_prices; |
|
| 234 | + } |
|
| 235 | 235 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public function create(EE_Base_Class $entity): array |
| 69 | 69 | { |
| 70 | - if (! $entity instanceof EE_Ticket) { |
|
| 70 | + if ( ! $entity instanceof EE_Ticket) { |
|
| 71 | 71 | throw new InvalidEntityException($entity, 'EE_Ticket'); |
| 72 | 72 | } |
| 73 | 73 | $is_free = true; |
@@ -91,9 +91,9 @@ discard block |
||
| 91 | 91 | $has_base_price, |
| 92 | 92 | $is_free |
| 93 | 93 | ); |
| 94 | - if (! $has_base_price) { |
|
| 94 | + if ( ! $has_base_price) { |
|
| 95 | 95 | $new_base_price = $this->createNewBasePrice($entity); |
| 96 | - $this->new_prices[ $new_base_price->ID() ] = $new_base_price; |
|
| 96 | + $this->new_prices[$new_base_price->ID()] = $new_base_price; |
|
| 97 | 97 | } |
| 98 | 98 | $this->applyTaxes($entity, $is_free); |
| 99 | 99 | $ticket_total = $entity->get_ticket_total_with_taxes(true); |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | private function applyTaxes(EE_Ticket $ticket, bool $is_free) |
| 115 | 115 | { |
| 116 | - if (! $is_free && ! empty($this->taxes)) { |
|
| 116 | + if ( ! $is_free && ! empty($this->taxes)) { |
|
| 117 | 117 | foreach ($this->taxes as $tax) { |
| 118 | 118 | // assign taxes but don't duplicate them because they operate globally |
| 119 | 119 | $ticket->set_taxable(true); |
@@ -162,12 +162,12 @@ discard block |
||
| 162 | 162 | $is_base_price = $default_price_clone->is_base_price(); |
| 163 | 163 | // add this price to ticket if it is a regular price modifier, ie: NOT a base price, |
| 164 | 164 | // OR if it IS a base price but this ticket does NOT already have a base price |
| 165 | - if (! $is_base_price || ! $has_base_price) { |
|
| 165 | + if ( ! $is_base_price || ! $has_base_price) { |
|
| 166 | 166 | $default_price_clone->set('PRC_ID', null); |
| 167 | 167 | $default_price_clone->set('PRC_is_default', false); |
| 168 | 168 | $default_price_clone->save(); |
| 169 | 169 | $default_price_clone->_add_relation_to($ticket, 'Ticket'); |
| 170 | - $this->new_prices[ $default_price_clone->ID() ] = $default_price_clone; |
|
| 170 | + $this->new_prices[$default_price_clone->ID()] = $default_price_clone; |
|
| 171 | 171 | // then recheck that a base price has been set so that we don't add another one |
| 172 | 172 | $has_base_price = $is_base_price ? true : $has_base_price; |
| 173 | 173 | } |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | ], |
| 193 | 193 | ] |
| 194 | 194 | ); |
| 195 | - if (! $base_price_type instanceof EE_Price_Type) { |
|
| 195 | + if ( ! $base_price_type instanceof EE_Price_Type) { |
|
| 196 | 196 | throw new RuntimeException( |
| 197 | 197 | esc_html__( |
| 198 | 198 | 'A valid base price type could not be retrieved from the database.', |
@@ -219,15 +219,15 @@ discard block |
||
| 219 | 219 | $default_prices = []; |
| 220 | 220 | if (is_array($prices)) { |
| 221 | 221 | foreach ($prices as $key => $price) { |
| 222 | - if (! $price instanceof EE_Price) { |
|
| 222 | + if ( ! $price instanceof EE_Price) { |
|
| 223 | 223 | throw new InvalidEntityException($price, 'EE_Price'); |
| 224 | 224 | } |
| 225 | 225 | // grab any taxes but don't do anything just yet |
| 226 | 226 | if ($price->is_tax()) { |
| 227 | - $this->taxes[ $price->ID() ] = $price; |
|
| 227 | + $this->taxes[$price->ID()] = $price; |
|
| 228 | 228 | continue; |
| 229 | 229 | } |
| 230 | - $default_prices[ $price->ID() ] = $price; |
|
| 230 | + $default_prices[$price->ID()] = $price; |
|
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | 233 | return $default_prices; |
@@ -22,66 +22,66 @@ |
||
| 22 | 22 | class DefaultTickets implements DefaultEntityGeneratorInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var DefaultPrices $default_prices |
|
| 27 | - */ |
|
| 28 | - protected $default_prices; |
|
| 25 | + /** |
|
| 26 | + * @var DefaultPrices $default_prices |
|
| 27 | + */ |
|
| 28 | + protected $default_prices; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var EEM_Ticket $ticket_model |
|
| 32 | - */ |
|
| 33 | - protected $ticket_model; |
|
| 30 | + /** |
|
| 31 | + * @var EEM_Ticket $ticket_model |
|
| 32 | + */ |
|
| 33 | + protected $ticket_model; |
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param DefaultPrices $default_prices |
|
| 38 | - * @param EEM_Ticket $ticket_model |
|
| 39 | - */ |
|
| 40 | - public function __construct(DefaultPrices $default_prices, EEM_Ticket $ticket_model) |
|
| 41 | - { |
|
| 42 | - $this->default_prices = $default_prices; |
|
| 43 | - $this->ticket_model = $ticket_model; |
|
| 44 | - } |
|
| 36 | + /** |
|
| 37 | + * @param DefaultPrices $default_prices |
|
| 38 | + * @param EEM_Ticket $ticket_model |
|
| 39 | + */ |
|
| 40 | + public function __construct(DefaultPrices $default_prices, EEM_Ticket $ticket_model) |
|
| 41 | + { |
|
| 42 | + $this->default_prices = $default_prices; |
|
| 43 | + $this->ticket_model = $ticket_model; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param EE_Datetime|EE_Base_Class $entity |
|
| 49 | - * @return EE_Ticket[] |
|
| 50 | - * @throws EE_Error |
|
| 51 | - * @throws InvalidEntityException |
|
| 52 | - * @throws ReflectionException |
|
| 53 | - * @since $VID:$ |
|
| 54 | - */ |
|
| 55 | - public function create(EE_Base_Class $entity): array |
|
| 56 | - { |
|
| 57 | - if (! $entity instanceof EE_Datetime) { |
|
| 58 | - throw new InvalidEntityException($entity, 'EE_Datetime'); |
|
| 59 | - } |
|
| 60 | - $new_tickets = []; |
|
| 61 | - $default_tickets = $this->ticket_model->get_all_default_tickets(); |
|
| 62 | - if (is_array($default_tickets)) { |
|
| 63 | - foreach ($default_tickets as $default_ticket) { |
|
| 64 | - if (! $default_ticket instanceof EE_Ticket) { |
|
| 65 | - throw new InvalidEntityException($default_ticket, 'EE_Ticket'); |
|
| 66 | - } |
|
| 67 | - $existing_default_prices = $default_ticket->prices(); |
|
| 68 | - // clone ticket, strip out ID, then save to get a new ID |
|
| 69 | - $default_ticket_clone = clone $default_ticket; |
|
| 70 | - $default_ticket_clone->set('TKT_ID', null); |
|
| 71 | - $default_ticket_clone->set('TKT_is_default', false); |
|
| 72 | - $default_ticket_clone->save(); |
|
| 73 | - $default_ticket_clone->_add_relation_to($entity, 'Datetime'); |
|
| 74 | - // temporarily adding relations to existing prices, but these will be cloned and removed |
|
| 75 | - // when passed to DefaultPrices::create() below so that they the clones can be freely mutated |
|
| 76 | - foreach ($existing_default_prices as $existing_default_price) { |
|
| 77 | - if ($existing_default_price instanceof EE_Price) { |
|
| 78 | - $existing_default_price->_add_relation_to($default_ticket_clone, 'Ticket'); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - $this->default_prices->create($default_ticket_clone); |
|
| 82 | - $new_tickets[ $default_ticket_clone->ID() ] = $default_ticket_clone; |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - return $new_tickets; |
|
| 86 | - } |
|
| 47 | + /** |
|
| 48 | + * @param EE_Datetime|EE_Base_Class $entity |
|
| 49 | + * @return EE_Ticket[] |
|
| 50 | + * @throws EE_Error |
|
| 51 | + * @throws InvalidEntityException |
|
| 52 | + * @throws ReflectionException |
|
| 53 | + * @since $VID:$ |
|
| 54 | + */ |
|
| 55 | + public function create(EE_Base_Class $entity): array |
|
| 56 | + { |
|
| 57 | + if (! $entity instanceof EE_Datetime) { |
|
| 58 | + throw new InvalidEntityException($entity, 'EE_Datetime'); |
|
| 59 | + } |
|
| 60 | + $new_tickets = []; |
|
| 61 | + $default_tickets = $this->ticket_model->get_all_default_tickets(); |
|
| 62 | + if (is_array($default_tickets)) { |
|
| 63 | + foreach ($default_tickets as $default_ticket) { |
|
| 64 | + if (! $default_ticket instanceof EE_Ticket) { |
|
| 65 | + throw new InvalidEntityException($default_ticket, 'EE_Ticket'); |
|
| 66 | + } |
|
| 67 | + $existing_default_prices = $default_ticket->prices(); |
|
| 68 | + // clone ticket, strip out ID, then save to get a new ID |
|
| 69 | + $default_ticket_clone = clone $default_ticket; |
|
| 70 | + $default_ticket_clone->set('TKT_ID', null); |
|
| 71 | + $default_ticket_clone->set('TKT_is_default', false); |
|
| 72 | + $default_ticket_clone->save(); |
|
| 73 | + $default_ticket_clone->_add_relation_to($entity, 'Datetime'); |
|
| 74 | + // temporarily adding relations to existing prices, but these will be cloned and removed |
|
| 75 | + // when passed to DefaultPrices::create() below so that they the clones can be freely mutated |
|
| 76 | + foreach ($existing_default_prices as $existing_default_price) { |
|
| 77 | + if ($existing_default_price instanceof EE_Price) { |
|
| 78 | + $existing_default_price->_add_relation_to($default_ticket_clone, 'Ticket'); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + $this->default_prices->create($default_ticket_clone); |
|
| 82 | + $new_tickets[ $default_ticket_clone->ID() ] = $default_ticket_clone; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + return $new_tickets; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -54,14 +54,14 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function create(EE_Base_Class $entity): array |
| 56 | 56 | { |
| 57 | - if (! $entity instanceof EE_Datetime) { |
|
| 57 | + if ( ! $entity instanceof EE_Datetime) { |
|
| 58 | 58 | throw new InvalidEntityException($entity, 'EE_Datetime'); |
| 59 | 59 | } |
| 60 | 60 | $new_tickets = []; |
| 61 | 61 | $default_tickets = $this->ticket_model->get_all_default_tickets(); |
| 62 | 62 | if (is_array($default_tickets)) { |
| 63 | 63 | foreach ($default_tickets as $default_ticket) { |
| 64 | - if (! $default_ticket instanceof EE_Ticket) { |
|
| 64 | + if ( ! $default_ticket instanceof EE_Ticket) { |
|
| 65 | 65 | throw new InvalidEntityException($default_ticket, 'EE_Ticket'); |
| 66 | 66 | } |
| 67 | 67 | $existing_default_prices = $default_ticket->prices(); |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | $this->default_prices->create($default_ticket_clone); |
| 82 | - $new_tickets[ $default_ticket_clone->ID() ] = $default_ticket_clone; |
|
| 82 | + $new_tickets[$default_ticket_clone->ID()] = $default_ticket_clone; |
|
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | return $new_tickets; |