2016-09-15 16:42:10 +03:00
|
|
|
|
<?php
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
|
|
|
|
use Bitrix\Main\ArgumentException;
|
|
|
|
|
use Bitrix\Main\ArgumentNullException;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
use Bitrix\Main\ArgumentOutOfRangeException;
|
2021-08-19 15:46:03 +03:00
|
|
|
|
use Bitrix\Main\Context;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
use Bitrix\Main\NotSupportedException;
|
|
|
|
|
use Bitrix\Main\SystemException;
|
2021-08-19 15:46:03 +03:00
|
|
|
|
use Bitrix\Sale\Basket;
|
|
|
|
|
use Bitrix\Sale\Delivery\Services\EmptyDeliveryService;
|
|
|
|
|
use Bitrix\Sale\Delivery\Services\Manager;
|
|
|
|
|
use Bitrix\Sale\Fuser;
|
|
|
|
|
use Bitrix\Sale\Internals\PaymentTable;
|
|
|
|
|
use Bitrix\Sale\Location\Search\Finder;
|
|
|
|
|
use Bitrix\Sale\Order;
|
|
|
|
|
use Bitrix\Sale\OrderUserProperties;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
use Bitrix\Sale\Payment;
|
2022-06-28 15:28:28 +03:00
|
|
|
|
use Intaro\RetailCrm\Component\ServiceLocator;
|
|
|
|
|
use Intaro\RetailCrm\Component\Builder\Bitrix\LoyaltyDataBuilder;
|
2021-08-19 15:46:03 +03:00
|
|
|
|
use Intaro\RetailCrm\Service\ManagerService;
|
2022-06-28 15:28:28 +03:00
|
|
|
|
use Intaro\RetailCrm\Service\OrderLoyaltyDataService;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
use Intaro\RetailCrm\Component\ConfigProvider;
|
|
|
|
|
use Intaro\RetailCrm\Component\Constants;
|
|
|
|
|
use Intaro\RetailCrm\Component\Handlers\EventsHandlers;
|
2023-04-26 17:18:38 +03:00
|
|
|
|
use RetailCrm\Response\ApiResponse;
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
IncludeModuleLangFile(__FILE__);
|
|
|
|
|
class RetailCrmHistory
|
|
|
|
|
{
|
|
|
|
|
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';
|
2023-12-21 17:24:46 +03:00
|
|
|
|
public static $CRM_PAYMENT = 'payment_arr';
|
2016-09-15 16:42:10 +03:00
|
|
|
|
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 = 'order_history';
|
2020-04-24 13:18:18 +03:00
|
|
|
|
public static $CRM_CUSTOMER_CORPORATE_HISTORY = 'customer_corp_history';
|
2016-09-15 16:42:10 +03:00
|
|
|
|
public static $CRM_CATALOG_BASE_PRICE = 'catalog_base_price';
|
|
|
|
|
public static $CRM_ORDER_NUMBERS = 'order_numbers';
|
|
|
|
|
public static $CRM_CANSEL_ORDER = 'cansel_order';
|
2017-12-04 17:44:30 +03:00
|
|
|
|
public static $CRM_CURRENCY = 'currency';
|
2020-02-18 10:54:42 +03:00
|
|
|
|
public static $CRM_DISCOUNT_ROUND = 'discount_round';
|
2023-12-19 17:13:31 +03:00
|
|
|
|
public static $CUSTOM_FIELDS_IS_ACTIVE = 'N';
|
2023-04-26 17:18:38 +03:00
|
|
|
|
const PAGE_LIMIT = 25;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2024-01-24 15:01:13 +03:00
|
|
|
|
private static $optionsPayment = [];
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
public static function customerHistory()
|
|
|
|
|
{
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!RetailcrmDependencyLoader::loadDependencies()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$page = 1;
|
|
|
|
|
$historyFilter = [];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$historyStart = RetailcrmConfigProvider::getCustomersHistorySinceId();
|
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$matchedCustomFields = RetailcrmConfigProvider::getMatchedUserFields();
|
|
|
|
|
$matchedCustomFields = is_array($matchedCustomFields) ? array_flip($matchedCustomFields) : [];
|
|
|
|
|
|
|
|
|
|
if (method_exists(RCrmActions::class, 'getTypeUserField')
|
|
|
|
|
&& method_exists(RCrmActions::class, 'convertCrmValueToCmsField')
|
|
|
|
|
) {
|
|
|
|
|
self::$CUSTOM_FIELDS_IS_ACTIVE = RetailcrmConfigProvider::getCustomFieldsStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$customUserFieldTypes = [];
|
|
|
|
|
|
|
|
|
|
if (self::$CUSTOM_FIELDS_IS_ACTIVE === 'Y') {
|
|
|
|
|
$customUserFieldTypes = RCrmActions::getTypeUserField();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($historyStart && $historyStart > 0) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$historyFilter['sinceId'] = $historyStart;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
do {
|
|
|
|
|
$historyResponse = RCrmActions::apiMethod($api, 'customersHistory', __METHOD__, $historyFilter);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
if (
|
|
|
|
|
!$historyResponse instanceof ApiResponse
|
|
|
|
|
|| !$historyResponse->isSuccessful()
|
|
|
|
|
|| empty($historyResponse['history'])
|
|
|
|
|
|| empty($historyResponse['pagination'])
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$customerHistory = $historyResponse['history'];
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
Logger::getInstance()->write($customerHistory, 'customerHistory');
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$customers = self::assemblyCustomer($customerHistory);
|
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
// Set sinceId for customer history
|
|
|
|
|
$lastId = array_pop($customerHistory);
|
|
|
|
|
$historyFilter['sinceId'] = $lastId['id'];
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
RetailcrmConfigProvider::setCustomersHistorySinceId($lastId['id']);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$newUser = new CUser();
|
2021-03-23 14:36:48 +03:00
|
|
|
|
$customerBuilder = new CustomerBuilder();
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($customers as $customer) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if (function_exists('retailCrmBeforeCustomerSave')) {
|
|
|
|
|
$newResCustomer = retailCrmBeforeCustomerSave($customer);
|
|
|
|
|
if (is_array($newResCustomer) && !empty($newResCustomer)) {
|
|
|
|
|
$customer = $newResCustomer;
|
2017-09-04 11:36:04 +03:00
|
|
|
|
} elseif ($newResCustomer === false) {
|
|
|
|
|
RCrmActions::eventLog('RetailCrmHistory::customerHistory', 'retailCrmBeforeCustomerSave()', 'UserCrmId = ' . $customer['id'] . '. Sending canceled after retailCrmBeforeCustomerSave');
|
|
|
|
|
|
|
|
|
|
continue;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($customer['deleted'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2020-07-23 14:49:55 +03:00
|
|
|
|
if (RetailcrmConfigProvider::isPhoneRequired()) {
|
|
|
|
|
if (empty($customer['phones'])) {
|
|
|
|
|
Logger::getInstance()->write('$customer["phones"] is empty. Customer ' . $customer['id'] . ' cannot be created', 'createCustomerError');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 11:39:23 +03:00
|
|
|
|
if (isset($customer['externalId']) && !is_numeric($customer['externalId'])) {
|
|
|
|
|
unset($customer['externalId']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$customerBuilder->setDataCrm($customer)->build();
|
|
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$customFields = self::getCustomUserFields($customer, $matchedCustomFields, $customUserFieldTypes);
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if (!isset($customer['externalId'])) {
|
|
|
|
|
if (!isset($customer['id'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$registerNewUser = true;
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2020-07-23 16:28:36 +03:00
|
|
|
|
if (!empty($customer['email'])) {
|
2023-03-31 11:44:26 +03:00
|
|
|
|
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'DESC'), ['=EMAIL' => $customer['email']]);
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
switch ($dbUser->SelectedRowsCount()) {
|
|
|
|
|
case 0:
|
|
|
|
|
$login = $customer['email'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$customerBuilder->setLogin($login);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
$arUser = $dbUser->Fetch();
|
|
|
|
|
$registeredUserID = $arUser['ID'];
|
|
|
|
|
$registerNewUser = false;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-03-31 11:44:26 +03:00
|
|
|
|
$lastBitrixUser = $dbUser->Fetch();
|
|
|
|
|
$registeredUserID = $lastBitrixUser['ID'];
|
|
|
|
|
$registerNewUser = false;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($registerNewUser === true) {
|
2022-12-05 16:32:28 +03:00
|
|
|
|
$customerBuilder->buildPassword();
|
2023-08-22 13:09:20 +03:00
|
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$registeredUserID = $newUser->Add(self::getDataUser($customerBuilder, $customFields));
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if ($registeredUserID === false) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'CUser::Register',
|
|
|
|
|
'Error register user: ' . $newUser->LAST_ERROR
|
|
|
|
|
);
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 11:44:26 +03:00
|
|
|
|
if (
|
|
|
|
|
RCrmActions::apiMethod
|
|
|
|
|
(
|
|
|
|
|
$api,
|
|
|
|
|
'customersFixExternalIds',
|
|
|
|
|
__METHOD__,
|
|
|
|
|
[['id' => $customer['id'], 'externalId' => $registeredUserID]]
|
|
|
|
|
) == false
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$customer['externalId'] = $registeredUserID;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($customer['externalId'])) {
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$customerBuilder->setDataCrm($customer);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($customer['phones'])) {
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$customerBuilder->setUser(
|
|
|
|
|
CUser::GetList(
|
|
|
|
|
($by = "ID"),
|
|
|
|
|
($order = "desc"),
|
2023-12-21 17:24:46 +03:00
|
|
|
|
['ID' => $customer['externalId']],
|
|
|
|
|
['FIELDS' => ['PERSONAL_PHONE', 'PERSONAL_MOBILE']]
|
2020-07-14 13:47:26 +03:00
|
|
|
|
)->fetch()
|
|
|
|
|
);
|
2016-10-31 17:56:11 +03:00
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2023-08-22 13:09:20 +03:00
|
|
|
|
$customerArray = $customerBuilder->getCustomer()->getObjectToArray();
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$customerArray = array_merge($customerArray, $customFields);
|
2023-08-22 13:09:20 +03:00
|
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$queryUpdate = $newUser->Update($customer['externalId'], self::convertBooleanFields($customerArray));
|
|
|
|
|
|
|
|
|
|
if (!$queryUpdate) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::customerHistory',
|
|
|
|
|
'Error update user',
|
|
|
|
|
$newUser->LAST_ERROR
|
|
|
|
|
);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if (function_exists('retailCrmAfterCustomerSave')) {
|
|
|
|
|
retailCrmAfterCustomerSave($customer);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
2021-03-23 14:36:48 +03:00
|
|
|
|
$customerBuilder->reset();
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$page++;
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
if ($page > self::PAGE_LIMIT) {
|
|
|
|
|
break;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2023-04-26 17:18:38 +03:00
|
|
|
|
} while ($historyResponse['pagination']['currentPage'] < $historyResponse['pagination']['totalPageCount']);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentException
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentNullException
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentOutOfRangeException
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentTypeException
|
|
|
|
|
* @throws \Bitrix\Main\NotImplementedException
|
|
|
|
|
* @throws \Bitrix\Main\NotSupportedException
|
|
|
|
|
* @throws \Bitrix\Main\ObjectException
|
|
|
|
|
* @throws \Bitrix\Main\ObjectNotFoundException
|
|
|
|
|
* @throws \Bitrix\Main\ObjectPropertyException
|
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
|
|
|
|
*/
|
|
|
|
|
public static function orderHistory(): bool
|
2016-10-04 17:57:39 +03:00
|
|
|
|
{
|
2016-09-15 16:42:10 +03:00
|
|
|
|
global $USER;
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
if (is_object($USER) === false) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$USER = new RetailUser();
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!RetailcrmDependencyLoader::loadDependencies()) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$optionsOrderTypes = RetailcrmConfigProvider::getOrderTypes();
|
|
|
|
|
$optionsDelivTypes = array_flip(RetailcrmConfigProvider::getDeliveryTypes());
|
|
|
|
|
$optionsPayStatuses = array_flip(RetailcrmConfigProvider::getPaymentStatuses()); // --statuses
|
|
|
|
|
$optionsOrderProps = RetailcrmConfigProvider::getOrderProps();
|
|
|
|
|
$optionsLegalDetails = RetailcrmConfigProvider::getLegalDetails();
|
|
|
|
|
$optionsOrderNumbers = RetailcrmConfigProvider::getOrderNumbers();
|
|
|
|
|
$optionsCanselOrder = RetailcrmConfigProvider::getCancellableOrderPaymentStatuses();
|
|
|
|
|
$currency = RetailcrmConfigProvider::getCurrencyOrDefault();
|
|
|
|
|
$contragentTypes = array_flip(RetailcrmConfigProvider::getContragentTypes());
|
2020-07-23 16:28:36 +03:00
|
|
|
|
$shipmentDeducted = RetailcrmConfigProvider::getShipmentDeducted();
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2024-01-24 15:01:13 +03:00
|
|
|
|
$optionsPayment = [
|
|
|
|
|
'payTypes' => array_flip(RetailcrmConfigProvider::getPaymentTypes()),
|
|
|
|
|
'paymentList' => array_flip(RetailcrmConfigProvider::getPayment()),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (RetailcrmConfigProvider::getSyncIntegrationPayment() === 'Y') {
|
|
|
|
|
$substitutedPayment = RetailcrmConfigProvider::getSubstitutionPaymentList();
|
|
|
|
|
|
|
|
|
|
foreach ($substitutedPayment as $origCode => $subsCode) {
|
|
|
|
|
if (isset($optionsPayment['payTypes'][$origCode])) {
|
|
|
|
|
$optionsPayment['payTypes'][$subsCode] = $optionsPayment['payTypes'][$origCode];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$optionsPayment = $optionsPayment;
|
|
|
|
|
|
|
|
|
|
unset($optionsPayment);
|
|
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$matchedCustomUserFields = RetailcrmConfigProvider::getMatchedUserFields() ?? [];
|
|
|
|
|
$matchedCustomUserFields = is_array($matchedCustomUserFields) ? array_flip($matchedCustomUserFields) : [];
|
|
|
|
|
|
|
|
|
|
$matchedCustomOrderFields = RetailcrmConfigProvider::getMatchedOrderProps() ?? [];
|
|
|
|
|
$matchedCustomOrderFields = is_array($matchedCustomOrderFields) ? array_flip($matchedCustomOrderFields) : [];
|
|
|
|
|
|
|
|
|
|
if (method_exists(RCrmActions::class, 'getTypeUserField')
|
|
|
|
|
&& method_exists(RCrmActions::class, 'convertCrmValueToCmsField')
|
|
|
|
|
) {
|
|
|
|
|
self::$CUSTOM_FIELDS_IS_ACTIVE = RetailcrmConfigProvider::getCustomFieldsStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$customUserFieldTypes = [];
|
|
|
|
|
|
|
|
|
|
if (self::$CUSTOM_FIELDS_IS_ACTIVE === 'Y') {
|
|
|
|
|
$customUserFieldTypes = RCrmActions::getTypeUserField();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$page = 1;
|
2022-06-28 15:28:28 +03:00
|
|
|
|
/* @var OrderLoyaltyDataService $orderLoyaltyDataService */
|
|
|
|
|
$orderLoyaltyDataService = ServiceLocator::get(OrderLoyaltyDataService::class);
|
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$historyFilter = [];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$historyStart = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($historyStart && $historyStart > 0) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$historyFilter['sinceId'] = $historyStart;
|
2018-09-21 12:22:49 +03:00
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
do {
|
|
|
|
|
$historyResponse = RCrmActions::apiMethod($api, 'ordersHistory', __METHOD__, $historyFilter);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
if (
|
|
|
|
|
!$historyResponse instanceof ApiResponse
|
|
|
|
|
|| !$historyResponse->isSuccessful()
|
|
|
|
|
|| empty($historyResponse['history'])
|
|
|
|
|
|| empty($historyResponse['pagination'])
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$orderHistory = $historyResponse['history'];
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
Logger::getInstance()->write($orderHistory, 'orderHistory');
|
2018-02-27 15:29:43 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$orders = self::assemblyOrder($orderHistory);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
|
2018-02-27 15:29:43 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
// Set sinceId for order history
|
|
|
|
|
$lastId = array_pop($orderHistory);
|
|
|
|
|
$historyFilter['sinceId'] = $lastId['id'];
|
|
|
|
|
|
|
|
|
|
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY, $lastId['id']);
|
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
//orders with changes
|
2016-09-15 16:42:10 +03:00
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
if (function_exists('retailCrmBeforeOrderSave')) {
|
|
|
|
|
$newResOrder = retailCrmBeforeOrderSave($order);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
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-06-13 13:05:58 +03:00
|
|
|
|
RCrmActions::eventLog('RetailCrmHistory::orderHistory',
|
|
|
|
|
'retailCrmBeforeOrderSave()',
|
|
|
|
|
'OrderCrmId = ' . $order['id'] . '. Sending canceled after retailCrmBeforeOrderSave'
|
|
|
|
|
);
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
|
|
|
|
continue;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-27 15:29:43 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
Logger::getInstance()->write($order, 'assemblyOrderHistory');
|
2018-02-27 15:29:43 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if (isset($order['deleted'])) {
|
|
|
|
|
if (isset($order['externalId'])) {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
self::cancelOrder($order['externalId']);
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$site = self::getSite($order['site']);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
|
|
|
|
if (empty($site)) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
__CLASS__ . '::' . __METHOD__,
|
|
|
|
|
'Bitrix\Sale\Order::create',
|
|
|
|
|
'Site = ' . $order['site'] . ' not found in setting. Order crm id=' . $order['id']
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 11:39:23 +03:00
|
|
|
|
if (isset($order['customer']['externalId']) && !is_numeric($order['customer']['externalId'])) {
|
|
|
|
|
unset($order['customer']['externalId']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder = new CorporateCustomerBuilder();
|
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$corporateContact = [];
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$orderCustomerExtId = $order['customer']['externalId'] ?? null;
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setOrderCustomerExtId($orderCustomerExtId)
|
|
|
|
|
->setContragentTypes($contragentTypes)
|
|
|
|
|
->setDataCrm($order)
|
|
|
|
|
->build();
|
2023-01-09 12:18:37 +03:00
|
|
|
|
$corporateCustomerBuilder->getCustomerBuilder()->buildPassword();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
|
|
|
|
if (RetailCrmOrder::isOrderCorporate($order)) {
|
|
|
|
|
// Fetch contact only if we think it's data is not fully present in order
|
|
|
|
|
if (!empty($order['contact'])) {
|
|
|
|
|
if (isset($order['contact']['email'])) {
|
|
|
|
|
$corporateContact = $order['contact'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$orderCustomerExtId = $corporateContact['externalId'] ?? null;
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setCorporateContact($corporateContact)
|
|
|
|
|
->setOrderCustomerExtId($orderCustomerExtId);
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
} else {
|
|
|
|
|
$response = false;
|
|
|
|
|
|
|
|
|
|
if (isset($order['contact']['externalId'])) {
|
|
|
|
|
$response = RCrmActions::apiMethod(
|
|
|
|
|
$api,
|
|
|
|
|
'customersGet',
|
|
|
|
|
__METHOD__,
|
|
|
|
|
$order['contact']['externalId'],
|
|
|
|
|
$order['site']
|
|
|
|
|
);
|
|
|
|
|
} elseif (isset($order['contact']['id'])) {
|
|
|
|
|
$response = RCrmActions::apiMethod(
|
|
|
|
|
$api,
|
|
|
|
|
'customersGetById',
|
|
|
|
|
__METHOD__,
|
|
|
|
|
$order['contact']['id'],
|
|
|
|
|
$order['site']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($response && isset($response['customer'])) {
|
|
|
|
|
$corporateContact = $response['customer'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$orderCustomerExtId = $corporateContact['externalId'] ?? null;
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setCorporateContact($corporateContact)
|
|
|
|
|
->setOrderCustomerExtId($orderCustomerExtId);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if (!isset($order['externalId'])) {
|
2023-12-21 17:24:46 +03:00
|
|
|
|
if (ConfigProvider::useCrmOrderMethods() === 'Y') {
|
|
|
|
|
$orderMethods = ConfigProvider::getCrmOrderMethods();
|
|
|
|
|
|
|
|
|
|
// 1. Клиент активировал опцию, но не выбрал способы оформления - пропускаем все заказы.
|
|
|
|
|
// 2. Если способа оформления заказа нет в выбранном в настройках списке - пропускаем заказ.
|
|
|
|
|
if ($orderMethods === [] || !in_array($order['orderMethod'], $orderMethods, true)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (empty($orderCustomerExtId)) {
|
|
|
|
|
if (!isset($order['customer']['id'])
|
|
|
|
|
|| (RetailCrmOrder::isOrderCorporate($order)
|
|
|
|
|
&& (!isset($order['contact']['id']) || !isset($order['customer']['id'])))
|
|
|
|
|
) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 14:49:55 +03:00
|
|
|
|
if (RetailcrmConfigProvider::isPhoneRequired()) {
|
|
|
|
|
if (empty($order['customer']['phones'])) {
|
|
|
|
|
Logger::getInstance()->write('$customer["phones"] is empty. Order ' . $order['id'] . ' cannot be created', 'createCustomerError');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$login = null;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$registerNewUser = true;
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!isset($order['customer']['email']) || empty($order['customer']['email'])) {
|
|
|
|
|
if (RetailCrmOrder::isOrderCorporate($order) && !empty($corporateContact['email'])) {
|
|
|
|
|
$login = $corporateContact['email'];
|
|
|
|
|
$order['customer']['email'] = $corporateContact['email'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setLogin($login)
|
|
|
|
|
->setEmail($corporateContact['email']);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
} else {
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$login = uniqid('user_' . time()) . '@example.com';
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$order['customer']['email'] = $login;
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setLogin($login)
|
|
|
|
|
->setEmail($login);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$dbUser = CUser::GetList(
|
|
|
|
|
($by = 'ID'),
|
2023-03-31 11:44:26 +03:00
|
|
|
|
($sort = 'DESC'),
|
|
|
|
|
['=EMAIL' => $order['customer']['email']]
|
2020-04-24 13:18:18 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
switch ($dbUser->SelectedRowsCount()) {
|
|
|
|
|
case 0:
|
|
|
|
|
$login = $order['customer']['email'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setLogin($login);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
$arUser = $dbUser->Fetch();
|
|
|
|
|
$registeredUserID = $arUser['ID'];
|
|
|
|
|
$registerNewUser = false;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-03-31 11:44:26 +03:00
|
|
|
|
$lastBitrixUser = $dbUser->Fetch();
|
|
|
|
|
$registeredUserID = $lastBitrixUser['ID'];
|
|
|
|
|
$registerNewUser = false;
|
2020-04-24 13:18:18 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if ($registerNewUser === true) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$userData = RetailCrmOrder::isOrderCorporate($order)
|
|
|
|
|
? $corporateContact
|
|
|
|
|
: $order['customer'];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setCorporateContact($userData);
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$newUser = new CUser();
|
2023-08-22 13:09:20 +03:00
|
|
|
|
$customerArray = $corporateCustomerBuilder->getCustomer()->getObjectToArray();
|
|
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$customFields = self::getCustomUserFields($userData, $matchedCustomUserFields, $customUserFieldTypes);
|
|
|
|
|
$customerArray = array_merge($customerArray, $customFields);
|
|
|
|
|
|
2023-08-22 13:09:20 +03:00
|
|
|
|
if (!array_key_exists('UF_SUBSCRIBE_USER_EMAIL', $customerArray)) {
|
|
|
|
|
$customerArray['UF_SUBSCRIBE_USER_EMAIL'] = 'Y';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$registeredUserID = $newUser->Add(self::convertBooleanFields($customerArray));
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if ($registeredUserID === false) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'CUser::Register',
|
|
|
|
|
'Error register user' . $newUser->LAST_ERROR
|
|
|
|
|
);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
|
|
|
|
continue;
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if(RCrmActions::apiMethod(
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$api,
|
|
|
|
|
'customersFixExternalIds',
|
|
|
|
|
__METHOD__,
|
2023-12-21 17:24:46 +03:00
|
|
|
|
[[
|
2020-07-14 13:47:26 +03:00
|
|
|
|
'id' => $order['customer']['id'],
|
|
|
|
|
'externalId' => $registeredUserID
|
2023-12-21 17:24:46 +03:00
|
|
|
|
]]) == false
|
2020-04-24 13:18:18 +03:00
|
|
|
|
) {
|
2018-09-21 12:22:49 +03:00
|
|
|
|
continue;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$orderCustomerExtId = isset($registeredUserID) ? $registeredUserID : null;
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$corporateCustomerBuilder->setOrderCustomerExtId($orderCustomerExtId);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$buyerProfileToAppend = [];
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (RetailCrmOrder::isOrderCorporate($order) && !empty($order['company'])) {
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$buyerProfile = $corporateCustomerBuilder->getBuyerProfile()->getObjectToArray();
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$buyerProfileToAppend = OrderUserProperties::getList([
|
2020-04-24 13:18:18 +03:00
|
|
|
|
"filter" => $buyerProfile
|
2023-12-21 17:24:46 +03:00
|
|
|
|
])->fetch();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
|
|
|
|
if (empty($buyerProfileToAppend)) {
|
|
|
|
|
$buyerProfileInstance = new CSaleOrderUserProps();
|
|
|
|
|
|
|
|
|
|
if ($buyerProfileInstance->Add($buyerProfile)) {
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$buyerProfileToAppend = OrderUserProperties::getList([
|
2020-04-24 13:18:18 +03:00
|
|
|
|
"filter" => $buyerProfile
|
2023-12-21 17:24:46 +03:00
|
|
|
|
])->fetch();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$newOrder = Order::create($site, $orderCustomerExtId, $currency);
|
|
|
|
|
|
2023-03-28 18:12:03 +03:00
|
|
|
|
self::setManager($newOrder, $order);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
|
|
|
|
if (isset($buyerProfileToAppend['ID']) && isset($optionsLegalDetails['legalName'])) {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$newOrder->setFields([
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$optionsLegalDetails['legalName'] => $buyerProfileToAppend['NAME'],
|
2021-08-19 15:46:03 +03:00
|
|
|
|
'PERSON_TYPE_ID' => $buyerProfileToAppend['PERSON_TYPE_ID'],
|
|
|
|
|
]);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
if (!is_object($newOrder) || !$newOrder instanceof Order) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Bitrix\Sale\Order::create',
|
|
|
|
|
'Error order create'
|
|
|
|
|
);
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$externalId = $newOrder->getId();
|
|
|
|
|
$order['externalId'] = $externalId;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
if (isset($order['externalId'])) {
|
2016-10-06 17:34:35 +03:00
|
|
|
|
$itemUpdate = false;
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2019-09-17 18:10:30 +03:00
|
|
|
|
if ($order['externalId'] && is_numeric($order['externalId'])) {
|
2017-12-04 17:44:30 +03:00
|
|
|
|
try {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$newOrder = Order::load($order['externalId']);
|
|
|
|
|
} catch (ArgumentNullException $e) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Bitrix\Sale\Order::load',
|
|
|
|
|
$e->getMessage() . ': ' . $order['externalId']
|
|
|
|
|
);
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
2016-10-03 16:56:59 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
if (!isset($newOrder)) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Bitrix\Sale\Order::load',
|
|
|
|
|
'Error order load number=' . $order['number']
|
|
|
|
|
);
|
2018-02-27 15:29:43 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$site = self::getSite($order['site']);
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
if (null === $site) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Bitrix\Sale\Order::edit',
|
|
|
|
|
sprintf(
|
|
|
|
|
'Site = %s not found in settings. Order number = %s',
|
|
|
|
|
$order['site'],
|
|
|
|
|
$order['number']
|
|
|
|
|
)
|
|
|
|
|
);
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2023-03-28 18:12:03 +03:00
|
|
|
|
self::setManager($newOrder, $order);
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$propsRemove = false;
|
2016-10-03 16:56:59 +03:00
|
|
|
|
$personType = $newOrder->getField('PERSON_TYPE_ID');
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
if (RetailCrmOrder::isOrderCorporate($order)
|
|
|
|
|
|| (!empty($order['contragentType']) && $order['contragentType'] == 'legal-entity')
|
|
|
|
|
) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$personType = $contragentTypes['legal-entity'];
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$newOrder->setField('PERSON_TYPE_ID', $personType);
|
|
|
|
|
|
|
|
|
|
$propsRemove = true;
|
2020-04-24 13:18:18 +03:00
|
|
|
|
} else {
|
|
|
|
|
if (isset($order['orderType']) && $order['orderType']) {
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$nType = [];
|
|
|
|
|
$tList = RCrmActions::OrderTypesList([['LID' => $site]]);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
foreach ($tList as $type) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (isset($optionsOrderTypes[$type['ID']])) {
|
|
|
|
|
$nType[$optionsOrderTypes[$type['ID']]] = $type['ID'];
|
|
|
|
|
}
|
2017-09-22 15:28:19 +03:00
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$newOptionsOrderTypes = $nType;
|
|
|
|
|
|
|
|
|
|
if ($newOptionsOrderTypes[$order['orderType']]) {
|
|
|
|
|
if ($personType != $newOptionsOrderTypes[$order['orderType']] && $personType != 0) {
|
|
|
|
|
$propsRemove = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$personType = $newOptionsOrderTypes[$order['orderType']];
|
|
|
|
|
$newOrder->setField('PERSON_TYPE_ID', $personType);
|
|
|
|
|
} elseif ($personType == 0) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'orderType not found',
|
|
|
|
|
'PERSON_TYPE_ID = 0'
|
|
|
|
|
);
|
2017-09-22 15:28:19 +03:00
|
|
|
|
}
|
2016-10-03 16:56:59 +03:00
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
//status
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($optionsPayStatuses[$order['status']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$newOrder->setField('STATUS_ID', $optionsPayStatuses[$order['status']]);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2023-03-28 18:12:03 +03:00
|
|
|
|
if (
|
|
|
|
|
is_array($optionsCanselOrder)
|
|
|
|
|
&& in_array($optionsPayStatuses[$order['status']], $optionsCanselOrder)
|
|
|
|
|
) {
|
2018-02-27 15:29:43 +03:00
|
|
|
|
self::unreserveShipment($newOrder);
|
2016-10-14 15:25:02 +03:00
|
|
|
|
$newOrder->setFieldNoDemand('CANCELED', 'Y');
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2016-10-14 15:25:02 +03:00
|
|
|
|
$newOrder->setFieldNoDemand('CANCELED', 'N');
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
|
|
|
|
if (array_key_exists('statusComment', $order)) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
self::setProp(
|
|
|
|
|
$newOrder,
|
|
|
|
|
RCrmActions::fromJSON($order['statusComment']),
|
|
|
|
|
'REASON_CANCELED'
|
|
|
|
|
);
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
2016-10-03 16:56:59 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
//props
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$propertyCollection = $newOrder->getPropertyCollection();
|
|
|
|
|
$propertyCollectionArr = $propertyCollection->getArray();
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$nProps = [];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($propertyCollectionArr['properties'] as $orderProp) {
|
|
|
|
|
if ($orderProp['ID'][0] == 'n') {
|
2016-10-03 16:56:59 +03:00
|
|
|
|
$orderProp['ID'] = substr($orderProp['ID'], 1);
|
2019-01-16 10:28:11 +03:00
|
|
|
|
$property = $propertyCollection->getItemById($orderProp['ID']);
|
|
|
|
|
|
|
|
|
|
if ($property) {
|
|
|
|
|
$orderProp['ID'] = $property->getField('ORDER_PROPS_ID');
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-10-03 16:56:59 +03:00
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-10-03 16:56:59 +03:00
|
|
|
|
$nProps[] = $orderProp;
|
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$orderDump = [];
|
2016-10-03 16:56:59 +03:00
|
|
|
|
$propertyCollectionArr['properties'] = $nProps;
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if ($propsRemove) {//delete props
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($propertyCollectionArr['properties'] as $orderProp) {
|
2017-10-24 11:41:11 +03:00
|
|
|
|
if ($orderProp['PROPS_GROUP_ID'] == 0) {
|
|
|
|
|
$somePropValue = $propertyCollection->getItemByOrderPropertyId($orderProp['ID']);
|
|
|
|
|
self::setProp($somePropValue);
|
|
|
|
|
}
|
2016-10-03 16:56:59 +03:00
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
|
2016-10-03 16:56:59 +03:00
|
|
|
|
|
|
|
|
|
$orderDump = $order;
|
|
|
|
|
$order = $orderCrm['order'];
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$propsKey = [];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($propertyCollectionArr['properties'] as $prop) {
|
2017-10-24 11:41:11 +03:00
|
|
|
|
if ($prop['PROPS_GROUP_ID'] != 0) {
|
|
|
|
|
$propsKey[$prop['CODE']]['ID'] = $prop['ID'];
|
|
|
|
|
$propsKey[$prop['CODE']]['TYPE'] = $prop['TYPE'];
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
|
|
|
|
// fio
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($order['firstName'] || $order['lastName'] || $order['patronymic']) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$fio = '';
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($propertyCollectionArr['properties'] as $prop) {
|
|
|
|
|
if (in_array($optionsOrderProps[$personType]['fio'], $prop)) {
|
2016-10-14 15:25:02 +03:00
|
|
|
|
$getFio = $newOrder->getPropertyCollection()->getItemByOrderPropertyId($prop['ID']);
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
2016-10-14 15:25:02 +03:00
|
|
|
|
if (method_exists($getFio, 'getValue')) {
|
|
|
|
|
$fio = $getFio->getValue();
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$fio = RCrmActions::explodeFio($fio);
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$newFio = [];
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($fio) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$newFio[] = isset($order['lastName'])
|
|
|
|
|
? RCrmActions::fromJSON($order['lastName'])
|
2021-08-19 15:46:03 +03:00
|
|
|
|
: ($fio['lastName'] ?? '');
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$newFio[] = isset($order['firstName'])
|
|
|
|
|
? RCrmActions::fromJSON($order['firstName'])
|
2021-08-19 15:46:03 +03:00
|
|
|
|
: ($fio['firstName'] ?? '');
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$newFio[] = isset($order['patronymic'])
|
|
|
|
|
? RCrmActions::fromJSON($order['patronymic'])
|
2021-08-19 15:46:03 +03:00
|
|
|
|
: ($fio['patronymic'] ?? '');
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2016-10-12 11:40:18 +03:00
|
|
|
|
$newFio[] = isset($order['lastName']) ? RCrmActions::fromJSON($order['lastName']) : '';
|
|
|
|
|
$newFio[] = isset($order['firstName']) ? RCrmActions::fromJSON($order['firstName']) : '';
|
|
|
|
|
$newFio[] = isset($order['patronymic']) ? RCrmActions::fromJSON($order['patronymic']) : '';
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$order['fio'] = trim(implode(' ', $newFio));
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2021-01-14 16:38:56 +03:00
|
|
|
|
if (array_key_exists('fio', $order)) {
|
|
|
|
|
$order['fio'] = str_replace("clear", "", $order['fio']);
|
|
|
|
|
}
|
2020-07-14 13:47:26 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
//optionsOrderProps
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($optionsOrderProps[$personType]) {
|
|
|
|
|
foreach ($optionsOrderProps[$personType] as $key => $orderProp) {
|
|
|
|
|
if (array_key_exists($key, $order)) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$somePropValue = $propertyCollection
|
|
|
|
|
->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
|
|
|
|
|
|
2020-11-11 14:46:27 +03:00
|
|
|
|
if ($key == 'fio' && '' !== trim($order['fio'])) {
|
2016-10-25 17:56:50 +03:00
|
|
|
|
self::setProp($somePropValue, $order[$key]);
|
|
|
|
|
} else {
|
|
|
|
|
self::setProp($somePropValue, RCrmActions::fromJSON($order[$key]));
|
|
|
|
|
}
|
2024-01-09 10:44:17 +03:00
|
|
|
|
} elseif ($propsKey[$orderProp]['TYPE'] === 'LOCATION'
|
|
|
|
|
&& (
|
|
|
|
|
isset($order['delivery']['address']['city'])
|
|
|
|
|
|| isset($order['delivery']['address']['region'])
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
$parameters = [
|
|
|
|
|
'filter' => [
|
|
|
|
|
'NAME.LANGUAGE_ID' => 'ru'
|
|
|
|
|
],
|
|
|
|
|
'limit' => 1,
|
|
|
|
|
'select' => ['*']
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$somePropValue = $propertyCollection
|
|
|
|
|
->getItemByOrderPropertyId($propsKey[$orderProp]['ID'])
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
$codeLocation = $somePropValue->getValue();
|
|
|
|
|
$subParameters = $parameters;
|
|
|
|
|
$subParameters['filter']['=CODE'] = RCrmActions::fromJSON($codeLocation);
|
|
|
|
|
$subLocation = Finder::find($subParameters)->fetch();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2024-01-09 10:44:17 +03:00
|
|
|
|
if (!isset($order['delivery']['address']['city'])) {
|
|
|
|
|
$parameters['filter']['=ID'] = RCrmActions::fromJSON($subLocation['CITY_ID']);
|
|
|
|
|
} else { // В системе город пишется с префиксом (г. Москва)
|
|
|
|
|
$loc = explode('.', $order['delivery']['address']['city']);
|
2022-06-14 11:57:44 +03:00
|
|
|
|
|
2024-01-09 10:44:17 +03:00
|
|
|
|
if (count($loc) === 1) {
|
2022-06-14 11:57:44 +03:00
|
|
|
|
$parameters['filter']['=NAME.NAME'] = RCrmActions::fromJSON(trim($loc[0]));
|
2018-05-23 12:19:59 +03:00
|
|
|
|
} else {
|
2022-06-14 11:57:44 +03:00
|
|
|
|
$parameters['filter']['=NAME.NAME'] = RCrmActions::fromJSON(trim($loc[1]));
|
2016-11-15 17:17:09 +03:00
|
|
|
|
}
|
2024-01-09 10:44:17 +03:00
|
|
|
|
}
|
2018-05-23 12:19:59 +03:00
|
|
|
|
|
2024-01-09 10:44:17 +03:00
|
|
|
|
if (!isset($order['delivery']['address']['region'])) {
|
|
|
|
|
$parameters['filter']['=PARENT.ID'] = RCrmActions::fromJSON($subLocation['REGION_ID']);
|
|
|
|
|
} else {
|
|
|
|
|
$parameters['filter']['PARENT.NAME.NAME'] = RCrmActions::fromJSON(trim($order['delivery']['address']['region']));
|
2022-06-14 11:57:44 +03:00
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2024-01-09 10:44:17 +03:00
|
|
|
|
$location = Finder::find($parameters)->fetch();
|
|
|
|
|
|
|
|
|
|
//При существовании района в локации, фильтр по региону изменяется
|
|
|
|
|
if (empty($location)) {
|
|
|
|
|
if (!isset($order['delivery']['address']['region'])) {
|
|
|
|
|
$parameters['filter']['=PARENT.PARENT.ID'] = RCrmActions::fromJSON($subLocation['REGION_ID']);
|
|
|
|
|
unset($parameters['filter']['=PARENT.ID']);
|
|
|
|
|
} else {
|
|
|
|
|
$parameters['filter']['PARENT.PARENT.NAME.NAME'] = RCrmActions::fromJSON(trim($order['delivery']['address']['region']));
|
|
|
|
|
unset($parameters['filter']['PARENT.NAME.NAME']);
|
2018-05-23 12:19:59 +03:00
|
|
|
|
}
|
2024-01-09 10:44:17 +03:00
|
|
|
|
|
|
|
|
|
$location = Finder::find($parameters)->fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($location)) {
|
|
|
|
|
self::setProp($somePropValue, $location['CODE']);
|
2018-05-23 12:19:59 +03:00
|
|
|
|
} else {
|
2024-01-09 10:44:17 +03:00
|
|
|
|
if (isset($order['externalId'])) {
|
|
|
|
|
$message = 'ExternalId: ' . $order['externalId'];
|
|
|
|
|
} else {
|
|
|
|
|
$message = 'CRM id: ' . $order['id'];
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'RetailCrmHistory::setProp',
|
2024-01-09 10:44:17 +03:00
|
|
|
|
'Ошибка обновления локации в заказе. ' . $message
|
2020-04-24 13:18:18 +03:00
|
|
|
|
);
|
2016-10-25 17:56:50 +03:00
|
|
|
|
}
|
2024-01-09 10:44:17 +03:00
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'RetailCrmHistory::setProp',
|
|
|
|
|
'Error when updating a location. Order: '
|
|
|
|
|
. $order['number'] . ' message:' . $exception->getMessage()
|
2020-04-24 13:18:18 +03:00
|
|
|
|
);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2024-01-09 10:44:17 +03:00
|
|
|
|
} elseif (is_array($order['delivery']['address']) && array_key_exists($key, $order['delivery']['address'])) {
|
|
|
|
|
$somePropValue = $propertyCollection
|
|
|
|
|
->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
|
|
|
|
|
self::setProp(
|
|
|
|
|
$somePropValue,
|
|
|
|
|
RCrmActions::fromJSON($order['delivery']['address'][$key])
|
|
|
|
|
);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-24 11:41:11 +03:00
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
if (isset($order['company']['id'])) {
|
|
|
|
|
if (!empty($order['company']['name'])) {
|
|
|
|
|
$order["legalName"] = $order['company']['name'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
// Corporate clients section
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($optionsLegalDetails[$personType]) {
|
|
|
|
|
foreach ($optionsLegalDetails[$personType] as $key => $orderProp) {
|
2019-03-28 14:35:26 +03:00
|
|
|
|
if (array_key_exists($key, $order)) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$somePropValue = $propertyCollection
|
|
|
|
|
->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
|
|
|
|
|
|
2019-09-17 18:10:30 +03:00
|
|
|
|
self::setProp($somePropValue, RCrmActions::fromJSON($order[$key]));
|
2023-03-15 11:55:04 +03:00
|
|
|
|
} elseif(is_array($order['contragent']) && array_key_exists($key, $order['contragent'])) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$somePropValue = $propertyCollection
|
|
|
|
|
->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
|
2019-11-01 14:22:23 +03:00
|
|
|
|
self::setProp($somePropValue, RCrmActions::fromJSON($order['contragent'][$key]));
|
2020-04-24 13:18:18 +03:00
|
|
|
|
} elseif (isset($order['company']) && (array_key_exists($key, $order['company'])
|
|
|
|
|
|| array_key_exists(
|
|
|
|
|
lcfirst(str_replace('legal', '', $key)),
|
|
|
|
|
$order['company'])
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$somePropValue = $propertyCollection
|
|
|
|
|
->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
|
|
|
|
|
|
|
|
|
|
// fallback for order[company][name]
|
|
|
|
|
if ($key == 'legalName') {
|
|
|
|
|
$key = 'name';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::setProp(
|
|
|
|
|
$somePropValue,
|
|
|
|
|
RCrmActions::fromJSON(
|
|
|
|
|
$key == 'legalAddress'
|
2021-08-19 15:46:03 +03:00
|
|
|
|
? ($order['company']['address']['text'] ?? '')
|
2020-04-24 13:18:18 +03:00
|
|
|
|
: $order['company'][$key]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} elseif (isset($order['company']['contragent'])
|
|
|
|
|
&& array_key_exists($key, $order['company']['contragent'])
|
|
|
|
|
) {
|
|
|
|
|
$somePropValue = $propertyCollection
|
|
|
|
|
->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
|
|
|
|
|
self::setProp($somePropValue, RCrmActions::fromJSON($order['company']['contragent'][$key]));
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-24 11:41:11 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($propsRemove) {
|
2016-10-03 16:56:59 +03:00
|
|
|
|
$order = $orderDump;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
//comments
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (array_key_exists('customerComment', $order)) {
|
2016-10-12 11:40:18 +03:00
|
|
|
|
self::setProp($newOrder, RCrmActions::fromJSON($order['customerComment']), 'USER_DESCRIPTION');
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (array_key_exists('managerComment', $order)) {
|
2016-10-12 11:40:18 +03:00
|
|
|
|
self::setProp($newOrder, RCrmActions::fromJSON($order['managerComment']), 'COMMENTS');
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2016-10-03 16:56:59 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
//items
|
|
|
|
|
$basket = $newOrder->getBasket();
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
if (!$basket) {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$basket = Basket::create($site);
|
2017-12-04 17:44:30 +03:00
|
|
|
|
$newOrder->setBasket($basket);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 15:29:43 +03:00
|
|
|
|
$fUserId = $basket->getFUserId(true);
|
|
|
|
|
|
2018-07-16 16:01:19 +03:00
|
|
|
|
if (!$fUserId) {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$fUserId = Fuser::getIdByUserId($order['customer']['externalId']);
|
2018-02-27 15:29:43 +03:00
|
|
|
|
$basket->setFUserId($fUserId);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
if (isset($order['contact']['id'])) {
|
|
|
|
|
$ExtId = null;
|
|
|
|
|
|
|
|
|
|
if (isset($order['contact']['externalId'])) {
|
|
|
|
|
$ExtId = $order['contact']['externalId'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($ExtId)) {
|
|
|
|
|
$newOrder->setFieldNoDemand('USER_ID', $ExtId);
|
|
|
|
|
} else {
|
2022-09-07 10:35:40 +03:00
|
|
|
|
$response = RCrmActions::apiMethod(
|
|
|
|
|
$api,
|
|
|
|
|
'customersGetById',
|
|
|
|
|
__METHOD__,
|
|
|
|
|
$order['contact']['id'],
|
|
|
|
|
$order['site']
|
|
|
|
|
);
|
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$newUser = new CUser();
|
|
|
|
|
$customerBuilder = new CustomerBuilder();
|
|
|
|
|
$customerBuilder->setDataCrm($response['customer'])->build();
|
|
|
|
|
|
|
|
|
|
if (!empty($response['customer']['email'])) {
|
|
|
|
|
$registerNewUser = true;
|
|
|
|
|
$dbUser = CUser::GetList(
|
|
|
|
|
($by = 'ID'),
|
2023-03-31 11:44:26 +03:00
|
|
|
|
($sort = 'DESC'),
|
|
|
|
|
['=EMAIL' => $response['customer']['email']]
|
2020-07-14 13:47:26 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
switch ($dbUser->SelectedRowsCount()) {
|
|
|
|
|
case 0:
|
|
|
|
|
$login = $response['customer']['email'];
|
|
|
|
|
$customerBuilder->setLogin($login);
|
2022-09-07 10:35:40 +03:00
|
|
|
|
$customerBuilder->buildPassword();
|
2020-07-14 13:47:26 +03:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
$arUser = $dbUser->Fetch();
|
|
|
|
|
$registeredUserID = $arUser['ID'];
|
|
|
|
|
$registerNewUser = false;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-03-31 11:44:26 +03:00
|
|
|
|
$lastBitrixUser = $dbUser->Fetch();
|
|
|
|
|
$registeredUserID = $lastBitrixUser['ID'];
|
|
|
|
|
$registerNewUser = false;
|
2020-07-14 13:47:26 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($registerNewUser === true) {
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$customFields = self::getCustomUserFields($response['customer'], $matchedCustomUserFields, $customUserFieldTypes);
|
|
|
|
|
$registeredUserID = $newUser->Add(self::getDataUser($customerBuilder, $customFields));
|
2023-08-22 13:09:20 +03:00
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
if ($registeredUserID === false) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'CUser::Register',
|
|
|
|
|
'Error register user: ' . $newUser->LAST_ERROR
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(RCrmActions::apiMethod(
|
|
|
|
|
$api,
|
|
|
|
|
'customersFixExternalIds',
|
|
|
|
|
__METHOD__,
|
2023-12-21 17:24:46 +03:00
|
|
|
|
[[
|
2020-07-14 13:47:26 +03:00
|
|
|
|
'id' => $response['customer']['id'],
|
|
|
|
|
'externalId' => $registeredUserID
|
2023-12-21 17:24:46 +03:00
|
|
|
|
]]
|
2020-07-14 13:47:26 +03:00
|
|
|
|
) == false
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$newOrder->setFieldNoDemand('USER_ID', $registeredUserID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 14:11:52 +03:00
|
|
|
|
$editBasketInfo = [];
|
|
|
|
|
$deleteBasketInfo = [];
|
|
|
|
|
$bonusesChargeTotal = null;
|
2023-01-16 11:05:29 +03:00
|
|
|
|
$loyaltyDiscount = null;
|
2022-07-21 14:11:52 +03:00
|
|
|
|
|
2016-10-06 17:34:35 +03:00
|
|
|
|
if (isset($order['items'])) {
|
|
|
|
|
$itemUpdate = true;
|
2019-09-17 15:42:26 +03:00
|
|
|
|
$response = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
|
2020-02-19 14:35:56 +03:00
|
|
|
|
|
2019-09-17 15:42:26 +03:00
|
|
|
|
if (isset($response['order'])) {
|
|
|
|
|
$orderTemp = $response['order'];
|
2020-02-18 10:54:42 +03:00
|
|
|
|
$duplicateItems = [];
|
2020-02-19 14:35:56 +03:00
|
|
|
|
|
2019-09-17 15:42:26 +03:00
|
|
|
|
foreach ($orderTemp['items'] as $item) {
|
2020-02-18 10:54:42 +03:00
|
|
|
|
$duplicateItems[$item['id']]['externalId'] += $item['offer']['externalId'];
|
|
|
|
|
$duplicateItems[$item['id']]['quantity'] += $item['quantity'];
|
2020-02-19 14:35:56 +03:00
|
|
|
|
$duplicateItems[$item['id']]['discountTotal'] +=
|
|
|
|
|
$item['quantity'] * $item['discountTotal'];
|
|
|
|
|
$duplicateItems[$item['id']]['initialPrice'] = (float) $item['initialPrice'];
|
|
|
|
|
$duplicateItems[$item['id']]['price_sum'] = ($item['quantity'] * $item['initialPrice'])
|
|
|
|
|
- ($item['quantity'] * $item['discountTotal']);
|
2019-09-17 15:42:26 +03:00
|
|
|
|
}
|
2020-02-19 14:35:56 +03:00
|
|
|
|
|
2019-09-17 15:42:26 +03:00
|
|
|
|
unset($orderTemp);
|
2020-02-19 14:35:56 +03:00
|
|
|
|
} else {
|
|
|
|
|
continue;
|
2019-09-17 15:42:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 10:54:42 +03:00
|
|
|
|
$collectItems = [];
|
2020-02-19 14:35:56 +03:00
|
|
|
|
|
2020-02-18 10:54:42 +03:00
|
|
|
|
foreach ($duplicateItems as $it) {
|
|
|
|
|
$collectItems[$it['externalId']]['quantity'] += $it['quantity'];
|
|
|
|
|
$collectItems[$it['externalId']]['price_sum'] += $it['price_sum'];
|
|
|
|
|
$collectItems[$it['externalId']]['discountTotal_sum'] += $it['discountTotal'];
|
2020-02-19 14:35:56 +03:00
|
|
|
|
|
|
|
|
|
if (isset($collectItems[$it['externalId']]['initialPrice_max'])) {
|
|
|
|
|
if ($collectItems[$it['externalId']]['initialPrice_max'] < $it['initialPrice']) {
|
|
|
|
|
$collectItems[$it['externalId']]['initialPrice_max'] = $it['initialPrice'];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$collectItems[$it['externalId']]['initialPrice_max'] = $it['initialPrice'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$collectItems[$it['externalId']]['initialPricesList'][] = $it['initialPrice'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($collectItems as $key => $itemData) {
|
|
|
|
|
if (count($itemData['initialPricesList']) > 1) {
|
|
|
|
|
$discountDelta = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($itemData['initialPrices'] as $initialPriceItem) {
|
|
|
|
|
$delta = $itemData['initialPrice_max'] - (float) $initialPriceItem;
|
|
|
|
|
|
|
|
|
|
if ($delta !== 0) {
|
|
|
|
|
$discountDelta += $delta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$collectItems[$key]['discountTotal_sum'] += $discountDelta;
|
|
|
|
|
}
|
2020-02-18 10:54:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
Logger::getInstance()->write($duplicateItems, 'duplicateItemsOrderHistory');
|
|
|
|
|
Logger::getInstance()->write($collectItems, 'collectItemsOrderHistory');
|
|
|
|
|
|
2020-02-18 10:54:42 +03:00
|
|
|
|
$optionDiscRound = COption::GetOptionString(self::$MODULE_ID, self::$CRM_DISCOUNT_ROUND, 0);
|
2019-09-17 15:42:26 +03:00
|
|
|
|
|
2016-10-06 17:34:35 +03:00
|
|
|
|
foreach ($order['items'] as $product) {
|
2020-02-18 10:54:42 +03:00
|
|
|
|
if ($collectItems[$product['offer']['externalId']]['quantity']) {
|
|
|
|
|
$product['quantity'] = $collectItems[$product['offer']['externalId']]['quantity'];
|
2019-09-17 15:42:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 17:34:35 +03:00
|
|
|
|
$item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']);
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2016-10-06 17:34:35 +03:00
|
|
|
|
if (!$item) {
|
2018-02-27 15:29:43 +03:00
|
|
|
|
if ($product['delete']) {
|
2016-10-06 17:34:35 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2016-10-06 17:34:35 +03:00
|
|
|
|
$item = $basket->createItem('catalog', $product['offer']['externalId']);
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2016-10-20 13:16:59 +03:00
|
|
|
|
if ($item instanceof \Bitrix\Sale\BasketItem) {
|
2016-10-12 11:40:18 +03:00
|
|
|
|
$elem = self::getInfoElement($product['offer']['externalId']);
|
2023-07-03 10:05:52 +03:00
|
|
|
|
$vatRate = null;
|
2020-02-19 14:35:56 +03:00
|
|
|
|
|
2023-07-03 10:05:52 +03:00
|
|
|
|
if (RetailcrmConfigProvider::getOrderVat() === 'Y') {
|
2023-09-01 14:06:23 +03:00
|
|
|
|
if ($product['vatRate'] === 0 || $product['vatRate'] === 'none') {
|
2023-07-03 10:05:52 +03:00
|
|
|
|
$vatRate = 0;
|
|
|
|
|
} elseif($product['vatRate'] !== null) {
|
|
|
|
|
$vatRate = $product['vatRate'] / 100;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$item->setFields([
|
2017-12-04 17:44:30 +03:00
|
|
|
|
'CURRENCY' => $newOrder->getCurrency(),
|
2016-10-20 17:41:07 +03:00
|
|
|
|
'LID' => $site,
|
2020-02-19 14:35:56 +03:00
|
|
|
|
'BASE_PRICE' => $collectItems[$product['offer']['externalId']]['initialPrice_max'],
|
2017-11-13 11:05:11 +03:00
|
|
|
|
'NAME' => $product['offer']['name'] ? RCrmActions::fromJSON($product['offer']['name']) : $elem['NAME'],
|
2017-09-04 11:36:04 +03:00
|
|
|
|
'DETAIL_PAGE_URL' => $elem['URL'],
|
|
|
|
|
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
|
|
|
|
|
'DIMENSIONS' => $elem['DIMENSIONS'],
|
|
|
|
|
'WEIGHT' => $elem['WEIGHT'],
|
2017-09-22 15:28:19 +03:00
|
|
|
|
'NOTES' => GetMessage('PRICE_TYPE'),
|
|
|
|
|
'PRODUCT_XML_ID' => $elem["XML_ID"],
|
2023-07-03 10:05:52 +03:00
|
|
|
|
'CATALOG_XML_ID' => $elem["IBLOCK_XML_ID"],
|
|
|
|
|
'VAT_INCLUDED' => $vatRate !== null ? 'Y' : 'N',
|
|
|
|
|
'VAT_RATE' => $vatRate,
|
|
|
|
|
]);
|
2016-10-06 17:34:35 +03:00
|
|
|
|
} else {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'createItem',
|
|
|
|
|
'Error item add'
|
|
|
|
|
);
|
2018-02-27 15:29:43 +03:00
|
|
|
|
|
2016-10-06 17:34:35 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2022-06-28 15:28:28 +03:00
|
|
|
|
if ($product['delete'] && $collectItems[$product['offer']['externalId']]['quantity'] <= 0) {
|
2022-09-09 14:41:23 +03:00
|
|
|
|
if (is_int($item->getBasketCode())) {
|
2022-06-28 15:28:28 +03:00
|
|
|
|
$deleteBasketInfo[] = $item->getBasketCode();
|
2022-09-09 14:41:23 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2022-09-09 14:41:23 +03:00
|
|
|
|
$item->delete();
|
|
|
|
|
|
|
|
|
|
continue;
|
2016-10-03 16:56:59 +03:00
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2016-10-06 17:34:35 +03:00
|
|
|
|
if ($product['quantity']) {
|
|
|
|
|
$item->setFieldNoDemand('QUANTITY', $product['quantity']);
|
2020-02-19 14:35:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('initialPrice_max', $collectItems[$product['offer']['externalId']])) {
|
|
|
|
|
$item->setField(
|
|
|
|
|
'BASE_PRICE',
|
|
|
|
|
$collectItems[$product['offer']['externalId']]['initialPrice_max']
|
|
|
|
|
);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 10:54:42 +03:00
|
|
|
|
if (array_key_exists('discountTotal_sum', $collectItems[$product['offer']['externalId']])) {
|
|
|
|
|
$item->setField('CUSTOM_PRICE', 'Y');
|
|
|
|
|
$item->setField('DISCOUNT_NAME', '');
|
|
|
|
|
$item->setField('DISCOUNT_VALUE', '');
|
2019-10-03 11:29:46 +03:00
|
|
|
|
|
2020-02-19 14:35:56 +03:00
|
|
|
|
// Полную цену позиции с учётом скидок делим на количество - получаем цену каждой единицы
|
|
|
|
|
// товара с учётом скидок.
|
|
|
|
|
$price = $collectItems[
|
|
|
|
|
$product['offer']['externalId']
|
|
|
|
|
]['price_sum'] / $collectItems[$product['offer']['externalId']]['quantity'];
|
2018-07-16 16:01:19 +03:00
|
|
|
|
|
2020-02-18 10:54:42 +03:00
|
|
|
|
if ('Y' == $optionDiscRound) {
|
|
|
|
|
$price = self::truncate($price, 2);
|
2016-10-06 17:34:35 +03:00
|
|
|
|
}
|
2020-02-18 10:54:42 +03:00
|
|
|
|
|
|
|
|
|
$item->setField('PRICE', $price);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
|
|
|
|
|
$manualProductDiscount = 0;
|
2023-01-16 11:05:29 +03:00
|
|
|
|
|
2022-06-28 15:28:28 +03:00
|
|
|
|
$bonusesChargeProduct = $product['bonusesCharge'] ?? null;
|
|
|
|
|
|
|
|
|
|
foreach ($product['discounts'] as $productDiscount) {
|
|
|
|
|
if ('manual_product' === $productDiscount['type']) {
|
|
|
|
|
$manualProductDiscount = $productDiscount['amount'];
|
|
|
|
|
}
|
2023-01-16 11:05:29 +03:00
|
|
|
|
|
|
|
|
|
if ('loyalty_level' === $productDiscount['type']) {
|
|
|
|
|
$loyaltyDiscount += $productDiscount['amount'];
|
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$editBasketInfo[] = [
|
|
|
|
|
'productId' => $item->getProductId(),
|
|
|
|
|
'productDiscount' => $manualProductDiscount / $item->getQuantity(),
|
|
|
|
|
'bonusesCharge' => $bonusesChargeProduct,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (null !== $bonusesChargeProduct) {
|
|
|
|
|
$bonusesChargeTotal += $bonusesChargeProduct;
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orderSumm = 0;
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($basket as $item) {
|
2018-09-21 12:22:49 +03:00
|
|
|
|
$orderSumm += $item->getFinalPrice();
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
|
if (is_array($order['delivery']) && array_key_exists('cost', $order['delivery'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$deliverySumm = $order['delivery']['cost'];
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$deliverySumm = $newOrder->getDeliveryPrice();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$orderSumm += $deliverySumm;
|
|
|
|
|
|
|
|
|
|
$order['summ'] = $orderSumm;
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
//payment
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$newHistoryPayments = [];
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if (array_key_exists('payments', $order)) {
|
|
|
|
|
if (!isset($orderCrm)) {
|
|
|
|
|
$orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
|
|
|
|
|
}
|
|
|
|
|
if ($orderCrm) {
|
2018-03-22 16:11:04 +03:00
|
|
|
|
self::paymentsUpdate($newOrder, $orderCrm['order'], $newHistoryPayments);
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
//delivery
|
2017-12-04 17:44:30 +03:00
|
|
|
|
if (array_key_exists('delivery', $order)) {
|
2016-10-06 17:34:35 +03:00
|
|
|
|
$itemUpdate = true;
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
//delete empty
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (!isset($orderCrm)) {
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
|
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if ($orderCrm) {
|
2018-01-12 11:14:33 +03:00
|
|
|
|
self::deliveryUpdate($newOrder, $optionsDelivTypes, $orderCrm['order']);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
if ($itemUpdate === true && $newOrder->getField('CANCELED') !== 'Y') {
|
2018-01-12 11:14:33 +03:00
|
|
|
|
self::shipmentItemReset($newOrder);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($orderCrm)) {
|
2018-09-21 12:22:49 +03:00
|
|
|
|
unset($orderCrm);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
|
2020-07-23 16:28:36 +03:00
|
|
|
|
if (isset($order['fullPaidAt']) && is_string($order['fullPaidAt'])) {
|
2020-08-28 13:04:26 +03:00
|
|
|
|
$newOrder->setFieldNoDemand('PAYED', 'Y');
|
2020-07-23 16:28:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($shipmentDeducted === 'Y') {
|
|
|
|
|
$collection = $newOrder->getShipmentCollection()->getNotSystemItems();
|
|
|
|
|
|
|
|
|
|
if (isset($order['shipped'])) {
|
|
|
|
|
if ($order['shipped']) {
|
|
|
|
|
if ($collection->count() === 0) {
|
|
|
|
|
$collection = $newOrder->getShipmentCollection();
|
|
|
|
|
$shipment = $collection->createItem();
|
|
|
|
|
$shipment->setField('DEDUCTED', 'Y');
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($collection as $shipment) {
|
|
|
|
|
$shipment->setField('DEDUCTED', 'Y');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($collection as $shipment) {
|
|
|
|
|
$shipment->setField('DEDUCTED', 'N');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-12 11:14:33 +03:00
|
|
|
|
$newOrder->setField('PRICE', $orderSumm);
|
2023-12-19 17:13:31 +03:00
|
|
|
|
|
|
|
|
|
if (self::$CUSTOM_FIELDS_IS_ACTIVE === 'Y' && !empty($matchedCustomOrderFields)) {
|
|
|
|
|
foreach ($order['customFields'] as $code => $value) {
|
|
|
|
|
if (isset($matchedCustomOrderFields[$code])) {
|
|
|
|
|
$arrayIdentifier = explode('#', $matchedCustomOrderFields[$code], 2);
|
|
|
|
|
$property = $propertyCollection->getItemByOrderPropertyId($arrayIdentifier[0]);
|
|
|
|
|
$value = RCrmActions::convertCrmValueToCmsField($value, $property->getType());
|
|
|
|
|
$queryResult = $property->setField('VALUE', $value);
|
|
|
|
|
|
|
|
|
|
if (!$queryResult->isSuccess()) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'CustomOrderPropSave',
|
|
|
|
|
'Error when saving a property: ' . $arrayIdentifier[1]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-21 12:22:49 +03:00
|
|
|
|
self::orderSave($newOrder);
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
if ($optionsOrderNumbers === 'Y' && isset($order['number'])) {
|
2018-01-23 17:24:32 +03:00
|
|
|
|
$newOrder->setField('ACCOUNT_NUMBER', $order['number']);
|
2018-09-21 12:22:49 +03:00
|
|
|
|
self::orderSave($newOrder);
|
2018-01-23 17:24:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 15:28:28 +03:00
|
|
|
|
//items loyalty info to HL
|
|
|
|
|
if (!empty($editBasketInfo)) {
|
|
|
|
|
$newBasket = $newOrder->getBasket();
|
|
|
|
|
$calculateItemsInput = [];
|
|
|
|
|
$basketItemIds = [];
|
|
|
|
|
|
|
|
|
|
foreach ($editBasketInfo as $itemInfo) {
|
|
|
|
|
$basketItem = self::getExistsItem($newBasket, 'catalog', $itemInfo['productId']);
|
|
|
|
|
if (false !== $basketItem) {
|
|
|
|
|
$calculateItemsInput[$basketItem->getId()]['SHOP_ITEM_DISCOUNT'] = $itemInfo['productDiscount'];
|
|
|
|
|
$calculateItemsInput[$basketItem->getId()]['BONUSES_CHARGE'] = $itemInfo['bonusesCharge'];
|
|
|
|
|
$basketItemIds[] = $basketItem->getId();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 11:05:29 +03:00
|
|
|
|
$orderLoyaltyDataService->saveBonusAndDiscToOrderProps(
|
|
|
|
|
$newOrder->getPropertyCollection(),
|
|
|
|
|
$loyaltyDiscount,
|
|
|
|
|
$bonusesChargeTotal
|
|
|
|
|
);
|
|
|
|
|
|
2022-06-28 15:28:28 +03:00
|
|
|
|
$hlInfoBuilder = new LoyaltyDataBuilder();
|
|
|
|
|
$hlInfoBuilder->setOrder($newOrder);
|
|
|
|
|
$hlInfoBuilder->setCalculateItemsInput($calculateItemsInput);
|
|
|
|
|
$hlInfoBuilder->setBonusCountTotal($bonusesChargeTotal);
|
|
|
|
|
$orderLoyaltyDataService->saveLoyaltyInfoToHl($hlInfoBuilder->build($basketItemIds)->getResult());
|
2023-01-16 11:05:29 +03:00
|
|
|
|
self::orderSave($newOrder);
|
2022-06-28 15:28:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($deleteBasketInfo)) {
|
|
|
|
|
$orderLoyaltyDataService->deleteLoyaltyInfoFromHl($deleteBasketInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 17:24:32 +03:00
|
|
|
|
if (!empty($newHistoryPayments)) {
|
2018-01-12 11:14:33 +03:00
|
|
|
|
foreach ($newOrder->getPaymentCollection() as $orderPayment) {
|
2023-03-15 11:55:04 +03:00
|
|
|
|
if (is_array($newHistoryPayments)
|
|
|
|
|
&& array_key_exists($orderPayment->getField('XML_ID'), $newHistoryPayments)
|
|
|
|
|
) {
|
2019-05-31 15:51:40 +03:00
|
|
|
|
$paymentId = $orderPayment->getId();
|
|
|
|
|
$paymentExternalId = RCrmActions::generatePaymentExternalId($paymentId);
|
2019-09-10 13:29:23 +03:00
|
|
|
|
if (is_null($paymentId)) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'paymentsUpdate',
|
|
|
|
|
'Save payment error, order=' . $order['number']
|
|
|
|
|
);
|
|
|
|
|
|
2019-09-10 13:29:23 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-09-17 15:42:26 +03:00
|
|
|
|
|
2021-01-13 17:51:36 +03:00
|
|
|
|
$newHistoryPayments[$orderPayment->getField('XML_ID')]['externalId'] = $paymentExternalId;
|
|
|
|
|
RCrmActions::apiMethod(
|
|
|
|
|
$api,
|
|
|
|
|
'paymentEditById',
|
|
|
|
|
__METHOD__,
|
|
|
|
|
$newHistoryPayments[$orderPayment->getField('XML_ID')]
|
|
|
|
|
);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2021-01-13 17:51:36 +03:00
|
|
|
|
if ($paymentId) {
|
2023-12-21 17:24:46 +03:00
|
|
|
|
PaymentTable::update($paymentId, ['XML_ID' => '']);
|
2018-09-21 12:22:49 +03:00
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
if (!$order['externalId']) {
|
2019-11-01 13:33:07 +03:00
|
|
|
|
$order["externalId"] = $newOrder->getId();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
|
|
|
|
if (RCrmActions::apiMethod(
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$api,
|
|
|
|
|
'ordersFixExternalIds',
|
|
|
|
|
__METHOD__,
|
2023-12-21 17:24:46 +03:00
|
|
|
|
[['id' => $order['id'], 'externalId' => $newOrder->getId()]]) == false
|
2020-04-24 13:18:18 +03:00
|
|
|
|
) {
|
2017-12-04 17:44:30 +03:00
|
|
|
|
continue;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2016-10-06 17:34:35 +03:00
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if (function_exists('retailCrmAfterOrderSave')) {
|
|
|
|
|
retailCrmAfterOrderSave($order);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-16 16:01:19 +03:00
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
unset($newOrder);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
$page++;
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2023-04-26 17:18:38 +03:00
|
|
|
|
if ($page > self::PAGE_LIMIT) {
|
|
|
|
|
break;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2023-04-26 17:18:38 +03:00
|
|
|
|
} while ($historyResponse['pagination']['currentPage'] < $historyResponse['pagination']['totalPageCount']);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
|
|
|
|
return false;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
/**
|
|
|
|
|
* @param string $shopCode
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public static function getSite(string $shopCode): ?string
|
|
|
|
|
{
|
|
|
|
|
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
if ($optionsSitesList) {
|
|
|
|
|
$searchResult = array_search($shopCode, $optionsSitesList, true);
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2023-03-31 11:29:58 +03:00
|
|
|
|
return !empty($searchResult) ? (string) $searchResult : null;
|
2021-09-08 10:19:29 +03:00
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$defaultSite = CSite::GetDefSite();
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
return is_string($defaultSite) ? $defaultSite : null;
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2019-09-17 15:42:26 +03:00
|
|
|
|
/**
|
|
|
|
|
* @param $array
|
|
|
|
|
* @param $value
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function search_array_by_value($array, $value)
|
|
|
|
|
{
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$results = [];
|
|
|
|
|
|
2019-09-17 15:42:26 +03:00
|
|
|
|
if (is_array($array)) {
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$found = array_search($value, $array);
|
|
|
|
|
|
2019-09-17 15:42:26 +03:00
|
|
|
|
if ($found) {
|
|
|
|
|
$results[] = $found;
|
|
|
|
|
}
|
2023-12-21 17:24:46 +03:00
|
|
|
|
|
2019-09-17 15:42:26 +03:00
|
|
|
|
foreach ($array as $subarray)
|
|
|
|
|
$results = array_merge($results, static::search_array_by_value($subarray, $value));
|
|
|
|
|
}
|
|
|
|
|
return $results;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
public static function assemblyCustomer($customerHistory)
|
|
|
|
|
{
|
2020-08-25 11:14:47 +03:00
|
|
|
|
$customerHistory = self::filterHistory($customerHistory, 'customer');
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$server = Context::getCurrent()->getServer()->getDocumentRoot();
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$fields = [];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) {
|
2018-09-21 12:22:49 +03:00
|
|
|
|
$objects = simplexml_load_file($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml');
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($objects->fields->field as $object) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$fields[(string)$object["group"]][(string)$object["id"]] = (string)$object;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$customers = [];
|
2016-09-15 16:42:10 +03:00
|
|
|
|
foreach ($customerHistory as $change) {
|
|
|
|
|
$change['customer'] = self::removeEmpty($change['customer']);
|
2018-09-21 12:22:49 +03:00
|
|
|
|
if ($customers[$change['customer']['id']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']] = array_merge($customers[$change['customer']['id']], $change['customer']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']] = $change['customer'];
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($change['customer']['contragent']['contragentType']) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$change['customer']['contragentType'] = self::newValue($change['customer']['contragent']['contragentType']);
|
|
|
|
|
unset($change['customer']['contragent']);
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if ($change['field'] == 'segments') {
|
|
|
|
|
if ($change['newValue']['code'] == "genshchini") {
|
|
|
|
|
$customers[$change['customer']['id']]["sex"] = "F";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($change['field'] == 'segments') {
|
|
|
|
|
if ($change['newValue']['code'] == "mugchini") {
|
|
|
|
|
$customers[$change['customer']['id']]["sex"] = "M";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($fields['customer'][$change['field']] == 'phones') {
|
2023-03-15 11:55:04 +03:00
|
|
|
|
if (is_array($customers[$change['customer']['id']]['phones'])) {
|
|
|
|
|
$key = count($customers[$change['customer']['id']]['phones']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($change['oldValue'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']]['phones'][$key]['old_number'] = $change['oldValue'];
|
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($change['newValue'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']]['phones'][$key]['number'] = $change['newValue'];
|
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
|
|
|
|
if ($fields['customerAddress'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']]['address'][$fields['customerAddress'][$change['field']]] = $change['newValue'];
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif ($fields['customerContragent'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']]['contragent'][$fields['customerContragent'][$change['field']]] = $change['newValue'];
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif ($fields['customer'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']][$fields['customer'][$change['field']]] = self::newValue($change['newValue']);
|
2019-11-01 13:49:02 +03:00
|
|
|
|
} elseif (strripos($change['field'], 'custom_') !== false) {
|
|
|
|
|
$customers[$change['customer']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($change['created'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']]['create'] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($change['deleted'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$customers[$change['customer']['id']]['deleted'] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return $customers;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
public static function assemblyOrder($orderHistory)
|
|
|
|
|
{
|
2020-08-25 11:14:47 +03:00
|
|
|
|
$orderHistory = self::filterHistory($orderHistory, 'order');
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$server = Context::getCurrent()->getServer()->getDocumentRoot();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$objects = simplexml_load_file(
|
|
|
|
|
$server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml'
|
|
|
|
|
);
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($objects->fields->field as $object) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$fields[(string)$object["group"]][(string)$object["id"]] = (string)$object;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$orders = [];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
foreach ($orderHistory as $change) {
|
|
|
|
|
$change['order'] = self::removeEmpty($change['order']);
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($change['order']['items']) {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$items = [];
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
foreach ($change['order']['items'] as $item) {
|
|
|
|
|
if (isset($change['created'])) {
|
2018-09-21 12:22:49 +03:00
|
|
|
|
$item['create'] = 1;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
$items[$item['id']] = $item;
|
|
|
|
|
}
|
|
|
|
|
$change['order']['items'] = $items;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if ($change['field'] == 'number') {
|
|
|
|
|
$orders[$change['order']['id']]['number'] = $change['newValue'];
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
if ($change['field'] == 'manager') {
|
|
|
|
|
$orders[$change['order']['id']]['managerId'] = $change['newValue'];
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
if (isset($change['oldValue']) && $change['field'] == 'customer') {
|
|
|
|
|
$orders[$change['order']['id']]['customer'] = $change['newValue'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($change['oldValue']) && $change['field'] == 'contact') {
|
|
|
|
|
$orders[$change['order']['id']]['contact'] = $change['newValue'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($change['oldValue']) && $change['field'] == 'company') {
|
|
|
|
|
$orders[$change['order']['id']]['company'] = $change['newValue'];
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if ($change['order']['payments']) {
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$payments = [];
|
2017-09-04 11:36:04 +03:00
|
|
|
|
foreach ($change['order']['payments'] as $payment) {
|
|
|
|
|
$payments[$payment['id']] = $payment;
|
|
|
|
|
}
|
|
|
|
|
$change['order']['payments'] = $payments;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
|
if (is_array($change['order']['contragent']) && isset($change['order']['contragent']) && count($change['order']['contragent']) > 0) {
|
2017-09-22 15:28:19 +03:00
|
|
|
|
foreach ($change['order']['contragent'] as $name => $value) {
|
|
|
|
|
$change['order'][$name] = self::newValue($value);
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
unset($change['order']['contragent']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-21 12:22:49 +03:00
|
|
|
|
if ($orders[$change['order']['id']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']] = $change['order'];
|
2018-09-21 12:22:49 +03:00
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if ($change['item']) {
|
2018-09-21 12:22:49 +03:00
|
|
|
|
if ($orders[$change['order']['id']]['items'][$change['item']['id']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item'];
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (empty($change['oldValue']) && $change['field'] == 'order_product') {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = 1;
|
|
|
|
|
unset($orders[$change['order']['id']]['items'][$change['item']['id']]['delete']);
|
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (empty($change['newValue']) && $change['field'] == 'order_product') {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = 1;
|
|
|
|
|
}
|
2017-09-22 15:28:19 +03:00
|
|
|
|
if (/*!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && */$fields['item'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue'];
|
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
} elseif ($change['payment']) {
|
2018-09-21 12:22:49 +03:00
|
|
|
|
if ($orders[$change['order']['id']]['payments'][$change['payment']['id']]) {
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$orders[$change['order']['id']]['payments'][$change['payment']['id']] = array_merge($orders[$change['order']['id']]['payments'][$change['payment']['id']], $change['payment']);
|
|
|
|
|
} else {
|
|
|
|
|
$orders[$change['order']['id']]['payments'][$change['payment']['id']] = $change['payment'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($change['oldValue']) && $change['field'] == 'payments') {
|
|
|
|
|
$orders[$change['order']['id']]['payments'][$change['payment']['id']]['create'] = 1;
|
|
|
|
|
unset($orders[$change['order']['id']]['payments'][$change['payment']['id']]['delete']);
|
|
|
|
|
}
|
|
|
|
|
if (empty($change['newValue']) && $change['field'] == 'payments') {
|
|
|
|
|
$orders[$change['order']['id']]['payments'][$change['payment']['id']]['delete'] = 1;
|
|
|
|
|
}
|
|
|
|
|
if (!$orders[$change['order']['id']]['payments'][$change['payment']['id']]['create'] && $fields['payment'][$change['field']]) {
|
|
|
|
|
$orders[$change['order']['id']]['payments'][$change['payment']['id']][$fields['payment'][$change['field']]] = $change['newValue'];
|
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
|
|
|
|
if ($fields['delivery'][$change['field']] == 'service') {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['delivery']['service']['code'] = self::newValue($change['newValue']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif ($fields['delivery'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['delivery'][$fields['delivery'][$change['field']]] = self::newValue($change['newValue']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif ($fields['orderAddress'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['delivery']['address'][$fields['orderAddress'][$change['field']]] = $change['newValue'];
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif ($fields['integrationDelivery'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['delivery']['service'][$fields['integrationDelivery'][$change['field']]] = self::newValue($change['newValue']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif ($fields['customerContragent'][$change['field']]) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']][$fields['customerContragent'][$change['field']]] = self::newValue($change['newValue']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif (strripos($change['field'], 'custom_') !== false) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']);
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} elseif ($fields['order'][$change['field']]){
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($change['created'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['create'] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($change['deleted'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$orders[$change['order']['id']]['deleted'] = 1;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
}
|
2020-07-14 13:47:26 +03:00
|
|
|
|
|
|
|
|
|
if ($change['field'] == 'last_name') {
|
|
|
|
|
if (true == is_null($change['newValue'])) {
|
|
|
|
|
$orders[$change['order']['id']]['lastName'] = 'clear';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($change['field'] == 'first_Name') {
|
|
|
|
|
if (true == is_null($change['newValue'])) {
|
|
|
|
|
$orders[$change['order']['id']]['firstName'] = 'clear';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($change['field'] == 'patronymic') {
|
|
|
|
|
if (true == is_null($change['newValue'])) {
|
|
|
|
|
$orders[$change['order']['id']]['patronymic'] = 'clear';
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return $orders;
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
/**
|
|
|
|
|
* Filters out history by these terms:
|
|
|
|
|
* - Changes from current API key will be added only if CMS changes are more actual than history.
|
|
|
|
|
* - All other changes will be merged as usual.
|
|
|
|
|
* It fixes these problems:
|
|
|
|
|
* - Changes from current API key are merged when it's not needed.
|
|
|
|
|
* - Changes from CRM can overwrite more actual changes from CMS due to ignoring current API key changes.
|
|
|
|
|
*
|
|
|
|
|
* @param array $historyEntries Raw history from CRM
|
|
|
|
|
* @param string $recordType Entity field name, e.g. `customer` or `order`.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private static function filterHistory($historyEntries, $recordType)
|
|
|
|
|
{
|
|
|
|
|
$history = [];
|
|
|
|
|
$organizedHistory = [];
|
|
|
|
|
$notOurChanges = [];
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
foreach ($historyEntries as $entry) {
|
|
|
|
|
if (!isset($entry[$recordType]['externalId'])) {
|
|
|
|
|
if ($entry['source'] == 'api'
|
|
|
|
|
&& isset($change['apiKey']['current'])
|
|
|
|
|
&& $entry['apiKey']['current'] == true
|
|
|
|
|
&& $entry['field'] != 'externalId'
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
$history[] = $entry;
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
$externalId = $entry[$recordType]['externalId'];
|
|
|
|
|
$field = $entry['field'];
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
if (!isset($organizedHistory[$externalId])) {
|
|
|
|
|
$organizedHistory[$externalId] = [];
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
if (!isset($notOurChanges[$externalId])) {
|
|
|
|
|
$notOurChanges[$externalId] = [];
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
if ($entry['source'] == 'api'
|
|
|
|
|
&& isset($entry['apiKey']['current'])
|
|
|
|
|
&& $entry['apiKey']['current'] == true
|
|
|
|
|
) {
|
|
|
|
|
if (isset($notOurChanges[$externalId][$field]) || $entry['field'] == 'externalId') {
|
|
|
|
|
$organizedHistory[$externalId][] = $entry;
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$organizedHistory[$externalId][] = $entry;
|
|
|
|
|
$notOurChanges[$externalId][$field] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
unset($notOurChanges);
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
foreach ($organizedHistory as $historyChunk) {
|
|
|
|
|
$history = array_merge($history, $historyChunk);
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2020-08-25 11:14:47 +03:00
|
|
|
|
return $history;
|
|
|
|
|
}
|
2022-02-04 15:16:14 +03:00
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
/**
|
|
|
|
|
* Update shipment in order
|
|
|
|
|
*
|
2022-03-02 15:40:53 +03:00
|
|
|
|
* @param \Bitrix\Sale\Order $bitrixOrder
|
|
|
|
|
* @param array $optionsDelivTypes
|
|
|
|
|
* @param array $crmOrder
|
2018-09-21 12:22:49 +03:00
|
|
|
|
*
|
2022-03-02 15:40:53 +03:00
|
|
|
|
* @return bool|null
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentException
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentNullException
|
|
|
|
|
* @throws \Bitrix\Main\ObjectNotFoundException
|
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
2017-12-04 17:44:30 +03:00
|
|
|
|
*/
|
2022-03-02 15:40:53 +03:00
|
|
|
|
public static function deliveryUpdate(Bitrix\Sale\Order $bitrixOrder, $optionsDelivTypes, $crmOrder)
|
2016-10-04 17:57:39 +03:00
|
|
|
|
{
|
2022-03-02 15:40:53 +03:00
|
|
|
|
if (!$bitrixOrder instanceof Bitrix\Sale\Order) {
|
2017-09-04 11:36:04 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
|
if ($bitrixOrder->getId()) {
|
2017-12-04 17:44:30 +03:00
|
|
|
|
$update = true;
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2017-12-04 17:44:30 +03:00
|
|
|
|
$update = false;
|
2016-10-04 17:57:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
|
$crmCode = $crmOrder['delivery']['code'] ?? false;
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$noDeliveryId = EmptyDeliveryService::getEmptyDeliveryServiceId();
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
|
|
|
|
if ($crmCode === false || !isset($optionsDelivTypes[$crmCode])) {
|
|
|
|
|
$deliveryId = $noDeliveryId;
|
|
|
|
|
} else {
|
|
|
|
|
$deliveryId = $optionsDelivTypes[$crmCode];
|
2016-10-04 17:57:39 +03:00
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
|
if (isset($crmOrder['delivery']['service']['code'])) {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$deliveryCode = Manager::getCodeById($deliveryId);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
$serviceCode = $crmOrder['delivery']['service']['code'];
|
2019-09-17 18:10:30 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$service = Manager::getService($deliveryId);
|
2019-10-08 16:23:18 +03:00
|
|
|
|
if (is_object($service)) {
|
|
|
|
|
$services = $service->getProfilesList();
|
|
|
|
|
if (!array_key_exists($serviceCode, $services)) {
|
|
|
|
|
$serviceCode = strtoupper($serviceCode);
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$serviceCode = str_replace('-', "_", $serviceCode);
|
2019-10-08 16:23:18 +03:00
|
|
|
|
}
|
2019-04-18 09:52:35 +03:00
|
|
|
|
}
|
2019-09-17 15:42:26 +03:00
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
if ($deliveryCode) {
|
|
|
|
|
try {
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$deliveryService = Manager::getObjectByCode($deliveryCode . ':' . $serviceCode);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
} catch (SystemException $systemException) {
|
2017-12-04 17:44:30 +03:00
|
|
|
|
RCrmActions::eventLog('RetailCrmHistory::deliveryEdit', '\Bitrix\Sale\Delivery\Services\Manager::getObjectByCode', $systemException->getMessage());
|
2016-10-04 17:57:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
if (isset($deliveryService)) {
|
|
|
|
|
$deliveryId = $deliveryService->getId();
|
2016-10-04 17:57:39 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
$delivery = Manager::getObjectById($deliveryId);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
$shipmentColl = $bitrixOrder->getShipmentCollection();
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
|
|
|
|
if ($delivery) {
|
2023-10-26 14:36:22 +03:00
|
|
|
|
$baseFields = [
|
|
|
|
|
'BASE_PRICE_DELIVERY' => $crmOrder['delivery']['cost'],
|
|
|
|
|
'PRICE_DELIVERY' => $crmOrder['delivery']['cost'],
|
|
|
|
|
'CURRENCY' => $bitrixOrder->getCurrency(),
|
|
|
|
|
'DELIVERY_NAME' => $delivery->getName(),
|
|
|
|
|
'CUSTOM_PRICE_DELIVERY' => 'Y',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (RetailcrmConfigProvider::getTrackNumberStatus() === 'Y') {
|
|
|
|
|
$baseFields['TRACKING_NUMBER'] = $crmOrder['delivery']['data']['trackNumber'] ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 15:16:14 +03:00
|
|
|
|
//В коллекции всегда есть одна скрытая системная доставка, к которой относятся нераспределенные товары
|
|
|
|
|
//Поэтому, если есть только системная доставка, то нужно создать новую
|
|
|
|
|
if (!$update || $shipmentColl->count() === 1) {
|
2019-09-17 15:42:26 +03:00
|
|
|
|
$shipment = $shipmentColl->createItem($delivery);
|
2023-10-26 14:36:22 +03:00
|
|
|
|
|
|
|
|
|
$shipment->setFields($baseFields);
|
2017-12-04 17:44:30 +03:00
|
|
|
|
} else {
|
2023-10-26 14:36:22 +03:00
|
|
|
|
$baseFields['DELIVERY_ID'] = $deliveryId;
|
|
|
|
|
|
2017-12-04 17:44:30 +03:00
|
|
|
|
foreach ($shipmentColl as $shipment) {
|
|
|
|
|
if (!$shipment->isSystem()) {
|
2023-10-26 14:36:22 +03:00
|
|
|
|
$shipment->setFields($baseFields);
|
2017-12-04 17:44:30 +03:00
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
return true;
|
2018-01-12 11:14:33 +03:00
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2018-01-12 11:14:33 +03:00
|
|
|
|
/**
|
|
|
|
|
* Update shipment item colletion
|
2018-09-21 12:22:49 +03:00
|
|
|
|
*
|
2018-02-27 15:29:43 +03:00
|
|
|
|
* @param \Bitrix\Sale\Order $order
|
2018-09-21 12:22:49 +03:00
|
|
|
|
*
|
2018-01-12 11:14:33 +03:00
|
|
|
|
* @return void | boolean
|
|
|
|
|
*/
|
|
|
|
|
public static function shipmentItemReset($order)
|
|
|
|
|
{
|
|
|
|
|
$shipmentCollection = $order->getShipmentCollection();
|
|
|
|
|
$basket = $order->getBasket();
|
|
|
|
|
|
|
|
|
|
foreach ($shipmentCollection as $shipment) {
|
2023-01-23 14:27:15 +03:00
|
|
|
|
if (!$shipment->isSystem() && !$shipment->isShipped()) {
|
2018-02-27 15:29:43 +03:00
|
|
|
|
$reserved = false;
|
|
|
|
|
|
2018-03-22 16:11:04 +03:00
|
|
|
|
if ($shipment->needReservation()) {
|
2018-02-27 15:29:43 +03:00
|
|
|
|
$reserved = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-12 11:14:33 +03:00
|
|
|
|
$shipmentItemColl = $shipment->getShipmentItemCollection();
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2018-02-27 15:29:43 +03:00
|
|
|
|
if ($reserved === true) {
|
|
|
|
|
$shipment->tryUnreserve();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-12 11:14:33 +03:00
|
|
|
|
try {
|
2018-02-27 15:29:43 +03:00
|
|
|
|
$shipmentItemColl->resetCollection($basket);
|
|
|
|
|
|
|
|
|
|
if ($reserved === true) {
|
|
|
|
|
$shipment->tryReserve();
|
|
|
|
|
}
|
2018-01-12 11:14:33 +03:00
|
|
|
|
} catch (\Bitrix\Main\NotSupportedException $NotSupportedException) {
|
|
|
|
|
RCrmActions::eventLog('RetailCrmHistory::shipmentItemReset', '\Bitrix\Sale\ShipmentItemCollection::resetCollection()', $NotSupportedException->getMessage());
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2018-02-27 15:29:43 +03:00
|
|
|
|
/**
|
|
|
|
|
* Unreserve items if order canceled
|
2018-09-21 12:22:49 +03:00
|
|
|
|
*
|
2018-02-27 15:29:43 +03:00
|
|
|
|
* @param \Bitrix\Sale\Order $order
|
2018-09-21 12:22:49 +03:00
|
|
|
|
*
|
2018-02-27 15:29:43 +03:00
|
|
|
|
* @return void | boolean
|
|
|
|
|
*/
|
|
|
|
|
public static function unreserveShipment($order)
|
|
|
|
|
{
|
|
|
|
|
$shipmentCollection = $order->getShipmentCollection();
|
|
|
|
|
|
|
|
|
|
foreach ($shipmentCollection as $shipment) {
|
|
|
|
|
if (!$shipment->isSystem()) {
|
|
|
|
|
try {
|
|
|
|
|
$shipment->tryUnreserve();
|
2022-03-02 15:40:53 +03:00
|
|
|
|
} catch (ArgumentOutOfRangeException $ArgumentOutOfRangeException) {
|
2018-02-27 15:29:43 +03:00
|
|
|
|
RCrmActions::eventLog('RetailCrmHistory::unreserveShipment', '\Bitrix\Sale\Shipment::tryUnreserve()', $ArgumentOutOfRangeException->getMessage());
|
|
|
|
|
|
|
|
|
|
return false;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
} catch (NotSupportedException $NotSupportedException) {
|
2018-02-27 15:29:43 +03:00
|
|
|
|
RCrmActions::eventLog('RetailCrmHistory::unreserveShipment', '\Bitrix\Sale\Shipment::tryUnreserve()', $NotSupportedException->getMessage());
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-12 11:14:33 +03:00
|
|
|
|
/**
|
|
|
|
|
* Update payment in order
|
2018-09-21 12:22:49 +03:00
|
|
|
|
*
|
2018-01-12 11:14:33 +03:00
|
|
|
|
* @param object $order
|
|
|
|
|
* @param array $paymentsCrm
|
|
|
|
|
* @param array $newHistoryPayments
|
2018-09-21 12:22:49 +03:00
|
|
|
|
*
|
2018-01-12 11:14:33 +03:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-12-21 17:24:46 +03:00
|
|
|
|
public static function paymentsUpdate($order, $paymentsCrm, &$newHistoryPayments = [])
|
2016-10-04 17:57:39 +03:00
|
|
|
|
{
|
2024-01-24 15:01:13 +03:00
|
|
|
|
$optionsPayTypes = self::$optionsPayment['payTypes'];
|
|
|
|
|
$optionsPayment = self::$optionsPayment['paymentList'];
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$allPaymentSystems = RCrmActions::PaymentList();
|
2024-01-24 15:01:13 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
foreach ($allPaymentSystems as $allPaymentSystem) {
|
|
|
|
|
$arPaySysmems[$allPaymentSystem['ID']] = $allPaymentSystem['NAME'];
|
|
|
|
|
}
|
2024-01-24 15:01:13 +03:00
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$paymentsList = [];
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$paymentColl = $order->getPaymentCollection();
|
2024-01-24 15:01:13 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
foreach ($paymentColl as $paymentData) {
|
|
|
|
|
$data = $paymentData->getFields()->getValues();
|
|
|
|
|
$paymentsList[$data['ID']] = $paymentData;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
//data from crm
|
|
|
|
|
$paySumm = 0;
|
2024-01-24 15:01:13 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
foreach ($paymentsCrm['payments'] as $paymentCrm) {
|
2017-11-13 11:05:11 +03:00
|
|
|
|
if (isset($paymentCrm['externalId']) && !empty($paymentCrm['externalId'])) {
|
2017-09-04 11:36:04 +03:00
|
|
|
|
//find the payment
|
2019-09-17 18:10:30 +03:00
|
|
|
|
$nowPaymentId = RCrmActions::getFromPaymentExternalId($paymentCrm['externalId']);
|
|
|
|
|
$nowPayment = $paymentsList[$nowPaymentId];
|
2017-09-04 11:36:04 +03:00
|
|
|
|
//update data
|
2022-03-02 15:40:53 +03:00
|
|
|
|
if ($nowPayment instanceof Payment) {
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$nowPayment->setField('SUM', $paymentCrm['amount']);
|
|
|
|
|
if ($optionsPayTypes[$paymentCrm['type']] != $nowPayment->getField('PAY_SYSTEM_ID')) {
|
|
|
|
|
$nowPayment->setField('PAY_SYSTEM_ID', $optionsPayTypes[$paymentCrm['type']]);
|
|
|
|
|
$nowPayment->setField('PAY_SYSTEM_NAME', $arPaySysmems[$optionsPayTypes[$paymentCrm['type']]]);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if (isset($optionsPayment[$paymentCrm['status']])) {
|
|
|
|
|
$nowPayment->setField('PAID', $optionsPayment[$paymentCrm['status']]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 18:10:30 +03:00
|
|
|
|
unset($paymentsList[$nowPaymentId]);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2019-09-10 13:23:51 +03:00
|
|
|
|
} elseif (array_key_exists($paymentCrm['type'], $optionsPayTypes)) {
|
2018-01-12 11:14:33 +03:00
|
|
|
|
$newHistoryPayments[$paymentCrm['id']] = $paymentCrm;
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$newPayment = $paymentColl->createItem();
|
|
|
|
|
$newPayment->setField('SUM', $paymentCrm['amount']);
|
|
|
|
|
$newPayment->setField('PAY_SYSTEM_ID', $optionsPayTypes[$paymentCrm['type']]);
|
|
|
|
|
$newPayment->setField('PAY_SYSTEM_NAME', $arPaySysmems[$optionsPayTypes[$paymentCrm['type']]]);
|
|
|
|
|
$newPayment->setField('PAID', $optionsPayment[$paymentCrm['status']] ? $optionsPayment[$paymentCrm['status']] : 'N');
|
2017-12-04 17:44:30 +03:00
|
|
|
|
$newPayment->setField('CURRENCY', $order->getCurrency());
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$newPayment->setField('IS_RETURN', 'N');
|
|
|
|
|
$newPayment->setField('PRICE_COD', '0.00');
|
|
|
|
|
$newPayment->setField('EXTERNAL_PAYMENT', 'N');
|
|
|
|
|
$newPayment->setField('UPDATED_1C', 'N');
|
2018-01-12 11:14:33 +03:00
|
|
|
|
$newPayment->setField('XML_ID', $paymentCrm['id']);
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
|
|
|
|
$newPaymentId = $newPayment->getId();
|
|
|
|
|
|
|
|
|
|
unset($paymentsList[$newPaymentId]);
|
2019-09-10 13:29:23 +03:00
|
|
|
|
} else {
|
|
|
|
|
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'paymentsUpdate', 'Save payment error, incorrect type: ' . $paymentCrm['type']);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2017-11-13 11:05:11 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if ($optionsPayment[$paymentCrm['status']] == 'Y') {
|
|
|
|
|
$paySumm += $paymentCrm['amount'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ($paymentsList as $payment) {
|
|
|
|
|
if ($payment->isPaid()) {
|
|
|
|
|
$payment->setPaid("N");
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$payment->delete();
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if ($paymentsCrm['totalSumm'] == $paySumm) {
|
|
|
|
|
$order->setFieldNoDemand('PAYED', 'Y');
|
|
|
|
|
} else {
|
|
|
|
|
$order->setFieldNoDemand('PAYED', 'N');
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
public static function newValue($value)
|
|
|
|
|
{
|
2023-03-15 11:55:04 +03:00
|
|
|
|
if (is_array($value) && array_key_exists('code', $value)) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return $value['code'];
|
2016-10-04 17:57:39 +03:00
|
|
|
|
} else {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 17:44:30 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
public static function removeEmpty($inputArray)
|
|
|
|
|
{
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$outputArray = [];
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (!empty($inputArray)) {
|
|
|
|
|
foreach ($inputArray as $key => $element) {
|
|
|
|
|
if (!empty($element) || $element === 0 || $element === '0') {
|
|
|
|
|
if (is_array($element)) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$element = self::removeEmpty($element);
|
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$outputArray[$key] = $element;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $outputArray;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
/**
|
|
|
|
|
* setProp
|
|
|
|
|
*
|
|
|
|
|
* @param \Bitrix\Sale\PropertyValueBase|\Bitrix\Sale\Order $obj
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param string $prop
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentNullException
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentOutOfRangeException
|
|
|
|
|
* @throws \Bitrix\Main\NotImplementedException
|
|
|
|
|
* @throws \Bitrix\Main\ObjectNotFoundException
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentException
|
|
|
|
|
*/
|
2017-11-13 11:05:11 +03:00
|
|
|
|
public static function setProp($obj, $value = '', $prop = '')
|
2016-10-04 17:57:39 +03:00
|
|
|
|
{
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!isset($obj) || empty($obj)) {
|
2016-10-03 16:56:59 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-04-13 17:07:58 +03:00
|
|
|
|
if ($prop && $value || $prop && !$value) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$obj->setField($prop, $value);
|
2017-11-13 11:05:11 +03:00
|
|
|
|
} elseif ($value && !$prop) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$obj->setValue($value);
|
2017-11-13 11:05:11 +03:00
|
|
|
|
} elseif (!$value && !$prop) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
$obj->delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
public static function getExistsItem($basket, $moduleId, $productId)
|
|
|
|
|
{
|
2017-09-04 11:36:04 +03:00
|
|
|
|
foreach ($basket as $basketItem) {
|
|
|
|
|
$itemExists = ($basketItem->getField('PRODUCT_ID') == $productId && $basketItem->getField('MODULE') == $moduleId);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
if ($itemExists) {
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return $basketItem;
|
2017-09-04 11:36:04 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2023-03-28 18:12:03 +03:00
|
|
|
|
public static function setManager($newOrder, $order)
|
|
|
|
|
{
|
|
|
|
|
if (array_key_exists('managerId', $order)) {
|
|
|
|
|
$service = ManagerService::getInstance();
|
|
|
|
|
|
|
|
|
|
$newOrder->setField('RESPONSIBLE_ID', $service->getManagerBitrixId($order['managerId']));
|
|
|
|
|
|
|
|
|
|
// If the order exists in CRM, need to save the changes in the CMS order
|
|
|
|
|
if (!empty($order['externalId'])) {
|
|
|
|
|
self::orderSave($newOrder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
public static function getInfoElement($offerId)
|
|
|
|
|
{
|
2016-10-03 16:56:59 +03:00
|
|
|
|
$elementInfo = CIBlockElement::GetByID($offerId)->fetch();
|
|
|
|
|
$url = CAllIBlock::ReplaceDetailUrl($elementInfo['DETAIL_PAGE_URL'], $elementInfo, false, 'E');
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$catalog = CCatalogProduct::GetByID($offerId);
|
|
|
|
|
|
2023-12-21 17:24:46 +03:00
|
|
|
|
$info = [
|
2016-10-03 16:56:59 +03:00
|
|
|
|
'NAME' => $elementInfo['NAME'],
|
|
|
|
|
'URL' => $url,
|
2023-12-21 17:24:46 +03:00
|
|
|
|
'DIMENSIONS' => serialize(
|
|
|
|
|
[
|
|
|
|
|
'WIDTH' => $catalog['WIDTH'],
|
|
|
|
|
'HEIGHT' => $catalog['HEIGHT'],
|
|
|
|
|
'LENGTH' => $catalog['LENGTH'],
|
|
|
|
|
]
|
|
|
|
|
),
|
2017-09-22 15:28:19 +03:00
|
|
|
|
'WEIGHT' => $catalog['WEIGHT'],
|
|
|
|
|
'XML_ID' => $elementInfo["XML_ID"],
|
|
|
|
|
'IBLOCK_XML_ID' => $elementInfo["IBLOCK_EXTERNAL_ID"]
|
2023-12-21 17:24:46 +03:00
|
|
|
|
];
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
2016-10-03 16:56:59 +03:00
|
|
|
|
return $info;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2018-09-21 12:22:49 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $order
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
private static function orderSave($order)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Order saving',
|
|
|
|
|
$exception->getMessage()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-18 10:54:42 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Truncate a float number
|
|
|
|
|
*
|
|
|
|
|
* @param float $val
|
|
|
|
|
* @param int f Number of precision
|
|
|
|
|
*
|
|
|
|
|
* @return float
|
|
|
|
|
*/
|
|
|
|
|
public static function truncate($val, $precision = "0")
|
|
|
|
|
{
|
|
|
|
|
if(($p = strpos($val, '.')) !== false
|
|
|
|
|
|| ($p = strpos($val, ',')) !== false
|
|
|
|
|
) {
|
|
|
|
|
$val = floatval(substr($val, 0, $p + 1 + $precision));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $val;
|
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
/**
|
2022-09-30 13:05:58 +03:00
|
|
|
|
* @param mixed $externalId
|
2021-08-19 15:46:03 +03:00
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2022-09-30 13:05:58 +03:00
|
|
|
|
public static function cancelOrder($externalId)
|
2016-09-15 16:42:10 +03:00
|
|
|
|
{
|
2021-08-19 15:46:03 +03:00
|
|
|
|
try {
|
|
|
|
|
$newOrder = Order::load($externalId);
|
|
|
|
|
} catch (ArgumentNullException $e) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Bitrix\Sale\Order::load',
|
|
|
|
|
$e->getMessage() . ': ' . $externalId
|
|
|
|
|
);
|
2017-09-04 11:36:04 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$newOrder instanceof Order) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Bitrix\Sale\Order::load',
|
|
|
|
|
'Error order load: ' . $externalId
|
2017-09-04 11:36:04 +03:00
|
|
|
|
);
|
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$newOrder->setField('CANCELED', 'Y');
|
|
|
|
|
$newOrder->save();
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmHistory::orderHistory',
|
|
|
|
|
'Bitrix\Sale\Order::cancelOrder',
|
|
|
|
|
'Error order canceled: ' . $externalId
|
|
|
|
|
);
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-22 13:09:20 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $array
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function convertBooleanFields($array)
|
|
|
|
|
{
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
|
if ($value === 'N') {
|
|
|
|
|
$array[$key] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $customerBuilder
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-12-19 17:13:31 +03:00
|
|
|
|
private static function getDataUser($customerBuilder, $customFields)
|
2023-08-22 13:09:20 +03:00
|
|
|
|
{
|
|
|
|
|
$customerArray = $customerBuilder->getCustomer()->getObjectToArray();
|
|
|
|
|
|
|
|
|
|
if (!array_key_exists('UF_SUBSCRIBE_USER_EMAIL', $customerArray)) {
|
|
|
|
|
$customerArray['UF_SUBSCRIBE_USER_EMAIL'] = 'Y';
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
|
$customerArray = array_merge($customerArray, $customFields);
|
|
|
|
|
|
2023-08-22 13:09:20 +03:00
|
|
|
|
return self::convertBooleanFields($customerArray);
|
|
|
|
|
}
|
2023-12-19 17:13:31 +03:00
|
|
|
|
|
|
|
|
|
private static function getCustomUserFields($customer, $matchedCustomFields, $customUserFieldTypes)
|
|
|
|
|
{
|
|
|
|
|
$customFields = [];
|
|
|
|
|
|
|
|
|
|
if (self::$CUSTOM_FIELDS_IS_ACTIVE === 'Y'
|
|
|
|
|
&& isset($customer['customFields'])
|
|
|
|
|
&& is_array($customer['customFields'])
|
|
|
|
|
) {
|
|
|
|
|
foreach ($customer['customFields'] as $code => $value) {
|
|
|
|
|
if (isset($matchedCustomFields[$code]) && !empty($customUserFieldTypes[$matchedCustomFields[$code]])) {
|
|
|
|
|
$type = $customUserFieldTypes[$matchedCustomFields[$code]];
|
|
|
|
|
$customFields[$matchedCustomFields[$code]] = RCrmActions::convertCrmValueToCmsField($value, $type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $customFields;
|
|
|
|
|
}
|
2018-01-23 10:20:53 +03:00
|
|
|
|
}
|