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

add AdressBuilder

This commit is contained in:
gorokh 2020-05-07 11:56:21 +03:00
parent 9bbed83dc3
commit 4f90f15039
6 changed files with 150 additions and 91 deletions

View file

@ -0,0 +1,60 @@
<?php
IncludeModuleLangFile(__FILE__);
/**
* Class AdressBuilder
*/
class AdressBuilder implements RetailcrmBuilderInterface
{
/** @var classes/general/Model/CustomerAddress */
public $customerAddress;
/** @var array $dataCrm customerHistory */
protected $dataCrm;
/**
* CustomerBuilder constructor.
*/
public function __construct()
{
$this->customerAddress = new CustomerAddress();
}
public function setDataCrm($dataCrm)
{
$this->dataCrm = $dataCrm;
return $this;
}
/**
* @param $array
* @param $key
* @param null $default
* @return mixed|null
*/
protected function getValue($array, $key, $default = NULL)
{
return isset($array[$key]) && !empty($array[$key]) ? $array[$key] : $default;
}
public function build()
{
$this->customerAddress->setText($this->getValue($this->dataCrm,'text'))
->setNotes($this->getValue($this->dataCrm,'notes'))
->setBuilding($this->getValue($this->dataCrm,'building'))
->setBlock($this->getValue($this->dataCrm,'block'))
->setCity($this->getValue($this->dataCrm,'city'))
->setFlat($this->getValue($this->dataCrm,'flat'))
->setHouse($this->getValue($this->dataCrm,'house'))
->setFloor($this->getValue($this->dataCrm,'floor'))
->setCountry($this->getValue($this->dataCrm,'countryIso'))
->setIndex($this->getValue($this->dataCrm,'index'))
->setIntercomCode($this->getValue($this->dataCrm,'intercomCode'))
->setMetro($this->getValue($this->dataCrm,'metro'))
->setRegion($this->getValue($this->dataCrm,'region'))
->setStreet($this->getValue($this->dataCrm,'street'));
return $this;
}
}

View file

