diff --git a/admin/model/extension/retailcrm/history.php b/admin/model/extension/retailcrm/history.php index c963532..537ae34 100644 --- a/admin/model/extension/retailcrm/history.php +++ b/admin/model/extension/retailcrm/history.php @@ -47,9 +47,14 @@ class ModelExtensionRetailcrmHistory extends Model $packs = $crm->ordersHistory(array( 'startDate' => $lastRun->format('Y-m-d H:i:s'), ), 1, 100); - if(!$packs->isSuccessful() && count($packs->history) <= 0) + $packsCustomers = $crm->customersHistory(array( + 'startDate' => $lastRun->format('Y-m-d H:i:s'), + ), 1, 100); + if(!$packs->isSuccessful() && count($packs->history) <= 0 && !$packsCustomers->isSuccessful() && count($Customers->history) <= 0) return false; + $orders = RetailcrmHistoryHelper::assemblyOrder($packs->history); + $customers = RetailcrmHistoryHelper::assemblyCustomer($packsCustomers->history); $generatedAt = $packs['generatedAt']; @@ -84,6 +89,19 @@ class ModelExtensionRetailcrmHistory extends Model unset($orders); + $updateCustomers = array(); + + foreach ($customers as $customer) { + + if (isset($customer['deleted'])) continue; + + if (isset($customer['externalId'])) { + $updateCustomers[] = $customer['id']; + } + } + + unset($customers); + if (!empty($newOrders)) { $orders = $crm->ordersList($filter = array('ids' => $newOrders)); if ($orders) { @@ -98,6 +116,13 @@ class ModelExtensionRetailcrmHistory extends Model } } + if (!empty($updateCustomers)) { + $customers = $crm->customersList($filter = array('ids' => $updateCustomers)); + if ($customers) { + $this->updateCustomers($customers['customers']); + } + } + $this->model_setting_setting->editSetting('retailcrm_history', array('retailcrm_history' => $generatedAt)); if (!empty($this->createResult['customers'])) { @@ -460,4 +485,29 @@ class ModelExtensionRetailcrmHistory extends Model return array('customers' => $customersIdsFix, 'orders' => $ordersIdsFix); } + + protected function updateCustomers($customers) + { + foreach ($customers as $customer) { + + $customer_id = $customer['externalId']; + $customerData = $this->model_customer_customer->getCustomer($customer_id); + + $customerData['firstname'] = $customer['firstName']; + $customerData['lastname'] = $customer['lastName']; + $customerData['email'] = $customer['email']; + $customerData['telephone'] = $customer['phones'][0]['number']; + + $customerAddress = $this->model_customer_customer->getAddress($customerData['address_id']); + + $customerAddress['firstname'] = $customer['firstName']; + $customerAddress['lastname'] = $customer['lastName']; + $customerAddress['address_1'] = $customer['address']['text']; + $customerAddress['city'] = $customer['address']['city']; + $customerAddress['postcode'] = $customer['address']['index']; + $customerData['address'] = array($customerAddress); + + $this->model_customer_customer->editCustomer($customer_id, $customerData); + } + } } diff --git a/system/library/retailcrm/RetailcrmHistoryHelper.php b/system/library/retailcrm/RetailcrmHistoryHelper.php index d21b327..604cf3e 100644 --- a/system/library/retailcrm/RetailcrmHistoryHelper.php +++ b/system/library/retailcrm/RetailcrmHistoryHelper.php @@ -80,6 +80,22 @@ class RetailcrmHistoryHelper { return $orders; } + public static function assemblyCustomer($customerHistory) + { + $customers = array(); + foreach ($customerHistory as $change) { + $change['order'] = self::removeEmpty($change['customer']); + + if(!empty($customers[$change['customer']['id']]) && $customers[$change['customer']['id']]) { + $customers[$change['customer']['id']] = array_merge($customers[$change['customer']['id']], $change['customer']); + } else { + $customers[$change['customer']['id']] = $change['customer']; + } + } + + return $customers; + } + public static function newValue($value) { if(isset($value['code'])) {