2016-09-15 16:42:10 +03:00
|
|
|
|
<?php
|
2020-04-24 13:18:18 +03:00
|
|
|
|
|
2021-09-08 13:00:50 +03:00
|
|
|
|
use Bitrix\Main\Context\Culture;
|
2024-01-24 15:01:13 +03:00
|
|
|
|
use Intaro\RetailCrm\Component\Constants;
|
2023-08-22 13:09:20 +03:00
|
|
|
|
use Intaro\RetailCrm\Component\ServiceLocator;
|
|
|
|
|
use Intaro\RetailCrm\Repository\UserRepository;
|
|
|
|
|
use Intaro\RetailCrm\Service\CustomerService;
|
|
|
|
|
use Intaro\RetailCrm\Service\LoyaltyAccountService;
|
2021-08-19 15:46:03 +03:00
|
|
|
|
use Intaro\RetailCrm\Service\ManagerService;
|
2021-09-08 10:19:29 +03:00
|
|
|
|
use Bitrix\Sale\Payment;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
use Bitrix\Catalog\Model\Event;
|
|
|
|
|
use Bitrix\Main\UserTable;
|
|
|
|
|
use Bitrix\Sale\Order;
|
|
|
|
|
use Intaro\RetailCrm\Component\ConfigProvider;
|
|
|
|
|
use Intaro\RetailCrm\Model\Api\Response\OrdersCreateResponse;
|
|
|
|
|
use Intaro\RetailCrm\Model\Api\Response\OrdersEditResponse;
|
2021-08-19 15:46:03 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
/**
|
2019-02-28 14:45:39 +03:00
|
|
|
|
* Class RetailCrmEvent
|
2016-09-15 16:42:10 +03:00
|
|
|
|
*/
|
2016-10-04 17:57:39 +03:00
|
|
|
|
class RetailCrmEvent
|
2018-12-26 11:41:06 +03:00
|
|
|
|
{
|
2022-03-02 15:40:53 +03:00
|
|
|
|
protected static $MODULE_ID = 'intaro.retailcrm';
|
|
|
|
|
protected static $CRM_API_HOST_OPTION = 'api_host';
|
2016-09-15 16:42:10 +03:00
|
|
|
|
protected static $CRM_API_KEY_OPTION = 'api_key';
|
2022-03-02 15:40:53 +03:00
|
|
|
|
protected static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
|
|
|
|
|
protected static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
|
|
|
|
|
protected static $CRM_PAYMENT_TYPES = 'pay_types_arr';
|
|
|
|
|
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
|
|
|
|
|
protected static $CRM_PAYMENT = 'payment_arr';
|
|
|
|
|
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
|
|
|
|
|
protected static $CRM_ORDER_PROPS = 'order_props';
|
|
|
|
|
protected static $CRM_LEGAL_DETAILS = 'legal_details';
|
|
|
|
|
protected static $CRM_CUSTOM_FIELDS = 'custom_fields';
|
|
|
|
|
protected static $CRM_CONTRAGENT_TYPE = 'contragent_type';
|
|
|
|
|
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
|
|
|
|
|
protected static $CRM_SITES_LIST = 'sites_list';
|
|
|
|
|
protected static $CRM_CC = 'cc';
|
|
|
|
|
protected static $CRM_CORP_NAME = 'nickName-corporate';
|
|
|
|
|
protected static $CRM_CORP_ADRES = 'adres-corporate';
|
2018-12-26 11:41:06 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
/**
|
2019-02-28 14:45:39 +03:00
|
|
|
|
* @param $arFields
|
2018-12-26 11:41:06 +03:00
|
|
|
|
*
|
2019-02-28 14:45:39 +03:00
|
|
|
|
* @return bool
|
|
|
|
|
* @throws InvalidArgumentException
|
2016-09-15 16:42:10 +03:00
|
|
|
|
*/
|
2023-03-15 11:55:04 +03:00
|
|
|
|
public static function OnAfterUserUpdate($arFields)
|
2019-02-28 14:45:39 +03:00
|
|
|
|
{
|
2023-08-22 13:09:20 +03:00
|
|
|
|
RetailCrmService::writeLogsSubscribe($arFields);
|
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
|
2019-02-28 14:45:39 +03:00
|
|
|
|
return false;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (!$arFields['RESULT']) {
|
2019-02-28 14:45:39 +03:00
|
|
|
|
return false;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
|
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
2016-10-03 16:56:59 +03:00
|
|
|
|
$resultOrder = RetailCrmUser::customerEdit($arFields, $api, $optionsSitesList);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
|
if (!$resultOrder) {
|
2016-10-03 16:56:59 +03:00
|
|
|
|
RCrmActions::eventLog('RetailCrmEvent::OnAfterUserUpdate', 'RetailCrmUser::customerEdit', 'error update customer');
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* onUpdateOrder
|
2019-02-28 14:45:39 +03:00
|
|
|
|
*
|
2020-04-24 13:18:18 +03:00
|
|
|
|
* @param mixed $ID - Order id
|
2016-09-15 16:42:10 +03:00
|
|
|
|
* @param mixed $arFields - Order arFields
|
|
|
|
|
*/
|
2023-03-15 11:55:04 +03:00
|
|
|
|
public static function onUpdateOrder($ID, $arFields)
|
2016-10-04 17:57:39 +03:00
|
|
|
|
{
|
|
|
|
|
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
|
2019-02-28 14:45:39 +03:00
|
|
|
|
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return;
|
2019-02-28 14:45:39 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = true;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
if (($arFields['CANCELED'] === 'Y')
|
2020-04-24 13:18:18 +03:00
|
|
|
|
&& (sizeof($arFields['BASKET_ITEMS']) == 0)
|
|
|
|
|
&& (sizeof($arFields['ORDER_PROP']) == 0)
|
2019-07-01 14:38:11 +03:00
|
|
|
|
) {
|
|
|
|
|
$GLOBALS['ORDER_DELETE_USER_ADMIN'] = true;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* orderDelete
|
2019-02-28 14:45:39 +03:00
|
|
|
|
*
|
2016-09-15 16:42:10 +03:00
|
|
|
|
* @param object $event - Order object
|
|
|
|
|
*/
|
2023-03-15 11:55:04 +03:00
|
|
|
|
public static function orderDelete($event)
|
2016-10-04 17:57:39 +03:00
|
|
|
|
{
|
2017-09-04 11:36:04 +03:00
|
|
|
|
$GLOBALS['RETAILCRM_ORDER_DELETE'] = true;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2023-04-11 11:57:06 +03:00
|
|
|
|
/**
|
|
|
|
|
* событие изменения корзины
|
|
|
|
|
*
|
|
|
|
|
* @param object $event
|
|
|
|
|
*/
|
2023-08-29 17:40:10 +03:00
|
|
|
|
public static function onChangeBasket($event)
|
2023-04-11 11:57:06 +03:00
|
|
|
|
{
|
2023-08-29 17:40:10 +03:00
|
|
|
|
$apiVersion = COption::GetOptionString(self::$MODULE_ID, 'api_version', 0);
|
|
|
|
|
|
|
|
|
|
if ($apiVersion !== 'v5') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 11:57:06 +03:00
|
|
|
|
$id = \Bitrix\Main\Engine\CurrentUser::get()->getId();
|
|
|
|
|
|
|
|
|
|
if ($id) {
|
|
|
|
|
$arBasket = RetailCrmCart::getBasketArray($event);
|
|
|
|
|
|
|
|
|
|
if ($arBasket === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$arBasket['USER_ID'] = $id;
|
|
|
|
|
|
|
|
|
|
RetailCrmCart::handlerCart($arBasket);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
|
/**
|
2019-02-28 14:45:39 +03:00
|
|
|
|
* @param $event
|
2018-12-26 11:41:06 +03:00
|
|
|
|
*
|
2022-03-02 15:40:53 +03:00
|
|
|
|
* @return array|bool|OrdersCreateResponse|OrdersEditResponse|null
|
|
|
|
|
* @throws \Bitrix\Main\ArgumentException
|
2020-02-19 14:35:56 +03:00
|
|
|
|
* @throws \Bitrix\Main\ObjectPropertyException
|
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
2022-03-02 15:40:53 +03:00
|
|
|
|
* @throws \Exception
|
2016-09-15 16:42:10 +03:00
|
|
|
|
*/
|
2022-03-02 15:40:53 +03:00
|
|
|
|
public static function orderSave($event)
|
2016-10-04 17:57:39 +03:00
|
|
|
|
{
|
2022-03-02 15:40:53 +03:00
|
|
|
|
if (!static::checkConfig()) {
|
|
|
|
|
return null;
|
2019-02-28 14:45:39 +03:00
|
|
|
|
}
|
2016-09-15 16:42:10 +03:00
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
|
$arOrder = static::getOrderArray($event);
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
|
//params
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$optionsOrderTypes = RetailcrmConfigProvider::getOrderTypes();
|
|
|
|
|
$optionsDelivTypes = RetailcrmConfigProvider::getDeliveryTypes();
|
|
|
|
|
$optionsPayTypes = RetailcrmConfigProvider::getPaymentTypes();
|
|
|
|
|
$optionsPayStatuses = RetailcrmConfigProvider::getPaymentStatuses(); // --statuses
|
|
|
|
|
$optionsPayment = RetailcrmConfigProvider::getPayment();
|
|
|
|
|
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
|
|
|
|
|
$optionsOrderProps = RetailcrmConfigProvider::getOrderProps();
|
|
|
|
|
$optionsLegalDetails = RetailcrmConfigProvider::getLegalDetails();
|
|
|
|
|
$optionsContragentType = RetailcrmConfigProvider::getContragentTypes();
|
|
|
|
|
$optionsCustomFields = RetailcrmConfigProvider::getCustomFields();
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
//corp cliente swich
|
|
|
|
|
$optionCorpClient = RetailcrmConfigProvider::getCorporateClientStatus();
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$arParams = RCrmActions::clearArr([
|
|
|
|
|
'optionsOrderTypes' => $optionsOrderTypes,
|
|
|
|
|
'optionsDelivTypes' => $optionsDelivTypes,
|
|
|
|
|
'optionsPayTypes' => $optionsPayTypes,
|
|
|
|
|
'optionsPayStatuses' => $optionsPayStatuses,
|
|
|
|
|
'optionsPayment' => $optionsPayment,
|
|
|
|
|
'optionsOrderProps' => $optionsOrderProps,
|
|
|
|
|
'optionsLegalDetails' => $optionsLegalDetails,
|
2019-02-28 14:45:39 +03:00
|
|
|
|
'optionsContragentType' => $optionsContragentType,
|
2021-09-08 10:19:29 +03:00
|
|
|
|
'optionsSitesList' => $optionsSitesList,
|
|
|
|
|
'optionsCustomFields' => $optionsCustomFields,
|
2023-12-19 17:13:31 +03:00
|
|
|
|
'customOrderProps' => RetailcrmConfigProvider::getMatchedOrderProps(),
|
2021-09-08 10:19:29 +03:00
|
|
|
|
]);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
|
//many sites?
|
|
|
|
|
if ($optionsSitesList) {
|
|
|
|
|
if (array_key_exists($arOrder['LID'], $optionsSitesList) && $optionsSitesList[$arOrder['LID']] !== null) {
|
|
|
|
|
$site = $optionsSitesList[$arOrder['LID']];
|
|
|
|
|
} else {
|
2022-03-02 15:40:53 +03:00
|
|
|
|
return null;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2019-02-28 14:45:39 +03:00
|
|
|
|
} elseif (!$optionsSitesList) {
|
|
|
|
|
$site = null;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2022-04-13 16:54:59 +03:00
|
|
|
|
$api->setSite($site);
|
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
|
//new order?
|
|
|
|
|
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arOrder['ID'], $site);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
|
if (isset($orderCrm['order'])) {
|
|
|
|
|
$methodApi = 'ordersEdit';
|
|
|
|
|
$arParams['crmOrder'] = $orderCrm['order'];
|
|
|
|
|
} else {
|
|
|
|
|
$methodApi = 'ordersCreate';
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$orderCompany = null;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
('Y' === $optionCorpClient)
|
2023-03-02 16:20:57 +03:00
|
|
|
|
&& true === RetailCrmCorporateClient::isCorpTookExternalId((string)$arOrder['USER_ID'], $api, $site)
|
2022-03-02 15:40:53 +03:00
|
|
|
|
) {
|
2023-03-02 16:20:57 +03:00
|
|
|
|
RetailCrmCorporateClient::setPrefixForExternalId((string) $arOrder['USER_ID'], $api, $site);
|
2021-07-08 16:29:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO эта управляющая конструкция по функционалу дублирует RetailCrmOrder::createCustomerForOrder.
|
|
|
|
|
// Необходимо устранить дублирование, вынеся логику в обособленный класс-сервис
|
2021-09-08 10:19:29 +03:00
|
|
|
|
if ('Y' === $optionCorpClient && $optionsContragentType[$arOrder['PERSON_TYPE_ID']] === 'legal-entity') {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
//corparate cliente
|
|
|
|
|
$nickName = '';
|
|
|
|
|
$address = '';
|
|
|
|
|
$corpAddress = '';
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$contragent = [];
|
|
|
|
|
$userCorp = [];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$corpName = RetailcrmConfigProvider::getCorporateClientName();
|
|
|
|
|
$corpAddress = RetailcrmConfigProvider::getCorporateClientAddress();
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
foreach ($arOrder['PROPS']['properties'] as $prop) {
|
2021-09-08 10:19:29 +03:00
|
|
|
|
if ($prop['CODE'] === $corpName) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$nickName = $prop['VALUE'][0];
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
if ($prop['CODE'] === $corpAddress) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$address = $prop['VALUE'][0];
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($optionsLegalDetails)
|
|
|
|
|
&& $search = array_search($prop['CODE'], $optionsLegalDetails[$arOrder['PERSON_TYPE_ID']])
|
|
|
|
|
) {
|
|
|
|
|
$contragent[$search] = $prop['VALUE'][0];//legal order data
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($contragentType)) {
|
|
|
|
|
$contragent['contragentType'] = $contragentType;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$customersCorporate = false;
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$response = $api->customersCorporateList(['companyName' => $nickName]);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if ($response && $response->getStatusCode() == 200) {
|
|
|
|
|
$customersCorporate = $response['customersCorporate'];
|
|
|
|
|
$singleCorp = reset($customersCorporate);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($singleCorp)) {
|
|
|
|
|
$userCorp['customerCorporate'] = $singleCorp;
|
|
|
|
|
$companiesResponse = $api->customersCorporateCompanies(
|
|
|
|
|
$singleCorp['id'],
|
2021-09-08 10:19:29 +03:00
|
|
|
|
[],
|
2020-04-24 13:18:18 +03:00
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
'id',
|
|
|
|
|
$site
|
|
|
|
|
);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if ($companiesResponse && $companiesResponse->isSuccessful()) {
|
|
|
|
|
$orderCompany = array_reduce(
|
|
|
|
|
$companiesResponse['companies'],
|
|
|
|
|
function ($carry, $item) use ($nickName) {
|
2021-09-08 10:19:29 +03:00
|
|
|
|
if (is_array($item) && $item['name'] === $nickName) {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$carry = $item;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
return $carry;
|
|
|
|
|
},
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmEvent::orderSave',
|
|
|
|
|
'ApiClient::customersCorporateList',
|
|
|
|
|
'error during fetching corporate customers'
|
|
|
|
|
);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
return null;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
//user
|
|
|
|
|
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arOrder['USER_ID'], $site);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!isset($userCrm['customer'])) {
|
2023-12-08 12:27:10 +03:00
|
|
|
|
$arUser = CUser::GetByID($arOrder['USER_ID'])->Fetch();
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($address)) {
|
|
|
|
|
$arUser['PERSONAL_STREET'] = $address;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$resultUser = RetailCrmUser::customerSend($arUser, $api, 'individual', true, $site);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!$resultUser) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
__CLASS__ . '::' . __METHOD__,
|
|
|
|
|
'RetailCrmUser::customerSend',
|
|
|
|
|
'error during creating customer'
|
|
|
|
|
);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
return null;
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$userCrm = ['customer' => ['externalId' => $arOrder['USER_ID']]];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!isset($userCorp['customerCorporate'])) {
|
|
|
|
|
$resultUserCorp = RetailCrmCorporateClient::clientSend(
|
|
|
|
|
$arOrder,
|
|
|
|
|
$api,
|
|
|
|
|
$optionsContragentType[$arOrder['PERSON_TYPE_ID']],
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
$site
|
|
|
|
|
);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
Logger::getInstance()->write($resultUserCorp, 'resultUserCorp');
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!$resultUserCorp) {
|
|
|
|
|
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmCorporateClient::clientSend', 'error during creating client');
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
return null;
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$arParams['customerCorporate'] = $resultUserCorp;
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$arParams['orderCompany'] = $resultUserCorp['mainCompany'] ?? null;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$customerCorporateAddress = [];
|
|
|
|
|
$customerCorporateCompany = [];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$addressResult = null;
|
|
|
|
|
$companyResult = null;
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($address)) {
|
|
|
|
|
//TODO address builder add
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$customerCorporateAddress = [
|
|
|
|
|
'name' => $nickName,
|
2020-04-24 13:18:18 +03:00
|
|
|
|
'isMain' => true,
|
2021-09-08 10:19:29 +03:00
|
|
|
|
'text' => $address,
|
|
|
|
|
];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$addressResult = $api->customersCorporateAddressesCreate($resultUserCorp['id'], $customerCorporateAddress, 'id', $site);
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$customerCorporateCompany = [
|
|
|
|
|
'name' => $nickName,
|
|
|
|
|
'isMain' => true,
|
|
|
|
|
'contragent' => $contragent,
|
|
|
|
|
];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($addressResult)) {
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$customerCorporateCompany['address'] = [
|
|
|
|
|
'id' => $addressResult['id'],
|
|
|
|
|
];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$companyResult = $api->customersCorporateCompaniesCreate($resultUserCorp['id'], $customerCorporateCompany, 'id', $site);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$customerCorporateContact = [
|
|
|
|
|
'isMain' => true,
|
|
|
|
|
'customer' => [
|
2020-04-24 13:18:18 +03:00
|
|
|
|
'externalId' => $arOrder['USER_ID'],
|
2021-09-08 10:19:29 +03:00
|
|
|
|
'site' => $site,
|
|
|
|
|
],
|
|
|
|
|
];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($companyResult)) {
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$orderCompany = [
|
|
|
|
|
'id' => $companyResult['id'],
|
|
|
|
|
];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$customerCorporateContact['companies'] = [
|
|
|
|
|
[
|
|
|
|
|
'company' => $orderCompany,
|
|
|
|
|
],
|
|
|
|
|
];
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$api->customersCorporateContactsCreate(
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$resultUserCorp['id'],
|
|
|
|
|
$customerCorporateContact,
|
|
|
|
|
'id',
|
|
|
|
|
$site
|
|
|
|
|
);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
|
$arParams['orderCompany'] = array_merge(
|
|
|
|
|
$customerCorporateCompany,
|
2021-09-08 10:19:29 +03:00
|
|
|
|
['id' => $companyResult['id']]
|
2020-07-14 13:47:26 +03:00
|
|
|
|
);
|
|
|
|
|
} else {
|
2020-04-24 13:18:18 +03:00
|
|
|
|
RetailCrmCorporateClient::addCustomersCorporateAddresses(
|
|
|
|
|
$userCorp['customerCorporate']['id'],
|
|
|
|
|
$nickName,
|
|
|
|
|
$address,
|
|
|
|
|
$api,
|
2022-04-13 16:54:59 +03:00
|
|
|
|
$site
|
2020-04-24 13:18:18 +03:00
|
|
|
|
);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$arParams['customerCorporate'] = $userCorp['customerCorporate'];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!empty($orderCompany)) {
|
|
|
|
|
$arParams['orderCompany'] = $orderCompany;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$arParams['contactExId'] = $userCrm['customer']['externalId'];
|
|
|
|
|
} else {
|
|
|
|
|
//user
|
|
|
|
|
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arOrder['USER_ID'], $site);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!isset($userCrm['customer'])) {
|
2023-12-08 12:27:10 +03:00
|
|
|
|
$arUser = CUser::GetByID($arOrder['USER_ID'])->Fetch();
|
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
|
$resultUser = RetailCrmUser::customerSend(
|
|
|
|
|
$arUser,
|
|
|
|
|
$api,
|
|
|
|
|
$optionsContragentType[$arOrder['PERSON_TYPE_ID']],
|
|
|
|
|
true,
|
|
|
|
|
$site
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
|
if (!$resultUser) {
|
2022-03-02 15:40:53 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmEvent::orderSave',
|
|
|
|
|
'RetailCrmUser::customerSend',
|
|
|
|
|
'error during creating customer'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return null;
|
2023-12-08 12:27:10 +03:00
|
|
|
|
}
|
2020-04-24 13:18:18 +03:00
|
|
|
|
}
|
2019-02-28 14:45:39 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
|
if (isset($arOrder['RESPONSIBLE_ID']) && !empty($arOrder['RESPONSIBLE_ID'])) {
|
|
|
|
|
$managerService = ManagerService::getInstance();
|
|
|
|
|
$arParams['managerId'] = $managerService->getManagerCrmId($arOrder['RESPONSIBLE_ID']);
|
|
|
|
|
}
|
2019-02-28 14:45:39 +03:00
|
|
|
|
//order
|
|
|
|
|
$resultOrder = RetailCrmOrder::orderSend($arOrder, $api, $arParams, true, $site, $methodApi);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
|
if (!$resultOrder) {
|
2022-03-02 15:40:53 +03:00
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmEvent::orderSave',
|
|
|
|
|
'RetailCrmOrder::orderSend',
|
|
|
|
|
'error during creating order'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return null;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
return $resultOrder;
|
2016-09-15 16:42:10 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2017-10-24 11:41:11 +03:00
|
|
|
|
/**
|
2019-02-28 14:45:39 +03:00
|
|
|
|
* @param \Bitrix\Sale\Payment $event
|
|
|
|
|
*
|
2020-04-24 13:18:18 +03:00
|
|
|
|
* @return bool
|
2019-02-28 14:45:39 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*
|
2017-10-24 11:41:11 +03:00
|
|
|
|
*/
|
2023-03-15 11:55:04 +03:00
|
|
|
|
public static function paymentSave(Payment $event)
|
2018-05-23 12:19:59 +03:00
|
|
|
|
{
|
2017-10-25 12:34:33 +03:00
|
|
|
|
$apiVersion = COption::GetOptionString(self::$MODULE_ID, 'api_version', 0);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2018-12-26 11:41:06 +03:00
|
|
|
|
/** @var \Bitrix\Sale\Order $order */
|
|
|
|
|
$order = $event->getCollection()->getOrder();
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2018-12-26 11:41:06 +03:00
|
|
|
|
if ((isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY'])
|
2021-09-08 10:19:29 +03:00
|
|
|
|
|| $apiVersion !== 'v5'
|
2018-12-26 11:41:06 +03:00
|
|
|
|
|| $order->isNew()
|
|
|
|
|
) {
|
2019-02-28 14:45:39 +03:00
|
|
|
|
return false;
|
2017-10-24 11:41:11 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
|
2020-04-24 13:18:18 +03:00
|
|
|
|
$optionsPaymentTypes = RetailcrmConfigProvider::getPaymentTypes();
|
|
|
|
|
$optionsPayStatuses = RetailcrmConfigProvider::getPayment();
|
2021-04-19 15:38:09 +03:00
|
|
|
|
$integrationPaymentTypes = RetailcrmConfigProvider::getIntegrationPaymentTypes();
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$arPayment = [
|
|
|
|
|
'ID' => $event->getId(),
|
|
|
|
|
'ORDER_ID' => $event->getField('ORDER_ID'),
|
|
|
|
|
'PAID' => $event->getField('PAID'),
|
2017-10-24 11:41:11 +03:00
|
|
|
|
'PAY_SYSTEM_ID' => $event->getField('PAY_SYSTEM_ID'),
|
2021-09-08 10:19:29 +03:00
|
|
|
|
'SUM' => $event->getField('SUM'),
|
|
|
|
|
'LID' => $order->getSiteId(),
|
|
|
|
|
'DATE_PAID' => $event->getField('DATE_PAID'),
|
|
|
|
|
];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2017-10-26 16:30:50 +03:00
|
|
|
|
if ($optionsSitesList) {
|
2018-01-12 11:14:33 +03:00
|
|
|
|
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
|
|
|
|
|
$site = $optionsSitesList[$arPayment['LID']];
|
2017-10-26 16:30:50 +03:00
|
|
|
|
} else {
|
2019-02-28 14:45:39 +03:00
|
|
|
|
return false;
|
2017-10-26 16:30:50 +03:00
|
|
|
|
}
|
|
|
|
|
} elseif (!$optionsSitesList) {
|
2017-10-24 11:41:11 +03:00
|
|
|
|
$site = null;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-04-19 15:38:09 +03:00
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
2017-10-24 11:41:11 +03:00
|
|
|
|
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arPayment['ORDER_ID'], $site);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2017-10-24 11:41:11 +03:00
|
|
|
|
if (isset($orderCrm['order'])) {
|
|
|
|
|
$payments = $orderCrm['order']['payments'];
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
|
$paymentsExternalIds = [];
|
|
|
|
|
|
|
|
|
|
if (!empty($payments)) {
|
2018-10-31 16:25:53 +03:00
|
|
|
|
foreach ($payments as $payment) {
|
|
|
|
|
if (isset($payment['externalId'])) {
|
2019-05-31 15:51:40 +03:00
|
|
|
|
if (RCrmActions::isNewExternalId($payment['externalId'])) {
|
|
|
|
|
$paymentsExternalIds[RCrmActions::getFromPaymentExternalId($payment['externalId'])] =
|
|
|
|
|
$payment;
|
|
|
|
|
} else {
|
|
|
|
|
$paymentsExternalIds[$payment['externalId']] = $payment;
|
|
|
|
|
}
|
2017-10-24 11:41:11 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 16:25:53 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2018-10-31 16:25:53 +03:00
|
|
|
|
if (!empty($arPayment['PAY_SYSTEM_ID']) && isset($optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']])) {
|
2024-01-24 15:01:13 +03:00
|
|
|
|
$paymentToCrm = [];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2018-10-31 16:25:53 +03:00
|
|
|
|
if (!empty($arPayment['ID'])) {
|
2019-05-31 15:51:40 +03:00
|
|
|
|
$paymentToCrm['externalId'] = RCrmActions::generatePaymentExternalId($arPayment['ID']);
|
2018-10-31 16:25:53 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2024-01-24 15:01:13 +03:00
|
|
|
|
if (!empty($arPayment['DATE_PAID'])) {
|
2018-10-31 16:25:53 +03:00
|
|
|
|
if (is_object($arPayment['DATE_PAID'])) {
|
2021-09-08 13:00:50 +03:00
|
|
|
|
$culture = new Culture(['FORMAT_DATETIME' => 'YYYY-MM-DD HH:MI:SS']);
|
2018-10-31 16:25:53 +03:00
|
|
|
|
$paymentToCrm['paidAt'] = $arPayment['DATE_PAID']->toString($culture);
|
|
|
|
|
} elseif (is_string($arPayment['DATE_PAID'])) {
|
|
|
|
|
$paymentToCrm['paidAt'] = $arPayment['DATE_PAID'];
|
2017-10-24 11:41:11 +03:00
|
|
|
|
}
|
2018-10-31 16:25:53 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2024-01-24 15:01:13 +03:00
|
|
|
|
if (!empty($optionsPayStatuses[$arPayment['PAID']])) {
|
2018-10-31 16:25:53 +03:00
|
|
|
|
$paymentToCrm['status'] = $optionsPayStatuses[$arPayment['PAID']];
|
2017-11-20 15:47:11 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2018-10-31 16:25:53 +03:00
|
|
|
|
if (!empty($arPayment['ORDER_ID'])) {
|
|
|
|
|
$paymentToCrm['order']['externalId'] = $arPayment['ORDER_ID'];
|
2017-10-24 11:41:11 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2020-09-25 12:17:29 +03:00
|
|
|
|
if (RetailcrmConfigProvider::shouldSendPaymentAmount()) {
|
|
|
|
|
$paymentToCrm['amount'] = $arPayment['SUM'];
|
|
|
|
|
}
|
2024-01-24 15:01:13 +03:00
|
|
|
|
|
|
|
|
|
$paymentToCrm = RetailCrmService::preparePayment($paymentToCrm, $arPayment, $optionsPaymentTypes);
|
2018-10-31 16:25:53 +03:00
|
|
|
|
} else {
|
|
|
|
|
RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'payments', 'OrderID = ' . $arPayment['ID'] . '. Payment not found.');
|
2019-02-28 14:45:39 +03:00
|
|
|
|
return false;
|
2018-10-31 16:25:53 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-05-31 15:51:40 +03:00
|
|
|
|
$arPaymentExtId = RCrmActions::generatePaymentExternalId($arPayment['ID']);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
|
if (!empty($paymentsExternalIds) && array_key_exists($arPaymentExtId, $paymentsExternalIds)) {
|
2019-05-31 15:51:40 +03:00
|
|
|
|
$paymentData = $paymentsExternalIds[$arPaymentExtId];
|
2023-03-15 11:55:04 +03:00
|
|
|
|
} elseif (!empty($paymentsExternalIds) && array_key_exists($arPayment['ID'], $paymentsExternalIds)) {
|
2019-05-31 15:51:40 +03:00
|
|
|
|
$paymentData = $paymentsExternalIds[$arPayment['ID']];
|
|
|
|
|
} else {
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$paymentData = [];
|
2019-05-31 15:51:40 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-05-31 15:51:40 +03:00
|
|
|
|
if (empty($paymentData)) {
|
2018-10-31 16:25:53 +03:00
|
|
|
|
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
|
2019-05-31 15:51:40 +03:00
|
|
|
|
} elseif ($paymentData['type'] == $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
|
2021-04-19 15:38:09 +03:00
|
|
|
|
if (in_array($paymentData['type'], $integrationPaymentTypes)) {
|
|
|
|
|
RCrmActions::eventLog(
|
|
|
|
|
'RetailCrmEvent::paymentSave',
|
|
|
|
|
'payments',
|
|
|
|
|
'OrderID = ' . $arPayment['ID'] . '. Integration payment detected, which is not editable.'
|
|
|
|
|
);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-05-31 15:51:40 +03:00
|
|
|
|
$paymentToCrm['externalId'] = $paymentData['externalId'];
|
2018-10-31 16:25:53 +03:00
|
|
|
|
RCrmActions::apiMethod($api, 'paymentEditByExternalId', __METHOD__, $paymentToCrm, $site);
|
2019-05-31 15:51:40 +03:00
|
|
|
|
} elseif ($paymentData['type'] != $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
|
|
|
|
|
RCrmActions::apiMethod(
|
|
|
|
|
$api,
|
|
|
|
|
'ordersPaymentDelete',
|
|
|
|
|
__METHOD__,
|
|
|
|
|
$paymentData['id']
|
|
|
|
|
);
|
2018-10-31 16:25:53 +03:00
|
|
|
|
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
|
2017-10-24 11:41:11 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
|
return true;
|
2017-10-24 11:41:11 +03:00
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2017-10-24 11:41:11 +03:00
|
|
|
|
/**
|
2019-02-28 14:45:39 +03:00
|
|
|
|
* @param \Bitrix\Sale\Payment $event
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
2017-10-24 11:41:11 +03:00
|
|
|
|
*/
|
2023-03-15 11:55:04 +03:00
|
|
|
|
public static function paymentDelete(Payment $event): void
|
2018-01-12 11:14:33 +03:00
|
|
|
|
{
|
2017-10-25 12:34:33 +03:00
|
|
|
|
$apiVersion = COption::GetOptionString(self::$MODULE_ID, 'api_version', 0);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2018-12-26 11:41:06 +03:00
|
|
|
|
if ((isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY'])
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|| $apiVersion !== 'v5'
|
2018-12-26 11:41:06 +03:00
|
|
|
|
|| !$event->getId()
|
|
|
|
|
) {
|
2017-10-24 11:41:11 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2021-09-08 10:19:29 +03:00
|
|
|
|
$arPayment = [
|
|
|
|
|
'ID' => $event->getId(),
|
2020-04-24 13:18:18 +03:00
|
|
|
|
'ORDER_ID' => $event->getField('ORDER_ID'),
|
2021-09-08 10:19:29 +03:00
|
|
|
|
'LID' => $event->getCollection()->getOrder()->getSiteId(),
|
|
|
|
|
];
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2018-01-12 11:14:33 +03:00
|
|
|
|
if ($optionsSitesList) {
|
|
|
|
|
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
|
|
|
|
|
$site = $optionsSitesList[$arPayment['LID']];
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} elseif (!$optionsSitesList) {
|
2017-10-25 11:25:09 +03:00
|
|
|
|
$site = null;
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
|
|
$api = new RetailCrm\ApiClient(ConfigProvider::getApiUrl(), ConfigProvider::getApiKey());
|
2017-12-04 17:44:30 +03:00
|
|
|
|
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arPayment['ORDER_ID'], $site);
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2017-10-24 11:41:11 +03:00
|
|
|
|
if (isset($orderCrm['order']['payments']) && $orderCrm['order']['payments']) {
|
|
|
|
|
foreach ($orderCrm['order']['payments'] as $payment) {
|
2019-05-31 15:51:40 +03:00
|
|
|
|
if (isset($payment['externalId'])
|
|
|
|
|
&& ($payment['externalId'] == $event->getId()
|
|
|
|
|
|| RCrmActions::getFromPaymentExternalId($payment['externalId']) == $event->getId())
|
|
|
|
|
) {
|
2017-10-24 11:41:11 +03:00
|
|
|
|
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $payment['id']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
2023-08-22 13:09:20 +03:00
|
|
|
|
/**
|
|
|
|
|
* Регистрирует пользователя в CRM системе после регистрации на сайте
|
|
|
|
|
*
|
|
|
|
|
* @param array $arFields
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public static function OnAfterUserRegister(array $arFields): void
|
|
|
|
|
{
|
|
|
|
|
if (isset($arFields['USER_ID']) && $arFields['USER_ID'] > 0) {
|
|
|
|
|
$user = UserRepository::getById($arFields['USER_ID']);
|
|
|
|
|
|
|
|
|
|
if (isset($_POST['REGISTER']['PERSONAL_PHONE'])) {
|
|
|
|
|
$phone = htmlspecialchars($_POST['REGISTER']['PERSONAL_PHONE']);
|
|
|
|
|
|
|
|
|
|
if ($user !== null) {
|
|
|
|
|
$user->setPersonalPhone($phone);
|
|
|
|
|
$user->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$arFields['PERSONAL_PHONE'] = $phone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* @var CustomerService $customerService */
|
|
|
|
|
$customerService = ServiceLocator::get(CustomerService::class);
|
|
|
|
|
$customer = $customerService->createModel($arFields['USER_ID']);
|
|
|
|
|
|
|
|
|
|
$customerService->createOrUpdateCustomer($customer);
|
|
|
|
|
|
|
|
|
|
RetailCrmService::writeLogsSubscribe($arFields);
|
|
|
|
|
|
|
|
|
|
//Если пользователь выразил желание зарегистрироваться в ПЛ и согласился со всеми правилами
|
|
|
|
|
if ((int) $arFields['UF_REG_IN_PL_INTARO'] === 1
|
|
|
|
|
&& (int) $arFields['UF_AGREE_PL_INTARO'] === 1
|
|
|
|
|
&& (int) $arFields['UF_PD_PROC_PL_INTARO'] === 1
|
|
|
|
|
) {
|
|
|
|
|
$phone = $arFields['PERSONAL_PHONE'] ?? '';
|
|
|
|
|
$card = $arFields['UF_CARD_NUM_INTARO'] ?? '';
|
|
|
|
|
$customerId = (string) $arFields['USER_ID'];
|
|
|
|
|
|
|
|
|
|
/** @var LoyaltyAccountService $service */
|
|
|
|
|
$service = ServiceLocator::get(LoyaltyAccountService::class);
|
|
|
|
|
$createResponse = $service->createLoyaltyAccount($phone, $card, $customerId);
|
|
|
|
|
|
|
|
|
|
$service->activateLpUserInBitrix($createResponse, $arFields['USER_ID']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $arFields
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
public static function OnAfterUserAdd($arFields)
|
|
|
|
|
{
|
|
|
|
|
RetailCrmService::writeLogsSubscribe($arFields);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private static function checkConfig(): bool
|
|
|
|
|
{
|
|
|
|
|
if (true == $GLOBALS['ORDER_DELETE_USER_ADMIN']) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] === false
|
|
|
|
|
&& $GLOBALS['RETAILCRM_ORDER_DELETE'] === true
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($GLOBALS['RETAIL_CRM_HISTORY'] === true) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!RetailcrmDependencyLoader::loadDependencies()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Bitrix\Sale\Order|\Bitrix\Main\Event $event
|
|
|
|
|
*
|
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
|
|
|
|
*/
|
|
|
|
|
private static function getOrderArray($event): ?array
|
|
|
|
|
{
|
|
|
|
|
if ($event instanceof Order) {
|
|
|
|
|
$obOrder = $event;
|
|
|
|
|
} elseif ($event instanceof Event) {
|
|
|
|
|
$obOrder = $event->getParameter('ENTITY');
|
|
|
|
|
} else {
|
|
|
|
|
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'events', 'event error');
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RetailCrmOrder::orderObjToArr($obOrder);
|
|
|
|
|
}
|
2019-02-28 14:45:39 +03:00
|
|
|
|
}
|