2017-03-22 11:52:11 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ModelExtensionRetailcrmCustomer extends Model {
|
2017-07-11 17:39:48 +03:00
|
|
|
public function sendToCrm($customer)
|
|
|
|
{
|
|
|
|
$this->initApi();
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
if(empty($customer))
|
|
|
|
return false;
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$customerToCrm = $this->process($customer);
|
|
|
|
|
|
|
|
$this->retailcrmApi->customersCreate($customerToCrm);
|
|
|
|
}
|
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
public function changeInCrm($customer)
|
|
|
|
{
|
|
|
|
$this->initApi();
|
2017-05-18 17:29:28 +03:00
|
|
|
|
|
|
|
if(empty($customer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$customerToCrm = $this->process($customer);
|
|
|
|
|
|
|
|
$this->retailcrmApi->customersEdit($customerToCrm);
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
private function process($customer) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$moduleTitle = $this->getModuleTitle();
|
|
|
|
|
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-07-11 17:39:48 +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
|
|
|
|
|
|
|
if (isset($this->settings[$moduleTitle . '_custom_field']) && $customer['custom_field']) {
|
|
|
|
$customFields = json_decode($customer['custom_field']);
|
|
|
|
|
|
|
|
foreach ($customFields as $key => $value) {
|
2017-09-21 10:09:26 +03:00
|
|
|
if (isset($this->settings[$moduleTitle . '_custom_field']['c_' . $key])) {
|
|
|
|
$customFieldsToCrm[$this->settings[$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-07-11 17:39:48 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
return $customerToCrm;
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
|
|
|
|
protected function initApi()
|
|
|
|
{
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$moduleTitle = $this->getModuleTitle();
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->settings = $this->model_setting_setting->getSetting($moduleTitle);
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
if(empty($this->settings[$moduleTitle . '_url']) || empty($this->settings[$moduleTitle . '_apikey']))
|
2017-07-11 17:39:48 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
|
|
|
|
|
|
|
|
$this->retailcrmApi = new RetailcrmProxy(
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->settings[$moduleTitle . '_url'],
|
|
|
|
$this->settings[$moduleTitle . '_apikey'],
|
2017-07-11 17:39:48 +03:00
|
|
|
DIR_SYSTEM . 'storage/logs/retailcrm.log',
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->settings[$moduleTitle . '_apiversion']
|
2017-07-11 17:39:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getModuleTitle()
|
|
|
|
{
|
|
|
|
if (version_compare(VERSION, '3.0', '<')) {
|
|
|
|
$title = 'retailcrm';
|
|
|
|
} else {
|
|
|
|
$title = 'module_retailcrm';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|