2014-04-30 03:51:52 +04:00
|
|
|
<?php
|
2017-09-01 10:14:13 +02:00
|
|
|
/**
|
|
|
|
* @author Retail Driver LCC
|
|
|
|
* @copyright RetailCRM
|
|
|
|
* @license GPL
|
2019-02-13 10:16:49 +03:00
|
|
|
* @version 2.2.11
|
2017-09-01 10:14:13 +02:00
|
|
|
* @link https://retailcrm.ru
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
|
2015-10-23 17:41:05 +03:00
|
|
|
date_default_timezone_set(@date_default_timezone_get());
|
2015-07-21 15:21:12 +03:00
|
|
|
}
|
|
|
|
|
2016-01-11 18:02:20 +03:00
|
|
|
if (!defined('_PS_VERSION_')) {
|
|
|
|
exit;
|
2014-04-30 03:51:52 +04:00
|
|
|
}
|
2016-01-11 18:02:20 +03:00
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
require_once(dirname(__FILE__) . '/bootstrap.php');
|
|
|
|
|
|
|
|
class RetailCRM extends Module
|
|
|
|
{
|
2018-05-28 17:09:31 +03:00
|
|
|
public $api = false;
|
2018-05-29 13:15:18 +03:00
|
|
|
public $default_lang;
|
|
|
|
public $default_currency;
|
|
|
|
public $default_country;
|
|
|
|
public $apiUrl;
|
|
|
|
public $apiKey;
|
|
|
|
public $apiVersion;
|
|
|
|
public $psVersion;
|
|
|
|
public $log;
|
|
|
|
public $confirmUninstall;
|
|
|
|
public $reference;
|
2019-10-23 17:52:23 +03:00
|
|
|
public $assetsBase;
|
2018-05-28 17:09:31 +03:00
|
|
|
|
|
|
|
private $use_new_hooks = true;
|
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
public function __construct()
|
2016-01-12 23:06:05 +03:00
|
|
|
{
|
|
|
|
$this->name = 'retailcrm';
|
|
|
|
$this->tab = 'export';
|
2020-04-14 11:55:06 +03:00
|
|
|
$this->version = '2.5.1';
|
2016-01-12 23:06:05 +03:00
|
|
|
$this->author = 'Retail Driver LCC';
|
|
|
|
$this->displayName = $this->l('RetailCRM');
|
|
|
|
$this->description = $this->l('Integration module for RetailCRM');
|
|
|
|
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
|
|
|
|
$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');
|
|
|
|
$this->apiUrl = Configuration::get('RETAILCRM_ADDRESS');
|
|
|
|
$this->apiKey = Configuration::get('RETAILCRM_API_TOKEN');
|
2017-08-14 12:06:10 +02:00
|
|
|
$this->apiVersion = Configuration::get('RETAILCRM_API_VERSION');
|
2016-01-12 23:06:05 +03:00
|
|
|
$this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_);
|
2017-09-01 10:14:13 +02:00
|
|
|
$this->psVersion = Tools::substr(_PS_VERSION_, 0, 3);
|
2017-08-14 12:06:10 +02:00
|
|
|
$this->log = _PS_ROOT_DIR_ . '/retailcrm.log';
|
2017-09-01 10:14:13 +02:00
|
|
|
$this->module_key = '149c765c6cddcf35e1f13ea6c71e9fa5';
|
2019-10-23 17:52:23 +03:00
|
|
|
$this->assetsBase =
|
|
|
|
Tools::getShopDomainSsl(true, true) .
|
|
|
|
__PS_BASE_URI__ .
|
|
|
|
'modules/' .
|
|
|
|
$this->name .
|
|
|
|
'/public';
|
2018-01-12 12:51:46 +03:00
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
if ($this->psVersion == '1.6') {
|
2016-01-12 23:06:05 +03:00
|
|
|
$this->bootstrap = true;
|
2017-05-05 17:31:51 +04:00
|
|
|
$this->use_new_hooks = false;
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
2017-08-14 12:06:10 +02:00
|
|
|
if ($this->apiUrl && $this->apiKey) {
|
|
|
|
$this->api = new RetailcrmProxy($this->apiUrl, $this->apiKey, $this->log, $this->apiVersion);
|
2016-01-12 23:06:05 +03:00
|
|
|
$this->reference = new RetailcrmReferences($this->api);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
public function install()
|
2016-01-12 23:06:05 +03:00
|
|
|
{
|
|
|
|
return (
|
|
|
|
parent::install() &&
|
|
|
|
$this->registerHook('newOrder') &&
|
|
|
|
$this->registerHook('actionOrderStatusPostUpdate') &&
|
|
|
|
$this->registerHook('actionPaymentConfirmation') &&
|
2017-05-05 17:31:51 +04:00
|
|
|
$this->registerHook('actionCustomerAccountAdd') &&
|
|
|
|
$this->registerHook('actionOrderEdited') &&
|
2019-01-18 17:04:32 +03:00
|
|
|
$this->registerHook('header') &&
|
2018-10-24 16:15:28 +03:00
|
|
|
($this->use_new_hooks ? $this->registerHook('actionCustomerAccountUpdate') : true) &&
|
|
|
|
($this->use_new_hooks ? $this->registerHook('actionValidateCustomerAddressForm') : true)
|
2016-01-12 23:06:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:04:32 +03:00
|
|
|
public function hookHeader()
|
|
|
|
{
|
2019-10-23 17:52:23 +03:00
|
|
|
$this->context->controller->addJS($this->assetsBase . '/js/exec-jobs.js');
|
|
|
|
|
2019-01-18 17:04:32 +03:00
|
|
|
if (Configuration::get('RETAILCRM_DAEMON_COLLECTOR_ACTIVE')
|
|
|
|
&& Configuration::get('RETAILCRM_DAEMON_COLLECTOR_KEY')
|
|
|
|
) {
|
|
|
|
$collector = new RetailcrmDaemonCollector(
|
|
|
|
$this->context->customer,
|
|
|
|
Configuration::get('RETAILCRM_DAEMON_COLLECTOR_KEY')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $collector->buildScript()->getJs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
public function uninstall()
|
2016-01-12 23:06:05 +03:00
|
|
|
{
|
2018-09-03 16:15:30 +03:00
|
|
|
$api = new RetailcrmProxy(
|
|
|
|
Configuration::get('RETAILCRM_ADDRESS'),
|
|
|
|
Configuration::get('RETAILCRM_API_TOKEN'),
|
|
|
|
_PS_ROOT_DIR_ . '/retailcrm.log',
|
|
|
|
Configuration::get('RETAILCRM_API_VERSION')
|
|
|
|
);
|
|
|
|
|
|
|
|
$clientId = Configuration::get('RETAILCRM_CLIENT_ID');
|
|
|
|
$this->integrationModule($api, $clientId, Configuration::get('RETAILCRM_API_VERSION'), false);
|
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
return parent::uninstall() &&
|
2019-06-18 11:54:05 +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_VERSION') &&
|
|
|
|
Configuration::deleteByName('RETAILCRM_LAST_CUSTOMERS_SYNC') &&
|
2019-10-23 17:52:23 +03:00
|
|
|
Configuration::deleteByName('RETAILCRM_API_SYNCHRONIZE_CARTS') &&
|
|
|
|
Configuration::deleteByName('RETAILCRM_API_SYNCHRONIZED_CART_STATUS') &&
|
2019-10-29 09:42:34 +03:00
|
|
|
Configuration::deleteByName('RETAILCRM_API_SYNCHRONIZED_CART_DELAY') &&
|
2019-06-18 11:54:05 +03:00
|
|
|
Configuration::deleteByName('RETAILCRM_LAST_ORDERS_SYNC');
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getContent()
|
2017-09-01 10:10:46 +03:00
|
|
|
{
|
2016-01-12 23:06:05 +03:00
|
|
|
$output = null;
|
|
|
|
$address = Configuration::get('RETAILCRM_ADDRESS');
|
|
|
|
$token = Configuration::get('RETAILCRM_API_TOKEN');
|
2017-08-14 12:06:10 +02:00
|
|
|
$version = Configuration::get('RETAILCRM_API_VERSION');
|
2016-01-12 23:06:05 +03:00
|
|
|
|
|
|
|
if (Tools::isSubmit('submit' . $this->name)) {
|
2019-05-28 11:37:29 +03:00
|
|
|
$ordersIds = (string)(Tools::getValue('RETAILCRM_UPLOAD_ORDERS_ID'));
|
2016-01-12 23:06:05 +03:00
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
if (!empty($ordersIds)) {
|
|
|
|
$output .= $this->uploadOrders(static::partitionId($ordersIds));
|
|
|
|
} else {
|
|
|
|
$address = (string)(Tools::getValue('RETAILCRM_ADDRESS'));
|
|
|
|
$token = (string)(Tools::getValue('RETAILCRM_API_TOKEN'));
|
|
|
|
$version = (string)(Tools::getValue('RETAILCRM_API_VERSION'));
|
|
|
|
$delivery = json_encode(Tools::getValue('RETAILCRM_API_DELIVERY'));
|
|
|
|
$status = json_encode(Tools::getValue('RETAILCRM_API_STATUS'));
|
|
|
|
$payment = json_encode(Tools::getValue('RETAILCRM_API_PAYMENT'));
|
|
|
|
$deliveryDefault = json_encode(Tools::getValue('RETAILCRM_API_DELIVERY_DEFAULT'));
|
|
|
|
$paymentDefault = json_encode(Tools::getValue('RETAILCRM_API_PAYMENT_DEFAULT'));
|
|
|
|
$statusExport = (string)(Tools::getValue('RETAILCRM_STATUS_EXPORT'));
|
|
|
|
$collectorActive = (Tools::getValue('RETAILCRM_DAEMON_COLLECTOR_ACTIVE_1'));
|
|
|
|
$collectorKey = (string)(Tools::getValue('RETAILCRM_DAEMON_COLLECTOR_KEY'));
|
|
|
|
$clientId = Configuration::get('RETAILCRM_CLIENT_ID');
|
2019-10-23 17:52:23 +03:00
|
|
|
$synchronizeCartsActive = (Tools::getValue('RETAILCRM_API_SYNCHRONIZE_CARTS_1'));
|
|
|
|
$synchronizedCartStatus = (string)(Tools::getValue('RETAILCRM_API_SYNCHRONIZED_CART_STATUS'));
|
2019-10-29 09:42:34 +03:00
|
|
|
$synchronizedCartDelay = (string)(Tools::getValue('RETAILCRM_API_SYNCHRONIZED_CART_DELAY'));
|
2019-05-28 11:37:29 +03:00
|
|
|
|
|
|
|
$settings = array(
|
|
|
|
'address' => $address,
|
|
|
|
'token' => $token,
|
|
|
|
'version' => $version,
|
2019-10-23 17:52:23 +03:00
|
|
|
'clientId' => $clientId,
|
|
|
|
'status' => $status,
|
|
|
|
'statusExport' => $statusExport,
|
|
|
|
'synchronizeCartStatus' => $synchronizedCartStatus
|
2019-05-28 11:37:29 +03:00
|
|
|
);
|
2016-08-29 15:59:19 +03:00
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
$output .= $this->validateForm($settings, $output);
|
|
|
|
|
|
|
|
if ($output === '') {
|
|
|
|
Configuration::updateValue('RETAILCRM_ADDRESS', $address);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_TOKEN', $token);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_VERSION', $version);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_DELIVERY', $delivery);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_STATUS', $status);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_PAYMENT', $payment);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_DELIVERY_DEFAULT', $deliveryDefault);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_PAYMENT_DEFAULT', $paymentDefault);
|
|
|
|
Configuration::updateValue('RETAILCRM_STATUS_EXPORT', $statusExport);
|
|
|
|
Configuration::updateValue('RETAILCRM_DAEMON_COLLECTOR_ACTIVE', $collectorActive);
|
|
|
|
Configuration::updateValue('RETAILCRM_DAEMON_COLLECTOR_KEY', $collectorKey);
|
2019-10-23 17:52:23 +03:00
|
|
|
Configuration::updateValue('RETAILCRM_API_SYNCHRONIZE_CARTS', $synchronizeCartsActive);
|
|
|
|
Configuration::updateValue('RETAILCRM_API_SYNCHRONIZED_CART_STATUS', $synchronizedCartStatus);
|
2019-10-29 09:42:34 +03:00
|
|
|
Configuration::updateValue('RETAILCRM_API_SYNCHRONIZED_CART_DELAY', $synchronizedCartDelay);
|
2019-05-28 11:37:29 +03:00
|
|
|
|
|
|
|
$output .= $this->displayConfirmation($this->l('Settings updated'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($version == 5 && $this->isRegisteredInHook('actionPaymentCCAdd') == 0) {
|
|
|
|
$this->registerHook('actionPaymentCCAdd');
|
|
|
|
} elseif ($version == 4 && $this->isRegisteredInHook('actionPaymentCCAdd') == 1) {
|
|
|
|
$hook_id = Hook::getIdByName('actionPaymentCCAdd');
|
|
|
|
$this->unregisterHook($hook_id);
|
|
|
|
}
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 12:06:10 +02:00
|
|
|
if ($address && $token) {
|
|
|
|
$this->api = new RetailcrmProxy($address, $token, $this->log, $version);
|
|
|
|
$this->reference = new RetailcrmReferences($this->api);
|
2016-08-29 15:59:19 +03:00
|
|
|
}
|
2018-01-12 12:51:46 +03:00
|
|
|
|
2017-08-14 12:06:10 +02:00
|
|
|
$output .= $this->displayConfirmation(
|
|
|
|
$this->l('Timezone settings must be identical to both of your crm and shop') .
|
2018-09-03 16:22:56 +03:00
|
|
|
"<a target=\"_blank\" href=\"$address/admin/settings#t-main\">$address/admin/settings#t-main</a>"
|
2017-08-14 12:06:10 +02:00
|
|
|
);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
$this->context->controller->addCSS($this->assetsBase . '/css/retailcrm-upload.css');
|
|
|
|
$this->context->controller->addJS($this->assetsBase . '/js/retailcrm-upload.js');
|
|
|
|
$this->context->controller->addJS($this->assetsBase . '/js/exec-jobs.js');
|
2016-01-12 23:06:05 +03:00
|
|
|
$this->display(__FILE__, 'retailcrm.tpl');
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
return $output . $this->displaySettingsForm() . $this->displayUploadOrdersForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uploadOrders($orderIds)
|
|
|
|
{
|
|
|
|
if (count($orderIds) > 10) {
|
|
|
|
return $this->displayConfirmation($this->l("Can't upload more than 10 orders per request"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($orderIds) < 1) {
|
|
|
|
return $this->displayConfirmation($this->l("At least one order ID should be specified"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$apiUrl = Configuration::get('RETAILCRM_ADDRESS');
|
|
|
|
$apiKey = Configuration::get('RETAILCRM_API_TOKEN');
|
|
|
|
$apiVersion = Configuration::get('RETAILCRM_API_VERSION');
|
|
|
|
|
|
|
|
if (!empty($apiUrl) && !empty($apiKey)) {
|
|
|
|
if (!($this->api instanceof RetailcrmProxy)) {
|
|
|
|
$this->api = new RetailcrmProxy($apiUrl, $apiKey, _PS_ROOT_DIR_ . '/retailcrm.log', $apiVersion);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->displayError($this->l("Can't upload orders - set API key and API URL first!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$orders = array();
|
|
|
|
$customers = array();
|
|
|
|
$isSuccessful = true;
|
|
|
|
|
|
|
|
foreach ($orderIds as $orderId) {
|
|
|
|
$object = new Order($orderId);
|
|
|
|
$customer = new Customer($object->id_customer);
|
|
|
|
|
|
|
|
array_push($customers, static::buildCrmCustomer($customer));
|
|
|
|
array_push($orders, static::buildCrmOrder($object, $customer, null, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($customers as $item) {
|
|
|
|
if ($this->api->customersGet($item['externalId']) === false) {
|
|
|
|
$this->api->customersCreate($item);
|
|
|
|
time_nanosleep(0, 50000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($orders as $item) {
|
|
|
|
if ($this->api->ordersGet($item['externalId']) === false) {
|
|
|
|
$response = $this->api->ordersCreate($item);
|
|
|
|
} else {
|
|
|
|
$response = $this->api->ordersEdit($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
$isSuccessful = is_bool($response) ? $response : $response->isSuccessful();
|
|
|
|
|
|
|
|
time_nanosleep(0, 50000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isSuccessful) {
|
|
|
|
return $this->displayConfirmation($this->l('All orders were uploaded successfully'));
|
|
|
|
} else {
|
|
|
|
$result = $this->displayWarning($this->l('Not all orders were uploaded successfully'));
|
|
|
|
|
|
|
|
foreach (RetailcrmApiErrors::getErrors() as $error) {
|
|
|
|
$result .= $this->displayError($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
/**
|
|
|
|
* Returns 'true' if provided date string is valid
|
|
|
|
*
|
|
|
|
* @param $date
|
|
|
|
* @param string $format
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function verifyDate($date, $format = "Y-m-d")
|
|
|
|
{
|
2019-10-23 17:52:23 +03:00
|
|
|
return $date !== "0000-00-00" && (bool)date_create_from_format($format, $date);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build array with order data for retailCRM from PrestaShop cart data
|
|
|
|
*
|
|
|
|
* @param Cart $cart Cart with data
|
|
|
|
* @param string $externalId External ID for order
|
|
|
|
* @param string $paymentType Payment type (buildCrmOrder requires it)
|
|
|
|
* @param string $status Status for order
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function buildCrmOrderFromCart(Cart $cart = null, $externalId = '', $paymentType = '', $status = '')
|
|
|
|
{
|
|
|
|
if (empty($cart) || empty($paymentType) || empty($status)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$order = new Order();
|
|
|
|
$order->id_cart = $cart->id;
|
|
|
|
$order->id_customer = $cart->id_customer;
|
|
|
|
$order->total_discounts = 0;
|
|
|
|
$order->module = $paymentType;
|
|
|
|
$order->payment = $paymentType;
|
|
|
|
$orderData = static::buildCrmOrder(
|
|
|
|
$order,
|
|
|
|
new Customer($cart->id_customer),
|
|
|
|
$cart,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$orderData['externalId'] = $externalId;
|
|
|
|
$orderData['status'] = $status;
|
|
|
|
|
|
|
|
unset($orderData['payments']);
|
|
|
|
|
|
|
|
return $orderData;
|
2019-06-18 11:54:05 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
/**
|
|
|
|
* Build array with order data for retailCRM from PrestaShop order data
|
|
|
|
*
|
|
|
|
* @param Order $order PrestaShop Order
|
|
|
|
* @param Customer $customer PrestaShop Customer
|
|
|
|
* @param Cart $orderCart Cart for provided order. Optional
|
|
|
|
* @param bool $isStatusExport Use status for export
|
|
|
|
* @param bool $preferCustomerAddress Use customer address even if delivery address is provided
|
2019-10-23 17:52:23 +03:00
|
|
|
* @param bool $dataFromCart Prefer data from cart
|
2019-05-28 11:37:29 +03:00
|
|
|
*
|
|
|
|
* @return array retailCRM order data
|
|
|
|
*/
|
|
|
|
public static function buildCrmOrder(
|
|
|
|
Order $order,
|
|
|
|
Customer $customer = null,
|
|
|
|
Cart $orderCart = null,
|
|
|
|
$isStatusExport = false,
|
2019-10-23 17:52:23 +03:00
|
|
|
$preferCustomerAddress = false,
|
|
|
|
$dataFromCart = false
|
2019-05-28 11:37:29 +03:00
|
|
|
) {
|
|
|
|
$apiVersion = Configuration::get('RETAILCRM_API_VERSION');
|
|
|
|
$statusExport = Configuration::get('RETAILCRM_STATUS_EXPORT');
|
|
|
|
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'), true);
|
|
|
|
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
|
|
|
|
$status = json_decode(Configuration::get('RETAILCRM_API_STATUS'), true);
|
|
|
|
|
|
|
|
if (Module::getInstanceByName('advancedcheckout') === false) {
|
|
|
|
$paymentType = $order->module;
|
|
|
|
} else {
|
|
|
|
$paymentType = $order->payment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order->current_state == 0) {
|
|
|
|
$order_status = $statusExport;
|
|
|
|
|
|
|
|
if (!$isStatusExport) {
|
|
|
|
$order_status =
|
|
|
|
array_key_exists($order->current_state, $status)
|
2019-06-18 11:54:05 +03:00
|
|
|
? $status[$order->current_state] : 'new';
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$order_status = array_key_exists($order->current_state, $status)
|
|
|
|
? $status[$order->current_state]
|
|
|
|
: $statusExport
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
$phone = '';
|
|
|
|
$cart = $orderCart;
|
|
|
|
|
|
|
|
if (is_null($cart)) {
|
|
|
|
$cart = new Cart($order->getCartIdStatic($order->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($customer)) {
|
|
|
|
$customer = new Customer($order->id_customer);
|
|
|
|
}
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$crmOrder = array_filter(array(
|
2019-05-28 11:37:29 +03:00
|
|
|
'externalId' => $order->id,
|
2019-06-18 11:54:05 +03:00
|
|
|
'createdAt' => static::verifyDate($order->date_add, 'Y-m-d H:i:s')
|
|
|
|
? $order->date_add : date('Y-m-d H:i:s'),
|
2019-05-28 11:37:29 +03:00
|
|
|
'status' => $order_status,
|
|
|
|
'firstName' => $customer->firstname,
|
|
|
|
'lastName' => $customer->lastname,
|
|
|
|
'email' => $customer->email,
|
2019-06-18 11:54:05 +03:00
|
|
|
));
|
2019-05-28 11:37:29 +03:00
|
|
|
|
|
|
|
$addressCollection = $cart->getAddressCollection();
|
|
|
|
$address = new Address($order->id_address_delivery);
|
|
|
|
|
|
|
|
if (is_null($address->id) || $preferCustomerAddress === true) {
|
|
|
|
$address = array_filter(
|
|
|
|
$addressCollection,
|
|
|
|
function ($v) use ($customer) {
|
|
|
|
return $v->id_customer == $customer->id;
|
|
|
|
}
|
|
|
|
);
|
2019-10-23 17:52:23 +03:00
|
|
|
|
|
|
|
if (is_array($address) && count($address) == 1) {
|
|
|
|
$address = reset($address);
|
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$address = static::addressParse($address);
|
|
|
|
$crmOrder = array_merge($crmOrder, $address['order']);
|
|
|
|
|
|
|
|
if ($phone) {
|
|
|
|
$crmOrder['phone'] = $phone;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($apiVersion != 5) {
|
|
|
|
if (array_key_exists($paymentType, $payment) && !empty($payment[$paymentType])) {
|
|
|
|
$crmOrder['paymentType'] = $payment[$paymentType];
|
|
|
|
}
|
|
|
|
|
|
|
|
$crmOrder['discount'] = round($order->total_discounts, 2);
|
|
|
|
} else {
|
|
|
|
$order_payment = array(
|
|
|
|
'externalId' => $order->id .'#'. $order->reference,
|
|
|
|
'amount' => round($order->total_paid, 2),
|
|
|
|
'type' => $payment[$paymentType] ? $payment[$paymentType] : ''
|
|
|
|
);
|
|
|
|
|
|
|
|
$crmOrder['discountManualAmount'] = round($order->total_discounts, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($order_payment)) {
|
|
|
|
$crmOrder['payments'][] = $order_payment;
|
|
|
|
} else {
|
|
|
|
$crmOrder['payments'] = array();
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
$idCarrier = $dataFromCart ? $cart->id_carrier : $order->id_carrier;
|
|
|
|
|
|
|
|
if (empty($idCarrier)) {
|
|
|
|
$idCarrier = $order->id_carrier;
|
|
|
|
$totalShipping = $order->total_shipping;
|
|
|
|
$totalShippingWithoutTax = $order->total_shipping_tax_excl;
|
|
|
|
} else {
|
|
|
|
$totalShipping = $dataFromCart ? $cart->getCarrierCost($idCarrier) : $order->total_shipping;
|
|
|
|
|
|
|
|
if (!empty($totalShipping) && $totalShipping != 0) {
|
|
|
|
$totalShippingWithoutTax = $dataFromCart
|
|
|
|
? $totalShipping - $cart->getCarrierCost($idCarrier, false)
|
|
|
|
: $order->total_shipping_tax_excl;
|
|
|
|
} else {
|
|
|
|
$totalShippingWithoutTax = $order->total_shipping_tax_excl;
|
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
if (array_key_exists($idCarrier, $delivery) && !empty($delivery[$idCarrier])) {
|
|
|
|
$crmOrder['delivery']['code'] = $delivery[$idCarrier];
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
if (isset($totalShipping) && ((int) $totalShipping) > 0) {
|
|
|
|
$crmOrder['delivery']['cost'] = round($totalShipping, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($totalShippingWithoutTax) && $totalShippingWithoutTax > 0) {
|
|
|
|
$crmOrder['delivery']['netCost'] = round($totalShippingWithoutTax, 2);
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$comment = $order->getFirstMessage();
|
|
|
|
|
|
|
|
if ($comment !== false) {
|
|
|
|
$crmOrder['customerComment'] = $comment;
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
if ($dataFromCart) {
|
|
|
|
$productStore = $cart;
|
|
|
|
$converter = function ($product) {
|
|
|
|
$product['product_attribute_id'] = $product['id_product_attribute'];
|
|
|
|
$product['product_quantity'] = $product['cart_quantity'];
|
|
|
|
$product['product_id'] = $product['id_product'];
|
|
|
|
$product['id_order_detail'] = $product['id_product'];
|
|
|
|
$product['product_name'] = $product['name'];
|
|
|
|
$product['product_price'] = $product['price'];
|
|
|
|
$product['purchase_supplier_price'] = $product['price'];
|
|
|
|
$product['product_price_wt'] = $product['price_wt'];
|
|
|
|
|
|
|
|
return $product;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
$productStore = $order;
|
|
|
|
$converter = function ($product) {
|
|
|
|
return $product;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($productStore->getProducts() as $productData) {
|
|
|
|
$product = $converter($productData);
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
if (isset($product['product_attribute_id']) && $product['product_attribute_id'] > 0) {
|
|
|
|
$productId = $product['product_id'] . '#' . $product['product_attribute_id'];
|
|
|
|
} else {
|
|
|
|
$productId = $product['product_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($product['attributes']) && $product['attributes']) {
|
|
|
|
$arProp = array();
|
|
|
|
$count = 0;
|
|
|
|
$arAttr = explode(",", $product['attributes']);
|
|
|
|
|
|
|
|
foreach ($arAttr as $valAttr) {
|
|
|
|
$arItem = explode(":", $valAttr);
|
|
|
|
|
|
|
|
if ($arItem[0] && $arItem[1]) {
|
|
|
|
$arProp[$count]['name'] = trim($arItem[0]);
|
|
|
|
$arProp[$count]['value'] = trim($arItem[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = array(
|
2019-10-03 17:57:07 +03:00
|
|
|
"externalIds" => array(
|
|
|
|
array(
|
|
|
|
'code' =>'prestashop',
|
|
|
|
'value' => $productId."_".$product['id_order_detail'],
|
|
|
|
),
|
|
|
|
),
|
2019-05-28 11:37:29 +03:00
|
|
|
'offer' => array('externalId' => $productId),
|
|
|
|
'productName' => $product['product_name'],
|
|
|
|
'quantity' => $product['product_quantity'],
|
|
|
|
'initialPrice' => round($product['product_price'], 2),
|
|
|
|
/*'initialPrice' => !empty($item['rate'])
|
|
|
|
? $item['price'] + ($item['price'] * $item['rate'] / 100)
|
|
|
|
: $item['price'],*/
|
|
|
|
'purchasePrice' => round($product['purchase_supplier_price'], 2)
|
|
|
|
);
|
|
|
|
|
2019-07-15 15:48:20 +03:00
|
|
|
if (true == Configuration::get('PS_TAX')) {
|
|
|
|
$item['initialPrice'] = round($product['product_price_wt'], 2);
|
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
if (isset($arProp)) {
|
|
|
|
$item['properties'] = $arProp;
|
|
|
|
}
|
|
|
|
|
|
|
|
$crmOrder['items'][] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order->id_customer) {
|
|
|
|
$crmOrder['customer']['externalId'] = $order->id_customer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $crmOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds retailCRM customer data from PrestaShop customer data
|
|
|
|
*
|
|
|
|
* @param Customer $object
|
|
|
|
* @param array $address
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function buildCrmCustomer(Customer $object, $address = array())
|
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
array(
|
|
|
|
'externalId' => $object->id,
|
|
|
|
'firstName' => $object->firstname,
|
|
|
|
'lastName' => $object->lastname,
|
|
|
|
'email' => $object->email,
|
2019-06-18 11:54:05 +03:00
|
|
|
'createdAt' => self::verifyDate($object->date_add, 'Y-m-d H:i:s')
|
|
|
|
? $object->date_add : date('Y-m-d H:i:s'),
|
|
|
|
'birthday' => self::verifyDate($object->birthday, 'Y-m-d')
|
|
|
|
? $object->birthday : date('Y-m-d', 0)
|
2019-05-28 11:37:29 +03:00
|
|
|
),
|
|
|
|
$address
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a string to id
|
|
|
|
*
|
|
|
|
* @param string $ids string with id
|
|
|
|
*
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public static function partitionId($ids)
|
|
|
|
{
|
|
|
|
$ids = explode(',', $ids);
|
|
|
|
|
|
|
|
$ranges = [];
|
|
|
|
|
|
|
|
foreach ($ids as $idx => $uid) {
|
|
|
|
if (strpos($uid, '-')) {
|
|
|
|
$range = explode('-', $uid);
|
|
|
|
$ranges = array_merge($ranges, range($range[0], $range[1]));
|
|
|
|
unset($ids[$idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = implode(',', array_merge($ids, $ranges));
|
|
|
|
$ids = explode(',', $ids);
|
|
|
|
|
|
|
|
return $ids;
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
public function displaySettingsForm()
|
2016-01-12 23:06:05 +03:00
|
|
|
{
|
|
|
|
$this->displayConfirmation($this->l('Settings updated'));
|
|
|
|
|
|
|
|
$default_lang = $this->default_lang;
|
2017-08-14 12:06:10 +02:00
|
|
|
$apiVersions = array(
|
|
|
|
array(
|
|
|
|
'option_id' => '4',
|
|
|
|
'name' => 'v4'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'option_id' => '5',
|
|
|
|
'name' => 'v5'
|
|
|
|
)
|
|
|
|
);
|
2018-01-12 12:51:46 +03:00
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
$fields_form = array();
|
2018-01-12 12:51:46 +03:00
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
/*
|
|
|
|
* Network connection form
|
|
|
|
*/
|
2019-01-18 17:04:32 +03:00
|
|
|
$fields_form[]['form'] = array(
|
2016-01-12 23:06:05 +03:00
|
|
|
'legend' => array(
|
|
|
|
'title' => $this->l('Network connection'),
|
|
|
|
),
|
|
|
|
'input' => array(
|
2017-08-14 12:06:10 +02:00
|
|
|
array(
|
|
|
|
'type' => 'select',
|
|
|
|
'name' => 'RETAILCRM_API_VERSION',
|
|
|
|
'label' => $this->l('API version'),
|
|
|
|
'options' => array(
|
2019-01-18 17:04:32 +03:00
|
|
|
'query' => $apiVersions,
|
|
|
|
'id' => 'option_id',
|
|
|
|
'name' => 'name'
|
2017-08-14 12:06:10 +02:00
|
|
|
)
|
|
|
|
),
|
2016-01-12 23:06:05 +03:00
|
|
|
array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label' => $this->l('CRM address'),
|
|
|
|
'name' => 'RETAILCRM_ADDRESS',
|
|
|
|
'size' => 20,
|
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label' => $this->l('CRM token'),
|
|
|
|
'name' => 'RETAILCRM_API_TOKEN',
|
|
|
|
'size' => 20,
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'submit' => array(
|
|
|
|
'title' => $this->l('Save'),
|
|
|
|
'class' => 'button'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-01-18 17:04:32 +03:00
|
|
|
/*
|
|
|
|
* Daemon Collector
|
|
|
|
*/
|
|
|
|
$fields_form[]['form'] = array(
|
|
|
|
'legend' => array('title' => $this->l('Daemon Collector')),
|
|
|
|
'input' => array(
|
|
|
|
array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'label' => $this->l('Activate'),
|
|
|
|
'name' => 'RETAILCRM_DAEMON_COLLECTOR_ACTIVE',
|
|
|
|
'values' => array(
|
|
|
|
'query' => array(
|
2019-06-18 11:54:05 +03:00
|
|
|
array(
|
2019-01-18 17:04:32 +03:00
|
|
|
'id_option' => 1,
|
2019-06-18 11:54:05 +03:00
|
|
|
)
|
|
|
|
),
|
2019-01-18 17:04:32 +03:00
|
|
|
'id' => 'id_option',
|
|
|
|
'name' => 'name'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label' => $this->l('Site key'),
|
|
|
|
'name' => 'RETAILCRM_DAEMON_COLLECTOR_KEY',
|
|
|
|
'size' => 20,
|
|
|
|
'required' => false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2016-01-12 23:06:05 +03:00
|
|
|
|
2016-08-29 15:59:19 +03:00
|
|
|
if ($this->api) {
|
2019-10-23 17:52:23 +03:00
|
|
|
/*
|
|
|
|
* Synchronize carts form
|
|
|
|
*/
|
2019-10-29 09:42:34 +03:00
|
|
|
$fields_form[]['form'] = array(
|
|
|
|
'legend' => array(
|
|
|
|
'title' => $this->l('Synchronization of buyer carts'),
|
|
|
|
),
|
|
|
|
'input' => array(
|
|
|
|
array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'label' => $this->l('Create orders for abandoned carts of buyers'),
|
|
|
|
'name' => 'RETAILCRM_API_SYNCHRONIZE_CARTS',
|
|
|
|
'values' => array(
|
|
|
|
'query' => array(
|
|
|
|
array(
|
|
|
|
'id_option' => 1,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'id' => 'id_option',
|
|
|
|
'name' => 'name'
|
|
|
|
)
|
2019-10-23 17:52:23 +03:00
|
|
|
),
|
2019-10-29 09:42:34 +03:00
|
|
|
array(
|
|
|
|
'type' => 'select',
|
|
|
|
'name' => 'RETAILCRM_API_SYNCHRONIZED_CART_STATUS',
|
|
|
|
'label' => $this->l('Order status for abandoned carts of buyers'),
|
|
|
|
'options' => array(
|
|
|
|
'query' => $this->reference->getStatuseDefaultExport(),
|
|
|
|
'id' => 'id_option',
|
|
|
|
'name' => 'name'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'type' => 'select',
|
|
|
|
'name' => 'RETAILCRM_API_SYNCHRONIZED_CART_DELAY',
|
|
|
|
'label' => $this->l('Upload abandoned carts'),
|
|
|
|
'options' => array(
|
|
|
|
'query' => array(
|
|
|
|
array(
|
|
|
|
'id_option' => '0',
|
|
|
|
'name' => $this->l('Immediately')
|
2019-10-23 17:52:23 +03:00
|
|
|
),
|
2019-10-29 09:42:34 +03:00
|
|
|
array(
|
|
|
|
'id_option' => '60',
|
|
|
|
'name' => $this->l('After 1 minute')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id_option' => '300',
|
|
|
|
'name' => $this->l('After 5 minutes')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id_option' => '600',
|
|
|
|
'name' => $this->l('After 10 minutes')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id_option' => '900',
|
|
|
|
'name' => $this->l('After 15 minutes')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id_option' => '1800',
|
|
|
|
'name' => $this->l('After 30 minutes')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id_option' => '2700',
|
|
|
|
'name' => $this->l('After 45 minute')
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id_option' => '3600',
|
|
|
|
'name' => $this->l('After 1 hour')
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'id' => 'id_option',
|
|
|
|
'name' => 'name'
|
2019-10-23 17:52:23 +03:00
|
|
|
)
|
|
|
|
)
|
2019-10-29 09:42:34 +03:00
|
|
|
)
|
|
|
|
);
|
2019-10-23 17:52:23 +03:00
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
/*
|
|
|
|
* Delivery
|
|
|
|
*/
|
2019-01-18 17:04:32 +03:00
|
|
|
$fields_form[]['form'] = array(
|
2016-01-12 23:06:05 +03:00
|
|
|
'legend' => array('title' => $this->l('Delivery')),
|
|
|
|
'input' => $this->reference->getDeliveryTypes(),
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Order status
|
|
|
|
*/
|
2019-01-18 17:04:32 +03:00
|
|
|
$fields_form[]['form'] = array(
|
2016-01-12 23:06:05 +03:00
|
|
|
'legend' => array('title' => $this->l('Order statuses')),
|
|
|
|
'input' => $this->reference->getStatuses(),
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Payment
|
|
|
|
*/
|
2019-01-18 17:04:32 +03:00
|
|
|
$fields_form[]['form'] = array(
|
2016-01-12 23:06:05 +03:00
|
|
|
'legend' => array('title' => $this->l('Payment types')),
|
|
|
|
'input' => $this->reference->getPaymentTypes(),
|
|
|
|
);
|
2017-04-05 17:56:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Default
|
|
|
|
*/
|
2019-01-18 17:04:32 +03:00
|
|
|
$fields_form[]['form'] = array(
|
2017-06-16 16:13:25 +03:00
|
|
|
'legend' => array('title' => $this->l('Default')),
|
2017-09-01 10:14:13 +02:00
|
|
|
'input' => $this->reference->getPaymentAndDeliveryForDefault(
|
|
|
|
array($this->l('Delivery method'), $this->l('Payment type'))
|
|
|
|
),
|
2017-04-05 17:56:26 +03:00
|
|
|
);
|
2017-09-27 14:38:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Status in export
|
|
|
|
*/
|
2019-01-18 17:04:32 +03:00
|
|
|
$fields_form[]['form'] = array(
|
2017-09-27 14:38:24 +02:00
|
|
|
'legend' => array('title' => $this->l('Default status')),
|
|
|
|
'input' => array(array(
|
|
|
|
'type' => 'select',
|
|
|
|
'name' => 'RETAILCRM_STATUS_EXPORT',
|
|
|
|
'label' => $this->l('Default status in export'),
|
|
|
|
'options' => array(
|
2019-01-18 17:04:32 +03:00
|
|
|
'query' => $this->reference->getStatuseDefaultExport(),
|
|
|
|
'id' => 'id_option',
|
|
|
|
'name' => 'name'
|
2017-09-27 14:38:24 +02:00
|
|
|
)
|
|
|
|
)),
|
|
|
|
);
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-05-28 11:37:29 +03:00
|
|
|
* Display forms
|
2016-01-12 23:06:05 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
$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' => sprintf(
|
|
|
|
"%s&configure=%s&save%s&token=%s",
|
|
|
|
AdminController::$currentIndex,
|
|
|
|
$this->name,
|
|
|
|
$this->name,
|
|
|
|
Tools::getAdminTokenLite('AdminModules')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'back' => array(
|
|
|
|
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'),
|
|
|
|
'desc' => $this->l('Back to list')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$helper->fields_value['RETAILCRM_ADDRESS'] = Configuration::get('RETAILCRM_ADDRESS');
|
|
|
|
$helper->fields_value['RETAILCRM_API_TOKEN'] = Configuration::get('RETAILCRM_API_TOKEN');
|
2017-08-14 12:06:10 +02:00
|
|
|
$helper->fields_value['RETAILCRM_API_VERSION'] = Configuration::get('RETAILCRM_API_VERSION');
|
2017-09-27 14:38:24 +02:00
|
|
|
$helper->fields_value['RETAILCRM_STATUS_EXPORT'] = Configuration::get('RETAILCRM_STATUS_EXPORT');
|
2019-01-18 17:04:32 +03:00
|
|
|
$helper->fields_value['RETAILCRM_DAEMON_COLLECTOR_ACTIVE_1'] = Configuration::get('RETAILCRM_DAEMON_COLLECTOR_ACTIVE');
|
2019-10-23 17:52:23 +03:00
|
|
|
$helper->fields_value['RETAILCRM_API_SYNCHRONIZE_CARTS_1'] = Configuration::get('RETAILCRM_API_SYNCHRONIZE_CARTS');
|
|
|
|
$helper->fields_value['RETAILCRM_API_SYNCHRONIZED_CART_STATUS'] = Configuration::get('RETAILCRM_API_SYNCHRONIZED_CART_STATUS');
|
2019-10-29 09:42:34 +03:00
|
|
|
$helper->fields_value['RETAILCRM_API_SYNCHRONIZED_CART_DELAY'] = Configuration::get('RETAILCRM_API_SYNCHRONIZED_CART_DELAY');
|
2019-01-18 17:04:32 +03:00
|
|
|
$helper->fields_value['RETAILCRM_DAEMON_COLLECTOR_KEY'] = Configuration::get('RETAILCRM_DAEMON_COLLECTOR_KEY');
|
2018-01-12 12:51:46 +03:00
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
$deliverySettings = Configuration::get('RETAILCRM_API_DELIVERY');
|
|
|
|
if (isset($deliverySettings) && $deliverySettings != '') {
|
|
|
|
$deliveryTypes = json_decode($deliverySettings);
|
|
|
|
if ($deliveryTypes) {
|
|
|
|
foreach ($deliveryTypes as $idx => $delivery) {
|
|
|
|
$name = 'RETAILCRM_API_DELIVERY[' . $idx . ']';
|
|
|
|
$helper->fields_value[$name] = $delivery;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$statusSettings = Configuration::get('RETAILCRM_API_STATUS');
|
|
|
|
if (isset($statusSettings) && $statusSettings != '') {
|
|
|
|
$statusTypes = json_decode($statusSettings);
|
|
|
|
if ($statusTypes) {
|
|
|
|
foreach ($statusTypes as $idx => $status) {
|
|
|
|
$name = 'RETAILCRM_API_STATUS[' . $idx . ']';
|
|
|
|
$helper->fields_value[$name] = $status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$paymentSettings = Configuration::get('RETAILCRM_API_PAYMENT');
|
|
|
|
if (isset($paymentSettings) && $paymentSettings != '') {
|
|
|
|
$paymentTypes = json_decode($paymentSettings);
|
|
|
|
if ($paymentTypes) {
|
|
|
|
foreach ($paymentTypes as $idx => $payment) {
|
|
|
|
$name = 'RETAILCRM_API_PAYMENT[' . $idx . ']';
|
|
|
|
$helper->fields_value[$name] = $payment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 17:56:26 +03:00
|
|
|
$paymentSettingsDefault = Configuration::get('RETAILCRM_API_PAYMENT_DEFAULT');
|
|
|
|
if (isset($paymentSettingsDefault) && $paymentSettingsDefault != '') {
|
|
|
|
$paymentTypesDefault = json_decode($paymentSettingsDefault);
|
|
|
|
if ($paymentTypesDefault) {
|
2019-06-18 11:54:05 +03:00
|
|
|
$name = 'RETAILCRM_API_PAYMENT_DEFAULT';
|
|
|
|
$helper->fields_value[$name] = $paymentTypesDefault;
|
2017-04-05 17:56:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$deliverySettingsDefault = Configuration::get('RETAILCRM_API_DELIVERY_DEFAULT');
|
|
|
|
if (isset($deliverySettingsDefault) && $deliverySettingsDefault != '') {
|
|
|
|
$deliveryTypesDefault = json_decode($deliverySettingsDefault);
|
|
|
|
if ($deliveryTypesDefault) {
|
|
|
|
$name = 'RETAILCRM_API_DELIVERY_DEFAULT';
|
|
|
|
$helper->fields_value[$name] = $deliveryTypesDefault;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 09:42:34 +03:00
|
|
|
$synchronizedCartsStatusDefault = Configuration::get('RETAILCRM_API_SYNCHRONIZED_CART_STATUS');
|
|
|
|
if (isset($synchronizedCartsStatusDefault) && $synchronizedCartsStatusDefault != '') {
|
|
|
|
$synchronizedCartsStatus = json_decode($synchronizedCartsStatusDefault);
|
|
|
|
if ($synchronizedCartsStatus) {
|
|
|
|
$name = 'RETAILCRM_API_SYNCHRONIZED_CART_STATUS';
|
|
|
|
$helper->fields_value[$name] = $synchronizedCartsStatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$synchronizedCartsDelayDefault = Configuration::get('RETAILCRM_API_SYNCHRONIZED_CART_DELAY');
|
|
|
|
if (isset($synchronizedCartsDelayDefault) && $synchronizedCartsDelayDefault != '') {
|
|
|
|
$synchronizedCartsDelay = json_decode($synchronizedCartsDelayDefault);
|
|
|
|
if ($synchronizedCartsDelay) {
|
|
|
|
$name = 'RETAILCRM_API_SYNCHRONIZED_CART_DELAY';
|
|
|
|
$helper->fields_value[$name] = $synchronizedCartsDelay;
|
2019-10-23 17:52:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
return $helper->generateForm($fields_form);
|
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
public function displayUploadOrdersForm()
|
|
|
|
{
|
|
|
|
$default_lang = $this->default_lang;
|
|
|
|
$fields_form = array();
|
|
|
|
|
|
|
|
if ($this->api) {
|
|
|
|
$fields_form[]['form'] = array(
|
|
|
|
'legend' => array('title' => $this->l('Manual Order Upload')),
|
|
|
|
'input' => array(
|
|
|
|
array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label' => $this->l('Orders IDs'),
|
|
|
|
'name' => 'RETAILCRM_UPLOAD_ORDERS_ID',
|
|
|
|
'required' => false
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'submit' => array(
|
|
|
|
'title' => $this->l('Upload'),
|
|
|
|
'class' => 'button'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$helper = new HelperForm();
|
|
|
|
|
|
|
|
$helper->module = $this;
|
|
|
|
$helper->name_controller = $this->name;
|
|
|
|
$helper->token = Tools::getAdminTokenLite('AdminModules');
|
|
|
|
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
|
|
|
|
$helper->id = "retailcrm_upload_form";
|
|
|
|
|
|
|
|
$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' => sprintf(
|
|
|
|
"%s&configure=%s&save%s&token=%s",
|
|
|
|
AdminController::$currentIndex,
|
|
|
|
$this->name,
|
|
|
|
$this->name,
|
|
|
|
Tools::getAdminTokenLite('AdminModules')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'back' => array(
|
|
|
|
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'),
|
|
|
|
'desc' => $this->l('Back to list')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$helper->fields_value['RETAILCRM_UPLOAD_ORDERS_ID'] = '';
|
|
|
|
|
|
|
|
return $helper->generateForm($fields_form);
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
public function hookActionCustomerAccountAdd($params)
|
|
|
|
{
|
2019-06-18 11:54:05 +03:00
|
|
|
if ($this->api) {
|
|
|
|
$customer = $params['newCustomer'];
|
|
|
|
$customerSend = static::buildCrmCustomer($customer);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$this->api->customersCreate($customerSend);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
2017-05-05 17:31:51 +04:00
|
|
|
// this hook added in 1.7
|
|
|
|
public function hookActionCustomerAccountUpdate($params)
|
|
|
|
{
|
2019-06-18 11:54:05 +03:00
|
|
|
if ($this->api) {
|
|
|
|
$customer = $params['customer'];
|
2018-10-30 17:07:14 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$customerSend = static::buildCrmCustomer($customer);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$addreses = $customer->getAddresses($this->default_lang);
|
|
|
|
$address = array_shift($addreses);
|
2018-10-30 17:07:14 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
if (!empty($address)){
|
2018-10-30 17:07:14 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
if (is_object($address)) {
|
|
|
|
$address = static::addressParse($address);
|
|
|
|
} else {
|
|
|
|
$address = new Address($address['id_address']);
|
|
|
|
$address = static::addressParse($address);
|
|
|
|
}
|
|
|
|
|
|
|
|
$customerSend = array_merge($customerSend, $address['customer']);
|
2018-10-30 17:07:14 +03:00
|
|
|
}
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
if (isset($params['cart'])){
|
|
|
|
$address = static::addressParse($params['cart']);
|
|
|
|
$customerSend = array_merge($customerSend, $address['customer']);
|
|
|
|
}
|
2018-10-30 17:07:14 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$customerSend = array_merge($customerSend, isset($address['customer']) ? $address['customer'] : []);
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$this->api->customersEdit($customerSend);
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return false;
|
2017-05-05 17:31:51 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 16:15:28 +03:00
|
|
|
// this hook added in 1.7
|
|
|
|
public function hookActionValidateCustomerAddressForm($params)
|
|
|
|
{
|
|
|
|
$customer = new Customer($params['cart']->id_customer);
|
|
|
|
$customerAddress = array('customer' => $customer, 'cart' => $params['cart']);
|
|
|
|
|
|
|
|
return $this->hookActionCustomerAccountUpdate($customerAddress);
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
public function hookNewOrder($params)
|
|
|
|
{
|
|
|
|
return $this->hookActionOrderStatusPostUpdate($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hookActionPaymentConfirmation($params)
|
|
|
|
{
|
2018-05-28 17:09:31 +03:00
|
|
|
if ($this->apiVersion == 4) {
|
|
|
|
$this->api->ordersEdit(
|
|
|
|
array(
|
|
|
|
'externalId' => $params['id_order'],
|
|
|
|
'paymentStatus' => 'paid'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-01-12 23:06:05 +03:00
|
|
|
|
|
|
|
return $this->hookActionOrderStatusPostUpdate($params);
|
|
|
|
}
|
|
|
|
|
2017-05-05 17:31:51 +04:00
|
|
|
public function hookActionOrderEdited($params)
|
2017-09-01 10:14:13 +02:00
|
|
|
{
|
2019-06-18 11:54:05 +03:00
|
|
|
if ($this->api) {
|
|
|
|
$order = array(
|
|
|
|
'externalId' => $params['order']->id,
|
|
|
|
'firstName' => $params['customer']->firstname,
|
|
|
|
'lastName' => $params['customer']->lastname,
|
|
|
|
'email' => $params['customer']->email,
|
|
|
|
'createdAt' => self::verifyDate($params['order']->date_add, 'Y-m-d H:i:s')
|
|
|
|
? $params['order']->date_add : date('Y-m-d H:i:s'),
|
|
|
|
'delivery' => array('cost' => $params['order']->total_shipping)
|
|
|
|
);
|
2017-05-05 17:31:51 +04:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
if ($this->apiVersion != 5) {
|
|
|
|
$order['discount'] = $params['order']->total_discounts;
|
|
|
|
} else {
|
|
|
|
$order['discountManualAmount'] = $params['order']->total_discounts;
|
|
|
|
}
|
2017-08-14 12:06:10 +02:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$orderdb = new Order($params['order']->id);
|
2018-01-12 14:05:04 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$comment = $orderdb->getFirstMessage();
|
|
|
|
if ($comment !== false) {
|
|
|
|
$order['customerComment'] = $comment;
|
|
|
|
}
|
2018-09-03 16:22:56 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
unset($comment);
|
2018-01-12 14:05:04 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
foreach ($orderdb->getProducts() as $item) {
|
|
|
|
if (isset($item['product_attribute_id']) && $item['product_attribute_id'] > 0) {
|
|
|
|
$productId = $item['product_id'] . '#' . $item['product_attribute_id'];
|
|
|
|
} else {
|
|
|
|
$productId = $item['product_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$order['items'][] = array(
|
2019-10-03 17:57:07 +03:00
|
|
|
"externalIds" => array(
|
|
|
|
array(
|
|
|
|
'code' =>'prestashop',
|
|
|
|
'value' => $productId."_".$item['id_order_detail'],
|
|
|
|
)
|
|
|
|
),
|
2019-06-18 11:54:05 +03:00
|
|
|
'initialPrice' => $item['unit_price_tax_incl'],
|
|
|
|
'quantity' => $item['product_quantity'],
|
|
|
|
'offer' => array('externalId' => $productId),
|
|
|
|
'productName' => $item['product_name'],
|
|
|
|
);
|
2017-05-05 17:31:51 +04:00
|
|
|
}
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
$order['customer']['externalId'] = $params['order']->id_customer;
|
|
|
|
$this->api->ordersEdit($order);
|
2017-05-05 17:31:51 +04:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return false;
|
2017-05-05 17:31:51 +04:00
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
private static function addressParse($address)
|
2018-10-24 16:15:28 +03:00
|
|
|
{
|
2019-06-18 11:54:05 +03:00
|
|
|
if (!isset($customer)) {
|
|
|
|
$customer = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($order)) {
|
|
|
|
$order = [];
|
|
|
|
}
|
|
|
|
|
2018-10-24 16:15:28 +03:00
|
|
|
if ($address instanceof Address) {
|
|
|
|
$postcode = $address->postcode;
|
|
|
|
$city = $address->city;
|
|
|
|
$addres_line = sprintf("%s %s", $address->address1, $address->address2);
|
|
|
|
$countryIso = CountryCore::getIsoById($address->id_country);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($postcode)) {
|
|
|
|
$customer['address']['index'] = $postcode;
|
|
|
|
$order['delivery']['address']['index'] = $postcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($city)) {
|
|
|
|
$customer['address']['city'] = $city;
|
|
|
|
$order['delivery']['address']['city'] = $city;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($addres_line)) {
|
|
|
|
$customer['address']['text'] = $addres_line;
|
|
|
|
$order['delivery']['address']['text'] = $addres_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($countryIso)) {
|
|
|
|
$order['countryIso'] = $countryIso;
|
|
|
|
$customer['address']['countryIso'] = $countryIso;
|
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
$phones = static::getPhone($address);
|
2018-10-24 16:15:28 +03:00
|
|
|
$order = array_merge($order, $phones['order']);
|
|
|
|
$customer = array_merge($customer, $phones['customer']);
|
|
|
|
$addressArray = array('order' => $order, 'customer' => $customer);
|
|
|
|
|
|
|
|
return $addressArray;
|
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
private static function getPhone($address)
|
2018-10-24 16:15:28 +03:00
|
|
|
{
|
2019-06-18 11:54:05 +03:00
|
|
|
if (!isset($customer)) {
|
|
|
|
$customer = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($order)) {
|
|
|
|
$order = [];
|
|
|
|
}
|
|
|
|
|
2018-10-24 16:15:28 +03:00
|
|
|
if (!empty($address->phone_mobile)){
|
|
|
|
$order['phone'] = $address->phone_mobile;
|
|
|
|
$customer['phones'][] = array('number'=> $address->phone_mobile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($address->phone)){
|
|
|
|
$order['additionalPhone'] = $address->phone;
|
|
|
|
$customer['phones'][] = array('number'=> $address->phone);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($order['phone']) && !empty($order['additionalPhone'])){
|
|
|
|
$order['phone'] = $order['additionalPhone'];
|
|
|
|
unset($order['additionalPhone']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$phonesArray = array('customer' => $customer, 'order' => $order);
|
|
|
|
|
|
|
|
return $phonesArray;
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
public function hookActionOrderStatusPostUpdate($params)
|
2017-09-01 10:10:46 +03:00
|
|
|
{
|
2016-01-16 17:55:40 +03:00
|
|
|
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'), true);
|
|
|
|
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
|
|
|
|
$status = json_decode(Configuration::get('RETAILCRM_API_STATUS'), true);
|
2016-01-12 23:06:05 +03:00
|
|
|
|
|
|
|
if (isset($params['orderStatus'])) {
|
2018-05-28 17:09:31 +03:00
|
|
|
$cart = $params['cart'];
|
2019-10-23 17:52:23 +03:00
|
|
|
$response = $this->api->ordersGet(self::getCartOrderExternalId($cart));
|
|
|
|
$order = static::buildCrmOrder($params['order'], $params['customer'], $cart, false);
|
2018-09-03 16:22:56 +03:00
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
if (!empty($response) && isset($response['order'])) {
|
|
|
|
$order['id'] = $response['order']['id'];
|
|
|
|
$this->api->ordersEdit($order, 'id');
|
|
|
|
} else {
|
|
|
|
$this->api->ordersCreate($order);
|
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
} elseif (isset($params['newOrderStatus'])) {
|
2017-04-05 17:56:26 +03:00
|
|
|
$statusCode = $params['newOrderStatus']->id;
|
|
|
|
|
|
|
|
if (array_key_exists($statusCode, $status) && !empty($status[$statusCode])) {
|
|
|
|
$orderStatus = $status[$statusCode];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($orderStatus)) {
|
|
|
|
$this->api->ordersEdit(
|
|
|
|
array(
|
|
|
|
'externalId' => $params['id_order'],
|
|
|
|
'status' => $orderStatus
|
|
|
|
)
|
|
|
|
);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
2017-04-05 17:56:26 +03:00
|
|
|
}
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
|
|
|
return false;
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
2016-08-29 15:59:19 +03:00
|
|
|
|
2017-08-14 12:06:10 +02:00
|
|
|
public function hookActionPaymentCCAdd($params)
|
|
|
|
{
|
|
|
|
$order_id = Order::getOrderByCartId($params['cart']->id);
|
|
|
|
$payments = $this->reference->getSystemPaymentModules();
|
|
|
|
$paymentCRM = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
|
|
|
|
|
|
|
|
foreach ($payments as $valPay) {
|
|
|
|
if ($valPay['name'] == $params['paymentCC']->payment_method) {
|
|
|
|
$payCode = $valPay['code'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($payCode, $paymentCRM) && !empty($paymentCRM[$payCode])) {
|
|
|
|
$payment = $paymentCRM[$payCode];
|
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2017-08-14 12:06:10 +02:00
|
|
|
$response = $this->api->ordersGet($order_id);
|
|
|
|
|
2017-09-27 11:54:20 +02:00
|
|
|
if ($response !== false) {
|
2017-08-14 12:06:10 +02:00
|
|
|
$orderCRM = $response['order'];
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2017-08-14 12:06:10 +02:00
|
|
|
if ($orderCRM && $orderCRM['payments']) {
|
|
|
|
foreach ($orderCRM['payments'] as $orderPayment) {
|
|
|
|
if ($orderPayment['type'] == $payment) {
|
|
|
|
$updatePayment = $orderPayment;
|
|
|
|
$updatePayment['amount'] = $params['paymentCC']->amount;
|
|
|
|
$updatePayment['paidAt'] = $params['paymentCC']->date_add;
|
|
|
|
if ($params['paymentCC']->amount == $orderCRM['totalSumm']) {
|
|
|
|
$updatePayment['status'] = 'paid';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($updatePayment)) {
|
|
|
|
$this->api->ordersPaymentEdit($updatePayment);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
2017-08-14 12:06:10 +02:00
|
|
|
} else {
|
2018-05-28 17:09:31 +03:00
|
|
|
$createPayment = array(
|
|
|
|
'externalId' => $params['paymentCC']->id,
|
|
|
|
'amount' => $params['paymentCC']->amount,
|
|
|
|
'paidAt' => $params['paymentCC']->date_add,
|
|
|
|
'type' => $payment,
|
|
|
|
'status' => 'paid',
|
|
|
|
'order' => array(
|
|
|
|
'externalId' => $order_id,
|
|
|
|
),
|
2017-08-14 12:06:10 +02:00
|
|
|
);
|
2018-05-28 17:09:31 +03:00
|
|
|
|
|
|
|
$this->api->ordersPaymentCreate($createPayment);
|
|
|
|
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
2017-08-14 12:06:10 +02:00
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
|
|
|
return false;
|
2017-08-14 12:06:10 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
private function validateCrmAddress($address)
|
|
|
|
{
|
2019-02-13 10:16:49 +03:00
|
|
|
if (preg_match("/https:\/\/(.*).retailcrm.(pro|ru|es)/", $address) === 1) {
|
2016-08-29 15:59:19 +03:00
|
|
|
return true;
|
2017-09-01 10:14:13 +02:00
|
|
|
}
|
2016-08-29 15:59:19 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-14 12:06:10 +02:00
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
private function validateApiVersion($settings)
|
|
|
|
{
|
|
|
|
$api = new RetailcrmProxy(
|
|
|
|
$settings['address'],
|
|
|
|
$settings['token'],
|
|
|
|
_PS_ROOT_DIR_ . '/retailcrm.log',
|
|
|
|
$settings['version']
|
|
|
|
);
|
2018-09-03 16:15:30 +03:00
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
$response = $api->deliveryTypesList();
|
2017-08-14 12:06:10 +02:00
|
|
|
|
2017-09-27 11:54:20 +02:00
|
|
|
if ($response !== false) {
|
2018-09-03 16:15:30 +03:00
|
|
|
if (!$settings['clientId']) {
|
2018-10-24 16:35:07 +03:00
|
|
|
$clientId = uniqid();
|
2018-09-03 16:15:30 +03:00
|
|
|
$result = $this->integrationModule($api, $clientId, $settings['version']);
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
Configuration::updateValue('RETAILCRM_CLIENT_ID', $clientId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-14 12:06:10 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
private function validateStatuses($statuses, $statusExport, $cartStatus)
|
|
|
|
{
|
2019-10-29 09:42:34 +03:00
|
|
|
if ($cartStatus != '' && ($cartStatus == $statusExport || (stripos($statuses, $cartStatus) !== false))) {
|
2019-10-23 17:52:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
private function validateForm($settings, $output)
|
|
|
|
{
|
2017-08-14 12:06:10 +02:00
|
|
|
if (!$this->validateCrmAddress($settings['address']) || !Validate::isGenericName($settings['address'])) {
|
|
|
|
$output .= $this->displayError($this->l('Invalid or empty crm address'));
|
|
|
|
} elseif (!$settings['token'] || $settings['token'] == '') {
|
|
|
|
$output .= $this->displayError($this->l('Invalid or empty crm api token'));
|
|
|
|
} elseif (!$this->validateApiVersion($settings)) {
|
|
|
|
$output .= $this->displayError($this->l('The selected version of the API is unavailable'));
|
2019-10-23 17:52:23 +03:00
|
|
|
} elseif (!$this->validateStatuses(
|
|
|
|
$settings['status'],
|
|
|
|
$settings['statusExport'],
|
|
|
|
$settings['synchronizeCartStatus'])
|
|
|
|
) {
|
|
|
|
$output .= $this->displayError($this->l('Order status for abandoned carts should not be used in other settings'));
|
2017-08-14 12:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2018-09-03 16:15:30 +03:00
|
|
|
|
2019-10-23 17:52:23 +03:00
|
|
|
/**
|
|
|
|
* Returns externalId for order
|
|
|
|
*
|
|
|
|
* @param Cart $cart
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getCartOrderExternalId(Cart $cart)
|
|
|
|
{
|
|
|
|
return sprintf('pscart_%d', $cart->id);
|
|
|
|
}
|
|
|
|
|
2018-09-03 16:15:30 +03:00
|
|
|
/**
|
|
|
|
* Activate/deactivate module in marketplace retailCRM
|
|
|
|
*
|
|
|
|
* @param \RetailcrmProxy $apiClient
|
|
|
|
* @param string $clientId
|
|
|
|
* @param string $apiVersion
|
|
|
|
* @param boolean $active
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function integrationModule($apiClient, $clientId, $apiVersion, $active = true)
|
|
|
|
{
|
|
|
|
$scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
|
|
|
|
$logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b845ce986911-prestashop2.svg';
|
2018-10-24 16:35:07 +03:00
|
|
|
$integrationCode = 'prestashop';
|
2018-09-03 16:15:30 +03:00
|
|
|
$name = 'PrestaShop';
|
|
|
|
$accountUrl = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
|
|
|
|
2018-10-25 13:37:56 +03:00
|
|
|
if ($apiVersion == '4') {
|
2018-09-03 16:15:30 +03:00
|
|
|
$configuration = array(
|
|
|
|
'name' => $name,
|
2018-10-24 16:35:07 +03:00
|
|
|
'code' => $integrationCode . '-' . $clientId,
|
2018-09-03 16:15:30 +03:00
|
|
|
'logo' => $logo,
|
|
|
|
'configurationUrl' => $accountUrl,
|
|
|
|
'active' => $active
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $apiClient->marketplaceSettingsEdit($configuration);
|
|
|
|
} else {
|
|
|
|
$configuration = array(
|
|
|
|
'clientId' => $clientId,
|
2018-10-24 16:35:07 +03:00
|
|
|
'code' => $integrationCode . '-' . $clientId,
|
|
|
|
'integrationCode' => $integrationCode,
|
2018-09-03 16:15:30 +03:00
|
|
|
'active' => $active,
|
|
|
|
'name' => $name,
|
|
|
|
'logo' => $logo,
|
|
|
|
'accountUrl' => $accountUrl
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $apiClient->integrationModulesEdit($configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$response) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|