@ -5,7 +5,7 @@ IncludeModuleLangFile(__FILE__);
/**
* Class CustomerBuilder
*/
class CustomerBuilder
class CustomerBuilder implements RetailcrmBuilderInterface
{
/** @var classes/general/Model/Customer */
public $customer;
@ -16,6 +16,11 @@ class CustomerBuilder
/** @var array $dataCrm customerHistory */
protected $dataCrm;
public $addressBuilder;
public $dbUser;
public $user;
/**
* CustomerBuilder constructor.
*/
@ -23,6 +28,7 @@ class CustomerBuilder
{
$this->customer = new Customer();
$this->customerAddress = new CustomerAddress();
$this->addressBuilder = new AdressBuilder();
}
/**
@ -71,6 +77,26 @@ class CustomerBuilder
return $this;
}
/**
* @param $dbUser
* @return $this
*/
public function setDbUser($dbUser)
{
$this->dbUser = $dbUser;
return $this;
}
/**
* @param $user
* @return $this
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* @param $key
* @param null $default
@ -117,7 +143,13 @@ class CustomerBuilder
public function buildAddress()
{
$this->customerAddress->setData($this->dataCrm['address']);
if (isset($this->dataCrm['address'])) {
$this->addressBuilder->setDataCrm($this->dataCrm['address']);
$this->addressBuilder->build();
$this->customerAddress = $this->addressBuilder->customerAddress;
} else {
$this->customerAddress = null;
}
}
public function createCustomer()
@ -131,13 +163,12 @@ class CustomerBuilder
$login = uniqid('user_' . time()) . '@crm.com';
$this->dataCrm['email'] = $login;
} else {
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $this->dataCrm['email']));
switch ($dbUser->SelectedRowsCount()) {
switch ($this->dbUser->SelectedRowsCount()) {
case 0:
$login = $this->dataCrm['email'];
break;
case 1:
$arUser = $dbUser->Fetch();
$arUser = $this->dbUser->Fetch();
$registeredUserID = $arUser['ID'];
$registerNewUser = false;
break;
@ -160,75 +191,66 @@ class CustomerBuilder
public function updateCustomer()
{
if (array_key_exists('firstName', $this->dataCrm)) {
$this->customer->setName($this->dataCrm['firstName']
? RCrmActions::fromJSON($this->dataCrm['firstName']) : '');
if (!empty($this->dataCrm['firstName'])) {
$this->customer->setName(RCrmActions::fromJSON($this->dataCrm['lastName']));
}
if (array_key_exists('lastName', $this->dataCrm)) {
$this->customer->setLastName($this->dataCrm['lastName']
? RCrmActions::fromJSON($this->dataCrm['lastName']) : '');
if (!empty($this->dataCrm['lastName'])) {
$this->customer->setLastName(RCrmActions::fromJSON($this->dataCrm['lastName']));
}
if (array_key_exists('patronymic', $this->dataCrm)) {
$this->customer->setSecondName($this->dataCrm['patronymic']
? RCrmActions::fromJSON($this->dataCrm['patronymic']) : '');
if (!empty($this->dataCrm['patronymic'])) {
$this->customer->setSecondName(RCrmActions::fromJSON($this->dataCrm['patronymic']));
}
if (isset($this->dataCrm['phones'])) {
$user = CUser::GetList(
($by = "ID"),
($order = "desc"),
array('ID' => $this->dataCrm['externalId']),
array('FIELDS' => array('PERSONAL_PHONE', 'PERSONAL_MOBILE'))
)->fetch();
foreach ($this->dataCrm['phones'] as $phone) {
if (isset($phone['old_number']) && in_array($phone['old_number'], $user)) {
$key = array_search($phone['old_number'], $user);
if (isset($phone['old_number']) && in_array($phone['old_number'], $this->user)) {
$key = array_search($phone['old_number'], $this->user);
if (isset($phone['number'])) {
$user[$key] = $phone['number'];
$this->user[$key] = $phone['number'];
} else {
$user[$key] = '';
$this->user[$key] = '';
}
}
if (isset($phone['number'])) {
if ((!isset($user['PERSONAL_PHONE']) || strlen($user['PERSONAL_PHONE']) == 0)
&& $user['PERSONAL_MOBILE'] != $phone['number']
if ((!isset($this->user['PERSONAL_PHONE']) || strlen($this->user['PERSONAL_PHONE']) == 0)
&& $this->user['PERSONAL_MOBILE'] != $phone['number']
) {
$this->customer->setPersonalPhone($phone['number']);
$user['PERSONAL_PHONE'] = $phone['number'];
$this->user['PERSONAL_PHONE'] = $phone['number'];
continue;
}
if ((!isset($user['PERSONAL_MOBILE']) || strlen($user['PERSONAL_MOBILE']) == 0)
&& $user['PERSONAL_PHONE'] != $phone['number']
if ((!isset($this->user['PERSONAL_MOBILE']) || strlen($this->user['PERSONAL_MOBILE']) == 0)
&& $this->user['PERSONAL_PHONE'] != $phone['number']
) {
$this->customer->setPersonalMobile($phone['number']);
$user['PERSONAL_MOBILE'] = $phone['number'];
$this->user['PERSONAL_MOBILE'] = $phone['number'];
continue;
}
}
}
}
if (array_key_exists('index', $this->dataCrm['address'])) {
$this->customer->setPersonalZip($this->dataCrm['address']['index']
? RCrmActions::fromJSON($this->dataCrm['address']['index']) : '');
}
if (array_key_exists('city', $this->dataCrm['address'])) {
$this->customer->setPersonalCity($this->dataCrm['address']['city']
? RCrmActions::fromJSON($this->dataCrm['address']['city']) : '');
if (!empty($this->dataCrm['index'])) {
$this->customer->setPersonalZip(RCrmActions::fromJSON($this->dataCrm['index']));
}
if (array_key_exists('birthday', $this->dataCrm)) {
$this->customer->setPersonalBirthday(date("d.m.Y", strtotime($this->dataCrm['birthday'])));
if (!empty($this->dataCrm['city'])) {
$this->customer->setPersonalCity(RCrmActions::fromJSON($this->dataCrm['city']));
}
if (array_key_exists('email', $this->dataCrm)) {
$this->customer->setEmail($this->dataCrm['email'] ? RCrmActions::fromJSON($this->dataCrm['email']) : '');
if (!empty($this->dataCrm['birthday'])) {
$this->customer->setPersonalBirthday(RCrmActions::fromJSON($this->dataCrm['birthday']));
}
if (array_key_exists('sex', $this->dataCrm)) {
$this->customer->setPersonalGender($this->dataCrm['sex'] ? RCrmActions::fromJSON($this->dataCrm['sex']) : '');
if (!empty($this->dataCrm['email'])) {
$this->customer->setEmail(RCrmActions::fromJSON($this->dataCrm['email']));
}
if (!empty($this->dataCrm['sex'])) {
$this->customer->setPersonalGender(RCrmActions::fromJSON($this->dataCrm['sex']));
}
}

View file

@ -23,6 +23,7 @@ class CustomerCorpBuilder implements RetailcrmBuilderInterface
public $buyerProfile;
protected $api;
public $dbUser;
/**
* CustomerCorpBuilder constructor.
@ -46,6 +47,16 @@ class CustomerCorpBuilder implements RetailcrmBuilderInterface
return $this;
}
/**
* @param $dbUser
* @return $this
*/
public function setDbUser($dbUser)
{
$this->dbUser = $dbUser;
return $this;
}
public function build()
{
if (RetailCrmOrder::isOrderCorporate($this->dataCrm)) {
@ -124,18 +135,12 @@ class CustomerCorpBuilder implements RetailcrmBuilderInterface
}
}
$dbUser = CUser::GetList(
($by = 'ID'),
($sort = 'ASC'),
array('=EMAIL' => $this->dataCrm['customer']['email'])
);
switch ($dbUser->SelectedRowsCount()) {
switch ($this->dbUser->SelectedRowsCount()) {
case 0:
$login = $this->dataCrm['customer']['email'];
break;
case 1:
$arUser = $dbUser->Fetch();
$arUser = $this->dbUser->Fetch();
$registeredUserID = $arUser['ID'];
$registerNewUser = false;
break;
@ -159,11 +164,11 @@ class CustomerCorpBuilder implements RetailcrmBuilderInterface
->setPassword($userPassword)
->setConfirmPassword($userPassword);
if ($userData['phones'][0]) {
if (!empty($userData['phones'][0])) {
$this->customer->setPersonalPhone($userData['phones'][0]);
}
if ($userData['phones'][1]) {
if (!empty($userData['phones'][1])) {
$this->customer->setPersonalMobile($userData['phones'][1]);
}
}
@ -181,6 +186,12 @@ class CustomerCorpBuilder implements RetailcrmBuilderInterface
public function buildAddress()
{
$this->customerAddress->setData($this->dataCrm['company']['address']);
if (isset($this->dataCrm['company']['address'])) {
$this->addressBuilder->setDataCrm($this->dataCrm['company']['address']);
$this->addressBuilder->build();
$this->customerAddress = $this->addressBuilder->customerAddress;
} else {
$this->customerAddress = null;
}
}
}

View file

@ -43,4 +43,4 @@ class BuyerProfile
return $this;
}
}
}

