1
0
mirror of synced 2024-11-23 22:06:11 +03:00
bitrix-module/intaro.retailcrm/classes/general/RCrmActions.php

616 lines
20 KiB
PHP
Raw Normal View History

2016-09-15 16:42:10 +03:00
<?php
IncludeModuleLangFile(__FILE__);
class RCrmActions
{
public static $MODULE_ID = 'intaro.retailcrm';
public static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
2017-09-04 11:36:04 +03:00
public static $CRM_API_VERSION = 'api_version';
2016-09-15 16:42:10 +03:00
const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED';
2016-10-04 17:57:39 +03:00
public static function SitesList()
{
2016-09-15 16:42:10 +03:00
$arSites = array();
$rsSites = CSite::GetList($by, $sort, array('ACTIVE' => 'Y'));
2016-10-04 17:57:39 +03:00
while ($ar = $rsSites->Fetch()) {
2018-10-18 12:07:10 +03:00
$arSites[] = $ar;
2016-09-15 16:42:10 +03:00
}
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
return $arSites;
}
2018-10-18 12:07:10 +03:00
2016-10-04 17:57:39 +03:00
public static function OrderTypesList($arSites)
{
2018-10-18 12:07:10 +03:00
$orderTypesList = array();
2016-10-04 17:57:39 +03:00
foreach ($arSites as $site) {
2016-09-15 16:42:10 +03:00
$personTypes = \Bitrix\Sale\PersonType::load($site['LID']);
$bitrixOrderTypesList = array();
2016-10-04 17:57:39 +03:00
foreach ($personTypes as $personType) {
if (!array_key_exists($personType['ID'], $orderTypesList)) {
2016-09-15 16:42:10 +03:00
$bitrixOrderTypesList[$personType['ID']] = $personType;
}
asort($bitrixOrderTypesList);
}
$orderTypesList = $orderTypesList + $bitrixOrderTypesList;
}
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
return $orderTypesList;
}
2018-10-18 12:07:10 +03:00
2016-10-04 17:57:39 +03:00
public static function DeliveryList()
{
2016-09-15 16:42:10 +03:00
$bitrixDeliveryTypesList = array();
$arDeliveryServiceAll = \Bitrix\Sale\Delivery\Services\Manager::getActiveList();
$noOrderId = \Bitrix\Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId();
2017-09-04 11:36:04 +03:00
$groups = array();
2016-10-04 17:57:39 +03:00
foreach ($arDeliveryServiceAll as $arDeliveryService) {
2017-09-04 11:36:04 +03:00
if ($arDeliveryService['CLASS_NAME'] == '\Bitrix\Sale\Delivery\Services\Group') {
$groups[] = $arDeliveryService['ID'];
}
}
foreach ($arDeliveryServiceAll as $arDeliveryService) {
2018-10-18 12:07:10 +03:00
if ((($arDeliveryService['PARENT_ID'] == '0' || $arDeliveryService['PARENT_ID'] == null) ||
2020-04-24 13:18:18 +03:00
in_array($arDeliveryService['PARENT_ID'], $groups)) &&
$arDeliveryService['ID'] != $noOrderId &&
$arDeliveryService['CLASS_NAME'] != '\Bitrix\Sale\Delivery\Services\Group') {
if (in_array($arDeliveryService['PARENT_ID'], $groups)) {
$arDeliveryService['PARENT_ID'] = 0;
}
2016-09-15 16:42:10 +03:00
$bitrixDeliveryTypesList[] = $arDeliveryService;
}
}
2016-09-15 16:42:10 +03:00
return $bitrixDeliveryTypesList;
}
2018-10-18 12:07:10 +03:00
2016-10-04 17:57:39 +03:00
public static function PaymentList()
{
2016-09-15 16:42:10 +03:00
$bitrixPaymentTypesList = array();
$dbPaymentAll = \Bitrix\Sale\PaySystem\Manager::getList(array(
'select' => array('ID', 'NAME'),
'filter' => array('ACTIVE' => 'Y')
));
2016-10-04 17:57:39 +03:00
while ($payment = $dbPaymentAll->fetch()) {
2016-09-15 16:42:10 +03:00
$bitrixPaymentTypesList[] = $payment;
}
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
return $bitrixPaymentTypesList;
2018-10-18 12:07:10 +03:00
}
2017-09-04 11:36:04 +03:00
2016-10-04 17:57:39 +03:00
public static function StatusesList()
{
2016-09-15 16:42:10 +03:00
$bitrixPaymentStatusesList = array();
2016-10-12 11:40:18 +03:00
$obStatuses = \Bitrix\Sale\Internals\StatusTable::getList(array(
'filter' => array('TYPE' => 'O', '=Bitrix\Sale\Internals\StatusLangTable:STATUS.LID' => LANGUAGE_ID),
2016-10-12 11:40:18 +03:00
'select' => array('ID', "NAME" => 'Bitrix\Sale\Internals\StatusLangTable:STATUS.NAME')
));
while ($arStatus = $obStatuses->fetch()) {
$bitrixPaymentStatusesList[$arStatus['ID']] = array(
'ID' => $arStatus['ID'],
'NAME' => $arStatus['NAME'],
);
2016-09-15 16:42:10 +03:00
}
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
return $bitrixPaymentStatusesList;
2017-09-04 11:36:04 +03:00
}
2016-10-04 17:57:39 +03:00
public static function OrderPropsList()
{
2016-09-15 16:42:10 +03:00
$bitrixPropsList = array();
$arPropsAll = \Bitrix\Sale\Internals\OrderPropsTable::getList(array(
'select' => array('*'),
'filter' => array('CODE' => '_%')
2016-09-15 16:42:10 +03:00
));
2016-10-04 17:57:39 +03:00
while ($prop = $arPropsAll->Fetch()) {
2016-09-15 16:42:10 +03:00
$bitrixPropsList[$prop['PERSON_TYPE_ID']][] = $prop;
}
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
return $bitrixPropsList;
2018-10-18 12:07:10 +03:00
}
public static function PricesExportList()
2017-09-04 11:36:04 +03:00
{
$catalogExportPrices = array();
2019-01-16 10:28:11 +03:00
$dbPriceType = CCatalogGroup::GetList(
array(),
array(),
false,
false,
array('ID', 'NAME', 'NAME_LANG')
);
2017-09-04 11:36:04 +03:00
while ($arPriceType = $dbPriceType->Fetch())
{
$catalogExportPrices[$arPriceType['ID']] = $arPriceType;
}
2018-10-18 12:07:10 +03:00
2017-09-04 11:36:04 +03:00
return $catalogExportPrices;
2018-10-18 12:07:10 +03:00
}
public static function StoresExportList()
2017-09-04 11:36:04 +03:00
{
$catalogExportStores = array();
$dbStores = CCatalogStore::GetList(array(), array("ACTIVE" => "Y"), false, false, array('ID', 'TITLE'));
while ($stores = $dbStores->Fetch()) {
$catalogExportStores[] = $stores;
}
2018-10-18 12:07:10 +03:00
2017-09-04 11:36:04 +03:00
return $catalogExportStores;
2018-10-18 12:07:10 +03:00
}
2017-09-04 11:36:04 +03:00
public static function IblocksExportList()
{
$catalogExportIblocks = array();
$dbIblocks = CIBlock::GetList(array("IBLOCK_TYPE" => "ASC", "NAME" => "ASC"), array('CHECK_PERMISSIONS' => 'Y','MIN_PERMISSION' => 'W'));
while ($iblock = $dbIblocks->Fetch()) {
if ($arCatalog = CCatalog::GetByIDExt($iblock["ID"])) {
if($arCatalog['CATALOG_TYPE'] == "D" || $arCatalog['CATALOG_TYPE'] == "X" || $arCatalog['CATALOG_TYPE'] == "P") {
$catalogExportIblocks[$iblock['ID']] = array(
'ID' => $iblock['ID'],
'IBLOCK_TYPE_ID' => $iblock['IBLOCK_TYPE_ID'],
'LID' => $iblock['LID'],
'CODE' => $iblock['CODE'],
'NAME' => $iblock['NAME'],
);
if ($arCatalog['CATALOG_TYPE'] == "X" || $arCatalog['CATALOG_TYPE'] == "P") {
$iblockOffer = CCatalogSKU::GetInfoByProductIBlock($iblock["ID"]);
$catalogExportIblocks[$iblock['ID']]['SKU'] = $iblockOffer;
}
}
}
}
2018-10-18 12:07:10 +03:00
2017-09-04 11:36:04 +03:00
return $catalogExportIblocks;
}
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
/**
*
* w+ event in bitrix log
*/
2016-10-04 17:57:39 +03:00
public static function eventLog($auditType, $itemId, $description)
{
2016-09-15 16:42:10 +03:00
CEventLog::Add(array(
"SEVERITY" => "SECURITY",
"AUDIT_TYPE_ID" => $auditType,
"MODULE_ID" => self::$MODULE_ID,
"ITEM_ID" => $itemId,
"DESCRIPTION" => $description,
));
}
/**
*
* Agent function
*
* @return self name
*/
2016-10-04 17:57:39 +03:00
public static function uploadOrdersAgent()
{
2016-09-15 16:42:10 +03:00
RetailCrmOrder::uploadOrders();
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if (is_array($failedIds) && !empty($failedIds)) {
RetailCrmOrder::uploadOrders(50, true);
}
2017-09-04 11:36:04 +03:00
return;
2016-09-15 16:42:10 +03:00
}
/**
*
* Agent function
*
* @return self name
*/
2016-10-04 17:57:39 +03:00
public static function orderAgent()
{
if (COption::GetOptionString('main', 'agents_use_crontab', 'N') != 'N') {
2016-09-15 16:42:10 +03:00
define('NO_AGENT_CHECK', true);
}
RetailCrmHistory::customerHistory();
RetailCrmHistory::orderHistory();
self::uploadOrdersAgent();
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
return 'RCrmActions::orderAgent();';
}
/**
* removes all empty fields from arrays
* working with nested arrs
*
* @param array $arr
* @return array
*/
2016-10-04 17:57:39 +03:00
public static function clearArr($arr)
{
2016-09-15 16:42:10 +03:00
if (is_array($arr) === false) {
return $arr;
}
$result = array();
foreach ($arr as $index => $node ) {
$result[ $index ] = is_array($node) === true ? self::clearArr($node) : trim($node);
if ($result[ $index ] == '' || $result[ $index ] === null || count($result[ $index ]) < 1) {
unset($result[ $index ]);
}
}
return $result;
}
/**
*
2020-04-24 13:18:18 +03:00
* @param array|bool|\SplFixedArray|string $str in SITE_CHARSET
*
* @return array|bool|\SplFixedArray|string $str in utf-8
* @global $APPLICATION
2016-09-15 16:42:10 +03:00
*/
2016-10-04 17:57:39 +03:00
public static function toJSON($str)
{
2016-09-15 16:42:10 +03:00
global $APPLICATION;
return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8');
}
/**
*
2020-04-24 13:18:18 +03:00
* @param string|array|\SplFixedArray $str in utf-8
*
* @return array|bool|\SplFixedArray|string $str in SITE_CHARSET
* @global $APPLICATION
2016-09-15 16:42:10 +03:00
*/
2016-10-04 17:57:39 +03:00
public static function fromJSON($str)
{
2016-09-15 16:42:10 +03:00
global $APPLICATION;
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
}
/**
* Extracts payment ID or client ID from payment externalId
* Payment ID - pass nothing or 'id' as second argument
* Client ID - pass 'client_id' as second argument
*
* @param $externalId
* @param string $data
* @return bool|string
*/
public static function getFromPaymentExternalId($externalId, $data = 'id')
{
switch ($data) {
case 'id':
if (false === strpos($externalId, '_')) {
return $externalId;
} else {
return substr($externalId, 0, strpos($externalId, '_'));
}
break;
case 'client_id':
if (false === strpos($externalId, '_')) {
return '';
} else {
return substr($externalId, strpos($externalId, '_'), count($externalId));
}
break;
}
return '';
}
/**
* Returns true if provided externalId in new format (id_clientId)
*
* @param $externalId
* @return bool
*/
public static function isNewExternalId($externalId)
{
return !(false === strpos($externalId, '_'));
}
/**
* Generates payment external ID
*
* @param $id
*
* @return string
*/
public static function generatePaymentExternalId($id)
{
return sprintf(
'%s_%s',
$id,
COption::GetOptionString(self::$MODULE_ID, 'client_id', 0)
);
}
2018-02-27 15:29:43 +03:00
/**
* Unserialize array
2018-07-16 16:01:19 +03:00
*
2018-02-27 15:29:43 +03:00
* @param string $string
2018-07-16 16:01:19 +03:00
*
* @return mixed
2018-02-27 15:29:43 +03:00
*/
public static function unserializeArrayRecursive($string)
{
2018-07-16 16:01:19 +03:00
if ($string === false || empty($string)) {
return false;
}
2018-02-27 15:29:43 +03:00
if (is_string($string)) {
$string = unserialize($string);
}
if (!is_array($string)) {
2018-03-22 16:11:04 +03:00
$string = self::unserializeArrayRecursive($string);
2018-02-27 15:29:43 +03:00
}
return $string;
}
2016-10-04 17:57:39 +03:00
public static function explodeFIO($fio)
{
2017-09-04 11:36:04 +03:00
$result = array();
2016-12-13 14:01:48 +03:00
$fio = preg_replace('|[\s]+|s', ' ', trim($fio));
2017-09-04 11:36:04 +03:00
if (empty($fio)) {
return $result;
} else {
$newFio = explode(" ", $fio, 3);
}
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
switch (count($newFio)) {
default:
case 0:
$result['firstName'] = $fio;
break;
case 1:
$result['firstName'] = $newFio[0];
break;
case 2:
$result = array(
'lastName' => $newFio[0],
'firstName' => $newFio[1]
);
break;
case 3:
$result = array(
'lastName' => $newFio[0],
'firstName' => $newFio[1],
'patronymic' => $newFio[2]
);
break;
}
return $result;
}
2018-10-18 12:07:10 +03:00
public static function sendConfiguration($api, $api_version, $active = true)
{
$scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$baseUrl = $scheme . $_SERVER['HTTP_HOST'];
$integrationCode = 'bitrix';
$logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5af47fe682bf2-1c-bitrix-logo.svg';
$accountUrl = $baseUrl . '/bitrix/admin';
$clientId = COption::GetOptionString(self::$MODULE_ID, 'client_id', 0);
if (!$clientId) {
$clientId = uniqid();
COption::SetOptionString(self::$MODULE_ID, 'client_id', $clientId);
}
$code = $integrationCode . '-' . $clientId;
if ($api_version == 'v4') {
$configuration = array(
'name' => GetMessage('API_MODULE_NAME'),
'code' => $code,
'logo' => $logo,
'configurationUrl' => $accountUrl,
'active' => $active
);
self::apiMethod($api, 'marketplaceSettingsEdit', __METHOD__, $configuration);
} else {
$configuration = array(
'clientId' => $clientId,
'code' => $code,
'integrationCode' => $integrationCode,
'active' => $active,
'name' => GetMessage('API_MODULE_NAME'),
'logo' => $logo,
'baseUrl' => $baseUrl,
'accountUrl' => $accountUrl
);
self::apiMethod($api, 'integrationModulesEdit', __METHOD__, $configuration);
}
}
2016-10-04 17:57:39 +03:00
public static function apiMethod($api, $methodApi, $method, $params, $site = null)
{
switch ($methodApi) {
2018-10-18 12:07:10 +03:00
case 'ordersPaymentDelete':
2017-09-04 11:36:04 +03:00
case 'ordersHistory':
2018-10-18 12:07:10 +03:00
case 'customerHistory':
2017-09-04 11:36:04 +03:00
case 'ordersFixExternalIds':
case 'customersFixExternalIds':
2020-04-24 13:18:18 +03:00
case 'customersCorporateContacts':
case 'customersList':
case 'customersCorporateList':
2017-09-04 11:36:04 +03:00
return self::proxy($api, $methodApi, $method, array($params));
case 'orderGet':
return self::proxy($api, 'ordersGet', $method, array($params, 'id', $site));
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
case 'ordersGet':
case 'ordersEdit':
case 'customersGet':
case 'customersEdit':
2020-04-24 13:18:18 +03:00
case 'customersСorporateGet':
2017-09-04 11:36:04 +03:00
return self::proxy($api, $methodApi, $method, array($params, 'externalId', $site));
2020-04-24 13:18:18 +03:00
case 'customersGetById':
return self::proxy($api, 'customersGet', $method, array($params, 'id', $site));
2018-10-18 12:07:10 +03:00
2017-10-24 11:41:11 +03:00
case 'paymentEditById':
return self::proxy($api, 'ordersPaymentEdit', $method, array($params, 'id', $site));
2018-10-18 12:07:10 +03:00
2017-10-24 11:41:11 +03:00
case 'paymentEditByExternalId':
return self::proxy($api, 'ordersPaymentEdit', $method, array($params, 'externalId', $site));
2018-10-18 12:07:10 +03:00
2016-09-15 16:42:10 +03:00
default:
2017-09-04 11:36:04 +03:00
return self::proxy($api, $methodApi, $method, array($params, $site));
2017-10-24 11:41:11 +03:00
}
2017-09-04 11:36:04 +03:00
}
2017-11-13 11:05:11 +03:00
2018-07-16 16:01:19 +03:00
private static function proxy($api, $methodApi, $method, $params) {
2017-09-04 11:36:04 +03:00
$version = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_VERSION, 0);
try {
$result = call_user_func_array(array($api, $methodApi), $params);
2016-09-15 16:42:10 +03:00
2020-04-24 13:18:18 +03:00
if (!$result) {
$err = new RuntimeException(
$methodApi . ": Got null instead of valid result!"
);
Logger::getInstance()->write(sprintf(
'%s%s%s',
$err->getMessage(),
PHP_EOL,
$err->getTraceAsString()
), 'apiErrors');
return false;
}
2017-09-04 11:36:04 +03:00
if ($result->getStatusCode() !== 200 && $result->getStatusCode() !== 201) {
2020-04-24 13:18:18 +03:00
if ($methodApi == 'ordersGet'
|| $methodApi == 'customersGet'
|| $methodApi == 'customersСorporateGet'
) {
Logger::getInstance()->write(array(
2017-09-04 11:36:04 +03:00
'api' => $version,
'methodApi' => $methodApi,
'errorMsg' => !empty($result['errorMsg']) ? $result['errorMsg'] : '',
'errors' => !empty($result['errors']) ? $result['errors'] : '',
'params' => $params
), 'apiErrors');
} elseif ($methodApi == 'customersUpload' || $methodApi == 'ordersUpload') {
2020-04-24 13:18:18 +03:00
Logger::getInstance()->write(array(
2017-09-04 11:36:04 +03:00
'api' => $version,
'methodApi' => $methodApi,
'errorMsg' => !empty($result['errorMsg']) ? $result['errorMsg'] : '',
'errors' => !empty($result['errors']) ? $result['errors'] : '',
'params' => $params
), 'uploadApiErrors');
} else {
2020-04-24 13:18:18 +03:00
self::eventLog(
__CLASS__ . '::' . $method,
'RetailCrm\ApiClient::' . $methodApi,
!empty($result['errorMsg']) ? $result['errorMsg'] : ''
);
Logger::getInstance()->write(array(
2017-09-04 11:36:04 +03:00
'api' => $version,
'methodApi' => $methodApi,
'errorMsg' => !empty($result['errorMsg']) ? $result['errorMsg'] : '',
'errors' => !empty($result['errors']) ? $result['errors'] : '',
'params' => $params
), 'apiErrors');
2016-09-15 16:42:10 +03:00
}
2017-11-13 11:05:11 +03:00
2017-09-04 11:36:04 +03:00
if (function_exists('retailCrmApiResult')) {
retailCrmApiResult($methodApi, false, $result->getStatusCode());
}
2017-11-13 11:05:11 +03:00
2017-09-04 11:36:04 +03:00
if ($result->getStatusCode() == 460) {
return true;
}
return false;
}
} catch (\RetailCrm\Exception\CurlException $e) {
2020-04-24 13:18:18 +03:00
static::logException(
$method,
$methodApi,
'CurlException',
'CurlException',
$e,
$version,
$params
2017-09-04 11:36:04 +03:00
);
return false;
} catch (InvalidArgumentException $e) {
2020-04-24 13:18:18 +03:00
static::logException(
$method,
$methodApi,
'InvalidArgumentException',
'ArgumentException',
$e,
$version,
$params
2017-09-04 11:36:04 +03:00
);
return false;
2018-07-16 16:01:19 +03:00
} catch (\RetailCrm\Exception\InvalidJsonException $e) {
2020-04-24 13:18:18 +03:00
static::logException(
$method,
$methodApi,
'InvalidJsonException',
'ArgumentException',
$e,
$version,
$params
2018-07-16 16:01:19 +03:00
);
2017-09-04 11:36:04 +03:00
}
2017-11-13 11:05:11 +03:00
2017-09-04 11:36:04 +03:00
if (function_exists('retailCrmApiResult')) {
2020-04-24 13:18:18 +03:00
retailCrmApiResult($methodApi, true, isset($result) ? $result->getStatusCode() : 0);
2017-09-04 11:36:04 +03:00
}
2017-11-13 11:05:11 +03:00
2018-07-16 16:01:19 +03:00
return isset($result) ? $result : false;
2016-09-15 16:42:10 +03:00
}
2020-04-24 13:18:18 +03:00
/**
* Log exception into log file and event log
*
* @param string $method
* @param string $methodApi
* @param string $exceptionName
* @param string $apiResultExceptionName
* @param \Exception|\Error|\Throwable $exception
* @param string $version
* @param array $params
*/
protected static function logException(
$method,
$methodApi,
$exceptionName,
$apiResultExceptionName,
$exception,
$version,
$params
) {
self::eventLog(
__CLASS__ . '::' . $method, 'RetailCrm\ApiClient::' . $methodApi . '::' . $exceptionName,
$exception->getCode() . ': ' . $exception->getMessage()
);
Logger::getInstance()->write(array(
'api' => $version,
'methodApi' => $methodApi,
'errorMsg' => $exception->getMessage(),
'errors' => $exception->getCode(),
'params' => $params
), 'apiErrors');
if (function_exists('retailCrmApiResult')) {
retailCrmApiResult($methodApi, false, $apiResultExceptionName);
}
}
2016-09-15 16:42:10 +03:00
}