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

add model customer

This commit is contained in:
gorokh 2020-04-29 20:31:13 +03:00
parent d0252f4ef2
commit 9bbed83dc3
8 changed files with 1023 additions and 0 deletions

View file

@ -0,0 +1,253 @@
<?php
IncludeModuleLangFile(__FILE__);
/**
* Class CustomerBuilder
*/
class CustomerBuilder
{
/** @var classes/general/Model/Customer */
public $customer;
/** @var classes/general/Model/CustomerAddress */
public $customerAddress;
/** @var array $dataCrm customerHistory */
protected $dataCrm;
/**
* CustomerBuilder constructor.
*/
public function __construct()
{
$this->customer = new Customer();
$this->customerAddress = new CustomerAddress();
}
/**
* @param $customer
* @return $this
*/
public function setCustomer($customer)
{
$this->customer = $customer;
return $this;
}
/**
* @return classes|Customer
*/
public function getCustomer()
{
return $this->customer;
}
/**
* @param $customerAddress
* @return $this
*/
public function setCustomerAddress($customerAddress)
{
$this->customerAddress = $customerAddress;
return $this;
}
/**
* @return classes|CustomerAddress
*/
public function getCustomerAddress()
{
return $this->customerAddress;
}
/**
* @param $dataCrm
* @return $this
*/
public function setDataCrm($dataCrm)
{
$this->dataCrm = $dataCrm;
return $this;
}
/**
* @param $key
* @param null $default
* @return mixed|null
*/
protected function getValue($key, $default = NULL)
{
return isset($this->dataCrm[$key]) && !empty($this->dataCrm[$key]) ? $this->dataCrm[$key] : $default;
}
/**
* @param $array
* @param $key
* @param null $default
* @return mixed|null
*/
protected function getValueArray($array, $key, $default = NULL)
{
return isset($this->dataCrm[$array][$key]) && !empty($this->dataCrm[$array][$key]) ? $this->dataCrm[$array][$key] : $default;
}
public function build()
{
if (isset($this->dataCrm['deleted'])) {
return false;
}
if (isset($this->dataCrm['externalId']) && !is_numeric($this->dataCrm['externalId'])) {
unset($this->dataCrm['externalId']);
}
if (!isset($this->dataCrm['externalId'])) {
$this->createCustomer();
}
if (isset($this->dataCrm['externalId'])) {
$this->updateCustomer();
}
if (isset($this->dataCrm['address'])) {
$this->buildAddress();
}
}
public function buildAddress()
{
$this->customerAddress->setData($this->dataCrm['address']);
}
public function createCustomer()
{
if (!isset($this->dataCrm['id'])) {
return false;
}
$registerNewUser = true;
if (!isset($this->dataCrm['email']) || $this->dataCrm['email'] == '') {
$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()) {
case 0:
$login = $this->dataCrm['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");
$this->customer->setEmail($this->dataCrm['email'])
->setLogin($login)
->setActive("Y")
->setPassword($userPassword)
->setConfirmPassword($userPassword);
}
}
public function updateCustomer()
{
if (array_key_exists('firstName', $this->dataCrm)) {
$this->customer->setName($this->dataCrm['firstName']
? RCrmActions::fromJSON($this->dataCrm['firstName']) : '');
}
if (array_key_exists('lastName', $this->dataCrm)) {
$this->customer->setLastName($this->dataCrm['lastName']
? RCrmActions::fromJSON($this->dataCrm['lastName']) : '');
}
if (array_key_exists('patronymic', $this->dataCrm)) {
$this->customer->setSecondName($this->dataCrm['patronymic']
? 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['number'])) {
$user[$key] = $phone['number'];
} else {
$user[$key] = '';
}
}
if (isset($phone['number'])) {
if ((!isset($user['PERSONAL_PHONE']) || strlen($user['PERSONAL_PHONE']) == 0)
&& $user['PERSONAL_MOBILE'] != $phone['number']
) {
$this->customer->setPersonalPhone($phone['number']);
$user['PERSONAL_PHONE'] = $phone['number'];
continue;
}
if ((!isset($user['PERSONAL_MOBILE']) || strlen($user['PERSONAL_MOBILE']) == 0)
&& $user['PERSONAL_PHONE'] != $phone['number']
) {
$this->customer->setPersonalMobile($phone['number']);
$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 (array_key_exists('birthday', $this->dataCrm)) {
$this->customer->setPersonalBirthday(date("d.m.Y", strtotime($this->dataCrm['birthday'])));
}
if (array_key_exists('email', $this->dataCrm)) {
$this->customer->setEmail($this->dataCrm['email'] ? RCrmActions::fromJSON($this->dataCrm['email']) : '');
}
if (array_key_exists('sex', $this->dataCrm)) {
$this->customer->setPersonalGender($this->dataCrm['sex'] ? RCrmActions::fromJSON($this->dataCrm['sex']) : '');
}
}
/**
* @param array $array
* @param array $symbols
* @return array
*/
function arrayClear(array $array, array $symbols = array('', 0, null))
{
return array_diff($array, $symbols);
}
/**
* @param $data
* @return array
*/
function objectToArray($data)
{
return $this->arrayClear(json_decode(json_encode($data), true));
}
}

View file

@ -0,0 +1,186 @@
<?php
IncludeModuleLangFile(__FILE__);
/**
* Class CustomerCorpBuilder
*/
class CustomerCorpBuilder implements RetailcrmBuilderInterface
{
/** @var classes/general/Model/Customer */
public $customer;
/** @var classes/general/Model/CustomerAddress */
public $customerAddress;
/** @var array $dataCrm customerHistory */
protected $dataCrm;
public $corporateContact;
public $orderCustomerExtId;
/** @var classes/general/Model/BuyerProfile */
public $buyerProfile;
protected $api;
/**
* CustomerCorpBuilder constructor.
* @param $api
*/
public function __construct($api)
{
$this->api = $api;
$this->customer = new Customer();
$this->customerAddress = new CustomerAddress();
$this->buyerProfile = new BuyerProfile();
}
/**
* @param $dataCrm
* @return $this
*/
public function setDataCrm($dataCrm)
{
$this->dataCrm = $dataCrm;
return $this;
}
public function build()
{
if (RetailCrmOrder::isOrderCorporate($this->dataCrm)) {
$this->getCorporateContact();
}
if (!isset($this->dataCrm['externalId'])) {
$this->buildCustomer();
$this->buildBuyerProfile();
}
if (isset($this->dataCrm['company']['address'])) {
$this->buildAddress();
}
}
public function getCorporateContact()
{
if (!empty($this->dataCrm['contact'])) {
if (isset($this->dataCrm['contact']['email'])) {
$this->corporateContact = $this->dataCrm['contact'];
$this->orderCustomerExtId = isset($this->corporateContact['externalId'])
? $this->corporateContact['externalId']
: null;
} else {
$response = false;
if (isset($this->dataCrm['contact']['externalId'])) {
$response = RCrmActions::apiMethod(
$this->api,
'customersGet',
__METHOD__,
$this->dataCrm['contact']['externalId'],
$this->dataCrm['site']
);
} elseif (isset($this->dataCrm['contact']['id'])) {
$response = RCrmActions::apiMethod(
$this->api,
'customersGetById',
__METHOD__,
$this->dataCrm['contact']['id'],
$this->dataCrm['site']
);
}
if ($response && isset($response['customer'])) {
$this->corporateContact = $response['customer'];
$this->orderCustomerExtId = isset($this->corporateContact['externalId'])
? $this->corporateContact['externalId']
: null;
}
}
}
}
public function buildCustomer()
{
if (empty($this->orderCustomerExtId)) {
if (!isset($this->dataCrm['customer']['id'])
|| (RetailCrmOrder::isOrderCorporate($this->dataCrm)
&& (!isset($this->dataCrm['contact']['id']) || !isset($this->dataCrm['customer']['id'])))
) {
return false;
}
$login = null;
$registerNewUser = true;
if (!isset($this->dataCrm['customer']['email']) || empty($this->dataCrm['customer']['email'])) {
if (RetailCrmOrder::isOrderCorporate($this->dataCrm) && !empty($this->corporateContact['email'])) {
$login = $this->corporateContact['email'];
$this->dataCrm['customer']['email'] = $this->corporateContact['email'];
} else {
$login = uniqid('user_' . time()) . '@crm.com';
$this->dataCrm['customer']['email'] = $login;
}
}
$dbUser = CUser::GetList(
($by = 'ID'),
($sort = 'ASC'),
array('=EMAIL' => $this->dataCrm['customer']['email'])
);
switch ($dbUser->SelectedRowsCount()) {
case 0:
$login = $this->dataCrm['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");
$userData = RetailCrmOrder::isOrderCorporate($this->dataCrm)
? $this->corporateContact
: $this->dataCrm['customer'];
$this->customer->setName(RCrmActions::fromJSON($userData['firstName']))
->setLastName(RCrmActions::fromJSON($userData['lastName']))
->setSecondName(RCrmActions::fromJSON($userData['patronymic']))
->setEmail($this->dataCrm['customer']['email'])
->setLogin($login)
->setActive("Y")
->setPassword($userPassword)
->setConfirmPassword($userPassword);
if ($userData['phones'][0]) {
$this->customer->setPersonalPhone($userData['phones'][0]);
}
if ($userData['phones'][1]) {
$this->customer->setPersonalMobile($userData['phones'][1]);
}
}
}
}
public function buildBuyerProfile()
{
if (RetailCrmOrder::isOrderCorporate($this->dataCrm) && !empty($this->dataCrm['company'])) {
$this->buyerProfile->setName($this->dataCrm['company']['name'])
->setPersonTypeId($this->dataCrm['contact']['externalId'])
->setUserId($this->contragentTypes['legal-entity']);
}
}
public function buildAddress()
{
$this->customerAddress->setData($this->dataCrm['company']['address']);
}
}

View file

@ -0,0 +1,46 @@
<?php
IncludeModuleLangFile(__FILE__);
/**
* Class BuyerProfile
*/
class BuyerProfile
{
public $NAME;
public $USER_ID;
public $PERSON_TYPE_ID;
/**
* @param $NAME
* @return $this
*/
public function setName($NAME)
{
$this->NAME = $NAME;
return $this;
}
/**
* @param $USER_ID
* @return $this
*/
public function setUserId($USER_ID)
{
$this->USER_ID = $USER_ID;
return $this;
}
/**
* @param $PERSON_TYPE_ID
* @return $this
*/
public function setPersonTypeId($PERSON_TYPE_ID)
{
$this->PERSON_TYPE_ID = $PERSON_TYPE_ID;
return $this;
}
}

View file

@ -0,0 +1,178 @@
<?php
IncludeModuleLangFile(__FILE__);
/**
* Class Customer
*/
class Customer
{
public $EMAIL;
public $LOGIN;
public $ACTIVE;
public $PASSWORD;
public $CONFIRM_PASSWORD;
public $NAME;
public $LAST_NAME;
public $SECOND_NAME;
public $PERSONAL_MOBILE;
public $PERSONAL_PHONE;
public $PERSONAL_ZIP;
public $PERSONAL_CITY;
public $PERSONAL_BIRTHDAY;
public $PERSONAL_GENDER;
/**
* @param $EMAIL
* @return $this
*/
public function setEmail($EMAIL)
{
$this->EMAIL = $EMAIL;
return $this;
}
/**
* @param $LOGIN
* @return $this
*/
public function setLogin($LOGIN)
{
$this->LOGIN = $LOGIN;
return $this;
}
/**
* @param $ACTIVE
* @return $this
*/
public function setActive($ACTIVE)
{
$this->ACTIVE = $ACTIVE;
return $this;
}
/**
* @param $PASSWORD
* @return $this
*/
public function setPassword($PASSWORD)
{
$this->PASSWORD = $PASSWORD;
return $this;
}
/**
* @param $CONFIRM_PASSWORD
* @return $this
*/
public function setConfirmPassword($CONFIRM_PASSWORD)
{
$this->CONFIRM_PASSWORD = $CONFIRM_PASSWORD;
return $this;
}
/**
* @param $NAME
* @return $this
*/
public function setName($NAME)
{
$this->NAME = $NAME;
return $this;
}
/**
* @param $LAST_NAME
* @return $this
*/
public function setLastName($LAST_NAME)
{
$this->LAST_NAME = $LAST_NAME;
return $this;
}
/**
* @param $SECOND_NAME
* @return $this
*/
public function setSecondName($SECOND_NAME)
{
$this->SECOND_NAME = $SECOND_NAME;
return $this;
}
/**
* @param $PERSONAL_MOBILE
* @return $this
*/
public function setPersonalMobile($PERSONAL_MOBILE)
{
$this->PERSONAL_MOBILE = $PERSONAL_MOBILE;
return $this;
}
/**
* @param $PERSONAL_PHONE
* @return $this
*/
public function setPersonalPhone($PERSONAL_PHONE)
{
$this->PERSONAL_PHONE = $PERSONAL_PHONE;
return $this;
}
/**
* @param $PERSONAL_ZIP
* @return $this
*/
public function setPersonalZip($PERSONAL_ZIP)
{
$this->PERSONAL_ZIP = $PERSONAL_ZIP;
return $this;
}
/**
* @param $PERSONAL_CITY
* @return $this
*/
public function setPersonalCity($PERSONAL_CITY)
{
$this->PERSONAL_CITY = $PERSONAL_CITY;
return $this;
}
/**
* @param $PERSONAL_BIRTHDAY
* @return $this
*/
public function setPersonalBirthday($PERSONAL_BIRTHDAY)
{
$this->PERSONAL_BIRTHDAY = $PERSONAL_BIRTHDAY;
return $this;
}
/**
* @param $PERSONAL_GENDER
* @return $this
*/
public function setPersonalGender($PERSONAL_GENDER)
{
$this->PERSONAL_GENDER = $PERSONAL_GENDER;
return $this;
}
}

View file

@ -0,0 +1,213 @@
<?php
IncludeModuleLangFile(__FILE__);
/**
* Class CustomerAddress
*/
class CustomerAddress
{
public $index;
public $country;
public $region;
public $city;
public $street;
public $building;
public $house;
public $block;
public $flat;
public $floor;
public $intercom_code;
public $metro;
public $notes;
public $text;
/**
* @param $index
* @return $this
*/
public function setIndex($index)
{
$this->index = $index;
return $this;
}
/**
* @param $country
* @return $this
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* @param $region
* @return $this
*/
public function setRegion($region)
{
$this->region = $region;
return $this;
}
/**
* @param $city
* @return $this
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* @param $street
* @return $this
*/
public function setStreet($street)
{
$this->street = $street;
return $this;
}
/**
* @param $building
* @return $this
*/
public function setBuilding($building)
{
$this->building = $building;
return $this;
}
/**
* @param $house
* @return $this
*/
public function setHouse($house)
{
$this->house = $house;
return $this;
}
/**
* @param $block
* @return $this
*/
public function setBlock($block)
{
$this->block = $block;
return $this;
}
/**
* @param $flat
* @return $this
*/
public function setFlat($flat)
{
$this->flat = $flat;
return $this;
}
/**
* @param $floor
* @return $this
*/
public function setFloor($floor)
{
$this->floor = $floor;
return $this;
}
/**
* @param $intercom_code
* @return $this
*/
public function setIntercomCode($intercom_code)
{
$this->intercom_code = $intercom_code;
return $this;
}
/**
* @param $metro
* @return $this
*/
public function setMetro($metro)
{
$this->metro = $metro;
return $this;
}
/**
* @param $notes
* @return $this
*/
public function setNotes($notes)
{
$this->notes = $notes;
return $this;
}
/**
* @param $text
* @return $this
*/
public function setText($text)
{
$this->text = $text;
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

@ -0,0 +1,118 @@
<?php
IncludeModuleLangFile(__FILE__);
/**
* Class CustomerContragent
*/
class CustomerContragent
{
public $contragentType;
public $legalName;
public $legalAddress;
public $certificateNumber;
public $certificateDate;
public $bank;
public $bankAddress;
public $corrAccount;
public $bankAccount;
/**
* @param $contragentType
* @return $this
*/
public function setContragentType($contragentType)
{
$this->contragentType = $contragentType;
return $this;
}
/**
* @param $legalName
* @return $this
*/
public function setLegalName($legalName)
{
$this->legalName = $legalName;
return $this;
}
/**
* @param $legalAddress
* @return $this
*/
public function setLegalAddress($legalAddress)
{
$this->legalAddress = $legalAddress;
return $this;
}
/**
* @param $certificateNumber
* @return $this
*/
public function setCertificateNumber($certificateNumber)
{
$this->certificateNumber = $certificateNumber;
return $this;
}
/**
* @param $certificateDate
* @return $this
*/
public function setCertificateDate($certificateDate)
{
$this->certificateDate = $certificateDate;
return $this;
}
/**
* @param $bank
* @return $this
*/
public function setBank($bank)
{
$this->bank = $bank;
return $this;
}
/**
* @param $bankAddress
* @return $this
*/
public function setBankAddress($bankAddress)
{
$this->bankAddress = $bankAddress;
return $this;
}
/**
* @param $corrAccount
* @return $this
*/
public function setCorrAccount($corrAccount)
{
$this->corrAccount = $corrAccount;
return $this;
}
/**
* @param $bankAccount
* @return $this
*/
public function setBankAccount($bankAccount)
{
$this->bankAccount = $bankAccount;
return $this;
}
}

View file

@ -0,0 +1,21 @@
<?php
IncludeModuleLangFile(__FILE__);
interface RetailcrmBuilderInterface
{
/**
* Set data array customerHistory
*
* @param array $dataCrm
*
* @return RetailcrmBuilderInterface
*/
public function setDataCrm($dataCrm);
/**
* Build result
*/
public function build();
}

View file

@ -26,5 +26,13 @@ CModule::AddAutoloadClasses(
'RetailCrmCorporateClient' => file_exists($server . '/bitrix/php_interface/retailcrm/RetailCrmCorporateClient.php') ? '../../php_interface/retailcrm/RetailCrmCorporateClient.php' : 'classes/general/user/RetailCrmCorporateClient.php',
'RetailcrmConfigProvider' => 'classes/general/RetailcrmConfigProvider.php',
'RetailcrmConstants' => 'classes/general/RetailcrmConstants.php',
'RetailcrmBuilderInterface' => 'classes/general/RetailcrmBuilderInterface.php',
'CustomerBuilder' => 'classes/general/CustomerBuilder.php',
'CustomerCorpBuilder' => 'classes/general/CustomerCorpBuilder.php',
'Customer' => 'classes/general/Model/Customer.php',
'CustomerAddress' => 'classes/general/Model/CustomerAddress.php',
'CustomerContragent' => 'classes/general/Model/CustomerContragent.php',
'BuyerProfile' => 'classes/general/Model/BuyerProfile.php',
)
);