PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.3
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.3
5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / bundle_model.php
latepoint / lib / models Last commit date
activity_model.php 1 year ago agent_meta_model.php 1 year ago agent_model.php 1 year ago booking_meta_model.php 1 year ago booking_model.php 1 year ago bundle_model.php 1 year 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 1 year ago invoice_model.php 1 year ago join_bundles_services_model.php 1 year ago location_category_model.php 1 year ago location_model.php 1 year ago meta_model.php 1 year ago model.php 1 year ago order_intent_meta_model.php 1 year ago order_intent_model.php 1 year ago order_item_model.php 1 year ago order_meta_model.php 1 year ago order_model.php 1 year ago payment_request_model.php 1 year ago process_job_model.php 1 year ago process_model.php 1 year ago service_category_model.php 1 year ago service_meta_model.php 1 year ago service_model.php 1 year 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
260 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
218 protected function properties_to_validate(){
219 $validations = array(
220 'name' => array('presence'),
221 );
222 return $validations;
223 }
224
225
226 protected function params_to_sanitize(){
227 return ['charge_amount' => 'money',
228 'deposit_amount' => 'money'
229 ];
230 }
231
232 protected function allowed_params($role = 'admin'){
233 $allowed_params = array('id',
234 'name',
235 'short_description',
236 'charge_amount',
237 'deposit_amount',
238 'status',
239 'visibility',
240 'order_number',
241 'updated_at',
242 'created_at');
243 return $allowed_params;
244 }
245
246
247 protected function params_to_save($role = 'admin'){
248 $params_to_save = array('id',
249 'name',
250 'short_description',
251 'charge_amount',
252 'deposit_amount',
253 'status',
254 'visibility',
255 'order_number',
256 'updated_at',
257 'created_at');
258 return $params_to_save;
259 }
260 }