add-booking-to-order.php
7 months ago
cancel-booking.php
11 months ago
create-agent.php
11 months ago
create-booking.php
1 year ago
create-coupon.php
11 months ago
create-customer.php
11 months ago
create-order.php
11 months ago
find-agent-by-email.php
2 years ago
find-booking-by-id.php
11 months ago
find-customer-by-email.php
2 years ago
list-bundles.php
1 year ago
update-booking.php
2 years ago
update-coupon.php
11 months ago
update-customer.php
3 months ago
add-booking-to-order.php
244 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AddBookingToOrder. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category AddBookingToOrder |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\LatePoint\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use OsBookingModel; |
| 18 | use OsCustomerModel; |
| 19 | use OsOrderModel; |
| 20 | use OsOrderItemModel; |
| 21 | use OsOrdersHelper; |
| 22 | use SureTriggers\Integrations\AutomateAction; |
| 23 | use SureTriggers\Traits\SingletonLoader; |
| 24 | |
| 25 | /** |
| 26 | * AddBookingToOrder |
| 27 | * |
| 28 | * @category AddBookingToOrder |
| 29 | * @package SureTriggers |
| 30 | * @author BSF <username@example.com> |
| 31 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 32 | * @link https://www.brainstormforce.com/ |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | class AddBookingToOrder extends AutomateAction { |
| 36 | |
| 37 | /** |
| 38 | * Integration type. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $integration = 'LatePoint'; |
| 43 | |
| 44 | /** |
| 45 | * Action name. |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | public $action = 'lp_add_booking_to_order'; |
| 50 | |
| 51 | use SingletonLoader; |
| 52 | |
| 53 | /** |
| 54 | * Register action. |
| 55 | * |
| 56 | * @param array $actions action data. |
| 57 | * @return array |
| 58 | */ |
| 59 | public function register( $actions ) { |
| 60 | $actions[ $this->integration ][ $this->action ] = [ |
| 61 | 'label' => __( 'Add Booking to Existing Order', 'suretriggers' ), |
| 62 | 'action' => 'lp_add_booking_to_order', |
| 63 | 'function' => [ $this, 'action_listener' ], |
| 64 | ]; |
| 65 | return $actions; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Action listener. |
| 70 | * |
| 71 | * @param int $user_id user_id. |
| 72 | * @param int $automation_id automation_id. |
| 73 | * @param array $fields fields. |
| 74 | * @param array $selected_options selectedOptions. |
| 75 | * |
| 76 | * @throws Exception Exception. |
| 77 | * |
| 78 | * @return array |
| 79 | */ |
| 80 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 81 | if ( ! class_exists( 'OsOrderModel' ) || ! class_exists( 'OsBookingModel' ) || |
| 82 | ! class_exists( 'OsOrderItemModel' ) || ! class_exists( 'OsOrdersHelper' ) ) { |
| 83 | return [ |
| 84 | 'status' => 'error', |
| 85 | 'message' => __( 'LatePoint plugin not installed.', 'suretriggers' ), |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | $required_params = [ |
| 90 | 'order_id', |
| 91 | 'service_id', |
| 92 | 'agent_id', |
| 93 | 'start_date', |
| 94 | 'start_time', |
| 95 | ]; |
| 96 | |
| 97 | foreach ( $required_params as $param ) { |
| 98 | if ( empty( $selected_options[ $param ] ) ) { |
| 99 | return [ |
| 100 | 'status' => 'error', |
| 101 | 'message' => sprintf( __( 'Missing required parameter: %s', 'suretriggers' ), $param ), |
| 102 | ]; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | $order_id = intval( $selected_options['order_id'] ); |
| 107 | $order = new OsOrderModel( $order_id ); |
| 108 | |
| 109 | if ( ! $order->id ) { |
| 110 | return [ |
| 111 | 'status' => 'error', |
| 112 | 'message' => __( 'Order not found.', 'suretriggers' ), |
| 113 | ]; |
| 114 | } |
| 115 | |
| 116 | try { |
| 117 | // Convert time to minutes. |
| 118 | $convert_to_minutes = function( $time ) { |
| 119 | if ( $time ) { |
| 120 | if ( ! preg_match( '/^\d{2}:\d{2}$/', $time ) ) { |
| 121 | throw new Exception( __( 'Invalid time format. Expected HH:MM format.', 'suretriggers' ) ); |
| 122 | } |
| 123 | $time_parts = explode( ':', $time ); |
| 124 | $hours = (int) $time_parts[0]; |
| 125 | $minutes = (int) $time_parts[1]; |
| 126 | return ( $hours * 60 ) + $minutes; |
| 127 | } |
| 128 | return null; |
| 129 | }; |
| 130 | |
| 131 | $start_time = $convert_to_minutes( $selected_options['start_time'] ); |
| 132 | $end_time = isset( $selected_options['end_time'] ) ? $convert_to_minutes( $selected_options['end_time'] ) : null; |
| 133 | |
| 134 | $start_date = gmdate( 'Y-m-d', strtotime( $selected_options['start_date'] ) ); |
| 135 | $start_date_formatted = gmdate( 'm/d/Y', strtotime( $selected_options['start_date'] ) ); |
| 136 | $end_date = $start_date; |
| 137 | |
| 138 | $booking_params = [ |
| 139 | 'agent_id' => intval( $selected_options['agent_id'] ), |
| 140 | 'service_id' => intval( $selected_options['service_id'] ), |
| 141 | 'customer_id' => $order->customer_id, |
| 142 | 'location_id' => isset( $selected_options['location_id'] ) ? intval( $selected_options['location_id'] ) : 1, |
| 143 | 'start_date' => $start_date, |
| 144 | 'start_date_formatted' => $start_date_formatted, |
| 145 | 'end_date' => $end_date, |
| 146 | 'start_time' => $start_time, |
| 147 | 'end_time' => $end_time, |
| 148 | 'status' => isset( $selected_options['status'] ) ? $selected_options['status'] : 'approved', |
| 149 | 'total_attendees' => isset( $selected_options['total_attendees'] ) ? intval( $selected_options['total_attendees'] ) : 1, |
| 150 | 'customer_comment' => isset( $selected_options['customer_comment'] ) ? sanitize_textarea_field( $selected_options['customer_comment'] ) : '', |
| 151 | 'payment_status' => 'not_paid', |
| 152 | 'buffer_before' => isset( $selected_options['buffer_before'] ) ? intval( $selected_options['buffer_before'] ) : 0, |
| 153 | 'buffer_after' => isset( $selected_options['buffer_after'] ) ? intval( $selected_options['buffer_after'] ) : 0, |
| 154 | 'source_url' => site_url(), |
| 155 | ]; |
| 156 | |
| 157 | $booking_custom_fields = []; |
| 158 | if ( ! empty( $selected_options['booking_fields'] ) ) { |
| 159 | foreach ( $selected_options['booking_fields'] as $field ) { |
| 160 | if ( is_array( $field ) && ! empty( $field ) ) { |
| 161 | foreach ( $field as $key => $value ) { |
| 162 | if ( false === strpos( $key, 'field_column' ) && '' !== $value ) { |
| 163 | $booking_custom_fields[ $key ] = sanitize_text_field( $value ); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | $booking_params['custom_fields'] = $booking_custom_fields; |
| 170 | |
| 171 | $order_item = new OsOrderItemModel(); |
| 172 | $order_item->order_id = $order->id; |
| 173 | $order_item->variant = defined( 'LATEPOINT_ITEM_VARIANT_BOOKING' ) ? LATEPOINT_ITEM_VARIANT_BOOKING : 'booking'; |
| 174 | |
| 175 | if ( ! $order_item->save() ) { |
| 176 | return [ |
| 177 | 'status' => 'error', |
| 178 | 'message' => __( 'Failed to create order item for booking.', 'suretriggers' ), |
| 179 | ]; |
| 180 | } |
| 181 | |
| 182 | // Use LatePoint helper for better consistency. |
| 183 | $booking = OsOrdersHelper::create_booking_object_from_booking_data_form( $booking_params ); |
| 184 | $booking->customer_id = $order->customer_id; |
| 185 | $booking->order_item_id = $order_item->id; |
| 186 | |
| 187 | if ( ! $booking->save() ) { |
| 188 | $order_item->delete(); |
| 189 | return [ |
| 190 | 'status' => 'error', |
| 191 | 'message' => sprintf( |
| 192 | __( 'Failed to save booking: %s', 'suretriggers' ), |
| 193 | implode( ', ', $booking->get_error_messages() ) |
| 194 | ), |
| 195 | ]; |
| 196 | } |
| 197 | |
| 198 | // Update order item following LatePoint pattern. |
| 199 | if ( $order_item->is_booking() ) { |
| 200 | $order_item->item_data = $booking->generate_item_data(); |
| 201 | $order_item->recalculate_prices(); |
| 202 | $order_item->save(); |
| 203 | } |
| 204 | |
| 205 | if ( method_exists( $order, 'recalculate_totals' ) ) { |
| 206 | $order->recalculate_totals(); |
| 207 | } else { |
| 208 | $order_items = $order->get_items(); |
| 209 | $total = 0; |
| 210 | $subtotal = 0; |
| 211 | foreach ( $order_items as $item ) { |
| 212 | $total += $item->total; |
| 213 | $subtotal += $item->subtotal; |
| 214 | } |
| 215 | $order->total = $total; |
| 216 | $order->subtotal = $subtotal; |
| 217 | } |
| 218 | |
| 219 | $order->save(); |
| 220 | |
| 221 | do_action( 'latepoint_booking_created', $booking ); |
| 222 | |
| 223 | return [ |
| 224 | 'status' => 'success', |
| 225 | 'message' => __( 'Booking added to order successfully.', 'suretriggers' ), |
| 226 | 'booking' => $booking->get_data_vars(), |
| 227 | 'order' => [ |
| 228 | 'id' => $order->id, |
| 229 | 'total' => $order->total, |
| 230 | 'subtotal' => $order->subtotal, |
| 231 | ], |
| 232 | ]; |
| 233 | |
| 234 | } catch ( Exception $e ) { |
| 235 | return [ |
| 236 | 'status' => 'error', |
| 237 | 'message' => $e->getMessage(), |
| 238 | ]; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | AddBookingToOrder::get_instance(); |
| 244 |