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

110 lines
4.0 KiB
PHP
Raw Normal View History

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)
{
if (!$api || empty($contragentType)) { // add cond to check $arParams
2016-09-15 16:42:10 +03:00
return false;
}
if (empty($arFields)) {
RCrmActions::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect customer');
return false;
}
$customer = array(
'externalId' => $arFields['ID'],
'firstName' => $arFields['NAME'],
'lastName' => $arFields['LAST_NAME'],
'patronymic' => $arFields['SECOND_NAME'],
'createdAt' => new \DateTime($arFields['DATE_REGISTER']),
'contragentType' => $contragentType
);
2016-10-04 17:57:39 +03:00
if (isset($arFields['PERSONAL_PHONE'])) {
2016-09-15 16:42:10 +03:00
$customer['phones'][]['number'] = $arFields['PERSONAL_PHONE'];
}
2016-10-04 17:57:39 +03:00
if (isset($arUser['WORK_PHONE'])) {
2016-09-15 16:42:10 +03:00
$customer['phones'][]['number'] = $arFields['WORK_PHONE'];
}
2016-10-04 17:57:39 +03:00
if (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;
}
}
$normalizer = new RestNormalizer();
$customer = $normalizer->normalize($customer, 'customers');
$log = new Logger();
$log->write($customer, 'customer');
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;
}
}
return $customer;
}
2016-10-03 16:56:59 +03:00
public static function customerEdit($arFields, $api, $optionsSitesList = array()){
if (empty($arFields)) {
RCrmActions::eventLog('RetailCrmUser::orderEdit', 'empty($arFields)', 'incorrect customer');
return false;
}
$customer = array(
'externalId' => $arFields['ID'],
'firstName' => $arFields['NAME'],
'lastName' => $arFields['LAST_NAME'],
'patronymic' => $arFields['SECOND_NAME'],
'email' => $arFields['EMAIL']
);
2016-10-04 17:57:39 +03:00
if (isset($arFields['PERSONAL_PHONE'])) {
2016-10-03 16:56:59 +03:00
$customer['phones'][]['number'] = $arFields['PERSONAL_PHONE'];
}
2016-10-04 17:57:39 +03:00
if (isset($arFields['WORK_PHONE'])) {
2016-10-03 16:56:59 +03:00
$customer['phones'][]['number'] = $arFields['WORK_PHONE'];
}
$found = false;
2016-10-04 17:57:39 +03:00
if (count($optionsSitesList) > 1) {
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;
}
}
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');
$log = new Logger();
$log->write($customer, 'customer');
if (function_exists('retailcrmBeforeCustomerSend')) {
$newResCustomer = intarocrm_before_customer_send($customer);
if (is_array($newResCustomer) && !empty($newResCustomer)) {
$customer = $newResCustomer;
}
}
RCrmActions::apiMethod($api, 'customersEdit', __METHOD__, $customer, $site);
}
return true;
}
2016-09-15 16:42:10 +03:00
}