diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php index f6eedf27..6563d6d6 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php @@ -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); } diff --git a/intaro.retailcrm/lib/component/handlers/eventshandlers.php b/intaro.retailcrm/lib/component/handlers/eventshandlers.php index d99ec5b5..75e96111 100644 --- a/intaro.retailcrm/lib/component/handlers/eventshandlers.php +++ b/intaro.retailcrm/lib/component/handlers/eventshandlers.php @@ -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 + ); + } } diff --git a/intaro.retailcrm/lib/service/orderloyaltydataservice.php b/intaro.retailcrm/lib/service/orderloyaltydataservice.php index 1d3bee45..898b9846 100644 --- a/intaro.retailcrm/lib/service/orderloyaltydataservice.php +++ b/intaro.retailcrm/lib/service/orderloyaltydataservice.php @@ -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 * diff --git a/intaro.retailcrm/options.php b/intaro.retailcrm/options.php index 555daad6..3c3364a0 100644 --- a/intaro.retailcrm/options.php +++ b/intaro.retailcrm/options.php @@ -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'); }