PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.5.1
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.5.1
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-users-contact.php
commercebird / includes / classes / zoho-inventory Last commit date
class-cmbird-categories-zi.php 9 months ago class-cmbird-image-zi.php 9 months ago class-import-items.php 9 months ago class-import-price-list.php 9 months ago class-multi-currency.php 9 months ago class-order-sync.php 9 months ago class-product.php 9 months ago class-users-contact.php 9 months ago index.php 1 year ago
class-users-contact.php
637 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Handles all user contact related functions for Zoho Inventory integration.
9 *
10 * @package WooZo Inventory
11 * @category Zoho Integration
12 * @author Roadmap Studios
13 * @link https://commercebird.com
14 */
15 class CMBIRD_Contact_ZI {
16
17 /**
18 * Configuration array for Zoho Inventory API.
19 *
20 * @var array
21 */
22 private array $config;
23 /**
24 * Initialize the class.
25 *
26 * Set up the object properties.
27 *
28 * @since 1.0.0
29 */
30 public function __construct() {
31 $config = array(
32
33 'ContactZI' => array(
34 'OID' => get_option( 'cmbird_zoho_inventory_oid' ),
35 'APIURL' => get_option( 'cmbird_zoho_inventory_url' ),
36 'Domain' => get_option( 'cmbird_zoho_inventory_domain' ),
37 ),
38
39 );
40
41 $this->config = $config;
42 }
43
44 /**
45 * Function to create a new contact in Zoho Inventory.
46 *
47 * Called by cmbird_sync_contact_on_order_placed and cmbird_sync_contact_on_user_register hooks.
48 *
49 * @param int $userid - user id from WordPress.
50 *
51 * @return int | void - Zoho Inventory contact id
52 */
53 public function cmbird_contact_create_function( $userid ) {
54 if ( empty( $userid ) ) {
55 return;
56 }
57 // $fd = fopen( __DIR__ . '/cmbird_contact_create_function.txt', 'w+' );
58 $user_order = wc_get_customer_last_order( $userid );
59
60 if ( $user_order ) {
61 // Billing Details.
62 $contact_name = $user_order->get_billing_first_name() . ' ' . $user_order->get_billing_last_name();
63 $company_name = $user_order->get_billing_company();
64 $billing_address = $user_order->get_billing_address_1();
65 $billing_address2 = $user_order->get_billing_address_2();
66 $billing_city = $user_order->get_billing_city();
67 $billing_state = $user_order->get_billing_state();
68 $billing_postcode = $user_order->get_billing_postcode();
69 $billing_country = $user_order->get_billing_country();
70
71 // Shipping Details.
72 $shipping_attention = $user_order->get_shipping_first_name() . ' ' . $user_order->get_shipping_last_name();
73 $shipping_address = $user_order->get_shipping_address_1();
74 $shipping_address2 = $user_order->get_shipping_address_2();
75 $shipping_city = $user_order->get_shipping_city();
76 $shipping_state = $user_order->get_shipping_state();
77 $shipping_postcode = $user_order->get_shipping_postcode();
78 $shipping_country = $user_order->get_shipping_country();
79
80 // Customer Details.
81 $first_name = $user_order->get_billing_first_name();
82 $last_name = $user_order->get_billing_last_name();
83 $email = $user_order->get_billing_email();
84 $mobile = $user_order->get_billing_phone();
85 } else {
86 // If no order found, fallback to user meta.
87 $contact_name = get_user_meta( $userid, 'first_name', true ) . ' ' . get_user_meta( $userid, 'last_name', true );
88 $company_name = get_user_meta( $userid, 'billing_company', true );
89 $billing_address = get_user_meta( $userid, 'billing_address_1', true );
90 $billing_address2 = get_user_meta( $userid, 'billing_address_2', true );
91 $billing_city = get_user_meta( $userid, 'billing_city', true );
92 $billing_state = get_user_meta( $userid, 'billing_state', true );
93 $billing_postcode = get_user_meta( $userid, 'billing_postcode', true );
94 $billing_country = get_user_meta( $userid, 'billing_country', true );
95
96 $shipping_address = get_user_meta( $userid, 'shipping_address_1', true );
97 $shipping_address2 = get_user_meta( $userid, 'shipping_address_2', true );
98 $shipping_city = get_user_meta( $userid, 'shipping_city', true );
99 $shipping_state = get_user_meta( $userid, 'shipping_state', true );
100 $shipping_postcode = get_user_meta( $userid, 'shipping_postcode', true );
101 $shipping_country = get_user_meta( $userid, 'shipping_country', true );
102
103 $first_name = get_user_meta( $userid, 'first_name', true );
104 $last_name = get_user_meta( $userid, 'last_name', true );
105 $email = get_user_meta( $userid, 'billing_email', true );
106 $mobile = get_user_meta( $userid, 'billing_phone', true );
107 }
108
109 $zi_customer_id = 0;
110
111 // get eu vat_number.
112 $eu_vat = '';
113 if ( class_exists( 'WC_EU_VAT_Number' ) ) {
114 $eu_vat = get_user_meta( $userid, 'vat_number', true );
115 }
116
117 // If shipping not available asign billing as shipping.
118 if ( empty( $shipping_address ) ) {
119 $shipping_address = $billing_address;
120 }
121 if ( empty( $shipping_address2 ) ) {
122 $shipping_address_2 = $billing_address2;
123 }
124 if ( empty( $shipping_postcode ) ) {
125 $shipping_postcode = $billing_postcode;
126 }
127 if ( empty( $shipping_city ) ) {
128 $shipping_city = $billing_city;
129 }
130 if ( empty( $shipping_country ) ) {
131 $shipping_country = $billing_country;
132 }
133 if ( empty( $shipping_state ) ) {
134 $shipping_state = $billing_state;
135 }
136 if ( empty( $shipping_attention ) ) {
137 $shipping_attention = $contact_name;
138 }
139
140 // get last order.
141 $currency_id = get_user_meta( $userid, 'zi_currency_id', true );
142 if ( empty( $currency_id ) && $userid ) {
143 $user_currency = $user_order->get_currency();
144 $multi_currency_handle = new CMBIRD_Multicurrency_Zoho();
145 $multi_currency_handle->zoho_currency_data( $user_currency, $userid );
146 }
147
148 // lets create the contact object.
149 if ( $company_name ) {
150 $pdt1 = '"contact_name": "' . $company_name . '","contact_type": "customer","company_name": "' . $company_name . '"';
151 } else {
152 $pdt1 = '"contact_name": "' . $contact_name . '","contact_type": "customer"';
153 }
154
155 $pdt1 .= ',"email": "' . $email . '","mobile": "' . $mobile . '"';
156 $pdt1 .= ',"currency_id": "' . $currency_id . '"';
157
158 $pdt1 .= ',"billing_address": { "attention": "' . $contact_name . '","address": "' . $billing_address . '","street2": "' . $billing_address2 . '","city": "' . $billing_city . '","state": "' . $billing_state . '","zip": "' . $billing_postcode . '","country": "' . $billing_country . '"},"shipping_address": { "attention": "' . $shipping_attention . '","address": "' . $shipping_address . '","street2": "' . $shipping_address2 . '","city": "' . $shipping_city . '","state": "' . $shipping_state . '","zip": "' . $shipping_postcode . '","country": "' . $shipping_country . '"},"contact_persons": [{"first_name": "' . $first_name . '","last_name": "' . $last_name . '","email": "' . $email . '","phone": "' . $mobile . '","is_primary_contact": true}]';
159
160 if ( $eu_vat > 0 ) {
161 $pdt1 .= ',"vat_reg_no": "' . $eu_vat . '","country_code": "' . $billing_country . '"';
162 }
163
164 // if $config domain is 'in' then also include 'place_of_contact'.
165 if ( 'in' === $this->config['ContactZI']['Domain'] ) {
166 $pdt1 .= ',"place_of_contact": "' . $billing_state . '"';
167 // apply gst_treatment if billing_company is not empty.
168 $gst_no = get_user_meta( $userid, 'gst_no', true );
169 // if gst_no is empty then get it from order.
170 if ( empty( $gst_no ) ) {
171 $gst_no = $user_order->get_meta( '_gst_no', true );
172 }
173 if ( $gst_no ) {
174 $pdt1 .= ',"gst_no": "' . $gst_no . '"';
175 }
176
177 // GST treatment logic - prioritize company + GST combination first.
178 if ( ! empty( $company_name ) && ! empty( $gst_no ) ) {
179 // Company with GST number.
180 $pdt1 .= ',"gst_treatment": "business_gst"';
181 } elseif ( ! empty( $company_name ) && empty( $gst_no ) && 'IN' === $billing_country ) {
182 // Company without GST number in India.
183 $pdt1 .= ',"gst_treatment": "business_none"';
184 } elseif ( 'IN' !== $billing_country ) {
185 // Non-Indian customer (overseas).
186 $pdt1 .= ',"gst_treatment": "overseas"';
187 } else {
188 // Individual consumer in India.
189 $pdt1 .= ',"gst_treatment": "consumer"';
190 }
191 }
192
193 $zoho_inventory_oid = $this->config['ContactZI']['OID'];
194 $zoho_inventory_url = $this->config['ContactZI']['APIURL'];
195
196 $data = array(
197 'JSONString' => '{' . $pdt1 . '}',
198 'organization_id' => $zoho_inventory_oid,
199 );
200
201 // logging.
202 // fwrite( $fd, PHP_EOL . 'Data Body: ' . print_r( $data, true ) );
203
204 $url = $zoho_inventory_url . 'inventory/v1/contacts';
205
206 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
207 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
208 $code = $json->code;
209 // fwrite( $fd, PHP_EOL . 'Response: ' . print_r( $json, true ) );
210
211 if ( '0' == $code || 0 == $code ) {
212 foreach ( $json->contact as $key => $value ) {
213
214 if ( 'contact_id' === trim( $key ) ) {
215 $res['zi_contact_id'] = $value;
216 }
217 if ( 'primary_contact_id' === trim( $key ) ) {
218 $res['zi_primary_contact_id'] = $value;
219 }
220 if ( 'created_time' === trim( $key ) ) {
221 $res['zi_created_time'] = $value;
222 }
223 if ( 'last_modified_time' === trim( $key ) ) {
224 $res['zi_last_modified_time'] = $value;
225 }
226 if ( 'billing_address' === trim( $key ) ) {
227 $res['zi_billing_address_id'] = $value->address_id;
228 }
229 if ( 'shipping_address' === trim( $key ) ) {
230 $res['zi_shipping_address_id'] = $value->address_id;
231 }
232 if ( 'contact_persons' === trim( $key ) ) {
233 $res['zi_contact_persons_id'] = $value;
234 }
235 }
236
237 foreach ( $res as $key => $val ) {
238 add_user_meta( $userid, $key, $val );
239 }
240
241 $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true );
242 }
243
244 return $zi_customer_id;
245 }
246
247 /**
248 * Function to update the Contact in Zoho when customer updates address on frontend
249 *
250 * @param int $userid - The user id.
251 * @param int $order_id - The order id (optional).
252 * @return string - The error message
253 */
254 public function cmbird_contact_update_function( $userid, $order_id = '' ) {
255 // start logging.
256 // $fd=fopen(__DIR__.'/contact-update-sync.txt','w+');
257
258 if ( $order_id ) {
259 $order = wc_get_order( $order_id );
260 $order_data = $order->get_data();
261
262 // BILLING INFORMATION:
263 $fname = $order_data['billing']['first_name'];
264 $lname = $order_data['billing']['last_name'];
265 $name = $fname . ' ' . $lname;
266 $contact_name = $name;
267
268 $company_name = $order_data['billing']['company'];
269 $billing_address = $order_data['billing']['address_1'];
270 $billing_address2 = $order_data['billing']['address_2'];
271 $billing_city = $order_data['billing']['city'];
272 $billing_state = $order_data['billing']['state'];
273 $billing_postcode = $order_data['billing']['postcode'];
274 $billing_country = $order_data['billing']['country'];
275 $billing_phone = $order_data['billing']['phone'];
276
277 // SHIPPING INFORMATION:
278 $shipping_first_name = $order_data['shipping']['first_name'];
279 $shipping_last_name = $order_data['shipping']['last_name'];
280 $shipping_address = $order_data['shipping']['address_1'];
281 $shipping_address2 = $order_data['shipping']['address_2'];
282 $shipping_city = $order_data['shipping']['city'];
283 $shipping_state = $order_data['shipping']['state'];
284 $shipping_postcode = $order_data['shipping']['postcode'];
285 $shipping_country = $order_data['shipping']['country'];
286 $shipping_phone = $order_data['shipping']['phone'];
287 $shipping_attention = $shipping_first_name . ' ' . $shipping_last_name;
288 } else {
289 $fname = get_user_meta( $userid, 'billing_first_name', true );
290 $lname = get_user_meta( $userid, 'billing_last_name', true );
291 $name = $fname . ' ' . $lname;
292 $contact_name = $name;
293 $company_name = get_user_meta( $userid, 'billing_company', true );
294 $billing_address = get_user_meta( $userid, 'billing_address_1', true );
295 $billing_address2 = get_user_meta( $userid, 'billing_address_2', true );
296 $billing_city = get_user_meta( $userid, 'billing_city', true );
297 $billing_state = get_user_meta( $userid, 'billing_state', true );
298 $billing_postcode = get_user_meta( $userid, 'billing_postcode', true );
299 $billing_country = get_user_meta( $userid, 'billing_country', true );
300 $shipping_first_name = get_user_meta( $userid, 'shipping_first_name', true );
301 $shipping_last_name = get_user_meta( $userid, 'shipping_last_name', true );
302 $shipping_address = get_user_meta( $userid, 'shipping_address_1', true );
303 $shipping_address2 = get_user_meta( $userid, 'shipping_address_2', true );
304 $shipping_city = get_user_meta( $userid, 'shipping_city', true );
305 $shipping_state = get_user_meta( $userid, 'shipping_state', true );
306 $shipping_postcode = get_user_meta( $userid, 'shipping_postcode', true );
307 $shipping_country = get_user_meta( $userid, 'shipping_country', true );
308 $billing_phone = get_user_meta( $userid, 'billing_phone', true );
309 }
310
311 $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true );
312
313 // get vat_number.
314 $eu_vat = '';
315 if ( class_exists( 'WC_EU_VAT_Number' ) ) {
316 $eu_vat = get_user_meta( $userid, 'vat_number', true );
317 }
318
319 // If shipping not available assign billing as shipping.
320 if ( empty( $shipping_address ) ) {
321 $shipping_address = $billing_address;
322 }
323 if ( empty( $shipping_address2 ) ) {
324 $shipping_address_2 = $billing_address2;
325 }
326 if ( empty( $shipping_postcode ) ) {
327 $shipping_postcode = $billing_postcode;
328 }
329 if ( empty( $shipping_city ) ) {
330 $shipping_city = $billing_city;
331 }
332 if ( empty( $shipping_country ) ) {
333 $shipping_country = $billing_country;
334 }
335 if ( empty( $shipping_state ) ) {
336 $shipping_state = $billing_state;
337 }
338 if ( empty( $shipping_attention ) ) {
339 $shipping_attention = $contact_name;
340 }
341 if ( empty( $shipping_phone ) ) {
342 $shipping_phone = $billing_phone;
343 }
344
345 // lets update the contact object.
346 if ( ! empty( $company_name ) ) {
347 $pdt2 = '"contact_name": "' . $company_name . '","contact_type": "customer","company_name": "' . $company_name . '"';
348 } else {
349 $pdt2 = '"contact_name": "' . $contact_name . '","contact_type": "customer"';
350 }
351
352 $pdt2 .= ',"billing_address": { "attention": "' . $contact_name . '","address": "' . $billing_address . '","street2": "' . $billing_address2 . '","city": "' . $billing_city . '","state": "' . $billing_state . '","zip": "' . $billing_postcode . '","country": "' . $billing_country . '"},"shipping_address": { "attention": "' . $shipping_attention . '","address": "' . $shipping_address . '","street2": "' . $shipping_address2 . '","city": "' . $shipping_city . '","state": "' . $shipping_state . '","zip": "' . $shipping_postcode . '","country": "' . $shipping_country . '","phone": "' . $shipping_phone . '"}, "phone": "' . $billing_phone . '", "mobile": "' . $billing_phone . '"';
353
354 if ( $eu_vat > 0 ) {
355 $pdt2 .= ',"vat_reg_no": "' . $eu_vat . '","country_code": "' . $billing_country . '"';
356 }
357
358 // if $config domain is 'in' then also include 'place_of_contact'.
359 if ( 'in' === $this->config['ContactZI']['Domain'] ) {
360 $pdt2 .= ',"place_of_contact": "' . $billing_state . '"';
361 // apply gst_treatment if billing_company is not empty.
362 if ( ! empty( $company_name ) ) {
363 $pdt2 .= ',"gst_treatment": "Business"';
364 } elseif ( 'IN' !== $billing_country ) {
365 $pdt2 .= ',"gst_treatment": "Overseas"';
366 } else {
367 $pdt2 .= ',"gst_treatment": "Consumer"';
368 }
369 $gst_no = get_user_meta( $userid, 'gst_no', true );
370 // if gst_no is empty then get it from order.
371 if ( empty( $gst_no ) && $order_id ) {
372 $gst_no = $order->get_meta( '_gst_no', true );
373 }
374 if ( $gst_no ) {
375 $pdt2 .= ',"gst_no": "' . $gst_no . '"';
376 }
377 }
378
379 $zoho_inventory_oid = $this->config['ContactZI']['OID'];
380 $zoho_inventory_url = $this->config['ContactZI']['APIURL'];
381
382 $data = array(
383 'JSONString' => '{' . $pdt2 . '}',
384 'organization_id' => $zoho_inventory_oid,
385 );
386 // fwrite($fd,PHP_EOL.'data: '.print_r($data, true));
387 $url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id;
388 // fwrite($fd,PHP_EOL.'URL: '. $url);
389 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
390 $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data );
391 // fwrite($fd, PHP_EOL.'Response log : '.print_r($json, true));
392 $code = $json->code;
393 $errmsg = $json->message;
394 // end logging.
395 // fclose($fd);
396
397 if ( '0' === $code || 0 === $code ) {
398 foreach ( $json->contact as $key => $value ) {
399
400 if ( 'contact_id' === trim( $key ) ) {
401 $res['zi_contact_id'] = $value;
402 }
403 if ( 'primary_contact_id' === trim( $key ) ) {
404 $res['zi_primary_contact_id'] = $value;
405 }
406 if ( 'created_time' === trim( $key ) ) {
407 $res['zi_created_time'] = $value;
408 }
409 if ( 'last_modified_time' === trim( $key ) ) {
410 $res['zi_last_modified_time'] = $value;
411 }
412 if ( 'billing_address' === trim( $key ) ) {
413 $res['zi_billing_address_id'] = $value->address_id;
414 }
415 if ( 'shipping_address' === trim( $key ) ) {
416 $res['zi_shipping_address_id'] = $value->address_id;
417 }
418 }
419
420 foreach ( $res as $key => $val ) {
421 update_user_meta( $userid, $key, $val );
422 }
423 }
424
425 return $errmsg;
426 }
427
428 /**
429 * Function to create a new contact person in Zoho Inventory
430 *
431 * @param int $userid - user id.
432 *
433 * @return int|string - contact person id or error message
434 */
435 public function cmbird_create_contact_person( $userid ) {
436 // $fd=fopen(__DIR__.'/cmbird_create_contact_person.txt','w+');
437
438 $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true );
439 $fname = get_user_meta( $userid, 'first_name', true );
440 $lname = get_user_meta( $userid, 'last_name', true );
441 $email = get_user_meta( $userid, 'billing_email', true );
442 $mobile = get_user_meta( $userid, 'billing_phone', true );
443
444 $zoho_inventory_oid = $this->config['ContactZI']['OID'];
445 $zoho_inventory_url = $this->config['ContactZI']['APIURL'];
446
447 $contact_person_data = '"contact_id": "' . $zi_customer_id . '","first_name": "' . $fname . '","last_name":"' . $lname . '","email": "' . $email . '","phone": "' . $mobile . '","mobile": "' . $mobile . '"';
448 $data = array(
449 'JSONString' => '{' . $contact_person_data . '}',
450 'organization_id' => $zoho_inventory_oid,
451 );
452
453 $url = $zoho_inventory_url . 'inventory/v1/contacts/contactpersons';
454
455 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
456 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
457
458 // fwrite($fd, PHP_EOL.'JSON : '.print_r($json, true));
459
460 // fclose($fd);
461 $code = $json->code;
462 $errmsg = $json->message;
463 if ( 0 == $code || '0' == $code ) {
464 return $json->contact_id;
465 } else {
466 return $errmsg;
467 }
468 }
469
470 /**
471 * Function to update contact person in Zoho Inventory.
472 *
473 * @param int $userid - user id.
474 * @param int $contact_person_id - contact person id.
475 *
476 * @return string - error message
477 */
478 public function cmbird_update_contact_person( $userid, $contact_person_id ) {
479 $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true );
480 $fname = get_user_meta( $userid, 'first_name', true );
481 $lname = get_user_meta( $userid, 'last_name', true );
482 $email = get_user_meta( $userid, 'billing_email', true );
483 $mobile = get_user_meta( $userid, 'billing_phone', true );
484
485 $zoho_inventory_oid = $this->config['ContactZI']['OID'];
486 $zoho_inventory_url = $this->config['ContactZI']['APIURL'];
487
488 $contact_person_data = '"contact_id": ' . $zi_customer_id . ',"first_name": "' . $fname . '","last_name":"' . $lname . '","email": "' . $email . '","phone": "' . $mobile . '","mobile": "' . $mobile . '"';
489 $data = array(
490 'JSONString' => '{' . $contact_person_data . '}',
491 'organization_id' => $zoho_inventory_oid,
492 );
493
494 $url = $zoho_inventory_url . 'inventory/v1/contacts/contactpersons/' . $contact_person_id;
495
496 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
497 $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data );
498
499 $code = $json->code;
500 $errmsg = $json->message;
501 return $errmsg;
502 }
503
504 /**
505 * Function to sync contacts from Zoho Inventory to WP users.
506 *
507 * @return void
508 */
509 public function cmbird_get_zoho_contacts() {
510 // $fd = fopen( __DIR__ . '/cmbird_get_zoho_contacts.txt', 'a+' );
511
512 $args = func_get_args();
513 if ( ! empty( $args ) ) {
514 $data = $args[0];
515 if ( isset( $data['page'] ) ) {
516 $page = $data['page'];
517 } else {
518 $page = 1;
519 }
520 } else {
521 $page = 1;
522 }
523
524 /* Get Url and org id */
525 $zoho_inventory_oid = $this->config['ContactZI']['OID'];
526 $zoho_inventory_url = $this->config['ContactZI']['APIURL'];
527
528 /* Get call url */
529 $url = $zoho_inventory_url . 'inventory/v1/contacts?organization_id=' . $zoho_inventory_oid . '&filter_by=Status.Active&per_page=100&page=' . $page;
530 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
531 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
532
533 if ( isset( $json->contacts ) ) {
534 foreach ( $json->contacts as $contacts ) {
535 // fwrite( $fd, PHP_EOL . 'Contact : ' . print_r( $contacts, true ) );
536 $contact_type = trim( $contacts->contact_type );
537 $zoho_contact_id = $contacts->contact_id;
538 $first_name = isset( $contacts->first_name ) ? $contacts->first_name : '';
539 $last_name = isset( $contacts->last_name ) ? $contacts->last_name : '';
540 $email = isset( $contacts->email ) ? $contacts->email : '';
541 $phone = isset( $contacts->phone ) ? $contacts->phone : '';
542 $company_name = isset( $contacts->company_name ) ? $contacts->company_name : '';
543 $credit_limit1 = $contacts->customer_credit_limit;
544 $credit_limit = preg_replace( '/[^0-9,]/', '', $credit_limit1 );
545 $gst_no = isset( $contacts->gst_no ) ? $contacts->gst_no : '';
546 $gst_treatment = isset( $contacts->gst_treatment ) ? $contacts->gst_treatment : '';
547 // check if user type is customer.
548 if ( ! empty( $email ) ) {
549 if ( 'customer' === $contact_type && ! email_exists( $email ) ) {
550 /* Create WC Customer */
551 $user_id = wc_create_new_customer( $email, '', wp_generate_password() );
552 if ( ! is_wp_error( $user_id ) ) {
553 update_user_meta( $user_id, 'zi_contact_id', $zoho_contact_id );
554 update_user_meta( $user_id, 'billing_company', $company_name );
555 update_user_meta( $user_id, 'billing_phone', $phone );
556 update_user_meta( $user_id, 'billing_first_name', $first_name );
557 update_user_meta( $user_id, 'billing_last_name', $last_name );
558 update_user_meta( $user_id, 'first_name', $first_name );
559 update_user_meta( $user_id, 'last_name', $last_name );
560 update_user_meta( $user_id, 'billing_email', $email );
561 // add gst_no and gst_treatment.
562 update_user_meta( $user_id, 'gst_no', $gst_no );
563 update_user_meta( $user_id, 'gst_treatment', $gst_treatment );
564 if ( class_exists( 'WooCommerceB2B' ) ) {
565 update_user_meta( $user_id, 'wcb2b_unpaid_limit', intval( $credit_limit ) );
566 }
567 } else {
568 echo esc_html( $user_id->get_error_message() );
569 }
570 } elseif ( email_exists( $email ) ) {
571 /* Update Wp User if already exist */
572 $user_data = get_user_by( 'email', $email );
573 if ( ! $user_data ) {
574 $user_id = $user_data->ID;
575 update_user_meta( $user_id, 'zi_contact_id', $zoho_contact_id );
576 update_user_meta( $user_id, 'billing_company', $company_name );
577 update_user_meta( $user_id, 'billing_phone', $phone );
578 update_user_meta( $user_id, 'billing_first_name', $first_name );
579 update_user_meta( $user_id, 'billing_last_name', $last_name );
580 update_user_meta( $user_id, 'first_name', $first_name );
581 update_user_meta( $user_id, 'last_name', $last_name );
582 // update gst_no and gst_treatment.
583 update_user_meta( $user_id, 'gst_no', $gst_no );
584 update_user_meta( $user_id, 'gst_treatment', $gst_treatment );
585 if ( class_exists( 'WooCommerceB2B' ) ) {
586 update_user_meta( $user_id, 'wcb2b_unpaid_limit', intval( $credit_limit ) );
587 }
588 } else {
589 echo esc_html( $user_data->get_error_message() );
590 }
591 }
592 } else {
593 continue;
594 }
595 }
596 if ( $json->page_context->has_more_page ) {
597 $data_arr = (object) array();
598 $data_arr->page = $page + 1;
599 $existing_schedule = as_has_scheduled_action( 'sync_zi_import_contacts', array( $data_arr ) );
600 if ( ! $existing_schedule ) {
601 as_schedule_single_action( time(), 'sync_zi_import_contacts', array( $data_arr ) );
602 }
603 }
604 }
605 // fclose( $fd );
606 }
607
608 /**
609 * Update customer store credit balance. Requires Advanced Coupons Plugin.
610 *
611 * @param int $user_id User ID of the customer.
612 * @param int $credit_limit Credit limit of the customer.
613 */
614 protected function cmbird_update_customer_store_credit_balance( $user_id, $credit_limit ) {
615 if ( ! empty( $user_id ) ) {
616 $endpoint = '/wc-store-credits/v1/entries‌';
617 $store_credit_balance = get_user_meta( $user_id, 'acfw_store_credit_balance', true );
618 if ( $store_credit_balance !== $credit_limit ) {
619 // first remove the user meta store credit.
620 update_user_meta( $user_id, 'acfw_store_credit_balance', $credit_limit );
621 // create payload for store credit.
622 $payload = array(
623 'user_id' => $user_id,
624 'amount' => $credit_limit,
625 'type' => 'increase',
626 'action' => 'admin_increase',
627 'object_id' => 1,
628 );
629
630 $request = new \WP_REST_Request( 'POST', $endpoint );
631 $request->set_body_params( $payload );
632 rest_do_request( $request );
633 }
634 }
635 }
636 }
637