PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.4.4
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.4.4
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / zoho-inventory / class-order-sync.php
commercebird / includes / classes / zoho-inventory Last commit date
class-categories.php 1 year ago class-import-image.php 1 year ago class-import-items.php 1 year ago class-import-price-list.php 1 year ago class-multi-currency.php 1 year ago class-order-sync.php 1 year ago class-product.php 1 year ago class-users-contact.php 1 year ago index.php 1 year ago
class-order-sync.php
642 lines
1 <?php
2
3 /**
4 * Class for handling Zoho Inventory order sync related functions.
5 */
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 class CMBIRD_Order_Sync_ZI {
11
12
13 private $zoho_inventory_oid;
14 private $zoho_inventory_url;
15 private $location_id;
16 private $parent_location_id;
17 private $enable_order_status;
18 private $enable_auto_number;
19 private $order_prefix;
20 private $custom_fields;
21 private $include_tax;
22
23 /**
24 * Initialize the class.
25 */
26 public function __construct() {
27 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
28 if ( ! empty( $zoho_inventory_access_token ) ) {
29 add_action( 'woocommerce_update_order', array( $this, 'salesorder_void' ) );
30 // get options
31 $this->zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
32 $this->zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
33 $this->location_id = get_option( 'cmbird_zoho_location_id_status' );
34 $this->parent_location_id = get_option( 'cmbird_zoho_parent_location_id_status' );
35 $this->enable_order_status = get_option( 'cmbird_zoho_enable_order_status_status' );
36 $this->enable_auto_number = get_option( 'cmbird_zoho_enable_auto_number_status' );
37 $this->order_prefix = get_option( 'cmbird_zoho_order_prefix_status' );
38 $this->custom_fields = json_decode( get_option( 'cmbird_wootozoho_custom_fields' ), true );
39 $this->include_tax = get_option( 'woocommerce_prices_include_tax' ) === 'yes';
40 } else {
41 return;
42 }
43 }
44
45 /**
46 * Function to map customer on checkout before placing order
47 * @param int $order_id Order ID.
48 *
49 */
50 public function cmbird_zi_sync_customer_checkout( $order_id ) {
51 // $fd = fopen( __DIR__ . '/cmbird_zi_sync_customer_checkout.txt', 'w+' );
52
53 $order = wc_get_order( $order_id );
54 $userid = $order->get_user_id();
55 $user_company = $order->get_billing_company();
56 $user_email = $order->get_billing_email();
57 $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true );
58
59 // Get currency code of the order
60 $currency_id = intval( get_user_meta( $userid, 'zi_currency_id', true ) );
61 if ( empty( $currency_id ) ) {
62 $currency_code = $order->get_currency();
63 $multi_currency_handle = new CMBIRD_Multicurrency_Zoho();
64 $currency_id = $multi_currency_handle->zoho_currency_data( $currency_code, $userid );
65 }
66
67 if ( $zi_customer_id ) {
68 $zoho_inventory_oid = $this->zoho_inventory_oid;
69 $zoho_inventory_url = $this->zoho_inventory_url;
70 $get_url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id . '/?organization_id=' . $zoho_inventory_oid;
71
72 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
73 $json = $execute_curl_call_handle->execute_curl_call_get( $get_url );
74
75 // fwrite($fd,PHP_EOL.'customer_json: '.print_r($json, true));
76
77 $code = $json->code;
78 if ( 0 !== $code || '0' !== $code ) {
79 delete_user_meta( $userid, 'zi_contact_id' );
80 delete_user_meta( $userid, 'zi_billing_address_id' );
81 delete_user_meta( $userid, 'zi_primary_contact_id' );
82 delete_user_meta( $userid, 'zi_shipping_address_id' );
83 delete_user_meta( $userid, 'zi_created_time' );
84 delete_user_meta( $userid, 'zi_last_modified_time' );
85 $zi_customer_id = '';
86 }
87 }
88
89 /**
90 * syncing customer if its not in Zoho yet
91 */
92 if ( ! $zi_customer_id ) {
93
94 // First check based on customer email address
95 $zoho_inventory_oid = $this->zoho_inventory_oid;
96 $zoho_inventory_url = $this->zoho_inventory_url;
97 // fwrite($fd,PHP_EOL.'$user_mail : '.$user_email);
98 $url = $zoho_inventory_url . 'inventory/v1/contacts?organization_id=' . $zoho_inventory_oid . '&email=' . $user_email;
99
100 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
101 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
102
103 $code = $json->code;
104 $message = $json->message;
105 if ( 0 === $code || '0' === $code ) {
106 // fwrite($fd, PHP_EOL . 'customer_json: ' . print_r($json, true));
107 if ( empty( $json->contacts ) ) {
108 // Second check based on Company Name
109 if ( $user_company ) {
110 $company_name = str_replace( ' ', '%20', $user_company );
111 $url = $zoho_inventory_url . 'inventory/v1/contacts?organization_id=' . $zoho_inventory_oid . '&filter_by=Status.Active&search_text=' . $company_name;
112
113 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
114 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
115
116 $code = $json->code;
117 if ( 0 === $code || '0' === $code ) {
118 if ( empty( $json->contacts ) ) {
119 $contact_class_handle = new CMBIRD_Contact_ZI();
120 $zi_customer_id = $contact_class_handle->cmbird_contact_create_function( $userid );
121 } else {
122 foreach ( $json->contacts[0] as $key => $value ) {
123 if ( 'contact_id' === $key ) {
124 $zi_customer_id = $value;
125 update_user_meta( $userid, 'zi_contact_id', $zi_customer_id );
126 }
127 }
128 $contact_class_handle = new CMBIRD_Contact_ZI();
129 $zi_customer_id = $contact_class_handle->cmbird_create_contact_person( $userid );
130 }
131 }
132 } else {
133 $contact_class_handle = new CMBIRD_Contact_ZI();
134 $zi_customer_id = $contact_class_handle->cmbird_contact_create_function( $userid );
135 }
136 } else {
137 // fwrite($fd,PHP_EOL.'Contacts : '.print_r($json->contacts,true));
138 foreach ( $json->contacts[0] as $key => $value ) {
139 if ( 'contact_id' === $key ) {
140 $zi_customer_id = $value;
141 update_user_meta( $userid, 'zi_contact_id', $zi_customer_id );
142 }
143 }
144 }
145 }
146 // Http request not processed properly.
147 // echo $message;
148 return $zi_customer_id;
149 } else {
150 $zoho_inventory_oid = $this->zoho_inventory_oid;
151 $zoho_inventory_url = $this->zoho_inventory_url;
152 $get_url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id . '/contactpersons/?organization_id=' . $zoho_inventory_oid;
153
154 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
155 $contactpersons_response = $execute_curl_call_handle->execute_curl_call_get( $get_url );
156
157 // fwrite( $fd, PHP_EOL . 'Contactpersons: ' . print_r($contactpersons_response, true) );
158
159 // first check within contactpersons endpoint and then map it with that contactperson if email-id matches
160 if ( 0 === $contactpersons_response->code || '0' === $contactpersons_response->code ) {
161 if ( ! empty( $contactpersons_response->contact_persons ) ) {
162 foreach ( $contactpersons_response->contact_persons as $key => $contact_persons ) {
163 $person_email = trim( $contact_persons->email );
164 if ( trim( $user_email ) === $person_email ) {
165 /* Match Contact */
166 $contactid = $contact_persons->contact_person_id;
167 update_user_meta( $userid, 'zi_contactperson_id_' . $key, $contactid );
168 if ( true === $contact_persons->is_primary_contact || 1 === $contact_persons->is_primary_contact ) {
169 $contact_class_handle = new CMBIRD_Contact_ZI();
170 $contact_class_handle->cmbird_contact_update_function( $userid, $order_id );
171 } else {
172 $contact_class_handle = new CMBIRD_Contact_ZI();
173 $contact_class_handle->cmbird_update_contact_person( $userid, $order_id );
174 }
175 }
176 }
177 } else {
178 $get_url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id . '/?organization_id=' . $zoho_inventory_oid;
179 $contact_res = $execute_curl_call_handle->execute_curl_call_get( $get_url );
180 if ( ( 0 === $contact_res->code || '0' === $contact_res->code ) && ! empty( $contact_res->contact ) ) {
181 foreach ( $contact_res as $contact_ ) {
182 if ( trim( $contact_->email ) == trim( $user_email ) ) {
183 // fwrite( $fd, PHP_EOL . 'Inside cmbird_contact_update_function' );
184 $contact_class_handle = new CMBIRD_Contact_ZI();
185 $contact_class_handle->cmbird_contact_update_function( $userid, $order_id );
186 } else {
187 // fwrite( $fd, PHP_EOL . 'Inside cmbird_create_contact_person' );
188 $contact_class_handle = new CMBIRD_Contact_ZI();
189 $contact_class_handle->cmbird_create_contact_person( $userid );
190 }
191 }
192 }
193 }
194 }
195 // fwrite( $fd, PHP_EOL . 'No contactpersons ' );
196 }
197 // fclose( $fd );
198 }
199
200 /**
201 * Function for admin zoho sync call.
202 *
203 * @param int $order_id Order ID.
204 */
205 public function zi_order_sync( $order_id ) {
206 $order = wc_get_order( $order_id );
207 if ( ! $order ) {
208 return;
209 }
210 // return if there is no zoho inventory access token
211 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
212 if ( empty( $zoho_inventory_access_token ) ) {
213 return;
214 }
215
216 $current_time = time();
217 $last_time = $order->get_meta( 'zi_last_order_sync_time', true );
218 if ( ! empty( $last_time ) && $current_time - $last_time < 60 ) {
219 return;
220 }
221
222 // Create an action hook to prevent orders getting synced if it contains an array of statuses
223 $ignored_statuses = apply_filters( 'cmbird_zi_order_sync_ignored_statuses', array( 'cancelled', 'failed', 'draft', 'trash' ) );
224 if ( in_array( $order->get_status(), $ignored_statuses ) ) {
225 return;
226 }
227
228 $zi_sales_order_id = $order->get_meta( 'zi_salesorder_id' );
229 $userid = $order->get_user_id();
230 $user_email = get_user_meta( $userid, 'billing_email', true );
231
232 if ( empty( $userid ) && empty( $user_email ) ) {
233 $order->add_order_note( 'Zoho Order Sync: guest orders are not supported' );
234 $order->save();
235 return;
236 }
237
238 $customer_id = get_user_meta( $userid, 'zi_contact_id', true );
239 $billing_id = get_user_meta( $userid, 'zi_billing_address_id', true );
240 $shipping_id = get_user_meta( $userid, 'zi_shipping_address_id', true );
241
242 if ( empty( $customer_id ) ) {
243 $customer_id = $this->cmbird_zi_sync_customer_checkout( $order_id );
244 } else {
245 $contact_class = new CMBIRD_Contact_ZI();
246 $contact_class->cmbird_contact_update_function( $userid, $order_id );
247 }
248
249 $line_items = $this->build_line_items( $order );
250 if ( empty( $line_items ) ) {
251 return;
252 }
253
254 $pdt = array(
255 'customer_id' => $customer_id,
256 'date' => $order->get_date_created()->format( 'Y-m-d' ),
257 'line_items' => $line_items,
258 'is_discount_before_tax' => true,
259 'discount_type' => 'item_level',
260 'price_precision' => '2',
261 'notes' => preg_replace( '/[^A-Za-z0-9\-]/', ' ', $order->get_customer_note() ),
262 'billing_address_id' => $billing_id,
263 'shipping_address_id' => $shipping_id,
264 'delivery_method' => $order->get_shipping_method(),
265 'is_inclusive_tax' => $this->include_tax,
266 'shipping_charge' => $order->get_shipping_total(),
267 'order_status' => $this->enable_order_status ? 'draft' : 'confirmed',
268 );
269
270 if ( $this->parent_location_id && $this->location_id ) {
271 $pdt['location_id'] = $this->parent_location_id;
272 }
273
274 $shipping_tax_id = $this->get_shipping_tax_id( $order );
275 if ( $shipping_tax_id ) {
276 $pdt['shipping_charge_tax_id'] = $shipping_tax_id;
277 }
278
279 $fees = $this->get_order_fees( $order );
280 $pdt = array_merge( $pdt, $fees );
281
282 $pdt['custom_fields'] = $this->prepare_custom_fields( $order );
283 $reference = $this->prepare_reference_number( $order );
284
285 if ( $this->enable_auto_number ) {
286 $pdt['reference_number'] = $reference;
287 } else {
288 $pdt['salesorder_number'] = $order->get_id();
289 }
290
291 $userid = $order->get_user_id();
292 $gst_no = get_user_meta( $userid, 'gst_no', true );
293 $gst_treatment = get_user_meta( $userid, 'gst_treatment', true );
294 if ( $gst_no ) {
295 $pdt['gst_no'] = $gst_no;
296 $pdt['gst_treatment'] = $gst_treatment;
297 }
298
299 $response_msg = $zi_sales_order_id ? $this->single_saleorder_zoho_inventory_update( $order_id, $zi_sales_order_id, wp_json_encode( $pdt ) ) : $this->single_saleorder_zoho_inventory( wp_json_encode( $pdt ), $order_id );
300
301 $order->update_meta_data( 'zi_body_request', wp_json_encode( $pdt ) );
302 $order->update_meta_data( 'zi_salesorder_id', $response_msg['zi_salesorder_id'] );
303 $order->update_meta_data( 'zi_last_order_sync_time', $current_time );
304 $order->add_order_note( 'Zoho Order Sync: ' . $response_msg['message'] );
305 $order->save();
306 }
307
308 private function build_line_items( $order ) {
309 $items = array();
310 foreach ( $order->get_items() as $item_id => $item ) {
311 $product = $item->get_product();
312 if ( ! $product ) {
313 continue;
314 }
315 $name = $item->get_name();
316 $meta = $item->get_formatted_meta_data();
317 $desc = '';
318 if ( ! empty( $meta ) ) {
319 foreach ( $meta as $meta_value ) {
320 $desc .= $meta_value->display_key . ': ' . wp_strip_all_tags( $meta_value->display_value ) . ' ';
321 }
322 $desc = sanitize_text_field( trim( $desc ) );
323 }
324
325 $quantity = $item->get_quantity();
326 $subtotal = $item->get_subtotal();
327 $total = $item->get_total();
328 $rate = $subtotal / max( 1, $quantity );
329 $discount = $subtotal > $total ? $subtotal - $total : 0;
330
331 $tax_data = $item->get_taxes();
332 $tax_id = '';
333 $tax_percent = 0;
334 if ( ! empty( $tax_data['subtotal'] ) ) {
335 $tax_rate_id = key( $tax_data['subtotal'] );
336 $tax_percent = WC_Tax::_get_tax_rate( $tax_rate_id )['tax_rate'];
337 $tax_id = $this->zi_get_tax_id( $tax_percent );
338 if ( $this->include_tax ) {
339 $rate += $rate * ( $tax_percent / 100 );
340 $discount += $discount * ( $tax_percent / 100 );
341 }
342 }
343
344 $line = array(
345 'item_id' => get_post_meta( $product->get_id(), 'zi_item_id', true ),
346 'name' => $name,
347 'description' => $desc,
348 'quantity' => $quantity,
349 'rate' => round( $rate, 2 ),
350 );
351
352 if ( $discount > 0 ) {
353 $line['discount'] = round( $discount, 2 );
354 }
355 if ( $tax_id ) {
356 $line['tax_percentage'] = $tax_percent;
357 $line['tax_id'] = $tax_id;
358 }
359 // add location id if set
360 if ( ! empty( $this->location_id ) ) {
361 $line['location_id'] = $this->location_id;
362 }
363 $items[] = $line;
364 }
365 return $items;
366 }
367
368 private function get_shipping_tax_id( $order ) {
369 $shipping_total = $order->get_shipping_total();
370 $shipping_tax = $order->get_shipping_tax();
371 if ( $shipping_total > 0 && $shipping_tax > 0 ) {
372 $percent = ( $shipping_tax / $shipping_total ) * 100;
373 return $this->zi_get_tax_id( round( $percent, 2 ) );
374 }
375 return '';
376 }
377
378 private function get_order_fees( $order ) {
379 $fees = $order->get_fees();
380 $data = array();
381 foreach ( $fees as $fee ) {
382 $amount = $fee->get_total();
383 if ( $amount > 0 ) {
384 $data['adjustment'] = $amount;
385 $data['adjustment_description'] = $fee->get_name();
386 }
387 }
388 return $data;
389 }
390
391 private function prepare_custom_fields( $order ) {
392 $fields = array();
393 if ( is_array( $this->custom_fields ) ) {
394 foreach ( $this->custom_fields as $meta_key => $label ) {
395 $fields[] = array(
396 'label' => $label,
397 'value' => $order->get_meta( $meta_key ),
398 );
399 }
400 }
401 return $fields;
402 }
403
404 private function prepare_reference_number( $order ) {
405 $transaction_id = $order->get_transaction_id();
406 if ( empty( $transaction_id ) ) {
407 $transaction_id = $order->get_meta( '_order_number', true );
408 }
409 if ( class_exists( 'WCJ_Order_Numbers' ) || class_exists( 'WC_Seq_Order_Number_Pro' ) ) {
410 return $this->order_prefix . $transaction_id;
411 }
412 if ( ! empty( $this->order_prefix ) ) {
413 return $this->order_prefix . '-' . $order->get_id();
414 }
415 return $order->get_id();
416 }
417
418 /**
419 * Void the sales order if cancelled in WooCommerce.
420 *
421 * @param int $order_id Order ID.
422 */
423 public function salesorder_void( $order_id ) {
424 if ( ! $order_id ) {
425 return;
426 }
427 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
428 if ( empty( $zoho_inventory_access_token ) ) {
429 return;
430 }
431
432 $order = wc_get_order( $order_id );
433 // return if order is already voided
434 if ( $order->get_meta( 'zi_salesorder_void', true ) ) {
435 return;
436 }
437 $order_status = $order->get_status();
438
439 if ( 'cancelled' === $order_status || 'wc-merged' === $order_status ) {
440 $zi_sales_order_id = $order->get_meta( 'zi_salesorder_id', true );
441 $zoho_inventory_oid = $this->zoho_inventory_oid;
442 $zoho_inventory_url = $this->zoho_inventory_url;
443
444 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id . '/status/void?organization_id=' .
445 $zoho_inventory_oid;
446 $data = '';
447 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
448 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
449
450 $errmsg = $json->message;
451 $code = $json->code;
452 if ( '0' === $code || 0 === $code ) {
453 // Add order meta key "zi_salesorder_void" to true
454 $order->update_meta_data( 'zi_salesorder_void', true );
455 $order->add_order_note( 'Zoho Order Void: ' . $errmsg );
456 $order->save();
457 return;
458 }
459 } else {
460 return;
461 }
462 }
463
464 /**
465 * Sync order from Woo to Zoho.
466 *
467 * @param string $pdt1 JSON string
468 * @param int $order_id Order ID.
469 * @return string Error message
470 */
471 public function single_saleorder_zoho_inventory( $pdt1, $order_id ) {
472 //start logging
473 // $fd = fopen( __DIR__ . '/order-sync-backend.txt', 'w+' );
474
475 $zoho_inventory_oid = $this->zoho_inventory_oid;
476 $zoho_inventory_url = $this->zoho_inventory_url;
477 $data = array(
478 'JSONString' => $pdt1,
479 'organization_id' => $zoho_inventory_oid,
480 );
481
482 //logging
483 // fwrite($fd, PHP_EOL . 'Data log : ' . print_r($data, true));
484
485 if ( empty( $data['JSONString'] ) ) {
486 return '';
487 }
488 $ignore_auto_no = $this->enable_auto_number ? 'false' : 'true';
489 $url = $zoho_inventory_url . 'inventory/v1/salesorders?ignore_auto_number_generation=' . $ignore_auto_no;
490
491 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
492 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
493
494 // fwrite( $fd, PHP_EOL . 'Data log : ' . print_r( $json, true ) );
495 $response = array();
496 $code = $json->code;
497 $errmsg = $json->message;
498 // fwrite($fd, PHP_EOL . 'Code : ' . $code);
499
500 if ( '0' === $code || 0 === $code ) {
501 foreach ( $json->salesorder as $key => $value ) {
502
503 if ( 'salesorder_id' === $key ) {
504 $response['zi_salesorder_id'] = $value;
505 // $order->add_meta_data('zi_salesorder_id', $value, true);
506 }
507 }
508 } else {
509 // send email to admin with the error message
510 // create message that contains the error message and order id
511 $message = 'Error in Zoho Inventory Order Sync: ' . $errmsg;
512 // append the link to the order edit page
513 $message .= '<br><br><a href="' . admin_url( 'admin.php?page=wc-orders&action=edit&id=' . $order_id ) . '">Click here to view the order in WP Admin</a>';
514 $message .= '<br><br>Order ID: ' . $order_id;
515 $message .= '<br><br>Request Body: ' . $pdt1;
516 $class_common = new CMBIRD_Common_Functions();
517 $class_common->send_email( 'Error in Zoho Order Sync', $message );
518 }
519 $response['message'] = $errmsg;
520 // fclose( $fd );
521
522 return $response;
523 }
524
525 /**
526 * Function for updating single sales order.
527 *
528 * @param int $order_id Order ID.
529 * @param string $zi_sales_order_id
530 * @param string $pdt1 JSON string
531 * @return string Error message
532 */
533 public function single_saleorder_zoho_inventory_update( $order_id, $zi_sales_order_id, $pdt1 ) {
534 // $fd = fopen( __DIR__. '/single_saleorder_update.txt', 'w+' );
535
536 $response = array();
537 $zoho_inventory_oid = $this->zoho_inventory_oid;
538 $zoho_inventory_url = $this->zoho_inventory_url;
539
540 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id;
541 $data = array(
542 'JSONString' => $pdt1,
543 'organization_id' => $zoho_inventory_oid,
544 );
545
546 $order = wc_get_order( $order_id );
547 // fwrite($fd, PHP_EOL. print_r($data, true)); //logging response
548 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
549 $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data );
550
551 // $code = $json->code;
552 $errmsg = $json->message;
553 $response['message'] = $errmsg;
554 $response['zi_salesorder_id'] = $zi_sales_order_id;
555
556 // echo '<pre>'; print_r($errmsg);
557
558 $package_id = $order->get_meta( 'zi_package_id', true );
559
560 if ( ! empty( $package_id ) ) {
561 // fwrite($fd, PHP_EOL. 'inside package exists'); //logging response
562 foreach ( $json->salesorder as $key => $value ) {
563 if ( 'date' === $key ) {
564 $ship_date = $value;
565 }
566
567 if ( 'line_items' === $key ) {
568 foreach ( $value as $kk => $vv ) {
569 $line_items[] = '{"so_line_item_id": "' . $vv->line_item_id . '","quantity": "' . $vv->quantity . '"}';
570 }
571 $impot = implode( ',', $line_items );
572
573 $json_package = '"date": "' . $ship_date . '","line_items": [' . $impot . ']';
574
575 $url_package = $zoho_inventory_url . 'inventory/v1/packages/' . $package_id;
576 $data3 = array(
577 'JSONString' => '{' . $json_package . '}',
578 'organization_id' => $zoho_inventory_oid,
579 );
580
581 $res_package = $execute_curl_call_handle->execute_curl_call_put( $url_package, $data3 );
582 }
583 }
584 }
585 // fclose( $fd ); //end of logging
586 return $response;
587 }
588
589 /**
590 * Function to get all Zoho Taxes.
591 *
592 * @param int $percentage Tax percentage.
593 * @return string Tax ID.
594 */
595 private function zi_get_tax_id( $percentage ) {
596 // $fd = fopen( __DIR__ . '/zi_get_tax_id.txt', 'a+' );
597 // get all options that contain zoho_inventory_tax_rate_ in the name using global $wpdb.
598 global $wpdb;
599 $zoho_tax_rates = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'cmbird_zoho_inventory_tax_rate_%'" );
600 $input_tax_percentage = floor( $percentage * 10 ) / 10;
601 // fwrite( $fd, PHP_EOL . 'Input Tax Percentage: ' . $input_tax_percentage );
602 $tax_id = '';
603 // for each zoho_tax_rate, check if the tax percentage matches the input percentage. The percentage at the end of the value e.g. 69497000002395146##BTW@@Hoog Exclusief##tax##21
604 foreach ( $zoho_tax_rates as $zoho_tax_rate ) {
605 $tax_rate = explode( '##', $zoho_tax_rate->option_value );
606 $tax_percentage = $tax_rate[3];
607 // Round the stored tax percentage to one decimal place for comparison
608 $stored_tax_percentage = round( $tax_percentage, 1 );
609 // Compare the rounded tax percentages with a tolerance for floating-point precision errors
610 if ( abs( $stored_tax_percentage - $input_tax_percentage ) < 0.01 ) {
611 $tax_id = $tax_rate[0];
612 break;
613 }
614 }
615 return $tax_id;
616 }
617
618 /**
619 * TODO: Get Order Transaction Fees
620 *
621 * @param int $order_id Order ID.
622 * @return float Transaction fees
623 */
624 protected function get_transaction_fees( $order_id ) {
625 $order = wc_get_order( $order_id );
626 switch ( true ) {
627 // get fees from Stripe, if exists
628 case $fees = $order->get_meta( '_stripe_fee' ):
629 break;
630 // get fees from Paypal, if exists
631 case ( $fees = $order->get_meta( '_paypal_transaction_fee' ) ) !== null:
632 break;
633 // otherwise fee is 0
634 default:
635 $fees = 0;
636 break;
637 }
638 return $fees;
639 }
640 }
641 $CMBIRD_Order_Sync_ZI = new CMBIRD_Order_Sync_ZI();
642