2017-03-22 11:52:11 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ModelExtensionRetailcrmCustomer extends Model {
|
2018-01-23 16:34:20 +03:00
|
|
|
protected $settings;
|
|
|
|
protected $moduleTitle;
|
|
|
|
protected $retailcrmApiClient;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
public function __construct($registry)
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
2018-01-23 16:34:20 +03:00
|
|
|
parent::__construct($registry);
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->load->library('retailcrm/retailcrm');
|
|
|
|
|
|
|
|
$this->moduleTitle = $this->retailcrm->getModuleTitle();
|
|
|
|
$this->settings = $this->model_setting_setting->getSetting($this->moduleTitle);
|
|
|
|
$this->retailcrmApiClient = $this->retailcrm->getApiClient();
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Upload customers
|
|
|
|
*
|
|
|
|
* @param array $customers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function uploadToCrm($customers)
|
|
|
|
{
|
|
|
|
if ($this->retailcrmApiClient === false || empty($customers)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
$customersToCrm = array();
|
|
|
|
|
|
|
|
foreach($customers as $customer) {
|
|
|
|
$customersToCrm[] = $this->process($customer);
|
|
|
|
}
|
|
|
|
|
|
|
|
$chunkedCustomers = array_chunk($customersToCrm, 50);
|
|
|
|
|
|
|
|
foreach($chunkedCustomers as $customersPart) {
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->retailcrmApiClient->customersUpload($customersPart);
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Edit customer
|
|
|
|
*
|
|
|
|
* @param array $customer
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-11 17:39:48 +03:00
|
|
|
public function changeInCrm($customer)
|
|
|
|
{
|
2018-01-23 16:34:20 +03:00
|
|
|
if ($this->retailcrmApiClient === false || empty($customer)) {
|
|
|
|
return;
|
2017-09-15 15:47:38 +03:00
|
|
|
}
|
2018-01-23 16:34:20 +03:00
|
|
|
|
2017-05-18 17:29:28 +03:00
|
|
|
$customerToCrm = $this->process($customer);
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
$this->retailcrmApiClient->customersEdit($customerToCrm);
|
2017-05-18 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
/**
|
|
|
|
* Process customer
|
|
|
|
*
|
|
|
|
* @param array $customer
|
|
|
|
*
|
|
|
|
* @return array $customerToCrm
|
|
|
|
*/
|
|
|
|
private function process($customer)
|
|
|
|
{
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$customerToCrm = array(
|
|
|
|
'externalId' => $customer['customer_id'],
|
|
|
|
'firstName' => $customer['firstname'],
|
|
|
|
'lastName' => $customer['lastname'],
|
|
|
|
'email' => $customer['email'],
|
|
|
|
'phones' => array(
|
|
|
|
array(
|
|
|
|
'number' => $customer['telephone']
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'createdAt' => $customer['date_added']
|
|
|
|
);
|
|
|
|
|
2017-05-18 17:29:28 +03:00
|
|
|
if (isset($customer['address'])) {
|
|
|
|
$customerToCrm['address'] = array(
|
|
|
|
'index' => $customer['address']['postcode'],
|
|
|
|
'countryIso' => $customer['address']['iso_code_2'],
|
|
|
|
'region' => $customer['address']['zone'],
|
|
|
|
'city' => $customer['address']['city'],
|
|
|
|
'text' => $customer['address']['address_1'] . ' ' . $customer['address']['address_2']
|
|
|
|
);
|
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2018-01-23 16:34:20 +03:00
|
|
|
if (isset($this->settings[$this->moduleTitle . '_custom_field']) && $customer['custom_field']) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$customFields = json_decode($customer['custom_field']);
|
|
|
|
|
|
|
|
foreach ($customFields as $key => $value) {
|
2018-01-23 16:34:20 +03:00
|
|
|
if (isset($this->settings[$this->moduleTitle . '_custom_field']['c_' . $key])) {
|
|
|
|
$customFieldsToCrm[$this->settings[$this->moduleTitle . '_custom_field']['c_' . $key]] = $value;
|
2017-09-15 15:47:38 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-21 10:09:26 +03:00
|
|
|
|
|
|
|
if (isset($customFieldsToCrm)) {
|
|
|
|
$customerToCrm['customFields'] = $customFieldsToCrm;
|
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
return $customerToCrm;
|
|
|
|
}
|
|
|
|
}
|