2014-04-30 03:51:52 +04:00
|
|
|
<?php
|
2017-09-01 10:14:13 +02:00
|
|
|
/**
|
2020-04-16 15:26:03 +03:00
|
|
|
* MIT License
|
2017-09-01 10:14:13 +02:00
|
|
|
*
|
2021-12-06 14:37:43 +03:00
|
|
|
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
2020-04-16 15:26:03 +03:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
* DISCLAIMER
|
|
|
|
*
|
|
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
|
|
* needs please refer to http://www.prestashop.com for more information.
|
|
|
|
*
|
|
|
|
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
|
2021-12-06 14:37:43 +03:00
|
|
|
* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
2020-04-16 15:26:03 +03:00
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License
|
|
|
|
*
|
|
|
|
* Don't forget to prefix your containers with your own identifier
|
|
|
|
* to avoid any conflicts with others containers.
|
2017-09-01 10:14:13 +02:00
|
|
|
*/
|
2021-12-06 14:37:43 +03:00
|
|
|
|
2017-09-01 10:14:13 +02:00
|
|
|
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
|
|
|
|
2021-12-06 14:37:43 +03:00
|
|
|
require_once dirname(__FILE__) . '/bootstrap.php';
|
2016-01-12 23:06:05 +03:00
|
|
|
|
|
|
|
class RetailCRM extends Module
|
|
|
|
{
|
2024-10-15 15:44:37 +06:00
|
|
|
const VERSION = '3.6.13';
|
2020-04-16 15:26:03 +03:00
|
|
|
const API_URL = 'RETAILCRM_ADDRESS';
|
|
|
|
const API_KEY = 'RETAILCRM_API_TOKEN';
|
|
|
|
const DELIVERY = 'RETAILCRM_API_DELIVERY';
|
|
|
|
const STATUS = 'RETAILCRM_API_STATUS';
|
2021-10-19 17:44:32 +03:00
|
|
|
const OUT_OF_STOCK_STATUS = 'RETAILCRM_API_OUT_OF_STOCK_STATUS';
|
2020-04-16 15:26:03 +03:00
|
|
|
const PAYMENT = 'RETAILCRM_API_PAYMENT';
|
|
|
|
const DELIVERY_DEFAULT = 'RETAILCRM_API_DELIVERY_DEFAULT';
|
|
|
|
const PAYMENT_DEFAULT = 'RETAILCRM_API_PAYMENT_DEFAULT';
|
|
|
|
const CLIENT_ID = 'RETAILCRM_CLIENT_ID';
|
|
|
|
const COLLECTOR_ACTIVE = 'RETAILCRM_DAEMON_COLLECTOR_ACTIVE';
|
|
|
|
const COLLECTOR_KEY = 'RETAILCRM_DAEMON_COLLECTOR_KEY';
|
|
|
|
const SYNC_CARTS_ACTIVE = 'RETAILCRM_API_SYNCHRONIZE_CARTS';
|
|
|
|
const SYNC_CARTS_STATUS = 'RETAILCRM_API_SYNCHRONIZED_CART_STATUS';
|
|
|
|
const SYNC_CARTS_DELAY = 'RETAILCRM_API_SYNCHRONIZED_CART_DELAY';
|
|
|
|
const UPLOAD_ORDERS = 'RETAILCRM_UPLOAD_ORDERS_ID';
|
|
|
|
const MODULE_LIST_CACHE_CHECKSUM = 'RETAILCRM_MODULE_LIST_CACHE_CHECKSUM';
|
2024-05-13 09:36:16 +03:00
|
|
|
const ENABLE_ICML_SERVICES = 'RETAILCRM_ENABLE_ICML_SERVICES';
|
2020-04-16 15:26:03 +03:00
|
|
|
const ENABLE_CORPORATE_CLIENTS = 'RETAILCRM_ENABLE_CORPORATE_CLIENTS';
|
|
|
|
const ENABLE_HISTORY_UPLOADS = 'RETAILCRM_ENABLE_HISTORY_UPLOADS';
|
|
|
|
const ENABLE_BALANCES_RECEIVING = 'RETAILCRM_ENABLE_BALANCES_RECEIVING';
|
2021-07-30 14:30:51 +03:00
|
|
|
const ENABLE_ORDER_NUMBER_SENDING = 'RETAILCRM_ENABLE_ORDER_NUMBER_SENDING';
|
|
|
|
const ENABLE_ORDER_NUMBER_RECEIVING = 'RETAILCRM_ENABLE_ORDER_NUMBER_RECEIVING';
|
2024-07-31 17:39:31 +03:00
|
|
|
const ENABLE_COMPANY_AND_VAT_NUMBER_SEND = 'RETAILCRM_ENABLE_COMPANY_AND_VAT_NUMBER_SEND';
|
|
|
|
const COMPANY_AND_VAT_NUMBER_CREATED = 'RETAILCRM_COMPANY_AND_VAT_NUMBER_CREATED';
|
2021-05-28 14:32:43 +03:00
|
|
|
const ENABLE_DEBUG_MODE = 'RETAILCRM_ENABLE_DEBUG_MODE';
|
2020-04-16 15:26:03 +03:00
|
|
|
const CONSULTANT_SCRIPT = 'RETAILCRM_CONSULTANT_SCRIPT';
|
|
|
|
const CONSULTANT_RCCT = 'RETAILCRM_CONSULTANT_RCCT';
|
2020-05-18 14:48:03 +03:00
|
|
|
const ENABLE_WEB_JOBS = 'RETAILCRM_ENABLE_WEB_JOBS';
|
2022-06-17 10:43:18 +03:00
|
|
|
const REQUIRED_CRM_SITE_ACCESS = 'access_selective';
|
|
|
|
const REQUIRED_CRM_SITE_COUNT = 1;
|
|
|
|
const REQUIRED_CRM_SCOPES = [
|
|
|
|
'order_read',
|
|
|
|
'order_write',
|
|
|
|
'customer_read',
|
|
|
|
'customer_write',
|
|
|
|
'store_read',
|
|
|
|
'store_write',
|
|
|
|
'reference_read',
|
|
|
|
'reference_write',
|
|
|
|
'analytics_write',
|
|
|
|
'telephony_read',
|
|
|
|
'telephony_write',
|
|
|
|
'delivery_read',
|
|
|
|
'delivery_write',
|
|
|
|
'user_read',
|
|
|
|
'user_write',
|
|
|
|
'segment_read',
|
|
|
|
'custom_fields_write',
|
|
|
|
'custom_fields_read',
|
|
|
|
'task_read',
|
|
|
|
'task_write',
|
|
|
|
'integration_read',
|
|
|
|
'integration_write',
|
|
|
|
'cost_read',
|
|
|
|
'cost_write',
|
|
|
|
'payments_read',
|
|
|
|
'payments_write',
|
|
|
|
'file_read',
|
|
|
|
'file_write',
|
|
|
|
'loyalty_read',
|
|
|
|
'loyalty_write',
|
|
|
|
'verification_write',
|
|
|
|
'verification_read',
|
|
|
|
];
|
|
|
|
|
2021-12-06 14:37:43 +03:00
|
|
|
// todo dynamically define controller classes
|
2022-05-30 18:20:21 +03:00
|
|
|
const ADMIN_CONTROLLERS
|
|
|
|
= [
|
|
|
|
RetailcrmSettingsLinkController::class,
|
|
|
|
RetailcrmSettingsController::class,
|
2022-06-20 16:03:31 +03:00
|
|
|
RetailcrmReferencesController::class,
|
|
|
|
RetailcrmCatalogController::class,
|
2022-05-30 18:20:21 +03:00
|
|
|
RetailcrmJobsController::class,
|
|
|
|
RetailcrmLogsController::class,
|
|
|
|
RetailcrmOrdersController::class,
|
|
|
|
RetailcrmExportController::class,
|
|
|
|
];
|
2020-04-16 15:26:03 +03:00
|
|
|
|
|
|
|
/** @var bool|\RetailcrmApiClientV5 */
|
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 $psVersion;
|
|
|
|
public $log;
|
|
|
|
public $confirmUninstall;
|
2020-04-16 15:26:03 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \RetailcrmReferences
|
|
|
|
*/
|
2018-05-29 13:15:18 +03:00
|
|
|
public $reference;
|
2019-10-23 17:52:23 +03:00
|
|
|
public $assetsBase;
|
2020-04-16 15:26:03 +03:00
|
|
|
private static $moduleListCache;
|
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';
|
2022-07-05 18:08:23 +03:00
|
|
|
$this->version = self::VERSION;
|
2020-04-16 15:26:03 +03:00
|
|
|
$this->author = 'DIGITAL RETAIL TECHNOLOGIES SL';
|
2021-07-09 11:57:12 +03:00
|
|
|
$this->displayName = $this->l('Simla.com');
|
|
|
|
$this->description = $this->l('Integration module for Simla.com');
|
2016-01-12 23:06:05 +03:00
|
|
|
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
|
2020-04-16 15:26:03 +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');
|
|
|
|
$this->apiUrl = Configuration::get(static::API_URL);
|
|
|
|
$this->apiKey = Configuration::get(static::API_KEY);
|
2023-08-24 09:25:03 +03:00
|
|
|
$this->ps_versions_compliancy = ['min' => '1.7.7.0', 'max' => _PS_VERSION_];
|
2017-09-01 10:14:13 +02:00
|
|
|
$this->psVersion = Tools::substr(_PS_VERSION_, 0, 3);
|
2020-04-23 15:49:29 +03:00
|
|
|
$this->log = RetailcrmLogger::getLogFile();
|
2020-04-16 15:26:03 +03:00
|
|
|
$this->module_key = 'dff3095326546f5fe8995d9e86288491';
|
2019-10-23 17:52:23 +03:00
|
|
|
$this->assetsBase =
|
|
|
|
Tools::getShopDomainSsl(true, true) .
|
|
|
|
__PS_BASE_URI__ .
|
|
|
|
'modules/' .
|
|
|
|
$this->name .
|
2020-04-16 15:26:03 +03:00
|
|
|
'/views';
|
2018-01-12 12:51:46 +03:00
|
|
|
|
2017-08-14 12:06:10 +02:00
|
|
|
if ($this->apiUrl && $this->apiKey) {
|
2022-05-30 18:20:21 +03:00
|
|
|
$this->api = new RetailcrmProxy($this->apiUrl, $this->apiKey);
|
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
|
|
|
{
|
2020-12-27 14:48:36 +03:00
|
|
|
if (Shop::isFeatureActive()) {
|
|
|
|
Shop::setContext(Shop::CONTEXT_ALL);
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
return
|
|
|
|
parent::install()
|
|
|
|
&& $this->registerHook('newOrder')
|
|
|
|
&& $this->registerHook('actionOrderStatusPostUpdate')
|
|
|
|
&& $this->registerHook('actionPaymentConfirmation')
|
|
|
|
&& $this->registerHook('actionCustomerAccountAdd')
|
|
|
|
&& $this->registerHook('actionOrderEdited')
|
|
|
|
&& $this->registerHook('actionCarrierUpdate')
|
|
|
|
&& $this->registerHook('header')
|
|
|
|
&& ($this->use_new_hooks ? $this->registerHook('actionCustomerAccountUpdate') : true)
|
|
|
|
&& ($this->use_new_hooks ? $this->registerHook('actionValidateCustomerAddressForm') : true)
|
|
|
|
&& $this->installDB()
|
2023-04-24 11:35:37 +03:00
|
|
|
&& $this->installTab();
|
2021-12-06 14:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs the tab for the admin controller
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function installTab()
|
|
|
|
{
|
|
|
|
/** @var RetailcrmAdminAbstractController $controller */
|
|
|
|
foreach (self::ADMIN_CONTROLLERS as $controller) {
|
|
|
|
$tab = new Tab();
|
|
|
|
$tab->id = $controller::getId();
|
|
|
|
$tab->id_parent = $controller::getParentId();
|
|
|
|
$tab->class_name = $controller::getClassName();
|
|
|
|
$tab->name = $controller::getName();
|
|
|
|
$tab->icon = $controller::getIcon();
|
|
|
|
$tab->position = $controller::getPosition();
|
|
|
|
$tab->active = 1;
|
|
|
|
$tab->module = $this->name;
|
|
|
|
|
|
|
|
if (!$tab->save()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws PrestaShopDatabaseException
|
|
|
|
* @throws PrestaShopException
|
|
|
|
*/
|
2022-09-12 21:22:53 +03:00
|
|
|
public function uninstallTab($forceAll = true)
|
2021-12-06 14:37:43 +03:00
|
|
|
{
|
|
|
|
/** @var RetailcrmAdminAbstractController $controller */
|
|
|
|
foreach (self::ADMIN_CONTROLLERS as $controller) {
|
|
|
|
$tabId = $controller::getId();
|
2022-09-12 21:22:53 +03:00
|
|
|
if (!$tabId || (!$forceAll && !is_subclass_of($controller, RetailcrmModuleDisablingAware::class))) {
|
2021-12-06 14:37:43 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tab = new Tab($tabId);
|
|
|
|
|
|
|
|
if (!$tab->delete()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 18:20:21 +03:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function uninstallOldTabs()
|
|
|
|
{
|
|
|
|
$moduleTabs = Tab::getCollectionFromModule($this->name);
|
|
|
|
|
|
|
|
/** @var Tab $tab */
|
|
|
|
foreach ($moduleTabs as $tab) {
|
|
|
|
$tabClassName = $tab->class_name . 'Controller';
|
|
|
|
|
|
|
|
if (!in_array($tabClassName, self::ADMIN_CONTROLLERS)) {
|
|
|
|
try {
|
|
|
|
$tab->delete();
|
|
|
|
} catch (PrestaShopException $e) {
|
|
|
|
RetailcrmLogger::writeCaller(
|
2022-06-03 17:49:00 +03:00
|
|
|
__METHOD__,
|
|
|
|
sprintf('Error while deleting old tabs: %s', $e->getMessage())
|
2022-05-30 18:20:21 +03:00
|
|
|
);
|
|
|
|
RetailcrmLogger::writeDebug(__METHOD__, $e->getTraceAsString());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:04:32 +03:00
|
|
|
public function hookHeader()
|
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
if (!empty($this->context) && !empty($this->context->controller)) {
|
2022-06-28 14:45:55 +03:00
|
|
|
$this->context->controller->addJS($this->assetsBase . '/js/worker.umd.min.js');
|
2019-01-18 17:04:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:25:01 +02:00
|
|
|
public static function updateCrmModuleState($idShop, $active = true)
|
2016-01-12 23:06:05 +03:00
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
$apiUrl = Configuration::get(static::API_URL);
|
|
|
|
$apiKey = Configuration::get(static::API_KEY);
|
|
|
|
|
|
|
|
if (!empty($apiUrl) && !empty($apiKey)) {
|
2022-05-30 18:20:21 +03:00
|
|
|
$api = new RetailcrmProxy($apiUrl, $apiKey);
|
2018-09-03 16:15:30 +03:00
|
|
|
|
2022-10-18 19:25:01 +02:00
|
|
|
$clientId = Configuration::get(static::CLIENT_ID, null, null, $idShop);
|
|
|
|
self::integrationModule($api, $clientId, $active);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uninstall()
|
|
|
|
{
|
|
|
|
if (Shop::isFeatureActive()) {
|
|
|
|
$shops = Shop::getShops();
|
|
|
|
} else {
|
2023-11-22 12:08:09 +03:00
|
|
|
$context = Context::getContext();
|
|
|
|
$shops[]['id_shop'] = $context->shop->id;
|
2022-10-18 19:25:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($shops as $shop) {
|
|
|
|
if (isset($shop['id_shop'])) {
|
|
|
|
self::updateCrmModuleState($shop['id_shop'], false);
|
|
|
|
}
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
2018-09-03 16:15:30 +03:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
return parent::uninstall()
|
2022-05-30 18:20:21 +03:00
|
|
|
&& Configuration::deleteByName(static::API_URL) // todo delete with SettingsItems class
|
2021-11-03 16:19:39 +07:00
|
|
|
&& Configuration::deleteByName(static::API_KEY)
|
|
|
|
&& Configuration::deleteByName(static::DELIVERY)
|
|
|
|
&& Configuration::deleteByName(static::STATUS)
|
|
|
|
&& Configuration::deleteByName(static::OUT_OF_STOCK_STATUS)
|
|
|
|
&& Configuration::deleteByName(static::PAYMENT)
|
|
|
|
&& Configuration::deleteByName(static::DELIVERY_DEFAULT)
|
|
|
|
&& Configuration::deleteByName(static::PAYMENT_DEFAULT)
|
|
|
|
&& Configuration::deleteByName(static::CLIENT_ID)
|
|
|
|
&& Configuration::deleteByName(static::COLLECTOR_ACTIVE)
|
|
|
|
&& Configuration::deleteByName(static::COLLECTOR_KEY)
|
|
|
|
&& Configuration::deleteByName(static::SYNC_CARTS_ACTIVE)
|
|
|
|
&& Configuration::deleteByName(static::SYNC_CARTS_STATUS)
|
|
|
|
&& Configuration::deleteByName(static::SYNC_CARTS_DELAY)
|
|
|
|
&& Configuration::deleteByName(static::UPLOAD_ORDERS)
|
|
|
|
&& Configuration::deleteByName(static::MODULE_LIST_CACHE_CHECKSUM)
|
2024-05-13 09:36:16 +03:00
|
|
|
&& Configuration::deleteByName(static::ENABLE_ICML_SERVICES)
|
2021-11-03 16:19:39 +07:00
|
|
|
&& Configuration::deleteByName(static::ENABLE_CORPORATE_CLIENTS)
|
|
|
|
&& Configuration::deleteByName(static::ENABLE_HISTORY_UPLOADS)
|
|
|
|
&& Configuration::deleteByName(static::ENABLE_BALANCES_RECEIVING)
|
|
|
|
&& Configuration::deleteByName(static::ENABLE_ORDER_NUMBER_SENDING)
|
|
|
|
&& Configuration::deleteByName(static::ENABLE_ORDER_NUMBER_RECEIVING)
|
2024-07-31 17:39:31 +03:00
|
|
|
&& Configuration::deleteByName(static::ENABLE_COMPANY_AND_VAT_NUMBER_SEND)
|
2021-11-03 16:19:39 +07:00
|
|
|
&& Configuration::deleteByName(static::ENABLE_DEBUG_MODE)
|
|
|
|
&& Configuration::deleteByName(static::ENABLE_WEB_JOBS)
|
|
|
|
&& Configuration::deleteByName('RETAILCRM_LAST_SYNC')
|
|
|
|
&& Configuration::deleteByName('RETAILCRM_LAST_ORDERS_SYNC')
|
|
|
|
&& Configuration::deleteByName('RETAILCRM_LAST_CUSTOMERS_SYNC')
|
|
|
|
&& Configuration::deleteByName(RetailcrmJobManager::LAST_RUN_NAME)
|
|
|
|
&& Configuration::deleteByName(RetailcrmJobManager::LAST_RUN_DETAIL_NAME)
|
|
|
|
&& Configuration::deleteByName(RetailcrmCatalogHelper::ICML_INFO_NAME)
|
|
|
|
&& Configuration::deleteByName(RetailcrmJobManager::IN_PROGRESS_NAME)
|
|
|
|
&& Configuration::deleteByName(RetailcrmJobManager::CURRENT_TASK)
|
|
|
|
&& Configuration::deleteByName(RetailcrmCli::CURRENT_TASK_CLI)
|
2021-12-06 14:37:43 +03:00
|
|
|
&& $this->uninstallDB()
|
2023-04-24 11:35:37 +03:00
|
|
|
&& $this->uninstallTab();
|
2021-12-06 14:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function enable($force_all = false)
|
|
|
|
{
|
2022-10-18 19:25:01 +02:00
|
|
|
$context = Context::getContext();
|
|
|
|
|
|
|
|
self::updateCrmModuleState($context->shop->id);
|
|
|
|
|
2021-12-06 14:37:43 +03:00
|
|
|
return parent::enable($force_all)
|
2023-04-24 11:35:37 +03:00
|
|
|
&& $this->installTab();
|
2021-12-06 14:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function disable($force_all = false)
|
|
|
|
{
|
2022-09-12 21:22:53 +03:00
|
|
|
if (!parent::disable($force_all)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-22 12:08:09 +03:00
|
|
|
$context = Context::getContext();
|
2022-10-18 19:25:01 +02:00
|
|
|
|
|
|
|
self::updateCrmModuleState($context->shop->id, false);
|
|
|
|
|
2022-09-12 21:22:53 +03:00
|
|
|
$sql = 'SELECT COUNT(`id_shop`) FROM `' . _DB_PREFIX_ . 'module_shop`
|
|
|
|
WHERE `id_module` = ' . (int) $this->id;
|
|
|
|
|
|
|
|
if ('0' === Db::getInstance($sql)->getValue($sql)) {
|
|
|
|
return $this->uninstallTab(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function installDB()
|
|
|
|
{
|
2023-09-21 09:40:04 +03:00
|
|
|
$resultQuery1 = Db::getInstance()->execute(
|
2021-11-03 16:19:39 +07:00
|
|
|
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'retailcrm_abandonedcarts` (
|
2020-04-16 15:26:03 +03:00
|
|
|
`id_cart` INT UNSIGNED UNIQUE NOT NULL,
|
|
|
|
`last_uploaded` DATETIME,
|
2023-09-21 09:40:04 +03:00
|
|
|
FOREIGN KEY (id_cart) REFERENCES `' . _DB_PREFIX_ . 'cart` (`id_cart`)
|
2020-04-16 15:26:03 +03:00
|
|
|
ON DELETE CASCADE
|
|
|
|
ON UPDATE CASCADE
|
2023-09-21 09:40:04 +03:00
|
|
|
) DEFAULT CHARSET=utf8;');
|
|
|
|
|
|
|
|
$resultQuery2 = Db::getInstance()->execute('
|
2021-12-06 14:37:43 +03:00
|
|
|
CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'retailcrm_exported_orders` (
|
|
|
|
`id_order` INT UNSIGNED UNIQUE NULL,
|
|
|
|
`id_order_crm` INT UNSIGNED UNIQUE NULL,
|
|
|
|
`errors` TEXT NULL,
|
|
|
|
`last_uploaded` DATETIME,
|
2023-09-21 09:40:04 +03:00
|
|
|
FOREIGN KEY (id_order) REFERENCES `' . _DB_PREFIX_ . 'orders` (`id_order`)
|
2021-12-06 14:37:43 +03:00
|
|
|
ON DELETE CASCADE
|
|
|
|
ON UPDATE CASCADE
|
2020-04-16 15:26:03 +03:00
|
|
|
) DEFAULT CHARSET=utf8;'
|
|
|
|
);
|
2023-09-21 09:40:04 +03:00
|
|
|
|
|
|
|
return $resultQuery1 && $resultQuery2;
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function uninstallDB()
|
|
|
|
{
|
2023-09-21 09:40:04 +03:00
|
|
|
return Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'retailcrm_abandonedcarts`')
|
|
|
|
&& Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'retailcrm_exported_orders`');
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
2022-03-04 13:14:32 +03:00
|
|
|
/**
|
|
|
|
* Remove files that was deleted\moved\renamed in a newer version and currently are outdated
|
|
|
|
*
|
|
|
|
* @param array $files File paths relative to the `modules/` directory
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function removeOldFiles($files)
|
|
|
|
{
|
|
|
|
foreach ($files as $file) {
|
|
|
|
try {
|
|
|
|
if (0 !== strpos($file, 'retailcrm/')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$relativePath = str_replace('retailcrm/', '', $file);
|
|
|
|
$fullPath = sprintf(
|
2022-06-03 17:49:00 +03:00
|
|
|
'%s/%s',
|
|
|
|
__DIR__,
|
|
|
|
$relativePath
|
2022-03-04 13:14:32 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!file_exists($fullPath)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RetailcrmLogger::writeCaller(
|
2022-06-03 17:49:00 +03:00
|
|
|
__METHOD__,
|
|
|
|
sprintf('Remove `%s`', $file)
|
2022-03-04 13:14:32 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
unlink($fullPath); // todo maybe check and remove empty directories
|
|
|
|
} catch (Exception $e) {
|
|
|
|
RetailcrmLogger::writeCaller(
|
|
|
|
__METHOD__,
|
|
|
|
sprintf('Error removing `%s`: %s', $file, $e->getMessage())
|
|
|
|
);
|
|
|
|
} catch (Error $e) {
|
|
|
|
RetailcrmLogger::writeCaller(
|
|
|
|
__METHOD__,
|
|
|
|
sprintf('Error removing `%s`: %s', $file, $e->getMessage())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:06:05 +03:00
|
|
|
public function getContent()
|
2017-09-01 10:10:46 +03:00
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
$address = Configuration::get(static::API_URL);
|
|
|
|
$token = Configuration::get(static::API_KEY);
|
2017-08-14 12:06:10 +02:00
|
|
|
if ($address && $token) {
|
2022-05-30 18:20:21 +03:00
|
|
|
$this->api = new RetailcrmProxy($address, $token);
|
2016-08-29 15:59:19 +03:00
|
|
|
}
|
2018-01-12 12:51:46 +03:00
|
|
|
|
2022-05-30 18:20:21 +03:00
|
|
|
$this->reference = new RetailcrmReferences($this->api);
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
$templateFactory = new RetailcrmTemplateFactory($this->context->smarty, $this->assetsBase);
|
2016-01-12 23:06:05 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return $templateFactory
|
|
|
|
->createTemplate($this)
|
|
|
|
->setContext($this->context)
|
2021-11-03 16:19:39 +07:00
|
|
|
->render(__FILE__)
|
|
|
|
;
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
public function hookActionCustomerAccountAdd($params)
|
2019-06-18 11:54:05 +03:00
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
if ($this->api) {
|
|
|
|
$customer = $params['newCustomer'];
|
|
|
|
|
2022-08-10 10:21:50 +03:00
|
|
|
$orderBuilder = new RetailcrmOrderBuilder();
|
|
|
|
$orderBuilder
|
|
|
|
->defaultLangFromConfiguration()
|
|
|
|
->setApi($this->api)
|
|
|
|
->setCmsCustomer($customer)
|
|
|
|
->createCustomerIfNotExist()
|
|
|
|
;
|
2020-04-16 15:26:03 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-10-23 17:52:23 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
// this hook added in 1.7
|
|
|
|
public function hookActionCustomerAccountUpdate($params)
|
2019-10-23 17:52:23 +03:00
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
if ($this->api) {
|
|
|
|
/** @var Customer|CustomerCore|null $customer */
|
|
|
|
$customer = isset($params['customer']) ? $params['customer'] : null;
|
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
if (empty($customer)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
/** @var Cart|CartCore|null $cart */
|
|
|
|
$cart = isset($params['cart']) ? $params['cart'] : null;
|
|
|
|
|
|
|
|
/** @var array $customerSend */
|
|
|
|
$customerSend = RetailcrmOrderBuilder::buildCrmCustomer($customer);
|
|
|
|
|
|
|
|
/** @var \RetailcrmAddressBuilder $addressBuilder */
|
|
|
|
$addressBuilder = new RetailcrmAddressBuilder();
|
|
|
|
|
|
|
|
/** @var Address|\AddressCore|array $address */
|
2021-11-03 16:19:39 +07:00
|
|
|
$address = [];
|
2020-04-16 15:26:03 +03:00
|
|
|
|
|
|
|
if (isset($customerSend['externalId'])) {
|
|
|
|
$customerData = $this->api->customersGet($customerSend['externalId']);
|
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
// Necessary part if we don't want to overwrite other phone numbers.
|
2020-04-16 15:26:03 +03:00
|
|
|
if ($customerData instanceof RetailcrmApiResponse
|
|
|
|
&& $customerData->isSuccessful()
|
|
|
|
&& $customerData->offsetExists('customer')
|
|
|
|
) {
|
|
|
|
$customerSend['phones'] = $customerData['customer']['phones'];
|
|
|
|
}
|
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
// Workaround: PrestaShop will return OLD address data, before editing.
|
|
|
|
// In order to circumvent this we are using post data to fill new address object.
|
|
|
|
if (Tools::getIsset('submitAddress')
|
|
|
|
&& Tools::getIsset('id_customer')
|
|
|
|
&& Tools::getIsset('id_address')
|
|
|
|
) {
|
|
|
|
$address = new Address(Tools::getValue('id_address'));
|
|
|
|
|
|
|
|
foreach (array_keys(Address::$definition['fields']) as $field) {
|
|
|
|
if (property_exists($address, $field) && Tools::getIsset($field)) {
|
|
|
|
$address->$field = Tools::getValue($field);
|
|
|
|
}
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
2021-04-05 14:53:57 +03:00
|
|
|
} else {
|
|
|
|
$addresses = $customer->getAddresses($this->default_lang);
|
|
|
|
$address = array_shift($addresses);
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
if (!empty($address)) {
|
|
|
|
$addressBuilder->setMode(RetailcrmAddressBuilder::MODE_CUSTOMER);
|
2020-04-16 15:26:03 +03:00
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
if (is_object($address)) {
|
|
|
|
$addressBuilder->setAddress($address);
|
|
|
|
} else {
|
|
|
|
$addressBuilder->setAddressId($address['id_address']);
|
|
|
|
}
|
2020-04-16 15:26:03 +03:00
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
$addressBuilder->build();
|
|
|
|
} elseif (!empty($cart)) {
|
|
|
|
$addressBuilder
|
|
|
|
->setMode(RetailcrmAddressBuilder::MODE_ORDER_DELIVERY)
|
|
|
|
->setAddressId($cart->id_address_invoice)
|
2021-11-03 16:19:39 +07:00
|
|
|
->build()
|
|
|
|
;
|
2021-04-05 14:53:57 +03:00
|
|
|
}
|
2020-04-16 15:26:03 +03:00
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
$customerSend = RetailcrmTools::mergeCustomerAddress($customerSend, $addressBuilder->getDataArray());
|
2020-04-16 15:26:03 +03:00
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
$this->api->customersEdit($customerSend);
|
2020-04-16 15:26:03 +03:00
|
|
|
|
2021-04-05 14:53:57 +03:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-23 17:52:23 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this hook added in 1.7
|
|
|
|
public function hookActionValidateCustomerAddressForm($params)
|
|
|
|
{
|
|
|
|
$customer = new Customer($params['cart']->id_customer);
|
2021-11-03 16:19:39 +07:00
|
|
|
$customerAddress = ['customer' => $customer, 'cart' => $params['cart']];
|
2019-10-23 17:52:23 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return $this->hookActionCustomerAccountUpdate($customerAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hookNewOrder($params)
|
|
|
|
{
|
|
|
|
return $this->hookActionOrderStatusPostUpdate($params);
|
|
|
|
}
|
2019-10-23 17:52:23 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
public function hookActionPaymentConfirmation($params)
|
|
|
|
{
|
|
|
|
return $this->hookActionOrderStatusPostUpdate($params);
|
2019-06-18 11:54:05 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 11:37:29 +03:00
|
|
|
/**
|
2020-04-16 15:26:03 +03:00
|
|
|
* This will ensure that our delivery mapping will not lose associations with edited deliveries.
|
|
|
|
* PrestaShop doesn't actually edit delivery - it will hide it via `delete` flag in DB and create new one.
|
|
|
|
* That's why we need to intercept this here and update delivery ID in mapping if necessary.
|
2019-05-28 11:37:29 +03:00
|
|
|
*
|
2020-04-16 15:26:03 +03:00
|
|
|
* @param array $params
|
2019-05-28 11:37:29 +03:00
|
|
|
*/
|
2020-04-16 15:26:03 +03:00
|
|
|
public function hookActionCarrierUpdate($params)
|
|
|
|
{
|
|
|
|
if (!array_key_exists('id_carrier', $params) || !array_key_exists('carrier', $params)) {
|
|
|
|
return;
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
/** @var Carrier|\CarrierCore $newCarrier */
|
|
|
|
$newCarrier = $params['carrier'];
|
|
|
|
$oldCarrierId = $params['id_carrier'];
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if (!($newCarrier instanceof Carrier) && !($newCarrier instanceof CarrierCore)) {
|
|
|
|
return;
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
$delivery = json_decode(Configuration::get(RetailCRM::DELIVERY), true);
|
2022-05-30 18:20:21 +03:00
|
|
|
$deliveryDefault = Configuration::get(static::DELIVERY_DEFAULT);
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if ($oldCarrierId == $deliveryDefault) {
|
2022-05-30 18:20:21 +03:00
|
|
|
Configuration::updateValue(static::DELIVERY_DEFAULT, $newCarrier->id);
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if (is_array($delivery) && array_key_exists($oldCarrierId, $delivery)) {
|
|
|
|
$delivery[$newCarrier->id] = $delivery[$oldCarrierId];
|
|
|
|
unset($delivery[$oldCarrierId]);
|
|
|
|
Configuration::updateValue(static::DELIVERY, json_encode($delivery));
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
public function hookActionOrderEdited($params)
|
|
|
|
{
|
2022-05-30 18:20:21 +03:00
|
|
|
// todo refactor it to call hookActionOrderStatusPostUpdate
|
2021-10-20 12:18:08 +03:00
|
|
|
if (!$this->api) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2021-10-20 12:18:08 +03:00
|
|
|
try {
|
|
|
|
RetailcrmExport::$api = $this->api;
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2021-10-20 12:18:08 +03:00
|
|
|
return RetailcrmExport::exportOrder($params['order']->id);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage());
|
|
|
|
RetailcrmLogger::writeNoCaller($e->getTraceAsString());
|
2022-02-07 14:19:39 +03:00
|
|
|
} catch (Error $e) {
|
|
|
|
RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage());
|
|
|
|
RetailcrmLogger::writeNoCaller($e->getTraceAsString());
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
public function hookActionOrderStatusPostUpdate($params)
|
|
|
|
{
|
2021-10-20 12:18:08 +03:00
|
|
|
if (!$this->api) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
$status = json_decode(Configuration::get(static::STATUS), true);
|
2019-10-23 17:52:23 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if (isset($params['orderStatus'])) {
|
|
|
|
try {
|
2021-10-20 12:18:08 +03:00
|
|
|
RetailcrmExport::$api = $this->api;
|
2020-04-16 15:26:03 +03:00
|
|
|
|
2021-11-15 15:14:16 +03:00
|
|
|
return RetailcrmExport::exportOrder($params['order']->id);
|
2021-10-20 12:18:08 +03:00
|
|
|
} catch (Exception $e) {
|
|
|
|
RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage());
|
|
|
|
RetailcrmLogger::writeNoCaller($e->getTraceAsString());
|
2022-02-07 14:19:39 +03:00
|
|
|
} catch (Error $e) {
|
|
|
|
RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage());
|
|
|
|
RetailcrmLogger::writeNoCaller($e->getTraceAsString());
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
|
|
|
|
2021-10-20 12:18:08 +03:00
|
|
|
return false;
|
2020-04-16 15:26:03 +03:00
|
|
|
} elseif (isset($params['newOrderStatus'])) {
|
2022-03-04 12:40:08 +03:00
|
|
|
$order = [
|
|
|
|
'externalId' => $params['id_order'],
|
|
|
|
];
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
$statusCode = $params['newOrderStatus']->id;
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if (array_key_exists($statusCode, $status) && !empty($status[$statusCode])) {
|
2022-03-04 12:40:08 +03:00
|
|
|
$order['status'] = $status[$statusCode];
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
|
|
|
|
2022-03-04 12:40:08 +03:00
|
|
|
$order = RetailcrmTools::filter('RetailcrmFilterOrderStatusUpdate', $order, $params);
|
|
|
|
|
|
|
|
if (isset($order['externalId']) && 1 < count($order)) {
|
|
|
|
$this->api->ordersEdit($order);
|
2019-10-23 17:52:23 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
public function hookActionPaymentCCAdd($params)
|
|
|
|
{
|
2022-05-30 18:20:21 +03:00
|
|
|
// todo add checks that module configured correctly
|
|
|
|
|
2021-10-12 14:35:29 +03:00
|
|
|
$payments = array_filter(json_decode(Configuration::get(static::PAYMENT), true));
|
|
|
|
$paymentType = false;
|
|
|
|
$externalId = false;
|
2019-10-23 17:52:23 +03:00
|
|
|
|
2021-10-12 14:35:29 +03:00
|
|
|
foreach ($this->reference->getSystemPaymentModules() as $paymentCMS) {
|
2021-12-06 14:37:43 +03:00
|
|
|
if ($paymentCMS['name'] === $params['paymentCC']->payment_method
|
2021-10-12 14:35:29 +03:00
|
|
|
&& array_key_exists($paymentCMS['code'], $payments)
|
|
|
|
&& !empty($payments[$paymentCMS['code']])
|
|
|
|
) {
|
|
|
|
$paymentType = $payments[$paymentCMS['code']];
|
|
|
|
break;
|
|
|
|
}
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
2019-10-23 17:52:23 +03:00
|
|
|
|
2021-10-12 14:35:29 +03:00
|
|
|
if (!$paymentType || empty($params['cart']) || empty((int) $params['cart']->id)) {
|
2021-02-26 15:42:08 +03:00
|
|
|
return false;
|
2021-04-05 18:00:04 +03:00
|
|
|
}
|
2021-02-26 15:42:08 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
$response = $this->api->ordersGet(RetailcrmTools::getCartOrderExternalId($params['cart']));
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
if (false !== $response && isset($response['order'])) {
|
2021-01-26 18:27:28 +03:00
|
|
|
$externalId = RetailcrmTools::getCartOrderExternalId($params['cart']);
|
|
|
|
} else {
|
2021-04-05 14:30:03 +03:00
|
|
|
if (version_compare(_PS_VERSION_, '1.7.1.0', '>=')) {
|
2021-10-12 14:35:29 +03:00
|
|
|
$id_order = (int) Order::getIdByCartId((int) $params['cart']->id);
|
2021-04-05 14:30:03 +03:00
|
|
|
} else {
|
2021-10-12 14:35:29 +03:00
|
|
|
$id_order = (int) Order::getOrderByCartId((int) $params['cart']->id);
|
2021-04-05 14:30:03 +03:00
|
|
|
}
|
2021-01-26 18:27:28 +03:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
if (0 < $id_order) {
|
2021-10-12 14:35:29 +03:00
|
|
|
// do not update payment if the order in Cart and OrderPayment aren't the same
|
|
|
|
if ($params['paymentCC']->order_reference) {
|
|
|
|
$order = Order::getByReference($params['paymentCC']->order_reference)->getFirst();
|
|
|
|
if (!$order || $order->id !== $id_order) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 14:30:03 +03:00
|
|
|
$response = $this->api->ordersGet($id_order);
|
2021-11-03 16:19:39 +07:00
|
|
|
if (false !== $response && isset($response['order'])) {
|
2021-04-05 14:30:03 +03:00
|
|
|
$externalId = $id_order;
|
2021-02-26 15:42:08 +03:00
|
|
|
}
|
2021-01-26 18:27:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
if (false === $externalId) {
|
2021-10-12 14:35:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
$status = (0 < round($params['paymentCC']->amount, 2) ? 'paid' : null);
|
2021-10-12 14:35:29 +03:00
|
|
|
$orderCRM = $response['order'];
|
|
|
|
|
|
|
|
if ($orderCRM && $orderCRM['payments']) {
|
|
|
|
foreach ($orderCRM['payments'] as $orderPayment) {
|
|
|
|
if ($orderPayment['type'] === $paymentType) {
|
|
|
|
$updatePayment = $orderPayment;
|
|
|
|
$updatePayment['amount'] = $params['paymentCC']->amount;
|
|
|
|
$updatePayment['paidAt'] = $params['paymentCC']->date_add;
|
|
|
|
$updatePayment['status'] = $status;
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-12 14:35:29 +03:00
|
|
|
}
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2021-10-12 14:35:29 +03:00
|
|
|
if (isset($updatePayment)) {
|
|
|
|
$this->api->ordersPaymentEdit($updatePayment, 'id');
|
|
|
|
} else {
|
2021-11-03 16:19:39 +07:00
|
|
|
$createPayment = [
|
2021-10-12 14:35:29 +03:00
|
|
|
'externalId' => $params['paymentCC']->id,
|
2021-11-03 16:19:39 +07:00
|
|
|
'amount' => $params['paymentCC']->amount,
|
|
|
|
'paidAt' => $params['paymentCC']->date_add,
|
|
|
|
'type' => $paymentType,
|
|
|
|
'status' => $status,
|
|
|
|
'order' => [
|
2021-10-12 14:35:29 +03:00
|
|
|
'externalId' => $externalId,
|
2021-11-03 16:19:39 +07:00
|
|
|
],
|
|
|
|
];
|
2019-07-15 15:48:20 +03:00
|
|
|
|
2021-10-12 14:35:29 +03:00
|
|
|
$this->api->ordersPaymentCreate($createPayment);
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return true;
|
2019-05-28 11:37:29 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
/**
|
|
|
|
* Activate/deactivate module in marketplace retailCRM
|
|
|
|
*
|
|
|
|
* @param \RetailcrmProxy $apiClient
|
|
|
|
* @param string $clientId
|
2021-11-03 16:19:39 +07:00
|
|
|
* @param bool $active
|
2020-04-16 15:26:03 +03:00
|
|
|
*
|
2021-11-03 16:19:39 +07:00
|
|
|
* @return bool
|
2020-04-16 15:26:03 +03:00
|
|
|
*/
|
2022-10-18 19:25:01 +02:00
|
|
|
public static function integrationModule($apiClient, $clientId, $active = true)
|
2019-05-28 11:37:29 +03:00
|
|
|
{
|
2022-10-18 19:25:01 +02:00
|
|
|
$context = Context::getContext();
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
$logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b845ce986911-prestashop2.svg';
|
|
|
|
$integrationCode = 'prestashop';
|
|
|
|
$name = 'PrestaShop';
|
2022-10-18 19:25:01 +02:00
|
|
|
$accountUrl = $context->link->getAdminLink('AdminModules') . '&configure=retailcrm';
|
2021-11-03 16:19:39 +07:00
|
|
|
$configuration = [
|
2020-04-16 15:26:03 +03:00
|
|
|
'clientId' => $clientId,
|
|
|
|
'code' => $integrationCode . '-' . $clientId,
|
|
|
|
'integrationCode' => $integrationCode,
|
|
|
|
'active' => $active,
|
|
|
|
'name' => $name,
|
|
|
|
'logo' => $logo,
|
2021-11-03 16:19:39 +07:00
|
|
|
'accountUrl' => $accountUrl,
|
|
|
|
];
|
2020-04-16 15:26:03 +03:00
|
|
|
$response = $apiClient->integrationModulesEdit($configuration);
|
2019-05-28 11:37:29 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if (!$response) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if ($response->isSuccessful()) {
|
2019-06-18 11:54:05 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
/**
|
|
|
|
* Loads data from modules list cache
|
|
|
|
*
|
|
|
|
* @return array|mixed
|
|
|
|
*/
|
|
|
|
private static function requireModulesCache()
|
2018-10-24 16:15:28 +03:00
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
if (file_exists(static::getModulesCache())) {
|
2021-11-03 16:19:39 +07:00
|
|
|
return require_once static::getModulesCache();
|
2019-06-18 11:54:05 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-18 11:54:05 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
/**
|
|
|
|
* Returns path to modules list cache
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function getModulesCache()
|
|
|
|
{
|
|
|
|
if (defined('_PS_CACHE_DIR_')) {
|
|
|
|
return _PS_CACHE_DIR_ . '/retailcrm_modules_cache.php';
|
2018-10-24 16:15:28 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if (!defined('_PS_ROOT_DIR_')) {
|
|
|
|
return '';
|
2018-10-24 16:15:28 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
$cacheDir = _PS_ROOT_DIR_ . '/cache';
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
if (false !== realpath($cacheDir) && is_dir($cacheDir)) {
|
2020-04-16 15:26:03 +03:00
|
|
|
return $cacheDir . '/retailcrm_modules_cache.php';
|
2018-10-24 16:15:28 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return _PS_ROOT_DIR_ . '/retailcrm_modules_cache.php';
|
|
|
|
}
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
/**
|
|
|
|
* Returns modules list, caches result. Recreates cache when needed.
|
|
|
|
* Activity indicator in cache will be rewrited by current state.
|
|
|
|
*
|
|
|
|
* @return array
|
2022-02-07 14:19:39 +03:00
|
|
|
*
|
|
|
|
* @throws PrestaShopException
|
2020-04-16 15:26:03 +03:00
|
|
|
*/
|
|
|
|
public static function getCachedCmsModulesList()
|
2018-10-24 16:15:28 +03:00
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
$storedHash = (string) Configuration::get(static::MODULE_LIST_CACHE_CHECKSUM);
|
|
|
|
$calculatedHash = md5(implode('#', Module::getModulesDirOnDisk(true)));
|
2019-06-18 11:54:05 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
if ($storedHash != $calculatedHash) {
|
2021-11-03 16:19:39 +07:00
|
|
|
$serializedModules = [];
|
2020-04-16 15:26:03 +03:00
|
|
|
static::$moduleListCache = Module::getModulesOnDisk(true);
|
2019-06-18 11:54:05 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
foreach (static::$moduleListCache as $module) {
|
|
|
|
$serializedModules[] = json_encode($module);
|
|
|
|
}
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
Configuration::updateValue(static::MODULE_LIST_CACHE_CHECKSUM, $calculatedHash);
|
|
|
|
static::writeModulesCache($serializedModules);
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
return static::$moduleListCache;
|
2022-02-07 14:19:39 +03:00
|
|
|
}
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
try {
|
|
|
|
if (is_array(static::$moduleListCache)) {
|
|
|
|
return static::$moduleListCache;
|
|
|
|
}
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
$modulesList = static::requireModulesCache();
|
2018-10-24 16:15:28 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
if (false === $modulesList) {
|
|
|
|
Configuration::updateValue(static::MODULE_LIST_CACHE_CHECKSUM, 'not exist');
|
2016-01-12 23:06:05 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
return static::getCachedCmsModulesList();
|
|
|
|
}
|
2018-09-03 16:22:56 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
static::$moduleListCache = [];
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
foreach ($modulesList as $serializedModule) {
|
|
|
|
$deserialized = json_decode($serializedModule);
|
|
|
|
|
|
|
|
if ($deserialized instanceof stdClass
|
2022-05-30 18:20:21 +03:00
|
|
|
&& property_exists($deserialized, 'name')
|
|
|
|
&& property_exists($deserialized, 'active')
|
|
|
|
) {
|
2022-02-07 14:19:39 +03:00
|
|
|
$deserialized->active = Module::isEnabled($deserialized->name);
|
|
|
|
static::$moduleListCache[] = $deserialized;
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
2022-02-07 14:19:39 +03:00
|
|
|
}
|
2018-05-28 17:09:31 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
static::$moduleListCache = array_filter(static::$moduleListCache);
|
|
|
|
unset($modulesList);
|
2017-04-05 17:56:26 +03:00
|
|
|
|
2022-02-07 14:19:39 +03:00
|
|
|
return static::$moduleListCache;
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage());
|
|
|
|
RetailcrmLogger::writeNoCaller($exception->getTraceAsString());
|
|
|
|
} catch (Error $exception) {
|
|
|
|
RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage());
|
|
|
|
RetailcrmLogger::writeNoCaller($exception->getTraceAsString());
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
2022-02-07 14:19:39 +03:00
|
|
|
|
|
|
|
Configuration::updateValue(static::MODULE_LIST_CACHE_CHECKSUM, 'exception');
|
|
|
|
|
|
|
|
return static::getCachedCmsModulesList();
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|
2016-08-29 15:59:19 +03:00
|
|
|
|
2020-04-16 15:26:03 +03:00
|
|
|
/**
|
|
|
|
* Writes module list to cache file.
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
*/
|
|
|
|
private static function writeModulesCache($data)
|
2017-08-14 12:06:10 +02:00
|
|
|
{
|
2020-04-16 15:26:03 +03:00
|
|
|
$file = fopen(static::getModulesCache(), 'w+');
|
2017-08-14 12:06:10 +02:00
|
|
|
|
2021-11-03 16:19:39 +07:00
|
|
|
if (false !== $file) {
|
2020-04-16 15:26:03 +03:00
|
|
|
fwrite($file, '<?php' . PHP_EOL);
|
|
|
|
fwrite($file, '// Autogenerated module list cache for retailCRM' . PHP_EOL);
|
|
|
|
fwrite($file, '// Delete this file if you cannot see some payment types in module' . PHP_EOL);
|
|
|
|
fwrite($file, 'return ' . var_export($data, true) . ';' . PHP_EOL);
|
|
|
|
fflush($file);
|
|
|
|
fclose($file);
|
2017-08-14 12:06:10 +02:00
|
|
|
}
|
2020-04-16 15:26:03 +03:00
|
|
|
}
|
2016-01-12 23:06:05 +03:00
|
|
|
}
|