2016-09-15 16:42:10 +03:00
|
|
|
<?php
|
|
|
|
IncludeModuleLangFile(__FILE__);
|
|
|
|
class RetailCrmOrder
|
|
|
|
{
|
|
|
|
public static $MODULE_ID = 'intaro.retailcrm';
|
|
|
|
public static $CRM_API_HOST_OPTION = 'api_host';
|
|
|
|
public static $CRM_API_KEY_OPTION = 'api_key';
|
|
|
|
public static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
|
|
|
|
public static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
|
|
|
|
public static $CRM_PAYMENT_TYPES = 'pay_types_arr';
|
|
|
|
public static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
|
|
|
public static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
|
|
|
|
public static $CRM_ORDER_LAST_ID = 'order_last_id';
|
|
|
|
public static $CRM_SITES_LIST = 'sites_list';
|
|
|
|
public static $CRM_ORDER_PROPS = 'order_props';
|
|
|
|
public static $CRM_LEGAL_DETAILS = 'legal_details';
|
|
|
|
public static $CRM_CUSTOM_FIELDS = 'custom_fields';
|
|
|
|
public static $CRM_CONTRAGENT_TYPE = 'contragent_type';
|
|
|
|
public static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
|
|
|
|
public static $CRM_ORDER_HISTORY_DATE = 'order_history_date';
|
|
|
|
public static $CRM_CATALOG_BASE_PRICE = 'catalog_base_price';
|
|
|
|
public static $CRM_ORDER_NUMBERS = 'order_numbers';
|
2018-05-23 12:19:59 +03:00
|
|
|
public static $CRM_ORDER_DIMENSIONS = 'order_dimensions';
|
2018-12-26 11:41:06 +03:00
|
|
|
public static $CRM_CURRENCY = 'currency';
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED';
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Creates order or returns order for mass upload
|
|
|
|
*
|
|
|
|
* @param array $arFields
|
|
|
|
* @param $api
|
|
|
|
* @param $arParams
|
|
|
|
* @param $send
|
|
|
|
* @return boolean
|
|
|
|
* @return array - array('order' = $order, 'customer' => $customer)
|
|
|
|
*/
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function orderSend($arFields, $api, $arParams, $send = false, $site = null, $methodApi = 'ordersEdit')
|
2018-10-04 16:28:14 +03:00
|
|
|
{
|
2016-10-04 17:57:39 +03:00
|
|
|
if (!$api || empty($arParams)) { // add cond to check $arParams
|
2016-09-15 16:42:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (empty($arFields)) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::orderSend', 'empty($arFields)', 'incorrect order');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:27:31 +03:00
|
|
|
$dimensionsSetting = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_DIMENSIONS, 'N');
|
2018-12-26 11:41:06 +03:00
|
|
|
$optionsCurrency = COption::GetOptionString(self::$MODULE_ID, self::$CRM_CURRENCY, 0);
|
|
|
|
$currency = $optionsCurrency ? $optionsCurrency : \Bitrix\Currency\CurrencyManager::getBaseCurrency();
|
2018-05-23 12:19:59 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
$order = array(
|
|
|
|
'number' => $arFields['NUMBER'],
|
|
|
|
'externalId' => $arFields['ID'],
|
2018-10-04 16:28:14 +03:00
|
|
|
'createdAt' => $arFields['DATE_INSERT'],
|
2016-09-15 16:42:10 +03:00
|
|
|
'customer' => array('externalId' => $arFields['USER_ID']),
|
|
|
|
'orderType' => isset($arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']]) ?
|
2019-09-17 15:42:26 +03:00
|
|
|
$arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']] : '',
|
2016-09-15 16:42:10 +03:00
|
|
|
'status' => isset($arParams['optionsPayStatuses'][$arFields['STATUS_ID']]) ?
|
2019-09-17 15:42:26 +03:00
|
|
|
$arParams['optionsPayStatuses'][$arFields['STATUS_ID']] : '',
|
2016-09-15 16:42:10 +03:00
|
|
|
'customerComment' => $arFields['USER_DESCRIPTION'],
|
|
|
|
'managerComment' => $arFields['COMMENTS'],
|
|
|
|
'delivery' => array(
|
|
|
|
'cost' => $arFields['PRICE_DELIVERY']
|
|
|
|
),
|
|
|
|
);
|
2017-09-04 11:36:04 +03:00
|
|
|
if ($send && isset($_COOKIE['_rc']) && $_COOKIE['_rc'] != '') {
|
2016-09-15 16:42:10 +03:00
|
|
|
$order['customer']['browserId'] = $_COOKIE['_rc'];
|
|
|
|
}
|
|
|
|
$order['contragent']['contragentType'] = $arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']];
|
|
|
|
|
2018-01-12 11:14:33 +03:00
|
|
|
if ($methodApi == 'ordersEdit') {
|
|
|
|
$order['discountManualAmount'] = 0;
|
|
|
|
$order['discountManualPercent'] = 0;
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//fields
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($arFields['PROPS']['properties'] as $prop) {
|
2018-06-13 13:05:58 +03:00
|
|
|
if (!empty($arParams['optionsLegalDetails'])
|
|
|
|
&& $search = array_search($prop['CODE'], $arParams['optionsLegalDetails'][$arFields['PERSON_TYPE_ID']])
|
|
|
|
) {
|
2017-09-04 11:36:04 +03:00
|
|
|
$order['contragent'][$search] = $prop['VALUE'][0];//legal order data
|
2018-06-13 13:05:58 +03:00
|
|
|
} elseif (!empty($arParams['optionsCustomFields'])
|
|
|
|
&& $search = array_search($prop['CODE'], $arParams['optionsCustomFields'][$arFields['PERSON_TYPE_ID']])
|
|
|
|
) {
|
2017-09-04 11:36:04 +03:00
|
|
|
$order['customFields'][$search] = $prop['VALUE'][0];//custom properties
|
|
|
|
} elseif ($search = array_search($prop['CODE'], $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']])) {//other
|
|
|
|
if (in_array($search, array('fio', 'phone', 'email'))) {//fio, phone, email
|
2016-09-15 16:42:10 +03:00
|
|
|
if ($search == 'fio') {
|
2017-09-04 11:36:04 +03:00
|
|
|
$order = array_merge($order, RCrmActions::explodeFIO($prop['VALUE'][0]));//add fio fields
|
2016-09-15 16:42:10 +03:00
|
|
|
} else {
|
2017-09-04 11:36:04 +03:00
|
|
|
$order[$search] = $prop['VALUE'][0];//phone, email
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
} else {//address
|
2016-10-14 15:25:02 +03:00
|
|
|
if ($prop['TYPE'] == 'LOCATION' && isset($prop['VALUE'][0]) && $prop['VALUE'][0] != '') {
|
2016-09-15 16:42:10 +03:00
|
|
|
$arLoc = \Bitrix\Sale\Location\LocationTable::getByCode($prop['VALUE'][0])->fetch();
|
2016-10-14 15:25:02 +03:00
|
|
|
if ($arLoc) {
|
2016-11-15 17:17:09 +03:00
|
|
|
$server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot();
|
|
|
|
$countrys = array();
|
2017-12-04 17:44:30 +03:00
|
|
|
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/country.xml')) {
|
2018-10-04 16:28:14 +03:00
|
|
|
$countrysFile = simplexml_load_file($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/country.xml');
|
2016-11-15 17:17:09 +03:00
|
|
|
foreach ($countrysFile->country as $country) {
|
|
|
|
$countrys[RCrmActions::fromJSON((string) $country->name)] = (string) $country->alpha;
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 15:25:02 +03:00
|
|
|
$location = \Bitrix\Sale\Location\Name\LocationTable::getList(array(
|
|
|
|
'filter' => array('=LOCATION_ID' => $arLoc['CITY_ID'], 'LANGUAGE_ID' => 'ru')
|
|
|
|
))->fetch();
|
2016-11-15 17:17:09 +03:00
|
|
|
if (count($countrys) > 0) {
|
|
|
|
$countryOrder = \Bitrix\Sale\Location\Name\LocationTable::getList(array(
|
|
|
|
'filter' => array('=LOCATION_ID' => $arLoc['COUNTRY_ID'], 'LANGUAGE_ID' => 'ru')
|
|
|
|
))->fetch();
|
|
|
|
if(isset($countrys[$countryOrder['NAME']])){
|
|
|
|
$order['countryIso'] = $countrys[$countryOrder['NAME']];
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 15:25:02 +03:00
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
$prop['VALUE'][0] = $location['NAME'];
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:49:24 +03:00
|
|
|
if (!empty($prop['VALUE'][0])) {
|
|
|
|
$order['delivery']['address'][$search] = $prop['VALUE'][0];
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//deliverys
|
2016-09-15 16:42:10 +03:00
|
|
|
if (array_key_exists($arFields['DELIVERYS'][0]['id'], $arParams['optionsDelivTypes'])) {
|
|
|
|
$order['delivery']['code'] = $arParams['optionsDelivTypes'][$arFields['DELIVERYS'][0]['id']];
|
2016-10-14 15:25:02 +03:00
|
|
|
if (isset($arFields['DELIVERYS'][0]['service']) && $arFields['DELIVERYS'][0]['service'] != '') {
|
2016-10-20 13:16:59 +03:00
|
|
|
$order['delivery']['service']['code'] = $arFields['DELIVERYS'][0]['service'];
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:29:43 +03:00
|
|
|
$weight = 0;
|
|
|
|
$width = 0;
|
|
|
|
$height = 0;
|
|
|
|
$length = 0;
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//basket
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($arFields['BASKET'] as $product) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$item = array(
|
2019-09-17 15:42:26 +03:00
|
|
|
'externalId' => $product['PRODUCT_ID'],
|
2016-09-15 16:42:10 +03:00
|
|
|
'quantity' => $product['QUANTITY'],
|
|
|
|
'offer' => array('externalId' => $product['PRODUCT_ID'],
|
2019-09-17 15:42:26 +03:00
|
|
|
'xmlId' => $product['PRODUCT_XML_ID']
|
|
|
|
),
|
2016-09-15 16:42:10 +03:00
|
|
|
'productName' => $product['NAME']
|
|
|
|
);
|
|
|
|
|
|
|
|
$pp = CCatalogProduct::GetByID($product['PRODUCT_ID']);
|
|
|
|
if (is_null($pp['PURCHASING_PRICE']) == false) {
|
2018-12-26 11:41:06 +03:00
|
|
|
if ($pp['PURCHASING_CURRENCY'] && $currency != $pp['PURCHASING_CURRENCY']) {
|
|
|
|
$purchasePrice = CCurrencyRates::ConvertCurrency(
|
|
|
|
(double) $pp['PURCHASING_PRICE'],
|
|
|
|
$pp['PURCHASING_CURRENCY'],
|
|
|
|
$currency
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$purchasePrice = $pp['PURCHASING_PRICE'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$item['purchasePrice'] = $purchasePrice;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
2018-07-16 16:01:19 +03:00
|
|
|
|
|
|
|
$discount = (double) $product['DISCOUNT_PRICE'];
|
|
|
|
|
|
|
|
if ($discount < 0) {
|
|
|
|
$item['discountManualAmount'] = 0;
|
|
|
|
$initialPrice = (double) $product['PRICE'];
|
|
|
|
} else {
|
|
|
|
$item['discountManualAmount'] = (double) $product['DISCOUNT_PRICE'];
|
|
|
|
$initialPrice = (double) $product['PRICE'] + (double) $product['DISCOUNT_PRICE'];
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
$item['discountManualPercent'] = 0;
|
2018-07-16 16:01:19 +03:00
|
|
|
$item['initialPrice'] = $initialPrice;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
$order['items'][] = $item;
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2018-10-17 17:27:31 +03:00
|
|
|
if ($send && $dimensionsSetting == 'Y') {
|
2018-03-22 16:11:04 +03:00
|
|
|
$dimensions = RCrmActions::unserializeArrayRecursive($product['DIMENSIONS']);
|
2018-07-16 16:01:19 +03:00
|
|
|
|
|
|
|
if ($dimensions !== false) {
|
|
|
|
$width += $dimensions['WIDTH'];
|
|
|
|
$height += $dimensions['HEIGHT'];
|
|
|
|
$length += $dimensions['LENGTH'];
|
2018-11-02 16:51:13 +03:00
|
|
|
$weight += $product['WEIGHT'] * $product['QUANTITY'];
|
2018-07-16 16:01:19 +03:00
|
|
|
}
|
2018-03-22 16:11:04 +03:00
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2018-10-17 17:27:31 +03:00
|
|
|
if ($send && $dimensionsSetting == 'Y') {
|
2018-03-22 16:11:04 +03:00
|
|
|
$order['width'] = $width;
|
|
|
|
$order['height'] = $height;
|
|
|
|
$order['length'] = $length;
|
|
|
|
$order['weight'] = $weight;
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//payments
|
|
|
|
$payments = array();
|
|
|
|
foreach ($arFields['PAYMENTS'] as $payment) {
|
2017-09-22 15:28:19 +03:00
|
|
|
if (!empty($payment['PAY_SYSTEM_ID']) && isset($arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']])) {
|
|
|
|
$pm = array(
|
|
|
|
'type' => $arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']],
|
|
|
|
'amount' => $payment['SUM']
|
|
|
|
);
|
|
|
|
if (!empty($payment['ID'])) {
|
2019-09-17 15:42:26 +03:00
|
|
|
$pm['externalId'] = RCrmActions::generatePaymentExternalId($payment['ID']);
|
2017-09-22 15:28:19 +03:00
|
|
|
}
|
|
|
|
if (!empty($payment['DATE_PAID'])) {
|
|
|
|
$pm['paidAt'] = new \DateTime($payment['DATE_PAID']);
|
|
|
|
}
|
|
|
|
if (!empty($arParams['optionsPayment'][$payment['PAID']])) {
|
|
|
|
$pm['status'] = $arParams['optionsPayment'][$payment['PAID']];
|
|
|
|
}
|
|
|
|
$payments[] = $pm;
|
|
|
|
} else {
|
2018-10-04 16:28:14 +03:00
|
|
|
RCrmActions::eventLog(
|
|
|
|
'RetailCrmOrder::orderSend',
|
|
|
|
'payments',
|
|
|
|
'OrderID = ' . $arFields['ID'] . '. Payment not found.'
|
|
|
|
);
|
|
|
|
|
2017-09-22 15:28:19 +03:00
|
|
|
continue;
|
2017-09-04 11:36:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count($payments) > 0) {
|
|
|
|
$order['payments'] = $payments;
|
|
|
|
}
|
2018-10-04 16:28:14 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//send
|
2016-09-15 16:42:10 +03:00
|
|
|
if (function_exists('retailCrmBeforeOrderSend')) {
|
2016-10-12 11:40:18 +03:00
|
|
|
$newResOrder = retailCrmBeforeOrderSend($order, $arFields);
|
2016-09-15 16:42:10 +03:00
|
|
|
if (is_array($newResOrder) && !empty($newResOrder)) {
|
|
|
|
$order = $newResOrder;
|
2017-09-04 11:36:04 +03:00
|
|
|
} elseif ($newResOrder === false) {
|
2018-10-04 16:28:14 +03:00
|
|
|
RCrmActions::eventLog(
|
|
|
|
'RetailCrmOrder::orderSend',
|
|
|
|
'retailCrmBeforeOrderSend()',
|
|
|
|
'OrderID = ' . $arFields['ID'] . '. Sending canceled after retailCrmBeforeOrderSend'
|
|
|
|
);
|
2017-11-13 11:05:11 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
return false;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$normalizer = new RestNormalizer();
|
2017-10-25 11:25:09 +03:00
|
|
|
$order = $normalizer->normalize($order, 'orders');
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
$log = new Logger();
|
2017-10-25 11:25:09 +03:00
|
|
|
$log->write($order, 'orderSend');
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
if($send) {
|
|
|
|
if (!RCrmActions::apiMethod($api, $methodApi, __METHOD__, $order, $site)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $order;
|
|
|
|
}
|
2018-10-04 16:28:14 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
/**
|
|
|
|
* Mass order uploading, without repeating; always returns true, but writes error log
|
|
|
|
* @param $pSize
|
|
|
|
* @param $failed -- flag to export failed orders
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function uploadOrders($pSize = 50, $failed = false, $orderList = false)
|
|
|
|
{
|
2016-09-15 16:42:10 +03:00
|
|
|
if (!CModule::IncludeModule("iblock")) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::uploadOrders', 'iblock', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!CModule::IncludeModule("sale")) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::uploadOrders', 'sale', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!CModule::IncludeModule("catalog")) {
|
|
|
|
RCrmActions::eventLog('RetailCrmOrder::uploadOrders', 'catalog', 'module not found');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resOrders = array();
|
|
|
|
$resCustomers = array();
|
|
|
|
$orderIds = 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));
|
|
|
|
|
|
|
|
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
|
|
|
|
$orderIds = $failedIds;
|
|
|
|
} elseif ($orderList !== false && count($orderList) > 0) {
|
|
|
|
$orderIds = $orderList;
|
|
|
|
} else {
|
2016-10-20 17:41:07 +03:00
|
|
|
$dbOrder = \Bitrix\Sale\Internals\OrderTable::GetList(array(
|
2016-09-15 16:42:10 +03:00
|
|
|
'order' => array("ID" => "ASC"),
|
|
|
|
'filter' => array('>ID' => $lastUpOrderId),
|
|
|
|
'limit' => $pSize,
|
|
|
|
'select' => array('ID')
|
|
|
|
));
|
2016-10-04 17:57:39 +03:00
|
|
|
while ($arOrder = $dbOrder->fetch()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$orderIds[] = $arOrder['ID'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
if (count($orderIds) <= 0) {
|
2016-09-15 16:42:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2018-10-04 16:28:14 +03:00
|
|
|
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
|
2016-09-15 16:42:10 +03: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));
|
|
|
|
$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));
|
|
|
|
|
|
|
|
$api = new RetailCrm\ApiClient($api_host, $api_key);
|
|
|
|
|
|
|
|
$arParams = array(
|
|
|
|
'optionsOrderTypes' => $optionsOrderTypes,
|
|
|
|
'optionsDelivTypes' => $optionsDelivTypes,
|
|
|
|
'optionsPayTypes' => $optionsPayTypes,
|
|
|
|
'optionsPayStatuses' => $optionsPayStatuses,
|
|
|
|
'optionsPayment' => $optionsPayment,
|
|
|
|
'optionsOrderProps' => $optionsOrderProps,
|
|
|
|
'optionsLegalDetails' => $optionsLegalDetails,
|
|
|
|
'optionsContragentType' => $optionsContragentType,
|
|
|
|
'optionsSitesList' => $optionsSitesList,
|
|
|
|
'optionsCustomFields' => $optionsCustomFields,
|
|
|
|
);
|
|
|
|
|
|
|
|
$recOrders = array();
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($orderIds as $orderId) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$id = \Bitrix\Sale\Order::load($orderId);
|
2016-10-04 17:57:39 +03:00
|
|
|
if (!$id) {
|
2016-09-15 16:42:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$order = self::orderObjToArr($id);
|
|
|
|
$user = Bitrix\Main\UserTable::getById($order['USER_ID'])->fetch();
|
2017-09-04 11:36:04 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
$arCustomers = RetailCrmUser::customerSend($user, $api, $optionsContragentType[$order['PERSON_TYPE_ID']], false, $site);
|
2018-10-04 16:28:14 +03:00
|
|
|
$arOrders = self::orderSend($order, $api, $arParams, false, $site);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
if (!$arCustomers || !$arOrders) {
|
2016-09-15 16:42:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
2018-10-04 16:28:14 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
$resCustomers[$order['LID']][] = $arCustomers;
|
2018-10-04 16:28:14 +03:00
|
|
|
$resOrders[$order['LID']][] = $arOrders;
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
$recOrders[] = $orderId;
|
|
|
|
}
|
2018-10-04 16:28:14 +03:00
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
if (count($resOrders) > 0) {
|
|
|
|
foreach ($resCustomers as $key => $customerLoad) {
|
2017-11-13 11:05:11 +03:00
|
|
|
if ($optionsSitesList) {
|
|
|
|
if (array_key_exists($key, $optionsSitesList) && $optionsSitesList[$key] != null) {
|
|
|
|
$site = $optionsSitesList[$key];
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} elseif (!$optionsSitesList) {
|
2018-01-12 11:14:33 +03:00
|
|
|
$site = null;
|
2017-09-04 11:36:04 +03:00
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
if (RCrmActions::apiMethod($api, 'customersUpload', __METHOD__, $customerLoad, $site) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count($optionsSitesList) > 1) {
|
|
|
|
time_nanosleep(0, 250000000);
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($resOrders as $key => $orderLoad) {
|
2017-11-13 11:05:11 +03:00
|
|
|
if ($optionsSitesList) {
|
|
|
|
if (array_key_exists($key, $optionsSitesList) && $optionsSitesList[$key] != null) {
|
|
|
|
$site = $optionsSitesList[$key];
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} elseif (!$optionsSitesList) {
|
2018-01-12 11:14:33 +03:00
|
|
|
$site = null;
|
2017-09-04 11:36:04 +03:00
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
if (RCrmActions::apiMethod($api, 'ordersUpload', __METHOD__, $orderLoad, $site) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count($optionsSitesList) > 1) {
|
|
|
|
time_nanosleep(0, 250000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function orderObjToArr($obOrder)
|
|
|
|
{
|
2018-10-04 16:28:14 +03:00
|
|
|
$culture = new \Bitrix\Main\Context\Culture(array("FORMAT_DATETIME" => "Y-m-d HH:i:s"));
|
2016-09-15 16:42:10 +03:00
|
|
|
$arOrder = array(
|
|
|
|
'ID' => $obOrder->getId(),
|
|
|
|
'NUMBER' => $obOrder->getField('ACCOUNT_NUMBER'),
|
|
|
|
'LID' => $obOrder->getSiteId(),
|
2018-10-04 16:28:14 +03:00
|
|
|
'DATE_INSERT' => $obOrder->getDateInsert()->toString($culture),
|
2016-09-15 16:42:10 +03:00
|
|
|
'STATUS_ID' => $obOrder->getField('STATUS_ID'),
|
|
|
|
'USER_ID' => $obOrder->getUserId(),
|
|
|
|
'PERSON_TYPE_ID' => $obOrder->getPersonTypeId(),
|
|
|
|
'CURRENCY' => $obOrder->getCurrency(),
|
2017-09-04 11:36:04 +03:00
|
|
|
'PAYMENTS' => array(),
|
2016-09-15 16:42:10 +03:00
|
|
|
'DELIVERYS' => array(),
|
|
|
|
'PRICE_DELIVERY' => $obOrder->getDeliveryPrice(),
|
|
|
|
'PROPS' => $obOrder->getPropertyCollection()->getArray(),
|
|
|
|
'DISCOUNTS' => $obOrder->getDiscount()->getApplyResult(),
|
|
|
|
'BASKET' => array(),
|
|
|
|
'USER_DESCRIPTION' => $obOrder->getField('USER_DESCRIPTION'),
|
|
|
|
'COMMENTS' => $obOrder->getField('COMMENTS'),
|
2016-11-15 17:17:09 +03:00
|
|
|
'REASON_CANCELED' => $obOrder->getField('REASON_CANCELED'),
|
2016-09-15 16:42:10 +03:00
|
|
|
);
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
$shipmentList = $obOrder->getShipmentCollection();
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($shipmentList as $shipmentData) {
|
2018-06-13 13:05:58 +03:00
|
|
|
if ($shipmentData->isSystem()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
if ($shipmentData->getDeliveryId()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$delivery = \Bitrix\Sale\Delivery\Services\Manager::getById($shipmentData->getDeliveryId());
|
2017-09-22 15:28:19 +03:00
|
|
|
$siteDeliverys = RCrmActions::DeliveryList();
|
|
|
|
foreach ($siteDeliverys as $siteDelivery) {
|
|
|
|
if ($siteDelivery['ID'] == $delivery['ID'] && $siteDelivery['PARENT_ID'] == 0) {
|
|
|
|
unset($delivery['PARENT_ID']);
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
if ($delivery['PARENT_ID']) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$servise = explode(':', $delivery['CODE']);
|
|
|
|
$shipment = array('id' => $delivery['PARENT_ID'], 'service' => $servise[1]);
|
2016-10-04 17:57:39 +03:00
|
|
|
} else {
|
2016-09-15 16:42:10 +03:00
|
|
|
$shipment = array('id' => $delivery['ID']);
|
|
|
|
}
|
|
|
|
$arOrder['DELIVERYS'][] = $shipment;
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
$paymentList = $obOrder->getPaymentCollection();
|
|
|
|
foreach ($paymentList as $paymentData) {
|
|
|
|
$arOrder['PAYMENTS'][] = $paymentData->getFields()->getValues();
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
$basketItems = $obOrder->getBasket();
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($basketItems as $item) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$arOrder['BASKET'][] = $item->getFields();
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
return $arOrder;
|
|
|
|
}
|
|
|
|
}
|