1
0
mirror of synced 2024-11-25 14:56:09 +03:00

Service for manager sync

* added service for manager sync
* added manager event handler in history uploading methods
* deleted logger dependence from repository
* extract config provider proxy methods from repository to service
This commit is contained in:
Сергей Чазов 2021-08-19 15:46:03 +03:00 committed by GitHub
parent c731fae369
commit 8458360a30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 641 additions and 481 deletions

View File

@ -60,27 +60,23 @@ class ApiClient
* Returns users list
*
* @param array $filter
* @param null $page
* @param null $limit
*
* @throws \RetailCrm\Exception\InvalidJsonException
* @throws \RetailCrm\Exception\CurlException
* @throws \InvalidArgumentException
* @param int|null $page
* @param int|null $limit
*
* @return ApiResponse
*/
public function usersList(array $filter = array(), $page = null, $limit = null)
public function usersList(array $filter = [], int $page = null, int $limit = null): ApiResponse
{
$parameters = array();
$parameters = [];
if (count($filter)) {
$parameters['filter'] = $filter;
}
if (null !== $page) {
$parameters['page'] = (int) $page;
$parameters['page'] = $page;
}
if (null !== $limit) {
$parameters['limit'] = (int) $limit;
$parameters['limit'] = $limit;
}
return $this->client->makeRequest(

View File

@ -1,4 +1,7 @@
<?php
use Intaro\RetailCrm\Service\ManagerService;
IncludeModuleLangFile(__FILE__);
class RCrmActions
{
@ -186,11 +189,11 @@ class RCrmActions
*
* @return self name
*/
public static function uploadOrdersAgent()
{
RetailCrmOrder::uploadOrders();
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if (is_array($failedIds) && !empty($failedIds)) {
RetailCrmOrder::uploadOrders(50, true);
}
@ -206,10 +209,13 @@ class RCrmActions
*/
public static function orderAgent()
{
if (COption::GetOptionString('main', 'agents_use_crontab', 'N') != 'N') {
if (COption::GetOptionString('main', 'agents_use_crontab', 'N') !== 'N') {
define('NO_AGENT_CHECK', true);
}
$service = ManagerService::getInstance();
$service->synchronizeManagers();
RetailCrmHistory::customerHistory();
RetailCrmHistory::orderHistory();
self::uploadOrdersAgent();
@ -354,10 +360,16 @@ class RCrmActions
return $string;
}
public static function explodeFIO($fio)
/**
* @param string|null $fio
*
* @return array
*/
public static function explodeFio(?string $fio): array
{
$result = array();
$result = [];
$fio = preg_replace('|[\s]+|s', ' ', trim($fio));
if (empty($fio)) {
return $result;
} else {
@ -373,17 +385,17 @@ class RCrmActions
$result['firstName'] = $newFio[0];
break;
case 2:
$result = array(
'lastName' => $newFio[0],
'firstName' => $newFio[1]
);
break;
case 3:
$result = array(
$result = [
'lastName' => $newFio[0],
'firstName' => $newFio[1],
'patronymic' => $newFio[2]
);
];
break;
case 3:
$result = [
'lastName' => $newFio[0],
'firstName' => $newFio[1],
'patronymic' => $newFio[2],
];
break;
}

View File

@ -11,6 +11,7 @@
namespace RetailCrm\Response;
use InvalidArgumentException;
use RetailCrm\Exception\InvalidJsonException;
/**
@ -166,7 +167,7 @@ class ApiResponse implements \ArrayAccess
public function offsetGet($offset)
{
if (!isset($this->response[$offset])) {
throw new \InvalidArgumentException("Property \"$offset\" not found");
throw new InvalidArgumentException("Property \"$offset\" not found");
}
return $this->response[$offset];

View File

@ -243,13 +243,31 @@ class RetailcrmConfigProvider
*/
public static function getSiteName(): string
{
$sitename = COption::GetOptionString('main', 'site_name');
$siteName = COption::GetOptionString('main', 'site_name');
if (!$sitename) {
if (!$siteName) {
return '';
}
return $sitename;
return $siteName;
}
/**
* @return mixed
*/
public static function getUsersMap()
{
return static::getUnserializedOption(RetailcrmConstants::CRM_USERS_MAP);
}
/**
* @param array|null $userMap
*
* @return bool
*/
public static function setUsersMap(?array $userMap): bool
{
return static::setOption(RetailcrmConstants::CRM_USERS_MAP, serialize($userMap));
}
/**
@ -772,9 +790,9 @@ class RetailcrmConfigProvider
* @param bool $desc
* @param string $site
*/
private static function setOption($name, $value = "", $desc = false, $site = "")
private static function setOption($name, string $value = '', bool $desc = false, string $site = ''): bool
{
COption::SetOptionString(
return COption::SetOptionString(
RetailcrmConstants::MODULE_ID,
$name,
$value,

View File

@ -76,4 +76,6 @@ class RetailcrmConstants
public const CRM_ONLINE_CONSULTANT = 'online_consultant';
public const CRM_ONLINE_CONSULTANT_SCRIPT = 'online_consultant_script';
public const CRM_PURCHASE_PRICE_NULL = 'purchasePrice_null';
public const CRM_USERS_MAP = 'crm_users_map';
public const BITRIX_USER_ID_PREFIX = 'bitrixUserId-';
}

View File

@ -1,5 +1,7 @@
<?php
use Intaro\RetailCrm\Service\ManagerService;
/**
* Class RetailCrmEvent
*/
@ -140,7 +142,6 @@ class RetailCrmEvent
return false;
}
$arOrder = RetailCrmOrder::orderObjToArr($obOrder);
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
@ -413,6 +414,11 @@ class RetailCrmEvent
}
}
if (isset($arOrder['RESPONSIBLE_ID']) && !empty($arOrder['RESPONSIBLE_ID'])) {
$managerService = ManagerService::getInstance();
$arParams['managerId'] = $managerService->getManagerCrmId($arOrder['RESPONSIBLE_ID']);
}
//order
$resultOrder = RetailCrmOrder::orderSend($arOrder, $api, $arParams, true, $site, $methodApi);

View File

@ -521,7 +521,7 @@ class RetailCrmHistory
}
}
$fio = RCrmActions::explodeFIO($fio);
$fio = RCrmActions::explodeFio($fio);
$newFio = array();
if ($fio) {
$newFio[] = isset($order['lastName']) ? RCrmActions::fromJSON($order['lastName']) : (isset($fio['lastName']) ? $fio['lastName'] : '');

View File

@ -1,4 +1,18 @@
<?php
use Bitrix\Main\ArgumentException;
use Bitrix\Main\ArgumentNullException;
use Bitrix\Main\Context;
use Bitrix\Sale\Basket;
use Bitrix\Sale\Delivery\Services\EmptyDeliveryService;
use Bitrix\Sale\Delivery\Services\Manager;
use Bitrix\Sale\Fuser;
use Bitrix\Sale\Internals\PaymentTable;
use Bitrix\Sale\Location\Search\Finder;
use Bitrix\Sale\Order;
use Bitrix\Sale\OrderUserProperties;
use Intaro\RetailCrm\Service\ManagerService;
IncludeModuleLangFile(__FILE__);
class RetailCrmHistory
{
@ -192,11 +206,24 @@ class RetailCrmHistory
}
}
public static function orderHistory()
/**
* @return bool
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ArgumentNullException
* @throws \Bitrix\Main\ArgumentOutOfRangeException
* @throws \Bitrix\Main\ArgumentTypeException
* @throws \Bitrix\Main\NotImplementedException
* @throws \Bitrix\Main\NotSupportedException
* @throws \Bitrix\Main\ObjectException
* @throws \Bitrix\Main\ObjectNotFoundException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public static function orderHistory(): bool
{
global $USER;
if (is_object($USER) == false) {
if (is_object($USER) === false) {
$USER = new RetailUser();
}
@ -227,11 +254,11 @@ class RetailCrmHistory
while (true) {
$orderHistory = RCrmActions::apiMethod($api, 'ordersHistory', __METHOD__, $historyFilter);
$orderH = isset($orderHistory['history']) ? $orderHistory['history'] : array();
$orderH = $orderHistory['history'] ?? [];
Logger::getInstance()->write($orderH, 'orderHistory');
if (count($orderH) == 0) {
if (count($orderH) === 0) {
if ($orderHistory['history']['totalPageCount'] > $orderHistory['history']['currentPage']) {
$historyFilter['page'] = $orderHistory['history']['currentPage'] + 1;
@ -265,30 +292,7 @@ class RetailCrmHistory
if (isset($order['deleted'])) {
if (isset($order['externalId'])) {
try {
$newOrder = Bitrix\Sale\Order::load($order['externalId']);
} catch (Bitrix\Main\ArgumentNullException $e) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::load',
$e->getMessage() . ': ' . $order['externalId']
);
continue;
}
if (!$newOrder instanceof \Bitrix\Sale\Order) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::load',
'Error order load: ' . $order['externalId']
);
continue;
}
$newOrder->setField('CANCELED', 'Y');
$newOrder->save();
self::cancelOrder($order['externalId']);
}
continue;
@ -317,7 +321,7 @@ class RetailCrmHistory
$corporateCustomerBuilder = new CorporateCustomerBuilder();
$corporateContact = array();
$orderCustomerExtId = isset($order['customer']['externalId']) ? $order['customer']['externalId'] : null;
$orderCustomerExtId = $order['customer']['externalId'] ?? null;
$corporateCustomerBuilder->setOrderCustomerExtId($orderCustomerExtId)
->setContragentTypes($contragentTypes)
->setDataCrm($order)
@ -329,9 +333,7 @@ class RetailCrmHistory
if (isset($order['contact']['email'])) {
$corporateContact = $order['contact'];
$orderCustomerExtId = isset($corporateContact['externalId'])
? $corporateContact['externalId']
: null;
$orderCustomerExtId = $corporateContact['externalId'] ?? null;
$corporateCustomerBuilder->setCorporateContact($corporateContact)
->setOrderCustomerExtId($orderCustomerExtId);
@ -359,9 +361,7 @@ class RetailCrmHistory
if ($response && isset($response['customer'])) {
$corporateContact = $response['customer'];
$orderCustomerExtId = isset($corporateContact['externalId'])
? $corporateContact['externalId']
: null;
$orderCustomerExtId = $corporateContact['externalId'] ?? null;
$corporateCustomerBuilder->setCorporateContact($corporateContact)
->setOrderCustomerExtId($orderCustomerExtId);
}
@ -467,7 +467,7 @@ class RetailCrmHistory
if (RetailCrmOrder::isOrderCorporate($order) && !empty($order['company'])) {
$buyerProfile = $corporateCustomerBuilder->getBuyerProfile()->getObjectToArray();
$buyerProfileToAppend = Bitrix\Sale\OrderUserProperties::getList(array(
$buyerProfileToAppend = OrderUserProperties::getList(array(
"filter" => $buyerProfile
))->fetch();
@ -475,23 +475,29 @@ class RetailCrmHistory
$buyerProfileInstance = new CSaleOrderUserProps();
if ($buyerProfileInstance->Add($buyerProfile)) {
$buyerProfileToAppend = Bitrix\Sale\OrderUserProperties::getList(array(
$buyerProfileToAppend = OrderUserProperties::getList(array(
"filter" => $buyerProfile
))->fetch();
}
}
}
$newOrder = Bitrix\Sale\Order::create($site, $orderCustomerExtId, $currency);
$newOrder = Order::create($site, $orderCustomerExtId, $currency);
if (isset($buyerProfileToAppend['ID']) && isset($optionsLegalDetails['legalName'])) {
$newOrder->setFields(array(
$optionsLegalDetails['legalName'] => $buyerProfileToAppend['NAME'],
'PERSON_TYPE_ID' => $buyerProfileToAppend['PERSON_TYPE_ID']
));
if (array_key_exists('managerId', $order)) {
$service = ManagerService::getInstance();
$newOrder->setField('RESPONSIBLE_ID', $service->getManagerBitrixId($order['managerId']));
}
if (!is_object($newOrder) || !$newOrder instanceof \Bitrix\Sale\Order) {
if (isset($buyerProfileToAppend['ID']) && isset($optionsLegalDetails['legalName'])) {
$newOrder->setFields([
$optionsLegalDetails['legalName'] => $buyerProfileToAppend['NAME'],
'PERSON_TYPE_ID' => $buyerProfileToAppend['PERSON_TYPE_ID'],
]);
}
if (!is_object($newOrder) || !$newOrder instanceof Order) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::create',
@ -510,8 +516,8 @@ class RetailCrmHistory
if ($order['externalId'] && is_numeric($order['externalId'])) {
try {
$newOrder = Bitrix\Sale\Order::load($order['externalId']);
} catch (Bitrix\Main\ArgumentNullException $e) {
$newOrder = Order::load($order['externalId']);
} catch (ArgumentNullException $e) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::load',
@ -522,7 +528,7 @@ class RetailCrmHistory
}
}
if (!isset($newOrder) || $newOrder === null) {
if (!isset($newOrder)) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::load',
@ -532,6 +538,12 @@ class RetailCrmHistory
continue;
}
if (array_key_exists('managerId', $order)) {
$service = ManagerService::getInstance();
$newOrder->setField('RESPONSIBLE_ID', $service->getManagerBitrixId($order['managerId']));
}
if ($optionsSitesList) {
$site = array_search($order['site'], $optionsSitesList);
} else {
@ -661,35 +673,37 @@ class RetailCrmHistory
// fio
if ($order['firstName'] || $order['lastName'] || $order['patronymic']) {
$fio = '';
foreach ($propertyCollectionArr['properties'] as $prop) {
if (in_array($optionsOrderProps[$personType]['fio'], $prop)) {
$getFio = $newOrder->getPropertyCollection()->getItemByOrderPropertyId($prop['ID']);
if (method_exists($getFio, 'getValue')) {
$fio = $getFio->getValue();
}
}
}
$fio = RCrmActions::explodeFIO($fio);
$fio = RCrmActions::explodeFio($fio);
$newFio = array();
if ($fio) {
$newFio[] = isset($order['lastName'])
? RCrmActions::fromJSON($order['lastName'])
: (isset($fio['lastName']) ? $fio['lastName'] : '');
: ($fio['lastName'] ?? '');
$newFio[] = isset($order['firstName'])
? RCrmActions::fromJSON($order['firstName'])
: (isset($fio['firstName']) ? $fio['firstName'] : '');
: ($fio['firstName'] ?? '');
$newFio[] = isset($order['patronymic'])
? RCrmActions::fromJSON($order['patronymic'])
: (isset($fio['patronymic']) ? $fio['patronymic'] : '');
: ($fio['patronymic'] ?? '');
$order['fio'] = trim(implode(' ', $newFio));
} else {
$newFio[] = isset($order['lastName']) ? RCrmActions::fromJSON($order['lastName']) : '';
$newFio[] = isset($order['firstName']) ? RCrmActions::fromJSON($order['firstName']) : '';
$newFio[] = isset($order['patronymic']) ? RCrmActions::fromJSON($order['patronymic']) : '';
$order['fio'] = trim(implode(' ', $newFio));
}
$order['fio'] = trim(implode(' ', $newFio));
}
if (array_key_exists('fio', $order)) {
@ -740,9 +754,9 @@ class RetailCrmHistory
try {
if (!isset($location)) {
$location = \Bitrix\Sale\Location\Search\Finder::find(
$location = Finder::find(
$parameters,
array('USE_INDEX' => false, 'USE_ORM' => false)
['USE_INDEX' => false, 'USE_ORM' => false]
)->fetch();
}
@ -750,7 +764,7 @@ class RetailCrmHistory
->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
self::setProp($somePropValue, $location['CODE']);
} catch (\Bitrix\Main\ArgumentException $argumentException) {
} catch (ArgumentException $argumentException) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'RetailCrmHistory::setProp',
@ -818,9 +832,7 @@ class RetailCrmHistory
$somePropValue,
RCrmActions::fromJSON(
$key == 'legalAddress'
? (isset($order['company']['address']['text'])
? $order['company']['address']['text']
: '')
? ($order['company']['address']['text'] ?? '')
: $order['company'][$key]
)
);
@ -850,14 +862,14 @@ class RetailCrmHistory
$basket = $newOrder->getBasket();
if (!$basket) {
$basket = Bitrix\Sale\Basket::create($site);
$basket = Basket::create($site);
$newOrder->setBasket($basket);
}
$fUserId = $basket->getFUserId(true);
if (!$fUserId) {
$fUserId = Bitrix\Sale\Fuser::getIdByUserId($order['customer']['externalId']);
$fUserId = Fuser::getIdByUserId($order['customer']['externalId']);
$basket->setFUserId($fUserId);
}
@ -1010,7 +1022,6 @@ class RetailCrmHistory
if (!$item) {
if ($product['delete']) {
continue;
}
@ -1124,7 +1135,7 @@ class RetailCrmHistory
}
}
if ($itemUpdate === true && $newOrder->getField('CANCELED') != 'Y') {
if ($itemUpdate === true && $newOrder->getField('CANCELED') !== 'Y') {
self::shipmentItemReset($newOrder);
}
@ -1161,7 +1172,7 @@ class RetailCrmHistory
$newOrder->setField('PRICE', $orderSumm);
self::orderSave($newOrder);
if ($optionsOrderNumbers == 'Y' && isset($order['number'])) {
if ($optionsOrderNumbers === 'Y' && isset($order['number'])) {
$newOrder->setField('ACCOUNT_NUMBER', $order['number']);
self::orderSave($newOrder);
}
@ -1191,7 +1202,7 @@ class RetailCrmHistory
);
if ($paymentId) {
\Bitrix\Sale\Internals\PaymentTable::update($paymentId, array('XML_ID' => ''));
PaymentTable::update($paymentId, array('XML_ID' => ''));
}
}
}
@ -1257,7 +1268,7 @@ class RetailCrmHistory
public static function assemblyCustomer($customerHistory)
{
$customerHistory = self::filterHistory($customerHistory, 'customer');
$server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot();
$server = Context::getCurrent()->getServer()->getDocumentRoot();
$fields = array();
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) {
$objects = simplexml_load_file($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml');
@ -1326,7 +1337,7 @@ class RetailCrmHistory
public static function assemblyOrder($orderHistory)
{
$orderHistory = self::filterHistory($orderHistory, 'order');
$server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot();
$server = Context::getCurrent()->getServer()->getDocumentRoot();
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) {
$objects = simplexml_load_file(
@ -1342,8 +1353,10 @@ class RetailCrmHistory
foreach ($orderHistory as $change) {
$change['order'] = self::removeEmpty($change['order']);
if ($change['order']['items']) {
$items = array();
$items = [];
foreach ($change['order']['items'] as $item) {
if (isset($change['created'])) {
$item['create'] = 1;
@ -1357,6 +1370,10 @@ class RetailCrmHistory
$orders[$change['order']['id']]['number'] = $change['newValue'];
}
if ($change['field'] == 'manager') {
$orders[$change['order']['id']]['managerId'] = $change['newValue'];
}
if (isset($change['oldValue']) && $change['field'] == 'customer') {
$orders[$change['order']['id']]['customer'] = $change['newValue'];
}
@ -1562,8 +1579,8 @@ class RetailCrmHistory
$update = false;
}
$crmCode = isset($orderCrm['delivery']['code']) ? $orderCrm['delivery']['code'] : false;
$noDeliveryId = \Bitrix\Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId();
$crmCode = $orderCrm['delivery']['code'] ?? false;
$noDeliveryId = EmptyDeliveryService::getEmptyDeliveryServiceId();
if ($crmCode === false || !isset($optionsDelivTypes[$crmCode])) {
$deliveryId = $noDeliveryId;
@ -1571,10 +1588,10 @@ class RetailCrmHistory
$deliveryId = $optionsDelivTypes[$crmCode];
if (isset($orderCrm['delivery']['service']['code'])) {
$deliveryCode = \Bitrix\Sale\Delivery\Services\Manager::getCodeById($deliveryId);
$deliveryCode = Manager::getCodeById($deliveryId);
$serviceCode = $orderCrm['delivery']['service']['code'];
$service = \Bitrix\Sale\Delivery\Services\Manager::getService($deliveryId);
$service = Manager::getService($deliveryId);
if (is_object($service)) {
$services = $service->getProfilesList();
if (!array_key_exists($serviceCode, $services)) {
@ -1585,7 +1602,7 @@ class RetailCrmHistory
if ($deliveryCode) {
try {
$deliveryService = \Bitrix\Sale\Delivery\Services\Manager::getObjectByCode($deliveryCode . ':' . $serviceCode);
$deliveryService = Manager::getObjectByCode($deliveryCode . ':' . $serviceCode);
} catch (Bitrix\Main\SystemException $systemException) {
RCrmActions::eventLog('RetailCrmHistory::deliveryEdit', '\Bitrix\Sale\Delivery\Services\Manager::getObjectByCode', $systemException->getMessage());
}
@ -1597,7 +1614,7 @@ class RetailCrmHistory
}
}
$delivery = \Bitrix\Sale\Delivery\Services\Manager::getObjectById($deliveryId);
$delivery = Manager::getObjectById($deliveryId);
$shipmentColl = $order->getShipmentCollection();
if ($delivery) {
@ -1910,39 +1927,45 @@ class RetailCrmHistory
return $val;
}
}
class RetailUser extends CUser
/**
* @param int $externalId
*
* @return void
*/
public static function cancelOrder(int $externalId)
{
public function GetID()
{
$rsUser = CUser::GetList(($by = 'ID'), ($order = 'DESC'), array('LOGIN' => 'retailcrm'));
if ($arUser = $rsUser->Fetch()) {
return $arUser['ID'];
} else {
$retailUser = new CUser;
$userPassword = uniqid();
$arFields = array(
"NAME" => 'retailcrm',
"LAST_NAME" => 'retailcrm',
"EMAIL" => 'retailcrm@retailcrm.com',
"LOGIN" => 'retailcrm',
"LID" => "ru",
"ACTIVE" => "Y",
"GROUP_ID" => array(2),
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
try {
$newOrder = Order::load($externalId);
} catch (ArgumentNullException $e) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::load',
$e->getMessage() . ': ' . $externalId
);
$id = $retailUser->Add($arFields);
if (!$id) {
return null;
} else {
return $id;
}
return;
}
if (!$newOrder instanceof Order) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::load',
'Error order load: ' . $externalId
);
return;
}
try {
$newOrder->setField('CANCELED', 'Y');
$newOrder->save();
} catch (Exception $exception) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Bitrix\Sale\Order::cancelOrder',
'Error order canceled: ' . $externalId
);
}
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* Class RetailUser
*/
class RetailUser extends CUser
{
/**
* @return int|mixed|string|null
*/
public function GetID()
{
$rsUser = CUser::GetList(($by = 'ID'), ($order = 'DESC'), ['LOGIN' => 'retailcrm']);
if ($arUser = $rsUser->Fetch()) {
return $arUser['ID'];
} else {
$retailUser = new CUser;
$userPassword = uniqid();
$arFields = [
"NAME" => 'retailcrm',
"LAST_NAME" => 'retailcrm',
"EMAIL" => 'retailcrm@retailcrm.com',
"LOGIN" => 'retailcrm',
"LID" => "ru",
"ACTIVE" => "Y",
"GROUP_ID" => [2],
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword,
];
$id = $retailUser->Add($arFields);
if (!$id) {
return null;
} else {
return $id;
}
}
}
}

View File

@ -86,7 +86,7 @@ class RetailCrmOrder
} elseif ($search = array_search($prop['CODE'], $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']])) {//other
if (in_array($search, array('fio', 'phone', 'email'))) {//fio, phone, email
if ($search == 'fio') {
$order = array_merge($order, RCrmActions::explodeFIO($prop['VALUE'][0]));//add fio fields
$order = array_merge($order, RCrmActions::explodeFio($prop['VALUE'][0]));//add fio fields
} elseif ($search == 'email' && mb_strlen($prop['VALUE'][0]) > 100) {
continue;
} else {

View File

@ -1,35 +1,53 @@
<?php
use Bitrix\Main\Context;
use Bitrix\Main\Context\Culture;
use Bitrix\Main\UserTable;
use Bitrix\Sale\Delivery\Services\Manager;
use Bitrix\Sale\Internals\Fields;
use Bitrix\Sale\Location\LocationTable;
use Bitrix\Sale\Order;
use Bitrix\Sale\OrderTable;
use RetailCrm\ApiClient;
use Intaro\RetailCrm\Service\ManagerService;
use RetailCrm\Response\ApiResponse;
IncludeModuleLangFile(__FILE__);
/**
* Class RetailCrmOrder
*/
class RetailCrmOrder
{
/**
*
* Creates order or returns order for mass upload
*
* @param array $arFields
* @param array $arOrder
* @param $api
* @param $arParams
* @param bool $send
* @param null $site
* @param string $methodApi
*
* @return boolean
* @return boolean|array
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public static function orderSend($arFields, $api, $arParams, $send = false, $site = null, $methodApi = 'ordersEdit')
{
public static function orderSend(
array $arOrder,
$api,
$arParams,
bool $send = false,
$site = null,
string $methodApi = 'ordersEdit'
) {
if (!$api || empty($arParams)) { // add cond to check $arParams
return false;
}
if (empty($arFields)) {
if (empty($arOrder)) {
RCrmActions::eventLog('RetailCrmOrder::orderSend', 'empty($arFields)', 'incorrect order');
return false;
}
@ -38,23 +56,20 @@ class RetailCrmOrder
$currency = RetailcrmConfigProvider::getCurrencyOrDefault();
$optionCorpClient = RetailcrmConfigProvider::getCorporateClientStatus();
$order = array(
'number' => $arFields['NUMBER'],
'externalId' => $arFields['ID'],
'createdAt' => $arFields['DATE_INSERT'],
$order = [
'number' => $arOrder['NUMBER'],
'externalId' => $arOrder['ID'],
'createdAt' => $arOrder['DATE_INSERT'],
'customer' => isset($arParams['customerCorporate'])
? array('id' => $arParams['customerCorporate']['id'])
: array('externalId' => $arFields['USER_ID']),
'orderType' => isset($arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']]) ?
$arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']] : '',
'status' => isset($arParams['optionsPayStatuses'][$arFields['STATUS_ID']]) ?
$arParams['optionsPayStatuses'][$arFields['STATUS_ID']] : '',
'customerComment' => $arFields['USER_DESCRIPTION'],
'managerComment' => $arFields['COMMENTS'],
'delivery' => array(
'cost' => $arFields['PRICE_DELIVERY']
),
);
? ['id' => $arParams['customerCorporate']['id']]
: ['externalId' => $arOrder['USER_ID']],
'orderType' => $arParams['optionsOrderTypes'][$arOrder['PERSON_TYPE_ID']] ?? '',
'status' => $arParams['optionsPayStatuses'][$arOrder['STATUS_ID']] ?? '',
'customerComment' => $arOrder['USER_DESCRIPTION'],
'managerComment' => $arOrder['COMMENTS'],
'managerId' => $arParams['managerId'] ?? null,
'delivery' => ['cost' => $arOrder['PRICE_DELIVERY']],
];
if (isset($arParams['contactExId'])) {
$order['contact']['externalId'] = $arParams['contactExId'];
@ -76,7 +91,7 @@ class RetailCrmOrder
$order['customer']['browserId'] = $_COOKIE['_rc'];
}
$order['contragent']['contragentType'] = $arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']];
$order['contragent']['contragentType'] = $arParams['optionsContragentType'][$arOrder['PERSON_TYPE_ID']];
if ($methodApi == 'ordersEdit') {
$order['discountManualAmount'] = 0;
@ -84,19 +99,19 @@ class RetailCrmOrder
}
//fields
foreach ($arFields['PROPS']['properties'] as $prop) {
foreach ($arOrder['PROPS']['properties'] as $prop) {
if (!empty($arParams['optionsLegalDetails'])
&& $search = array_search($prop['CODE'], $arParams['optionsLegalDetails'][$arFields['PERSON_TYPE_ID']])
&& $search = array_search($prop['CODE'], $arParams['optionsLegalDetails'][$arOrder['PERSON_TYPE_ID']])
) {
$order['contragent'][$search] = $prop['VALUE'][0];//legal order data
} elseif (!empty($arParams['optionsCustomFields'])
&& $search = array_search($prop['CODE'], $arParams['optionsCustomFields'][$arFields['PERSON_TYPE_ID']])
&& $search = array_search($prop['CODE'], $arParams['optionsCustomFields'][$arOrder['PERSON_TYPE_ID']])
) {
$order['customFields'][$search] = $prop['VALUE'][0];//custom properties
} elseif ($search = array_search($prop['CODE'], $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']])) {//other
} elseif ($search = array_search($prop['CODE'], $arParams['optionsOrderProps'][$arOrder['PERSON_TYPE_ID']])) {//other
if (in_array($search, array('fio', 'phone', 'email'))) {//fio, phone, email
if ($search == 'fio') {
$order = array_merge($order, RCrmActions::explodeFIO($prop['VALUE'][0]));//add fio fields
$order = array_merge($order, RCrmActions::explodeFio($prop['VALUE'][0]));//add fio fields
} elseif ($search == 'email' && mb_strlen($prop['VALUE'][0]) > 100) {
continue;
} else {
@ -109,10 +124,10 @@ class RetailCrmOrder
}
} else {//address
if ($prop['TYPE'] == 'LOCATION' && isset($prop['VALUE'][0]) && $prop['VALUE'][0] != '') {
$arLoc = \Bitrix\Sale\Location\LocationTable::getByCode($prop['VALUE'][0])->fetch();
$arLoc = LocationTable::getByCode($prop['VALUE'][0])->fetch();
if ($arLoc) {
$server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot();
$countrys = array();
$server = Context::getCurrent()->getServer()->getDocumentRoot();
$countrys = [];
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/country.xml')) {
$countrysFile = simplexml_load_file($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/country.xml');
@ -121,9 +136,9 @@ class RetailCrmOrder
}
}
$location = \Bitrix\Sale\Location\Name\LocationTable::getList(array(
'filter' => array('=LOCATION_ID' => $arLoc['CITY_ID'], 'LANGUAGE_ID' => 'ru')
))->fetch();
$location = \Bitrix\Sale\Location\Name\LocationTable::getList([
'filter' => ['=LOCATION_ID' => $arLoc['CITY_ID'], 'LANGUAGE_ID' => 'ru'],
])->fetch();
if (count($countrys) > 0) {
$countryOrder = \Bitrix\Sale\Location\Name\LocationTable::getList(array(
@ -145,10 +160,10 @@ class RetailCrmOrder
}
//deliverys
if (array_key_exists($arFields['DELIVERYS'][0]['id'], $arParams['optionsDelivTypes'])) {
$order['delivery']['code'] = $arParams['optionsDelivTypes'][$arFields['DELIVERYS'][0]['id']];
if (isset($arFields['DELIVERYS'][0]['service']) && $arFields['DELIVERYS'][0]['service'] != '') {
$order['delivery']['service']['code'] = $arFields['DELIVERYS'][0]['service'];
if (array_key_exists($arOrder['DELIVERYS'][0]['id'], $arParams['optionsDelivTypes'])) {
$order['delivery']['code'] = $arParams['optionsDelivTypes'][$arOrder['DELIVERYS'][0]['id']];
if (isset($arOrder['DELIVERYS'][0]['service']) && $arOrder['DELIVERYS'][0]['service'] != '') {
$order['delivery']['service']['code'] = $arOrder['DELIVERYS'][0]['service'];
}
}
@ -168,7 +183,7 @@ class RetailCrmOrder
}
//basket
foreach ($arFields['BASKET'] as $position => $product) {
foreach ($arOrder['BASKET'] as $position => $product) {
$itemId = null;
$externalId = $position . "_" . $product['PRODUCT_ID'];
@ -197,15 +212,15 @@ class RetailCrmOrder
);
}
$item = array(
$item = [
'externalIds' => $externalIds,
'quantity' => $product['QUANTITY'],
'offer' => array(
'offer' => [
'externalId' => $product['PRODUCT_ID'],
'xmlId' => $product['PRODUCT_XML_ID']
),
'productName' => $product['NAME']
);
'xmlId' => $product['PRODUCT_XML_ID'],
],
'productName' => $product['NAME'],
];
if (isset($itemId)) {
$item['id'] = $itemId;
@ -259,8 +274,9 @@ class RetailCrmOrder
$integrationPayment = RetailcrmConfigProvider::getIntegrationPaymentTypes();
//payments
$payments = array();
foreach ($arFields['PAYMENTS'] as $payment) {
$payments = [];
foreach ($arOrder['PAYMENTS'] as $payment) {
if (!empty($payment['PAY_SYSTEM_ID']) && isset($arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']])) {
$pm = array(
'type' => $arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']]
@ -289,26 +305,25 @@ class RetailCrmOrder
RCrmActions::eventLog(
'RetailCrmOrder::orderSend',
'payments',
'OrderID = ' . $arFields['ID'] . '. Payment not found.'
'OrderID = ' . $arOrder['ID'] . '. Payment not found.'
);
}
}
continue;
}
}
if (count($payments) > 0) {
$order['payments'] = $payments;
}
//send
if (function_exists('retailCrmBeforeOrderSend')) {
$newResOrder = retailCrmBeforeOrderSend($order, $arFields);
$newResOrder = retailCrmBeforeOrderSend($order, $arOrder);
if (is_array($newResOrder) && !empty($newResOrder)) {
$order = $newResOrder;
} elseif ($newResOrder === false) {
RCrmActions::eventLog(
'RetailCrmOrder::orderSend',
'retailCrmBeforeOrderSend()',
'OrderID = ' . $arFields['ID'] . '. Sending canceled after retailCrmBeforeOrderSend'
'OrderID = ' . $arOrder['ID'] . '. Sending canceled after retailCrmBeforeOrderSend'
);
return false;
@ -338,15 +353,15 @@ class RetailCrmOrder
*
* @param int $pSize
* @param bool $failed -- flag to export failed orders
* @param bool $orderList
* @param array|null $orderList
*
* @return boolean
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ArgumentNullException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
* @throws \Bitrix\Main\ArgumentException
*/
public static function uploadOrders($pSize = 50, $failed = false, $orderList = false)
public static function uploadOrders(int $pSize = 50, bool $failed = false, array $orderList = []): bool
{
if (!RetailcrmDependencyLoader::loadDependencies()) {
return true;
@ -363,15 +378,15 @@ class RetailCrmOrder
if ($failed == true && $failedIds !== false && count($failedIds) > 0) {
$orderIds = $failedIds;
} elseif ($orderList !== false && count($orderList) > 0) {
} elseif (count($orderList) > 0) {
$orderIds = $orderList;
} else {
$dbOrder = \Bitrix\Sale\Internals\OrderTable::GetList(array(
'order' => array("ID" => "ASC"),
'filter' => array('>ID' => $lastUpOrderId),
$dbOrder = OrderTable::GetList([
'order' => ["ID" => "ASC"],
'filter' => ['>ID' => $lastUpOrderId],
'limit' => $pSize,
'select' => array('ID')
));
'select' => ['ID'],
]);
while ($arOrder = $dbOrder->fetch()) {
$orderIds[] = $arOrder['ID'];
@ -412,15 +427,16 @@ class RetailCrmOrder
foreach ($orderIds as $orderId) {
$site = null;
$id = Order::load($orderId);
$orderObj = Order::load($orderId);
if (!$id) {
if (!$orderObj) {
continue;
}
$arCustomer = [];
$arCustomerCorporate = [];
$order = self::orderObjToArr($id);
$order = self::orderObjToArr($orderObj);
$user = UserTable::getById($order['USER_ID'])->fetch();
$site = RetailCrmOrder::getSite($order['LID'], $optionsSitesList);
if (true === $site) {
@ -429,6 +445,11 @@ class RetailCrmOrder
self::createCustomerForOrder($api, $arCustomer, $arCustomerCorporate,$arParams, $order, $site);
if (isset($order['RESPONSIBLE_ID']) && !empty($order['RESPONSIBLE_ID'])) {
$managerService = ManagerService::getInstance();
$arParams['managerId'] = $managerService->getManagerCrmId((int) $order['RESPONSIBLE_ID']);
}
$arOrders = self::orderSend($order, $api, $arParams, false, $site,'ordersCreate');
if (!$arCustomer || !$arOrders) {
@ -498,6 +519,7 @@ class RetailCrmOrder
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
* @throws \Exception
*/
public static function createCustomerForOrder(
ApiClient $api,
@ -602,7 +624,7 @@ class RetailCrmOrder
$customerLegalName,
$orderData['delivery']['address']['text'],
$api,
$site = null
null
);
} elseif (array_key_exists($customerLegalName, $resCustomersCorporate)) {
$createResponse = $api->customersCorporateCreate(
@ -664,7 +686,7 @@ class RetailCrmOrder
*
* @return false|mixed|null
*/
public static function getSite($key, $optionsSitesList)
public static function getSite(string $key, array $optionsSitesList)
{
if ($optionsSitesList) {
if (array_key_exists($key, $optionsSitesList) && $optionsSitesList[$key] != null) {
@ -686,9 +708,9 @@ class RetailCrmOrder
*
* @return array|false
*/
public static function uploadItems($pack, $method, $keyResponse, $api, $optionsSitesList)
public static function uploadItems(array $pack, string $method, string $keyResponse, ApiClient $api, array $optionsSitesList)
{
$uploaded = array();
$uploaded = [];
$sizePack = 50;
foreach ($pack as $key => $itemLoad) {
@ -716,7 +738,7 @@ class RetailCrmOrder
return false;
}
if ($response instanceof \RetailCrm\Response\ApiResponse) {
if ($response instanceof ApiResponse) {
if ($response->offsetExists($keyResponse)) {
$uploaded = array_merge($uploaded, $response[$keyResponse]);
}
@ -750,10 +772,10 @@ class RetailCrmOrder
* @return array
* @throws \Bitrix\Main\SystemException
*/
public static function orderObjToArr($obOrder)
public static function orderObjToArr(Order $obOrder): array
{
$culture = new \Bitrix\Main\Context\Culture(array("FORMAT_DATETIME" => "Y-m-d HH:i:s"));
$arOrder = array(
$culture = new Culture(['FORMAT_DATETIME' => 'Y-m-d HH:i:s']);
$arOrder = [
'ID' => $obOrder->getId(),
'NUMBER' => $obOrder->getField('ACCOUNT_NUMBER'),
'LID' => $obOrder->getSiteId(),
@ -762,16 +784,17 @@ class RetailCrmOrder
'USER_ID' => $obOrder->getUserId(),
'PERSON_TYPE_ID' => $obOrder->getPersonTypeId(),
'CURRENCY' => $obOrder->getCurrency(),
'PAYMENTS' => array(),
'DELIVERYS' => array(),
'PAYMENTS' => [],
'DELIVERYS' => [],
'PRICE_DELIVERY' => $obOrder->getDeliveryPrice(),
'PROPS' => $obOrder->getPropertyCollection()->getArray(),
'DISCOUNTS' => $obOrder->getDiscount()->getApplyResult(),
'BASKET' => array(),
'BASKET' => [],
'USER_DESCRIPTION' => $obOrder->getField('USER_DESCRIPTION'),
'COMMENTS' => $obOrder->getField('COMMENTS'),
'REASON_CANCELED' => $obOrder->getField('REASON_CANCELED')
);
'REASON_CANCELED' => $obOrder->getField('REASON_CANCELED'),
'RESPONSIBLE_ID' => $obOrder->getField('RESPONSIBLE_ID'),
];
$shipmentList = $obOrder->getShipmentCollection();
@ -781,18 +804,19 @@ class RetailCrmOrder
}
if ($shipmentData->getDeliveryId()) {
$delivery = \Bitrix\Sale\Delivery\Services\Manager::getById($shipmentData->getDeliveryId());
$delivery = Manager::getById($shipmentData->getDeliveryId());
$siteDeliverys = RCrmActions::DeliveryList();
foreach ($siteDeliverys as $siteDelivery) {
if ($siteDelivery['ID'] == $delivery['ID'] && $siteDelivery['PARENT_ID'] == 0) {
unset($delivery['PARENT_ID']);
}
}
if ($delivery['PARENT_ID']) {
$servise = explode(':', $delivery['CODE']);
$shipment = array('id' => $delivery['PARENT_ID'], 'service' => $servise[1]);
$service = explode(':', $delivery['CODE']);
$shipment = ['id' => $delivery['PARENT_ID'], 'service' => $service[1]];
} else {
$shipment = array('id' => $delivery['ID']);
$shipment = ['id' => $delivery['ID']];
}
$arOrder['DELIVERYS'][] = $shipment;
}

View File

@ -0,0 +1,14 @@
<?php
namespace RetailCrm\Component\Exception;
use Exception;
/**
* Class FailedDbOperationException
*
* @package RetailCrm\Component\Exception
*/
class FailedDbOperationException extends Exception
{
}

View File

@ -0,0 +1,3 @@
<?php
$MESS['REP_ERR'] = 'Ошибка репозитория в методе #METHOD#';

View File

@ -0,0 +1,67 @@
<?php
namespace Intaro\RetailCrm\Repository;
use Bitrix\Main\ORM\Objectify\EntityObject;
use Bitrix\Main\UserTable;
use RetailCrm\Component\Exception\FailedDbOperationException;
use RetailcrmConfigProvider;
use RetailcrmConstants;
/**
* Class ManagerRepository
*
* @package Intaro\RetailCrm\Repository
*/
class ManagerRepository
{
/**
* @param array $newMatches
*
* @return void
* @throws \RetailCrm\Component\Exception\FailedDbOperationException
*/
public function addManagersToMapping(array $newMatches): void
{
$usersMap = RetailcrmConfigProvider::getUsersMap();
if (is_array($usersMap)) {
$recordData = array_merge($usersMap, $newMatches);
} else {
$recordData = $newMatches;
}
if (!RetailcrmConfigProvider::setUsersMap($recordData)) {
throw new FailedDbOperationException();
}
}
/**
* @param string $email
*
* @return int|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public function getManagerBitrixIdByEmail(string $email): ?int
{
/** @var \Bitrix\Main\ORM\Objectify\EntityObject $user */
$user = UserTable::query()
->addSelect('ID')
->where('EMAIL', $email)
->exec()
->fetchObject();
if ($user instanceof EntityObject) {
$userId = $user->get('ID');
if (is_int($userId) && $userId > 0) {
return $userId;
}
}
return null;
}
}

View File

@ -0,0 +1,202 @@
<?php
namespace Intaro\RetailCrm\Service;
use Bitrix\Main\ArgumentException;
use Bitrix\Main\ObjectPropertyException;
use Bitrix\Main\SystemException;
use Intaro\RetailCrm\Repository\ManagerRepository;
use InvalidArgumentException;
use Logger;
use RetailCrm\ApiClient;
use RetailCrm\Component\Exception\FailedDbOperationException;
use RetailcrmConfigProvider;
use RetailcrmConstants;
/**
* Отвечает за работу с ответственными лицами в заказах
*
* Class ManagerService
*
* @package Intaro\RetailCrm\Service
*/
class ManagerService
{
protected static $instance;
/**
* @var \Intaro\RetailCrm\Repository\ManagerRepository
*/
private $repository;
/**
* @var \RetailCrm\ApiClient
*/
private $client;
/**
* @var \Logger
*/
private $logger;
/**
* ManagerService constructor.
*/
private function __construct()
{
$this->client = new ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
$this->repository = new ManagerRepository();
$this->logger = Logger::getInstance();
}
/**
* @return \Intaro\RetailCrm\Service\ManagerService
*
* TODO заменить вызов на сервис-локатор, когда он приедет
*/
public static function getInstance(): ManagerService
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Синхронизирует пользователей CRM и Битрикс
*/
public function synchronizeManagers(): void
{
$currentPage = 1;
RetailcrmConfigProvider::setUsersMap([]);
do {
$crmUsers = $this->getCrmUsersPage($currentPage);
$matchesArray = $this->findMatchesInBitrix($crmUsers);
if (!empty($matchesArray)) {
try {
$this->repository->addManagersToMapping($matchesArray);
} catch (FailedDbOperationException $exception) {
$this->logger->write(GetMessage('REP_ERR', ['#METHOD#' => __METHOD__]),'serviceErrors');
}
}
$currentPage++;
} while (count($crmUsers) > 0);
}
/**
* @param string $bitrixUserId
*
* @return int|null
*/
public function getManagerCrmId(string $bitrixUserId): ?int
{
$usersMap = RetailcrmConfigProvider::getUsersMap();
return $usersMap[RetailcrmConstants::BITRIX_USER_ID_PREFIX . $bitrixUserId] ?? null;
}
/**
* @param int|null $crmManagerId
*
* @return int
*/
public function getManagerBitrixId(?int $crmManagerId): ?int
{
$usersMap = RetailcrmConfigProvider::getUsersMap();
if (!is_array($usersMap) || count($usersMap) === 0) {
return null;
}
$flipUserMap = array_flip($usersMap);
if (!isset($flipUserMap[$crmManagerId])) {
return null;
}
$managerId = str_replace(RetailcrmConstants::BITRIX_USER_ID_PREFIX, '', $flipUserMap[$crmManagerId]);
if (empty($managerId)) {
return null;
}
return (int) $managerId;
}
/**
* @param int $pageNumber
*
* @return array
*/
private function getCrmUsersPage(int $pageNumber): array
{
$response = $this->client->usersList([], $pageNumber);
if (!$response->isSuccessful()) {
return [];
}
try {
$users = $response->offsetGet('users');
if (is_array($users)) {
return $users;
}
return [];
} catch (InvalidArgumentException $exception) {
return [];
}
}
/**
* @param array $crmUsers
*
* @return array
*/
private function findMatchesInBitrix(array $crmUsers): array
{
$matchesUsers = [];
foreach ($crmUsers as $crmUser) {
$matchesUser = $this->getMatchesForCrmUser($crmUser);
if (count($matchesUser) > 0) {
$bitrixId = RetailcrmConstants::BITRIX_USER_ID_PREFIX . $matchesUser['bitrixUserId'];
$matchesUsers[$bitrixId] = $matchesUser['crmUserId'];
}
}
return $matchesUsers;
}
/**
* @param array $crmUser
*
* @return array
*/
private function getMatchesForCrmUser(array $crmUser): array
{
if (!empty($crmUser['email']) && !empty($crmUser['id'])) {
try {
$bitrixUserId = $this->repository->getManagerBitrixIdByEmail($crmUser['email']);
if (is_int($bitrixUserId)) {
return [
'bitrixUserId' => $bitrixUserId,
'crmUserId' => $crmUser['id']
];
}
} catch (ObjectPropertyException | ArgumentException | SystemException $e) {
$this->logger->write(GetMessage('REP_ERR', ['#METHOD#' => __METHOD__]), 'serviceErrors');
}
}
return [];
}
}

View File

@ -1,251 +0,0 @@
<?php
/**
* Class RetailCrmOrderTest
*/
use \Bitrix\Main\Loader;
use \Bitrix\Main\Application;
class RetailCrmOrderTest extends BitrixTestCase
{
protected $retailCrmOrder;
protected $test;
/**
* @inheritdoc
*/
public function setUp()
{
parent::setUp();
$cache = Application::getInstance()->getManagedCache();
$cache->clean('b_option:intaro.retailcrm');
$this->retailCrmOrder = \Mockery::mock('RetailCrmOrder');
COption::SetOptionString('intaro.retailcrm', 'api_version', 'v5');
CModule::IncludeModule('intaro.retailcrm');
RetailcrmConfigProvider::setPaymentTypes(['bitrixPayment' => 'crmPayment']);
RetailcrmConfigProvider::setIntegrationPaymentTypes(['crmPayment']);
}
public function testModuleInstalled()
{
$this->assertTrue(Loader::includeModule("intaro.retailcrm"));
}
/**
* @param $pSize
* @param $failed
* @param $orderList
*
* @dataProvider getData
*/
public function testUploadOrders($pSize, $failed, $orderList)
{
$this->assertEquals(50, $pSize);
$this->assertFalse($failed);
if ($orderList) {
$this->assertEquals(3, sizeof($orderList));
$this->retailCrmOrder->shouldReceive('uploadOrders')
->andReturn(
array(
array('id' => 001, 'externalId' => 2),
array('id' => 002, 'externalId' => 3),
array('id' => 003, 'externalId' => 4)
)
);
$result = $this->retailCrmOrder->uploadOrders();
foreach ($result as $key => $order) {
$this->assertEquals($order["externalId"], $orderList[$key]);
}
} else {
$this->assertFalse($orderList);
}
}
/**
* @return array
*/
public function getData()
{
return [
[
'pSize' => 50,
'failed' => false,
'orderList' => false
],
[
'pSize' => 50,
'failed' => false,
'orderList' => array(2,3,4)
]
];
}
/**
* @param $pack
* @param $method
* @param $keyResponse
* @param $apiMock
* @param $optionsSitesList
*
* @dataProvider getDataUpload
*/
public function testUploadItems($pack, $method, $keyResponse, $apiMock, $optionsSitesList)
{
$responseBody1 = '';
for ($i = 1; $i < 51; $i++) {
$responseBody1 .= "{\"id\":".$i.",\"externalId\":".$i."},";
}
$responseBody1 = substr($responseBody1,0,-1);
$responseBody1 ='{
"success":true,
"'.$keyResponse.'":['.$responseBody1.']
}';
$responseBody2 ='{
"success":true,
"uploadedCustomers":[
{"id":51,"externalId":"51"}
]
}';
$apiMock->shouldReceive($method)
->andReturn(
new \RetailCrm\Response\ApiResponse(
200,
$responseBody1
),
new \RetailCrm\Response\ApiResponse(
200,
$responseBody2
)
);
$test = new RetailCrmOrder();
$result = $test::uploadItems($pack, $method, $keyResponse, $apiMock, $optionsSitesList);
$this->assertEquals(sizeof($pack['s1']), sizeof($result));
}
/**
* @return mixed
*/
public function getCustomerList()
{
$faker = Faker\Factory::create();
$customerList = [];
for ($i = 1; $i < 52; $i++) {
$customerList['s1'][$i]['externalId'] = $i;
$customerList['s1'][$i]['email'] = $faker->email;
$customerList['s1'][$i]['createdAt'] = $faker->date('Y-m-d H:i:s');
$customerList['s1'][$i]['subscribed'] = '';
$customerList['s1'][$i]['contragent'] = ['contragentType' => 'individual'];
$customerList['s1'][$i]['firstName'] = $faker->firstName;
$customerList['s1'][$i]['lastName'] = $faker->lastName;
}
return $customerList;
}
/**
* @return array
*/
public function getDataUpload()
{
return [
[
'pack' => $this->getCustomerList(),
'customersUpload',
'uploadedCustomers',
'api'=>\Mockery::mock('RetailCrm\ApiClient'),
'optionsSitesList'=>[]
],
[
'pack'=> [],
'ordersUpload',
'uploadedOrders',
'api'=>\Mockery::mock('RetailCrm\ApiClient'),
'optionsSitesList'=>[]
]
];
}
/**
* @param array $arFields
* @param array $arParams
* @param string $methodApi
* @param array $expected
*
* @dataProvider orderSendProvider
*/
public function testOrderSend($arFields, $arParams, $methodApi, $expected)
{
$orderSend = RetailCrmOrder::orderSend(
$arFields,
new stdClass(),
$arParams,
false,
null,
$methodApi
);
$this->assertEquals($expected['payments'][0], $orderSend['payments'][0]);
}
/**
* @return array[]
*/
public function orderSendProvider()
{
$arFields = [
'ID' => 1,
'NUMBER' => 1,
'USER_ID' => 1,
'STATUS_ID' => 1,
'PERSON_TYPE_ID' => 'individual',
'DATE_INSERT' => '2015-02-22 00:00:00',
'USER_DESCRIPTION' => 'userComment',
'COMMENTS' => 'managerComment',
'PRICE_DELIVERY' => '100',
'PROPS' => ['properties' => []],
'DELIVERYS' => [[
'id' => 'test',
'service' => 'service'
]],
'BASKET' => [],
'PAYMENTS' => [[
'ID' => 1,
'PAY_SYSTEM_ID' => 'bitrixPayment',
'SUM' => 1000,
'DATE_PAID' => '2021-02-08 10:36:16',
'PAID' => 'paid'
]]
];
$arParams = [
'optionsPayTypes' => ['bitrixPayment' => 'crmPayment'],
'optionsPayment' => ['paid' => 'Y']
];
return [[
'arFields' => $arFields,
'arParams' => $arParams,
'methodApi' => 'ordersCreate',
'expected' => [
'number' => strval($arFields['NUMBER']),
'externalId' => strval($arFields['ID']),
'payments' => [[
'type' => $arParams['optionsPayTypes'][$arFields['PAYMENTS'][0]['PAY_SYSTEM_ID']],
'externalId' => RCrmActions::generatePaymentExternalId($arFields['PAYMENTS'][0]['ID']),
'paidAt' => '2021-02-08 10:36:16'
]]
],
]];
}
}