PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.2
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.2
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 / admin / includes / Actions / Ajax / ZohoInventoryAjax.php
commercebird / admin / includes / Actions / Ajax Last commit date
AcfAjax.php 1 year ago ExactOnlineAjax.php 1 year ago ZohoCRMAjax.php 1 year ago ZohoInventoryAjax.php 1 year ago index.php 1 year ago
ZohoInventoryAjax.php
881 lines
1 <?php
2
3 namespace CommerceBird\Admin\Actions\Ajax;
4
5 use CMBIRD_Auth_Zoho;
6 use CMBIRD_API_Handler_Zoho;
7 use CMBIRD_Pricelist_ZI;
8 use CMBIRD_Categories_ZI;
9 use CommerceBird\Admin\Template;
10 use CommerceBird\Admin\Traits\AjaxRequest;
11 use CommerceBird\Admin\Traits\OptionStatus;
12 use CommerceBird\Admin\Traits\Singleton;
13 use CommerceBird\Admin\Traits\LogWriter;
14 use Throwable;
15 use WC_Tax;
16 use WpOrg\Requests\Exception;
17 use function gettype;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22 final class ZohoInventoryAjax {
23
24 use Singleton;
25 use AjaxRequest;
26 use OptionStatus;
27 use LogWriter;
28
29 private const FORMS = array(
30 'settings' => array(
31 'cors',
32 'id',
33 'email',
34 ),
35 'tax' => array(
36 'selectedTaxRates',
37 ),
38 'product' => array(
39 'item_from_zoho',
40 'disable_stock_sync',
41 'disable_product_sync',
42 'enable_accounting_stock',
43 ),
44 'cron' => array(
45 'form',
46 'categories',
47 ),
48 'order' => array(
49 'disable_sync',
50 'enable_auto_number',
51 'enable_order_status',
52 'order_prefix',
53 'warehouse_id',
54 'enable_warehousestock',
55 ),
56 'contact' => array(
57 'enable_cron',
58 ),
59 'price' => array(
60 'wp_user_role',
61 'zoho_inventory_pricelist',
62 'wcb2b',
63 ),
64 );
65
66 private const ACTIONS = array(
67 'get_subscription' => 'subscription_get',
68 'get_settings' => 'settings_get',
69 'save_settings' => 'settings_set',
70 'reset_settings' => 'settings_reset',
71 'get_zoho_connect' => 'connection_get',
72 'save_zoho_connect' => 'connection_set',
73 'reset_zoho_connect' => 'connection_reset',
74 'get_zoho_tax' => 'tax_get',
75 'save_zoho_tax' => 'tax_set',
76 'reset_zoho_tax' => 'tax_reset',
77 'get_zoho_product' => 'product_get',
78 'save_zoho_product' => 'product_set',
79 'reset_zoho_product' => 'product_reset',
80 'get_zoho_cron' => 'cron_get',
81 'save_zoho_cron' => 'cron_set',
82 'reset_zoho_cron' => 'cron_reset',
83 'get_zoho_order' => 'order_get',
84 'save_zoho_order' => 'order_set',
85 'reset_zoho_order' => 'order_reset',
86 'get_zoho_contact' => 'contact_get',
87 'save_zoho_contact' => 'contact_set',
88 'reset_zoho_contact' => 'contact_reset',
89 'get_zoho_price' => 'price_get',
90 'save_zoho_price' => 'price_set',
91 'reset_zoho_price' => 'price_reset',
92 'get_zoho_fields' => 'fields_get',
93 'save_zoho_fields' => 'fields_set',
94 'reset_zoho_fields' => 'fields_reset',
95 'is_connected' => 'connection_done',
96 'get_wc_taxes' => 'wc_tax_collect',
97 'get_zoho_taxes' => 'zoho_tax_rates_collect',
98 'get_zoho_categories' => 'zoho_categories_collect',
99 'get_zoho_warehouses' => 'zoho_warehouses_collect',
100 'get_zoho_prices' => 'zoho_prices_collect',
101 'get_all_custom_fields' => 'wc_custom_fields_collect',
102 'handle_code' => 'handle_code',
103 );
104
105 private const SOURCE = 'zoho';
106
107 /**
108 * ZohoInventoryAjax constructor.
109 */
110 public function __construct() {
111 $this->load_actions();
112 }
113
114 /**
115 * Collects custom checkout fields for WooCommerce.
116 *
117 * This function verifies the request and collects custom fields from the
118 * THWCFD_Utils::get_checkout_fields() function if the THWCFD class exists.
119 *
120 * @return void
121 */
122 public function wc_custom_fields_collect(): void {
123 $this->verify();
124 $types = array( 'billing', 'shipping', 'additional' );
125 $all_fields = array();
126 $custom_fields = array();
127 // Get all the fields.
128 foreach ( $types as $type ) {
129 // Skip if an unsupported type.
130 if (
131 ! in_array(
132 $type,
133 array(
134 'billing',
135 'shipping',
136 'additional',
137 ),
138 true,
139 )
140 ) {
141 continue;
142 }
143
144 $temp_fields = get_option( 'wc_fields_' . $type );
145 if ( false !== $temp_fields ) {
146 $all_fields = array_merge( $all_fields, $temp_fields );
147 }
148 }
149 // Loop through each field to see if it is a custom field.
150 foreach ( $all_fields as $name => $options ) {
151 if ( isset( $options['custom'] ) && $options['custom'] ) {
152 $label = trim( $options['label'] );
153 $custom_fields[ $name ] = empty( $label ) ? __( 'Please set label', 'commercebird' ) : $label;
154 }
155 }
156
157 $this->response = $custom_fields;
158
159 $this->serve();
160 }
161
162 /**
163 * Retrieves the connection details.
164 *
165 * @return void
166 */
167 public function connection_get(): void {
168 $this->verify();
169 $this->response['account_domain'] = get_option( 'cmbird_zoho_inventory_domain' );
170 $this->response['organization_id'] = get_option( 'cmbird_zoho_inventory_oid' );
171 $this->response['client_id'] = get_option( 'cmbird_zoho_inventory_cid' );
172 $this->response['client_secret'] = get_option( 'cmbird_zoho_inventory_cs' );
173 $this->response['inventory_url'] = get_option( 'cmbird_zoho_inventory_url' );
174 $this->response['redirect_uri'] = get_option( 'cmbird_authorization_redirect_uri' );
175 $this->serve();
176 }
177
178 /**
179 * Retrieves the subscription details.
180 *
181 * @return void
182 */
183 public function subscription_get(): void {
184 $this->verify();
185 $this->response = $this->get_subscription_data();
186 $this->serve();
187 }
188
189 /**
190 * @return array
191 */
192 public function get_subscription_data(): array {
193 $transient = get_transient( 'zoho_subscription' );
194 if ( ! empty( $transient ) ) {
195 return $transient;
196 }
197
198 $data = $this->get_subscription_data_from_api();
199
200 if ( ! empty( $data ) ) {
201 set_transient( 'zoho_subscription', $data, WEEK_IN_SECONDS );
202 }
203
204 return $data;
205 }
206
207 private function get_subscription_data_from_api(): array {
208 $subscription_id = get_option( 'cmbird_zoho_id_status', 0 );
209 if ( ! $subscription_id ) {
210 return array();
211 }
212
213 $response = wp_safe_remote_get(
214 'https://commercebird.com/wp-json/app/v1/subscription/?subscription_id=' . urlencode( $subscription_id ),
215 array(
216 'headers' => array(
217 'Accept' => 'application/json',
218 'Authorization' => 'Basic Y2tfYjAzMDViODhmNmQ1ZDI2ZTY0MjNjMDczZjZmOTVkZTExOWNjOWU1NTpjc182MDljMTNmMjgxODE2YjkzNzQ5OWIyYTAwNTJlMTE0NTc0NWFjZGMz',
219 ),
220 'timeout' => 30,
221 ),
222 );
223
224 if ( is_wp_error( $response ) ) {
225 $this->errors = array( 'message' => $response->get_error_messages() );
226 return array();
227 }
228
229 $body = wp_remote_retrieve_body( $response );
230 $decode = json_decode( $body, true );
231
232 if ( ! $decode || ! array_key_exists( 'line_items', $decode ) ) {
233 return array();
234 }
235
236 $data = $this->extract_data(
237 $decode,
238 array(
239 'fee_lines',
240 'total',
241 'currency',
242 'next_payment_date_gmt',
243 'needs_payment',
244 'payment_url',
245 'status',
246 'variation_id',
247 'line_items',
248 'billing',
249 ),
250 );
251 $data['variation_id'] = array_column( $data['line_items'], 'variation_id' );
252 $data['plan'] = array_column( $data['line_items'], 'name' );
253
254 return $data;
255 }
256
257 /**
258 * Resets the settings details.
259 *
260 * @return void
261 */
262 public function settings_reset(): void {
263 $this->verify();
264 $this->option_status_remove( self::FORMS['settings'], self::SOURCE );
265 $this->response = array( 'message' => 'Reset successfully!' );
266 $this->serve();
267 }
268
269 /**
270 * Retrieves the settings details.
271 *
272 * @return void
273 */
274 public function settings_get(): void {
275 $this->verify();
276 $this->response = $this->option_status_get( self::FORMS['settings'], self::SOURCE );
277 $this->serve();
278 }
279
280 /**
281 * Sets the settings for the class.
282 *
283 * @return void
284 */
285 public function settings_set(): void {
286 try {
287 $this->verify( self::FORMS['settings'] );
288 if ( $this->data ) {
289 $this->option_status_update( $this->data, self::SOURCE );
290 delete_transient( 'zoho_subscription' );
291 } else {
292 $this->errors = array(
293 'message' => 'Invalid Inputs',
294 $this->data,
295 );
296 }
297 } catch (Exception $exception) {
298 $this->errors = array( 'message' => $exception->getMessage() );
299 }
300 $this->serve();
301 }
302
303 /**
304 * Retrieves the fields and serves the response.
305 *
306 * @return void
307 */
308 public function fields_get(): void {
309 $this->verify();
310 $this->response['form'] = get_option( 'cmbird_wootozoho_custom_fields', array() );
311 $this->serve();
312 }
313
314 /**
315 * Sets the custom fields for the form.
316 *
317 * @return void
318 */
319 public function fields_set(): void {
320 $this->verify( array( 'form' ) );
321 try {
322 update_option( 'cmbird_wootozoho_custom_fields', $this->data['form'] );
323 $this->response = array( 'message' => 'saved' );
324 } catch (Throwable $throwable) {
325 $this->errors = array( 'message' => $throwable->getMessage() );
326 }
327 $this->serve();
328 }
329
330 /**
331 * Resets the fields.
332 *
333 * @return void
334 */
335 public function fields_reset(): void {
336 $this->verify();
337 delete_option( 'cmbird_wootozoho_custom_fields' );
338 $this->response = array( 'message' => 'Reset successfully!' );
339 $this->serve();
340 }
341
342 /**
343 * Resets the price.
344 *
345 * @return void
346 */
347 public function price_reset(): void {
348 $this->verify();
349 delete_option( 'cmbird_zoho_pricelist_id' );
350 delete_option( 'cmbird_zoho_pricelist_role' );
351 delete_option( 'cmbird_zoho_pricelist_wcb2b_groups' );
352 $this->response = array( 'message' => 'Reset successfully!' );
353 $this->serve();
354 }
355
356 /**
357 * Retrieves the price information.
358 *
359 * @return void
360 */
361 public function price_get(): void {
362 $this->verify();
363 $this->response['zoho_inventory_pricelist'] = get_option( 'cmbird_zoho_pricelist_id' );
364 $this->response['wp_user_role'] = get_option( 'cmbird_zoho_pricelist_role' );
365 if ( class_exists( 'WooCommerceB2B' ) ) {
366 $this->response['wcb2b'] = get_option( 'cmbird_zoho_pricelist_wcb2b_groups' );
367 }
368 $this->serve();
369 }
370
371 /**
372 * Sets the price and saves the pricelist.
373 *
374 */
375 public function price_set(): void {
376 $this->verify( self::FORMS['price'] );
377 try {
378 $import_pricelist = new CMBIRD_Pricelist_ZI();
379 $success = $import_pricelist->save_price_list( $this->data );
380 if ( class_exists( 'Addify_B2B_Plugin' ) ) {
381 update_option( 'cmbird_zoho_pricelist_role', $this->data['wp_user_role'] );
382 } else {
383 update_option( 'cmbird_zoho_pricelist_wcb2b_groups', $this->data['wcb2b'] );
384 }
385 if ( $success ) {
386 $this->response = array( 'message' => 'Saved' );
387 if ( class_exists( 'WooCommerceB2B' ) ) {
388 $this->response['wcb2b'] = $this->data['wcb2b'];
389 }
390 } else {
391 $this->errors = array( 'message' => 'Failed to save' );
392 }
393 } catch (Throwable $throwable) {
394 $this->errors = array( 'message' => $throwable->getMessage() );
395 $this->write_log(
396 array(
397 'message' => $throwable->getMessage(),
398 'files' => wp_list_pluck( $throwable->getTrace(), 'file', 'line' ),
399 ),
400 'zoho-ajax-error'
401 );
402 }
403
404 $this->serve();
405 }
406
407 /**
408 * Collects Zoho prices.
409 *
410 * @return void
411 */
412 public function zoho_prices_collect(): void {
413 $this->verify();
414 $price_list_class = new CMBIRD_Pricelist_ZI();
415 $prices = $price_list_class->zi_get_all_pricelist();
416 $this->response = wp_list_pluck( $prices, 'name', 'pricebook_id' );
417 $this->serve();
418 }
419
420 /**
421 * Retrieves the contact details.
422 * @return void
423 */
424 public function contact_get() {
425 $this->verify();
426 $this->response = $this->option_status_get( self::FORMS['contact'], self::SOURCE );
427 $this->serve();
428 }
429 /**
430 * Sets the contact and updates the status.
431 * @return void
432 */
433 public function contact_set() {
434 $this->verify( self::FORMS['contact'] );
435 $this->option_status_update( $this->data, self::SOURCE );
436 if ( isset( $this->data['enable'] ) && ! empty( $this->data['enable'] ) ) {
437 if ( ! wp_next_scheduled( 'zoho_contact_sync' ) ) {
438 wp_schedule_event( time(), 'daily', 'zoho_contact_sync' );
439 }
440 } else {
441 wp_clear_scheduled_hook( 'zoho_contact_sync' );
442 }
443 $this->response = array( 'message' => 'Saved!' );
444 $this->serve();
445 }
446 /**
447 * Resets the contact.
448 * @return void
449 */
450 public function contact_reset() {
451 $this->verify();
452 $this->option_status_remove( self::FORMS['contact'], self::SOURCE );
453 $this->response = array( 'message' => 'Reset successfully!' );
454 $this->serve();
455 }
456
457 /**
458 * Sets the order and updates the status.
459 *
460 * @return void
461 */
462 public function order_set(): void {
463 $this->verify( self::FORMS['order'] );
464 $this->option_status_update( $this->data, self::SOURCE );
465 $this->response = array( 'message' => 'Saved!' );
466 $this->serve();
467 }
468
469 /**
470 * Retrieves the order.
471 *
472 * @return void
473 */
474 public function order_get(): void {
475 $this->verify();
476 $this->response = $this->option_status_get( self::FORMS['order'], self::SOURCE );
477 $this->serve();
478 }
479
480 /**
481 * Resets the order.
482 *
483 * @return void
484 */
485 public function order_reset(): void {
486 $this->verify();
487 $this->option_status_remove( self::FORMS['order'], self::SOURCE );
488 $this->response = array( 'message' => 'Reset successfully!' );
489 $this->serve();
490 }
491
492 /**
493 * Collects Zoho warehouses.
494 *
495 * This function verifies the request, retrieves the Zoho inventory
496 * organization ID and URL from the options, constructs the URL
497 * for the API call, and executes the cURL call to retrieve the
498 * warehouses. The response is then processed to extract the
499 * warehouse names and IDs, and stored in the class property.
500 *
501 * @return void
502 */
503 public function zoho_warehouses_collect(): void {
504 $this->verify();
505 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
506 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
507 $url = $zoho_inventory_url . 'inventory/v1/settings/warehouses?organization_id=' . $zoho_inventory_oid;
508 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
509 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
510 $this->response = wp_list_pluck( $json->warehouses, 'warehouse_name', 'warehouse_id' );
511 $this->serve();
512 }
513
514 /**
515 * Sets the cron for the class.
516 *
517 */
518 public function cron_set(): void {
519 $this->verify( self::FORMS['cron'] );
520 if ( array_key_exists( 'form', $this->data ) ) {
521 $decode = json_decode( $this->data['form'], true );
522 update_option( 'zi_cron_interval', $decode['zi_cron_interval'] );
523 unset( $decode['zi_cron_interval'] );
524 $this->option_status_update( $decode, self::SOURCE );
525 }
526 if ( array_key_exists( 'categories', $this->data ) ) {
527 $decode = json_decode( $this->data['categories'] );
528 update_option( 'cmbird_zoho_item_category', serialize( $decode ) );
529 }
530 $this->response = array( 'message' => 'Saved' );
531 // also set the cron job to sync categories.
532 if ( ! wp_next_scheduled( 'cmbird_zi_category_cron' ) ) {
533 wp_schedule_event( time(), 'daily', 'cmbird_zi_category_cron' );
534 }
535 $this->serve();
536 }
537
538 /**
539 * Executes the cron_get function.
540 *
541 * This function verifies the form status and retrieves the form options
542 * for syncing name, price, image, and description. It also retrieves
543 * the cron interval and the Zoho item categories.
544 *
545 * @return void
546 */
547 public function cron_get(): void {
548 $this->verify();
549 $this->response = array();
550 $this->response['form'] = $this->option_status_get(
551 array(
552 'disable_name_sync',
553 'disable_price_sync',
554 'disable_image_sync',
555 'disable_description_sync',
556 ),
557 self::SOURCE,
558 );
559 $this->response['form']['zi_cron_interval'] = get_option( 'zi_cron_interval', 'none' );
560 $categories = get_option( 'cmbird_zoho_item_category', '' );
561 $this->response['categories'] = maybe_unserialize( $categories );
562 $this->serve();
563 }
564
565 /**
566 * Resets the cron job.
567 *
568 * This function verifies the cron job, removes the specified options,
569 * deletes the 'zoho_item_category' option, and serves the cron job.
570 *
571 * @return void
572 */
573 public function cron_reset(): void {
574 $this->verify();
575 $this->option_status_remove(
576 array(
577 'disable_name_sync',
578 'disable_price_sync',
579 'disable_image_sync',
580 'disable_description_sync',
581 ),
582 self::SOURCE,
583 );
584 delete_option( 'cmbird_zoho_item_category' );
585 // delete all options that contain 'zoho_id_for_term_id_'
586 global $wpdb;
587 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'zoho_id_for_term_id_%'" );
588 $this->response = array( 'message' => 'Reset successfully!' );
589 $this->serve();
590 }
591
592 /**
593 * Collects Zoho categories.
594 *
595 * This function verifies the data and retrieves the Zoho item categories.
596 * If the categories are successfully retrieved, it filters the data to
597 * include only the 'name' and 'category_id' fields, and removes the
598 * category with ID -1. Finally, it sets the response with the filtered
599 * categories and serves the response.
600 */
601 public function zoho_categories_collect(): void {
602 $this->verify();
603 $category_class = new CMBIRD_Categories_ZI();
604 $categories = $category_class->cmbird_get_zoho_item_categories();
605 if ( gettype( $categories ) === 'array' ) {
606 // $filtered = wp_list_pluck( $categories, 'name', 'category_id' );
607 // unset( $filtered[ -1 ] );
608 $this->response = $categories;
609 }
610 $this->serve();
611 }
612
613 /**
614 * Executes the connection process and handles the response.
615 */
616 public function connection_done(): void {
617 $this->verify();
618 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
619 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
620 $url = $zoho_inventory_url . 'inventory/v1/apiusagereport/dashboard?version=all&organization_id=' . $zoho_inventory_oid;
621 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
622 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
623 $api_usage = $json->data->api_usage;
624 if ( is_wp_error( $json ) ) {
625 $this->errors = array( 'message' => $json->get_error_message() );
626 } elseif ( empty( $json ) ) {
627 $this->errors = array( 'message' => 'We lost connection with zoho. please refresh page.' );
628 } else {
629 // Convert stdClass of json->api_usage to an array to ensure JSON encoding works properly.
630 $this->response = json_decode( wp_json_encode( $api_usage ), true );
631 }
632 $this->serve();
633 }
634
635 /**
636 * Executes the product_set function.
637 */
638 public function product_set(): void {
639 $this->verify( self::FORMS['product'] );
640 if ( ! empty( $this->data ) ) {
641 $this->option_status_update( $this->data, self::SOURCE );
642 }
643 $this->response = array( 'message' => 'Saved!' );
644 $this->serve();
645 }
646
647 /**
648 * Retrieves the product data.
649 *
650 * @return void
651 */
652 public function product_get(): void {
653 $this->verify();
654 $this->response = $this->option_status_get( self::FORMS['product'], self::SOURCE );
655 $this->serve();
656 }
657
658 /**
659 * Resets the product.
660 *
661 * @return void
662 */
663 public function product_reset(): void {
664 $this->verify();
665 $this->option_status_remove( self::FORMS['product'], self::SOURCE );
666 $this->response = array( 'message' => 'Reset successfully!' );
667 $this->serve();
668 }
669
670 /**
671 * Retrieves the tax information.
672 *
673 * @return void
674 */
675 public function tax_get(): void {
676 $this->verify();
677 $this->response = array();
678 foreach ( $this->wc_taxes() as $tax ) {
679 $this->response['selectedTaxRates'][] = $tax['id'] . '^^' . get_option( 'cmbird_zoho_inventory_tax_rate_' . $tax['id'] );
680 }
681 $this->serve();
682 }
683
684 /**
685 * Retrieves an array of all tax rates from WooCommerce.
686 *
687 * @return array An array of all tax rates.
688 */
689 public function wc_taxes(): array {
690 $wc_tax_array = array();
691 $tax_classes = WC_Tax::get_tax_classes(); // Retrieve all tax classes.
692 if ( ! in_array( '', $tax_classes ) ) { // Make sure "Standard rate" (empty class name) is present.
693 array_unshift( $tax_classes, '' );
694 }
695
696 foreach ( $tax_classes as $tax_class ) { // For each tax class, get all rates.
697 $taxes = WC_Tax::get_rates_for_tax_class( $tax_class );
698
699 foreach ( $taxes as $key => $tax ) {
700 $taxarray = (array) $tax;
701 $taxarray['id'] = $key;
702 $wc_tax_array[] = $taxarray;
703 }
704 }
705
706 return $wc_tax_array;
707 }
708
709 /**
710 * Resets the tax settings.
711 *
712 * This function verifies the tax settings and then deletes the options
713 * related to the tax rates
714 *
715 * @return void
716 */
717 public function tax_reset(): void {
718 $this->verify();
719 foreach ( $this->wc_taxes() as $tax ) {
720 delete_option( 'cmbird_zoho_inventory_tax_rate_' . $tax['id'] );
721 }
722 $this->response = array( 'message' => 'Reset successfully!' );
723 $this->serve();
724 }
725
726 /**
727 * Saves tax rates to the database.
728 */
729 public function tax_set(): void {
730 $this->verify( self::FORMS['tax'] );
731 $rates = array_filter( $this->data['selectedTaxRates'] );
732 try {
733 foreach ( $rates as $value ) {
734 $valarray = explode( '^^', $value );
735 update_option( 'cmbird_zoho_inventory_tax_rate_' . $valarray[0], $valarray[1] );
736 }
737 $this->response = array( 'message' => 'Saved' );
738 } catch (Throwable $throwable) {
739 $this->errors = array( 'message' => $throwable->getMessage() );
740 }
741
742 $this->serve();
743 }
744
745 /**
746 * Sets the connection for the Zoho Inventory API.
747 */
748 public function connection_set(): void {
749 $this->verify(
750 array(
751 'account_domain',
752 'organization_id',
753 'client_id',
754 'client_secret',
755 'redirect_uri',
756 ),
757 );
758 try {
759 $inventory = sprintf( 'https://www.zohoapis.%s/', $this->data['account_domain'] );
760 update_option( 'cmbird_zoho_inventory_domain', $this->data['account_domain'] );
761 update_option( 'cmbird_zoho_inventory_oid', $this->data['organization_id'] );
762 update_option( 'cmbird_zoho_inventory_cid', $this->data['client_id'] );
763 update_option( 'cmbird_zoho_inventory_cs', $this->data['client_secret'] );
764 update_option( 'cmbird_zoho_inventory_url', $inventory );
765 update_option( 'cmbird_authorization_redirect_uri', $this->data['redirect_uri'] );
766 $redirect = esc_url_raw( 'https://accounts.zoho.' . $this->data['account_domain'] . '/oauth/v2/auth?response_type=code&client_id=' . $this->data['client_id'] . '&scope=ZohoInventory.FullAccess.all&redirect_uri=' . $this->data['redirect_uri'] . '&prompt=consent&access_type=offline&state=' . wp_create_nonce( Template::NAME ) );
767 $this->response = array(
768 'redirect' => $redirect,
769 'message' => 'We are redirecting you to zoho. please wait...',
770 );
771 } catch (Throwable $throwable) {
772 $this->errors = array( 'message' => $throwable->getMessage() );
773 }
774
775 $this->serve();
776 }
777
778 /**
779 * Resets the connection by deleting specific options from the database.
780 */
781 public function connection_reset(): void {
782 $this->verify();
783 try {
784 // Check if resetAll is true (sent from Vue)
785 $reset_all = isset( $this->data['reset'] ) && $this->data['reset'] == 1;
786 if ( ! $reset_all ) {
787 $options = array(
788 'cmbird_zoho_inventory_domain',
789 'cmbird_zoho_inventory_oid',
790 'cmbird_zoho_inventory_cid',
791 'cmbird_zoho_inventory_cs',
792 'cmbird_zoho_inventory_url',
793 'cmbird_zoho_inventory_access_token',
794 );
795 foreach ( $options as $zi_option ) {
796 delete_option( $zi_option );
797 }
798 $this->response = array( 'message' => 'Reset successfully!' );
799 } else {
800 global $wpdb;
801 // Delete all product and customer meta keys
802 $wpdb->query( "DELETE FROM {$wpdb->prefix}postmeta WHERE meta_key IN ('zi_item_id', 'zi_category_id')" );
803 $wpdb->query( "DELETE FROM {$wpdb->prefix}usermeta WHERE meta_key IN ('zi_contact_id', 'zi_primary_contact_id', 'zi_billing_address_id', 'zi_shipping_address_id', 'zi_contact_persons_id')" );
804 $this->response['message'] = __( 'Mapping reset completed', 'commercebird' );
805 }
806
807 } catch (Throwable $throwable) {
808 $this->errors = array( 'message' => $throwable->getMessage() );
809 }
810
811 $this->serve();
812 }
813
814 /**
815 * Retrieves the Zoho tax rates for the organization.
816 *
817 * This function verifies the request, retrieves the organization ID and URL
818 * from the options, constructs the URL for the API request, and executes
819 * the cURL call to fetch the tax rates. If the response contains the
820 * 'taxes' key, it sets the response to the tax rates. Otherwise, it sets
821 * the errors to a default message.
822 */
823 public function zoho_tax_rates_collect(): void {
824 $this->verify();
825 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
826 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
827 $url = $zoho_inventory_url . 'inventory/v1/settings/taxes?organization_id=' . $zoho_inventory_oid;
828 try {
829 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
830 $json = (array) $execute_curl_call_handle->execute_curl_call_get( $url );
831 if ( array_key_exists( 'taxes', $json ) ) {
832 $this->response = $json['taxes'];
833 } else {
834 $this->errors = array( 'message' => 'Something went wrong!' );
835 }
836 } catch (Throwable $throwable) {
837 $this->errors = array( 'message' => $throwable->getMessage() );
838 }
839
840 $this->serve();
841 }
842
843 /**
844 * Executes the wc_tax_collect function.
845 *
846 * This function verifies the current state, retrieves the taxes using the
847 * wc_taxes method, and serves the response.
848 */
849 public function wc_tax_collect(): void {
850 $this->verify();
851 $this->response = $this->wc_taxes();
852 $this->serve();
853 }
854
855 /**
856 * Handles the oAuth code.
857 */
858 public function handle_code(): void {
859 $this->verify();
860 if ( array_key_exists( 'code', $this->request ) ) {
861 $class_functions = new CMBIRD_Auth_Zoho();
862 $code = $this->request['code'];
863 try {
864 $access_token = $class_functions->get_zoho_access_token( $code, 'zoho_inventory' );
865 if ( array_key_exists( 'access_token', $access_token ) ) {
866 update_option( 'cmbird_zoho_inventory_auth_code', $code );
867 update_option( 'cmbird_zoho_inventory_access_token', $access_token['access_token'] );
868 update_option( 'cmbird_zoho_inventory_refresh_token', $access_token['refresh_token'] );
869 update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $access_token['expires_in'] );
870 $this->response = (array) $access_token;
871 } else {
872 $this->errors = (array) $access_token;
873 }
874 } catch (Throwable $throwable) {
875 $this->errors = array( 'message' => $throwable->getMessage() );
876 }
877 }
878 $this->serve();
879 }
880 }
881