PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.36
OttoKit: All-in-One Automation Platform v1.1.36
1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / late-point / actions / add-booking-to-order.php
suretriggers / src / Integrations / late-point / actions Last commit date
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