Update a customer's from history (#28)

This commit is contained in:
iyzoer 2017-05-18 14:11:12 +04:00 committed by Alex Lushpai
parent 00f273ff3c
commit 80c96f4981
3 changed files with 75 additions and 4 deletions

View File

@ -44,14 +44,19 @@ class ModelExtensionRetailcrmHistory extends Model
? new DateTime($history['retailcrm_history']) ? new DateTime($history['retailcrm_history'])
: new DateTime(date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))))); : new DateTime(date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s')))));
$packs = $crm->ordersHistory(array( $packsOrders = $crm->ordersHistory(array(
'startDate' => $lastRun->format('Y-m-d H:i:s'), 'startDate' => $lastRun->format('Y-m-d H:i:s'),
), 1, 100); ), 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(!$packsOrders->isSuccessful() && count($packsOrders->history) <= 0 && !$packsCustomers->isSuccessful() && count($Customers->history) <= 0)
return false; return false;
$orders = RetailcrmHistoryHelper::assemblyOrder($packs->history);
$generatedAt = $packs['generatedAt']; $orders = RetailcrmHistoryHelper::assemblyOrder($packsOrders->history);
$customers = RetailcrmHistoryHelper::assemblyCustomer($packsCustomers->history);
$generatedAt = $packsOrders['generatedAt'];
$this->subtotalSettings = $this->model_setting_setting->getSetting('sub_total'); $this->subtotalSettings = $this->model_setting_setting->getSetting('sub_total');
$this->totalSettings = $this->model_setting_setting->getSetting('total'); $this->totalSettings = $this->model_setting_setting->getSetting('total');
@ -84,6 +89,19 @@ class ModelExtensionRetailcrmHistory extends Model
unset($orders); 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)) { if (!empty($newOrders)) {
$orders = $crm->ordersList($filter = array('ids' => $newOrders)); $orders = $crm->ordersList($filter = array('ids' => $newOrders));
if ($orders) { 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)); $this->model_setting_setting->editSetting('retailcrm_history', array('retailcrm_history' => $generatedAt));
if (!empty($this->createResult['customers'])) { if (!empty($this->createResult['customers'])) {
@ -460,4 +485,29 @@ class ModelExtensionRetailcrmHistory extends Model
return array('customers' => $customersIdsFix, 'orders' => $ordersIdsFix); 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);
}
}
} }

View File

@ -171,6 +171,7 @@ class ModelExtensionRetailcrmOrder extends Model {
$this->load->model('setting/setting'); $this->load->model('setting/setting');
$settings = $this->model_setting_setting->getSetting('retailcrm'); $settings = $this->model_setting_setting->getSetting('retailcrm');
$settingPaid = $this->model_setting_setting->getSetting($order_data['payment_code']);
if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) { if(!empty($settings['retailcrm_url']) && !empty($settings['retailcrm_apikey'])) {
$this->load->model('catalog/product'); $this->load->model('catalog/product');
@ -296,6 +297,10 @@ class ModelExtensionRetailcrmOrder extends Model {
$order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']];
} }
if ($order_data['order_status_id'] == $settingPaid[$order_data['payment_code'] . '_order_status_id']) {
$order['paymentStatus'] = 'paid';
}
$this->retailcrm->ordersEdit($order); $this->retailcrm->ordersEdit($order);
} }
} }

View File

@ -80,6 +80,22 @@ class RetailcrmHistoryHelper {
return $orders; 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) public static function newValue($value)
{ {
if(isset($value['code'])) { if(isset($value['code'])) {