From c27a932f2e7a95e8457670b84940cf31fa1cfe71 Mon Sep 17 00:00:00 2001 From: gleemand Date: Wed, 10 Aug 2022 10:21:50 +0300 Subject: [PATCH] Added customer search by email --- .../Forward Synchronization/Customers.md | 5 ++ .../Forward Synchronization/Orders.md | 5 ++ doc/3. Customization/Filters.md | 1 + retailcrm/lib/RetailcrmOrderBuilder.php | 56 +++++++++++++------ retailcrm/retailcrm.php | 9 ++- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/doc/2. Workflow/Forward Synchronization/Customers.md b/doc/2. Workflow/Forward Synchronization/Customers.md index 7824d44..e88d203 100644 --- a/doc/2. Workflow/Forward Synchronization/Customers.md +++ b/doc/2. Workflow/Forward Synchronization/Customers.md @@ -1,5 +1,10 @@ # Customers +## Предотвращение дублирования клиентов + +При создании нового клиента в Prestashop происходит поиск клиента в CRM по externalId и по email. Если клиент найден, то в CRM обновляется его адрес и `externalId`. +Для отключения поиска по email необходимо использовать [фильтр](../../3.%20Customization/Filters.md) `RetailcrmFilterFindCustomerByEmail`, который должен возвращать пустой массив `[]`. + ## Адреса корпоративных клиентов Клиент считается корпоративным, если в его `invoice address` поле `company` не пустое. diff --git a/doc/2. Workflow/Forward Synchronization/Orders.md b/doc/2. Workflow/Forward Synchronization/Orders.md index 8d8e914..69a8232 100644 --- a/doc/2. Workflow/Forward Synchronization/Orders.md +++ b/doc/2. Workflow/Forward Synchronization/Orders.md @@ -2,4 +2,9 @@ ## Create +## Предотвращение дублирования клиентов + +При создании нового заказа в Prestashop происходит поиск клиента в CRM по externalId и по email. Если клиент найден, то в CRM обновляется его адрес и `externalId`. +Для отключения поиска по email необходимо использовать [фильтр](../../3.%20Customization/Filters.md) `RetailcrmFilterFindCustomerByEmail`, который должен возвращать пустой массив `[]`. + ## Update diff --git a/doc/3. Customization/Filters.md b/doc/3. Customization/Filters.md index 8a82b09..b130173 100644 --- a/doc/3. Customization/Filters.md +++ b/doc/3. Customization/Filters.md @@ -25,6 +25,7 @@ There are list of available filters: * *RetailcrmFilterOrdersHistoryCreate* - array with order info, loaded from CRM * *RetailcrmFilterOrdersHistoryUpdate* - array with assembled history for order, loaded from CRM +* *RetailcrmFilterFindCustomerByEmail* - customer array, found by email in CRM (to prevent duplicating) * *RetailcrmFilterSaveCustomer* - built customer object, which will be saved to CMS * *RetailcrmFilterSaveCustomerAddress* - built customer address object, which will be saved to CMS * *RetailcrmFilterSaveCorporateCustomer* - built corporate customer object, which will be saved to CMS diff --git a/retailcrm/lib/RetailcrmOrderBuilder.php b/retailcrm/lib/RetailcrmOrderBuilder.php index da3e06d..85f1ad8 100644 --- a/retailcrm/lib/RetailcrmOrderBuilder.php +++ b/retailcrm/lib/RetailcrmOrderBuilder.php @@ -251,7 +251,7 @@ class RetailcrmOrderBuilder * * @return array|bool */ - private function createCustomerIfNotExist() + public function createCustomerIfNotExist() { $this->validateCmsCustomer(); @@ -271,12 +271,24 @@ class RetailcrmOrderBuilder $customer = $this->createdCustomer; } else { $crmCustomer = RetailcrmTools::mergeCustomerAddress($customer, $this->buildRegularAddress()); - if (!RetailcrmTools::isEqualCustomerAddress($customer, $crmCustomer)) { + + if ( + !RetailcrmTools::isEqualCustomerAddress($customer, $crmCustomer) + || !isset($crmCustomer['externalId']) + || $crmCustomer['externalId'] !== $this->cmsCustomer->id + ) { if (isset($crmCustomer['tags'])) { unset($crmCustomer['tags']); } - $response = $this->api->customersEdit($crmCustomer); + $by = 'externalId'; + + if (!isset($crmCustomer['externalId']) || $crmCustomer['externalId'] !== $this->cmsCustomer->id) { + $crmCustomer['externalId'] = $this->cmsCustomer->id; + $by = 'id'; + } + + $response = $this->api->customersEdit($crmCustomer, $by); if ($response instanceof RetailcrmApiResponse && $response->isSuccessful()) { $customer = $crmCustomer; @@ -609,21 +621,7 @@ class RetailcrmOrderBuilder { $this->validateCmsCustomer(); - if (empty($this->cmsCustomer->id) || $this->cmsCustomer->is_guest) { - if (!empty($this->cmsCustomer->email)) { - $customers = $this->api->customersList(['email' => $this->cmsCustomer->email]); - - if ($customers - && $customers->isSuccessful() - && $customers->offsetExists('customers') - && !empty($customers['customers']) - ) { - $customers = $customers['customers']; - - return reset($customers); - } - } - } else { + if (!empty($this->cmsCustomer->id) && !$this->cmsCustomer->is_guest) { $customer = $this->api->customersGet($this->cmsCustomer->id); if ($customer && $customer->isSuccessful() && $customer->offsetExists('customer')) { @@ -631,6 +629,28 @@ class RetailcrmOrderBuilder } } + if (!empty($this->cmsCustomer->email)) { + $customers = $this->api->customersList(['email' => $this->cmsCustomer->email]); + + if ($customers + && $customers->isSuccessful() + && $customers->offsetExists('customers') + && !empty($customers['customers']) + ) { + $customer = reset($customers['customers']); + + if ($customer['email'] === $this->cmsCustomer->email) { + return RetailcrmTools::filter( + 'RetailcrmFilterFindCustomerByEmail', + $customer, + [ + 'cmsCustomer' => $this->cmsCustomer, + ] + ); + } + } + } + return []; } diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 58a61d3..c118734 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -460,9 +460,14 @@ class RetailCRM extends Module { if ($this->api) { $customer = $params['newCustomer']; - $customerSend = RetailcrmOrderBuilder::buildCrmCustomer($customer); - $this->api->customersCreate($customerSend); + $orderBuilder = new RetailcrmOrderBuilder(); + $orderBuilder + ->defaultLangFromConfiguration() + ->setApi($this->api) + ->setCmsCustomer($customer) + ->createCustomerIfNotExist() + ; return true; }