2014-04-30 03:51:52 +04:00
|
|
|
<?php
|
|
|
|
|
2015-10-23 17:41:05 +03:00
|
|
|
if (!defined('_PS_VERSION_')) {
|
|
|
|
exit;
|
|
|
|
}
|
2014-04-30 03:51:52 +04:00
|
|
|
|
2015-10-23 17:41:05 +03:00
|
|
|
if (
|
|
|
|
function_exists('date_default_timezone_set')
|
|
|
|
&&
|
|
|
|
function_exists('date_default_timezone_get')
|
|
|
|
) {
|
|
|
|
date_default_timezone_set(@date_default_timezone_get());
|
2015-07-21 15:21:12 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 17:41:05 +03:00
|
|
|
require 'lib/vendor/Retailcrm.php';
|
|
|
|
require 'lib/vendor/Service.php';
|
|
|
|
|
|
|
|
if (file_exists('lib/custom/References.php')) {
|
|
|
|
require 'lib/custom/References.php';
|
|
|
|
} else {
|
|
|
|
require 'lib/classes/References.php';
|
2014-04-30 03:51:52 +04:00
|
|
|
}
|
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
class RetailCRM extends Module
|
2014-04-30 03:51:52 +04:00
|
|
|
{
|
|
|
|
function __construct()
|
|
|
|
{
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->name = 'retailcrm';
|
2014-04-30 03:51:52 +04:00
|
|
|
$this->tab = 'market_place';
|
2015-10-23 17:41:05 +03:00
|
|
|
$this->version = '1.1';
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->author = 'Retail Driver LCC';
|
2014-04-30 03:51:52 +04:00
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->displayName = $this->l('RetailCRM');
|
|
|
|
$this->description = $this->l('Integration module for RetailCRM');
|
2014-04-30 03:51:52 +04:00
|
|
|
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
|
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->apiUrl = Configuration::get('RETAILCRM_ADDRESS');
|
|
|
|
$this->apiKey = Configuration::get('RETAILCRM_API_TOKEN');
|
|
|
|
|
|
|
|
if (!empty($this->apiUrl) && !empty($this->apiKey)) {
|
|
|
|
$this->api = new ApiClient(
|
|
|
|
$this->apiUrl,
|
|
|
|
$this->apiKey
|
|
|
|
);
|
|
|
|
}
|
2014-05-08 20:11:28 +04:00
|
|
|
|
2015-07-21 15:21:12 +03:00
|
|
|
$this->default_lang = (int) Configuration::get('PS_LANG_DEFAULT');
|
|
|
|
$this->default_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
|
|
|
|
$this->default_country = (int) Configuration::get('PS_COUNTRY_DEFAULT');
|
2014-06-02 18:36:24 +04:00
|
|
|
|
|
|
|
$this->response = array();
|
|
|
|
$this->customerFix = array();
|
|
|
|
$this->orderFix = array();
|
|
|
|
|
|
|
|
$this->address_id = null;
|
|
|
|
$this->customer_id = null;
|
|
|
|
|
|
|
|
$this->customer = null;
|
2014-05-13 04:49:47 +04:00
|
|
|
|
2015-07-21 15:21:12 +03:00
|
|
|
$this->ref = new References($this->api);
|
|
|
|
|
2014-04-30 03:51:52 +04:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
function install()
|
|
|
|
{
|
2014-05-08 20:11:28 +04:00
|
|
|
return (
|
|
|
|
parent::install() &&
|
|
|
|
$this->registerHook('newOrder') &&
|
2014-05-13 04:49:47 +04:00
|
|
|
$this->registerHook('actionOrderStatusPostUpdate') &&
|
2015-10-26 16:14:34 +03:00
|
|
|
$this->registerHook('actionPaymentConfirmation') &&
|
|
|
|
$this->registerHook('actionCustomerAccountAdd')
|
2014-05-08 20:11:28 +04:00
|
|
|
);
|
2014-04-30 03:51:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function uninstall()
|
|
|
|
{
|
|
|
|
return parent::uninstall() &&
|
2015-07-20 17:59:27 +03:00
|
|
|
Configuration::deleteByName('RETAILCRM_ADDRESS') &&
|
|
|
|
Configuration::deleteByName('RETAILCRM_API_TOKEN') &&
|
|
|
|
Configuration::deleteByName('RETAILCRM_API_STATUS') &&
|
|
|
|
Configuration::deleteByName('RETAILCRM_API_DELIVERY') &&
|
|
|
|
Configuration::deleteByName('RETAILCRM_LAST_SYNC') &&
|
|
|
|
Configuration::deleteByName('RETAILCRM_API_ADDR')
|
2014-04-30 03:51:52 +04:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getContent()
|
|
|
|
{
|
|
|
|
$output = null;
|
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$address = Configuration::get('RETAILCRM_ADDRESS');
|
|
|
|
$token = Configuration::get('RETAILCRM_API_TOKEN');
|
2014-04-30 03:51:52 +04:00
|
|
|
|
|
|
|
if (!$address || $address == '') {
|
2015-07-21 15:21:12 +03:00
|
|
|
$output .= $this->displayError( $this->l('Invalid or empty crm address') );
|
2014-04-30 03:51:52 +04:00
|
|
|
} elseif (!$token || $token == '') {
|
2015-07-21 15:21:12 +03:00
|
|
|
$output .= $this->displayError( $this->l('Invalid or empty crm api token') );
|
2014-06-02 18:36:24 +04:00
|
|
|
} else {
|
|
|
|
$output .= $this->displayConfirmation(
|
|
|
|
$this->l('Timezone settings must be identical to both of your crm and shop') .
|
|
|
|
" <a target=\"_blank\" href=\"$address/admin/settings#t-main\">$address/admin/settings#t-main</a>"
|
|
|
|
);
|
2014-04-30 03:51:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Tools::isSubmit('submit'.$this->name))
|
|
|
|
{
|
2015-07-20 17:59:27 +03:00
|
|
|
$address = strval(Tools::getValue('RETAILCRM_ADDRESS'));
|
|
|
|
$token = strval(Tools::getValue('RETAILCRM_API_TOKEN'));
|
|
|
|
$delivery = json_encode(Tools::getValue('RETAILCRM_API_DELIVERY'));
|
|
|
|
$status = json_encode(Tools::getValue('RETAILCRM_API_STATUS'));
|
|
|
|
$payment = json_encode(Tools::getValue('RETAILCRM_API_PAYMENT'));
|
|
|
|
$order_address = json_encode(Tools::getValue('RETAILCRM_API_ADDR'));
|
2014-04-30 03:51:52 +04:00
|
|
|
|
|
|
|
if (!$address || empty($address) || !Validate::isGenericName($address)) {
|
|
|
|
$output .= $this->displayError( $this->l('Invalid crm address') );
|
|
|
|
} elseif (!$token || empty($token) || !Validate::isGenericName($token)) {
|
|
|
|
$output .= $this->displayError( $this->l('Invalid crm api token') );
|
|
|
|
} else {
|
2015-07-20 17:59:27 +03:00
|
|
|
Configuration::updateValue('RETAILCRM_ADDRESS', $address);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_TOKEN', $token);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_DELIVERY', $delivery);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_STATUS', $status);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_PAYMENT', $payment);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_ADDR', $order_address);
|
2014-04-30 03:51:52 +04:00
|
|
|
$output .= $this->displayConfirmation($this->l('Settings updated'));
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 17:41:05 +03:00
|
|
|
|
|
|
|
$this->display(__FILE__, 'retailcrm.tpl');
|
2014-04-30 03:51:52 +04:00
|
|
|
|
|
|
|
return $output.$this->displayForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function displayForm()
|
|
|
|
{
|
2014-05-13 04:49:47 +04:00
|
|
|
|
2014-04-30 03:51:52 +04:00
|
|
|
$this->displayConfirmation($this->l('Settings updated'));
|
|
|
|
|
2014-05-13 04:49:47 +04:00
|
|
|
$default_lang = $this->default_lang;
|
2015-07-20 17:59:27 +03:00
|
|
|
$intaroCrm = $this->api;
|
2014-04-30 03:51:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Network connection form
|
|
|
|
*/
|
|
|
|
$fields_form[0]['form'] = array(
|
|
|
|
'legend' => array(
|
|
|
|
'title' => $this->l('Network connection'),
|
|
|
|
),
|
|
|
|
'input' => array(
|
|
|
|
array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label' => $this->l('CRM address'),
|
2015-07-20 17:59:27 +03:00
|
|
|
'name' => 'RETAILCRM_ADDRESS',
|
2014-04-30 03:51:52 +04:00
|
|
|
'size' => 20,
|
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label' => $this->l('CRM token'),
|
2015-07-20 17:59:27 +03:00
|
|
|
'name' => 'RETAILCRM_API_TOKEN',
|
2014-04-30 03:51:52 +04:00
|
|
|
'size' => 20,
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'submit' => array(
|
|
|
|
'title' => $this->l('Save'),
|
|
|
|
'class' => 'button'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-05-08 20:11:28 +04:00
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
if (!empty($this->apiUrl) && !empty($this->apiKey)) {
|
|
|
|
/*
|
|
|
|
* Delivery
|
|
|
|
*/
|
|
|
|
$fields_form[1]['form'] = array(
|
|
|
|
'legend' => array(
|
|
|
|
'title' => $this->l('Delivery'),
|
|
|
|
),
|
2015-07-21 15:21:12 +03:00
|
|
|
'input' => $this->ref->getDeliveryTypes(),
|
2015-07-20 17:59:27 +03:00
|
|
|
);
|
2014-04-30 03:51:52 +04:00
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
/*
|
|
|
|
* Order status
|
|
|
|
*/
|
|
|
|
$fields_form[2]['form'] = array(
|
|
|
|
'legend' => array(
|
|
|
|
'title' => $this->l('Order statuses'),
|
|
|
|
),
|
2015-07-21 15:21:12 +03:00
|
|
|
'input' => $this->ref->getStatuses(),
|
2015-07-20 17:59:27 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Payment
|
|
|
|
*/
|
|
|
|
$fields_form[3]['form'] = array(
|
|
|
|
'legend' => array(
|
|
|
|
'title' => $this->l('Payment types'),
|
|
|
|
),
|
2015-07-21 15:21:12 +03:00
|
|
|
'input' => $this->ref->getPaymentTypes(),
|
2015-07-20 17:59:27 +03:00
|
|
|
);
|
|
|
|
}
|
2014-05-08 20:11:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Address fields
|
|
|
|
*/
|
|
|
|
$fields_form[4]['form'] = array(
|
|
|
|
'legend' => array(
|
|
|
|
'title' => $this->l('Address'),
|
|
|
|
),
|
|
|
|
'input' => $this->getAddressFields()
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Diplay forms
|
|
|
|
*/
|
|
|
|
|
|
|
|
$helper = new HelperForm();
|
|
|
|
|
|
|
|
$helper->module = $this;
|
|
|
|
$helper->name_controller = $this->name;
|
|
|
|
$helper->token = Tools::getAdminTokenLite('AdminModules');
|
|
|
|
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
|
|
|
|
|
|
|
|
$helper->default_form_language = $default_lang;
|
|
|
|
$helper->allow_employee_form_lang = $default_lang;
|
|
|
|
|
|
|
|
$helper->title = $this->displayName;
|
|
|
|
$helper->show_toolbar = true;
|
|
|
|
$helper->toolbar_scroll = true;
|
|
|
|
$helper->submit_action = 'submit'.$this->name;
|
|
|
|
$helper->toolbar_btn = array(
|
|
|
|
'save' =>
|
|
|
|
array(
|
|
|
|
'desc' => $this->l('Save'),
|
|
|
|
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
|
|
|
|
'&token='.Tools::getAdminTokenLite('AdminModules'),
|
|
|
|
),
|
|
|
|
'back' => array(
|
|
|
|
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
|
|
|
|
'desc' => $this->l('Back to list')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$helper->fields_value['RETAILCRM_ADDRESS'] = Configuration::get('RETAILCRM_ADDRESS');
|
|
|
|
$helper->fields_value['RETAILCRM_API_TOKEN'] = Configuration::get('RETAILCRM_API_TOKEN');
|
2014-05-08 20:11:28 +04:00
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$deliverySettings = Configuration::get('RETAILCRM_API_DELIVERY');
|
2014-05-08 20:11:28 +04:00
|
|
|
if (isset($deliverySettings) && $deliverySettings != '')
|
|
|
|
{
|
|
|
|
$deliveryTypes = json_decode($deliverySettings);
|
|
|
|
foreach ($deliveryTypes as $idx => $delivery) {
|
2015-07-20 17:59:27 +03:00
|
|
|
$name = 'RETAILCRM_API_DELIVERY[' . $idx . ']';
|
2014-05-08 20:11:28 +04:00
|
|
|
$helper->fields_value[$name] = $delivery;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$statusSettings = Configuration::get('RETAILCRM_API_STATUS');
|
2014-05-08 20:11:28 +04:00
|
|
|
if (isset($statusSettings) && $statusSettings != '')
|
|
|
|
{
|
|
|
|
$statusTypes = json_decode($statusSettings);
|
|
|
|
foreach ($statusTypes as $idx => $status) {
|
2015-07-20 17:59:27 +03:00
|
|
|
$name = 'RETAILCRM_API_STATUS[' . $idx . ']';
|
2014-05-08 20:11:28 +04:00
|
|
|
$helper->fields_value[$name] = $status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$paymentSettings = Configuration::get('RETAILCRM_API_PAYMENT');
|
2014-05-08 20:11:28 +04:00
|
|
|
if (isset($paymentSettings) && $paymentSettings != '')
|
|
|
|
{
|
|
|
|
$paymentTypes = json_decode($paymentSettings);
|
|
|
|
foreach ($paymentTypes as $idx => $payment) {
|
2015-07-20 17:59:27 +03:00
|
|
|
$name = 'RETAILCRM_API_PAYMENT[' . $idx . ']';
|
2014-05-08 20:11:28 +04:00
|
|
|
$helper->fields_value[$name] = $payment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$addressSettings = Configuration::get('RETAILCRM_API_ADDR');
|
2014-05-08 20:11:28 +04:00
|
|
|
if (isset($addressSettings) && $addressSettings != '')
|
|
|
|
{
|
|
|
|
$addressTypes = json_decode($addressSettings);
|
|
|
|
foreach ($addressTypes as $idx => $address) {
|
2015-07-20 17:59:27 +03:00
|
|
|
$name = 'RETAILCRM_API_ADDR[' . $idx . ']';
|
2014-05-08 20:11:28 +04:00
|
|
|
$helper->fields_value[$name] = $address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $helper->generateForm($fields_form);
|
|
|
|
}
|
|
|
|
|
2015-10-23 17:41:05 +03:00
|
|
|
public function getAddressFields()
|
|
|
|
{
|
|
|
|
$addressFields = array();
|
|
|
|
$address = explode(' ', str_replace("\n", ' ', AddressFormat::getAddressCountryFormat($this->context->country->id)));
|
|
|
|
|
|
|
|
if (!empty($address)) {
|
|
|
|
foreach ($address as $idx => $a) {
|
2015-10-27 17:31:53 +03:00
|
|
|
if (!in_array($a, array('vat_number', 'phone_mobile', 'company'))) {
|
|
|
|
if (!strpos($a, ':')) {
|
|
|
|
$a = preg_replace('/_/', ' ', $a);
|
|
|
|
$a = preg_replace('/[\,\.]/', '', $a);
|
|
|
|
$addressFields[] = array(
|
|
|
|
'type' => 'select',
|
|
|
|
'label' => $this->l((string) ucfirst($a)),
|
|
|
|
'name' => 'RETAILCRM_API_ADDR[' . $idx . ']',
|
|
|
|
'required' => false,
|
|
|
|
'options' => array(
|
|
|
|
'query' => array(
|
|
|
|
array(
|
|
|
|
'name' => '',
|
|
|
|
'id_option' => ''
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('FIRST_NAME'),
|
|
|
|
'id_option' => 'first_name'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('LAST_NAME'),
|
|
|
|
'id_option' => 'last_name'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('PHONE'),
|
|
|
|
'id_option' => 'phone'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('EMAIL'),
|
|
|
|
'id_option' => 'email'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('ADDRESS'),
|
|
|
|
'id_option' => 'address'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('COUNTRY'),
|
|
|
|
'id_option' => 'country'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('REGION'),
|
|
|
|
'id_option' => 'region'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('CITY'),
|
|
|
|
'id_option' => 'city'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('ZIP'),
|
|
|
|
'id_option' => 'index'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('STREET'),
|
|
|
|
'id_option' => 'street'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('BUILDING'),
|
|
|
|
'id_option' => 'building'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('FLAT'),
|
|
|
|
'id_option' => 'flat'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('INTERCOMCODE'),
|
|
|
|
'id_option' => 'intercomcode'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('FLOOR'),
|
|
|
|
'id_option' => 'floor'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('BLOCK'),
|
|
|
|
'id_option' => 'block'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => $this->l('HOUSE'),
|
|
|
|
'ID' => 'house'
|
|
|
|
)
|
2015-10-23 17:41:05 +03:00
|
|
|
),
|
2015-10-27 17:31:53 +03:00
|
|
|
'id' => 'id_option',
|
|
|
|
'name' => 'name'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-10-23 17:41:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $addressFields;
|
|
|
|
}
|
|
|
|
|
2015-10-26 16:14:34 +03:00
|
|
|
public function hookActionCustomerAccountAdd($params)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->api->customersCreate(
|
|
|
|
array(
|
|
|
|
'externalId' => $params['newCustomer']->id,
|
|
|
|
'firstName' => $params['newCustomer']->firstname,
|
|
|
|
'lastName' => $params['newCustomer']->lastname,
|
|
|
|
'email' => $params['newCustomer']->email,
|
|
|
|
'createdAt' => $params['newCustomer']->date_add
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch (CurlException $e) {
|
|
|
|
error_log('customerCreate: connection error', 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
|
|
|
}
|
|
|
|
catch (InvalidJsonException $e) {
|
|
|
|
error_log('customerCreate: ' . $e->getMessage(), 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 20:11:28 +04:00
|
|
|
public function hookNewOrder($params)
|
|
|
|
{
|
|
|
|
return $this->hookActionOrderStatusPostUpdate($params);
|
|
|
|
}
|
|
|
|
|
2014-05-13 04:49:47 +04:00
|
|
|
public function hookActionPaymentConfirmation($params)
|
|
|
|
{
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->api->ordersEdit(
|
2014-05-13 04:49:47 +04:00
|
|
|
array(
|
|
|
|
'externalId' => $params['id_order'],
|
|
|
|
'paymentStatus' => 'paid',
|
|
|
|
'createdAt' => $params['cart']->date_upd
|
|
|
|
)
|
|
|
|
);
|
2015-07-20 17:59:27 +03:00
|
|
|
|
2014-05-13 04:49:47 +04:00
|
|
|
return $this->hookActionOrderStatusPostUpdate($params);
|
|
|
|
}
|
|
|
|
|
2014-05-08 20:11:28 +04:00
|
|
|
public function hookActionOrderStatusPostUpdate($params)
|
|
|
|
{
|
|
|
|
$address_id = Address::getFirstCustomerAddressId($params['cart']->id_customer);
|
2015-07-21 15:21:12 +03:00
|
|
|
$sql = 'SELECT * FROM '._DB_PREFIX_.'address WHERE id_address='.(int) $address_id;
|
2014-05-08 20:11:28 +04:00
|
|
|
$address = Db::getInstance()->ExecuteS($sql);
|
|
|
|
$address = $address[0];
|
2015-07-20 17:59:27 +03:00
|
|
|
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'));
|
|
|
|
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'));
|
2014-05-08 20:11:28 +04:00
|
|
|
$inCart = $params['cart']->getProducts();
|
|
|
|
|
|
|
|
if (isset($params['orderStatus'])) {
|
|
|
|
try {
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->api->customersCreate(
|
2014-05-08 20:11:28 +04:00
|
|
|
array(
|
|
|
|
'externalId' => $params['cart']->id_customer,
|
|
|
|
'lastName' => $params['customer']->lastname,
|
|
|
|
'firstName' => $params['customer']->firstname,
|
|
|
|
'email' => $params['customer']->email,
|
|
|
|
'phones' => array(
|
|
|
|
array(
|
|
|
|
'number' => $address['phone'],
|
|
|
|
'type' => 'mobile'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'number' => $address['phone_mobile'],
|
|
|
|
'type' => 'mobile'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'createdAt' => $params['customer']->date_add
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-07-20 17:59:27 +03:00
|
|
|
catch (CurlException $e) {
|
2015-10-23 17:41:05 +03:00
|
|
|
error_log("customerCreate: connection error", 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
2014-05-08 20:11:28 +04:00
|
|
|
}
|
2015-07-20 17:59:27 +03:00
|
|
|
catch (InvalidJsonException $e) {
|
2015-10-23 17:41:05 +03:00
|
|
|
error_log('customerCreate: ' . $e->getMessage(), 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
2014-05-08 20:11:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$items = array();
|
|
|
|
foreach ($inCart as $item) {
|
|
|
|
$items[] = array(
|
2015-10-26 16:14:34 +03:00
|
|
|
'initialPrice' => (!empty($item['rate'])) ? $item['price'] + ($item['price'] * $item['rate'] / 100) : $item['price'],
|
2014-05-08 20:11:28 +04:00
|
|
|
'quantity' => $item['quantity'],
|
|
|
|
'productId' => $item['id_product'],
|
|
|
|
'productName' => $item['name'],
|
|
|
|
'createdAt' => $item['date_add']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dTypeKey = $params['cart']->id_carrier;
|
2015-10-26 16:14:34 +03:00
|
|
|
|
2014-05-14 16:50:31 +04:00
|
|
|
if (Module::getInstanceByName('advancedcheckout') === false) {
|
|
|
|
$pTypeKey = $params['order']->module;
|
|
|
|
} else {
|
|
|
|
$pTypeKey = $params['order']->payment;
|
|
|
|
}
|
2015-10-26 16:14:34 +03:00
|
|
|
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->api->ordersCreate(
|
2014-05-08 20:11:28 +04:00
|
|
|
array(
|
|
|
|
'externalId' => $params['order']->id,
|
|
|
|
'orderType' => 'eshop-individual',
|
|
|
|
'orderMethod' => 'shopping-cart',
|
2015-10-26 16:14:34 +03:00
|
|
|
'status' => 'new',
|
2014-05-08 20:11:28 +04:00
|
|
|
'customerId' => $params['cart']->id_customer,
|
|
|
|
'firstName' => $params['customer']->firstname,
|
|
|
|
'lastName' => $params['customer']->lastname,
|
|
|
|
'phone' => $address['phone'],
|
|
|
|
'email' => $params['customer']->email,
|
2014-05-14 16:50:31 +04:00
|
|
|
'paymentType' => $payment->$pTypeKey,
|
2015-10-26 16:14:34 +03:00
|
|
|
'delivery' => array(
|
|
|
|
'code' => $delivery->$dTypeKey,
|
|
|
|
'cost' => $params['order']->total_shipping,
|
|
|
|
'address' => array(
|
|
|
|
'city' => $address['city'],
|
|
|
|
'index' => $address['postcode'],
|
|
|
|
'text' => $address['address1'],
|
|
|
|
)
|
2014-05-08 20:11:28 +04:00
|
|
|
),
|
|
|
|
'discount' => $params['order']->total_discounts,
|
|
|
|
'items' => $items,
|
|
|
|
'createdAt' => $params['order']->date_add
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-07-20 17:59:27 +03:00
|
|
|
catch (CurlException $e) {
|
2015-10-23 17:41:05 +03:00
|
|
|
error_log('orderCreate: connection error', 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
2014-05-08 20:11:28 +04:00
|
|
|
}
|
2015-07-20 17:59:27 +03:00
|
|
|
catch (InvalidJsonException $e) {
|
2015-10-23 17:41:05 +03:00
|
|
|
error_log('orderCreate: ' . $e->getMessage(), 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
2014-05-08 20:11:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:53:20 +03:00
|
|
|
if (!empty($params['newOrderStatus'])) {
|
2014-05-13 04:49:47 +04:00
|
|
|
$statuses = OrderState::getOrderStates($this->default_lang);
|
2015-07-20 17:59:27 +03:00
|
|
|
$aStatuses = json_decode(Configuration::get('RETAILCRM_API_STATUS'));
|
2014-05-13 04:49:47 +04:00
|
|
|
foreach ($statuses as $status) {
|
|
|
|
if ($status['name'] == $params['newOrderStatus']->name) {
|
|
|
|
$currStatus = $status['id_order_state'];
|
|
|
|
try {
|
2015-07-20 17:59:27 +03:00
|
|
|
$this->api->ordersEdit(
|
2014-05-13 04:49:47 +04:00
|
|
|
array(
|
|
|
|
'externalId' => $params['id_order'],
|
2015-10-26 17:53:20 +03:00
|
|
|
'status' => $aStatuses->$currStatus
|
2014-05-13 04:49:47 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-07-20 17:59:27 +03:00
|
|
|
catch (CurlException $e) {
|
2015-10-23 17:41:05 +03:00
|
|
|
error_log('orderStatusUpdate: connection error', 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
2014-06-02 18:36:24 +04:00
|
|
|
}
|
2015-07-20 17:59:27 +03:00
|
|
|
catch (InvalidJsonException $e) {
|
2015-10-23 17:41:05 +03:00
|
|
|
error_log('orderStatusUpdate: ' . $e->getMessage(), 3, _PS_ROOT_DIR_ . "log/retailcrm.log");
|
2014-06-02 18:36:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 15:21:12 +03:00
|
|
|
}
|
2014-04-30 03:51:52 +04:00
|
|
|
}
|