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