2016-09-15 16:42:10 +03:00
|
|
|
<?php
|
2021-05-31 11:14:02 +03:00
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
use Bitrix\Main\Context;
|
|
|
|
use Bitrix\Main\Context\Culture;
|
2021-07-08 16:29:34 +03:00
|
|
|
use Bitrix\Main\UserTable;
|
2021-08-19 15:46:03 +03:00
|
|
|
use Bitrix\Sale\Delivery\Services\Manager;
|
2021-05-31 11:14:02 +03:00
|
|
|
use Bitrix\Sale\Internals\Fields;
|
2021-09-08 10:19:29 +03:00
|
|
|
use Bitrix\Sale\Internals\OrderTable;
|
2021-08-19 15:46:03 +03:00
|
|
|
use Bitrix\Sale\Location\LocationTable;
|
2021-07-08 16:29:34 +03:00
|
|
|
use Bitrix\Sale\Order;
|
2022-03-02 15:40:53 +03:00
|
|
|
use Intaro\RetailCrm\Component\Factory\ClientFactory;
|
|
|
|
use Intaro\RetailCrm\Component\ServiceLocator;
|
|
|
|
use Intaro\RetailCrm\Service\LoyaltyService;
|
2021-07-08 16:29:34 +03:00
|
|
|
use RetailCrm\ApiClient;
|
2021-08-19 15:46:03 +03:00
|
|
|
use Intaro\RetailCrm\Service\ManagerService;
|
2022-03-02 15:40:53 +03:00
|
|
|
use Intaro\RetailCrm\Service\LoyaltyAccountService;
|
2021-08-19 15:46:03 +03:00
|
|
|
use RetailCrm\Response\ApiResponse;
|
2022-02-04 15:16:14 +03:00
|
|
|
use \Bitrix\Sale\Location\Name\LocationTable as LocationTableName;
|
2022-03-02 15:40:53 +03:00
|
|
|
use Intaro\RetailCrm\Component\ConfigProvider;
|
2021-05-31 11:14:02 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
IncludeModuleLangFile(__FILE__);
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RetailCrmOrder
|
|
|
|
*/
|
2016-09-15 16:42:10 +03:00
|
|
|
class RetailCrmOrder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creates order or returns order for mass upload
|
|
|
|
*
|
2021-08-19 15:46:03 +03:00
|
|
|
* @param array $arOrder
|
2020-02-19 14:35:56 +03:00
|
|
|
* @param $api
|
|
|
|
* @param $arParams
|
|
|
|
* @param bool $send
|
|
|
|
* @param null $site
|
|
|
|
* @param string $methodApi
|
|
|
|
*
|
2022-03-02 15:40:53 +03:00
|
|
|
* @return array|false|\Intaro\RetailCrm\Model\Api\Response\OrdersCreateResponse|\Intaro\RetailCrm\Model\Api\Response\OrdersEditResponse|null
|
2020-02-19 14:35:56 +03:00
|
|
|
* @throws \Bitrix\Main\ArgumentException
|
|
|
|
* @throws \Bitrix\Main\ObjectPropertyException
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
2016-09-15 16:42:10 +03:00
|
|
|
*/
|
2021-08-19 15:46:03 +03:00
|
|
|
public static function orderSend(
|
|
|
|
array $arOrder,
|
|
|
|
$api,
|
|
|
|
$arParams,
|
|
|
|
bool $send = false,
|
|
|
|
$site = null,
|
|
|
|
string $methodApi = 'ordersEdit'
|
|
|
|
) {
|
2016-10-04 17:57:39 +03:00
|
|
|
if (!$api || empty($arParams)) { // add cond to check $arParams
|
2022-03-02 15:40:53 +03:00
|
|
|
return null;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
|
|
if (empty($arOrder)) {
|
2016-09-15 16:42:10 +03:00
|
|
|
RCrmActions::eventLog('RetailCrmOrder::orderSend', 'empty($arFields)', 'incorrect order');
|
2022-03-02 15:40:53 +03:00
|
|
|
return null;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
$dimensionsSetting = RetailcrmConfigProvider::getOrderDimensions();
|
|
|
|
$currency = RetailcrmConfigProvider::getCurrencyOrDefault();
|
|
|
|
$optionCorpClient = RetailcrmConfigProvider::getCorporateClientStatus();
|
2018-05-23 12:19:59 +03:00
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
$order = [
|
|
|
|
'number' => $arOrder['NUMBER'],
|
|
|
|
'externalId' => $arOrder['ID'],
|
|
|
|
'createdAt' => $arOrder['DATE_INSERT'],
|
|
|
|
'customer' => isset($arParams['customerCorporate'])
|
|
|
|
? ['id' => $arParams['customerCorporate']['id']]
|
|
|
|
: ['externalId' => $arOrder['USER_ID']],
|
|
|
|
'orderType' => $arParams['optionsOrderTypes'][$arOrder['PERSON_TYPE_ID']] ?? '',
|
|
|
|
'status' => $arParams['optionsPayStatuses'][$arOrder['STATUS_ID']] ?? '',
|
|
|
|
'customerComment' => $arOrder['USER_DESCRIPTION'],
|
|
|
|
'managerComment' => $arOrder['COMMENTS'],
|
|
|
|
'managerId' => $arParams['managerId'] ?? null,
|
|
|
|
'delivery' => ['cost' => $arOrder['PRICE_DELIVERY']],
|
|
|
|
];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2022-12-05 16:32:28 +03:00
|
|
|
if (!empty($arOrder['REASON_CANCELED'])) {
|
|
|
|
$order['statusComment'] = $arOrder['REASON_CANCELED'];
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
if (isset($arParams['contactExId'])) {
|
|
|
|
$order['contact']['externalId'] = $arParams['contactExId'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($arParams['orderCompany']) && !empty($arParams['orderCompany'])) {
|
|
|
|
$company = $arParams['orderCompany'];
|
|
|
|
|
|
|
|
if (isset($company['id'])) {
|
|
|
|
$order['company']['id'] = $company['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($company['name'])) {
|
|
|
|
$order['contragent']['legalName'] = $company['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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'];
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
$order['contragent']['contragentType'] = $arParams['optionsContragentType'][$arOrder['PERSON_TYPE_ID']];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//fields
|
2021-08-19 15:46:03 +03:00
|
|
|
foreach ($arOrder['PROPS']['properties'] as $prop) {
|
2018-06-13 13:05:58 +03:00
|
|
|
if (!empty($arParams['optionsLegalDetails'])
|
2021-08-19 15:46:03 +03:00
|
|
|
&& $search = array_search($prop['CODE'], $arParams['optionsLegalDetails'][$arOrder['PERSON_TYPE_ID']])
|
2018-06-13 13:05:58 +03:00
|
|
|
) {
|
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'])
|
2021-08-19 15:46:03 +03:00
|
|
|
&& $search = array_search($prop['CODE'], $arParams['optionsCustomFields'][$arOrder['PERSON_TYPE_ID']])
|
2018-06-13 13:05:58 +03:00
|
|
|
) {
|
2017-09-04 11:36:04 +03:00
|
|
|
$order['customFields'][$search] = $prop['VALUE'][0];//custom properties
|
2021-08-19 15:46:03 +03:00
|
|
|
} elseif ($search = array_search($prop['CODE'], $arParams['optionsOrderProps'][$arOrder['PERSON_TYPE_ID']])) {//other
|
2021-09-08 10:19:29 +03:00
|
|
|
if (in_array($search, ['fio', 'phone', 'email'])) {//fio, phone, email
|
|
|
|
if ($search === 'fio') {
|
2021-08-19 15:46:03 +03:00
|
|
|
$order = array_merge($order, RCrmActions::explodeFio($prop['VALUE'][0]));//add fio fields
|
2021-09-08 10:19:29 +03:00
|
|
|
} elseif ($search === 'email' && mb_strlen($prop['VALUE'][0]) > 100) {
|
2020-12-22 13:03:32 +03:00
|
|
|
continue;
|
2016-09-15 16:42:10 +03:00
|
|
|
} else {
|
2020-07-14 13:47:26 +03:00
|
|
|
// ignoring a property with a non-set group if the field value is already set
|
|
|
|
if (!empty($order[$search]) && $prop['PROPS_GROUP_ID'] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
2021-09-08 10:19:29 +03:00
|
|
|
if ($prop['TYPE'] === 'LOCATION' && isset($prop['VALUE'][0]) && $prop['VALUE'][0] != '') {
|
2021-08-19 15:46:03 +03:00
|
|
|
$arLoc = LocationTable::getByCode($prop['VALUE'][0])->fetch();
|
2016-10-14 15:25:02 +03:00
|
|
|
if ($arLoc) {
|
2021-08-19 15:46:03 +03:00
|
|
|
$server = Context::getCurrent()->getServer()->getDocumentRoot();
|
|
|
|
$countrys = [];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2022-02-04 15:16:14 +03:00
|
|
|
$location = LocationTableName::getList([
|
2021-09-08 10:19:29 +03:00
|
|
|
'filter' => ['=LOCATION_ID' => $arLoc['CITY_ID'], 'LANGUAGE_ID' => 'ru']
|
2021-08-19 15:46:03 +03:00
|
|
|
])->fetch();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2016-11-15 17:17:09 +03:00
|
|
|
if (count($countrys) > 0) {
|
2022-02-04 15:16:14 +03:00
|
|
|
$countryOrder = LocationTableName::getList([
|
2021-09-08 10:19:29 +03:00
|
|
|
'filter' => ['=LOCATION_ID' => $arLoc['COUNTRY_ID'], 'LANGUAGE_ID' => 'ru']
|
|
|
|
])->fetch();
|
2016-11-15 17:17:09 +03:00
|
|
|
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
|
2021-08-19 15:46:03 +03:00
|
|
|
if (array_key_exists($arOrder['DELIVERYS'][0]['id'], $arParams['optionsDelivTypes'])) {
|
|
|
|
$order['delivery']['code'] = $arParams['optionsDelivTypes'][$arOrder['DELIVERYS'][0]['id']];
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
if (isset($arOrder['DELIVERYS'][0]['service']) && $arOrder['DELIVERYS'][0]['service'] != '') {
|
|
|
|
$order['delivery']['service']['code'] = $arOrder['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;
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if ('ordersEdit' === $methodApi) {
|
2022-04-18 16:43:52 +03:00
|
|
|
$response = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $order['externalId'], $site);
|
2019-12-23 17:55:19 +03:00
|
|
|
if (isset($response['order'])) {
|
2019-12-24 15:41:12 +03:00
|
|
|
foreach ($response['order']['items'] as $k => $item) {
|
2022-03-02 15:40:53 +03:00
|
|
|
$externalId = $k .'_'. $item['offer']['externalId'];
|
2019-12-24 15:41:12 +03:00
|
|
|
$orderItems[$externalId] = $item;
|
2019-12-23 17:55:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//basket
|
2021-08-19 15:46:03 +03:00
|
|
|
foreach ($arOrder['BASKET'] as $position => $product) {
|
2020-08-18 15:33:54 +03:00
|
|
|
$itemId = null;
|
2021-09-08 10:19:29 +03:00
|
|
|
$externalId = $position . '_' . $product['PRODUCT_ID'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
2019-12-24 15:41:12 +03:00
|
|
|
if (isset($orderItems[$externalId])) { //update
|
|
|
|
$externalIds = $orderItems[$externalId]['externalIds'];
|
2020-04-30 11:56:43 +03:00
|
|
|
$itemId = $orderItems[$externalId]['id'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
$key = array_search('bitrix', array_column($externalIds, 'code'));
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if ($externalIds[$key]['code'] === 'bitrix') {
|
|
|
|
$externalIds[$key] = [
|
2019-12-24 15:41:12 +03:00
|
|
|
'code' => 'bitrix',
|
|
|
|
'value' => $externalId,
|
2021-09-08 10:19:29 +03:00
|
|
|
];
|
2019-12-24 15:41:12 +03:00
|
|
|
} else {
|
2021-09-08 10:19:29 +03:00
|
|
|
$externalIds[] = [
|
2019-12-24 15:41:12 +03:00
|
|
|
'code' => 'bitrix',
|
|
|
|
'value' => $externalId,
|
2021-09-08 10:19:29 +03:00
|
|
|
];
|
2022-03-02 15:40:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$keyBasketId = array_search('bitrixBasketId', array_column($externalIds, 'code'));
|
|
|
|
|
|
|
|
if ($externalIds[$keyBasketId]['code'] === 'bitrixBasketId') {
|
|
|
|
$externalIds[$keyBasketId] = [
|
|
|
|
'code' => 'bitrixBasketId',
|
|
|
|
'value' => $product['ID'],
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$externalIds[] = [
|
|
|
|
'code' => 'bitrixBasketId',
|
|
|
|
'value' => $product['ID'],
|
|
|
|
];
|
|
|
|
}
|
2019-12-23 17:55:19 +03:00
|
|
|
} else { //create
|
2021-09-08 10:19:29 +03:00
|
|
|
$externalIds = [
|
|
|
|
[
|
2019-12-24 15:41:12 +03:00
|
|
|
'code' => 'bitrix',
|
|
|
|
'value' => $externalId,
|
2022-03-02 15:40:53 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'code' => 'bitrixBasketId',
|
|
|
|
'value' => $product['ID'],
|
|
|
|
],
|
2021-09-08 10:19:29 +03:00
|
|
|
];
|
2019-12-23 17:55:19 +03:00
|
|
|
}
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
$item = [
|
|
|
|
'externalIds' => $externalIds,
|
|
|
|
'quantity' => $product['QUANTITY'],
|
|
|
|
'offer' => [
|
2020-07-14 13:47:26 +03:00
|
|
|
'externalId' => $product['PRODUCT_ID'],
|
2021-08-19 15:46:03 +03:00
|
|
|
'xmlId' => $product['PRODUCT_XML_ID'],
|
|
|
|
],
|
|
|
|
'productName' => $product['NAME'],
|
|
|
|
];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2020-04-30 11:56:43 +03:00
|
|
|
if (isset($itemId)) {
|
|
|
|
$item['id'] = $itemId;
|
|
|
|
}
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
$catalogProduct = CCatalogProduct::GetByID($product['PRODUCT_ID']);
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if (is_null($catalogProduct['PURCHASING_PRICE']) === false) {
|
|
|
|
if ($catalogProduct['PURCHASING_CURRENCY'] && $currency != $catalogProduct['PURCHASING_CURRENCY']) {
|
2018-12-26 11:41:06 +03:00
|
|
|
$purchasePrice = CCurrencyRates::ConvertCurrency(
|
2021-09-08 10:19:29 +03:00
|
|
|
(double) $catalogProduct['PURCHASING_PRICE'],
|
|
|
|
$catalogProduct['PURCHASING_CURRENCY'],
|
2018-12-26 11:41:06 +03:00
|
|
|
$currency
|
|
|
|
);
|
|
|
|
} else {
|
2021-09-08 10:19:29 +03:00
|
|
|
$purchasePrice = $catalogProduct['PURCHASING_PRICE'];
|
2018-12-26 11:41:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$item['purchasePrice'] = $purchasePrice;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
2018-07-16 16:01:19 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
$discount = (double) $product['DISCOUNT_PRICE'];
|
|
|
|
$dpItem = $product['BASE_PRICE'] - $product['PRICE'];
|
|
|
|
|
|
|
|
if ( $dpItem > 0 && $discount <= 0) {
|
|
|
|
$discount = $dpItem;
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
$item['discountManualPercent'] = 0;
|
2022-03-02 15:40:53 +03:00
|
|
|
$item['initialPrice'] = (double) $product['BASE_PRICE'];
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
if (
|
|
|
|
$product['BASE_PRICE'] >= $product['PRICE']
|
|
|
|
&& $methodApi === 'ordersEdit'
|
|
|
|
&& ConfigProvider::getLoyaltyProgramStatus() === 'Y'
|
|
|
|
) {
|
|
|
|
/** @var LoyaltyService $service */
|
|
|
|
$service = ServiceLocator::get(LoyaltyService::class);
|
2022-06-28 15:28:28 +03:00
|
|
|
$item['discountManualAmount'] = $service->getInitialDiscount((int) $product['ID']) ?? $discount;
|
2022-03-02 15:40:53 +03:00
|
|
|
} elseif ($product['BASE_PRICE'] >= $product['PRICE']) {
|
2021-05-31 11:14:02 +03:00
|
|
|
$item['discountManualAmount'] = self::getDiscountManualAmount($product);
|
|
|
|
$item['initialPrice'] = (double) $product['BASE_PRICE'];
|
|
|
|
} else {
|
|
|
|
$item['discountManualAmount'] = 0;
|
|
|
|
$item['initialPrice'] = $product['PRICE'];
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
$order['items'][] = $item;
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2021-09-08 10:19:29 +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
|
|
|
|
2021-09-08 10:19:29 +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
|
2021-08-19 15:46:03 +03:00
|
|
|
$payments = [];
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
foreach ($arOrder['PAYMENTS'] as $payment) {
|
2021-09-08 13:00:50 +03:00
|
|
|
$isIntegrationPayment = RetailCrmService::isIntegrationPayment($payment['PAY_SYSTEM_ID'] ?? null);
|
|
|
|
|
2017-09-22 15:28:19 +03:00
|
|
|
if (!empty($payment['PAY_SYSTEM_ID']) && isset($arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']])) {
|
2021-09-08 10:19:29 +03:00
|
|
|
$pm = [
|
2020-09-25 12:17:29 +03:00
|
|
|
'type' => $arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']]
|
2021-09-08 10:19:29 +03:00
|
|
|
];
|
2020-09-25 12:17:29 +03:00
|
|
|
|
2017-09-22 15:28:19 +03:00
|
|
|
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
|
|
|
}
|
2020-09-25 12:17:29 +03:00
|
|
|
|
2021-09-08 13:00:50 +03:00
|
|
|
if (!empty($payment['DATE_PAID']) && !$isIntegrationPayment) {
|
2017-09-22 15:28:19 +03:00
|
|
|
$pm['paidAt'] = new \DateTime($payment['DATE_PAID']);
|
|
|
|
}
|
2020-09-25 12:17:29 +03:00
|
|
|
|
2021-09-08 13:00:50 +03:00
|
|
|
if (!empty($arParams['optionsPayment'][$payment['PAID']]) && !$isIntegrationPayment) {
|
2021-09-08 10:19:29 +03:00
|
|
|
$pm['status'] = $arParams['optionsPayment'][$payment['PAID']];
|
2017-09-22 15:28:19 +03:00
|
|
|
}
|
2020-09-25 12:17:29 +03:00
|
|
|
|
|
|
|
if (RetailcrmConfigProvider::shouldSendPaymentAmount()) {
|
|
|
|
$pm['amount'] = $payment['SUM'];
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:28:19 +03:00
|
|
|
$payments[] = $pm;
|
|
|
|
} else {
|
2018-10-04 16:28:14 +03:00
|
|
|
RCrmActions::eventLog(
|
|
|
|
'RetailCrmOrder::orderSend',
|
|
|
|
'payments',
|
2021-08-19 15:46:03 +03:00
|
|
|
'OrderID = ' . $arOrder['ID'] . '. Payment not found.'
|
2018-10-04 16:28:14 +03:00
|
|
|
);
|
2017-09-04 11:36:04 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-19 15:46:03 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
if (count($payments) > 0) {
|
|
|
|
$order['payments'] = $payments;
|
|
|
|
}
|
2018-10-04 16:28:14 +03:00
|
|
|
|
2022-06-28 15:28:28 +03:00
|
|
|
if (!empty($arParams['crmOrder']['privilegeType'])) {
|
|
|
|
$order['privilegeType'] = $arParams['crmOrder']['privilegeType'];
|
|
|
|
} elseif (ConfigProvider::getLoyaltyProgramStatus() === 'Y' && LoyaltyAccountService::getLoyaltyPersonalStatus()) {
|
|
|
|
$order['privilegeType'] = 'loyalty_level';
|
|
|
|
} else {
|
|
|
|
$order['privilegeType'] = 'none';
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
//send
|
2016-09-15 16:42:10 +03:00
|
|
|
if (function_exists('retailCrmBeforeOrderSend')) {
|
2021-08-19 15:46:03 +03:00
|
|
|
$newResOrder = retailCrmBeforeOrderSend($order, $arOrder);
|
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()',
|
2021-08-19 15:46:03 +03:00
|
|
|
'OrderID = ' . $arOrder['ID'] . '. Sending canceled after retailCrmBeforeOrderSend'
|
2018-10-04 16:28:14 +03:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:11:27 +03:00
|
|
|
if ('ordersEdit' === $methodApi) {
|
|
|
|
$order = RetailCrmService::unsetIntegrationDeliveryFields($order);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
Logger::getInstance()->write($order, 'orderSend');
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
/** @var \Intaro\RetailCrm\Component\ApiClient\ClientAdapter $client */
|
|
|
|
$client = ClientFactory::createClientAdapter();
|
|
|
|
|
|
|
|
if ($send) {
|
|
|
|
if ($methodApi === 'ordersCreate') {
|
|
|
|
return $client->createOrder($order, $site);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($methodApi === 'ordersEdit') {
|
|
|
|
return $client->editOrder($order, $site);
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-02-19 14:35:56 +03:00
|
|
|
*
|
2021-08-19 15:46:03 +03:00
|
|
|
* @param int $pSize
|
|
|
|
* @param bool $failed -- flag to export failed orders
|
|
|
|
* @param array|null $orderList
|
2020-02-19 14:35:56 +03:00
|
|
|
*
|
2022-03-02 15:40:53 +03:00
|
|
|
* @return bool
|
2021-08-19 15:46:03 +03:00
|
|
|
* @throws \Bitrix\Main\ArgumentException
|
2020-02-19 14:35:56 +03:00
|
|
|
* @throws \Bitrix\Main\ArgumentNullException
|
|
|
|
* @throws \Bitrix\Main\ObjectPropertyException
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
2016-09-15 16:42:10 +03:00
|
|
|
*/
|
2021-08-19 15:46:03 +03:00
|
|
|
public static function uploadOrders(int $pSize = 50, bool $failed = false, array $orderList = []): bool
|
2016-10-04 17:57:39 +03:00
|
|
|
{
|
2020-04-24 13:18:18 +03:00
|
|
|
if (!RetailcrmDependencyLoader::loadDependencies()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
$ordersPack = [];
|
|
|
|
$resCustomers = [];
|
|
|
|
$resCustomersAdded = [];
|
|
|
|
$resCustomersCorporate = [];
|
|
|
|
$orderIds = [];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
$lastUpOrderId = RetailcrmConfigProvider::getLastOrderId();
|
|
|
|
$failedIds = RetailcrmConfigProvider::getFailedOrdersIds();
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
|
|
|
|
$orderIds = $failedIds;
|
2021-08-19 15:46:03 +03:00
|
|
|
} elseif (count($orderList) > 0) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$orderIds = $orderList;
|
|
|
|
} else {
|
2021-08-19 15:46:03 +03:00
|
|
|
$dbOrder = OrderTable::GetList([
|
2022-03-02 15:40:53 +03:00
|
|
|
'order' => ['ID' => 'ASC'],
|
2021-08-19 15:46:03 +03:00
|
|
|
'filter' => ['>ID' => $lastUpOrderId],
|
|
|
|
'limit' => $pSize,
|
|
|
|
'select' => ['ID'],
|
|
|
|
]);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
$optionsOrderTypes = RetailcrmConfigProvider::getOrderTypes();
|
|
|
|
$optionsDelivTypes = RetailcrmConfigProvider::getDeliveryTypes();
|
|
|
|
$optionsPayTypes = RetailcrmConfigProvider::getPaymentTypes();
|
|
|
|
$optionsPayStatuses = RetailcrmConfigProvider::getPaymentStatuses(); // --statuses
|
|
|
|
$optionsPayment = RetailcrmConfigProvider::getPayment();
|
|
|
|
$optionsOrderProps = RetailcrmConfigProvider::getOrderProps();
|
|
|
|
$optionsLegalDetails = RetailcrmConfigProvider::getLegalDetails();
|
|
|
|
$optionsContragentType = RetailcrmConfigProvider::getContragentTypes();
|
|
|
|
$optionsCustomFields = RetailcrmConfigProvider::getCustomFields();
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
$api = new ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
$arParams = [
|
2016-09-15 16:42:10 +03:00
|
|
|
'optionsOrderTypes' => $optionsOrderTypes,
|
|
|
|
'optionsDelivTypes' => $optionsDelivTypes,
|
|
|
|
'optionsPayTypes' => $optionsPayTypes,
|
|
|
|
'optionsPayStatuses' => $optionsPayStatuses,
|
|
|
|
'optionsPayment' => $optionsPayment,
|
|
|
|
'optionsOrderProps' => $optionsOrderProps,
|
|
|
|
'optionsLegalDetails' => $optionsLegalDetails,
|
|
|
|
'optionsContragentType' => $optionsContragentType,
|
2021-09-08 10:19:29 +03:00
|
|
|
'optionsSitesList' => RetailcrmConfigProvider::getSitesList(),
|
2016-09-15 16:42:10 +03:00
|
|
|
'optionsCustomFields' => $optionsCustomFields,
|
2021-09-08 10:19:29 +03:00
|
|
|
];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
$recOrders = [];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($orderIds as $orderId) {
|
2021-09-08 10:19:29 +03:00
|
|
|
$bitrixOrder = Order::load($orderId);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if (!$bitrixOrder) {
|
2016-09-15 16:42:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
$arCustomer = [];
|
|
|
|
$arCustomerCorporate = [];
|
2021-09-08 10:19:29 +03:00
|
|
|
$order = self::orderObjToArr($bitrixOrder);
|
|
|
|
$site = self::getCrmShopCodeByLid($order['LID'], $arParams['optionsSitesList']);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if (null === $site && count($arParams['optionsSitesList']) > 0) {
|
2020-04-24 13:18:18 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
self::createCustomerForOrder($api, $arCustomer, $arCustomerCorporate,$arParams, $order, $site);
|
2017-09-04 11:36:04 +03:00
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
if (isset($order['RESPONSIBLE_ID']) && !empty($order['RESPONSIBLE_ID'])) {
|
|
|
|
$managerService = ManagerService::getInstance();
|
|
|
|
$arParams['managerId'] = $managerService->getManagerCrmId((int) $order['RESPONSIBLE_ID']);
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:27:02 +03:00
|
|
|
$arOrders = self::orderSend($order, $api, $arParams, false, $site,'ordersCreate');
|
2016-09-15 16:42:10 +03:00
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
if (!$arCustomer || !$arOrders) {
|
2016-09-15 16:42:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
2018-10-04 16:28:14 +03:00
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
if (!empty($arCustomerCorporate) && !empty($arCustomerCorporate['nickName'])) {
|
|
|
|
$resCustomersCorporate[$arCustomerCorporate['nickName']] = $arCustomerCorporate;
|
|
|
|
}
|
|
|
|
|
2022-02-04 15:16:14 +03:00
|
|
|
if (
|
|
|
|
array_key_exists('externalId', $arCustomer)
|
|
|
|
&& !in_array($arCustomer['externalId'], $resCustomersAdded, true)
|
|
|
|
) {
|
|
|
|
$resCustomersAdded[] = $arCustomer['externalId'];
|
2020-04-24 13:18:18 +03:00
|
|
|
$resCustomers[$order['LID']][] = $arCustomer;
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
$ordersPack[$order['LID']][] = $arOrders;
|
2016-09-15 16:42:10 +03:00
|
|
|
$recOrders[] = $orderId;
|
|
|
|
}
|
2018-10-04 16:28:14 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
if (count($ordersPack) > 0) {
|
2021-09-08 10:19:29 +03:00
|
|
|
if (false === RetailCrmOrder::uploadCustomersList($resCustomers, $api, $arParams['optionsSitesList'])) {
|
2020-04-24 13:18:18 +03:00
|
|
|
return false;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
if ('Y' === RetailcrmConfigProvider::getCorporateClientStatus()) {
|
2021-07-08 16:29:34 +03:00
|
|
|
$cachedCorporateIds = [];
|
|
|
|
|
|
|
|
foreach ($ordersPack as $lid => $lidOrdersList) {
|
|
|
|
foreach ($lidOrdersList as $key => $orderData) {
|
|
|
|
$lidOrdersList[$key] = self::addCorporateCustomerToOrder(
|
|
|
|
$orderData,
|
|
|
|
$api,
|
|
|
|
$resCustomersCorporate,
|
|
|
|
$cachedCorporateIds
|
|
|
|
);
|
2017-11-13 11:05:11 +03:00
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
$ordersPack[$lid] = $lidOrdersList;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if (false === RetailCrmOrder::uploadOrdersList($ordersPack, $api, $arParams['optionsSitesList'])) {
|
2020-04-24 13:18:18 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
|
2020-04-24 13:18:18 +03:00
|
|
|
RetailcrmConfigProvider::setFailedOrdersIds(array_diff($failedIds, $recOrders));
|
2022-03-02 15:40:53 +03:00
|
|
|
} elseif (count($orderList) === 0 && $lastUpOrderId < max($recOrders)) {
|
2020-04-24 13:18:18 +03:00
|
|
|
RetailcrmConfigProvider::setLastOrderId(max($recOrders));
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
/**
|
|
|
|
* @param \RetailCrm\ApiClient $api
|
|
|
|
* @param array $arCustomer
|
|
|
|
* @param array $arCustomerCorporate
|
|
|
|
* @param array $arParams
|
|
|
|
* @param array $order
|
|
|
|
* @param $site
|
|
|
|
*
|
|
|
|
* @throws \Bitrix\Main\ArgumentException
|
|
|
|
* @throws \Bitrix\Main\ObjectPropertyException
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
2021-08-19 15:46:03 +03:00
|
|
|
* @throws \Exception
|
2021-07-08 16:29:34 +03:00
|
|
|
*/
|
|
|
|
public static function createCustomerForOrder(
|
|
|
|
ApiClient $api,
|
|
|
|
array &$arCustomer,
|
|
|
|
array &$arCustomerCorporate,
|
|
|
|
array &$arParams,
|
|
|
|
array $order,
|
|
|
|
$site
|
|
|
|
): void {
|
|
|
|
$optionsContragentType = RetailcrmConfigProvider::getContragentTypes();
|
|
|
|
$user = UserTable::getById($order['USER_ID'])->fetch();
|
|
|
|
|
|
|
|
if ('Y' === RetailcrmConfigProvider::getCorporateClientStatus()) {
|
|
|
|
if (true === RetailCrmCorporateClient::isCorpTookExternalId((string) $user['ID'], $api)) {
|
|
|
|
RetailCrmCorporateClient::setPrefixForExternalId((string) $user['ID'], $api);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
'Y' === RetailcrmConfigProvider::getCorporateClientStatus()
|
|
|
|
&& $optionsContragentType[$order['PERSON_TYPE_ID']] === 'legal-entity'
|
|
|
|
) {
|
|
|
|
// TODO check if order is corporate, and if it IS - make corporate order
|
|
|
|
$arCustomer = RetailCrmUser::customerSend(
|
|
|
|
$user,
|
|
|
|
$api,
|
|
|
|
'individual',
|
|
|
|
false,
|
|
|
|
$site
|
|
|
|
);
|
|
|
|
|
|
|
|
$arCustomerCorporate = RetailCrmCorporateClient::clientSend(
|
|
|
|
$order,
|
|
|
|
$api,
|
|
|
|
'legal-entity',
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
$site
|
|
|
|
);
|
|
|
|
|
|
|
|
$arParams['orderCompany'] = isset($arCustomerCorporate['companies'])
|
|
|
|
? reset($arCustomerCorporate['companies'])
|
|
|
|
: null;
|
|
|
|
$arParams['contactExId'] = $user['ID'];
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arCustomer = RetailCrmUser::customerSend(
|
|
|
|
$user,
|
|
|
|
$api,
|
|
|
|
$optionsContragentType[$order['PERSON_TYPE_ID']],
|
|
|
|
false,
|
|
|
|
$site
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($arParams['contactExId'])) {
|
|
|
|
unset($arParams['contactExId']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $orderData
|
|
|
|
* @param \RetailCrm\ApiClient $api
|
|
|
|
* @param array $resCustomersCorporate
|
|
|
|
* @param array $cachedCorporateIds
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function addCorporateCustomerToOrder(
|
|
|
|
array $orderData,
|
|
|
|
ApiClient $api,
|
|
|
|
array $resCustomersCorporate,
|
|
|
|
array &$cachedCorporateIds
|
|
|
|
): array {
|
|
|
|
$customerLegalName = $orderData['contragent']['legalName'];
|
|
|
|
|
|
|
|
if (
|
|
|
|
isset($orderData['contragent']['contragentType'])
|
|
|
|
&& $orderData['contragent']['contragentType'] === 'legal-entity'
|
|
|
|
&& !empty($customerLegalName)
|
|
|
|
) {
|
|
|
|
if (isset($cachedCorporateIds[$customerLegalName])) {
|
|
|
|
$orderData['customer'] = ['id' => $cachedCorporateIds[$customerLegalName]];
|
|
|
|
} else {
|
|
|
|
$corpListResponse = $api->customersCorporateList(['nickName' => [$customerLegalName]]);
|
|
|
|
|
|
|
|
if (
|
|
|
|
$corpListResponse
|
|
|
|
&& $corpListResponse->isSuccessful()
|
|
|
|
&& $corpListResponse->offsetExists('customersCorporate')
|
|
|
|
&& !empty($corpListResponse['customersCorporate'])
|
|
|
|
) {
|
|
|
|
$corpListResponse = $corpListResponse['customersCorporate'];
|
|
|
|
$corpListResponse = reset($corpListResponse);
|
|
|
|
|
|
|
|
$orderData['customer'] = ['id' => $corpListResponse['id']];
|
|
|
|
$cachedCorporateIds[$customerLegalName] = $corpListResponse['id'];
|
|
|
|
|
|
|
|
RetailCrmCorporateClient::addCustomersCorporateAddresses(
|
|
|
|
$orderData['customer']['id'],
|
|
|
|
$customerLegalName,
|
|
|
|
$orderData['delivery']['address']['text'],
|
|
|
|
$api,
|
2021-08-19 15:46:03 +03:00
|
|
|
null
|
2021-07-08 16:29:34 +03:00
|
|
|
);
|
|
|
|
} elseif (array_key_exists($customerLegalName, $resCustomersCorporate)) {
|
|
|
|
$createResponse = $api->customersCorporateCreate(
|
|
|
|
$resCustomersCorporate[$customerLegalName]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($createResponse && $createResponse->isSuccessful()) {
|
|
|
|
$orderData['customer'] = ['id' => $createResponse['id']];
|
|
|
|
$cachedCorporateIds[$customerLegalName] = $createResponse['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time_nanosleep(0, 250000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $orderData;
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:06:37 +03:00
|
|
|
/**
|
|
|
|
* @param array $resCustomers
|
|
|
|
* @param RetailCrm\ApiClient $api
|
|
|
|
* @param array $optionsSitesList
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2021-09-08 10:19:29 +03:00
|
|
|
public static function uploadCustomersList(array $resCustomers, ApiClient $api, array $optionsSitesList)
|
2020-09-11 15:06:37 +03:00
|
|
|
{
|
2021-09-08 10:19:29 +03:00
|
|
|
return self::uploadItems(
|
2020-09-11 15:06:37 +03:00
|
|
|
$resCustomers,
|
|
|
|
'customersUpload',
|
|
|
|
'uploadedCustomers',
|
|
|
|
$api,
|
|
|
|
$optionsSitesList
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $resOrders
|
|
|
|
* @param RetailCrm\ApiClient $api
|
|
|
|
* @param array $optionsSitesList
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2021-09-08 10:19:29 +03:00
|
|
|
public static function uploadOrdersList(array $resOrders, ApiClient $api, array $optionsSitesList)
|
2020-09-11 15:06:37 +03:00
|
|
|
{
|
2021-09-08 10:19:29 +03:00
|
|
|
return self::uploadItems(
|
2020-09-11 15:06:37 +03:00
|
|
|
$resOrders,
|
|
|
|
'ordersUpload',
|
|
|
|
'uploadedOrders',
|
|
|
|
$api,
|
|
|
|
$optionsSitesList
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-08 10:19:29 +03:00
|
|
|
* @param string $orderLid
|
2021-08-19 15:46:03 +03:00
|
|
|
* @param array $optionsSitesList
|
2020-09-11 15:06:37 +03:00
|
|
|
*
|
2021-09-08 10:19:29 +03:00
|
|
|
* @return string|null
|
2020-09-11 15:06:37 +03:00
|
|
|
*/
|
2021-09-08 10:19:29 +03:00
|
|
|
public static function getCrmShopCodeByLid(string $orderLid, array $optionsSitesList): ?string
|
2020-09-11 15:06:37 +03:00
|
|
|
{
|
2021-09-08 10:19:29 +03:00
|
|
|
return $optionsSitesList[$orderLid] ?? null;
|
2020-09-11 15:06:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-08 10:19:29 +03:00
|
|
|
* @param array $pack
|
|
|
|
* @param string $method
|
|
|
|
* @param string $keyResponse
|
2020-09-11 15:06:37 +03:00
|
|
|
* @param RetailCrm\ApiClient $api
|
2021-09-08 10:19:29 +03:00
|
|
|
* @param array $optionsSitesList
|
2020-09-11 15:06:37 +03:00
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2021-09-08 10:19:29 +03:00
|
|
|
public static function uploadItems(
|
|
|
|
array $pack,
|
|
|
|
string $method,
|
|
|
|
string $keyResponse,
|
|
|
|
ApiClient $api,
|
|
|
|
array $optionsSitesList
|
|
|
|
) {
|
2021-08-19 15:46:03 +03:00
|
|
|
$uploaded = [];
|
2020-09-11 15:06:37 +03:00
|
|
|
$sizePack = 50;
|
|
|
|
|
|
|
|
foreach ($pack as $key => $itemLoad) {
|
2021-09-08 10:19:29 +03:00
|
|
|
$site = self::getCrmShopCodeByLid($key, $optionsSitesList);
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if (null === $site && count($optionsSitesList) > 0) {
|
2020-09-11 15:06:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$chunkList = array_chunk($itemLoad, $sizePack, true);
|
|
|
|
|
|
|
|
foreach ($chunkList as $chunk) {
|
|
|
|
time_nanosleep(0, 250000000);
|
|
|
|
|
|
|
|
/** @var \RetailCrm\Response\ApiResponse|bool $response */
|
|
|
|
$response = RCrmActions::apiMethod(
|
|
|
|
$api,
|
|
|
|
$method,
|
|
|
|
__METHOD__,
|
|
|
|
$chunk,
|
|
|
|
$site
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
if (($response instanceof ApiResponse) && $response->offsetExists($keyResponse)) {
|
|
|
|
$uploaded = array_merge($uploaded, $response[$keyResponse]);
|
2020-09-11 15:06:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uploaded;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
/**
|
|
|
|
* Returns true if provided order array is corporate order data
|
|
|
|
*
|
|
|
|
* @param array|\ArrayAccess $order
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-08 16:29:34 +03:00
|
|
|
public static function isOrderCorporate($order): bool
|
2020-04-24 13:18:18 +03:00
|
|
|
{
|
2021-09-08 10:19:29 +03:00
|
|
|
return isset($order['customer'], $order['customer']['type'])
|
|
|
|
&& (is_array($order) || $order instanceof ArrayAccess)
|
2021-07-08 16:29:34 +03:00
|
|
|
&& $order['customer']['type'] === 'customer_corporate';
|
2020-04-24 13:18:18 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 14:35:56 +03:00
|
|
|
/**
|
|
|
|
* Converts order object to array
|
|
|
|
*
|
|
|
|
* @param \Bitrix\Sale\Order $obOrder
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
|
|
|
*/
|
2021-08-19 15:46:03 +03:00
|
|
|
public static function orderObjToArr(Order $obOrder): array
|
2016-10-04 17:57:39 +03:00
|
|
|
{
|
2021-08-19 15:46:03 +03:00
|
|
|
$culture = new Culture(['FORMAT_DATETIME' => 'Y-m-d HH:i:s']);
|
|
|
|
$arOrder = [
|
2016-09-15 16:42:10 +03:00
|
|
|
'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(),
|
2021-08-19 15:46:03 +03:00
|
|
|
'PAYMENTS' => [],
|
|
|
|
'DELIVERYS' => [],
|
2016-09-15 16:42:10 +03:00
|
|
|
'PRICE_DELIVERY' => $obOrder->getDeliveryPrice(),
|
|
|
|
'PROPS' => $obOrder->getPropertyCollection()->getArray(),
|
|
|
|
'DISCOUNTS' => $obOrder->getDiscount()->getApplyResult(),
|
2021-08-19 15:46:03 +03:00
|
|
|
'BASKET' => [],
|
2016-09-15 16:42:10 +03:00
|
|
|
'USER_DESCRIPTION' => $obOrder->getField('USER_DESCRIPTION'),
|
|
|
|
'COMMENTS' => $obOrder->getField('COMMENTS'),
|
2021-08-19 15:46:03 +03:00
|
|
|
'REASON_CANCELED' => $obOrder->getField('REASON_CANCELED'),
|
|
|
|
'RESPONSIBLE_ID' => $obOrder->getField('RESPONSIBLE_ID'),
|
|
|
|
];
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
$shipmentList = $obOrder->getShipmentCollection();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
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()) {
|
2021-08-19 15:46:03 +03:00
|
|
|
$delivery = Manager::getById($shipmentData->getDeliveryId());
|
2017-09-22 15:28:19 +03:00
|
|
|
$siteDeliverys = RCrmActions::DeliveryList();
|
2021-08-19 15:46:03 +03:00
|
|
|
|
2017-09-22 15:28:19 +03:00
|
|
|
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']) {
|
2021-08-19 15:46:03 +03:00
|
|
|
$service = explode(':', $delivery['CODE']);
|
|
|
|
$shipment = ['id' => $delivery['PARENT_ID'], 'service' => $service[1]];
|
2016-10-04 17:57:39 +03:00
|
|
|
} else {
|
2021-08-19 15:46:03 +03:00
|
|
|
$shipment = ['id' => $delivery['ID']];
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
$arOrder['DELIVERYS'][] = $shipment;
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
$paymentList = $obOrder->getPaymentCollection();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
foreach ($paymentList as $paymentData) {
|
|
|
|
$arOrder['PAYMENTS'][] = $paymentData->getFields()->getValues();
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
$basketItems = $obOrder->getBasket();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
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;
|
|
|
|
}
|
2021-08-19 15:46:03 +03:00
|
|
|
|
2021-05-31 11:14:02 +03:00
|
|
|
/**
|
|
|
|
* @param \Bitrix\Sale\Internals\Fields $product
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public static function getDiscountManualAmount(Fields $product): float
|
|
|
|
{
|
|
|
|
if ($product->get('CUSTOM_PRICE') === 'Y') {
|
|
|
|
$sumDifference = $product->get('BASE_PRICE') - $product->get('PRICE');
|
|
|
|
return $sumDifference > 0 ? $sumDifference : 0.0;
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-05-31 11:14:02 +03:00
|
|
|
$discount = (double) $product->get('DISCOUNT_PRICE');
|
|
|
|
$dpItem = $product->get('BASE_PRICE') - $product->get('PRICE');
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-05-31 11:14:02 +03:00
|
|
|
if ($dpItem > 0 && $discount <= 0) {
|
|
|
|
return $dpItem;
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
2021-05-31 11:14:02 +03:00
|
|
|
return $discount;
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|