1
0
mirror of synced 2024-11-23 05:46:09 +03:00
bitrix-module/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php

695 lines
28 KiB
PHP
Raw Normal View History

2016-09-15 16:42:10 +03:00
<?php
IncludeModuleLangFile(__FILE__);
class RetailCrmOrder
{
/**
*
* Creates order or returns order for mass upload
*
* @param array $arFields
* @param $api
* @param $arParams
* @param bool $send
* @param null $site
* @param string $methodApi
*
2016-09-15 16:42:10 +03:00
* @return boolean
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
2016-09-15 16:42:10 +03:00
*/
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;
}
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
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'],
2020-04-24 13:18:18 +03:00
'customer' => isset($arParams['customerCorporate'])
? array('id' => $arParams['customerCorporate']['id'])
: array('externalId' => $arFields['USER_ID']),
2016-09-15 16:42:10 +03:00
'orderType' => isset($arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']]) ?
$arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']] : '',
2016-09-15 16:42:10 +03:00
'status' => isset($arParams['optionsPayStatuses'][$arFields['STATUS_ID']]) ?
$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']
),
);
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
2016-09-15 16:42:10 +03:00
$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 {
// 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
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();
2020-04-24 13:18:18 +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
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();
2020-04-24 13:18:18 +03:00
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'];
}
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;
if ('ordersEdit' == $methodApi) {
$response = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $order['externalId']);
if (isset($response['order'])) {
2019-12-24 15:41:12 +03:00
foreach ($response['order']['items'] as $k => $item) {
$externalId = $k ."_". $item['offer']['externalId'];
$orderItems[$externalId] = $item;
}
}
}
2017-09-04 11:36:04 +03:00
//basket
2019-12-24 15:41:12 +03:00
foreach ($arFields['BASKET'] as $position => $product) {
2020-04-24 13:18:18 +03:00
$externalId = $position . "_" . $product['PRODUCT_ID'];
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'];
$key = array_search("bitrix", array_column($externalIds, 'code'));
2019-12-24 15:41:12 +03:00
if ($externalIds[$key]['code'] == "bitrix") {
$externalIds[$key] = array(
'code' => 'bitrix',
'value' => $externalId,
);
} else {
$externalIds[] = array(
2019-12-24 15:41:12 +03:00
'code' => 'bitrix',
'value' => $externalId,
);
}
} else { //create
$externalIds = array(
array(
2019-12-24 15:41:12 +03:00
'code' => 'bitrix',
'value' => $externalId,
)
);
}
2016-09-15 16:42:10 +03:00
$item = array(
2020-04-24 13:18:18 +03:00
'externalIds' => $externalIds,
2016-09-15 16:42:10 +03:00
'quantity' => $product['QUANTITY'],
'offer' => array(
'externalId' => $product['PRODUCT_ID'],
'xmlId' => $product['PRODUCT_XML_ID']
),
2016-09-15 16:42:10 +03:00
'productName' => $product['NAME']
);
2020-04-30 11:56:43 +03:00
if (isset($itemId)) {
$item['id'] = $itemId;
}
2016-09-15 16:42:10 +03:00
$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'];
2020-02-18 10:54:42 +03:00
$dpItem = $product['BASE_PRICE'] - $product['PRICE'];
2020-02-18 10:54:42 +03:00
if ( $dpItem > 0 && $discount <= 0) {
$discount = $dpItem;
}
2017-09-04 11:36:04 +03:00
$item['discountManualPercent'] = 0;
$item['discountManualAmount'] = $discount;
$item['initialPrice'] = (double) $product['BASE_PRICE'];
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) {
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'])) {
$pm['externalId'] = RCrmActions::generatePaymentExternalId($payment['ID']);
}
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.'
);
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
}
}
if ('ordersEdit' === $methodApi) {
$order = RetailCrmService::unsetIntegrationDeliveryFields($order);
}
2016-09-15 16:42:10 +03:00
$normalizer = new RestNormalizer();
$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
2020-04-24 13:18:18 +03:00
if ($send) {
2016-09-15 16:42:10 +03:00
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 int $pSize
* @param bool $failed -- flag to export failed orders
* @param bool $orderList
*
2016-09-15 16:42:10 +03:00
* @return boolean
* @throws \Bitrix\Main\ArgumentNullException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
* @throws \Bitrix\Main\ArgumentException
2016-09-15 16:42:10 +03:00
*/
2016-10-04 17:57:39 +03:00
public static function uploadOrders($pSize = 50, $failed = false, $orderList = false)
{
2020-04-24 13:18:18 +03:00
if (!RetailcrmDependencyLoader::loadDependencies()) {
2016-09-15 16:42:10 +03:00
return true;
}
$resOrders = array();
$resCustomers = array();
2020-04-24 13:18:18 +03:00
$resCustomersAdded = array();
$resCustomersCorporate = array();
2016-09-15 16:42:10 +03:00
$orderIds = array();
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;
} 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')
));
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
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
$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();
$getSite = function ($key) use ($optionsSitesList) {
if ($optionsSitesList) {
if (array_key_exists($key, $optionsSitesList) && $optionsSitesList[$key] != null) {
return $optionsSitesList[$key];
} else {
return false;
}
}
2016-09-15 16:42:10 +03:00
2020-04-24 13:18:18 +03:00
return null;
};
2016-09-15 16:42:10 +03:00
2020-04-24 13:18:18 +03:00
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
2016-09-15 16:42:10 +03:00
$arParams = array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps,
'optionsLegalDetails' => $optionsLegalDetails,
'optionsContragentType' => $optionsContragentType,
'optionsSitesList' => $optionsSitesList,
'optionsCustomFields' => $optionsCustomFields,
);
$recOrders = array();
2020-04-24 13:18:18 +03:00
2016-10-04 17:57:39 +03:00
foreach ($orderIds as $orderId) {
2020-04-24 13:18:18 +03:00
$site = null;
2016-09-15 16:42:10 +03:00
$id = \Bitrix\Sale\Order::load($orderId);
2020-04-24 13:18:18 +03:00
2016-10-04 17:57:39 +03:00
if (!$id) {
2016-09-15 16:42:10 +03:00
continue;
}
2020-04-24 13:18:18 +03:00
$arCustomer = array();
$arCustomerCorporate = array();
2016-09-15 16:42:10 +03:00
$order = self::orderObjToArr($id);
$user = Bitrix\Main\UserTable::getById($order['USER_ID'])->fetch();
2020-04-24 13:18:18 +03:00
$site = $getSite($order['LID']);
if (true === $site) {
continue;
}
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;
2020-04-24 13:18:18 +03:00
$arParams['contactExId'] = $user['ID'];
} else {
$arCustomer = RetailCrmUser::customerSend(
$user,
$api,
$optionsContragentType[$order['PERSON_TYPE_ID']],
false,
$site
);
if (isset($arParams['contactExId'])) {
unset($arParams['contactExId']);
}
}
2017-09-04 11:36:04 +03:00
2018-10-04 16:28:14 +03:00
$arOrders = self::orderSend($order, $api, $arParams, false, $site);
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;
}
$email = isset($arCustomer['email']) ? $arCustomer['email'] : '';
2018-10-04 16:28:14 +03:00
2020-04-24 13:18:18 +03:00
if (!in_array($email, $resCustomersAdded)) {
$resCustomersAdded[] = $email;
$resCustomers[$order['LID']][] = $arCustomer;
}
$resCustomers[$order['LID']][] = $arCustomer;
2020-04-24 13:18:18 +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) {
2020-04-24 13:18:18 +03:00
$uploadItems = function ($pack, $method) use ($getSite, $api, $optionsSitesList) {
$uploaded = array();
foreach ($pack as $key => $itemLoad) {
$site = $getSite($key);
if (true === $site) {
2017-11-13 11:05:11 +03:00
continue;
}
2020-04-24 13:18:18 +03:00
/** @var \RetailCrm\Response\ApiResponse|bool $response */
$response = RCrmActions::apiMethod(
$api,
$method,
__METHOD__,
$itemLoad,
$site
);
if ($response === false) {
return false;
}
if ($response instanceof \RetailCrm\Response\ApiResponse) {
if ($response->offsetExists('uploadedCustomers')) {
$uploaded = array_merge($uploaded, $response['uploadedCustomers']);
}
if ($response->offsetExists('uploadedOrders')) {
$uploaded = array_merge($uploaded, $response['uploadedOrders']);
}
}
if (count($optionsSitesList) > 1) {
time_nanosleep(0, 250000000);
}
2016-09-15 16:42:10 +03:00
}
2020-04-24 13:18:18 +03:00
return $uploaded;
};
if (false === $uploadItems($resCustomers, 'customersUpload')) {
return false;
2016-09-15 16:42:10 +03:00
}
2020-04-24 13:18:18 +03:00
if ("Y" == RetailcrmConfigProvider::getCorporateClientStatus()) {
$cachedCorporateIds = array();
foreach ($resOrders as $packKey => $pack) {
foreach ($pack as $key => $orderData) {
if (isset($orderData['contragent']['contragentType'])
&& $orderData['contragent']['contragentType'] == 'legal-entity'
&& !empty($orderData['contragent']['legalName'])
) {
if (isset($cachedCorporateIds[$orderData['contragent']['legalName']])) {
$orderData['customer'] = array(
'id' => $cachedCorporateIds[$orderData['contragent']['legalName']]
);
} else {
$corpData = $api->customersCorporateList(array(
'nickName' => array($orderData['contragent']['legalName'])
));
if ($corpData
&& $corpData->isSuccessful()
&& $corpData->offsetExists('customersCorporate')
&& !empty($corpData['customersCorporate'])
) {
$corpData = $corpData['customersCorporate'];
$corpData = reset($corpData);
$orderData['customer'] = array('id' => $corpData['id']);
$cachedCorporateIds[$orderData['contragent']['legalName']] = $corpData['id'];
RetailCrmCorporateClient::addCustomersCorporateAddresses(
$orderData['customer']['id'],
$orderData['contragent']['legalName'],
$orderData['delivery']['address']['text'],
$api,
$site = null
);
} elseif (array_key_exists(
$orderData['contragent']['legalName'],
$resCustomersCorporate
)) {
$createResponse = $api
->customersCorporateCreate(
$resCustomersCorporate[$orderData['contragent']['legalName']]
);
if ($createResponse && $createResponse->isSuccessful()) {
$orderData['customer'] = array('id' => $createResponse['id']);
$cachedCorporateIds[$orderData['contragent']['legalName']]
= $createResponse['id'];
}
}
time_nanosleep(0, 250000000);
}
$pack[$key] = $orderData;
}
2017-11-13 11:05:11 +03:00
}
2020-04-24 13:18:18 +03:00
$resOrders[$packKey] = $pack;
2016-09-15 16:42:10 +03:00
}
}
2020-04-24 13:18:18 +03:00
if (false === $uploadItems($resOrders, 'ordersUpload')) {
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));
2016-09-15 16:42:10 +03:00
} elseif ($lastUpOrderId < max($recOrders) && $orderList === false) {
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
2020-04-24 13:18:18 +03:00
/**
* Returns true if provided order array is corporate order data
*
* @param array|\ArrayAccess $order
*
* @return bool
*/
public static function isOrderCorporate($order)
{
return (is_array($order) || $order instanceof ArrayAccess)
&& isset($order['customer'])
&& isset($order['customer']['type'])
&& $order['customer']['type'] == 'customer_corporate';
}
/**
* Converts order object to array
*
* @param \Bitrix\Sale\Order $obOrder
*
* @return array
* @throws \Bitrix\Main\SystemException
*/
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'),
2020-04-24 13:18:18 +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();
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()) {
2016-09-15 16:42:10 +03:00
$delivery = \Bitrix\Sale\Delivery\Services\Manager::getById($shipmentData->getDeliveryId());
$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();
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;
}
}