class-cmbird-categories-zi.php
7 months ago
class-cmbird-image-zi.php
5 months ago
class-import-items.php
4 months ago
class-import-price-list.php
9 months ago
class-multi-currency.php
7 months ago
class-order-sync.php
4 months ago
class-product.php
4 months ago
class-users-contact.php
5 months ago
index.php
1 year ago
class-users-contact.php
724 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 ( 'first_name' === trim( $key ) ) { |
| 221 | $res['zi_first_name'] = $value; |
| 222 | } |
| 223 | if ( 'last_name' === trim( $key ) ) { |
| 224 | $res['zi_last_name'] = $value; |
| 225 | } |
| 226 | if ( 'email' === trim( $key ) ) { |
| 227 | $res['zi_email'] = $value; |
| 228 | } |
| 229 | if ( 'created_time' === trim( $key ) ) { |
| 230 | $res['zi_created_time'] = $value; |
| 231 | } |
| 232 | if ( 'last_modified_time' === trim( $key ) ) { |
| 233 | $res['zi_last_modified_time'] = $value; |
| 234 | } |
| 235 | if ( 'billing_address' === trim( $key ) ) { |
| 236 | if ( isset( $value->address_id ) ) { |
| 237 | $res['zi_billing_address_id'] = $value->address_id; |
| 238 | } |
| 239 | $res['zi_billing_address'] = $value; |
| 240 | } |
| 241 | if ( 'shipping_address' === trim( $key ) ) { |
| 242 | if ( isset( $value->address_id ) ) { |
| 243 | $res['zi_shipping_address_id'] = $value->address_id; |
| 244 | } |
| 245 | $res['zi_shipping_address'] = $value; |
| 246 | } |
| 247 | if ( 'contact_persons' === trim( $key ) ) { |
| 248 | // contact_persons is an array; get the primary contact if available. |
| 249 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 250 | foreach ( $value as $contact_person ) { |
| 251 | if ( isset( $contact_person->is_primary_contact ) && $contact_person->is_primary_contact ) { |
| 252 | $res['zi_contact_person'] = $contact_person; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | if ( 'currency_id' === trim( $key ) ) { |
| 259 | $res['zi_currency_id'] = $value; |
| 260 | } |
| 261 | if ( 'gst_treatment' === trim( $key ) ) { |
| 262 | $res['zi_gst_treatment'] = $value; |
| 263 | } |
| 264 | if ( 'gst_no' === trim( $key ) ) { |
| 265 | $res['zi_gst_no'] = $value; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | foreach ( $res as $key => $val ) { |
| 270 | add_user_meta( $userid, $key, $val ); |
| 271 | } |
| 272 | |
| 273 | $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true ); |
| 274 | } |
| 275 | |
| 276 | return $zi_customer_id; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Function to update the Contact in Zoho when customer updates address on frontend |
| 281 | * |
| 282 | * @param int|string $userid - The user id. |
| 283 | * @param int|string $order_id - The order id (optional). |
| 284 | * @return string - The error message |
| 285 | */ |
| 286 | public function cmbird_contact_update_function( $userid = '', $order_id = '' ) { |
| 287 | // start logging. |
| 288 | // $fd=fopen(__DIR__.'/contact-update-sync.txt','w+'); |
| 289 | |
| 290 | $order = null; |
| 291 | if ( $order_id ) { |
| 292 | $order = wc_get_order( $order_id ); |
| 293 | $order_data = $order->get_data(); |
| 294 | |
| 295 | // BILLING INFORMATION: |
| 296 | $fname = $order_data['billing']['first_name']; |
| 297 | $lname = $order_data['billing']['last_name']; |
| 298 | $name = $fname . ' ' . $lname; |
| 299 | $contact_name = $name; |
| 300 | |
| 301 | $company_name = $order_data['billing']['company']; |
| 302 | $billing_address = $order_data['billing']['address_1']; |
| 303 | $billing_address2 = $order_data['billing']['address_2']; |
| 304 | $billing_city = $order_data['billing']['city']; |
| 305 | $billing_state = $order_data['billing']['state']; |
| 306 | $billing_postcode = $order_data['billing']['postcode']; |
| 307 | $billing_country = $order_data['billing']['country']; |
| 308 | $billing_phone = $order_data['billing']['phone']; |
| 309 | |
| 310 | // SHIPPING INFORMATION: |
| 311 | $shipping_first_name = $order_data['shipping']['first_name']; |
| 312 | $shipping_last_name = $order_data['shipping']['last_name']; |
| 313 | $shipping_address = $order_data['shipping']['address_1']; |
| 314 | $shipping_address2 = $order_data['shipping']['address_2']; |
| 315 | $shipping_city = $order_data['shipping']['city']; |
| 316 | $shipping_state = $order_data['shipping']['state']; |
| 317 | $shipping_postcode = $order_data['shipping']['postcode']; |
| 318 | $shipping_country = $order_data['shipping']['country']; |
| 319 | $shipping_phone = $order_data['shipping']['phone']; |
| 320 | $shipping_attention = $shipping_first_name . ' ' . $shipping_last_name; |
| 321 | } else { |
| 322 | $fname = get_user_meta( $userid, 'billing_first_name', true ); |
| 323 | $lname = get_user_meta( $userid, 'billing_last_name', true ); |
| 324 | $name = $fname . ' ' . $lname; |
| 325 | $contact_name = $name; |
| 326 | $company_name = get_user_meta( $userid, 'billing_company', true ); |
| 327 | $billing_address = get_user_meta( $userid, 'billing_address_1', true ); |
| 328 | $billing_address2 = get_user_meta( $userid, 'billing_address_2', true ); |
| 329 | $billing_city = get_user_meta( $userid, 'billing_city', true ); |
| 330 | $billing_state = get_user_meta( $userid, 'billing_state', true ); |
| 331 | $billing_postcode = get_user_meta( $userid, 'billing_postcode', true ); |
| 332 | $billing_country = get_user_meta( $userid, 'billing_country', true ); |
| 333 | $shipping_first_name = get_user_meta( $userid, 'shipping_first_name', true ); |
| 334 | $shipping_last_name = get_user_meta( $userid, 'shipping_last_name', true ); |
| 335 | $shipping_address = get_user_meta( $userid, 'shipping_address_1', true ); |
| 336 | $shipping_address2 = get_user_meta( $userid, 'shipping_address_2', true ); |
| 337 | $shipping_city = get_user_meta( $userid, 'shipping_city', true ); |
| 338 | $shipping_state = get_user_meta( $userid, 'shipping_state', true ); |
| 339 | $shipping_postcode = get_user_meta( $userid, 'shipping_postcode', true ); |
| 340 | $shipping_country = get_user_meta( $userid, 'shipping_country', true ); |
| 341 | $billing_phone = get_user_meta( $userid, 'billing_phone', true ); |
| 342 | } |
| 343 | |
| 344 | $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true ); |
| 345 | if ( empty( $zi_customer_id ) && $order ) { |
| 346 | // get from order meta if userid is empty. |
| 347 | $zi_customer_id = $order->get_meta( 'zi_contact_id', true ); |
| 348 | } |
| 349 | if ( empty( $zi_customer_id ) ) { |
| 350 | return 'Zoho Inventory Contact ID not found for user.'; |
| 351 | } |
| 352 | |
| 353 | // get vat_number. |
| 354 | $eu_vat = ''; |
| 355 | if ( class_exists( 'WC_EU_VAT_Number' ) ) { |
| 356 | $eu_vat = get_user_meta( $userid, 'vat_number', true ); |
| 357 | } |
| 358 | |
| 359 | // If shipping not available assign billing as shipping. |
| 360 | if ( empty( $shipping_address ) ) { |
| 361 | $shipping_address = $billing_address; |
| 362 | } |
| 363 | if ( empty( $shipping_address2 ) ) { |
| 364 | $shipping_address_2 = $billing_address2; |
| 365 | } |
| 366 | if ( empty( $shipping_postcode ) ) { |
| 367 | $shipping_postcode = $billing_postcode; |
| 368 | } |
| 369 | if ( empty( $shipping_city ) ) { |
| 370 | $shipping_city = $billing_city; |
| 371 | } |
| 372 | if ( empty( $shipping_country ) ) { |
| 373 | $shipping_country = $billing_country; |
| 374 | } |
| 375 | if ( empty( $shipping_state ) ) { |
| 376 | $shipping_state = $billing_state; |
| 377 | } |
| 378 | if ( empty( $shipping_attention ) ) { |
| 379 | $shipping_attention = $contact_name; |
| 380 | } |
| 381 | if ( empty( $shipping_phone ) ) { |
| 382 | $shipping_phone = $billing_phone; |
| 383 | } |
| 384 | |
| 385 | // lets update the contact object. |
| 386 | if ( ! empty( $company_name ) ) { |
| 387 | $pdt2 = '"contact_name": "' . $company_name . '","contact_type": "customer","company_name": "' . $company_name . '"'; |
| 388 | } else { |
| 389 | $pdt2 = '"contact_name": "' . $contact_name . '","contact_type": "customer"'; |
| 390 | } |
| 391 | |
| 392 | $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 . '"'; |
| 393 | |
| 394 | if ( $eu_vat > 0 ) { |
| 395 | $pdt2 .= ',"vat_reg_no": "' . $eu_vat . '","country_code": "' . $billing_country . '"'; |
| 396 | } |
| 397 | |
| 398 | // if $config domain is 'in' then also include 'place_of_contact'. |
| 399 | if ( 'in' === $this->config['ContactZI']['Domain'] ) { |
| 400 | $pdt2 .= ',"place_of_contact": "' . $billing_state . '"'; |
| 401 | // apply gst_treatment if billing_company is not empty. |
| 402 | if ( ! empty( $company_name ) ) { |
| 403 | $pdt2 .= ',"gst_treatment": "Business"'; |
| 404 | } elseif ( 'IN' !== $billing_country ) { |
| 405 | $pdt2 .= ',"gst_treatment": "Overseas"'; |
| 406 | } else { |
| 407 | $pdt2 .= ',"gst_treatment": "Consumer"'; |
| 408 | } |
| 409 | $gst_no = get_user_meta( $userid, 'gst_no', true ); |
| 410 | // if gst_no is empty then get it from order. |
| 411 | if ( empty( $gst_no ) && $order_id ) { |
| 412 | $gst_no = $order->get_meta( '_gst_no', true ); |
| 413 | } |
| 414 | if ( $gst_no ) { |
| 415 | $pdt2 .= ',"gst_no": "' . $gst_no . '"'; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | $zoho_inventory_oid = $this->config['ContactZI']['OID']; |
| 420 | $zoho_inventory_url = $this->config['ContactZI']['APIURL']; |
| 421 | |
| 422 | $data = array( |
| 423 | 'JSONString' => '{' . $pdt2 . '}', |
| 424 | 'organization_id' => $zoho_inventory_oid, |
| 425 | ); |
| 426 | // fwrite($fd,PHP_EOL.'data: '.print_r($data, true)); |
| 427 | $url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id; |
| 428 | // fwrite($fd,PHP_EOL.'URL: '. $url); |
| 429 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 430 | $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data ); |
| 431 | // fwrite($fd, PHP_EOL.'Response log : '.print_r($json, true)); |
| 432 | $code = $json->code; |
| 433 | $errmsg = $json->message; |
| 434 | // end logging. |
| 435 | // fclose($fd); |
| 436 | |
| 437 | if ( '0' === $code || 0 === $code ) { |
| 438 | foreach ( $json->contact as $key => $value ) { |
| 439 | |
| 440 | if ( 'contact_id' === trim( $key ) ) { |
| 441 | $res['zi_contact_id'] = $value; |
| 442 | } |
| 443 | if ( 'primary_contact_id' === trim( $key ) ) { |
| 444 | $res['zi_primary_contact_id'] = $value; |
| 445 | } |
| 446 | if ( 'created_time' === trim( $key ) ) { |
| 447 | $res['zi_created_time'] = $value; |
| 448 | } |
| 449 | if ( 'last_modified_time' === trim( $key ) ) { |
| 450 | $res['zi_last_modified_time'] = $value; |
| 451 | } |
| 452 | if ( 'billing_address' === trim( $key ) ) { |
| 453 | if ( isset( $value->address_id ) ) { |
| 454 | $res['zi_billing_address_id'] = $value->address_id; |
| 455 | } |
| 456 | $res['zi_billing_address'] = $value; |
| 457 | } |
| 458 | if ( 'shipping_address' === trim( $key ) ) { |
| 459 | if ( isset( $value->address_id ) ) { |
| 460 | $res['zi_shipping_address_id'] = $value->address_id; |
| 461 | } |
| 462 | $res['zi_shipping_address'] = $value; |
| 463 | } |
| 464 | if ( 'gst_treatment' === trim( $key ) ) { |
| 465 | $res['zi_gst_treatment'] = $value; |
| 466 | } |
| 467 | if ( 'gst_no' === trim( $key ) ) { |
| 468 | $res['zi_gst_no'] = $value; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | if ( ! empty( $userid ) ) { |
| 473 | foreach ( $res as $key => $val ) { |
| 474 | update_user_meta( $userid, $key, $val ); |
| 475 | } |
| 476 | } elseif ( $order ) { |
| 477 | // save as order meta if userid is empty. |
| 478 | foreach ( $res as $key => $val ) { |
| 479 | $order->update_meta_data( $key, $val ); |
| 480 | } |
| 481 | $order->save(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return $errmsg; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Function to create a new contact person in Zoho Inventory |
| 490 | * |
| 491 | * @param int $userid - user id. |
| 492 | * |
| 493 | * @return int|string - contact person id or error message |
| 494 | */ |
| 495 | public function cmbird_create_contact_person( $userid ) { |
| 496 | // $fd=fopen(__DIR__.'/cmbird_create_contact_person.txt','w+'); |
| 497 | |
| 498 | $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true ); |
| 499 | $fname = get_user_meta( $userid, 'first_name', true ); |
| 500 | $lname = get_user_meta( $userid, 'last_name', true ); |
| 501 | $email = get_user_meta( $userid, 'billing_email', true ); |
| 502 | $mobile = get_user_meta( $userid, 'billing_phone', true ); |
| 503 | |
| 504 | $zoho_inventory_oid = $this->config['ContactZI']['OID']; |
| 505 | $zoho_inventory_url = $this->config['ContactZI']['APIURL']; |
| 506 | |
| 507 | $contact_person_data = '"contact_id": "' . $zi_customer_id . '","first_name": "' . $fname . '","last_name":"' . $lname . '","email": "' . $email . '","phone": "' . $mobile . '","mobile": "' . $mobile . '"'; |
| 508 | $data = array( |
| 509 | 'JSONString' => '{' . $contact_person_data . '}', |
| 510 | 'organization_id' => $zoho_inventory_oid, |
| 511 | ); |
| 512 | |
| 513 | $url = $zoho_inventory_url . 'inventory/v1/contacts/contactpersons'; |
| 514 | |
| 515 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 516 | $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data ); |
| 517 | |
| 518 | // fwrite($fd, PHP_EOL.'JSON : '.print_r($json, true)); |
| 519 | |
| 520 | // fclose($fd); |
| 521 | $code = $json->code; |
| 522 | $errmsg = $json->message; |
| 523 | if ( 0 == $code || '0' == $code ) { |
| 524 | return $json->contact_id; |
| 525 | } else { |
| 526 | return $errmsg; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Function to update contact person in Zoho Inventory. |
| 532 | * |
| 533 | * @param int $userid - user id. |
| 534 | * @param int $contact_person_id - contact person id. |
| 535 | * |
| 536 | * @return string - error message |
| 537 | */ |
| 538 | public function cmbird_update_contact_person( $userid, $contact_person_id ) { |
| 539 | $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true ); |
| 540 | $fname = get_user_meta( $userid, 'first_name', true ); |
| 541 | $lname = get_user_meta( $userid, 'last_name', true ); |
| 542 | $email = get_user_meta( $userid, 'billing_email', true ); |
| 543 | $mobile = get_user_meta( $userid, 'billing_phone', true ); |
| 544 | |
| 545 | $zoho_inventory_oid = $this->config['ContactZI']['OID']; |
| 546 | $zoho_inventory_url = $this->config['ContactZI']['APIURL']; |
| 547 | |
| 548 | $contact_person_data = '"contact_id": ' . $zi_customer_id . ',"first_name": "' . $fname . '","last_name":"' . $lname . '","email": "' . $email . '","phone": "' . $mobile . '","mobile": "' . $mobile . '"'; |
| 549 | $data = array( |
| 550 | 'JSONString' => '{' . $contact_person_data . '}', |
| 551 | 'organization_id' => $zoho_inventory_oid, |
| 552 | ); |
| 553 | |
| 554 | $url = $zoho_inventory_url . 'inventory/v1/contacts/contactpersons/' . $contact_person_id; |
| 555 | |
| 556 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 557 | $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data ); |
| 558 | |
| 559 | $code = $json->code; |
| 560 | $errmsg = $json->message; |
| 561 | return $errmsg; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Function to sync contacts from Zoho Inventory to WP users. |
| 566 | * |
| 567 | * @return void |
| 568 | */ |
| 569 | public function cmbird_get_zoho_contacts() { |
| 570 | // $fd = fopen( __DIR__ . '/cmbird_get_zoho_contacts.txt', 'a+' ); |
| 571 | |
| 572 | $args = func_get_args(); |
| 573 | if ( ! empty( $args ) ) { |
| 574 | $data = $args[0]; |
| 575 | if ( isset( $data['page'] ) ) { |
| 576 | $page = $data['page']; |
| 577 | } else { |
| 578 | $page = 1; |
| 579 | } |
| 580 | } else { |
| 581 | $page = 1; |
| 582 | } |
| 583 | |
| 584 | /* Get Url and org id */ |
| 585 | $zoho_inventory_oid = $this->config['ContactZI']['OID']; |
| 586 | $zoho_inventory_url = $this->config['ContactZI']['APIURL']; |
| 587 | |
| 588 | /* Get call url */ |
| 589 | $url = $zoho_inventory_url . 'inventory/v1/contacts?organization_id=' . $zoho_inventory_oid . '&filter_by=Status.Active&per_page=100&page=' . $page; |
| 590 | $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho(); |
| 591 | $json = $execute_curl_call_handle->execute_curl_call_get( $url ); |
| 592 | |
| 593 | if ( isset( $json->contacts ) ) { |
| 594 | foreach ( $json->contacts as $contacts ) { |
| 595 | // fwrite( $fd, PHP_EOL . 'Contact : ' . print_r( $contacts, true ) ); |
| 596 | $contact_type = trim( $contacts->contact_type ); |
| 597 | $zoho_contact_id = $contacts->contact_id; |
| 598 | $company_name = isset( $contacts->company_name ) ? $contacts->company_name : ''; |
| 599 | $gst_no = isset( $contacts->gst_no ) ? $contacts->gst_no : ''; |
| 600 | $gst_treatment = isset( $contacts->gst_treatment ) ? $contacts->gst_treatment : ''; |
| 601 | |
| 602 | // Extract contact information - prioritize contact-level fields over contact_persons. |
| 603 | $first_name = isset( $contacts->first_name ) ? $contacts->first_name : ''; |
| 604 | $last_name = isset( $contacts->last_name ) ? $contacts->last_name : ''; |
| 605 | $email = isset( $contacts->email ) ? $contacts->email : ''; |
| 606 | $phone = isset( $contacts->phone ) ? $contacts->phone : ''; |
| 607 | |
| 608 | // If contact-level fields are empty, try to extract from primary contact person. |
| 609 | if ( empty( $first_name ) || empty( $last_name ) || empty( $email ) ) { |
| 610 | if ( isset( $contacts->contact_persons ) && is_array( $contacts->contact_persons ) ) { |
| 611 | foreach ( $contacts->contact_persons as $contact_person ) { |
| 612 | if ( isset( $contact_person->is_primary_contact ) && $contact_person->is_primary_contact ) { |
| 613 | if ( empty( $first_name ) && isset( $contact_person->first_name ) ) { |
| 614 | $first_name = $contact_person->first_name; |
| 615 | } |
| 616 | if ( empty( $last_name ) && isset( $contact_person->last_name ) ) { |
| 617 | $last_name = $contact_person->last_name; |
| 618 | } |
| 619 | if ( empty( $email ) && isset( $contact_person->email ) ) { |
| 620 | $email = $contact_person->email; |
| 621 | } |
| 622 | if ( empty( $phone ) && isset( $contact_person->phone ) ) { |
| 623 | $phone = $contact_person->phone; |
| 624 | } |
| 625 | break; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | $credit_limit1 = isset( $contacts->customer_credit_limit ) ? $contacts->customer_credit_limit : 0; |
| 632 | $credit_limit = preg_replace( '/[^0-9,]/', '', $credit_limit1 ); |
| 633 | |
| 634 | // check if user type is customer. |
| 635 | if ( ! empty( $email ) ) { |
| 636 | if ( 'customer' === $contact_type && ! email_exists( $email ) ) { |
| 637 | /* Create WC Customer */ |
| 638 | $user_id = wc_create_new_customer( $email, '', wp_generate_password() ); |
| 639 | if ( ! is_wp_error( $user_id ) ) { |
| 640 | update_user_meta( $user_id, 'zi_contact_id', $zoho_contact_id ); |
| 641 | update_user_meta( $user_id, 'billing_company', $company_name ); |
| 642 | update_user_meta( $user_id, 'billing_phone', $phone ); |
| 643 | update_user_meta( $user_id, 'billing_first_name', $first_name ); |
| 644 | update_user_meta( $user_id, 'billing_last_name', $last_name ); |
| 645 | update_user_meta( $user_id, 'first_name', $first_name ); |
| 646 | update_user_meta( $user_id, 'last_name', $last_name ); |
| 647 | update_user_meta( $user_id, 'billing_email', $email ); |
| 648 | // add gst_no and gst_treatment. |
| 649 | update_user_meta( $user_id, 'gst_no', $gst_no ); |
| 650 | update_user_meta( $user_id, 'gst_treatment', $gst_treatment ); |
| 651 | if ( class_exists( 'WooCommerceB2B' ) ) { |
| 652 | update_user_meta( $user_id, 'wcb2b_unpaid_limit', intval( $credit_limit ) ); |
| 653 | } |
| 654 | } else { |
| 655 | echo esc_html( $user_id->get_error_message() ); |
| 656 | } |
| 657 | } elseif ( email_exists( $email ) ) { |
| 658 | /* Update Wp User if already exist */ |
| 659 | $user_data = get_user_by( 'email', $email ); |
| 660 | if ( ! $user_data ) { |
| 661 | $user_id = $user_data->ID; |
| 662 | update_user_meta( $user_id, 'zi_contact_id', $zoho_contact_id ); |
| 663 | update_user_meta( $user_id, 'billing_company', $company_name ); |
| 664 | update_user_meta( $user_id, 'billing_phone', $phone ); |
| 665 | update_user_meta( $user_id, 'billing_first_name', $first_name ); |
| 666 | update_user_meta( $user_id, 'billing_last_name', $last_name ); |
| 667 | update_user_meta( $user_id, 'first_name', $first_name ); |
| 668 | update_user_meta( $user_id, 'last_name', $last_name ); |
| 669 | // update gst_no and gst_treatment. |
| 670 | update_user_meta( $user_id, 'gst_no', $gst_no ); |
| 671 | update_user_meta( $user_id, 'gst_treatment', $gst_treatment ); |
| 672 | if ( class_exists( 'WooCommerceB2B' ) ) { |
| 673 | update_user_meta( $user_id, 'wcb2b_unpaid_limit', intval( $credit_limit ) ); |
| 674 | } |
| 675 | } else { |
| 676 | echo esc_html( $user_data->get_error_message() ); |
| 677 | } |
| 678 | } |
| 679 | } else { |
| 680 | continue; |
| 681 | } |
| 682 | } |
| 683 | if ( $json->page_context->has_more_page ) { |
| 684 | $data_arr = (object) array(); |
| 685 | $data_arr->page = $page + 1; |
| 686 | $existing_schedule = as_has_scheduled_action( 'sync_zi_import_contacts', array( $data_arr ) ); |
| 687 | if ( ! $existing_schedule ) { |
| 688 | as_schedule_single_action( time(), 'sync_zi_import_contacts', array( $data_arr ) ); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | // fclose( $fd ); |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Update customer store credit balance. Requires Advanced Coupons Plugin. |
| 697 | * |
| 698 | * @param int $user_id User ID of the customer. |
| 699 | * @param int $credit_limit Credit limit of the customer. |
| 700 | */ |
| 701 | protected function cmbird_update_customer_store_credit_balance( $user_id, $credit_limit ) { |
| 702 | if ( ! empty( $user_id ) ) { |
| 703 | $endpoint = '/wc-store-credits/v1/entries'; |
| 704 | $store_credit_balance = get_user_meta( $user_id, 'acfw_store_credit_balance', true ); |
| 705 | if ( $store_credit_balance !== $credit_limit ) { |
| 706 | // first remove the user meta store credit. |
| 707 | update_user_meta( $user_id, 'acfw_store_credit_balance', $credit_limit ); |
| 708 | // create payload for store credit. |
| 709 | $payload = array( |
| 710 | 'user_id' => $user_id, |
| 711 | 'amount' => $credit_limit, |
| 712 | 'type' => 'increase', |
| 713 | 'action' => 'admin_increase', |
| 714 | 'object_id' => 1, |
| 715 | ); |
| 716 | |
| 717 | $request = new \WP_REST_Request( 'POST', $endpoint ); |
| 718 | $request->set_body_params( $payload ); |
| 719 | rest_do_request( $request ); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 |