2016-09-15 16:42:10 +03:00
|
|
|
<?php
|
2024-04-29 11:44:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @category RetailCRM
|
|
|
|
* @package RetailCRM\User
|
|
|
|
* @author RetailCRM <integration@retailcrm.ru>
|
|
|
|
* @license MIT
|
|
|
|
* @link http://retailcrm.ru
|
|
|
|
* @see http://retailcrm.ru/docs
|
|
|
|
*/
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
IncludeModuleLangFile(__FILE__);
|
2024-04-29 11:44:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RetailCrmUser
|
|
|
|
*
|
|
|
|
* @category RetailCRM
|
|
|
|
* @package RetailCRM\User
|
|
|
|
*/
|
2016-09-15 16:42:10 +03:00
|
|
|
class RetailCrmUser
|
|
|
|
{
|
2021-07-08 16:29:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $arFields
|
|
|
|
* @param $api
|
|
|
|
* @param $contragentType
|
|
|
|
* @param false $send
|
|
|
|
* @param null $site
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function customerSend(array $arFields, $api, $contragentType, bool $send = false, $site = null)
|
2019-11-01 12:00:54 +03:00
|
|
|
{
|
2017-09-04 11:36:04 +03:00
|
|
|
if (!$api || empty($contragentType)) {
|
2016-09-15 16:42:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-08 16:29:34 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
if (empty($arFields)) {
|
2016-10-31 17:56:11 +03:00
|
|
|
RCrmActions::eventLog('RetailCrmUser::customerSend', 'empty($arFields)', 'incorrect customer');
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
return false;
|
2017-09-04 15:51:40 +03:00
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
$customer = self::getSimpleCustomer($arFields);
|
|
|
|
$customer['createdAt'] = new \DateTime($arFields['DATE_REGISTER']);
|
|
|
|
$customer['contragent'] = ['contragentType' => $contragentType];
|
2020-12-22 13:03:32 +03:00
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
if (RetailcrmConfigProvider::getCustomFieldsStatus() === 'Y') {
|
|
|
|
$customer['customFields'] = self::getCustomFields($arFields);
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
if ($send && isset($_COOKIE['_rc']) && $_COOKIE['_rc'] != '') {
|
2016-09-15 16:42:10 +03:00
|
|
|
$customer['browserId'] = $_COOKIE['_rc'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function_exists('retailCrmBeforeCustomerSend')) {
|
|
|
|
$newResCustomer = retailCrmBeforeCustomerSend($customer);
|
2021-07-08 16:29:34 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
if (is_array($newResCustomer) && !empty($newResCustomer)) {
|
|
|
|
$customer = $newResCustomer;
|
2017-09-04 11:36:04 +03:00
|
|
|
} elseif ($newResCustomer === false) {
|
|
|
|
RCrmActions::eventLog('RetailCrmUser::customerSend', 'retailCrmBeforeCustomerSend()', 'UserID = ' . $arFields['ID'] . '. Sending canceled after retailCrmBeforeCustomerSend');
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
return false;
|
2016-09-15 16:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$normalizer = new RestNormalizer();
|
|
|
|
$customer = $normalizer->normalize($customer, 'customers');
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2023-12-08 12:27:10 +03:00
|
|
|
if (array_key_exists('UF_SUBSCRIBE_USER_EMAIL', $arFields)) {
|
|
|
|
// UF_SUBSCRIBE_USER_EMAIL = '1' or '0'
|
|
|
|
$customer['subscribed'] = (bool) $arFields['UF_SUBSCRIBE_USER_EMAIL'];
|
2023-08-22 13:09:20 +03:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
Logger::getInstance()->write($customer, 'customerSend');
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
if (
|
|
|
|
$send
|
|
|
|
&& !RCrmActions::apiMethod($api, 'customersCreate', __METHOD__, $customer, $site)
|
|
|
|
) {
|
2016-09-15 16:42:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
return $customer;
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
public static function customerEdit($arFields, $api, $optionsSitesList = array()) : bool
|
2021-07-08 16:29:34 +03:00
|
|
|
{
|
2016-10-03 16:56:59 +03:00
|
|
|
if (empty($arFields)) {
|
2016-10-31 17:56:11 +03:00
|
|
|
RCrmActions::eventLog('RetailCrmUser::customerEdit', 'empty($arFields)', 'incorrect customer');
|
2016-10-03 16:56:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2021-07-08 16:29:34 +03:00
|
|
|
$customer = self::getSimpleCustomer($arFields);
|
2016-10-03 16:56:59 +03:00
|
|
|
$found = false;
|
2021-07-08 16:29:34 +03:00
|
|
|
|
2023-12-19 17:13:31 +03:00
|
|
|
if (RetailcrmConfigProvider::getCustomFieldsStatus() === 'Y') {
|
|
|
|
$customer['customFields'] = self::getCustomFields($arFields);
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
if (count($optionsSitesList) > 0) {
|
2016-10-04 17:57:39 +03:00
|
|
|
foreach ($optionsSitesList as $site) {
|
2016-10-03 16:56:59 +03:00
|
|
|
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arFields['ID'], $site);
|
2016-10-04 17:57:39 +03:00
|
|
|
if (isset($userCrm['customer'])) {
|
2016-10-03 16:56:59 +03:00
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 17:57:39 +03:00
|
|
|
} else {
|
2016-10-03 16:56:59 +03:00
|
|
|
$site = null;
|
|
|
|
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arFields['ID'], $site);
|
2016-10-04 17:57:39 +03:00
|
|
|
if (isset($userCrm['customer'])) {
|
2016-10-03 16:56:59 +03:00
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
if ($found) {
|
2016-10-03 16:56:59 +03:00
|
|
|
$normalizer = new RestNormalizer();
|
|
|
|
$customer = $normalizer->normalize($customer, 'customers');
|
2023-08-22 13:09:20 +03:00
|
|
|
$customer = self::getBooleanFields($customer, $arFields);
|
2016-10-03 16:56:59 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
if (function_exists('retailCrmBeforeCustomerSend')) {
|
|
|
|
$newResCustomer = retailCrmBeforeCustomerSend($customer);
|
2016-10-03 16:56:59 +03:00
|
|
|
if (is_array($newResCustomer) && !empty($newResCustomer)) {
|
|
|
|
$customer = $newResCustomer;
|
2017-09-04 11:36:04 +03:00
|
|
|
} elseif ($newResCustomer === false) {
|
|
|
|
RCrmActions::eventLog('RetailCrmUser::customerEdit', 'retailCrmBeforeCustomerSend()', 'UserID = ' . $arFields['ID'] . '. Sending canceled after retailCrmBeforeCustomerSend');
|
|
|
|
|
|
|
|
return false;
|
2016-10-03 16:56:59 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
|
2020-04-24 13:18:18 +03:00
|
|
|
Logger::getInstance()->write($customer, 'customerSend');
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2016-10-03 16:56:59 +03:00
|
|
|
RCrmActions::apiMethod($api, 'customersEdit', __METHOD__, $customer, $site);
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2016-10-03 16:56:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
2021-07-08 16:29:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $arFields
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function getSimpleCustomer(array $arFields): array
|
|
|
|
{
|
|
|
|
$customer['externalId'] = $arFields['ID'];
|
|
|
|
$customer['firstName'] = $arFields['NAME'] ?? null;
|
|
|
|
$customer['lastName'] = $arFields['LAST_NAME'] ?? null;
|
|
|
|
$customer['patronymic'] = $arFields['SECOND_NAME'] ?? null;
|
|
|
|
$customer['phones'][]['number'] = $arFields['PERSONAL_PHONE'] ?? null;
|
|
|
|
$customer['phones'][]['number'] = $arFields['WORK_PHONE'] ?? null;
|
|
|
|
$customer['address']['city'] = $arFields['PERSONAL_CITY'] ?? null;
|
|
|
|
$customer['address']['text'] = $arFields['PERSONAL_STREET'] ?? null;
|
|
|
|
$customer['address']['index'] = $arFields['PERSONAL_ZIP'] ?? null;
|
|
|
|
|
|
|
|
if (mb_strlen($arFields['EMAIL']) < 100) {
|
|
|
|
$customer['email'] = $arFields['EMAIL'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
}
|
2023-08-22 13:09:20 +03:00
|
|
|
|
|
|
|
private static function getBooleanFields($customer, $arFields)
|
|
|
|
{
|
|
|
|
if (isset($arFields['UF_SUBSCRIBE_USER_EMAIL'])) {
|
|
|
|
if ($arFields['UF_SUBSCRIBE_USER_EMAIL'] === "1") {
|
|
|
|
$customer['subscribed'] = true;
|
|
|
|
} else {
|
|
|
|
$customer['subscribed'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
}
|
2023-12-19 17:13:31 +03:00
|
|
|
|
|
|
|
private static function getCustomFields(array $arFields)
|
|
|
|
{
|
|
|
|
if (!method_exists(RCrmActions::class, 'getTypeUserField')
|
|
|
|
|| !method_exists(RCrmActions::class, 'convertCmsFieldToCrmValue')
|
|
|
|
) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$customUserFields = RetailcrmConfigProvider::getMatchedUserFields();
|
|
|
|
$typeList = RCrmActions::getTypeUserField();
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ($customUserFields as $code => $codeCrm) {
|
|
|
|
if (isset($arFields[$code])) {
|
|
|
|
$type = $typeList[$code] ?? '';
|
|
|
|
$result[$codeCrm] = RCrmActions::convertCmsFieldToCrmValue($arFields[$code], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
}
|