add-booking-to-order.php
5 months ago
cancel-booking.php
10 months ago
create-agent.php
10 months ago
create-booking.php
1 year ago
create-coupon.php
10 months ago
create-customer.php
10 months ago
create-order.php
10 months ago
find-agent-by-email.php
2 years ago
find-booking-by-id.php
10 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
10 months ago
update-customer.php
2 months ago
create-order.php
235 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateBundleOrder. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateBundleOrder |
| 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 OsAgentModel; |
| 18 | use OsBookingModel; |
| 19 | use OsCustomerModel; |
| 20 | use OsOrderModel; |
| 21 | use OsOrdersHelper; |
| 22 | use OsOrderItemModel; |
| 23 | use OsBundleModel; |
| 24 | use SureTriggers\Integrations\AutomateAction; |
| 25 | use SureTriggers\Traits\SingletonLoader; |
| 26 | |
| 27 | /** |
| 28 | * CreateBundleOrder |
| 29 | * |
| 30 | * @category CreateBundleOrder |
| 31 | * @package SureTriggers |
| 32 | * @author BSF <username@example.com> |
| 33 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 34 | * @link https://www.brainstormforce.com/ |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | class CreateBundleOrder extends AutomateAction { |
| 38 | /** |
| 39 | * Integration type. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public $integration = 'LatePoint'; |
| 44 | |
| 45 | /** |
| 46 | * Action name. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | public $action = 'lp_create_order'; |
| 51 | |
| 52 | use SingletonLoader; |
| 53 | |
| 54 | /** |
| 55 | * Register action. |
| 56 | * |
| 57 | * @param array $actions action data. |
| 58 | * @return array |
| 59 | */ |
| 60 | public function register( $actions ) { |
| 61 | $actions[ $this->integration ][ $this->action ] = [ |
| 62 | 'label' => __( 'Create Bundle Order', 'suretriggers' ), |
| 63 | 'action' => 'lp_create_order', |
| 64 | 'function' => [ $this, 'action_listener' ], |
| 65 | ]; |
| 66 | return $actions; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Action listener. |
| 71 | * |
| 72 | * @param int $user_id user_id. |
| 73 | * @param int $automation_id automation_id. |
| 74 | * @param array $fields fields. |
| 75 | * @param array $selected_options selectedOptions. |
| 76 | * |
| 77 | * @throws Exception Exception. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 82 | if ( ! class_exists( 'OsBundleModel' ) || ! class_exists( 'OsCustomerModel' ) || |
| 83 | ! class_exists( 'OsOrderModel' ) || ! class_exists( 'OsOrderItemModel' ) ) { |
| 84 | return [ |
| 85 | 'status' => 'error', |
| 86 | 'message' => 'LatePoint plugin not installed.', |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | $required_params = [ |
| 91 | 'bundle_id', |
| 92 | 'customer_type', |
| 93 | ]; |
| 94 | |
| 95 | foreach ( $required_params as $param ) { |
| 96 | if ( ! isset( $selected_options[ $param ] ) ) { |
| 97 | return [ |
| 98 | 'status' => 'error', |
| 99 | 'message' => "Missing required parameter: {$param}", |
| 100 | ]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | $bundle = new OsBundleModel( $selected_options['bundle_id'] ); |
| 105 | |
| 106 | if ( ! $bundle->id ) { |
| 107 | return [ |
| 108 | 'status' => 'error', |
| 109 | 'message' => 'Invalid bundle ID provided.', |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | $customer_type = isset( $selected_options['customer_type'] ) ? |
| 114 | $selected_options['customer_type'] : 'new'; |
| 115 | |
| 116 | $customer_id = null; |
| 117 | if ( 'existing' === $customer_type ) { |
| 118 | $customer_id = isset( $selected_options['customer_id'] ) ? $selected_options['customer_id'] : null; |
| 119 | |
| 120 | if ( ! $customer_id ) { |
| 121 | return [ |
| 122 | 'status' => 'error', |
| 123 | 'message' => 'Customer ID not provided.', |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | if ( 'new' === $customer_type ) { |
| 130 | $customer_params = [ |
| 131 | 'first_name' => isset( $selected_options['customer_first_name'] ) ? $selected_options['customer_first_name'] : '', |
| 132 | 'last_name' => isset( $selected_options['customer_last_name'] ) ? $selected_options['customer_last_name'] : '', |
| 133 | 'email' => isset( $selected_options['customer_email'] ) ? $selected_options['customer_email'] : '', |
| 134 | 'phone' => isset( $selected_options['customer_phone'] ) ? $selected_options['customer_phone'] : '', |
| 135 | 'notes' => isset( $selected_options['customer_notes'] ) ? $selected_options['customer_notes'] : '', |
| 136 | 'admin_notes' => isset( $selected_options['admin_notes'] ) ? $selected_options['admin_notes'] : '', |
| 137 | ]; |
| 138 | |
| 139 | $customer_custom_fields = []; |
| 140 | if ( ! empty( $selected_options['customer_fields'] ) ) { |
| 141 | foreach ( $selected_options['customer_fields'] as $field ) { |
| 142 | if ( is_array( $field ) && ! empty( $field ) ) { |
| 143 | foreach ( $field as $key => $value ) { |
| 144 | if ( false === strpos( $key, 'field_column' ) && '' !== $value ) { |
| 145 | $customer_custom_fields[ $key ] = $value; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | $customer_params['custom_fields'] = $customer_custom_fields; |
| 153 | |
| 154 | $customer = new OsCustomerModel(); |
| 155 | $existing_customer = $customer->where( [ 'email' => $customer_params['email'] ] ) |
| 156 | ->set_limit( 1 ) |
| 157 | ->get_results_as_models(); |
| 158 | |
| 159 | if ( isset( $existing_customer->id ) && ! empty( $existing_customer->id ) ) { |
| 160 | $customer = new OsCustomerModel( $existing_customer->id ); |
| 161 | } else { |
| 162 | $customer = new OsCustomerModel(); |
| 163 | } |
| 164 | |
| 165 | $customer->set_data( $customer_params ); |
| 166 | |
| 167 | if ( ! $customer->save() ) { |
| 168 | $errors = $customer->get_error_messages(); |
| 169 | $error_msg = isset( $errors[0] ) ? $errors[0] : 'Customer could not be created.'; |
| 170 | return [ |
| 171 | 'status' => 'error', |
| 172 | 'message' => $error_msg, |
| 173 | ]; |
| 174 | } |
| 175 | } else { |
| 176 | $customer = new OsCustomerModel( $customer_id ); |
| 177 | if ( ! $customer->id ) { |
| 178 | return [ |
| 179 | 'status' => 'error', |
| 180 | 'message' => 'Customer not found.', |
| 181 | ]; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $order = new OsOrderModel(); |
| 186 | $order->status = isset( $selected_options['status'] ) ? $selected_options['status'] : 'open'; |
| 187 | $order->fulfillment_status = isset( $selected_options['fulfillment_status'] ) ? $selected_options['fulfillment_status'] : $order->get_default_fulfillment_status(); |
| 188 | $order->customer_id = $customer->id; |
| 189 | $order->payment_status = isset( $selected_options['payment_status'] ) ? $selected_options['payment_status'] : 'not_paid'; |
| 190 | |
| 191 | if ( ! $order->save() ) { |
| 192 | $errors = $order->get_error_messages(); |
| 193 | $error_msg = isset( $errors[0] ) ? $errors[0] : 'Order could not be created.'; |
| 194 | return [ |
| 195 | 'status' => 'error', |
| 196 | 'message' => $error_msg, |
| 197 | ]; |
| 198 | } |
| 199 | |
| 200 | $order_item_model = new OsOrderItemModel(); |
| 201 | $order_item_model->variant = defined( 'LATEPOINT_ITEM_VARIANT_BUNDLE' ) ? LATEPOINT_ITEM_VARIANT_BUNDLE : 'bundle'; |
| 202 | $order_item_model->order_id = $order->id; |
| 203 | |
| 204 | if ( ! $order_item_model->save() ) { |
| 205 | $errors = $order_item_model->get_error_messages(); |
| 206 | $error_msg = isset( $errors[0] ) ? $errors[0] : 'Order Item could not be created.'; |
| 207 | return [ |
| 208 | 'status' => 'error', |
| 209 | 'message' => $error_msg, |
| 210 | ]; |
| 211 | } |
| 212 | |
| 213 | $bundle_data = [ |
| 214 | 'bundle_id' => $bundle->id, |
| 215 | ]; |
| 216 | |
| 217 | $order_item_model->item_data = wp_json_encode( $bundle_data ); |
| 218 | $order_item_model->recalculate_prices(); |
| 219 | |
| 220 | $order->total = $order_item_model->total; |
| 221 | $order->subtotal = $order_item_model->subtotal; |
| 222 | $order->save(); |
| 223 | $order_item_model->save(); |
| 224 | |
| 225 | do_action( 'latepoint_order_created', $order ); |
| 226 | |
| 227 | return [ |
| 228 | 'bundle' => $bundle->get_data_vars(), |
| 229 | 'order' => $order->get_data_vars(), |
| 230 | ]; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | CreateBundleOrder::get_instance(); |
| 235 |