1
0
mirror of synced 2024-11-27 07:46:09 +03:00
bitrix-module/intaro.intarocrm/classes/general/ICrmOrderActions.php

1543 lines
68 KiB
PHP
Raw Normal View History

2013-10-30 17:01:34 +04:00
<?php
2013-12-30 00:37:29 +04:00
IncludeModuleLangFile(__FILE__);
2013-10-30 17:01:34 +04:00
class ICrmOrderActions
{
protected static $MODULE_ID = 'intaro.intarocrm';
protected static $CRM_API_HOST_OPTION = 'api_host';
protected static $CRM_API_KEY_OPTION = 'api_key';
protected static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
protected static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
protected static $CRM_PAYMENT_TYPES = 'pay_types_arr';
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
protected static $CRM_ORDER_SITES = 'sites_ids';
protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
protected static $CRM_ORDER_HISTORY_DATE = 'order_history_date';
2014-05-22 17:48:44 +04:00
protected static $CRM_MULTISHIP_INTEGRATION_CODE = 'multiship';
protected static $MUTLISHIP_DELIVERY_TYPE = 'mlsp';
protected static $MULTISHIP_MODULE_VER = 'multiship.v2';
2013-10-30 17:01:34 +04:00
2013-12-25 16:30:46 +04:00
const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED';
2013-10-30 17:01:34 +04:00
/**
* Mass order uploading, without repeating; always returns true, but writes error log
2014-01-28 17:44:21 +04:00
* @param $pSize
2013-10-30 17:01:34 +04:00
* @param $failed -- flag to export failed orders
* @return boolean
*/
public static function uploadOrders($pSize = 50, $failed = false) {
2014-11-24 20:15:54 +03:00
// COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
2013-10-30 17:01:34 +04:00
if (!CModule::IncludeModule("iblock")) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found');
return true;
}
if (!CModule::IncludeModule("sale")) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found');
return true;
}
if (!CModule::IncludeModule("catalog")) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found');
return true;
}
$resOrders = array();
$resCustomers = array();
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
$lastOrderId = 0;
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if (!$failedIds)
$failedIds = array();
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId));
$dbFailedOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('ID' => $failedIds));
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
//saved cat params
$optionsOrderTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0));
$optionsDelivTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_DELIVERY_TYPES_ARR, 0));
$optionsPayTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0));
$optionsPayStatuses = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0)); // --statuses
$optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0));
$optionsSites = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_SITES, 0));
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
$api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionSites' => $optionsSites,
'optionsOrderProps' => $optionsOrderProps
);
if (!$failed) {
//packmode
$orderCount = 0;
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
if (is_array($optionsSites))
if (!empty($optionsSites))
if (!in_array($arOrder['LID'], $optionsSites))
continue;
$result = self::orderCreate($arOrder, $api, $arParams);
if (!$result['order'] || !$result['customer'])
continue;
$orderCount++;
$resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$lastOrderId = $arOrder['ID'];
if ($orderCount >= $pSize) {
2014-03-22 14:52:17 +04:00
try {
$customers = $api->customerUpload($resCustomers);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload',
$e->getCode() . ': ' . $e->getMessage()
);
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2013-10-30 17:01:34 +04:00
2014-03-22 14:52:17 +04:00
return false;
2013-10-30 17:01:34 +04:00
}
2014-03-22 14:52:17 +04:00
try {
$orders = $api->orderUpload($resOrders);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload',
$e->getCode() . ': ' . $e->getMessage()
);
2013-10-30 17:01:34 +04:00
2014-03-22 14:52:17 +04:00
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2013-10-30 17:01:34 +04:00
2014-03-22 14:52:17 +04:00
return false;
2013-10-30 17:01:34 +04:00
}
if ($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
return true; // end of pack
}
}
if (!empty($resOrders)) {
2014-03-22 14:52:17 +04:00
try {
$customers = $api->customerUpload($resCustomers);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload',
$e->getCode() . ': ' . $e->getMessage()
);
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
2013-10-30 17:01:34 +04:00
}
2014-03-22 14:52:17 +04:00
try {
$orders = $api->orderUpload($resOrders);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload',
$e->getCode() . ': ' . $e->getMessage()
);
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
2013-10-30 17:01:34 +04:00
}
}
if ($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
} else {
2013-12-25 16:30:46 +04:00
// failed orders upload
2013-10-30 17:01:34 +04:00
$orderCount = 0;
$recOrders = array();
while ($arOrder = $dbFailedOrder->GetNext()) { // here orders by id asc
if (is_array($optionsSites))
if (!empty($optionsSites))
if (!in_array($arOrder['LID'], $optionsSites))
continue;
$result = self::orderCreate($arOrder, $api, $arParams);
if (!$result['order'] || !$result['customer'])
continue;
$orderCount++;
$resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$recOrders[] = $arOrder['ID'];
if ($orderCount >= $pSize) {
2014-03-22 14:52:17 +04:00
try {
$customers = $api->customerUpload($resCustomers);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload',
$e->getCode() . ': ' . $e->getMessage()
);
2013-10-30 17:01:34 +04:00
2014-03-22 14:52:17 +04:00
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2013-10-30 17:01:34 +04:00
2014-03-22 14:52:17 +04:00
return false;
2013-10-30 17:01:34 +04:00
}
2014-03-22 14:52:17 +04:00
try {
$orders = $api->orderUpload($resOrders);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload',
$e->getCode() . ': ' . $e->getMessage()
);
2013-10-30 17:01:34 +04:00
2014-03-22 14:52:17 +04:00
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2013-10-30 17:01:34 +04:00
2014-03-22 14:52:17 +04:00
return false;
2013-10-30 17:01:34 +04:00
}
if (!empty($recOrders)) {
$failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
return true; // end of pack
}
}
if (!empty($resOrders)) {
2014-03-22 14:52:17 +04:00
try {
$customers = $api->customerUpload($resCustomers);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload',
$e->getCode() . ': ' . $e->getMessage()
);
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
2013-10-30 17:01:34 +04:00
}
2014-03-22 14:52:17 +04:00
try {
$orders = $api->orderUpload($resOrders);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload',
$e->getCode() . ': ' . $e->getMessage()
);
if($e->getCode() != 201)
if($e->getCode() != 460)
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
2013-10-30 17:01:34 +04:00
}
}
if (!empty($recOrders)) {
$failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
}
return true; //all ok!
}
2013-12-25 16:30:46 +04:00
protected static function updateCancelProp($arProduct, $value) {
$propUpdated = false;
foreach($arProduct['PROPS'] as $key => $item) {
if ($item['CODE'] == self::CANCEL_PROPERTY_CODE) {
$arProduct['PROPS'][$key]['VALUE'] = $value;
$propUpdated = true;
break;
}
}
if (!$propUpdated) {
$arProduct['PROPS'][] = array(
2013-12-29 21:27:23 +04:00
'NAME' => GetMessage('PRODUCT_CANCEL'),
2013-12-25 16:30:46 +04:00
'CODE' => self::CANCEL_PROPERTY_CODE,
'VALUE' => $value,
'SORT' => 10,
);
}
return $arProduct;
}
2013-10-30 17:01:34 +04:00
/**
2013-12-25 16:30:46 +04:00
*
2014-11-24 20:15:54 +03:00
* History update, cron usage only
2013-10-30 17:01:34 +04:00
* @global CUser $USER
* @return boolean
*/
public static function orderHistory() {
global $USER;
2013-12-25 16:30:46 +04:00
2013-12-26 20:40:11 +04:00
if(isset($_SESSION["SESS_AUTH"]["USER_ID"]) && $_SESSION["SESS_AUTH"]["USER_ID"]) {
2013-12-26 01:49:50 +04:00
$realUser = $USER->GetID();
$USER->Logout();
} else { // for agent; to add order User
// find similar login! (email / name can be used as login, due to settings diff)
2014-10-28 15:32:58 +03:00
$rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => '%intarocrm%'));
2013-12-25 16:30:46 +04:00
2013-11-28 15:08:07 +04:00
if($arUser = $rsUser->Fetch()) {
$USER = new CUser;
2013-12-26 01:49:50 +04:00
$USER->Authorize($arUser['ID']);
2013-11-28 15:08:07 +04:00
} else {
$login = 'intarocrm';
$serverName = 0 < strlen(SITE_SERVER_NAME)? SITE_SERVER_NAME : 'server.com';
2013-11-28 17:02:21 +04:00
$email = $login . '@' . $serverName;
2013-11-28 15:08:07 +04:00
$userPassword = randString(10);
$user = new CUser;
$arFields = array(
"NAME" => $login,
"LAST_NAME" => $login,
"EMAIL" => $email,
"LOGIN" => $login,
"LID" => "ru",
"ACTIVE" => "Y",
"GROUP_ID" => array(2),
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
);
$id = $user->Add($arFields);
2013-12-25 16:30:46 +04:00
2013-11-28 15:08:07 +04:00
if (!$id) {
self::eventLog('ICrmOrderActions::orderHistory', 'USER', $user->LAST_ERROR);
return;
}
$USER = new CUser;
2013-12-26 01:49:50 +04:00
$USER->Authorize($id);
2013-11-28 15:08:07 +04:00
}
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if (!CModule::IncludeModule("iblock")) {
//handle err
self::eventLog('ICrmOrderActions::orderHistory', 'iblock', 'module not found');
return true;
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if (!CModule::IncludeModule("sale")) {
//handle err
self::eventLog('ICrmOrderActions::orderHistory', 'sale', 'module not found');
return true;
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if (!CModule::IncludeModule("catalog")) {
//handle err
self::eventLog('ICrmOrderActions::orderHistory', 'catalog', 'module not found');
return true;
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$defaultSiteId = 0;
$rsSites = CSite::GetList($by, $sort, array('DEF' => 'Y'));
2013-11-15 15:55:54 +04:00
while ($ar = $rsSites->Fetch()) {
2013-10-30 17:01:34 +04:00
$defaultSiteId = $ar['LID'];
2013-11-15 15:55:54 +04:00
break;
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
//saved cat params (crm -> bitrix)
$optionsOrderTypes = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0)));
$optionsDelivTypes = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_DELIVERY_TYPES_ARR, 0)));
$optionsPayTypes = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0)));
$optionsPayStatuses = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0))); // --statuses
$optionsPayment = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0)));
$optionsSites = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_SITES, 0));
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
$api = new IntaroCrm\RestApi($api_host, $api_key);
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$dateStart = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY_DATE, null);
2014-01-28 12:44:20 +04:00
if(!$dateStart) {
$dateStart = new \DateTime();
$dateStart = $dateStart->format('Y-m-d H:i:s');
}
2014-03-22 14:52:17 +04:00
try {
$orderHistory = $api->orderHistory($dateStart);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderHistory',
$e->getCode() . ': ' . $e->getMessage()
);
return true;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderHistory::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
return true;
}
2013-12-25 16:30:46 +04:00
2014-03-22 14:52:17 +04:00
$dateFinish = $api->getGeneratedAt();
2014-03-13 12:58:52 +04:00
2014-07-09 14:42:45 +04:00
// default orderType
$defaultOrderType = 1;
// if it not a ph. entity
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
if ($arOrderTypesList = $dbOrderTypesList->Fetch())
$defaultOrderType = $arOrderTypesList['ID'];
2014-05-19 13:41:10 +04:00
// apiv3 !
if(!$dateFinish) $dateFinish = new \DateTime();
2014-03-13 13:37:16 +04:00
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = true;
2014-03-13 13:21:43 +04:00
// clear intarocrm user basket
CSaleBasket::DeleteAll($USER->GetID());
2013-10-30 17:01:34 +04:00
// pushing existing orders
foreach ($orderHistory as $order) {
if(function_exists('intarocrm_order_pre_persist')) {
$order = intarocrm_order_pre_persist($order);
}
2013-10-30 17:01:34 +04:00
2014-01-27 19:14:43 +04:00
if(!isset($order['externalId']) || !$order['externalId']) {
2013-10-30 17:01:34 +04:00
// custom orderType function
2014-02-12 11:06:32 +04:00
if(function_exists('intarocrm_set_order_type')) {
$orderType = intarocrm_set_order_type($order);
if($orderType)
$optionsOrderTypes[$order['orderType']] = $orderType;
2014-07-09 14:42:45 +04:00
else
$optionsOrderTypes[$order['orderType']] = $defaultOrderType;
2014-02-12 11:06:32 +04:00
}
2013-10-31 10:49:20 +04:00
// we dont need new orders without any customers (can check only for externalId)
2014-01-28 12:44:20 +04:00
if(!isset($order['customer']['externalId']) && !$order['customer']['externalId']) {
2014-07-22 16:30:06 +04:00
if(!isset($order['customer']['id']) || !$order['customer']['id']) continue; // if no id then skip
2014-01-29 18:42:52 +04:00
if (!$order['customer']['email']) {
2014-02-01 17:02:40 +04:00
$login = 'user_' . (microtime(true) * 10000) . mt_rand(1, 1000);
2014-01-29 18:42:52 +04:00
$server_name = 0 < strlen(SITE_SERVER_NAME)?
SITE_SERVER_NAME : 'server.com';
$order['customer']['email'] = $login . '@' . $server_name;
$registerNewUser = true;
} else {
// if email already used
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email']));
if ($dbUser->SelectedRowsCount() == 0) {
$login = $order['customer']['email'];
2014-01-28 12:44:20 +04:00
$registerNewUser = true;
2014-01-29 18:42:52 +04:00
} elseif ($dbUser->SelectedRowsCount() == 1) {
$arUser = $dbUser->Fetch();
$registeredUserID = $arUser['ID'];
2014-01-28 12:44:20 +04:00
} else {
2014-02-01 17:02:40 +04:00
$login = 'user_' . (microtime(true) * 10000) . mt_rand(1, 1000);
2014-01-29 18:42:52 +04:00
$registerNewUser = true;
2014-01-28 12:44:20 +04:00
}
2014-01-29 18:42:52 +04:00
}
if($registerNewUser) {
$useCaptcha = COption::GetOptionString('main', 'captcha_registration', 'N');
if ($useCaptcha == 'Y')
COption::SetOptionString('main', 'captcha_registration', 'N');
$userPassword = randString(10);
$newUser = $USER->Register($login, self::fromJSON($order['customer']['firstName']), self::fromJSON($order['customer']['lastName']),
2014-01-29 18:42:52 +04:00
$userPassword, $userPassword, $order['customer']['email']);
if ($useCaptcha == 'Y')
COption::SetOptionString('main', 'captcha_registration', 'Y');
if ($newUser['TYPE'] == 'ERROR') {
self::eventLog('ICrmOrderActions::orderHistory', 'CUser::Register', $newUser['MESSAGE']);
continue;
} else {
$registeredUserID = $USER->GetID();
$USER->Logout();
2014-01-28 12:44:20 +04:00
}
}
2014-01-29 18:42:52 +04:00
$order['customer']['externalId'] = $registeredUserID;
}
2014-01-28 12:44:20 +04:00
2014-03-22 14:52:17 +04:00
try {
$api->customerFixExternalIds(array(array('id' => $order['customer']['id'], 'externalId' => $order['customer']['externalId'])));
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds',
$e->getCode() . ': ' . $e->getMessage()
);
continue;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2014-01-29 18:42:52 +04:00
continue;
2014-01-28 12:44:20 +04:00
}
2013-10-30 17:01:34 +04:00
// new order
$newOrderFields = array(
2013-10-31 10:49:20 +04:00
'LID' => $defaultSiteId,
2014-07-09 14:42:45 +04:00
'PERSON_TYPE_ID' => ($optionsOrderTypes[$order['orderType']]) ? $optionsOrderTypes[$order['orderType']] : $defaultOrderType,
2013-10-30 17:01:34 +04:00
'PAYED' => 'N',
'CANCELED' => 'N',
'STATUS_ID' => 'N',
'PRICE' => 0,
'CURRENCY' => 'RUB',
2013-10-31 10:49:20 +04:00
'USER_ID' => $order['customer']['externalId'],
2013-10-30 17:01:34 +04:00
'PAY_SYSTEM_ID' => 0,
'PRICE_DELIVERY' => 0,
'DELIVERY_ID' => 0,
'DISCOUNT_VALUE' => 0,
'USER_DESCRIPTION' => ''
);
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if(isset($order['number']) && $order['number'])
$GLOBALS['ICRM_ACCOUNT_NUMBER'] = $order['number'];
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$order['externalId'] = CSaleOrder::Add($newOrderFields);
if(isset($GLOBALS['ICRM_ACCOUNT_NUMBER']))
unset($GLOBALS['ICRM_ACCOUNT_NUMBER']);
2014-03-22 14:52:17 +04:00
try {
$api->orderFixExternalIds(array(array('id' => $order['id'], 'externalId' => $order['externalId'])));
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds',
$e->getCode() . ': ' . $e->getMessage()
);
continue;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
continue;
2013-12-25 16:30:46 +04:00
}
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
if(isset($order['externalId']) && $order['externalId']) {
2014-02-12 09:44:35 +04:00
// custom orderType function
2014-02-12 11:06:32 +04:00
if(function_exists('intarocrm_set_order_type')) {
2014-02-12 09:44:35 +04:00
$orderType = intarocrm_set_order_type($order);
if($orderType)
$optionsOrderTypes[$order['orderType']] = $orderType;
2014-07-09 14:42:45 +04:00
else
$optionsOrderTypes[$order['orderType']] = $defaultOrderType;
2014-02-12 09:44:35 +04:00
}
2013-10-30 17:01:34 +04:00
$arFields = CSaleOrder::GetById($order['externalId']);
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
// incorrect order
if(!$arFields || empty($arFields))
continue;
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$LID = $arFields['LID'];
2013-10-31 10:49:20 +04:00
$userId = $arFields['USER_ID'];
2013-12-25 16:30:46 +04:00
if(isset($order['customer']['externalId']) && $order['customer']['externalId'])
2013-10-31 10:49:20 +04:00
$userId = $order['customer']['externalId'];
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID']));
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
while ($ar = $rsOrderProps->Fetch()) {
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']) && isset($order['delivery']['address']) && $order['delivery']['address']) {
2013-10-30 17:01:34 +04:00
switch ($ar['CODE']) {
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index']: if (isset($order['delivery']['address']['index']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['index'])));
break;
2014-05-19 13:41:10 +04:00
case ($ar['CODE'] == 'CITY') or ($ar['CODE'] == 'LOCATION'): if (isset($order['delivery']['address']['city'])) {
2014-04-16 12:27:34 +04:00
$prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']);
if($prop['TYPE'] == 'LOCATION') {
2014-05-19 13:41:10 +04:00
$cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city']));
2014-04-16 12:27:34 +04:00
if (!$cityId)
break;
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => $cityId));
break;
}
2014-05-19 13:41:10 +04:00
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['city'])));
2014-04-16 12:27:34 +04:00
}
2013-10-30 17:01:34 +04:00
break;
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['text']: if (isset($order['delivery']['address']['text']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['text'])));
2013-10-30 17:01:34 +04:00
break;
}
2014-01-28 17:44:21 +04:00
if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) {
2013-10-30 17:01:34 +04:00
switch ($ar['CODE']) {
/* case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']);
break;
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['region']: $resOrderDeliveryAddress['region'] = self::toJSON($ar['VALUE']);
break;
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['city']: $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
break; */
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['street']: if (isset($order['delivery']['address']['street']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['street'])));
2013-10-30 17:01:34 +04:00
break;
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['building']: if (isset($order['delivery']['address']['building']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['building'])));
2013-10-30 17:01:34 +04:00
break;
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['flat']: if (isset($order['delivery']['address']['flat']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['flat'])));
2013-10-30 17:01:34 +04:00
break;
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['intercomcode']: if (isset($order['delivery']['address']['intercomcode']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['intercomcode'])));
2013-10-30 17:01:34 +04:00
break;
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['floor']: if (isset($order['delivery']['address']['floor']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['floor'])));
2013-10-30 17:01:34 +04:00
break;
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['block']: if (isset($order['delivery']['address']['block']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['block'])));
2013-10-30 17:01:34 +04:00
break;
2014-05-19 13:41:10 +04:00
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['house']: if (isset($order['delivery']['address']['house']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['delivery']['address']['house'])));
2013-10-30 17:01:34 +04:00
break;
}
}
}
switch ($ar['CODE']) {
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['fio']:
$contactName = array(); // cleanup
2013-10-31 10:49:20 +04:00
if (isset($order['lastName']))
$contactName['lastName'] = self::fromJSON($order['lastName']);
2014-09-15 11:49:44 +04:00
if (isset($order['firstName']))
$contactName['firstName'] = self::fromJSON($order['firstName']);
2013-10-31 10:49:20 +04:00
if (isset($order['patronymic']))
$contactName['patronymic'] = self::fromJSON($order['patronymic']);
if (!isset($contactName) || empty($contactName))
break;
2013-10-30 17:01:34 +04:00
2013-10-31 10:49:20 +04:00
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => implode(" ", $contactName)));
2013-10-30 17:01:34 +04:00
break;
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['phone']: if (isset($order['phone']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['phone'])));
break;
case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['email']: if (isset($order['email']))
CSaleOrderPropsValue::Update($ar['ID'], array('VALUE' => self::fromJSON($order['email'])));
break;
}
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
// here check if smth wasnt added or new propetties
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']) && isset($order['delivery']['address']) && $order['delivery']['address']) {
if (isset($order['delivery']['address']['index']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['index'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['index']), $order['externalId']);
2013-12-25 16:30:46 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['city'])) {
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['city'], self::fromJSON($order['delivery']['address']['city']), $order['externalId']);
self::addOrderProperty('CITY', self::fromJSON($order['delivery']['address']['city']), $order['externalId']);
2014-02-01 12:11:42 +04:00
2014-05-19 13:41:10 +04:00
$cityId = self::getLocationCityId(self::fromJSON($order['delivery']['address']['city']));
2014-02-01 12:11:42 +04:00
if ($cityId)
self::addOrderProperty('LOCATION', $cityId, $order['externalId']);
else
self::addOrderProperty('LOCATION', 0, $order['externalId']);
}
2013-10-30 17:01:34 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['text']))
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['text'], self::fromJSON($order['delivery']['address']['text']), $order['externalId']);
2013-10-30 17:01:34 +04:00
2014-01-28 17:44:21 +04:00
if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) {
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['street']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['street'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['street']), $order['externalId']);
2013-12-25 16:30:46 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['building']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['building'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['bulding']), $order['externalId']);
2013-12-25 16:30:46 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['flat']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['flat'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['flat']), $order['externalId']);
2013-12-25 16:30:46 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['intercomcode']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['intercomcode'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['intercomcode']), $order['externalId']);
2013-10-30 17:01:34 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['floor']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['floor'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['floor']), $order['externalId']);
2013-12-25 16:30:46 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['block']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['block'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['block']), $order['externalId']);
2013-12-25 16:30:46 +04:00
2014-05-19 13:41:10 +04:00
if (isset($order['delivery']['address']['house']))
2013-12-25 16:30:46 +04:00
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['house'],
2014-05-19 13:41:10 +04:00
self::fromJSON($order['delivery']['address']['house']), $order['externalId']);
2013-10-30 17:01:34 +04:00
}
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if (isset($order['phone']))
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['phone'],
self::fromJSON($order['phone']), $order['externalId']);
if (isset($order['email']))
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['email'],
self::fromJSON($order['email']), $order['externalId']);
$contactName = array(); // cleanup
2013-10-30 17:01:34 +04:00
if (isset($order['firstName']))
$contactName['firstName'] = self::fromJSON($order['firstName']);
if (isset($order['lastName']))
$contactName['lastName'] = self::fromJSON($order['lastName']);
if (isset($order['patronymic']))
$contactName['patronymic'] = self::fromJSON($order['patronymic']);
if (isset($contactName) && !empty($contactName))
self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['fio'],
implode(" ", $contactName), $order['externalId']);
foreach($order['items'] as $item) {
// del from basket
if(isset($item['deleted']) && $item['deleted']) {
$p = CSaleBasket::GetList(
array('PRODUCT_ID' => 'ASC'),
array('ORDER_ID' => $order['externalId'], 'PRODUCT_ID' => $item['id']))->Fetch();
if($p)
CSaleBasket::Delete($p['ID']);
continue;
}
if(!isset($item['offer']) && !$item['offer']['externalId'])
continue;
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$p = CSaleBasket::GetList(
array('PRODUCT_ID' => 'ASC'),
2013-12-25 16:30:46 +04:00
array('ORDER_ID' => $order['externalId'], 'PRODUCT_ID' => $item['offer']['externalId'])
)->Fetch();
if (!$p) {
2013-10-30 17:01:34 +04:00
$p = CIBlockElement::GetByID($item['offer']['externalId'])->Fetch();
// select iblock to obtain an CATALOG_XML_ID
$iblock = CIBlock::GetByID($p['IBLOCK_ID'])->Fetch();
$p['CATALOG_XML_ID'] = $iblock['XML_ID'];
// product field XML_ID is called PRODUCT_XML_ID in basket
$p['PRODUCT_XML_ID'] = $p['XML_ID'];
unset($p['XML_ID']);
2013-12-25 16:30:46 +04:00
}
else {
//for basket props updating (in props we save cancel status)
$propResult = CSaleBasket::GetPropsList(
2013-12-25 18:38:25 +04:00
array(''),
array('BASKET_ID' => $p['ID']),
false,
false,
array('NAME', 'CODE', 'VALUE', 'SORT')
2013-12-25 16:30:46 +04:00
);
while($r = $propResult->Fetch()) {
$p['PROPS'][] = $r;
}
}
2013-10-30 17:01:34 +04:00
// change existing basket items
$arProduct = array();
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
// create new
if(isset($item['created']) && $item['created']) {
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$productPrice = GetCatalogProductPrice($item['offer']['externalId'], 1);
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$arProduct = array(
'FUSER_ID' => $userId,
'ORDER_ID' => $order['externalId'],
'QUANTITY' => $item['quantity'],
'CURRENCY' => $productPrice['CURRENCY'],
'LID' => $LID,
'PRODUCT_ID' => $item['offer']['externalId'],
'PRODUCT_PRICE_ID' => $p['PRODUCT_PRICE_ID'],
'WEIGHT' => $p['WEIGHT'],
'DELAY' => $p['DELAY'],
'CAN_BUY' => $p['CAN_BUY'],
'MODULE' => $p['MODULE'],
2013-12-25 18:38:25 +04:00
'NOTES' => $item['comment'] ?: $p['NOTES'],
2013-10-30 17:01:34 +04:00
'PRODUCT_PROVIDER_CLASS' => $p['PRODUCT_PROVIDER_CLASS'],
'DETAIL_PAGE_URL' => $p['DETAIL_PAGE_URL'],
'CATALOG_XML_ID' => $p['CATALOG_XML_ID'],
2013-11-28 15:08:07 +04:00
'PRODUCT_XML_ID' => $p['PRODUCT_XML_ID'],
'CUSTOM_PRICE' => 'Y'
2013-10-30 17:01:34 +04:00
);
if (isset($item['initialPrice']) && $item['initialPrice'])
$arProduct['PRICE'] = (double) $item['initialPrice'];
2013-11-14 16:41:19 +04:00
if (isset($item['discount'])) {
2013-12-25 16:30:46 +04:00
$arProduct['DISCOUNT_PRICE'] = $item['discount'];
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
if (isset($item['discountPercent'])) {
2013-10-30 17:01:34 +04:00
$arProduct['DISCOUNT_VALUE'] = $item['discountPercent'];
$newPrice = floor ($arProduct['PRICE'] / 100 * (100 - $arProduct['DISCOUNT_VALUE']));
2013-11-15 17:26:51 +04:00
$arProduct['DISCOUNT_PRICE'] = $arProduct['DISCOUNT_PRICE'] + $arProduct['PRICE'] - $newPrice;
2013-10-30 17:01:34 +04:00
}
2013-11-15 17:26:51 +04:00
if(isset($item['discount']) || isset($item['discountPercent']))
2013-12-25 16:30:46 +04:00
$arProduct['PRICE'] -= $arProduct['DISCOUNT_PRICE'];
2013-11-15 17:26:51 +04:00
2013-10-30 17:01:34 +04:00
if (isset($item['offer']['name']) && $item['offer']['name'])
2013-11-28 15:08:07 +04:00
$arProduct['NAME'] = self::fromJSON($item['offer']['name']);
2013-10-30 17:01:34 +04:00
2013-12-25 16:30:46 +04:00
if (isset($item['isCanceled'])) {
//for product excluding from order
$arProduct['PRICE'] = 0;
2013-12-25 18:38:25 +04:00
$arProduct = self::updateCancelProp($arProduct, 1);
2013-12-25 16:30:46 +04:00
}
2013-10-30 17:01:34 +04:00
CSaleBasket::Add($arProduct);
continue;
}
2013-12-25 16:30:46 +04:00
$arProduct['PROPS'] = $p['PROPS'];
2013-10-30 17:01:34 +04:00
2013-12-25 18:38:25 +04:00
if (!isset($item['isCanceled'])) {
2013-12-25 16:30:46 +04:00
// update old
if (isset($item['initialPrice']) && $item['initialPrice'])
$arProduct['PRICE'] = (double) $item['initialPrice'];
if (isset($item['discount'])) {
$arProduct['DISCOUNT_PRICE'] = $item['discount'];
}
2013-10-30 17:01:34 +04:00
2013-12-25 16:30:46 +04:00
if (isset($item['discountPercent'])) {
$arProduct['DISCOUNT_VALUE'] = $item['discountPercent'];
$newPrice = floor ($arProduct['PRICE'] / 100 * (100 - $arProduct['DISCOUNT_VALUE']));
$arProduct['DISCOUNT_PRICE'] = $arProduct['DISCOUNT_PRICE'] + $arProduct['PRICE'] - $newPrice;
}
if(isset($item['discount']) || isset($item['discountPercent']))
$arProduct['PRICE'] -= $arProduct['DISCOUNT_PRICE'];
2013-12-25 18:38:25 +04:00
$arProduct = self::updateCancelProp($arProduct, 0);
2013-12-25 16:30:46 +04:00
}
else {
//for product excluding from order
$arProduct['PRICE'] = 0;
2013-12-25 18:38:25 +04:00
$arProduct = self::updateCancelProp($arProduct, 1);
2013-11-15 17:26:51 +04:00
}
2013-10-30 17:01:34 +04:00
if (isset($item['quantity']) && $item['quantity'])
$arProduct['QUANTITY'] = $item['quantity'];
if (isset($item['offer']['name']) && $item['offer']['name'])
2013-11-28 15:08:07 +04:00
$arProduct['NAME'] = self::fromJSON($item['offer']['name']);
2013-10-30 17:01:34 +04:00
CSaleBasket::Update($p['ID'], $arProduct);
CSaleBasket::DeleteAll($userId);
2013-12-25 16:30:46 +04:00
}
2013-10-30 17:01:34 +04:00
2014-07-17 11:06:42 +04:00
if(!isset($order['delivery']) || !isset($order['delivery']['cost']))
$order['delivery']['cost'] = $arFields['PRICE_DELIVERY'];
2013-11-06 16:01:25 +04:00
2014-02-01 12:11:42 +04:00
if(!isset($order['summ']) || (isset($order['summ']) && !$order['summ'] && $order['summ'] !== 0))
2013-11-14 16:41:19 +04:00
$order['summ'] = $arFields['PRICE'] - $arFields['PRICE_DELIVERY'];
$wasCanaceled = false;
if($arFields['CANCELED'] == 'Y')
2013-12-25 16:30:46 +04:00
$wasCanaceled = true;
2013-11-13 23:05:19 +04:00
2014-05-19 13:41:10 +04:00
$resultDeliveryTypeId = $optionsDelivTypes[$order['delivery']['code']];
2014-01-27 19:14:43 +04:00
2014-05-19 13:41:10 +04:00
if(isset($order['delivery']['service']) && !empty($order['delivery']['service'])) {
if (strpos($order['delivery']['service']['code'], "-") !== false)
$deliveryServiceCode = explode("-", $order['delivery']['service']['code'], 2);
2014-01-27 19:14:43 +04:00
if ($deliveryServiceCode)
$resultDeliveryTypeId = $resultDeliveryTypeId . ':' . $deliveryServiceCode[1];
}
2014-05-22 17:48:44 +04:00
if(isset($order['delivery']) && $order['delivery'] && isset($order['delivery']['integrationCode']) &&
$order['delivery']['integrationCode'] == self::$CRM_MULTISHIP_INTEGRATION_CODE &&
2014-05-22 18:29:03 +04:00
isset($order['delivery']['data']) && $order['delivery']['data'] &&
isset($order['delivery']['data']['service']) && $order['delivery']['data']['service']) {
2014-05-22 17:48:44 +04:00
if(CModule::IncludeModule(self::$MULTISHIP_MODULE_VER)) {
2014-05-22 18:29:03 +04:00
$resultDeliveryTypeId = $resultDeliveryTypeId . ':' . $order['delivery']['data']['service'];
2014-05-22 17:48:44 +04:00
}
}
2013-10-30 17:01:34 +04:00
// orderUpdate
$arFields = self::clearArr(array(
2014-07-17 11:06:42 +04:00
'PRICE_DELIVERY' => $order['delivery']['cost'],
2014-05-19 13:41:10 +04:00
'PRICE' => $order['summ'] + (double) $order['delivery']['cost'],
2013-10-30 17:01:34 +04:00
'DATE_MARKED' => $order['markDatetime'],
'USER_ID' => $userId, //$order['customer']
'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']],
2013-11-09 01:12:20 +04:00
//'PAYED' => $optionsPayment[$order['paymentStatus']],
2013-10-30 17:01:34 +04:00
//'PERSON_TYPE_ID' => $optionsOrderTypes[$order['orderType']],
2014-01-27 19:14:43 +04:00
'DELIVERY_ID' => $resultDeliveryTypeId,
'STATUS_ID' => $optionsPayStatuses[$order['status']],
'REASON_CANCELED' => self::fromJSON($order['statusComment']),
'USER_DESCRIPTION' => self::fromJSON($order['customerComment']),
'COMMENTS' => self::fromJSON($order['managerComment'])
2013-10-30 17:01:34 +04:00
));
2013-12-25 16:30:46 +04:00
if(!empty($arFields))
CSaleOrder::Update($order['externalId'], $arFields);
2013-10-30 17:01:34 +04:00
if(isset($order['status']) && $order['status']) {
if(isset($optionsPayStatuses[$order['status']]) && $optionsPayStatuses[$order['status']]) {
// set STATUS_ID
CSaleOrder::StatusOrder($order['externalId'], $optionsPayStatuses[$order['status']]);
// uncancel order
if($wasCanaceled && ($optionsPayStatuses[$order['status']] != 'YY'))
CSaleOrder::CancelOrder($order['externalId'], "N", $order['statusComment']);
2013-11-14 16:41:19 +04:00
// cancel order
if($optionsPayStatuses[$order['status']] == 'YY')
CSaleOrder::CancelOrder($order['externalId'], "Y", $order['statusComment']);
}
}
2013-11-09 01:12:20 +04:00
// set PAYED
if(isset($order['paymentStatus']) && $order['paymentStatus'] && $optionsPayment[$order['paymentStatus']])
2013-11-15 16:51:57 +04:00
CSaleOrder::PayOrder($order['externalId'], $optionsPayment[$order['paymentStatus']]);
if(function_exists('intarocrm_order_post_persist')) {
intarocrm_order_post_persist($order);
}
2013-12-25 16:30:46 +04:00
}
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if(count($orderHistory))
2014-03-13 13:00:51 +04:00
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY_DATE, $dateFinish->format('Y-m-d H:i:s'));
2013-12-25 16:30:46 +04:00
// clear intarocrm user basket
CSaleBasket::DeleteAll($USER->GetID());
2013-12-26 01:49:50 +04:00
$USER->Logout();
if($realUser) $USER->Authorize($realUser);
2014-03-13 13:21:43 +04:00
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = false;
2013-10-30 17:01:34 +04:00
return true;
}
/**
*
* w+ event in bitrix log
*/
public static function eventLog($auditType, $itemId, $description) {
CEventLog::Add(array(
"SEVERITY" => "SECURITY",
"AUDIT_TYPE_ID" => $auditType,
"MODULE_ID" => self::$MODULE_ID,
"ITEM_ID" => $itemId,
"DESCRIPTION" => $description,
));
}
/**
*
* Agent function
*
* @return self name
*/
public static function uploadOrdersAgent() {
self::uploadOrders();
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if(is_array($failedIds) && !empty($failedIds))
self::uploadOrders(50, true); // upload failed orders
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
return 'ICrmOrderActions::uploadOrdersAgent();';
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
/**
*
* Agent function
*
* @return self name
*/
public static function orderHistoryAgent() {
self::orderHistory();
return 'ICrmOrderActions::orderHistoryAgent();';
}
2013-12-25 16:30:46 +04:00
/**
*
* Agent function
*
* @return self name
*/
2014-11-25 15:56:18 +03:00
public static function notForkedOrderAgent() {
self::uploadOrdersAgent();
self::orderHistory();
2014-11-25 15:56:18 +03:00
return 'ICrmOrderActions::notForkedOrderAgent();';
}
2013-10-30 17:01:34 +04:00
/**
2013-12-25 16:30:46 +04:00
*
2014-11-24 20:15:54 +03:00
* Agent function
*
* @return self name
*/
2014-11-25 15:56:18 +03:00
public static function orderAgent() {
2014-11-25 15:32:28 +03:00
if(self::isForkable()) {
2014-11-24 22:05:56 +03:00
$ch = curl_init();
2014-11-27 15:59:01 +03:00
curl_setopt($ch, CURLOPT_URL, ($_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') .
$_SERVER['SERVER_NAME'] . '/retailcrm/agent.php');
2014-11-24 22:05:56 +03:00
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch,CURLOPT_HEADER, true); // headers
2014-11-27 16:00:10 +03:00
curl_exec($ch);
2014-11-24 22:05:56 +03:00
curl_close($ch);
2014-11-24 20:15:54 +03:00
} else {
2014-11-25 15:58:33 +03:00
self::notForkedOrderAgent();
2014-11-24 20:15:54 +03:00
}
2014-11-25 15:56:18 +03:00
return 'ICrmOrderActions::orderAgent();';
2014-11-24 20:15:54 +03:00
}
/**
*
* Creates order or returns array of order and customer for mass upload
2013-12-25 16:30:46 +04:00
*
2014-01-28 17:44:21 +04:00
* @param array $arFields
* @param $api
* @param $arParams
* @param $send
2013-10-30 17:01:34 +04:00
* @return boolean
* @return array - array('order' = $order, 'customer' => $customer)
*/
public static function orderCreate($arFields, $api, $arParams, $send = false) {
if(!$api || empty($arParams)) { // add cond to check $arParams
return false;
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if (empty($arFields)) {
//handle err
self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order');
return false;
}
$rsUser = CUser::GetByID($arFields['USER_ID']);
$arUser = $rsUser->Fetch();
$createdAt = new \DateTime($arUser['DATE_REGISTER']);
$createdAt = $createdAt->format('Y-m-d H:i:s');
// push customer (for crm)
$firstName = self::toJSON($arUser['NAME']);
$lastName = self::toJSON($arUser['LAST_NAME']);
$patronymic = self::toJSON($arUser['SECOND_NAME']);
// convert encoding for comment
$statusComment = self::toJson($arFields['REASON_CANCELED']);
$customerComment = self::toJson($arFields['USER_DESCRIPTION']);
$managerComment = self::toJson($arFields['COMMENTS']);
2013-10-31 11:45:26 +04:00
$phones = array();
2013-10-30 17:01:34 +04:00
$phonePersonal = array(
'number' => self::toJSON($arUser['PERSONAL_PHONE']),
'type' => 'mobile'
);
2013-10-31 11:44:12 +04:00
if($phonePersonal['number'])
$phones[] = $phonePersonal;
2013-10-30 17:01:34 +04:00
$phoneWork = array(
'number' => self::toJSON($arUser['WORK_PHONE']),
'type' => 'work'
);
2013-10-31 11:44:12 +04:00
if($phoneWork['number'])
$phones[] = $phoneWork;
2013-10-30 17:01:34 +04:00
$customer = self::clearArr(array(
'externalId' => $arFields['USER_ID'],
'lastName' => $lastName,
'firstName' => $firstName,
'patronymic' => $patronymic,
'phones' => $phones,
'createdAt' => $createdAt
));
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
// delivery types
$arId = array();
if (strpos($arFields['DELIVERY_ID'], ":") !== false)
$arId = explode(":", $arFields["DELIVERY_ID"]);
if ($arId)
$resultDeliveryTypeId = $arId[0];
else
$resultDeliveryTypeId = $arFields['DELIVERY_ID'];
2014-01-27 19:14:43 +04:00
// deliveryService
$deliveryService = array();
if(count($arId) > 1) {
$dbDeliveryType = CSaleDeliveryHandler::GetBySID($arId[0]);
if ($arDeliveryType = $dbDeliveryType->GetNext()) {
foreach($arDeliveryType['PROFILES'] as $id => $profile) {
if($id == $arId[1]) {
$deliveryService = array(
'code' => $arId[0] . '-' . $id,
'name' => $profile['TITLE']
);
}
}
}
}
2013-10-30 17:01:34 +04:00
$resOrder = array();
$resOrderDeliveryAddress = array();
$contactNameArr = array();
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID']));
while ($ar = $rsOrderProps->Fetch()) {
switch ($ar['CODE']) {
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['index']: $resOrderDeliveryAddress['index'] = self::toJSON($ar['VALUE']);
break;
2014-04-16 12:27:34 +04:00
case 'CITY':
$prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']);
if($prop['TYPE'] == 'LOCATION') {
$resOrderDeliveryAddress['city'] = CSaleLocation::GetByID($ar['VALUE']);
$resOrderDeliveryAddress['city'] = self::toJSON($resOrderDeliveryAddress['city']['CITY_NAME_LANG']);
break;
}
$resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
2013-10-30 17:01:34 +04:00
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['text']: $resOrderDeliveryAddress['text'] = self::toJSON($ar['VALUE']);
break;
case 'LOCATION': if(!isset($resOrderDeliveryAddress['city']) && !$resOrderDeliveryAddress['city']) {
2014-04-16 14:34:05 +04:00
$prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']);
if($prop['TYPE'] == 'LOCATION') {
$resOrderDeliveryAddress['city'] = CSaleLocation::GetByID($ar['VALUE']);
$resOrderDeliveryAddress['city'] = self::toJSON($resOrderDeliveryAddress['city']['CITY_NAME_LANG']);
break;
}
$resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
break;
2013-10-30 17:01:34 +04:00
}
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['fio']: $contactNameArr = self::explodeFIO($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['phone']: $resOrder['phone'] = $ar['VALUE'];
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['email']: $resOrder['email'] = $ar['VALUE'];
break;
}
2013-12-25 16:30:46 +04:00
2014-04-17 11:35:34 +04:00
if (count($arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]) > 4) {
2013-10-30 17:01:34 +04:00
switch ($ar['CODE']) {
/*case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['region']: $resOrderDeliveryAddress['region'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['city']: $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
break; */
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['street']: $resOrderDeliveryAddress['street'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['building']: $resOrderDeliveryAddress['building'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['flat']: $resOrderDeliveryAddress['flat'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['intercomcode']: $resOrderDeliveryAddress['intercomcode'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['floor']: $resOrderDeliveryAddress['floor'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['block']: $resOrderDeliveryAddress['block'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['house']: $resOrderDeliveryAddress['house'] = self::toJSON($ar['VALUE']);
break;
}
}
}
$items = array();
2014-08-12 10:53:26 +04:00
$rsOrderBasket = CSaleBasket::GetList(array('ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
2013-10-30 17:01:34 +04:00
while ($p = $rsOrderBasket->Fetch()) {
2013-12-25 16:30:46 +04:00
//for basket props updating (in props we save cancel status)
$propCancel = CSaleBasket::GetPropsList(
array(),
array('BASKET_ID' => $p['ID'], 'CODE' => self::CANCEL_PROPERTY_CODE)
)->Fetch();
2013-12-25 18:38:25 +04:00
2013-12-25 16:30:46 +04:00
if ($propCancel) {
$propCancel = (int)$propCancel['VALUE'];
}
$pr = CCatalogProduct::GetList(array(), array('ID' => $p['PRODUCT_ID']))->Fetch();
2014-10-28 15:32:58 +03:00
if(!$pr) {
2013-10-30 17:01:34 +04:00
$pr = '';
2014-10-28 15:32:58 +03:00
unset($item['productId']);
} else $pr = $pr['PURCHASING_PRICE'];
2013-10-30 17:01:34 +04:00
2013-12-25 18:38:25 +04:00
$item = array(
2013-11-15 17:49:40 +04:00
'discountPercent' => 0,
2013-10-30 17:01:34 +04:00
'quantity' => $p['QUANTITY'],
'productId' => $p['PRODUCT_ID'],
2013-12-25 18:38:25 +04:00
'productName' => self::toJSON($p['NAME']),
'comment' => $p['NOTES'],
2013-10-30 17:01:34 +04:00
);
2013-12-25 16:30:46 +04:00
//if it is canceled product don't send price
if (!$propCancel) {
2013-12-25 18:38:25 +04:00
$item['initialPrice'] = (double) $p['PRICE'] + (double) $p['DISCOUNT_PRICE'];
$item['discount'] = $p['DISCOUNT_PRICE'];
2013-12-25 16:30:46 +04:00
}
2013-12-25 18:38:25 +04:00
$items[] = $item;
2013-10-30 17:01:34 +04:00
}
if($arFields['CANCELED'] == 'Y')
$arFields['STATUS_ID'] = $arFields['CANCELED'].$arFields['CANCELED'];
$createdAt = new \DateTime($arFields['DATE_INSERT']);
$createdAt = $createdAt->format('Y-m-d H:i:s');
2014-05-19 13:41:10 +04:00
$delivery = array(
'code' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId],
'service' => ($arParams['optionsDelivTypes'][$resultDeliveryTypeId]) ? $deliveryService : '',
2014-07-17 11:06:42 +04:00
'address' => $resOrderDeliveryAddress,
'cost' => $arFields['PRICE_DELIVERY']
2014-05-19 13:41:10 +04:00
);
2014-05-22 17:48:44 +04:00
if($arParams['optionsDelivTypes'][$resultDeliveryTypeId] == self::$MUTLISHIP_DELIVERY_TYPE) {
if(CModule::IncludeModule(self::$MULTISHIP_MODULE_VER)) {
$multishipArr = DBOrdersMlsp::GetByOI($arFields['ID']);
2014-11-04 21:03:28 +03:00
$delivery['data'] = array('ms_id' => $multishipArr['MULTISHIP_ID']);
2014-05-22 17:48:44 +04:00
$delivery['integrationCode'] = 'multiship';
}
}
2013-10-30 17:01:34 +04:00
$resOrder = array(
'customer' => $customer,
2013-10-30 17:01:34 +04:00
'number' => $arFields['ACCOUNT_NUMBER'],
'phone' => $resOrder['phone'],
'email' => $resOrder['email'],
'summ' => $arFields['PRICE'],
'markDateTime' => $arFields['DATE_MARKED'],
'externalId' => $arFields['ID'],
'customerId' => $arFields['USER_ID'],
'paymentType' => $arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']],
'paymentStatus' => $arParams['optionsPayment'][$arFields['PAYED']],
'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']],
2014-05-19 13:41:10 +04:00
/*'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId],
'deliveryService' => ($arParams['optionsDelivTypes'][$resultDeliveryTypeId]) ? $deliveryService : '',*/
2013-10-30 17:01:34 +04:00
'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']],
'statusComment' => $statusComment,
'customerComment' => $customerComment,
'managerComment' => $managerComment,
2013-10-30 17:01:34 +04:00
'createdAt' => $createdAt,
2014-05-19 13:41:10 +04:00
//'deliveryAddress' => $resOrderDeliveryAddress,
'delivery' => $delivery,
2013-12-28 19:20:38 +04:00
'discount' => $arFields['DISCOUNT_VALUE'],
2013-10-30 17:01:34 +04:00
'items' => $items
);
if(isset($arParams['optionsSites']) && is_array($arParams['optionsSites'])
&& in_array($arFields['LID'], $arParams['optionsSites']))
$resOrder['site'] = $arFields['LID'];
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
// parse fio
if(count($contactNameArr) == 1) {
$resOrder['firstName'] = $contactNameArr[0];
} else {
$resOrder['lastName'] = $contactNameArr[0];
$resOrder['firstName'] = $contactNameArr[1];
$resOrder['patronymic'] = $contactNameArr[2];
}
2013-12-25 16:30:46 +04:00
// custom orderType function
2014-02-12 11:06:32 +04:00
if(function_exists('intarocrm_get_order_type')) {
2014-02-10 15:23:57 +04:00
$orderType = intarocrm_get_order_type($arFields);
2014-02-10 12:19:36 +04:00
if($orderType)
$resOrder['orderType'] = $orderType;
2014-02-12 11:19:46 +04:00
else
$orderType['orderType'] = 'new';
2014-02-10 12:19:36 +04:00
}
// custom order & customer fields function
if(function_exists('intarocrm_before_order_send')) {
$newResOrder = intarocrm_before_order_send($resOrder);
if(is_array($newResOrder) && !empty($newResOrder))
$resOrder = $newResOrder;
}
2013-10-30 17:01:34 +04:00
$resOrder = self::clearArr($resOrder);
if(isset($resOrder['customer']) && is_array($resOrder['customer']) && !empty($resOrder['customer'])) {
$customer = $resOrder['customer'];
unset($resOrder['customer']);
}
2014-03-22 14:52:17 +04:00
if($send) {
try {
$customer = $api->customerEdit($customer);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
}
2014-03-22 14:52:17 +04:00
try {
return $api->orderEdit($resOrder);
} catch (\IntaroCrm\Exception\ApiException $e) {
self::eventLog(
'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::orderEdit',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
} catch (\IntaroCrm\Exception\CurlException $e) {
self::eventLog(
'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::orderEdit::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
return false;
}
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
return array(
'order' => $resOrder,
'customer' => $customer
);
}
/**
* removes all empty fields from arrays
* working with nested arrs
*
2014-01-28 17:44:21 +04:00
* @param array $arr
2014-01-29 18:42:52 +04:00
* @return array
2013-10-30 17:01:34 +04:00
*/
public static function clearArr($arr) {
if(!$arr || !is_array($arr))
return false;
foreach($arr as $key => $value) {
2013-11-15 17:51:51 +04:00
if((!($value) && $value !== 0) || (is_array($value) && empty($value)))
2013-10-30 17:01:34 +04:00
unset($arr[$key]);
if(is_array($value) && !empty($value))
$arr[$key] = self::clearArr($value);
}
return $arr;
}
/**
*
2014-01-28 17:44:21 +04:00
* @global $APPLICATION
* @param $str in SITE_CHARSET
* @return $str in utf-8
2013-10-30 17:01:34 +04:00
*/
public static function toJSON($str) {
global $APPLICATION;
return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8');
}
/**
*
2014-01-28 17:44:21 +04:00
* @global $APPLICATION
* @param $str in utf-8
* @return $str in SITE_CHARSET
2013-10-30 17:01:34 +04:00
*/
public static function fromJSON($str) {
global $APPLICATION;
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
}
public static function explodeFIO($str) {
if(!$str)
return array();
$array = explode(" ", self::toJSON($str), 3);
$newArray = array();
foreach($array as $ar) {
if(!$ar)
continue;
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$newArray[] = $ar;
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
return $newArray;
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
public static function addOrderProperty($code, $value, $order) {
if (!$code)
return;
if (!CModule::IncludeModule('sale'))
return;
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if ($arProp = CSaleOrderProps::GetList(array(), array('CODE' => $code))->Fetch()) {
return CSaleOrderPropsValue::Add(array(
'NAME' => $arProp['NAME'],
'CODE' => $arProp['CODE'],
'ORDER_PROPS_ID' => $arProp['ID'],
'ORDER_ID' => $order,
'VALUE' => $value,
));
}
}
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
public static function getLocationCityId($cityName) {
if(!$cityName)
return;
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
$dbLocation = CSaleLocation::GetList(
array(
"SORT" => "ASC",
"CITY_NAME_LANG" => "ASC"
2013-12-25 16:30:46 +04:00
),
2013-10-30 17:01:34 +04:00
array("LID" => "ru", "CITY_NAME" => $cityName), false, false, array());
2013-12-25 16:30:46 +04:00
2013-10-30 17:01:34 +04:00
if($location = $dbLocation->Fetch())
return $location['ID'];
}
2014-11-24 20:15:54 +03:00
/*
* Returns true if a not a crontab
* or if $dir exists
*/
public static function isForkable() {
$fork = COption::GetOptionString('main', 'agents_use_crontab', 'N');
2014-11-25 01:21:19 +03:00
if($fork === 'N') {
2014-11-25 15:32:28 +03:00
$file = $_SERVER['DOCUMENT_ROOT'] . '/retailcrm/agent.php';
2014-11-27 15:59:01 +03:00
return file_exists($file) && is_callable('curl_init') &&
isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME'];
2014-11-25 01:21:19 +03:00
}
2014-11-24 20:15:54 +03:00
2014-11-25 01:21:19 +03:00
return false;
2014-11-24 20:15:54 +03:00
}
}