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';
|
|
|
|
|
|
|
|
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()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$arSites[] = $ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arSites;
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function OrderTypesList($arSites)
|
|
|
|
{
|
2016-09-15 16:42: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $orderTypesList;
|
|
|
|
}
|
|
|
|
|
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();
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($arDeliveryServiceAll as $arDeliveryService) {
|
2016-11-15 17:17:09 +03:00
|
|
|
if (($arDeliveryService['PARENT_ID'] == '0' || $arDeliveryService['PARENT_ID'] == null) && $arDeliveryService['ID'] != $noOrderId) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$bitrixDeliveryTypesList[] = $arDeliveryService;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bitrixDeliveryTypesList;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bitrixPaymentTypesList;
|
|
|
|
}
|
|
|
|
|
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'),
|
|
|
|
'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
|
|
|
}
|
|
|
|
|
|
|
|
return $bitrixPaymentStatusesList;
|
|
|
|
}
|
|
|
|
|
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('*')
|
|
|
|
));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bitrixPropsList;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'RCrmActions::uploadOrdersAgent();';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @global $APPLICATION
|
|
|
|
* @param $str in SITE_CHARSET
|
|
|
|
* @return $str in utf-8
|
|
|
|
*/
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @global $APPLICATION
|
|
|
|
* @param $str in utf-8
|
|
|
|
* @return $str in SITE_CHARSET
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function explodeFIO($fio)
|
|
|
|
{
|
2016-09-15 16:42:10 +03:00
|
|
|
$newFio = empty($fio) ? false : explode(" ", $fio, 3);
|
|
|
|
$result = array();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function apiMethod($api, $methodApi, $method, $params, $site = null)
|
|
|
|
{
|
|
|
|
switch ($methodApi) {
|
2016-09-15 16:42:10 +03:00
|
|
|
case 'ordersGet':
|
|
|
|
case 'ordersEdit':
|
|
|
|
case 'customersGet':
|
|
|
|
case 'customersEdit':
|
|
|
|
try {
|
|
|
|
$result = $api->$methodApi($params, 'externalId', $site);
|
2016-10-31 17:56:11 +03:00
|
|
|
if (isset($result['errorMsg'])) {
|
|
|
|
self::eventLog(__CLASS__.'::'.$method, 'RetailCrm\ApiClient::'.$methodApi, $result['errorMsg']);
|
|
|
|
|
|
|
|
$log = new Logger();
|
|
|
|
$log->write(array($methodApi, $result['errorMsg'], $result['errors'], $params), 'apiErrors');
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
|
|
|
self::eventLog(
|
|
|
|
__CLASS__.'::'.$method, 'RetailCrm\ApiClient::'.$methodApi.'::CurlException',
|
|
|
|
$e->getCode() . ': ' . $e->getMessage()
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
self::eventLog(
|
|
|
|
__CLASS__.'::'.$method, 'RetailCrm\ApiClient::'.$methodApi.'::InvalidArgumentException',
|
|
|
|
$e->getCode() . ': ' . $e->getMessage()
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
default:
|
|
|
|
try {
|
|
|
|
$result = $api->$methodApi($params, $site);
|
2016-10-31 17:56:11 +03:00
|
|
|
if (isset($result['errorMsg'])) {
|
|
|
|
if ($methodApi != 'customersUpload') {
|
|
|
|
self::eventLog(__CLASS__.'::'.$method, 'RetailCrm\ApiClient::'.$methodApi, $result['errorMsg']);
|
|
|
|
}
|
|
|
|
$log = new Logger();
|
|
|
|
$log->write(array($methodApi, $result['errorMsg'], $result['errors'], $params), 'apiErrors');
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
|
|
|
self::eventLog(
|
|
|
|
__CLASS__.'::'.$method, 'RetailCrm\ApiClient::'.$methodApi.'::CurlException',
|
|
|
|
$e->getCode() . ': ' . $e->getMessage()
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
self::eventLog(
|
|
|
|
__CLASS__.'::'.$method, 'RetailCrm\ApiClient::'.$methodApi.'::InvalidArgumentException',
|
|
|
|
$e->getCode() . ': ' . $e->getMessage()
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|