2016-09-15 16:42:10 +03:00
|
|
|
<?php
|
|
|
|
IncludeModuleLangFile(__FILE__);
|
|
|
|
class RetailCrmUser
|
|
|
|
{
|
2016-10-04 17:57:39 +03:00
|
|
|
public static function customerSend($arFields, $api, $contragentType, $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;
|
|
|
|
}
|
|
|
|
if (empty($arFields)) {
|
2016-10-31 17:56:11 +03:00
|
|
|
RCrmActions::eventLog('RetailCrmUser::customerSend', 'empty($arFields)', 'incorrect customer');
|
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
|
|
|
$customer = array(
|
|
|
|
'externalId' => $arFields['ID'],
|
2016-10-31 17:56:11 +03:00
|
|
|
'email' => $arFields['EMAIL'],
|
2016-09-15 16:42:10 +03:00
|
|
|
'createdAt' => new \DateTime($arFields['DATE_REGISTER']),
|
2018-02-27 15:29:43 +03:00
|
|
|
'contragent' => array(
|
|
|
|
'contragentType' => $contragentType
|
|
|
|
)
|
2016-09-15 16:42:10 +03:00
|
|
|
);
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2017-09-04 15:51:40 +03:00
|
|
|
if (!empty($arFields['NAME'])) {
|
|
|
|
$customer['firstName'] = $arFields['NAME'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['LAST_NAME'])) {
|
|
|
|
$customer['lastName'] = $arFields['LAST_NAME'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['SECOND_NAME'])) {
|
|
|
|
$customer['patronymic'] = $arFields['SECOND_NAME'];
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
if (!empty($arFields['PERSONAL_PHONE'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$customer['phones'][]['number'] = $arFields['PERSONAL_PHONE'];
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
if (!empty($arFields['WORK_PHONE'])) {
|
2016-09-15 16:42:10 +03:00
|
|
|
$customer['phones'][]['number'] = $arFields['WORK_PHONE'];
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2017-09-04 15:51:40 +03:00
|
|
|
if (!empty($arFields['PERSONAL_CITY'])) {
|
|
|
|
$customer['address']['city'] = $arFields['PERSONAL_CITY'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['PERSONAL_STREET'])) {
|
|
|
|
$customer['address']['text'] = $arFields['PERSONAL_STREET'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['PERSONAL_ZIP'])) {
|
|
|
|
$customer['address']['index'] = $arFields['PERSONAL_ZIP'];
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
$log = new Logger();
|
2017-09-22 15:28:19 +03:00
|
|
|
$log->write($customer, 'customerSend');
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2016-10-04 17:57:39 +03:00
|
|
|
if ($send) {
|
2016-09-15 16:42:10 +03:00
|
|
|
if (!RCrmActions::apiMethod($api, 'customersCreate', __METHOD__, $customer, $site)) {
|
|
|
|
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
|
|
|
|
2016-10-03 16:56:59 +03:00
|
|
|
public static function customerEdit($arFields, $api, $optionsSitesList = array()){
|
|
|
|
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
|
|
|
|
2016-10-03 16:56:59 +03:00
|
|
|
$customer = array(
|
|
|
|
'externalId' => $arFields['ID'],
|
2016-10-31 17:56:11 +03:00
|
|
|
'email' => $arFields['EMAIL'],
|
2016-10-03 16:56:59 +03:00
|
|
|
);
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2017-09-04 15:51:40 +03:00
|
|
|
if (!empty($arFields['NAME'])) {
|
|
|
|
$customer['firstName'] = $arFields['NAME'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['LAST_NAME'])) {
|
|
|
|
$customer['lastName'] = $arFields['LAST_NAME'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['SECOND_NAME'])) {
|
|
|
|
$customer['patronymic'] = $arFields['SECOND_NAME'];
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2017-09-04 11:36:04 +03:00
|
|
|
if (!empty($arFields['PERSONAL_PHONE'])) {
|
2016-10-03 16:56:59 +03:00
|
|
|
$customer['phones'][]['number'] = $arFields['PERSONAL_PHONE'];
|
|
|
|
}
|
2017-09-04 11:36:04 +03:00
|
|
|
if (!empty($arFields['WORK_PHONE'])) {
|
2016-10-03 16:56:59 +03:00
|
|
|
$customer['phones'][]['number'] = $arFields['WORK_PHONE'];
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2017-09-04 15:51:40 +03:00
|
|
|
if (!empty($arFields['PERSONAL_CITY'])) {
|
|
|
|
$customer['address']['city'] = $arFields['PERSONAL_CITY'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['PERSONAL_STREET'])) {
|
|
|
|
$customer['address']['text'] = $arFields['PERSONAL_STREET'];
|
|
|
|
}
|
|
|
|
if (!empty($arFields['PERSONAL_ZIP'])) {
|
|
|
|
$customer['address']['index'] = $arFields['PERSONAL_ZIP'];
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
|
2016-10-03 16:56:59 +03:00
|
|
|
$found = false;
|
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');
|
|
|
|
|
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
|
|
|
|
2019-11-01 12:00:54 +03:00
|
|
|
$log = new Logger();
|
|
|
|
$log->write($customer, 'customerSend');
|
|
|
|
|
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;
|
|
|
|
}
|
2019-11-01 12:00:54 +03:00
|
|
|
}
|