View file

@ -175,39 +175,4 @@ class CustomerAddress
return $this;
}
/**
* @param $array
* @param $key
* @param null $default
* @return mixed|null
*/
protected function getValue($array, $key, $default = NULL)
{
return isset($array[$key]) && !empty($array[$key]) ? $array[$key] : $default;
}
/**
* @param $arrayData
* @return $this
*/
public function setData($arrayData)
{
$this->setText($this->getValue($arrayData,'text'))
->setNotes($this->getValue($arrayData,'notes'))
->setBuilding($this->getValue($arrayData,'building'))
->setBlock($this->getValue($arrayData,'block'))
->setCity($this->getValue($arrayData,'city'))
->setFlat($this->getValue($arrayData,'flat'))
->setHouse($this->getValue($arrayData,'house'))
->setFloor($this->getValue($arrayData,'floor'))
->setCountry($this->getValue($arrayData,'countryIso'))
->setIndex($this->getValue($arrayData,'index'))
->setIntercomCode($this->getValue($arrayData,'intercomCode'))
->setMetro($this->getValue($arrayData,'metro'))
->setRegion($this->getValue($arrayData,'region'))
->setStreet($this->getValue($arrayData,'street'));
return $this;
}
}

View file

@ -34,5 +34,6 @@ CModule::AddAutoloadClasses(
'CustomerAddress' => 'classes/general/Model/CustomerAddress.php',
'CustomerContragent' => 'classes/general/Model/CustomerContragent.php',
'BuyerProfile' => 'classes/general/Model/BuyerProfile.php',
'AdressBuilder' => 'classes/general/AdressBuilder.php',
)
);