activity_model.php
11 months ago
agent_meta_model.php
1 year ago
agent_model.php
1 year ago
booking_meta_model.php
1 year ago
booking_model.php
11 months ago
bundle_meta_model.php
11 months ago
bundle_model.php
11 months ago
cart_item_model.php
1 year ago
cart_meta_model.php
1 year ago
cart_model.php
1 year ago
connector_model.php
1 year ago
customer_meta_model.php
1 year ago
customer_model.php
9 months ago
invoice_model.php
1 year ago
join_bundles_services_model.php
1 year ago
location_category_model.php
9 months ago
location_model.php
1 year ago
meta_model.php
1 year ago
model.php
9 months ago
off_period_model.php
1 year ago
order_intent_meta_model.php
1 year ago
order_intent_model.php
9 months ago
order_item_model.php
1 year ago
order_meta_model.php
1 year ago
order_model.php
11 months ago
otp_model.php
9 months ago
payment_request_model.php
1 year ago
process_job_model.php
1 year ago
process_model.php
1 year ago
recurrence_model.php
1 year ago
service_category_model.php
9 months ago
service_meta_model.php
1 year ago
service_model.php
9 months ago
session_model.php
1 year ago
settings_model.php
1 year ago
step_settings_model.php
1 year ago
transaction_intent_model.php
1 year ago
transaction_model.php
1 year ago
transaction_refund_model.php
1 year ago
work_period_model.php
1 year ago
bundle_model.php
295 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2023 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | class OsBundleModel extends OsModel { |
| 7 | var $services; |
| 8 | |
| 9 | var $id, |
| 10 | $name, |
| 11 | $short_description, |
| 12 | $charge_amount, |
| 13 | $deposit_amount, |
| 14 | $status, |
| 15 | $visibility, |
| 16 | $order_number, |
| 17 | $updated_at, |
| 18 | $created_at; |
| 19 | |
| 20 | function __construct($id = false) { |
| 21 | parent::__construct(); |
| 22 | $this->table_name = LATEPOINT_TABLE_BUNDLES; |
| 23 | $this->join_table_name_bundles_services = LATEPOINT_TABLE_JOIN_BUNDLES_SERVICES; |
| 24 | |
| 25 | if ($id) { |
| 26 | $this->load_by_id($id); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public function generate_data_vars(): array { |
| 31 | $vars = [ |
| 32 | 'id' => $this->id, |
| 33 | 'name' => $this->name |
| 34 | ]; |
| 35 | |
| 36 | return $vars; |
| 37 | } |
| 38 | |
| 39 | function has_service($service_id): bool{ |
| 40 | $services = $this->get_services(); |
| 41 | foreach($services as $service){ |
| 42 | if($service->id == $service_id) return true; |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | function quantity_for_service($service_id): int{ |
| 48 | $services = $this->get_services(); |
| 49 | foreach($services as $service){ |
| 50 | if($service->id == $service_id) return (!empty($service->join_attributes['quantity']) ? $service->join_attributes['quantity'] : 0); |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | function duration_for_service($service_id): int{ |
| 56 | $services = $this->get_services(); |
| 57 | foreach($services as $service){ |
| 58 | if($service->id == $service_id) return (!empty($service->join_attributes['duration']) ? $service->join_attributes['duration'] : $service->duration); |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | function total_attendees_for_service($service_id): int{ |
| 64 | $services = $this->get_services(); |
| 65 | foreach($services as $service){ |
| 66 | if($service->id == $service_id) return (!empty($service->join_attributes['total_attendees']) ? $service->join_attributes['total_attendees'] : 1); |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | public function generate_params_for_booking_form(){ |
| 72 | $params = [ |
| 73 | "bundle_id" => $this->id |
| 74 | ]; |
| 75 | |
| 76 | /** |
| 77 | * Returns an array of params generated from OsBundleModel to be used in a booking form |
| 78 | * |
| 79 | * @since 5.0.0 |
| 80 | * @hook latepoint_generated_bundle_params_for_booking_form |
| 81 | * |
| 82 | * @param {array} $params Array of booking params |
| 83 | * @param {OsBundleModel} $bundle Instance of <code>OsBundleModel</code> that params are being generated for |
| 84 | * |
| 85 | * @returns {array} Filtered array of booking params |
| 86 | */ |
| 87 | return apply_filters('latepoint_generated_bundle_params_for_booking_form', $params, $this); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * @return mixed|void |
| 93 | * |
| 94 | * Returns full amount to charge in database format 1999.0000 |
| 95 | * |
| 96 | */ |
| 97 | public function full_amount_to_charge(){ |
| 98 | return OsBundlesHelper::calculate_full_amount_for_bundle($this); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return mixed|void |
| 103 | * |
| 104 | * Returns deposit amount to charge in database format 1999.0000 |
| 105 | * |
| 106 | */ |
| 107 | public function deposit_amount_to_charge(){ |
| 108 | return OsBundlesHelper::calculate_deposit_amount_for_bundle($this); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | public function save_services($services){ |
| 113 | if(!$services) return true; |
| 114 | $connections_to_save = []; |
| 115 | $connections_to_remove = []; |
| 116 | foreach($services as $service_key => $service){ |
| 117 | $service_id = str_replace('service_', '', $service_key); |
| 118 | $connection = [ |
| 119 | 'bundle_id' => $this->id, |
| 120 | 'service_id' => $service_id, |
| 121 | 'quantity' => $service['quantity'], |
| 122 | 'total_attendees' => $service['total_attendees'], |
| 123 | 'duration' => $service['duration'], |
| 124 | ]; |
| 125 | if($service['connected'] == 'yes'){ |
| 126 | $connections_to_save[] = $connection; |
| 127 | }else{ |
| 128 | $connections_to_remove[] = $connection; |
| 129 | } |
| 130 | } |
| 131 | if(!empty($connections_to_save)){ |
| 132 | foreach($connections_to_save as $connection_to_save){ |
| 133 | $join_bundle_service = new OsJoinBundlesServicesModel(); |
| 134 | $existing = $join_bundle_service->where(['bundle_id' => $connection_to_save['bundle_id'], 'service_id' => $connection_to_save['service_id']])->set_limit(1)->get_results_as_models(); |
| 135 | if($existing){ |
| 136 | $existing->quantity = $connection_to_save['quantity']; |
| 137 | $existing->total_attendees = $connection_to_save['total_attendees']; |
| 138 | $existing->duration = $connection_to_save['duration']; |
| 139 | $existing->save(); |
| 140 | }else{ |
| 141 | $join_bundle_service->set_data($connection_to_save); |
| 142 | $join_bundle_service->save(); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | if(!empty($connections_to_remove)){ |
| 147 | foreach($connections_to_remove as $connection_to_remove){ |
| 148 | $join_bundle_service = new OsJoinBundlesServicesModel(); |
| 149 | $join_bundle_service->delete_where(['bundle_id' => $connection_to_remove['bundle_id'], 'service_id' => $connection_to_remove['service_id']]); |
| 150 | } |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | public function get_formatted_charge_amount(){ |
| 157 | if($this->charge_amount > 0){ |
| 158 | return OsMoneyHelper::format_price($this->charge_amount); |
| 159 | }else{ |
| 160 | return 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public function get_service_and_quantity_descriptions(): array{ |
| 165 | $bundle_services = $this->get_services(); |
| 166 | $bundle_services_descriptions = []; |
| 167 | foreach ($bundle_services as $service) { |
| 168 | $qty = $service->join_attributes['quantity']; |
| 169 | $qty_html = $qty > 1 ? ' [' . $qty . ']' : ''; |
| 170 | $bundle_services_descriptions[] = $service->name . $qty_html; |
| 171 | } |
| 172 | return $bundle_services_descriptions; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | public function get_services($order_item_id = false) : array{ |
| 177 | if (!isset($this->services)) { |
| 178 | $bundle_services = new OsJoinBundlesServicesModel(); |
| 179 | $bundle_services = $bundle_services->get_services_for_bundle_id($this->id); |
| 180 | |
| 181 | $this->services = []; |
| 182 | |
| 183 | if ($bundle_services) { |
| 184 | foreach ($bundle_services as $bundle_service) { |
| 185 | $service = new OsServiceModel($bundle_service->service_id); |
| 186 | $service->join_attributes['quantity'] = $bundle_service->quantity; |
| 187 | $service->join_attributes['total_attendees'] = $bundle_service->total_attendees; |
| 188 | $service->join_attributes['duration'] = $bundle_service->duration; |
| 189 | if($order_item_id){ |
| 190 | $bookings = new OsBookingModel(); |
| 191 | $service->join_attributes['total_scheduled_bookings'] = $bookings->where(['order_item_id' => $order_item_id, 'service_id' => $service->id])->should_not_be_cancelled()->count(); |
| 192 | } |
| 193 | $this->services[] = $service; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return $this->services; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | public function is_hidden(){ |
| 202 | return ($this->visibility == LATEPOINT_BUNDLE_VISIBILITY_HIDDEN); |
| 203 | } |
| 204 | |
| 205 | public function should_be_active(){ |
| 206 | return $this->where(['status' => LATEPOINT_BUNDLE_STATUS_ACTIVE]); |
| 207 | } |
| 208 | |
| 209 | public function should_not_be_hidden(){ |
| 210 | return $this->where(['visibility !=' => LATEPOINT_BUNDLE_VISIBILITY_HIDDEN]); |
| 211 | } |
| 212 | |
| 213 | public function is_active(){ |
| 214 | return ($this->status == LATEPOINT_BUNDLE_STATUS_ACTIVE); |
| 215 | } |
| 216 | |
| 217 | public function delete_meta_by_key($meta_key){ |
| 218 | if($this->is_new_record()) return false; |
| 219 | |
| 220 | $meta = new OsBundleMetaModel(); |
| 221 | return $meta->delete_by_key($meta_key, $this->id); |
| 222 | } |
| 223 | |
| 224 | public function get_meta_by_key($meta_key, $default = false){ |
| 225 | if($this->is_new_record()) return $default; |
| 226 | |
| 227 | $meta = new OsBundleMetaModel(); |
| 228 | return $meta->get_by_key($meta_key, $this->id, $default); |
| 229 | } |
| 230 | |
| 231 | public function save_meta_by_key($meta_key, $meta_value){ |
| 232 | if($this->is_new_record()) return false; |
| 233 | |
| 234 | $meta = new OsBundleMetaModel(); |
| 235 | return $meta->save_by_key($meta_key, $meta_value, $this->id); |
| 236 | } |
| 237 | |
| 238 | public function delete($id = false){ |
| 239 | if(!$id && isset($this->id)){ |
| 240 | $id = $this->id; |
| 241 | } |
| 242 | |
| 243 | if($id && $this->db->delete( $this->table_name, array('id' => $id), array( '%d' ))){ |
| 244 | $this->db->delete(LATEPOINT_TABLE_BUNDLE_META, array('object_id' => $id), array( '%d' ) ); |
| 245 | do_action('latepoint_bundle_deleted', $id); |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | protected function properties_to_validate(){ |
| 254 | $validations = array( |
| 255 | 'name' => array('presence'), |
| 256 | ); |
| 257 | return $validations; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | protected function params_to_sanitize(){ |
| 262 | return ['charge_amount' => 'money', |
| 263 | 'deposit_amount' => 'money' |
| 264 | ]; |
| 265 | } |
| 266 | |
| 267 | protected function allowed_params($role = 'admin'){ |
| 268 | $allowed_params = array('id', |
| 269 | 'name', |
| 270 | 'short_description', |
| 271 | 'charge_amount', |
| 272 | 'deposit_amount', |
| 273 | 'status', |
| 274 | 'visibility', |
| 275 | 'order_number', |
| 276 | 'updated_at', |
| 277 | 'created_at'); |
| 278 | return $allowed_params; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | protected function params_to_save($role = 'admin'){ |
| 283 | $params_to_save = array('id', |
| 284 | 'name', |
| 285 | 'short_description', |
| 286 | 'charge_amount', |
| 287 | 'deposit_amount', |
| 288 | 'status', |
| 289 | 'visibility', |
| 290 | 'order_number', |
| 291 | 'updated_at', |
| 292 | 'created_at'); |
| 293 | return $params_to_save; |
| 294 | } |
| 295 | } |