PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.8
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.8
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
635 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
211 $current_time = time();
212 $last_time = $order->get_meta( 'zi_last_order_sync_time', true );
213 if ( ! empty( $last_time ) && $current_time - $last_time < 60 ) {
214 return;
215 }
216
217 $zi_sales_order_id = $order->get_meta( 'zi_salesorder_id' );
218 $userid = $order->get_user_id();
219 $user_email = get_user_meta( $userid, 'billing_email', true );
220
221 if ( empty( $userid ) && empty( $user_email ) ) {
222 $order->add_order_note( 'Zoho Order Sync: guest orders are not supported' );
223 $order->save();
224 return;
225 }
226
227 $customer_id = get_user_meta( $userid, 'zi_contact_id', true );
228 $billing_id = get_user_meta( $userid, 'zi_billing_address_id', true );
229 $shipping_id = get_user_meta( $userid, 'zi_shipping_address_id', true );
230
231 if ( empty( $customer_id ) ) {
232 $customer_id = $this->cmbird_zi_sync_customer_checkout( $order_id );
233 } else {
234 $contact_class = new CMBIRD_Contact_ZI();
235 $contact_class->cmbird_contact_update_function( $userid, $order_id );
236 }
237
238 $line_items = $this->build_line_items( $order );
239 if ( empty( $line_items ) ) {
240 return;
241 }
242
243 $pdt = array(
244 'customer_id' => $customer_id,
245 'date' => $order->get_date_created()->format( 'Y-m-d' ),
246 'line_items' => $line_items,
247 'is_discount_before_tax' => true,
248 'discount_type' => 'item_level',
249 'price_precision' => '2',
250 'notes' => preg_replace( '/[^A-Za-z0-9\-]/', ' ', $order->get_customer_note() ),
251 'billing_address_id' => $billing_id,
252 'shipping_address_id' => $shipping_id,
253 'delivery_method' => $order->get_shipping_method(),
254 'is_inclusive_tax' => $this->include_tax,
255 'shipping_charge' => $this->get_shipping_total( $order ),
256 'order_status' => $this->enable_order_status ? 'draft' : 'confirmed',
257 );
258
259 if ( $this->parent_location_id ) {
260 $pdt['location_id'] = $this->parent_location_id;
261 }
262
263 $shipping_tax_id = $this->get_shipping_tax_id( $order );
264 if ( $shipping_tax_id ) {
265 $pdt['shipping_charge_tax_id'] = $shipping_tax_id;
266 }
267
268 $fees = $this->get_order_fees( $order );
269 $pdt = array_merge( $pdt, $fees );
270
271 $pdt['custom_fields'] = $this->prepare_custom_fields( $order );
272 $reference = $this->prepare_reference_number( $order );
273
274 if ( $this->enable_auto_number ) {
275 $pdt['reference_number'] = $reference;
276 } else {
277 $pdt['salesorder_number'] = $order->get_id();
278 }
279
280 $userid = $order->get_user_id();
281 $gst_no = get_user_meta( $userid, 'gst_no', true );
282 $gst_treatment = get_user_meta( $userid, 'gst_treatment', true );
283 if ( $gst_no ) {
284 $pdt['gst_no'] = $gst_no;
285 $pdt['gst_treatment'] = $gst_treatment;
286 }
287
288 $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 );
289
290 $order->update_meta_data( 'zi_body_request', wp_json_encode( $pdt ) );
291 $order->update_meta_data( 'zi_salesorder_id', $response_msg['zi_salesorder_id'] );
292 $order->update_meta_data( 'zi_last_order_sync_time', $current_time );
293 $order->add_order_note( 'Zoho Order Sync: ' . $response_msg['message'] );
294 $order->save();
295 }
296
297 private function build_line_items( $order ) {
298 $items = array();
299 foreach ( $order->get_items() as $item_id => $item ) {
300 $product = $item->get_product();
301 if ( ! $product ) {
302 continue;
303 }
304 $name = $item->get_name();
305 $meta = $item->get_formatted_meta_data();
306 $desc = '';
307 if ( ! empty( $meta ) ) {
308 foreach ( $meta as $meta_value ) {
309 $desc .= $meta_value->display_key . ': ' . wp_strip_all_tags( $meta_value->display_value ) . ' ';
310 }
311 $desc = sanitize_text_field( trim( $desc ) );
312 }
313
314 $quantity = $item->get_quantity();
315 $subtotal = $item->get_subtotal();
316 $total = $item->get_total();
317 $rate = $subtotal / max( 1, $quantity );
318 $discount = $subtotal > $total ? $subtotal - $total : 0;
319
320 $tax_data = $item->get_taxes();
321 $tax_id = '';
322 $tax_percent = 0;
323 if ( ! empty( $tax_data['subtotal'] ) ) {
324 $tax_rate_id = key( $tax_data['subtotal'] );
325 $tax_percent = WC_Tax::_get_tax_rate( $tax_rate_id )['tax_rate'];
326 $tax_id = $this->zi_get_tax_id( $tax_percent );
327 if ( $this->include_tax ) {
328 $rate += $rate * ( $tax_percent / 100 );
329 $discount += $discount * ( $tax_percent / 100 );
330 }
331 }
332
333 $line = array(
334 'item_id' => get_post_meta( $product->get_id(), 'zi_item_id', true ),
335 'name' => $name,
336 'description' => $desc,
337 'quantity' => $quantity,
338 'rate' => round( $rate, 2 ),
339 );
340
341 if ( $discount > 0 ) {
342 $line['discount'] = round( $discount, 2 );
343 }
344 if ( $tax_id ) {
345 $line['tax_percentage'] = $tax_percent;
346 $line['tax_id'] = $tax_id;
347 }
348 // add location id if set
349 if ( $this->location_id ) {
350 $line['location_id'] = $this->location_id;
351 }
352 $items[] = $line;
353 }
354 return $items;
355 }
356
357 private function get_shipping_total( $order ) {
358 return round( $order->get_shipping_total() + $order->get_shipping_tax(), 2 );
359 }
360
361 private function get_shipping_tax_id( $order ) {
362 $shipping_total = $order->get_shipping_total();
363 $shipping_tax = $order->get_shipping_tax();
364 if ( $shipping_total > 0 && $shipping_tax > 0 ) {
365 $percent = ( $shipping_tax / $shipping_total ) * 100;
366 return $this->zi_get_tax_id( round( $percent, 2 ) );
367 }
368 return '';
369 }
370
371 private function get_order_fees( $order ) {
372 $fees = $order->get_fees();
373 $data = array();
374 foreach ( $fees as $fee ) {
375 $amount = $fee->get_total();
376 if ( $amount > 0 ) {
377 $data['adjustment'] = $amount;
378 $data['adjustment_description'] = $fee->get_name();
379 }
380 }
381 return $data;
382 }
383
384 private function prepare_custom_fields( $order ) {
385 $fields = array();
386 if ( is_array( $this->custom_fields ) ) {
387 foreach ( $this->custom_fields as $meta_key => $label ) {
388 $fields[] = array(
389 'label' => $label,
390 'value' => $order->get_meta( $meta_key ),
391 );
392 }
393 }
394 return $fields;
395 }
396
397 private function prepare_reference_number( $order ) {
398 $transaction_id = $order->get_transaction_id();
399 if ( empty( $transaction_id ) ) {
400 $transaction_id = $order->get_meta( '_order_number', true );
401 }
402 if ( class_exists( 'WCJ_Order_Numbers' ) || class_exists( 'WC_Seq_Order_Number_Pro' ) ) {
403 return $this->order_prefix . $transaction_id;
404 }
405 if ( ! empty( $this->order_prefix ) ) {
406 return $this->order_prefix . '-' . $order->get_id();
407 }
408 return $order->get_id();
409 }
410
411 /**
412 * Void the sales order if cancelled in WooCommerce.
413 *
414 * @param int $order_id Order ID.
415 */
416 public function salesorder_void( $order_id ) {
417 if ( ! $order_id ) {
418 return;
419 }
420 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
421 if ( empty( $zoho_inventory_access_token ) ) {
422 return;
423 }
424
425 $order = wc_get_order( $order_id );
426 // return if order is already voided
427 if ( $order->get_meta( 'zi_salesorder_void', true ) ) {
428 return;
429 }
430 $order_status = $order->get_status();
431
432 if ( 'cancelled' === $order_status || 'wc-merged' === $order_status ) {
433 $zi_sales_order_id = $order->get_meta( 'zi_salesorder_id', true );
434 $zoho_inventory_oid = $this->zoho_inventory_oid;
435 $zoho_inventory_url = $this->zoho_inventory_url;
436
437 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id . '/status/void?organization_id=' .
438 $zoho_inventory_oid;
439 $data = '';
440 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
441 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
442
443 $errmsg = $json->message;
444 $code = $json->code;
445 if ( '0' === $code || 0 === $code ) {
446 // Add order meta key "zi_salesorder_void" to true
447 $order->update_meta_data( 'zi_salesorder_void', true );
448 $order->add_order_note( 'Zoho Order Void: ' . $errmsg );
449 $order->save();
450 return;
451 }
452 } else {
453 return;
454 }
455 }
456
457 /**
458 * Sync order from Woo to Zoho.
459 *
460 * @param string $pdt1 JSON string
461 * @param int $order_id Order ID.
462 * @return string Error message
463 */
464 public function single_saleorder_zoho_inventory( $pdt1, $order_id ) {
465 //start logging
466 // $fd = fopen( __DIR__ . '/order-sync-backend.txt', 'w+' );
467
468 $zoho_inventory_oid = $this->zoho_inventory_oid;
469 $zoho_inventory_url = $this->zoho_inventory_url;
470 $data = array(
471 'JSONString' => $pdt1,
472 'organization_id' => $zoho_inventory_oid,
473 );
474
475 //logging
476 // fwrite($fd, PHP_EOL . 'Data log : ' . print_r($data, true));
477
478 if ( empty( $data['JSONString'] ) ) {
479 return '';
480 }
481 $ignore_auto_no = $this->enable_auto_number ? 'false' : 'true';
482 $url = $zoho_inventory_url . 'inventory/v1/salesorders?ignore_auto_number_generation=' . $ignore_auto_no;
483
484 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
485 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
486
487 // fwrite( $fd, PHP_EOL . 'Data log : ' . print_r( $json, true ) );
488 $response = array();
489 $code = $json->code;
490 $errmsg = $json->message;
491 // fwrite($fd, PHP_EOL . 'Code : ' . $code);
492
493 if ( '0' === $code || 0 === $code ) {
494 foreach ( $json->salesorder as $key => $value ) {
495
496 if ( 'salesorder_id' === $key ) {
497 $response['zi_salesorder_id'] = $value;
498 // $order->add_meta_data('zi_salesorder_id', $value, true);
499 }
500 }
501 } else {
502 // send email to admin with the error message
503 // create message that contains the error message and order id
504 $message = 'Error in Zoho Inventory Order Sync: ' . $errmsg;
505 // append the link to the order edit page
506 $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>';
507 $message .= '<br><br>Order ID: ' . $order_id;
508 $message .= '<br><br>Request Body: ' . $pdt1;
509 $class_common = new CMBIRD_Common_Functions();
510 $class_common->send_email( 'Error in Zoho Order Sync', $message );
511 }
512 $response['message'] = $errmsg;
513 // fclose( $fd );
514
515 return $response;
516 }
517
518 /**
519 * Function for updating single sales order.
520 *
521 * @param int $order_id Order ID.
522 * @param string $zi_sales_order_id
523 * @param string $pdt1 JSON string
524 * @return string Error message
525 */
526 public function single_saleorder_zoho_inventory_update( $order_id, $zi_sales_order_id, $pdt1 ) {
527 // $fd = fopen( __DIR__. '/single_saleorder_update.txt', 'w+' );
528
529 $response = array();
530 $zoho_inventory_oid = $this->zoho_inventory_oid;
531 $zoho_inventory_url = $this->zoho_inventory_url;
532
533 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id;
534 $data = array(
535 'JSONString' => $pdt1,
536 'organization_id' => $zoho_inventory_oid,
537 );
538
539 $order = wc_get_order( $order_id );
540 // fwrite($fd, PHP_EOL. print_r($data, true)); //logging response
541 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
542 $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data );
543
544 // $code = $json->code;
545 $errmsg = $json->message;
546 $response['message'] = $errmsg;
547 $response['zi_salesorder_id'] = $zi_sales_order_id;
548
549 // echo '<pre>'; print_r($errmsg);
550
551 $package_id = $order->get_meta( 'zi_package_id', true );
552
553 if ( ! empty( $package_id ) ) {
554 // fwrite($fd, PHP_EOL. 'inside package exists'); //logging response
555 foreach ( $json->salesorder as $key => $value ) {
556 if ( 'date' === $key ) {
557 $ship_date = $value;
558 }
559
560 if ( 'line_items' === $key ) {
561 foreach ( $value as $kk => $vv ) {
562 $line_items[] = '{"so_line_item_id": "' . $vv->line_item_id . '","quantity": "' . $vv->quantity . '"}';
563 }
564 $impot = implode( ',', $line_items );
565
566 $json_package = '"date": "' . $ship_date . '","line_items": [' . $impot . ']';
567
568 $url_package = $zoho_inventory_url . 'inventory/v1/packages/' . $package_id;
569 $data3 = array(
570 'JSONString' => '{' . $json_package . '}',
571 'organization_id' => $zoho_inventory_oid,
572 );
573
574 $res_package = $execute_curl_call_handle->execute_curl_call_put( $url_package, $data3 );
575 }
576 }
577 }
578 // fclose( $fd ); //end of logging
579 return $response;
580 }
581
582 /**
583 * Function to get all Zoho Taxes.
584 *
585 * @param int $percentage Tax percentage.
586 * @return string Tax ID.
587 */
588 private function zi_get_tax_id( $percentage ) {
589 // $fd = fopen( __DIR__ . '/zi_get_tax_id.txt', 'a+' );
590 // get all options that contain zoho_inventory_tax_rate_ in the name using global $wpdb.
591 global $wpdb;
592 $zoho_tax_rates = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'cmbird_zoho_inventory_tax_rate_%'" );
593 $input_tax_percentage = floor( $percentage * 10 ) / 10;
594 // fwrite( $fd, PHP_EOL . 'Input Tax Percentage: ' . $input_tax_percentage );
595 $tax_id = '';
596 // 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
597 foreach ( $zoho_tax_rates as $zoho_tax_rate ) {
598 $tax_rate = explode( '##', $zoho_tax_rate->option_value );
599 $tax_percentage = $tax_rate[3];
600 // Round the stored tax percentage to one decimal place for comparison
601 $stored_tax_percentage = round( $tax_percentage, 1 );
602 // Compare the rounded tax percentages with a tolerance for floating-point precision errors
603 if ( abs( $stored_tax_percentage - $input_tax_percentage ) < 0.01 ) {
604 $tax_id = $tax_rate[0];
605 break;
606 }
607 }
608 return $tax_id;
609 }
610
611 /**
612 * TODO: Get Order Transaction Fees
613 *
614 * @param int $order_id Order ID.
615 * @return float Transaction fees
616 */
617 protected function get_transaction_fees( $order_id ) {
618 $order = wc_get_order( $order_id );
619 switch ( true ) {
620 // get fees from Stripe, if exists
621 case $fees = $order->get_meta( '_stripe_fee' ):
622 break;
623 // get fees from Paypal, if exists
624 case ( $fees = $order->get_meta( '_paypal_transaction_fee' ) ) !== null:
625 break;
626 // otherwise fee is 0
627 default:
628 $fees = 0;
629 break;
630 }
631 return $fees;
632 }
633 }
634 $CMBIRD_Order_Sync_ZI = new CMBIRD_Order_Sync_ZI();
635