2020-07-14 13:47:26 +03:00
|
|
|
<?php
|
|
|
|
|
2024-04-29 11:44:31 +03:00
|
|
|
/**
|
|
|
|
* @category RetailCRM
|
|
|
|
* @package RetailCRM
|
|
|
|
* @author RetailCRM <integration@retailcrm.ru>
|
|
|
|
* @license MIT
|
|
|
|
* @link http://retailcrm.ru
|
|
|
|
* @see http://retailcrm.ru/docs
|
|
|
|
*/
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
/**
|
|
|
|
* Class CustomerBuilder
|
2024-04-29 11:44:31 +03:00
|
|
|
*
|
|
|
|
* @category RetailCRM
|
|
|
|
* @package RetailCRM
|
2020-07-14 13:47:26 +03:00
|
|
|
*/
|
|
|
|
class CustomerBuilder extends AbstractBuilder implements RetailcrmBuilderInterface
|
|
|
|
{
|
|
|
|
/** @var Customer */
|
|
|
|
protected $customer;
|
|
|
|
|
|
|
|
/** @var CustomerAddress */
|
|
|
|
protected $customerAddress;
|
|
|
|
|
|
|
|
/** @var array $dataCrm customerHistory */
|
|
|
|
protected $dataCrm;
|
|
|
|
|
|
|
|
/** @var AddressBuilder */
|
|
|
|
protected $addressBuilder;
|
|
|
|
|
|
|
|
/** @var CUser */
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/** @var bool $registerNewUser */
|
|
|
|
protected $registerNewUser;
|
|
|
|
|
|
|
|
/** @var int $registeredUserID */
|
|
|
|
protected $registeredUserID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CustomerBuilder constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->customer = new Customer();
|
|
|
|
$this->customerAddress = new CustomerAddress();
|
|
|
|
$this->addressBuilder = new AddressBuilder();
|
|
|
|
}
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
/**
|
|
|
|
* @param Customer $customer
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function setCustomer(Customer $customer)
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
$this->customer = $customer;
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Customer
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function getCustomer()
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
return $this->customer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CustomerAddress $customerAddress
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-03-23 14:36:48 +03:00
|
|
|
public function setCustomerAddress($customerAddress): CustomerBuilder
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
$this->customerAddress = $customerAddress;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return CustomerAddress
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function getCustomerAddress()
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
return $this->customerAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $dataCrm
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function setDataCrm($dataCrm)
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
$this->dataCrm = $dataCrm;
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $user
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function setUser($user)
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
/**
|
|
|
|
* @param int $registeredUserID
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function setRegisteredUserID(int $registeredUserID)
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
$this->registeredUserID = $registeredUserID;
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function getRegisteredUserID()
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
return $this->registeredUserID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function getRegisterNewUser()
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
return $this->registerNewUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function build()
|
|
|
|
{
|
|
|
|
if (!empty($this->dataCrm['firstName'])) {
|
|
|
|
$this->customer->setName($this->fromJSON($this->dataCrm['firstName']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->dataCrm['lastName'])) {
|
|
|
|
$this->customer->setLastName($this->fromJSON($this->dataCrm['lastName']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->dataCrm['patronymic'])) {
|
|
|
|
$this->customer->setSecondName($this->fromJSON($this->dataCrm['patronymic']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->dataCrm['phones'])) {
|
|
|
|
foreach ($this->dataCrm['phones'] as $phone) {
|
2023-03-15 11:55:04 +03:00
|
|
|
if (is_array($this->user) && isset($phone['old_number']) && in_array($phone['old_number'], $this->user)) {
|
2021-04-05 13:40:53 +03:00
|
|
|
$key = array_search($phone['old_number'], $this->user);
|
2020-07-14 13:47:26 +03:00
|
|
|
|
|
|
|
if (isset($phone['number'])) {
|
|
|
|
$this->user[$key] = $phone['number'];
|
|
|
|
} else {
|
|
|
|
$this->user[$key] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($phone['number'])) {
|
2021-03-23 14:36:48 +03:00
|
|
|
if ((!isset($this->user['PERSONAL_PHONE']) || '' == $this->user['PERSONAL_PHONE'])
|
2020-07-14 13:47:26 +03:00
|
|
|
&& $this->user['PERSONAL_MOBILE'] != $phone['number']
|
|
|
|
) {
|
|
|
|
$this->customer->setPersonalPhone($phone['number']);
|
|
|
|
$this->user['PERSONAL_PHONE'] = $phone['number'];
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-23 14:36:48 +03:00
|
|
|
if ((!isset($this->user['PERSONAL_MOBILE']) || '' == $this->user['PERSONAL_MOBILE'])
|
2020-07-14 13:47:26 +03:00
|
|
|
&& $this->user['PERSONAL_PHONE'] != $phone['number']
|
|
|
|
) {
|
|
|
|
$this->customer->setPersonalMobile($phone['number']);
|
|
|
|
$this->user['PERSONAL_MOBILE'] = $phone['number'];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->dataCrm['address']['index'])) {
|
|
|
|
$this->customer->setPersonalZip($this->fromJSON($this->dataCrm['address']['index']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->dataCrm['address']['city'])) {
|
|
|
|
$this->customer->setPersonalCity($this->fromJSON($this->dataCrm['address']['city']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->dataCrm['birthday'])) {
|
|
|
|
$this->customer->setPersonalBirthday($this->fromJSON(
|
|
|
|
date("d.m.Y", strtotime($this->dataCrm['birthday']))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->dataCrm['email'])) {
|
|
|
|
$this->customer->setEmail($this->fromJSON($this->dataCrm['email']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->dataCrm['sex'])) {
|
|
|
|
$this->customer->setPersonalGender($this->fromJSON($this->dataCrm['sex']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!isset($this->dataCrm['email']) || $this->dataCrm['email'] == '')
|
|
|
|
&& (!isset($this->dataCrm['externalId']))
|
|
|
|
) {
|
|
|
|
$login = uniqid('user_' . time()) . '@example.com';
|
|
|
|
$this->customer->setLogin($login)
|
|
|
|
->setEmail($login);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->dataCrm['address'])) {
|
|
|
|
$this->buildAddress();
|
|
|
|
}
|
2023-08-22 13:09:20 +03:00
|
|
|
|
|
|
|
// клиент считается подписанным при значении равном null
|
|
|
|
if (array_key_exists('emailMarketingUnsubscribedAt', $this->dataCrm)) {
|
|
|
|
if (empty($this->dataCrm['emailMarketingUnsubscribedAt'])) {
|
|
|
|
$this->customer->setSubscribe('Y');
|
|
|
|
} else {
|
|
|
|
$this->customer->setSubscribe('N');
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 15:10:19 +03:00
|
|
|
|
|
|
|
if (empty($this->dataCrm['externalId'])
|
|
|
|
&& (empty($this->dataCrm['firstName'])
|
|
|
|
|| empty($this->dataCrm['email']))
|
|
|
|
) {
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
|
|
|
$customerResponse = RCrmActions::apiMethod($api, 'customersGetById', __METHOD__, $this->dataCrm['id']);
|
|
|
|
|
|
|
|
if ($customerResponse instanceof RetailCrm\Response\ApiResponse
|
|
|
|
&& $customerResponse->isSuccessful()
|
|
|
|
&& !empty($customerResponse['customer'])
|
|
|
|
) {
|
|
|
|
$crmCustomer = $customerResponse['customer'];
|
|
|
|
|
|
|
|
if (empty($this->dataCrm['email'])
|
|
|
|
&& !empty($crmCustomer['email'])
|
|
|
|
) {
|
|
|
|
$email = $crmCustomer['email'];
|
|
|
|
|
|
|
|
$this->customer->setEmail($this->fromJSON($email));
|
|
|
|
$this->customer->setLogin($email);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->dataCrm['firstName'])
|
|
|
|
&& !empty($crmCustomer['firstName'])
|
|
|
|
) {
|
|
|
|
$this->customer->setName($this->fromJSON($crmCustomer['firstName']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 13:47:26 +03:00
|
|
|
}
|
|
|
|
|
2022-09-07 10:35:40 +03:00
|
|
|
public function buildPassword()
|
|
|
|
{
|
|
|
|
$userPassword = uniqid("R");
|
|
|
|
$this->customer->setPassword($userPassword)
|
|
|
|
->setConfirmPassword($userPassword);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
public function buildAddress()
|
|
|
|
{
|
|
|
|
if (isset($this->dataCrm['address'])) {
|
|
|
|
$this->addressBuilder->setDataCrm($this->dataCrm['address'])->build();
|
|
|
|
$this->customerAddress = $this->addressBuilder->getCustomerAddress();
|
|
|
|
} else {
|
|
|
|
$this->customerAddress = null;
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
/**
|
|
|
|
* @param string $login
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function setLogin(string $login)
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
$this->customer->setLogin($login);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2021-03-23 14:36:48 +03:00
|
|
|
|
2020-07-14 13:47:26 +03:00
|
|
|
/**
|
|
|
|
* @param string $email
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-04-05 13:40:53 +03:00
|
|
|
public function setEmail(string $email)
|
2020-07-14 13:47:26 +03:00
|
|
|
{
|
|
|
|
$this->customer->setEmail($email);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2021-03-23 14:36:48 +03:00
|
|
|
|
|
|
|
public function reset(): void
|
|
|
|
{
|
|
|
|
$this->customer = new Customer();
|
|
|
|
$this->customerAddress = new CustomerAddress();
|
|
|
|
$this->addressBuilder->reset();
|
|
|
|
}
|
2020-07-14 13:47:26 +03:00
|
|
|
}
|