late-point.php
318 lines
| 1 | <?php |
| 2 | /** |
| 3 | * LatePoint core integrations file |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package SureTrigger |
| 7 | */ |
| 8 | |
| 9 | namespace SureTriggers\Integrations\LatePoint; |
| 10 | |
| 11 | use Exception; |
| 12 | use OsAgentModel; |
| 13 | use OsBookingModel; |
| 14 | use OsCustomerModel; |
| 15 | use OsOrderModel; |
| 16 | use OsOrdersHelper; |
| 17 | use OsOrderItemModel; |
| 18 | use SureTriggers\Controllers\IntegrationsController; |
| 19 | use SureTriggers\Integrations\Integrations; |
| 20 | use SureTriggers\Traits\SingletonLoader; |
| 21 | |
| 22 | /** |
| 23 | * Class SureTrigger |
| 24 | * |
| 25 | * @package SureTriggers\Integrations\LatePoint |
| 26 | */ |
| 27 | class LatePoint extends Integrations { |
| 28 | |
| 29 | use SingletonLoader; |
| 30 | |
| 31 | /** |
| 32 | * ID |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $id = 'LatePoint'; |
| 37 | |
| 38 | /** |
| 39 | * SureTrigger constructor. |
| 40 | */ |
| 41 | public function __construct() { |
| 42 | $this->name = __( 'LatePoint', 'suretriggers' ); |
| 43 | $this->description = __( 'Appointment Scheduling Plugin for WordPress.', 'suretriggers' ); |
| 44 | $this->icon_url = SURE_TRIGGERS_URL . 'assets/icons/late-point.svg'; |
| 45 | |
| 46 | parent::__construct(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Create/Update booking. |
| 51 | * |
| 52 | * @param array $selected_options Selected options. |
| 53 | * @param bool $is_update is update. |
| 54 | * @return array |
| 55 | * @throws Exception Exception. |
| 56 | */ |
| 57 | public static function create_or_update_booking( $selected_options, $is_update = false ) { |
| 58 | if ( ! class_exists( 'OsBookingModel' ) || ! class_exists( 'OsCustomerModel' ) || ! class_exists( 'OsOrderModel' ) || ! class_exists( 'OsOrderItemModel' ) || ! class_exists( 'OsOrdersHelper' ) ) { |
| 59 | throw new Exception( 'LatePoint plugin not installed.' ); |
| 60 | } |
| 61 | |
| 62 | if ( $is_update ) { |
| 63 | $booking_id = isset( $selected_options['booking_id'] ) ? $selected_options['booking_id'] : null; |
| 64 | if ( ! $booking_id ) { |
| 65 | throw new Exception( 'Booking ID not provided.' ); |
| 66 | } |
| 67 | |
| 68 | $booking = new OsBookingModel( $booking_id ); |
| 69 | if ( ! isset( $booking->id ) || ! $booking->id ) { |
| 70 | throw new Exception( 'Booking not found.' ); |
| 71 | } |
| 72 | $old_booking = clone $booking; |
| 73 | } else { |
| 74 | $booking = new OsBookingModel(); |
| 75 | } |
| 76 | |
| 77 | $customer_type = isset( $selected_options['customer_type'] ) ? $selected_options['customer_type'] : 'new'; |
| 78 | $customer_id = null; |
| 79 | |
| 80 | if ( 'existing' === $customer_type ) { |
| 81 | $customer_id = isset( $selected_options['customer_id'] ) ? $selected_options['customer_id'] : null; |
| 82 | if ( ! $customer_id ) { |
| 83 | throw new Exception( 'Customer ID not provided.' ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $start_date = isset( $selected_options['start_date'] ) ? gmdate( 'Y-m-d', strtotime( $selected_options['start_date'] ) ) : ''; |
| 88 | |
| 89 | $convert_to_minutes = function( $time ) { |
| 90 | if ( $time ) { |
| 91 | if ( ! preg_match( '/^\d{2}:\d{2}$/', $time ) ) { |
| 92 | throw new Exception( 'Invalid time format. Expected HH:MM format.' ); |
| 93 | } |
| 94 | $time_parts = explode( ':', $time ); |
| 95 | $hours = (int) $time_parts[0]; |
| 96 | $minutes = (int) $time_parts[1]; |
| 97 | return ( $hours * 60 ) + $minutes; |
| 98 | } |
| 99 | return null; |
| 100 | }; |
| 101 | $start_time = null; |
| 102 | $end_time = null; |
| 103 | |
| 104 | if ( isset( $selected_options['start_time'] ) ) { |
| 105 | $start_time = $convert_to_minutes( $selected_options['start_time'] ); |
| 106 | } |
| 107 | |
| 108 | if ( isset( $selected_options['end_time'] ) ) { |
| 109 | $end_time = $convert_to_minutes( $selected_options['end_time'] ); |
| 110 | } |
| 111 | |
| 112 | $booking_params = [ |
| 113 | 'agent_id' => isset( $selected_options['agent_id'] ) ? $selected_options['agent_id'] : null, |
| 114 | 'location_id' => isset( $selected_options['agent_id'] ) ? $selected_options['agent_id'] : null, |
| 115 | 'status' => isset( $selected_options['status'] ) ? $selected_options['status'] : '', |
| 116 | 'total_attendees' => isset( $selected_options['total_attendees'] ) ? $selected_options['total_attendees'] : 1, |
| 117 | 'service_id' => isset( $selected_options['service_id'] ) ? $selected_options['service_id'] : null, |
| 118 | 'start_date' => $start_date, |
| 119 | 'start_time' => $start_time, |
| 120 | 'end_time' => $end_time, |
| 121 | 'customer_comment' => isset( $selected_options['customer_comment'] ) ? $selected_options['customer_comment'] : '', |
| 122 | 'payment_status' => 'not_paid', |
| 123 | 'buffer_before' => isset( $selected_options['buffer_before'] ) ? $selected_options['buffer_before'] : 0, |
| 124 | 'buffer_after' => isset( $selected_options['buffer_after'] ) ? $selected_options['buffer_after'] : 0, |
| 125 | 'source_url' => site_url(), |
| 126 | ]; |
| 127 | $booking_custom_fields = []; |
| 128 | if ( ! empty( $selected_options['booking_fields'] ) ) { |
| 129 | foreach ( $selected_options['booking_fields'] as $field ) { |
| 130 | if ( is_array( $field ) && ! empty( $field ) ) { |
| 131 | foreach ( $field as $key => $value ) { |
| 132 | if ( false === strpos( $key, 'field_column' ) && '' !== $value ) { |
| 133 | $booking_custom_fields[ $key ] = $value; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | $booking_params['custom_fields'] = $booking_custom_fields; |
| 142 | |
| 143 | $booking->set_data( $booking_params ); |
| 144 | |
| 145 | // Set custom end time/date if it was passed in params. |
| 146 | if ( isset( $booking_params['end_time']['formatted_value'] ) ) { |
| 147 | $booking->set_custom_end_time_and_date( $booking_params ); |
| 148 | } |
| 149 | |
| 150 | if ( 'new' === $customer_type ) { |
| 151 | $customer_params = [ |
| 152 | 'first_name' => isset( $selected_options['customer_first_name'] ) ? $selected_options['customer_first_name'] : '', |
| 153 | 'last_name' => isset( $selected_options['customer_last_name'] ) ? $selected_options['customer_last_name'] : '', |
| 154 | 'email' => isset( $selected_options['customer_email'] ) ? $selected_options['customer_email'] : '', |
| 155 | 'phone' => isset( $selected_options['customer_phone'] ) ? $selected_options['customer_phone'] : '', |
| 156 | 'notes' => isset( $selected_options['customer_notes'] ) ? $selected_options['customer_notes'] : '', |
| 157 | ]; |
| 158 | |
| 159 | $old_customer_data = []; |
| 160 | $customer = new OsCustomerModel(); |
| 161 | $customer = $customer->where( [ 'email' => $customer_params['email'] ] )->set_limit( 1 )->get_results_as_models(); |
| 162 | |
| 163 | if ( isset( $customer->id ) && ! empty( $customer->id ) ) { |
| 164 | $is_new_customer = false; |
| 165 | $customer = new OsCustomerModel( $customer->id ); |
| 166 | $old_customer_data = $customer->get_data_vars(); |
| 167 | } else { |
| 168 | $is_new_customer = true; |
| 169 | $customer = new OsCustomerModel(); |
| 170 | } |
| 171 | $customer_custom_fields = []; |
| 172 | if ( ! empty( $selected_options['customer_fields'] ) ) { |
| 173 | foreach ( $selected_options['customer_fields'] as $field ) { |
| 174 | if ( is_array( $field ) && ! empty( $field ) ) { |
| 175 | foreach ( $field as $key => $value ) { |
| 176 | if ( false === strpos( $key, 'field_column' ) && '' !== $value ) { |
| 177 | $customer_custom_fields[ $key ] = $value; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | $customer_params['custom_fields'] = $customer_custom_fields; |
| 184 | $customer->set_data( $customer_params ); |
| 185 | if ( ! $customer->save() ) { |
| 186 | $errors = $customer->get_error_messages(); |
| 187 | $error_msg = isset( $errors[0] ) ? $errors[0] : 'Customer could not be created.'; |
| 188 | throw new Exception( $error_msg ); |
| 189 | } |
| 190 | |
| 191 | if ( $is_new_customer ) { |
| 192 | do_action( 'latepoint_customer_created', $customer ); |
| 193 | } else { |
| 194 | do_action( 'latepoint_customer_updated', $customer, $old_customer_data ); |
| 195 | } |
| 196 | } else { |
| 197 | $customer = new OsCustomerModel( $customer_id ); |
| 198 | if ( ! $customer->id ) { |
| 199 | throw new Exception( 'Customer not found.' ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | $order = new OsOrderModel(); |
| 204 | $order->status = isset( $selected_options['status'] ) ? $selected_options['status'] : OsOrdersHelper::get_default_order_status(); |
| 205 | $order->fulfillment_status = $order->get_default_fulfillment_status(); |
| 206 | $order->customer_comment = isset( $selected_options['customer_comment'] ) ? $selected_options['customer_comment'] : ''; |
| 207 | $order->customer_id = $customer->id; |
| 208 | $order->payment_status = 'not_paid'; |
| 209 | |
| 210 | // Save the order and check for errors. |
| 211 | if ( ! $order->save() ) { |
| 212 | $errors = $order->get_error_messages(); |
| 213 | $error_msg = isset( $errors[0] ) ? $errors[0] : 'Order could not be created.'; |
| 214 | throw new Exception( $error_msg ); |
| 215 | } |
| 216 | |
| 217 | $order_item_model = new OsOrderItemModel(); |
| 218 | $order_item_model->variant = 'booking'; |
| 219 | $order_item_model->order_id = $order->id; |
| 220 | |
| 221 | if ( $order_item_model->save() ) { |
| 222 | $booking->customer_id = $order->customer_id; |
| 223 | $booking->order_item_id = $order_item_model->id; |
| 224 | if ( $booking->save() ) { |
| 225 | $order_item_model->item_data = $booking->generate_item_data(); |
| 226 | $order_item_model->recalculate_prices(); |
| 227 | $order->total = $order_item_model->total; |
| 228 | $order->subtotal = $order_item_model->subtotal; |
| 229 | $order->save(); |
| 230 | $order_item_model->save(); |
| 231 | } |
| 232 | } else { |
| 233 | $errors = $order_item_model->get_error_messages(); |
| 234 | $error_msg = isset( $errors[0] ) ? $errors[0] : 'Order Item could not be created.'; |
| 235 | throw new Exception( $error_msg ); |
| 236 | } |
| 237 | |
| 238 | $booking->set_utc_datetimes(); |
| 239 | |
| 240 | if ( ! $booking->save() ) { |
| 241 | $errors = $booking->get_error_messages(); |
| 242 | $operation = $is_update ? 'updated' : 'created'; |
| 243 | $error_msg = isset( $errors[0] ) ? $errors[0] : 'Booking could not be ' . $operation . '.'; |
| 244 | throw new Exception( $error_msg ); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | if ( $is_update ) { |
| 249 | do_action( 'latepoint_booking_updated', $booking, $old_booking ); |
| 250 | } else { |
| 251 | |
| 252 | do_action( 'latepoint_booking_created', $booking ); |
| 253 | do_action( 'latepoint_order_created', $order ); |
| 254 | } |
| 255 | $return_data = $booking->get_data_vars(); |
| 256 | $return_data['service_id'] = ! empty( $booking->service_id ) ? $booking->service_id : ( ! empty( $booking_params['service_id'] ) ? $booking_params['service_id'] : null ); |
| 257 | $return_data['order'] = $order->get_data_vars(); |
| 258 | $return_data['total_attendees'] = $selected_options['total_attendees']; |
| 259 | return $return_data; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Find object by email. |
| 264 | * |
| 265 | * @param array $selected_options selected options. |
| 266 | * @param string $object model name. |
| 267 | * @return array |
| 268 | * @throws Exception Exception. |
| 269 | */ |
| 270 | public static function find_object_by_email( $selected_options, $object ) { |
| 271 | |
| 272 | if ( ! class_exists( 'OsAgentModel' ) || ! class_exists( 'OsCustomerModel' ) ) { |
| 273 | throw new Exception( 'LatePoint plugin not installed.' ); |
| 274 | } |
| 275 | |
| 276 | $email = isset( $selected_options['email'] ) ? trim( $selected_options['email'] ) : ''; |
| 277 | |
| 278 | if ( empty( $email ) ) { |
| 279 | throw new Exception( $object . ' Email Address not provided.' ); |
| 280 | } |
| 281 | |
| 282 | $model = 'Agent' === $object ? new OsAgentModel() : new OsCustomerModel(); |
| 283 | $model = $model->where( [ 'email' => $email ] )->set_limit( 1 )->get_results( ARRAY_A ); |
| 284 | |
| 285 | $model_data = []; |
| 286 | $model_data['found'] = 'no'; |
| 287 | if ( 'Customer' == $object ) { |
| 288 | global $wpdb; |
| 289 | $customer_fields = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key,meta_value FROM {$wpdb->prefix}latepoint_customer_meta WHERE object_id= %s", intval( $model['id'] ) ) ); |
| 290 | if ( ! empty( $customer_fields ) ) { |
| 291 | foreach ( $customer_fields as $field ) { |
| 292 | $model[ $field->meta_key ] = $field->meta_value; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | if ( $model ) { |
| 299 | unset( $model['password'] ); |
| 300 | $model_data = $model; |
| 301 | $model_data['found'] = 'yes'; |
| 302 | } |
| 303 | |
| 304 | return $model_data; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Is Plugin depended on plugin is installed or not. |
| 309 | * |
| 310 | * @return bool |
| 311 | */ |
| 312 | public function is_plugin_installed() { |
| 313 | return class_exists( 'LatePoint' ); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | IntegrationsController::register( LatePoint::class ); |
| 318 |