1
0
Fork 0
mirror of synced 2025-04-20 01:21:01 +00:00

used customerBuilder on customerHistory

This commit is contained in:
gorokh 2020-05-07 15:51:04 +03:00
parent 4f90f15039
commit 1d25481e9e
2 changed files with 49 additions and 132 deletions

View file

@ -17,9 +17,10 @@ class CustomerBuilder implements RetailcrmBuilderInterface
protected $dataCrm;
public $addressBuilder;
public $dbUser;
public $user;
public $registerNewUser;
public $registeredUserID;
/**
* CustomerBuilder constructor.
@ -97,6 +98,16 @@ class CustomerBuilder implements RetailcrmBuilderInterface
return $this;
}
/**
* @param $registeredUserID
* @return $this
*/
public function setRegisteredUserID($registeredUserID)
{
$this->registeredUserID = $registeredUserID;
return $this;
}
/**
* @param $key
* @param null $default
@ -132,7 +143,7 @@ class CustomerBuilder implements RetailcrmBuilderInterface
$this->createCustomer();
}
if (isset($this->dataCrm['externalId'])) {
if (isset($this->registeredUserID)) {
$this->updateCustomer();
}
@ -158,7 +169,7 @@ class CustomerBuilder implements RetailcrmBuilderInterface
return false;
}
$registerNewUser = true;
$this->registerNewUser = true;
if (!isset($this->dataCrm['email']) || $this->dataCrm['email'] == '') {
$login = uniqid('user_' . time()) . '@crm.com';
$this->dataCrm['email'] = $login;
@ -169,8 +180,8 @@ class CustomerBuilder implements RetailcrmBuilderInterface
break;
case 1:
$arUser = $this->dbUser->Fetch();
$registeredUserID = $arUser['ID'];
$registerNewUser = false;
$this->setRegisteredUserID($arUser['ID']);
$this->registerNewUser = false;
break;
default:
$login = uniqid('user_' . time()) . '@crm.com';
@ -178,7 +189,7 @@ class CustomerBuilder implements RetailcrmBuilderInterface
}
}
if ($registerNewUser === true) {
if ( $this->registerNewUser === true) {
$userPassword = uniqid("R");
$this->customer->setEmail($this->dataCrm['email'])
@ -192,7 +203,7 @@ class CustomerBuilder implements RetailcrmBuilderInterface
public function updateCustomer()
{
if (!empty($this->dataCrm['firstName'])) {
$this->customer->setName(RCrmActions::fromJSON($this->dataCrm['lastName']));
$this->customer->setName(RCrmActions::fromJSON($this->dataCrm['firstName']));
}
if (!empty($this->dataCrm['lastName'])) {

View file

@ -68,6 +68,7 @@ class RetailCrmHistory
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
$newUser = new CUser();
$customerBuilder = new CustomerBuilder();
foreach ($customers as $customer) {
if (function_exists('retailCrmBeforeCustomerSave')) {
@ -81,54 +82,21 @@ class RetailCrmHistory
}
}
if (isset($customer['deleted'])) {
continue;
$customerBuilder->setDataCrm($customer);
if (isset($customer['email'])) {
$customerBuilder->setDbUser(
CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $customer['email']))
);
}
$customerBuilder->build();
if (isset($customer['externalId']) && !is_numeric($customer['externalId'])) {
unset($customer['externalId']);
}
if (!isset($customer['externalId'])) {
if (!isset($customer['id'])) {
continue;
}
$registerNewUser = true;
if (!isset($customer['email']) || $customer['email'] == '') {
$login = uniqid('user_' . time()) . '@crm.com';
$customer['email'] = $login;
} else {
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $customer['email']));
switch ($dbUser->SelectedRowsCount()) {
case 0:
$login = $customer['email'];
break;
case 1:
$arUser = $dbUser->Fetch();
$registeredUserID = $arUser['ID'];
$registerNewUser = false;
break;
default:
$login = uniqid('user_' . time()) . '@crm.com';
break;
}
}
if ($registerNewUser === true) {
$userPassword = uniqid("R");
$arFields = array(
"EMAIL" => $customer['email'],
"LOGIN" => $login,
"ACTIVE" => "Y",
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
if (!isset($customerBuilder->registeredUserID)) {
if (true === $customerBuilder->registerNewUser) {
$customerBuilder->registeredUserID = $newUser->Add(
$customerBuilder->objectToArray($customerBuilder->customer)
);
$registeredUserID = $newUser->Add($arFields);
if ($registeredUserID === false) {
if ($customerBuilder->registeredUserID === false) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'CUser::Register',
@ -139,97 +107,35 @@ class RetailCrmHistory
}
if(RCrmActions::apiMethod(
$api,
'customersFixExternalIds',
__METHOD__,
array(array('id' => $customer['id'], 'externalId' => $registeredUserID))) == false
$api,
'customersFixExternalIds',
__METHOD__,
array(array('id' => $customer['id'], 'externalId' => $customerBuilder->registeredUserID))) == false
) {
continue;
}
}
$customer['externalId'] = $registeredUserID;
$customer['externalId'] = $customerBuilder->registeredUserID;
}
}
if (isset($customer['externalId'])) {
$arUser = array();
if (array_key_exists('firstName', $customer)) {
$arUser["NAME"] = $customer['firstName']
? RCrmActions::fromJSON($customer['firstName']) : '';
}
if (array_key_exists('lastName', $customer)) {
$arUser["LAST_NAME"] = $customer['lastName']
? RCrmActions::fromJSON($customer['lastName']) : '';
}
if (array_key_exists('patronymic', $customer)) {
$arUser["SECOND_NAME"] = $customer['patronymic']
? RCrmActions::fromJSON($customer['patronymic']) : '';
}
// if (array_key_exists('email', $customer)) {
// $arUser["EMAIL"] = $customer['email'] ? RCrmActions::fromJSON($customer['email']) : '';
// }
if (isset($customer['phones'])) {
$user = CUser::GetList(
($by = "ID"),
($order = "desc"),
array('ID' => $customer['externalId']),
array('FIELDS' => array('PERSONAL_PHONE', 'PERSONAL_MOBILE'))
)->fetch();
foreach ($customer['phones'] as $phone) {
if (isset($phone['old_number']) && in_array($phone['old_number'], $user)) {
$key = array_search($phone['old_number'], $user);
if (isset($phone['number'])) {
$arUser[$key] = $phone['number'];
$user[$key] = $phone['number'];
} else {
$arUser[$key] = '';
$user[$key] = '';
}
}
if (isset($phone['number'])) {
if ((!isset($user['PERSONAL_PHONE']) || strlen($user['PERSONAL_PHONE']) == 0)
&& $user['PERSONAL_MOBILE'] != $phone['number']
) {
$arUser['PERSONAL_PHONE'] = $phone['number'];
$user['PERSONAL_PHONE'] = $phone['number'];
continue;
}
if ((!isset($user['PERSONAL_MOBILE']) || strlen($user['PERSONAL_MOBILE']) == 0)
&& $user['PERSONAL_PHONE'] != $phone['number']
) {
$arUser['PERSONAL_MOBILE'] = $phone['number'];
$user['PERSONAL_MOBILE'] = $phone['number'];
continue;
}
}
}
}
if (array_key_exists('index', $customer['address'])) {
$arUser["PERSONAL_ZIP"] = $customer['address']['index']
? RCrmActions::fromJSON($customer['address']['index']) : '';
}
if (array_key_exists('city', $customer['address'])) {
$arUser["PERSONAL_CITY"] = $customer['address']['city']
? RCrmActions::fromJSON($customer['address']['city']) : '';
$customerBuilder->setUser(
CUser::GetList(
($by = "ID"),
($order = "desc"),
array('ID' => $customer['externalId']),
array('FIELDS' => array('PERSONAL_PHONE', 'PERSONAL_MOBILE'))
)->fetch()
);
}
$customerBuilder->build();
if (array_key_exists('birthday', $customer)) {
$arUser["PERSONAL_BIRTHDAY"] = date("d.m.Y", strtotime($customer['birthday']));
}
if (array_key_exists('email', $customer)) {
$arUser["EMAIL"] = $customer['email'] ? RCrmActions::fromJSON($customer['email']) : '';
}
if (array_key_exists('sex', $customer)) {
$arUser["PERSONAL_GENDER"] = $customer['sex'] ? RCrmActions::fromJSON($customer['sex']) : '';
}
$u = $newUser->Update($customer['externalId'], $arUser);
$u = $newUser->Update(
$customer['externalId'],
$customerBuilder->objectToArray($customerBuilder->customer)
);
if (!$u) {
RCrmActions::eventLog(
'RetailCrmHistory::customerHistory',