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

Removing unnecessary elements, fixing a broken method, adding code to OrderHistory

This commit is contained in:
Ivan Chaplygin 2022-12-29 09:51:58 +03:00
parent be5d7ba33e
commit 7f3f0c35ce
4 changed files with 29 additions and 87 deletions

View file

@ -939,6 +939,7 @@ class RetailCrmHistory
$editBasketInfo = [];
$deleteBasketInfo = [];
$bonusesChargeTotal = null;
$loyaltyDiscount = null;
if (isset($order['items'])) {
$itemUpdate = true;
@ -1083,12 +1084,17 @@ class RetailCrmHistory
}
$manualProductDiscount = 0;
$bonusesChargeProduct = $product['bonusesCharge'] ?? null;
foreach ($product['discounts'] as $productDiscount) {
if ('manual_product' === $productDiscount['type']) {
$manualProductDiscount = $productDiscount['amount'];
}
if ('loyalty_level' === $productDiscount['type']) {
$loyaltyDiscount += $productDiscount['amount'];
}
}
$editBasketInfo[] = [
@ -1179,6 +1185,8 @@ class RetailCrmHistory
}
}
EventsHandlers::$disableSaleHandler = true;
$newOrder->setField('PRICE', $orderSumm);
self::orderSave($newOrder);
@ -1202,13 +1210,22 @@ class RetailCrmHistory
}
}
$orderLoyaltyDataService->saveBonusAndDiscToOrderProps(
$newOrder->getPropertyCollection(),
$loyaltyDiscount,
$bonusesChargeTotal
);
$hlInfoBuilder = new LoyaltyDataBuilder();
$hlInfoBuilder->setOrder($newOrder);
$hlInfoBuilder->setCalculateItemsInput($calculateItemsInput);
$hlInfoBuilder->setBonusCountTotal($bonusesChargeTotal);
$orderLoyaltyDataService->saveLoyaltyInfoToHl($hlInfoBuilder->build($basketItemIds)->getResult());
self::orderSave($newOrder);
}
EventsHandlers::$disableSaleHandler = false;
if (!empty($deleteBasketInfo)) {
$orderLoyaltyDataService->deleteLoyaltyInfoFromHl($deleteBasketInfo);
}

View file

@ -29,6 +29,7 @@ use Intaro\RetailCrm\Service\CustomerService;
use Intaro\RetailCrm\Service\OrderLoyaltyDataService;
use Intaro\RetailCrm\Service\Utils;
use Logger;
use RCrmActions;
use RetailCrmEvent;
use Throwable;
@ -120,28 +121,6 @@ class EventsHandlers
}
}
/**
* Обновляет информацию о Программе лояльности в административной панели.
* При каждом открытии заказа делает запрос к CRM и получает актуальную информацию.
*
* @param $items
*/
public function OnAdminContextMenuShowHandler(&$items)
{
global $APPLICATION;
if (
$_SERVER['REQUEST_METHOD'] === 'GET'
&& $_REQUEST['ID'] > 0
&& $APPLICATION->GetCurPage() === '/bitrix/admin/sale_order_view.php'
) {
/* @var OrderLoyaltyDataService $service */
$service = ServiceLocator::get(OrderLoyaltyDataService::class);
$service->updateLoyaltyInfo($_REQUEST['ID']);
}
}
/**
* Обработчик события, вызываемого ПОСЛЕ сохранения заказа (OnSaleOrderSaved)
*
@ -164,7 +143,6 @@ class EventsHandlers
// TODO: Replace old call with a new one.
$saveResult = RetailCrmEvent::orderSave($order);
Utils::handleApiErrors($saveResult);
$isBonusInput = (
@ -220,13 +198,20 @@ class EventsHandlers
$discountInput,
$loyaltyBonusMsg
);
$hlInfoBuilder->setCalculateItemsInput($calculateItemsInput);
$orderLoyaltyDataService->saveLoyaltyInfoToHl($hlInfoBuilder->build()->getResult());
$order->save();
self::$disableSaleHandler = false;
}
} catch (Throwable $exception) {
Logger::getInstance()->write(GetMessage('CAN_NOT_SAVE_ORDER') . $exception->getMessage(), 'uploadApiErrors');
RCrmActions::eventLog(
'intaro.retailcrm/event', 'Event',
'error catch '. $exception
);
}
}

View file

@ -151,61 +151,18 @@ class OrderLoyaltyDataService
foreach ($props as $prop) {
if ($prop->getField('CODE') === 'LP_DISCOUNT_INFO') {
$this->saveLpInfoToField($prop, $loyaltyDiscountInput);
break;
}
}
foreach ($props as $prop) {
if ($prop->getField('CODE') === 'LP_BONUS_INFO') {
$this->saveLpInfoToField($prop, $loyaltyBonus);
break;
}
}
}
/**
* Обновляет данные о начисленных бонусах и скидках в полях заказа
*
* @param int $orderId
*/
public function updateLoyaltyInfo(int $orderId): void
{
/** @var \Intaro\RetailCrm\Component\ApiClient\ClientAdapter $client */
$client = ClientFactory::createClientAdapter();
$response = $client->getOrder($orderId);
if ($response === null || !is_array($response->order->items)) {
return;
}
try {
$order = Order::load($orderId);
if ($order === null) {
return;
}
$loyaltyDiscount = 0;
/** @var OrderProduct $item */
foreach ($response->order->items as $item) {
foreach ($item->discounts as $discount) {
if (in_array($discount->type, ['personal', 'loyalty_level'])) {
$loyaltyDiscount += $discount->amount;
}
}
}
$this->saveBonusAndDiscToOrderProps(
$order->getPropertyCollection(),
$loyaltyDiscount ?? 0.0,
$response->order->bonusesChargeTotal
);
EventsHandlers::$disableSaleHandler = true;
$order->save();
EventsHandlers::$disableSaleHandler = false;
} catch (Exception $exception) {
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
}
}
/**
* @param int $orderId
*

View file

@ -597,23 +597,6 @@ if (isset($_POST['Update']) && ($_POST['Update'] === 'Y')) {
}
ConfigProvider::setLoyaltyProgramStatus('Y');
$eventManager = EventManager::getInstance();
$eventManager->unRegisterEventHandler('sale',
'OnSaleOrderSaved',
Constants::MODULE_ID,
'RetailCrmEvent',
'orderSave'
);
$eventManager->registerEventHandler(
'main',
'OnAdminContextMenuShow',
Constants::MODULE_ID,
EventsHandlers::class,
'OnAdminContextMenuShowHandler'
);
} else {
ConfigProvider::setLoyaltyProgramStatus('N');
}