From c52a616f9aa2299fb291ce27d1ac7bc06371e011 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 15 Jan 2020 17:49:40 +0300 Subject: [PATCH] WIP: different (better) logic for corporate clients, fix for possible problem with identifiers --- .../class-wc-retailcrm-abstracts-data.php | 8 + src/include/class-wc-retailcrm-customers.php | 375 +++++------------- src/include/class-wc-retailcrm-history.php | 88 +--- src/include/class-wc-retailcrm-orders.php | 262 ++++-------- ...-retailcrm-customer-corporate-response.php | 123 ------ .../class-wc-retailcrm-order-address.php | 17 +- src/retailcrm.php | 1 - 7 files changed, 189 insertions(+), 685 deletions(-) delete mode 100644 src/include/customer/class-wc-retailcrm-customer-corporate-response.php diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php index 631b200..dcd6f79 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php @@ -60,4 +60,12 @@ abstract class WC_Retailcrm_Abstracts_Data { return apply_filters('retailcrm_before_send_' . $this->filter_name, WC_Retailcrm_Plugin::clearArray($this->data)); } + + /** + * @return array + */ + protected function get_data_without_filters() + { + return $this->data; + } } diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index 7b87aa4..00feb7c 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -12,8 +12,16 @@ if (!class_exists('WC_Retailcrm_Customers')) : /** * Class WC_Retailcrm_Customers */ - class WC_Retailcrm_Customers { + class WC_Retailcrm_Customers + { + /** + * Administrator role + */ + const ADMIN_ROLE = 'administrator'; + /** + * Every customer has this role + */ const CUSTOMER_ROLE = 'customer'; /** @var bool | WC_Retailcrm_Proxy | \WC_Retailcrm_Client_V5 */ @@ -31,9 +39,6 @@ if (!class_exists('WC_Retailcrm_Customers')) : /** @var array */ private $customerCorporate = array(); - /** @var array */ - private $customerCorporateContact = array(); - /** @var array */ private $customerCorporateCompany = array(); @@ -43,8 +48,8 @@ if (!class_exists('WC_Retailcrm_Customers')) : /** * WC_Retailcrm_Customers constructor. * - * @param bool | WC_Retailcrm_Proxy $retailcrm - * @param array $retailcrm_settings + * @param bool | WC_Retailcrm_Proxy $retailcrm + * @param array $retailcrm_settings * @param WC_Retailcrm_Customer_Address $customer_address */ public function __construct($retailcrm = false, $retailcrm_settings, $customer_address) @@ -107,6 +112,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : * Upload customers to CRM * * @param array $ids + * * @return array mixed */ public function customersUpload($ids = array()) @@ -116,9 +122,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : } $users = get_users(array('include' => $ids)); - $corporateEnabled = $this->isCorporateEnabled(); $data_customers = array(); - $data_corporate = array(); foreach ($users as $user) { if (!\in_array(self::CUSTOMER_ROLE, $user->roles)) { @@ -126,13 +130,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : } $customer = $this->wcCustomerGet($user->ID); - if ($corporateEnabled && static::customerPossiblyCorporate($customer)) { - $data_corporate[] = $customer; - } else { - $this->processCustomer($customer); - $data_customers[] = $this->customer; - } - + $this->processCustomer($customer); $data_customers[] = $this->customer; } @@ -143,11 +141,6 @@ if (!class_exists('WC_Retailcrm_Customers')) : time_nanosleep(0, 250000000); } - foreach ($data_corporate as $corporateCustomer) { - $this->createCorporateCustomer($corporateCustomer); - time_nanosleep(0, 50000000); - } - return $data; } @@ -159,38 +152,6 @@ if (!class_exists('WC_Retailcrm_Customers')) : * @return mixed */ public function createCustomer($customer) - { - if ($this->isCorporateEnabled() && static::customerPossiblyCorporate($customer)) { - return $this->createCorporateCustomer($customer); - } else { - return $this->createRegularCustomer($customer); - } - } - - /** - * Update customer in CRM - * - * @param $customer - * - * @return void|\WC_Customer - */ - public function updateCustomer($customer) - { - if ($this->isCorporateEnabled() && static::customerPossiblyCorporate($customer)) { - return $this->updateCorporateCustomer($customer); - } else { - return $this->updateRegularCustomer($customer); - } - } - - /** - * Create regular customer in CRM - * - * @param int | WC_Customer $customer - * - * @return mixed - */ - public function createRegularCustomer($customer) { if (!$this->retailcrm) { return null; @@ -217,13 +178,13 @@ if (!class_exists('WC_Retailcrm_Customers')) : } /** - * Edit regular customer in CRM + * Update customer in CRM * - * @param int $customer_id + * @param $customer_id * - * @return WC_Customer $customer + * @return void|\WC_Customer */ - public function updateRegularCustomer($customer_id) + public function updateCustomer($customer_id) { if (!$this->retailcrm) { return; @@ -231,7 +192,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : $customer = $this->wcCustomerGet($customer_id); - if ($customer->get_role() == self::CUSTOMER_ROLE){ + if ($customer->get_role() == self::CUSTOMER_ROLE) { $this->processCustomer($customer); $this->retailcrm->customersEdit($this->customer); } @@ -242,11 +203,14 @@ if (!class_exists('WC_Retailcrm_Customers')) : /** * Create corporate customer in CRM * + * @param int $crmCustomerId * @param int | WC_Customer $customer + * @param \WC_Order $order * * @return mixed + * @throws \Exception */ - public function createCorporateCustomer($customer) + public function createCorporateCustomerForOrder($crmCustomerId, $customer, $order) { if (!$this->retailcrm) { return null; @@ -261,7 +225,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : } if ($customer->get_role() == self::CUSTOMER_ROLE) { - $this->processCorporateCustomer($customer); + $this->processCorporateCustomer($crmCustomerId, $customer, $order); $response = $this->retailcrm->customersCorporateCreate($this->customerCorporate); return $this->fillCorporateCustomer($response); @@ -270,198 +234,41 @@ if (!class_exists('WC_Retailcrm_Customers')) : return null; } - /** - * Edit customer in CRM - * - * @param int $customer_id - * - * @return WC_Customer|void $customer - */ - public function updateCorporateCustomer($customer_id) - { - if (!$this->retailcrm) { - return; - } - - $customer = $this->wcCustomerGet($customer_id); - - if ($customer->get_role() == self::CUSTOMER_ROLE){ - $this->processCorporateCustomer($customer); - $response = $this->retailcrm->customersCorporateGet($this->customerCorporate['externalId']); - - $this->fillCorporateCustomer($response); - } - - return $customer; - } - /** * Fills corporate customer with required data after customer was created or updated. * Create or update response after sending customer must be passed. * * @param \WC_Retailcrm_Response $response * - * @return \WC_Retailcrm_Customer_Corporate_Response|null + * @return string|int|null */ protected function fillCorporateCustomer($response) { - $customerData = array(); - $addressId = 0; - $companyId = 0; - $contactId = 0; - $contactExternalId = ''; - - if (!$response->isSuccessful()) { + if (!$response->isSuccessful() || $response->isSuccessful() && !$response->offsetExists('id')) { return null; } - if ($response->offsetExists('customerCorporate') && isset($response['customerCorporate']['id'])) { - $customerData = $response['customerCorporate']; - } else { - $customerData = $response; - } + $customerId = $response['id']; + $response = $this->retailcrm->customersCorporateAddressesCreate( + $customerId, + $this->customerCorporateAddress, + 'id', + $this->retailcrm->getSingleSiteForKey() + ); - if (!empty($customerData['id'])) { - $customerData = $this->retailcrm->customersCorporateGet($customerData['id'], 'id'); - - if ($customerData->isSuccessful() && isset($customerData['customerCorporate'])) { - $this->customerCorporate = $customerData['customerCorporate']; - $customerData = $customerData['customerCorporate']; - - // Create main address or obtain existing address - if (empty($customerData['mainAddress'])) { - $addressCreateResponse = $this->retailcrm->customersCorporateAddressesCreate( - $customerData['id'], - $this->customerCorporateAddress, - 'id' - ); - - if ($addressCreateResponse->isSuccessful() && isset($addressCreateResponse['id'])) { - $this->customerCorporateAddress['id'] = $addressCreateResponse['id']; - $addressId = (int) $addressCreateResponse['id']; - } - } else { - $addressEditResponse = $this->retailcrm->customersCorporateAddressesEdit( - $customerData['id'], - $customerData['mainAddress']['id'], - $this->customerCorporateAddress, - 'id', - 'id' - ); - - if ($addressEditResponse->isSuccessful() && isset($addressEditResponse['id'])) { - $this->customerCorporateAddress['id'] = $addressEditResponse['id']; - $addressId = (int) $addressEditResponse['id']; - } - } - - // Update address in company if address was obtained / created - if (!empty($this->customerCorporateCompany) - && isset($this->customerCorporateAddress['id']) - ) { - $this->customerCorporateCompany['address'] = array( - 'id' => $this->customerCorporateAddress['id'] - ); - } - - // Create main company or obtain existing - if (empty($customerData['mainCompany'])) { - $companyCreateResponse = $this->retailcrm->customersCorporateCompaniesCreate( - $customerData['id'], - $this->customerCorporateCompany, - 'id' - ); - - if ($companyCreateResponse->isSuccessful() && isset($companyCreateResponse['id'])) { - $this->customerCorporateCompany['id'] = $companyCreateResponse['id']; - $companyId = (int) $companyCreateResponse['id']; - } - } else { - $companyEditResponse = $this->retailcrm->customersCorporateCompaniesEdit( - $customerData['id'], - $customerData['mainCompany']['id'], - $this->customerCorporateCompany, - 'id', - 'id' - ); - - if ($companyEditResponse->isSuccessful() && isset($companyEditResponse['id'])) { - $this->customerCorporateCompany['id'] = $companyEditResponse['id']; - $companyId = (int) $companyEditResponse['id']; - } - } - - // Create main customer or obtain existing - if (empty($customerData['mainCustomerContact'])) { - $contactCustomerCreated = false; - $contactCustomerGetResponse = - $this->retailcrm->customersGet($this->customerCorporateContact['externalId']); - - if ($contactCustomerGetResponse->isSuccessful() && isset($contactCustomerGetResponse['customer'])) { - $this->customerCorporateContact['id'] = $contactCustomerGetResponse['customer']['id']; - $this->retailcrm->customersEdit($this->customerCorporateContact, 'id'); - $contactId = (int) $contactCustomerGetResponse['customer']['id']; - $contactExternalId = $this->customerCorporateContact['externalId']; - - $contactCustomerCreated = true; - } else { - $contactCustomerCreateResponse = $this->retailcrm->customersCreate($this->customerCorporateContact); - - if ($contactCustomerCreateResponse->isSuccessful() && isset($contactCustomerCreateResponse['id'])) { - $contactId = (int) $contactCustomerCreateResponse['id']; - $contactExternalId = $this->customerCorporateContact['externalId']; - $contactCustomerCreated = true; - } - } - - if ($contactCustomerCreated) { - $contactPair = array( - 'isMain' => true, - 'customer' => array( - 'id' => $contactId, - 'externalId' => $contactExternalId, - 'site' => $this->retailcrm->getSingleSiteForKey() - ) - ); - - // Update company in contact in company was obtained / created - if (!empty($this->customerCorporateContact) - && isset($this->customerCorporateCompany['id']) - ) { - $contactPair['companies'] = array( - array( - 'company' => array( - 'id' => $this->customerCorporateCompany['id'] - ) - ) - ); - } - - $this->retailcrm->customersCorporateContactsCreate( - $customerData['id'], - $contactPair, - 'id' - ); - } - } else { - $this->customerCorporateContact['id'] = $customerData['mainCustomerContact']['customer']['id']; - $this->retailcrm->customersEdit($this->customerCorporateContact, 'id'); - $contactId = (int) $this->customerCorporateContact['id']; - $contactExternalId = $this->customerCorporateContact['externalId']; - } - } - - return new WC_Retailcrm_Customer_Corporate_Response( - isset($this->customerCorporate['id']) ? $this->customerCorporate['id'] : 0, - $this->customerCorporate['externalId'], - $addressId, - $companyId, - $contactId, - $contactExternalId + if ($response->isSuccessful() && $response->offsetExists('id')) { + $this->customerCorporateCompany['address'] = array( + 'id' => $response['id'], + ); + $this->retailcrm->customersCorporateCompaniesCreate( + $customerId, + $this->customerCorporateCompany, + 'id', + $this->retailcrm->getSingleSiteForKey() ); } - return null; + return $customerId; } /** @@ -489,64 +296,53 @@ if (!class_exists('WC_Retailcrm_Customers')) : if ($customer->get_billing_phone()) { $data_customer['phones'][] = array( - 'number' => $customer->get_billing_phone() + 'number' => $customer->get_billing_phone() ); } - $this->customer = apply_filters('retailcrm_process_customer', WC_Retailcrm_Plugin::clearArray($data_customer), $customer); + $this->customer = apply_filters( + 'retailcrm_process_customer', + WC_Retailcrm_Plugin::clearArray($data_customer), + $customer + ); } /** * Process corporate customer * + * @param int $crmCustomerId * @param WC_Customer $customer + * @param \WC_Order $order * * @return void */ - protected function processCorporateCustomer($customer) + protected function processCorporateCustomer($crmCustomerId, $customer, $order) { - $createdAt = $customer->get_date_created(); - $firstName = $customer->get_first_name(); - $data_contact = array( - 'createdAt' => $createdAt->date('Y-m-d H:i:s'), - 'firstName' => $firstName ? $firstName : $customer->get_username(), - 'lastName' => $customer->get_last_name(), - 'email' => $customer->get_email(), - 'address' => $this->customer_address->build($customer)->get_data() - ); $data_company = array( 'isMain' => true, - 'name' => $customer->get_billing_company() + 'name' => $order->get_billing_company() ); + $data_customer = array( - 'externalId' => $customer->get_id(), - 'nickName' => $data_contact['firstName'] + 'nickName' => $customer->get_billing_company(), + 'contact' => array( + 'id' => $crmCustomerId, + 'isMain' => true + ) ); - if ($customer->get_id() > 0) { - $data_contact['externalId'] = static::getContactPersonExternalId($customer->get_id()); - } - - if ($customer->get_billing_phone()) { - $data_contact['phones'][] = array( - 'number' => $customer->get_billing_phone() - ); - } + $orderAddress = new WC_Retailcrm_Order_Address(); + $address = $orderAddress->setAddressType('billing')->build($order)->get_data(); $this->customerCorporate = apply_filters( 'retailcrm_process_customer_corporate', WC_Retailcrm_Plugin::clearArray($data_customer), $customer ); - $this->customerCorporateContact = apply_filters( - 'retailcrm_process_customer_corporate_contact', - WC_Retailcrm_Plugin::clearArray($data_contact), - $customer - ); $this->customerCorporateAddress = apply_filters( 'retailcrm_process_customer_corporate_address', WC_Retailcrm_Plugin::clearArray(array_merge( - $data_contact['address'], + $address, array('isMain' => true) )), $customer @@ -579,7 +375,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : $customer = reset($search['customers']); } else { - $customer = $search['customer']; + $customer = !empty($search['customer']) ? $search['customer'] : false; } return $customer; @@ -589,6 +385,27 @@ if (!class_exists('WC_Retailcrm_Customers')) : } /** + * Returns customer data by externalId or by email, returns false in case of failure + * + * @param $customerExternalId + * @param $customerEmailOrPhone + * + * @return array|bool + */ + public function findCustomerEmailOrId($customerExternalId, $customerEmailOrPhone) + { + $customer = $this->searchCustomer(array('externalId' => $customerExternalId)); + + if (!$customer) { + $customer = $this->searchCustomer(array('email' => $customerEmailOrPhone)); + } + + return $customer; + } + + /** + * Search by provided filter, returns first found customer + * * @param array $filter * * @return bool|array @@ -599,17 +416,21 @@ if (!class_exists('WC_Retailcrm_Customers')) : $search = $this->retailcrm->customersCorporateGet($filter['externalId']); } elseif (isset($filter['email'])) { $search = $this->retailcrm->customersCorporateList(array('email' => $filter['email'])); + } elseif (!empty($filter)) { + $search = $this->retailcrm->customersCorporateList($filter); } - if ($search->isSuccessful()) { + if (isset($search) && $search->isSuccessful()) { if (isset($search['customersCorporate'])) { if (empty($search['customersCorporate'])) { return false; } $customer = reset($search['customersCorporate']); - } else { + } elseif (isset($search['customerCorporate'])) { $customer = $search['customerCorporate']; + } else { + $customer = false; } return $customer; @@ -626,7 +447,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : */ public function buildCustomerFromOrderData($order) { - $new_customer = new WC_Customer; + $new_customer = new WC_Customer(); foreach ($order->get_address('billing') as $prop => $value) { $new_customer->{'set_billing_' . $prop}($value); @@ -644,6 +465,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : * @param int $customer_id * * @return WC_Customer + * @throws \Exception */ public function wcCustomerGet($customer_id) { @@ -657,20 +479,5 @@ if (!class_exists('WC_Retailcrm_Customers')) : { return $this->customer; } - - public static function getContactPersonExternalId($wpCustomerId) - { - return 'wpcontact_' . $wpCustomerId; - } - - public static function isContactPersonExternalId($wpCustomerId) - { - return strpos($wpCustomerId, 'wpcontact_') !== false; - } - - public static function getCustomerIdFromContact($contactExternalId) - { - return str_ireplace('wpcontact_', '', $contactExternalId); - } } endif; diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 960bc55..f922754 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -11,12 +11,12 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : /** * Class WC_Retailcrm_History + * @todo Make changes for correct history with corporate clients! */ class WC_Retailcrm_History { protected $startDateOrders; protected $startDateCustomers; - protected $startDateCustomersCorporate; protected $startDate; protected $retailcrm_settings; @@ -52,7 +52,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $this->startDate = new DateTime(date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))))); $this->startDateOrders = $this->startDate; $this->startDateCustomers = $this->startDate; - $this->startDateCustomersCorporate = $this->startDate; } /** @@ -64,7 +63,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : { $orders_since_id = get_option('retailcrm_orders_history_since_id'); $customers_since_id = get_option('retailcrm_customers_history_since_id'); - $customers_corporate_since_id = get_option('retailcrm_customers_corporate_history_since_id'); if (!$orders_since_id && isset($this->retailcrm_settings['history_orders'])) { $this->startDateOrders = new DateTime($this->retailcrm_settings['history_orders']); @@ -74,86 +72,10 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $this->startDateCustomers = new DateTime($this->retailcrm_settings['history_orders']); } - if (!$customers_corporate_since_id && isset($this->retailcrm_settings['history_customers_corporate'])) { - $this->startDateCustomersCorporate = new DateTime($this->retailcrm_settings['history_orders']); - } - $this->customersHistory($this->startDateCustomers->format('Y-m-d H:i:s'), $customers_since_id); - - if ($this->retailcrm->getCorporateEnabled()) { - $this->customersCorporateHistory($this->startDateCustomersCorporate->format('Y-m-d H:i:s'), $customers_corporate_since_id); - } - $this->ordersHistory($this->startDateOrders->format('Y-m-d H:i:s'), $orders_since_id); } - protected function customersCorporateHistory($date, $since_id) - { - if ($since_id) { - $response = $this->retailcrm->customersCorporateHistory(array('sinceId' => $since_id)); - } else { - $response = $this->retailcrm->customersCorporateHistory(array('startDate' => $date)); - } - - if ($response->isSuccessful()) { - if (empty($response['history'])) { - return; - } - - $history = $response['history']; - $end_change = end($history); - $new_since_id = $end_change['id']; - - // Mapped history fields to WP_User metadata fields - $mapping = array( - 'company.name' => array( - 'billing_company', - 'shipping_company', - ), - 'address.region' => 'billing_state', - 'address.index' => 'billing_postcode', - 'address.country' => 'billing_country', - 'address.city' => 'billing_city' - ); - - foreach ($history as $record) { - if ($record['source'] == 'api' && $record['apiKey']['current'] == true) { - continue; - } - - if (isset($record['customer']['externalId'])) { - $customer = new WC_Customer($record['customer']['externalId']); - - if ($customer->get_id() == 0) { - continue; - } - - WC_Retailcrm_Plugin::$history_run = true; - - if (isset($mapping[$record['field']]) && isset($record['customer']['externalId'])) { - if ($record['newValue']) { - if (is_array($mapping[$record['field']])) { - foreach ($mapping[$record['field']] as $mappingField) { - update_user_meta($record['customer']['externalId'], $mappingField, $record['newValue']); - } - } else { - update_user_meta($record['customer']['externalId'], $mapping[$record['field']], $record['newValue']); - } - } - } - - WC_Retailcrm_Plugin::$history_run = false; - } - } - } - - if (empty($response)) { - return; - } - - update_option('retailcrm_customers_corporate_history_since_id', $new_since_id); - } - /** * History customers * @@ -196,13 +118,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } if (isset($record['customer']['externalId'])) { - if (WC_Retailcrm_Customers::isContactPersonExternalId($record['customer']['externalId'])) { - $customer = new WC_Customer( - WC_Retailcrm_Customers::getCustomerIdFromContact($record['customer']['externalId']) - ); - } else { - $customer = new WC_Customer($record['customer']['externalId']); - } + $customer = new WC_Customer($record['customer']['externalId']); if ($customer->get_id() == 0) { continue; diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 3291c5a..271c16d 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -65,6 +65,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * @param bool $withCustomers * @param array $include * @return array $uploadOrders | null + * @todo Implement correct logic for corporate customers */ public function ordersUpload($include = array(), $withCustomers = false) { @@ -72,7 +73,6 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return null; } - $isCorporateEnabled = $this->retailcrm->getCorporateEnabled(); $uploader = new WC_Retailcrm_Customers( $this->retailcrm, $this->retailcrm_settings, @@ -87,7 +87,6 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : )); $orders_data = array(); - $orders_data_corps = array(); foreach ($orders as $data_order) { $order = wc_get_order($data_order->ID); @@ -103,96 +102,25 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } } - if ($isCorporateEnabled - && WC_Retailcrm_Customers::customerPossiblyCorporate(new WC_Customer($customer->get('ID'))) - ) { - $corporate = $this->retailcrm->customersCorporateGet($customer->get('ID')); - time_nanosleep(0, 100000000); - - if ($corporate instanceof WC_Retailcrm_Response - && $corporate->isSuccessful() - && $corporate->offsetExists('customerCorporate') - && isset($corporate['customerCorporate']['mainCustomerContact']) - && isset($corporate['customerCorporate']['mainCustomerContact']['customer']) - && isset($corporate['customerCorporate']['mainCustomerContact']['customer']['id']) - && isset($corporate['customerCorporate']['mainCustomerContact']['customer']['externalId']) - ) { - $this->order['contact'] = array( - 'id' => - $corporate['customerCorporate']['mainCustomerContact']['customer']['id'] - ); - - $orders_data_corps[] = $this->order; - } else { - $orders_data[] = $this->order; - } - } else { - $orders_data[] = $this->order; - } + $orders_data[] = $this->order; } - $corporateCustomers = array(); - if ($withCustomers === true && !empty($customers)) { - if ($isCorporateEnabled) { - foreach ($customers as $key => $customer) { - if (WC_Retailcrm_Customers::customerPossiblyCorporate(new WC_Customer($customer))) { - $corporateCustomers[] = $customer; - unset($customers[$key]); - } - } + $uploadCustomers = array_chunk($customers, 50); - $uploadCustomers = array_chunk($customers, 50); - - foreach ($uploadCustomers as $uploadCustomer) { - $this->customers->customersUpload($uploadCustomer); - time_nanosleep(0, 250000000); - } - - foreach ($corporateCustomers as $corporateCustomer) { - $response = $uploader->createCorporateCustomer(new WC_Customer($corporateCustomer)); - - if ($response instanceof WC_Retailcrm_Customer_Corporate_Response) { - if ($response->getContactId() != 0) { - foreach ($orders_data as $key => $order) { - if (isset($order['customer']['externalId']) - && $order['customer']['externalId'] == $response->getExternalId() - ) { - $order['contact'] = array( - 'id' => $response->getContactId() - ); - $orders_data_corps[] = $order; - unset($orders_data[$key]); - - break; - } - } - } - } - } - } else { - $uploadCustomers = array_chunk($customers, 50); - - foreach ($uploadCustomers as $uploadCustomer) { - $this->customers->customersUpload($uploadCustomer); - time_nanosleep(0, 250000000); - } + foreach ($uploadCustomers as $uploadCustomer) { + $this->customers->customersUpload($uploadCustomer); + time_nanosleep(0, 250000000); } } $uploadOrders = array_chunk(WC_Retailcrm_Plugin::clearArray($orders_data), 50); - $uploadOrdersCorps = array_chunk(WC_Retailcrm_Plugin::clearArray($orders_data_corps), 50); foreach ($uploadOrders as $uploadOrder) { $this->retailcrm->ordersUpload($uploadOrder); time_nanosleep(0, 250000000); } - foreach ($uploadOrdersCorps as $uploadOrdersCorp) { - $this->retailcrm->ordersUpload($uploadOrdersCorp); - time_nanosleep(0, 250000000); - } - return $uploadOrders; } @@ -201,133 +129,25 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * * @param $order_id * - * @param bool $forceRegularCustomer - * * @return mixed * @throws \Exception */ - public function orderCreate($order_id, $forceRegularCustomer = false) + public function orderCreate($order_id) { if (!$this->retailcrm) { return null; } - $isCorporateEnabled = $forceRegularCustomer ? false : $this->retailcrm->getCorporateEnabled(); $wcOrder = wc_get_order($order_id); $this->processOrder($wcOrder); $wpUser = $wcOrder->get_user(); if ($wpUser instanceof WP_User) { - $wpUserId = (int)$wpUser->get('ID'); - $wooCustomer = new WC_Customer($wpUserId); - - if ($isCorporateEnabled && WC_Retailcrm_Customers::customerPossiblyCorporate($wooCustomer)) { - $foundRegularCustomer = $this->customers->searchCustomer(array( - 'externalId' => $wpUserId - )); - - // If regular customer was found - create order with it. - if (!empty($foundRegularCustomer)) { - return $this->orderCreate($order_id, true); - } - - $foundCustomer = $this->customers->searchCorporateCustomer(array( - 'externalId' => $wpUserId - )); - - if (empty($foundCustomer)) { - $customerData = $this->customers->createCorporateCustomer($wpUserId); - - if ($customerData instanceof WC_Retailcrm_Customer_Corporate_Response) { - if (!empty($customerData->getId())) { - $this->order['customer']['id'] = $customerData->getId(); - } - - if (!empty($customerData->getContactId())) { - $this->order['contact']['id'] = $customerData->getContactId(); - } - - if (!empty($customerData->getContactExternalId())) { - $this->order['contact']['externalId'] = $customerData->getContactExternalId(); - } - } - } else { - $this->order['customer']['externalId'] = $foundCustomer['externalId']; - - if (isset($foundCustomer['mainCustomerContact']['customer']['id'])) { - $this->order['contact']['id'] = $foundCustomer['mainCustomerContact']['customer']['id']; - } - } - } else { - $foundCustomer = $this->customers->searchCustomer(array( - 'externalId' => $wpUserId - )); - - if (empty($foundCustomer)) { - $customerId = $this->customers->createRegularCustomer($wpUserId); - - if (!empty($customerId)) { - $this->order['customer']['id'] = $customerId; - } - } else { - $this->order['customer']['externalId'] = $foundCustomer['externalId']; - } - } + $wpUserId = (int) $wpUser->get('ID'); + $this->fillOrderCreate($wpUserId, $wpUser->get('email'), $wcOrder); } else { $wcCustomer = $this->customers->buildCustomerFromOrderData($wcOrder); - - if ($isCorporateEnabled && WC_Retailcrm_Customers::customerPossiblyCorporate($wcCustomer)) { - $foundRegularCustomer = $this->customers->searchCustomer(array( - 'email' => $wcOrder->get_billing_email() - )); - - // If regular customer was found - create order with it. - if (!empty($foundRegularCustomer)) { - return $this->orderCreate($order_id, true); - } - - $foundCustomer = $this->customers->searchCorporateCustomer(array( - 'email' => $wcOrder->get_billing_email() - )); - - if (empty($foundCustomer)) { - $customerData = $this->customers->createCorporateCustomer($wcCustomer); - - if ($customerData instanceof WC_Retailcrm_Customer_Corporate_Response) { - if (!empty($customerData->getId())) { - $this->order['customer']['id'] = $customerData->getId(); - } - - if (!empty($customerData->getContactId())) { - $this->order['contact']['id'] = $customerData->getContactId(); - } - - if (!empty($customerData->getContactExternalId())) { - $this->order['contact']['externalId'] = $customerData->getContactExternalId(); - } - } - } else { - $this->order['customer']['externalId'] = $foundCustomer['externalId']; - - if (isset($foundCustomer['mainCustomerContact']['customer']['id'])) { - $this->order['contact']['id'] = $foundCustomer['mainCustomerContact']['customer']['id']; - } - } - } else { - $foundCustomer = $this->customers->searchCustomer(array( - 'email' => $wcOrder->get_billing_email() - )); - - if (empty($foundCustomer)) { - $customerId = $this->customers->createRegularCustomer($wcCustomer); - - if (!empty($customerId)) { - $this->order['customer']['id'] = $customerId; - } - } else { - $this->order['customer']['externalId'] = $foundCustomer['externalId']; - } - } + $this->fillOrderCreate(0, $wcCustomer->get_email(), $wcOrder); } $this->retailcrm->ordersCreate($this->order); @@ -335,6 +155,56 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return $wcOrder; } + /** + * Fill order on create + * + * @param int $wcCustomerId + * @param string $wcCustomerEmail + * @param \WC_Order $wcOrder + * + * @throws \Exception + */ + protected function fillOrderCreate($wcCustomerId, $wcCustomerEmail, $wcOrder) + { + $foundCustomerId = ''; + $foundCustomer = $this->customers->findCustomerEmailOrId($wcCustomerId, $wcCustomerEmail); + + if (empty($foundCustomer)) { + $foundCustomerId = $this->customers->createCustomer($wcCustomerId); + + if (!empty($foundCustomerId)) { + $this->order['customer']['id'] = $foundCustomerId; + } + } else { + $this->order['customer']['id'] = $foundCustomer['id']; + $foundCustomerId = $foundCustomer['id']; + } + + if ($this->retailcrm->getCorporateEnabled() && static::isCorporateOrder($wcOrder)) { + $crmCorporate = $this->customers->searchCorporateCustomer(array( + 'contactIds' => array($foundCustomerId) + )); + + // TODO Incorrect logic here: it makes duplicates of corporate clients. Fix that. + if (empty($crmCorporate) || (!empty($crmCorporate) + && isset($crmCorporate['mainCompany']) + && isset($crmCorporate['mainCompany']['name']) + && $crmCorporate['mainCompany']['name'] != $wcOrder->get_billing_company()) + ) { + $corporateId = $this->customers->createCorporateCustomerForOrder( + $foundCustomerId, + $wcCustomerId, + $wcOrder + ); + $this->order['customer']['id'] = $corporateId; + } else { + $this->order['customer']['id'] = $crmCorporate['id']; + } + + $this->order['contact']['id'] = $foundCustomerId; + } + } + /** * Edit order in CRM * @@ -524,5 +394,17 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : { return $this->payment; } + + /** + * Returns true if provided order is for corporate customer + * + * @param WC_Order $order + * + * @return bool + */ + public static function isCorporateOrder($order) + { + return !empty($order->get_billing_company()); + } } endif; diff --git a/src/include/customer/class-wc-retailcrm-customer-corporate-response.php b/src/include/customer/class-wc-retailcrm-customer-corporate-response.php deleted file mode 100644 index 33b0585..0000000 --- a/src/include/customer/class-wc-retailcrm-customer-corporate-response.php +++ /dev/null @@ -1,123 +0,0 @@ - - * @license http://retailcrm.ru Proprietary - * @link http://retailcrm.ru - * @see http://help.retailcrm.ru - */ - -if (!class_exists('WC_Retailcrm_Customer_Corporate_Response')) : -/** - * Class WC_Retailcrm_Customer_Corporate_Response - */ -class WC_Retailcrm_Customer_Corporate_Response -{ - /** - * @var int $corporateId - */ - private $corporateId; - - /** - * @var string $corporateExternalId - */ - private $corporateExternalId; - - /** - * @var int $addressId - */ - private $addressId; - - /** - * @var int $companyId - */ - private $companyId; - - /** - * @var int $contactId - */ - private $contactId; - - /** - * @var int $contactExternalId - */ - private $contactExternalId; - - /** - * WC_Retailcrm_Customer_Corporate_Response constructor. - * - * @param int $corporateId - * @param string $corporateExternalId - * @param int $addressId - * @param int $companyId - * @param int $contactId - * @param string $contactExternalId - */ - public function __construct( - $corporateId, - $corporateExternalId, - $addressId, - $companyId, - $contactId, - $contactExternalId - ) { - $this->corporateId = $corporateId; - $this->corporateExternalId = $corporateExternalId; - $this->addressId = $addressId; - $this->companyId = $companyId; - $this->contactId = $contactId; - $this->contactExternalId = $contactExternalId; - } - - /** - * @return int - */ - public function getId() - { - return $this->corporateId; - } - - /** - * @return string - */ - public function getExternalId() - { - return $this->corporateExternalId; - } - - /** - * @return int - */ - public function getAddressId() - { - return $this->addressId; - } - - /** - * @return int - */ - public function getCompanyId() - { - return $this->companyId; - } - - /** - * @return int - */ - public function getContactId() - { - return $this->contactId; - } - - /** - * @return int - */ - public function getContactExternalId() - { - return $this->contactExternalId; - } -} - -endif; diff --git a/src/include/order/class-wc-retailcrm-order-address.php b/src/include/order/class-wc-retailcrm-order-address.php index 308d70f..54843fe 100644 --- a/src/include/order/class-wc-retailcrm-order-address.php +++ b/src/include/order/class-wc-retailcrm-order-address.php @@ -12,6 +12,21 @@ class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address { protected $filter_name = 'order_address'; + protected $address_type = 'shipping'; + + /** + * Sets address type to work with + * + * @param string $addressType + * + * @return \WC_Retailcrm_Order_Address + */ + public function setAddressType($addressType = 'shipping') + { + $this->address_type = $addressType; + + return $this; + } /** * @param WC_Order $order @@ -20,7 +35,7 @@ class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address */ public function build($order) { - $address = $order->get_address('shipping'); + $address = $order->get_address($this->address_type); if (!empty($address)) { $data = array( diff --git a/src/retailcrm.php b/src/retailcrm.php index 3cfda64..49cb19a 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -47,7 +47,6 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-item.php'); require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-address.php'); require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-address.php'); - require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-corporate-response.php'); require_once(dirname(__FILE__ ) . '/include/class-wc-retailcrm-base.php'); require_once(dirname(__FILE__ ) . '/include/functions.php'); add_filter('woocommerce_integrations', array( $this, 'add_integration'));