1
0
mirror of synced 2024-11-26 23:36:08 +03:00
bitrix-module/intaro.intarocrm/classes/general/ICrmOrderActions.php

1060 lines
47 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';
2015-03-03 16:37:34 +03:00
protected static $CRM_SITES_LIST = 'sites_list';
2013-10-30 17:01:34 +04:00
protected static $CRM_ORDER_PROPS = 'order_props';
2015-03-03 16:37:34 +03:00
protected static $CRM_LEGAL_DETAILS = 'legal_details';
protected static $CRM_CUSTOM_FIELDS = 'custom_fields';
protected static $CRM_CONTRAGENT_TYPE = 'contragent_type';
2013-10-30 17:01:34 +04:00
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
protected static $CRM_ORDER_HISTORY_DATE = 'order_history_date';
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
*/
2015-03-03 16:37:34 +03:00
public static function uploadOrders($pSize = 50, $failed = false, $orderList = false) {
2013-10-30 17:01:34 +04:00
if (!CModule::IncludeModule("iblock")) {
self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found');
return true;
}
if (!CModule::IncludeModule("sale")) {
self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found');
return true;
}
if (!CModule::IncludeModule("catalog")) {
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);
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
2015-03-03 16:37:34 +03:00
$arFilter = array();
$arCount = false;
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
$arFilter['ID'] = $failedIds;
} elseif ($orderList !== false && count($orderList) > 0) {
$arFilter['ID'] = $orderList;
} else {
$arFilter['>ID'] = $lastUpOrderId;
$arCount['nTopCount'] = $pSize;
}
if ( (isset($arFilter['ID']) && count($arFilter['ID']) > 0) || isset($arFilter['>ID']) ) {
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), $arFilter, false, $arCount);
if ($dbOrder->SelectedRowsCount() <= 0) {
return false;
}
} else {
return false;
}
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);
2015-03-03 16:37:34 +03:00
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
2013-10-30 17:01:34 +04:00
$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));
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
2015-03-03 16:37:34 +03:00
$optionsLegalDetails = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_LEGAL_DETAILS, 0));
$optionsContragentType = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CONTRAGENT_TYPE, 0));
$optionsCustomFields = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CUSTOM_FIELDS, 0));
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$api = new RetailCrm\RestApi($api_host, $api_key);
2013-10-30 17:01:34 +04:00
$arParams = array(
2015-03-03 16:37:34 +03:00
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps,
'optionsLegalDetails' => $optionsLegalDetails,
'optionsContragentType' => $optionsContragentType,
'optionsSitesList' => $optionsSitesList ,
'optionsCustomFields' => $optionsCustomFields,
2013-10-30 17:01:34 +04:00
);
2015-03-03 16:37:34 +03:00
$recOrders = array();
while ($arOrder = $dbOrder->GetNext()) {
$result = self::orderCreate($arOrder, $api, $arParams);
if (!$result['order'] || !$result['customer']){
continue;
2013-10-30 17:01:34 +04:00
}
2014-03-22 14:52:17 +04:00
2015-03-03 16:37:34 +03:00
$resOrders[$arOrder['LID']][] = $result['order'];
$resCustomers[$arOrder['LID']][] = $result['customer'];
2014-03-22 14:52:17 +04:00
2015-03-03 16:37:34 +03:00
$recOrders[] = $arOrder['ID'];
}
if(count($resOrders) > 0){
foreach($resCustomers as $key => $customerLoad){
$site = count($optionsSitesList) > 1 ? $optionsSitesList[$key] : null;
if (self::apiMethod($api, 'customerUpload', __METHOD__, $customerLoad, $site) === false) {
2014-03-22 14:52:17 +04:00
return false;
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
if (count($optionsSitesList) > 1) {
time_nanosleep(0, 250000000);
}
}
foreach($resOrders as $key => $orderLoad){
$site = count($optionsSitesList) > 1 ? $optionsSitesList[$key] : null;
if (self::apiMethod($api, 'orderUpload', __METHOD__, $orderLoad, $site) === false) {
2014-03-22 14:52:17 +04:00
return false;
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
if (count($optionsSitesList) > 1) {
time_nanosleep(0, 250000000);
}
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize(array_diff($failedIds, $recOrders)));
} elseif ($lastUpOrderId < max($recOrders) && $orderList === false) {
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, max($recOrders));
}
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
return true;
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
/**
*
* Creates order or returns array of order and customer for mass upload
*
* @param array $arFields
* @param $api
* @param $arParams
* @param $send
* @return boolean
* @return array - array('order' = $order, 'customer' => $customer)
*/
public static function orderCreate($arFields, $api, $arParams, $send = false, $site = null) {
if(!$api || empty($arParams)) { // add cond to check $arParams
return false;
}
if (empty($arFields)) {
self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order');
return false;
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
if (isset($arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['city']) == false) {
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'], 'CODE' => 'LOCATION'));
$arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['city'] = $rsOrderProps->SelectedRowsCount() < 1 ? 'CITY' : 'LOCATION';
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$normalizer = new RestNormalizer();
$normalizer->setValidation(__DIR__ . '/config/retailcrm.json');
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$customer = array();
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
if ($arFields['CANCELED'] == 'Y') {
$arFields['STATUS_ID'] = $arFields['CANCELED'].$arFields['CANCELED'];
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$order = array(
'number' => $arFields['ACCOUNT_NUMBER'],
'externalId' => $arFields['ID'],
'createdAt' => new \DateTime($arFields['DATE_INSERT']),
'customerId' => $arFields['USER_ID'],
'discount' => $arFields['DISCOUNT_VALUE'],
'markDateTime' => $arFields['DATE_MARKED'],
'paymentType' => isset($arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']]) ?
$arParams['optionsPayTypes'][$arFields['PAY_SYSTEM_ID']] : '',
'paymentStatus' => isset($arParams['optionsPayment'][$arFields['PAYED']]) ?
$arParams['optionsPayment'][$arFields['PAYED']] : '',
'orderType' => isset($arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']]) ?
$arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']] : '',
'contragentType' => isset($arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']]) ?
$arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']] : '',
'status' => isset($arParams['optionsPayStatuses'][$arFields['STATUS_ID']]) ?
$arParams['optionsPayStatuses'][$arFields['STATUS_ID']] : '',
'statusComment' => $arFields['REASON_CANCELED'],
'customerComment' => $arFields['USER_DESCRIPTION'],
'managerComment' => $arFields['COMMENTS'],
'delivery' => array(
'cost' => $arFields['PRICE_DELIVERY']
),
);
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID']));
while ($ar = $rsOrderProps->Fetch()) {
if ($search = array_search($ar['CODE'], $arParams['optionsLegalDetails'][$arFields['PERSON_TYPE_ID']])) {
$order[$search] = $ar['VALUE'];
$customer[$search] = $ar['VALUE'];
} elseif ($search = array_search($ar['CODE'], $arParams['optionsCustomFields'][$arFields['PERSON_TYPE_ID']])) {
$order['customFields'][$search] = $ar['VALUE'];
} elseif ($search = array_search($ar['CODE'], $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']])) {
if (in_array($search, array('fio', 'phone', 'email'))) {
if ($search == 'fio') {
$order = array_merge($order, self::explodeFIO($ar['VALUE']));
} else {
$order[$search] = $ar['VALUE'];
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
} else {
$prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']);
if ($prop['TYPE'] == 'LOCATION') {
2015-03-04 11:11:07 +03:00
$ar['VALUE'] = CSaleLocation::GetByID(
method_exists('CSaleLocation', 'getLocationIDbyCODE') ?
CSaleLocation::getLocationIDbyCODE($ar['VALUE']) : $ar['VALUE']
);
2015-03-03 16:37:34 +03:00
$ar['VALUE'] = $ar['VALUE']['CITY_NAME_LANG'];
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
$order['delivery']['address'][$search] = $ar['VALUE'];
2013-10-30 17:01:34 +04:00
}
}
2015-03-03 16:37:34 +03:00
}
if (strpos($arFields['DELIVERY_ID'], ":") !== false){
$arFields["DELIVERY_ID"] = explode(":", $arFields["DELIVERY_ID"], 2);
if ($arDeliveryType = CSaleDeliveryHandler::GetBySID(reset($arFields["DELIVERY_ID"]))->GetNext()) {
if (array_key_exists(end($arFields["DELIVERY_ID"]), $arDeliveryType['PROFILES'])) {
$arFields["DELIVERY_SERVICE"] = array(
'code' => implode('-', $arFields["DELIVERY_ID"]),
'name' => $arDeliveryType['PROFILES'][end($arFields["DELIVERY_ID"])]['TITLE']
2014-03-22 14:52:17 +04:00
);
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
}
$arFields["DELIVERY_ID"] = reset($arFields["DELIVERY_ID"]);
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
if (array_key_exists($arFields['DELIVERY_ID'], $arParams['optionsDelivTypes'])) {
$order['delivery']['code'] = $arParams['optionsDelivTypes'][$arFields["DELIVERY_ID"]];
if (isset($arFields["DELIVERY_SERVICE"])) {
$order['delivery']['service'] = $arFields["DELIVERY_SERVICE"];
}
}
2014-03-22 14:52:17 +04:00
2015-03-03 16:37:34 +03:00
$rsOrderBasket = CSaleBasket::GetList(array('ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
while ($p = $rsOrderBasket->Fetch()) {
$item = array(
'quantity' => $p['QUANTITY'],
'productId' => $p['PRODUCT_ID'],
2015-03-19 15:09:37 +03:00
'xmlId' => $p['PRODUCT_XML_ID'],
2015-03-03 16:37:34 +03:00
'productName' => $p['NAME'],
'comment' => $p['NOTES'],
2015-03-19 15:09:37 +03:00
'createdAt' => new \DateTime($p['DATE_INSERT'])
2015-03-03 16:37:34 +03:00
);
2014-03-22 14:52:17 +04:00
2015-03-19 15:09:37 +03:00
$pp = CCatalogProduct::GetByID($p['PRODUCT_ID']);
if (is_null($pp['PURCHASING_PRICE']) == false) {
$item['purchasePrice'] = $pp['PURCHASING_PRICE'];
}
2015-03-03 16:37:34 +03:00
$propCancel = CSaleBasket::GetPropsList(array(), array('BASKET_ID' => $p['ID'], 'CODE' => self::CANCEL_PROPERTY_CODE))->Fetch();
2015-03-19 15:09:37 +03:00
if (!$propCancel || ($propCancel && !(int)$propCancel['VALUE'])) {
$item['discount'] = (double) $p['DISCOUNT_PRICE'];
2015-03-03 16:37:34 +03:00
$item['initialPrice'] = (double) $p['PRICE'] + (double) $p['DISCOUNT_PRICE'];
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
$order['items'][] = $item;
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
$arUser = CUser::GetByID($arFields['USER_ID'])->Fetch();
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
$customer = array(
'externalId' => $arFields['USER_ID'],
'lastName' => $arUser['LAST_NAME'],
'firstName' => $arUser['NAME'],
'patronymic' => $arUser['SECOND_NAME'],
'phones' => array(
array('number' => $arUser['PERSONAL_PHONE']),
array('number' => $arUser['WORK_PHONE'])
),
'createdAt' => new \DateTime($arUser['DATE_REGISTER']),
'contragentType' => $arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']]
);
if(function_exists('intarocrm_get_order_type')) {
$orderType = intarocrm_get_order_type($arFields);
if ($orderType) {
$order['orderType'] = $orderType;
}
}
if (function_exists('intarocrm_before_order_send')) {
$newResOrder = intarocrm_before_order_send($resOrder);
if (is_array($newResOrder) && !empty($newResOrder)) {
$resOrder = $newResOrder;
2013-12-25 16:30:46 +04:00
}
}
2015-03-03 16:37:34 +03:00
$customer = $normalizer->normalize($customer, 'customers');
$order = $normalizer->normalize($order, 'orders');
2015-03-19 15:09:37 +03:00
2015-03-03 16:37:34 +03:00
if (isset($arParams['optionsSitesList']) && is_array($arParams['optionsSitesList']) &&
array_key_exists($arFields['LID'], $arParams['optionsSitesList'])) {
$site = $arParams['optionsSitesList'][$arFields['LID']];
2013-12-25 16:30:46 +04:00
}
2015-03-03 16:37:34 +03:00
if($send) {
if (!self::apiMethod($api, 'customerEdit', __METHOD__, $customer, $site)) {
return false;
}
if ($orderEdit = self::apiMethod($api, 'orderEdit', __METHOD__, $order, $site)) {
return $orderEdit;
} else {
return false;
}
}
return array(
'order' => $order,
'customer' => $customer
);
2013-12-25 16:30:46 +04:00
}
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;
2015-02-12 17:18:46 +03:00
if (is_object($USER) == false) {
2015-02-13 16:15:58 +03:00
$USER = new RetailUser;
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")) {
self::eventLog('ICrmOrderActions::orderHistory', 'iblock', 'module not found');
2015-03-03 16:37:34 +03:00
return false;
2013-10-30 17:01:34 +04:00
}
if (!CModule::IncludeModule("sale")) {
self::eventLog('ICrmOrderActions::orderHistory', 'sale', 'module not found');
2015-03-03 16:37:34 +03:00
return false;
2013-10-30 17:01:34 +04:00
}
if (!CModule::IncludeModule("catalog")) {
self::eventLog('ICrmOrderActions::orderHistory', 'catalog', 'module not found');
2015-03-03 16:37:34 +03:00
return false;
2013-10-30 17:01:34 +04:00
}
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);
$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)));
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
2015-03-03 16:37:34 +03:00
$optionsLegalDetails = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_LEGAL_DETAILS, 0));
$optionsContragentType = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CONTRAGENT_TYPE, 0));
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$optionsCustomFields = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CUSTOM_FIELDS, 0));
foreach ($optionsOrderProps as $code => $value) {
if (isset($optionsLegalDetails[$code])) {
$optionsOrderProps[$code] = array_merge($optionsOrderProps[$code], $optionsLegalDetails[$code]);
}
if (isset($optionsCustomFields[$code])) {
$optionsOrderProps[$code] = array_merge($optionsOrderProps[$code], $optionsCustomFields[$code]);
}
$optionsOrderProps[$code]['location'] = 'LOCATION';
if (array_search('CITY', $optionsOrderProps[$code]) == false) {
$optionsOrderProps[$code]['city'] = 'CITY';
}
if (array_search('ZIP', $optionsOrderProps[$code]) == false) {
$optionsOrderProps[$code]['index'] = 'ZIP';
}
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$api = new RetailCrm\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);
2015-03-03 16:37:34 +03:00
if (is_null($dateStart)) {
2014-01-28 12:44:20 +04:00
$dateStart = new \DateTime();
$dateStart = $dateStart->format('Y-m-d H:i:s');
}
2014-03-22 14:52:17 +04:00
try {
2015-03-04 11:24:57 +03:00
$orderHistory = $api->orderHistory($dateStart);
2015-03-03 16:37:34 +03:00
} catch (\RetailCrm\Exception\CurlException $e) {
2014-03-22 14:52:17 +04:00
self::eventLog(
2015-03-03 16:37:34 +03:00
'ICrmOrderActions::orderHistory', 'RetailCrm\RestApi::orderHistory::CurlException',
2014-03-22 14:52:17 +04:00
$e->getCode() . ': ' . $e->getMessage()
);
2015-03-03 16:37:34 +03:00
return false;
2014-03-22 14:52:17 +04:00
}
2013-12-25 16:30:46 +04:00
2015-03-19 15:09:37 +03:00
$orderHistory = isset($orderHistory['orders']) ? $orderHistory['orders'] : array();
2015-03-04 11:24:57 +03:00
2014-03-22 14:52:17 +04:00
$dateFinish = $api->getGeneratedAt();
2015-03-03 16:37:34 +03:00
if (is_null($dateFinish) || $dateFinish == false) {
$dateFinish = new \DateTime();
}
2014-03-13 12:58:52 +04:00
2014-07-09 14:42:45 +04:00
$defaultOrderType = 1;
2015-03-03 16:37:34 +03:00
$dbOrderTypesList = CSalePersonType::GetList(array(), array("ACTIVE" => "Y"));
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
2014-07-09 14:42:45 +04:00
$defaultOrderType = $arOrderTypesList['ID'];
2015-02-12 17:18:46 +03:00
}
2014-05-19 13:41:10 +04:00
2014-03-13 13:37:16 +04:00
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = true;
2014-03-13 13:21:43 +04:00
2013-10-30 17:01:34 +04:00
foreach ($orderHistory as $order) {
2015-03-03 16:37:34 +03:00
if (function_exists('intarocrm_order_pre_persist')) {
$order = intarocrm_order_pre_persist($order);
}
2013-10-30 17:01:34 +04:00
2015-02-12 17:18:46 +03:00
if (!isset($order['externalId'])) {
2013-10-30 17:01:34 +04:00
// custom orderType function
2015-02-12 17:18:46 +03:00
if (function_exists('intarocrm_set_order_type')) {
2014-02-12 11:06:32 +04:00
$orderType = intarocrm_set_order_type($order);
2015-02-12 17:18:46 +03:00
if ($orderType) {
2014-02-12 11:06:32 +04:00
$optionsOrderTypes[$order['orderType']] = $orderType;
2015-02-12 17:18:46 +03:00
} else {
2014-07-09 14:42:45 +04:00
$optionsOrderTypes[$order['orderType']] = $defaultOrderType;
2015-02-12 17:18:46 +03:00
}
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)
2015-02-12 17:18:46 +03:00
if (!isset($order['customer']['externalId'])) {
if (!isset($order['customer']['id'])) {
continue;
}
$registerNewUser = true;
if (!isset($order['customer']['email'])) {
$login = $order['customer']['email'] = uniqid('user_' . time()) . '@crm.com';
2014-01-29 18:42:52 +04:00
} else {
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email']));
2015-02-12 17:18:46 +03:00
switch ($dbUser->SelectedRowsCount()) {
case 0:
$login = $order['customer']['email'];
break;
case 1:
$arUser = $dbUser->Fetch();
$registeredUserID = $arUser['ID'];
$registerNewUser = false;
break;
default:
$login = uniqid('user_' . time()) . '@crm.com';
2015-03-03 16:37:34 +03:00
break;
2014-01-28 12:44:20 +04:00
}
2014-01-29 18:42:52 +04:00
}
2015-02-12 17:18:46 +03:00
if ($registerNewUser === true) {
$userPassword = uniqid();
$newUser = new CUser;
$arFields = array(
"NAME" => self::fromJSON($order['customer']['firstName']),
"LAST_NAME" => self::fromJSON($order['customer']['lastName']),
"EMAIL" => $order['customer']['email'],
"LOGIN" => $login,
"LID" => "ru",
"ACTIVE" => "Y",
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
);
$registeredUserID = $newUser->Add($arFields);
if ($registeredUserID === false) {
self::eventLog('ICrmOrderActions::orderHistory', 'CUser::Register', 'Error register user');
continue;
}
try {
$api->customerFixExternalIds(array(array('id' => $order['customer']['id'], 'externalId' => $registeredUserID)));
2015-03-03 16:37:34 +03:00
} catch (\RetailCrm\Exception\CurlException $e) {
2015-02-12 17:18:46 +03:00
self::eventLog(
2015-03-03 16:37:34 +03:00
'ICrmOrderActions::orderHistory', 'RetailCrm\RestApi::customerFixExternalIds::CurlException',
2015-02-12 17:18:46 +03:00
$e->getCode() . ': ' . $e->getMessage()
);
2014-01-29 18:42:52 +04:00
continue;
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
2013-10-30 17:01:34 +04:00
// new order
$newOrderFields = array(
2015-03-03 16:37:34 +03:00
'LID' => CSite::GetDefSite(),
'PERSON_TYPE_ID' => isset($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
2015-03-03 16:37:34 +03:00
if(count($optionsSitesList) > 1 && $lid = array_search($order['site'], $optionsSitesList)){
$newOrderFields['LID'] = $lid;
}
2015-02-12 17:18:46 +03:00
$externalId = CSaleOrder::Add($newOrderFields);
2015-03-03 16:37:34 +03:00
2015-02-12 17:18:46 +03:00
if (!isset($order['externalId'])) {
try {
$api->orderFixExternalIds(array(array('id' => $order['id'], 'externalId' => $externalId)));
2015-03-03 16:37:34 +03:00
} catch (\RetailCrm\Exception\CurlException $e) {
2015-02-12 17:18:46 +03:00
self::eventLog(
2015-03-03 16:37:34 +03:00
'ICrmOrderActions::orderHistory', 'RetailCrm\RestApi::orderFixExternalIds::CurlException',
2015-02-12 17:18:46 +03:00
$e->getCode() . ': ' . $e->getMessage()
);
2013-12-25 16:30:46 +04:00
2015-02-12 17:18:46 +03:00
continue;
}
2013-12-25 16:30:46 +04:00
}
2015-02-12 17:18:46 +03:00
$order['externalId'] = $externalId;
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
2015-02-12 17:18:46 +03:00
if (isset($order['externalId']) && $order['externalId']) {
2014-02-12 09:44:35 +04:00
// custom orderType function
2015-02-12 17:18:46 +03:00
if (function_exists('intarocrm_set_order_type')) {
2014-02-12 09:44:35 +04:00
$orderType = intarocrm_set_order_type($order);
2015-02-12 17:18:46 +03:00
if ($orderType) {
2014-02-12 09:44:35 +04:00
$optionsOrderTypes[$order['orderType']] = $orderType;
2015-02-12 17:18:46 +03:00
} else {
2014-07-09 14:42:45 +04:00
$optionsOrderTypes[$order['orderType']] = $defaultOrderType;
2015-02-12 17:18:46 +03:00
}
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
2015-02-12 17:18:46 +03:00
if ($arFields === false || empty($arFields)) {
2013-10-30 17:01:34 +04:00
continue;
2015-02-12 17:18:46 +03:00
}
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'];
2015-02-12 17:18:46 +03:00
if(isset($order['customer']['externalId']) && !is_null($order['customer']['externalId'])) {
2013-10-31 10:49:20 +04:00
$userId = $order['customer']['externalId'];
2015-02-12 17:18:46 +03:00
}
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']));
2015-03-03 16:37:34 +03:00
$arUpdateProps = array();
2013-10-30 17:01:34 +04:00
while ($ar = $rsOrderProps->Fetch()) {
2015-03-03 16:37:34 +03:00
$prop = CSaleOrderProps::GetByID($ar['ORDER_PROPS_ID']);
$arUpdateProps[ $ar['CODE'] ] = array('ID' => $ar['ID'], 'TYPE' => $prop['TYPE'], 'VALUE' => $ar['VALUE']);
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$order['fio'] = trim(
implode(
' ',
array(
isset($order['lastName']) ? $order['lastName'] : '',
isset($order['firstName']) ? $order['firstName'] : '',
isset($order['patronymic']) ? $order['patronymic'] : '',
)
)
);
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
if (isset($order['delivery']['address']['city'])) {
$order['location'] = $order['delivery']['address']['city'];
}
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
if (isset($order['orderType']) && isset($optionsOrderTypes[ $order['orderType'] ])) {
if (isset($optionsOrderProps[$arFields['PERSON_TYPE_ID']])) {
foreach ($optionsOrderProps[$arFields['PERSON_TYPE_ID']] as $code => $value) {
if (in_array($code, array_keys($order)) === false && isset($optionsOrderProps[$optionsOrderTypes[$order['orderType']]][$code])) {
$order[ $code ] = $arUpdateProps[$optionsOrderProps[$arFields['PERSON_TYPE_ID']][$code]]['VALUE'];
}
2015-02-12 17:18:46 +03:00
}
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
//update ordertype
CSaleOrder::Update($order['externalId'], array('PERSON_TYPE_ID' => $optionsOrderTypes[ $order['orderType'] ]));
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
$arProp = CSaleOrderProps::GetList(array(), array('PERSON_TYPE_ID' => $optionsOrderTypes[ $order['orderType'] ]));
$typeParam = array();
while ($ar = $arProp->Fetch()) {
$typeParam[ $ar['CODE'] ] = $ar['CODE'];
}
foreach (array_diff_key($arUpdateProps, $typeParam) as $code => $param) {
if (isset($arUpdateProps[$code])) {
CSaleOrderPropsValue::Delete($param['ID']);
}
}
$arFields['PERSON_TYPE_ID'] = $optionsOrderTypes[ $order['orderType'] ];
2015-02-12 17:18:46 +03:00
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
array_walk_recursive(
self::clearArr($order),
'self::recursiveUpdate',
array(
'update' => $arUpdateProps,
'type' => $arFields['PERSON_TYPE_ID'],
'options' => $optionsOrderProps,
'orderId' => $order['externalId']
)
);
2013-10-30 17:01:34 +04:00
foreach($order['items'] as $item) {
if(isset($item['deleted']) && $item['deleted']) {
2015-03-03 16:37:34 +03:00
if ($p = CSaleBasket::GetList(array(), array('ORDER_ID' => $order['externalId'], 'PRODUCT_ID' => $item['id']))->Fetch()) {
2013-10-30 17:01:34 +04:00
CSaleBasket::Delete($p['ID']);
2015-02-12 17:18:46 +03:00
}
2015-03-03 16:37:34 +03:00
continue;
2013-10-30 17:01:34 +04:00
}
2015-03-03 16:37:34 +03:00
if (isset($item['offer']) === false && isset($item['offer']['externalId']) === false) {
2013-10-30 17:01:34 +04:00
continue;
2015-02-12 17:18:46 +03:00
}
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
$p = CSaleBasket::GetList(array(),array('ORDER_ID' => $order['externalId'], 'PRODUCT_ID' => $item['offer']['externalId']))->Fetch();
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
if ($p == false) {
2013-10-30 17:01:34 +04:00
$p = CIBlockElement::GetByID($item['offer']['externalId'])->Fetch();
$iblock = CIBlock::GetByID($p['IBLOCK_ID'])->Fetch();
$p['CATALOG_XML_ID'] = $iblock['XML_ID'];
$p['PRODUCT_XML_ID'] = $p['XML_ID'];
unset($p['XML_ID']);
2015-03-03 16:37:34 +03:00
} elseif ($propResult = CSaleBasket::GetPropsList(array(''),array('BASKET_ID' => $p['ID']))) {
while ($r = $propResult->Fetch()) {
2013-12-25 16:30:46 +04:00
$p['PROPS'][] = $r;
}
}
2013-10-30 17:01:34 +04:00
$arProduct = array();
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
if (isset($item['created']) && $item['created'] == true) {
2013-10-30 17:01:34 +04:00
$productPrice = GetCatalogProductPrice($item['offer']['externalId'], 1);
$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
);
2015-03-03 16:37:34 +03:00
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
if (isset($item['isCanceled']) == false) {
2015-02-12 17:18:46 +03:00
if (isset($item['initialPrice']) && $item['initialPrice']) {
2013-10-30 17:01:34 +04:00
$arProduct['PRICE'] = (double) $item['initialPrice'];
2015-02-12 17:18:46 +03:00
}
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'];
2015-03-03 16:37:34 +03:00
$newPrice = round($arProduct['PRICE'] / 100 * (100 - $arProduct['DISCOUNT_VALUE']), 2);
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
}
2015-02-12 17:18:46 +03:00
if(isset($item['discount']) || isset($item['discountPercent'])) {
2013-12-25 16:30:46 +04:00
$arProduct['PRICE'] -= $arProduct['DISCOUNT_PRICE'];
2015-02-12 17:18:46 +03:00
}
if (isset($item['offer']['name']) && $item['offer']['name']) {
2013-11-28 15:08:07 +04:00
$arProduct['NAME'] = self::fromJSON($item['offer']['name']);
2015-02-12 17:18:46 +03:00
}
2013-12-25 18:38:25 +04:00
$arProduct = self::updateCancelProp($arProduct, 0);
2015-03-03 16:37:34 +03:00
} elseif (isset($item['isCanceled'])) {
2013-12-25 16:30:46 +04:00
$arProduct['PRICE'] = 0;
2013-12-25 18:38:25 +04:00
$arProduct = self::updateCancelProp($arProduct, 1);
2013-11-15 17:26:51 +04:00
}
2015-03-03 16:37:34 +03:00
if (isset($item['created']) && $item['created'] == true) {
CSaleBasket::Add($arProduct);
continue;
}
2013-10-30 17:01:34 +04:00
2015-03-03 16:37:34 +03:00
if (count($p['PROPS']) > 0) {
$arProduct['PROPS'] = $p['PROPS'];
}
2015-02-12 17:18:46 +03:00
if (isset($item['quantity']) && $item['quantity']) {
2013-10-30 17:01:34 +04:00
$arProduct['QUANTITY'] = $item['quantity'];
2015-02-12 17:18:46 +03:00
}
if (isset($item['offer']['name']) && $item['offer']['name']) {
2013-11-28 15:08:07 +04:00
$arProduct['NAME'] = self::fromJSON($item['offer']['name']);
2015-02-12 17:18:46 +03:00
}
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
2015-03-03 16:37:34 +03:00
if (isset($order['delivery']) === false || isset($order['delivery']['cost']) === false) {
2014-07-17 11:06:42 +04:00
$order['delivery']['cost'] = $arFields['PRICE_DELIVERY'];
2015-02-12 17:18:46 +03:00
}
2013-11-06 16:01:25 +04:00
2015-03-03 16:37:34 +03:00
if (isset($order['summ']) === false || $order['summ'] <= 0) {
2013-11-14 16:41:19 +04:00
$order['summ'] = $arFields['PRICE'] - $arFields['PRICE_DELIVERY'];
2015-02-12 17:18:46 +03:00
}
2013-11-14 16:41:19 +04:00
2015-02-12 17:18:46 +03:00
$wasCanaceled = $arFields['CANCELED'] == 'Y' ? true : false;
2013-11-13 23:05:19 +04:00
2015-03-03 16:37:34 +03:00
if (isset($optionsDelivTypes[$order['delivery']['code']])) {
$resultDeliveryTypeId = $optionsDelivTypes[$order['delivery']['code']];
} else {
$resultDeliveryTypeId = isset($order['delivery']['service']) && isset($order['delivery']['service']['code']) ?
reset(explode(":", $arFields['DELIVERY_ID'], 1)) :
$arFields['DELIVERY_ID'];
2014-01-27 19:14:43 +04:00
}
2015-03-03 16:37:34 +03:00
if(isset($order['delivery']['service']) && isset($order['delivery']['service']['code'])) {
$deliveryHandler = reset(CSaleDeliveryHandler::GetBySID($resultDeliveryTypeId)->arResult);
if (count($deliveryHandler) > 0 && array_key_exists($order['delivery']['service']['code'], $deliveryHandler['PROFILES'])) {
$resultDeliveryTypeId = $resultDeliveryTypeId . ':' . $order['delivery']['service']['code'];
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'],
2015-02-12 17:18:46 +03:00
'USER_ID' => $userId,
2013-10-30 17:01:34 +04:00
'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']],
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
2015-02-12 17:18:46 +03:00
if (isset($order['discount'])) {
$arFields['DISCOUNT_VALUE'] = $order['discount'];
$arFields['PRICE'] -= $order['discount'];
}
if(!empty($arFields)) {
CSaleOrder::Update($order['externalId'], $arFields);
2015-02-12 17:18:46 +03:00
}
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']]);
2015-03-03 16:37:34 +03:00
if($wasCanaceled && $optionsPayStatuses[ $order['status'] ] != 'YY') {
CSaleOrder::CancelOrder($order['externalId'], "N", $order['statusComment']);
2015-03-03 16:37:34 +03:00
} elseif ($optionsPayStatuses[ $order['status'] ] == 'YY') {
CSaleOrder::CancelOrder($order['externalId'], "Y", $order['statusComment']);
2015-02-12 17:18:46 +03:00
}
}
}
2013-11-09 01:12:20 +04:00
// set PAYED
2015-02-12 17:18:46 +03:00
if(isset($order['paymentStatus']) && $order['paymentStatus'] && $optionsPayment[$order['paymentStatus']]) {
2013-11-15 16:51:57 +04:00
CSaleOrder::PayOrder($order['externalId'], $optionsPayment[$order['paymentStatus']]);
2015-02-12 17:18:46 +03:00
}
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
2015-03-04 11:24:57 +03:00
if (count($orderHistory) > 0) {
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'));
2015-02-12 17:18:46 +03:00
}
2014-03-13 13:21:43 +04:00
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = false;
2013-10-30 17:01:34 +04:00
return true;
}
2015-03-03 16:37:34 +03:00
protected static function recursiveUpdate($value, $code, $param)
{
$value = self::fromJSON($value);
if (in_array($code, array('customer', 'items')) === false && isset($param['options'][$param['type']][$code])) {
self::updateProps($value, $code, $param);
}
}
protected static function updateProps($value, $code, $param)
{
if ($value == '' || !CModule::IncludeModule('sale')) {
return false;
}
$add = false;
if (isset($param['update'][ $param['options'][$param['type']][$code] ]) == false) {
if ($arProp = CSaleOrderProps::GetList(array(), array('CODE' => $param['options'][$param['type']][$code]))->Fetch()) {
$param['update'][ $param['options'][$param['type']][$code] ] = array(
'NAME' => $arProp['NAME'],
'CODE' => $arProp['CODE'],
'ORDER_PROPS_ID' => $arProp['ID'],
'TYPE' => $arProp['TYPE'],
'ORDER_ID' => $param['orderId'],
'VALUE' => ''
);
$add = true;
} else {
return false;
}
}
if ($param['update'][ $param['options'][$param['type']][$code] ]['TYPE'] == 'LOCATION') {
$value = self::getLocation($value);
if ($value == false) {
return false;
}
}
if ($param['update'][ $param['options'][$param['type']][$code] ]['VALUE'] != $value) {
if ($add === true) {
$param['update'][ $param['options'][$param['type']][$code] ]['VALUE'] = $value;
CSaleOrderPropsValue::Add($param['update'][ $param['options'][$param['type']][$code] ]);
} else {
CSaleOrderPropsValue::Update($param['update'][ $param['options'][$param['type']][$code] ]['ID'], array('VALUE' => $value));
}
}
}
protected static function updateCancelProp($arProduct, $value) {
if (isset($arProduct['PROPS'])) {
foreach($arProduct['PROPS'] as $key => $item) {
if ($item['CODE'] == self::CANCEL_PROPERTY_CODE) {
$arProduct['PROPS'][$key]['VALUE'] = $value;
break;
}
}
$arProduct['PROPS'][] = array(
'NAME' => GetMessage('PRODUCT_CANCEL'),
'CODE' => self::CANCEL_PROPERTY_CODE,
'VALUE' => $value,
'SORT' => 10,
);
}
return $arProduct;
}
public static function getLocation($value) {
if (is_string($value) === false) {
return false;
} elseif ($location = CSaleLocation::GetList(array(), array("LID" => LANGUAGE_ID, "CITY_NAME" => $value))->Fetch()) {
2015-03-04 11:11:07 +03:00
return method_exists('CSaleLocation', 'getLocationCODEbyID') ?
CSaleLocation::getLocationCODEbyID($location['ID']) : $location['ID'];
2015-03-03 16:37:34 +03:00
} else {
return false;
}
}
2013-10-30 17:01:34 +04:00
/**
*
* 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));
2015-02-16 11:44:14 +03:00
if (is_array($failedIds) && !empty($failedIds)) {
self::uploadOrders(50, true);
}
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
/**
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() {
2015-02-12 17:18:46 +03:00
if(COption::GetOptionString('main', 'agents_use_crontab', 'N') != 'N') {
define('NO_AGENT_CHECK', true);
2014-11-27 18:58:34 +03:00
}
2015-02-16 11:44:14 +03:00
self::uploadOrdersAgent();
self::orderHistory();
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
}
2013-10-30 17:01:34 +04:00
/**
* 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) {
2015-02-16 12:21:49 +03:00
if (is_array($arr) === false) {
return $arr;
}
2013-10-30 17:01:34 +04:00
2015-02-16 12:21:49 +03:00
$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 ]);
}
2013-10-30 17:01:34 +04:00
}
2015-02-16 12:21:49 +03:00
return $result;
2013-10-30 17:01:34 +04:00
}
/**
*
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);
}
2015-02-16 12:21:49 +03:00
public static function explodeFIO($fio) {
2015-03-03 16:37:34 +03:00
$newFio = empty($fio) ? false : explode(" ", $fio, 3);
2015-02-16 12:21:49 +03:00
$result = array();
switch (count($newFio)) {
default:
case 0:
$result['firstName'] = $fio;
break;
case 1:
$result['firstName'] = $newFio[0];
break;
case 2:
$result = array(
2015-03-03 16:37:34 +03:00
'lastName' => $newFio[0],
'firstName' => $newFio[1]
2015-02-16 12:21:49 +03:00
);
break;
case 3:
$result = array(
2015-03-03 16:37:34 +03:00
'lastName' => $newFio[0],
'firstName' => $newFio[1],
2015-02-16 12:21:49 +03:00
'patronymic' => $newFio[2]
);
break;
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
2015-02-16 12:21:49 +03:00
return $result;
2013-10-30 17:01:34 +04:00
}
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
public static function apiMethod($api, $methodApi, $method, $params, $site = null) {
switch($methodApi){
case 'ordersGet':
case 'orderEdit':
case 'customerGet':
case 'customerEdit':
try {
$result = $api->$methodApi($params, 'externalId', $site);
} catch (\RetailCrm\Exception\CurlException $e) {
self::eventLog(
__CLASS__.'::'.$method, 'RetailCrm\RestApi::'.$methodApi.'::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
return false;
}
return $result;
default:
try {
$result = $api->$methodApi($params, $site);
} catch (\RetailCrm\Exception\CurlException $e) {
self::eventLog(
__CLASS__.'::'.$method, 'RetailCrm\RestApi::'.$methodApi.'::CurlException',
$e->getCode() . ': ' . $e->getMessage()
);
2013-12-25 16:30:46 +04:00
2015-03-03 16:37:34 +03:00
return false;
}
return $result;
}
2013-10-30 17:01:34 +04:00
}
}
2015-02-13 16:15:58 +03:00
class RetailUser extends CUser
{
public function GetID()
{
2015-02-16 11:44:14 +03:00
$rsUser = CUser::GetList(($by='ID'), ($order='DESC'), array('LOGIN' => 'retailcrm%'));
2015-02-13 16:15:58 +03:00
if ($arUser = $rsUser->Fetch()) {
return $arUser['ID'];
} else {
$retailUser = new CUser;
$userPassword = uniqid();
$arFields = array(
2015-02-16 10:30:04 +03:00
"NAME" => 'retailcrm',
"LAST_NAME" => 'retailcrm',
"EMAIL" => 'retailcrm@retailcrm.com',
"LOGIN" => 'retailcrm',
"LID" => "ru",
"ACTIVE" => "Y",
"GROUP_ID" => array(2),
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
);
$id = $retailUser->Add($arFields);
if (!$id) {
return null;
} else {
return $id;
}
2015-02-13 16:15:58 +03:00
}
}
2015-02-13 17:56:50 +03:00
}