PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.14
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.14
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 11 months ago class-import-items.php 11 months ago class-import-price-list.php 1 year ago class-multi-currency.php 1 year ago class-order-sync.php 11 months ago class-product.php 1 year ago class-users-contact.php 1 year ago index.php 1 year ago
class-order-sync.php
631 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' => $order->get_shipping_total(),
256 'order_status' => $this->enable_order_status ? 'draft' : 'confirmed',
257 );
258
259 if ( $this->parent_location_id && $this->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 ( ! empty( $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_tax_id( $order ) {
358 $shipping_total = $order->get_shipping_total();
359 $shipping_tax = $order->get_shipping_tax();
360 if ( $shipping_total > 0 && $shipping_tax > 0 ) {
361 $percent = ( $shipping_tax / $shipping_total ) * 100;
362 return $this->zi_get_tax_id( round( $percent, 2 ) );
363 }
364 return '';
365 }
366
367 private function get_order_fees( $order ) {
368 $fees = $order->get_fees();
369 $data = array();
370 foreach ( $fees as $fee ) {
371 $amount = $fee->get_total();
372 if ( $amount > 0 ) {
373 $data['adjustment'] = $amount;
374 $data['adjustment_description'] = $fee->get_name();
375 }
376 }
377 return $data;
378 }
379
380 private function prepare_custom_fields( $order ) {
381 $fields = array();
382 if ( is_array( $this->custom_fields ) ) {
383 foreach ( $this->custom_fields as $meta_key => $label ) {
384 $fields[] = array(
385 'label' => $label,
386 'value' => $order->get_meta( $meta_key ),
387 );
388 }
389 }
390 return $fields;
391 }
392
393 private function prepare_reference_number( $order ) {
394 $transaction_id = $order->get_transaction_id();
395 if ( empty( $transaction_id ) ) {
396 $transaction_id = $order->get_meta( '_order_number', true );
397 }
398 if ( class_exists( 'WCJ_Order_Numbers' ) || class_exists( 'WC_Seq_Order_Number_Pro' ) ) {
399 return $this->order_prefix . $transaction_id;
400 }
401 if ( ! empty( $this->order_prefix ) ) {
402 return $this->order_prefix . '-' . $order->get_id();
403 }
404 return $order->get_id();
405 }
406
407 /**
408 * Void the sales order if cancelled in WooCommerce.
409 *
410 * @param int $order_id Order ID.
411 */
412 public function salesorder_void( $order_id ) {
413 if ( ! $order_id ) {
414 return;
415 }
416 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
417 if ( empty( $zoho_inventory_access_token ) ) {
418 return;
419 }
420
421 $order = wc_get_order( $order_id );
422 // return if order is already voided
423 if ( $order->get_meta( 'zi_salesorder_void', true ) ) {
424 return;
425 }
426 $order_status = $order->get_status();
427
428 if ( 'cancelled' === $order_status || 'wc-merged' === $order_status ) {
429 $zi_sales_order_id = $order->get_meta( 'zi_salesorder_id', true );
430 $zoho_inventory_oid = $this->zoho_inventory_oid;
431 $zoho_inventory_url = $this->zoho_inventory_url;
432
433 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id . '/status/void?organization_id=' .
434 $zoho_inventory_oid;
435 $data = '';
436 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
437 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
438
439 $errmsg = $json->message;
440 $code = $json->code;
441 if ( '0' === $code || 0 === $code ) {
442 // Add order meta key "zi_salesorder_void" to true
443 $order->update_meta_data( 'zi_salesorder_void', true );
444 $order->add_order_note( 'Zoho Order Void: ' . $errmsg );
445 $order->save();
446 return;
447 }
448 } else {
449 return;
450 }
451 }
452
453 /**
454 * Sync order from Woo to Zoho.
455 *
456 * @param string $pdt1 JSON string
457 * @param int $order_id Order ID.
458 * @return string Error message
459 */
460 public function single_saleorder_zoho_inventory( $pdt1, $order_id ) {
461 //start logging
462 // $fd = fopen( __DIR__ . '/order-sync-backend.txt', 'w+' );
463
464 $zoho_inventory_oid = $this->zoho_inventory_oid;
465 $zoho_inventory_url = $this->zoho_inventory_url;
466 $data = array(
467 'JSONString' => $pdt1,
468 'organization_id' => $zoho_inventory_oid,
469 );
470
471 //logging
472 // fwrite($fd, PHP_EOL . 'Data log : ' . print_r($data, true));
473
474 if ( empty( $data['JSONString'] ) ) {
475 return '';
476 }
477 $ignore_auto_no = $this->enable_auto_number ? 'false' : 'true';
478 $url = $zoho_inventory_url . 'inventory/v1/salesorders?ignore_auto_number_generation=' . $ignore_auto_no;
479
480 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
481 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
482
483 // fwrite( $fd, PHP_EOL . 'Data log : ' . print_r( $json, true ) );
484 $response = array();
485 $code = $json->code;
486 $errmsg = $json->message;
487 // fwrite($fd, PHP_EOL . 'Code : ' . $code);
488
489 if ( '0' === $code || 0 === $code ) {
490 foreach ( $json->salesorder as $key => $value ) {
491
492 if ( 'salesorder_id' === $key ) {
493 $response['zi_salesorder_id'] = $value;
494 // $order->add_meta_data('zi_salesorder_id', $value, true);
495 }
496 }
497 } else {
498 // send email to admin with the error message
499 // create message that contains the error message and order id
500 $message = 'Error in Zoho Inventory Order Sync: ' . $errmsg;
501 // append the link to the order edit page
502 $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>';
503 $message .= '<br><br>Order ID: ' . $order_id;
504 $message .= '<br><br>Request Body: ' . $pdt1;
505 $class_common = new CMBIRD_Common_Functions();
506 $class_common->send_email( 'Error in Zoho Order Sync', $message );
507 }
508 $response['message'] = $errmsg;
509 // fclose( $fd );
510
511 return $response;
512 }
513
514 /**
515 * Function for updating single sales order.
516 *
517 * @param int $order_id Order ID.
518 * @param string $zi_sales_order_id
519 * @param string $pdt1 JSON string
520 * @return string Error message
521 */
522 public function single_saleorder_zoho_inventory_update( $order_id, $zi_sales_order_id, $pdt1 ) {
523 // $fd = fopen( __DIR__. '/single_saleorder_update.txt', 'w+' );
524
525 $response = array();
526 $zoho_inventory_oid = $this->zoho_inventory_oid;
527 $zoho_inventory_url = $this->zoho_inventory_url;
528
529 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id;
530 $data = array(
531 'JSONString' => $pdt1,
532 'organization_id' => $zoho_inventory_oid,
533 );
534
535 $order = wc_get_order( $order_id );
536 // fwrite($fd, PHP_EOL. print_r($data, true)); //logging response
537 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
538 $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data );
539
540 // $code = $json->code;
541 $errmsg = $json->message;
542 $response['message'] = $errmsg;
543 $response['zi_salesorder_id'] = $zi_sales_order_id;
544
545 // echo '<pre>'; print_r($errmsg);
546
547 $package_id = $order->get_meta( 'zi_package_id', true );
548
549 if ( ! empty( $package_id ) ) {
550 // fwrite($fd, PHP_EOL. 'inside package exists'); //logging response
551 foreach ( $json->salesorder as $key => $value ) {
552 if ( 'date' === $key ) {
553 $ship_date = $value;
554 }
555
556 if ( 'line_items' === $key ) {
557 foreach ( $value as $kk => $vv ) {
558 $line_items[] = '{"so_line_item_id": "' . $vv->line_item_id . '","quantity": "' . $vv->quantity . '"}';
559 }
560 $impot = implode( ',', $line_items );
561
562 $json_package = '"date": "' . $ship_date . '","line_items": [' . $impot . ']';
563
564 $url_package = $zoho_inventory_url . 'inventory/v1/packages/' . $package_id;
565 $data3 = array(
566 'JSONString' => '{' . $json_package . '}',
567 'organization_id' => $zoho_inventory_oid,
568 );
569
570 $res_package = $execute_curl_call_handle->execute_curl_call_put( $url_package, $data3 );
571 }
572 }
573 }
574 // fclose( $fd ); //end of logging
575 return $response;
576 }
577
578 /**
579 * Function to get all Zoho Taxes.
580 *
581 * @param int $percentage Tax percentage.
582 * @return string Tax ID.
583 */
584 private function zi_get_tax_id( $percentage ) {
585 // $fd = fopen( __DIR__ . '/zi_get_tax_id.txt', 'a+' );
586 // get all options that contain zoho_inventory_tax_rate_ in the name using global $wpdb.
587 global $wpdb;
588 $zoho_tax_rates = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'cmbird_zoho_inventory_tax_rate_%'" );
589 $input_tax_percentage = floor( $percentage * 10 ) / 10;
590 // fwrite( $fd, PHP_EOL . 'Input Tax Percentage: ' . $input_tax_percentage );
591 $tax_id = '';
592 // 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
593 foreach ( $zoho_tax_rates as $zoho_tax_rate ) {
594 $tax_rate = explode( '##', $zoho_tax_rate->option_value );
595 $tax_percentage = $tax_rate[3];
596 // Round the stored tax percentage to one decimal place for comparison
597 $stored_tax_percentage = round( $tax_percentage, 1 );
598 // Compare the rounded tax percentages with a tolerance for floating-point precision errors
599 if ( abs( $stored_tax_percentage - $input_tax_percentage ) < 0.01 ) {
600 $tax_id = $tax_rate[0];
601 break;
602 }
603 }
604 return $tax_id;
605 }
606
607 /**
608 * TODO: Get Order Transaction Fees
609 *
610 * @param int $order_id Order ID.
611 * @return float Transaction fees
612 */
613 protected function get_transaction_fees( $order_id ) {
614 $order = wc_get_order( $order_id );
615 switch ( true ) {
616 // get fees from Stripe, if exists
617 case $fees = $order->get_meta( '_stripe_fee' ):
618 break;
619 // get fees from Paypal, if exists
620 case ( $fees = $order->get_meta( '_paypal_transaction_fee' ) ) !== null:
621 break;
622 // otherwise fee is 0
623 default:
624 $fees = 0;
625 break;
626 }
627 return $fees;
628 }
629 }
630 $CMBIRD_Order_Sync_ZI = new CMBIRD_Order_Sync_ZI();
631