2017-03-22 11:52:11 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ModelExtensionRetailcrmCustomer extends Model {
|
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
public function uploadToCrm($customers)
|
|
|
|
{
|
|
|
|
$this->initApi();
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
if(empty($customers))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$customersToCrm = array();
|
|
|
|
|
|
|
|
foreach($customers as $customer) {
|
|
|
|
$customersToCrm[] = $this->process($customer);
|
|
|
|
}
|
|
|
|
|
|
|
|
$chunkedCustomers = array_chunk($customersToCrm, 50);
|
|
|
|
|
|
|
|
foreach($chunkedCustomers as $customersPart) {
|
|
|
|
$this->retailcrmApi->customersUpload($customersPart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2017-07-11 17:39:48 +03:00
|
|
|
|
2017-05-18 17:29:28 +03:00
|
|
|
$customerToCrm = $this->process($customer);
|
|
|
|
|
|
|
|
$this->retailcrmApi->customersEdit($customerToCrm);
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
private function process($customer) {
|
|
|
|
$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-03-22 11:52:11 +03:00
|
|
|
return $customerToCrm;
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
|
|
|
|
protected function initApi()
|
|
|
|
{
|
|
|
|
$moduleTitle = $this->getModuleTitle();
|
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$settings = $this->model_setting_setting->getSetting($moduleTitle);
|
|
|
|
|
|
|
|
if(empty($settings[$moduleTitle . '_url']) || empty($settings[$moduleTitle . '_apikey']))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
require_once DIR_SYSTEM . 'library/retailcrm/bootstrap.php';
|
|
|
|
|
|
|
|
$this->retailcrmApi = new RetailcrmProxy(
|
|
|
|
$settings[$moduleTitle . '_url'],
|
|
|
|
$settings[$moduleTitle . '_apikey'],
|
|
|
|
DIR_SYSTEM . 'storage/logs/retailcrm.log',
|
|
|
|
$settings[$moduleTitle . '_apiversion']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|