1
0
Fork 0
mirror of synced 2025-04-18 16:41:02 +00:00

added hlInfo builder, deleted methods from services

This commit is contained in:
Sergey Chazov 2021-05-07 10:43:07 +03:00 committed by Neur0toxine
parent 3d5007d8d0
commit 681811c16e
3 changed files with 184 additions and 99 deletions

View file

@ -0,0 +1,172 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Component\Builder\Bitrix
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Component\Builder\Bitrix;
use Bitrix\Main\ArgumentNullException;
use Bitrix\Sale\BasketItemBase;
use Bitrix\Sale\Order;
use Exception;
use Intaro\RetailCrm\Component\Builder\BuilderInterface;
use Intaro\RetailCrm\Component\Constants;
use Intaro\RetailCrm\Component\ServiceLocator;
use Intaro\RetailCrm\Model\Api\OrderProduct;
use Intaro\RetailCrm\Model\Api\Response\Order\Loyalty\OrderLoyaltyApplyResponse;
use Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData;
use Intaro\RetailCrm\Service\CookieService;
use Intaro\RetailCrm\Service\LoyaltyService;
use Intaro\RetailCrm\Service\OrderLoyaltyDataService;
use Logger;
/**
* Class LoyaltyDataBuilder
*
* @package Intaro\RetailCrm\Component\Builder\Bitrix
*/
class LoyaltyDataBuilder implements BuilderInterface
{
/** @var Order $order */
private $order;
/** @var OrderLoyaltyApplyResponse $applyResponse */
private $applyResponse;
/** @var array */
private $calculateItemsInput;
/** @var Logger $logger */
private $logger;
/** @var OrderLoyaltyData[] $data */
private $data;
/**
* LoyaltyDataBuilder constructor.
*/
public function __construct()
{
$this->logger = Logger::getInstance();
}
/**
* @return \Intaro\RetailCrm\Component\Builder\BuilderInterface
*/
public function build(): BuilderInterface
{
try {
/** @var BasketItemBase $basketItem */
foreach ($this->order->getBasket() as $key => $basketItem) {
$loyaltyHl = new OrderLoyaltyData();
$loyaltyHl->orderId = $this->order->getId();
$loyaltyHl->itemId= $basketItem->getProductId();
$loyaltyHl->basketItemPositionId = $basketItem->getId();
$loyaltyHl->quantity = $basketItem->getQuantity();
$loyaltyHl->name = $basketItem->getField('NAME');
$loyaltyHl->defaultDiscount
= $this->calculateItemsInput[$loyaltyHl->basketItemPositionId]['SHOP_ITEM_DISCOUNT'] ?? null;
$this->addBonusInfo($loyaltyHl, $key);
$this->data[] = $loyaltyHl;
}
} catch (ArgumentNullException | Exception $exception) {
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
}
return $this;
}
/**
* @return \Intaro\RetailCrm\Component\Builder\BuilderInterface
*/
public function reset(): BuilderInterface
{
$this->data = null;
$this->order = null;
$this->applyResponse = null;
return $this;
}
/**
* @return \Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData[]
*/
public function getResult(): array
{
return $this->data;
}
/**
* @param \Bitrix\Sale\Order $order
*/
public function setOrder(Order $order): void
{
$this->order = $order;
}
/**
* @param \Intaro\RetailCrm\Model\Api\Response\Order\Loyalty\OrderLoyaltyApplyResponse $applyResponse
*/
public function setApplyResponse(OrderLoyaltyApplyResponse $applyResponse): void
{
$this->applyResponse = $applyResponse;
}
/**
* @param array $calculateItemsInput
*/
public function setCalculateItemsInput(array $calculateItemsInput): void
{
$this->calculateItemsInput = $calculateItemsInput;
}
/**
* @param \Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData $loyaltyHl
* @param int $key
*/
private function addBonusInfo(OrderLoyaltyData $loyaltyHl, int $key): void
{
if (null === $this->applyResponse) {
return;
}
/** @var \Intaro\RetailCrm\Service\CookieService $service */
$service = ServiceLocator::get(CookieService::class);
$isDebited = false;
$checkId = '';
//если верификация необходима, но не пройдена
if (
isset($this->applyResponse->verification, $this->applyResponse->verification->checkId)
&& !isset($response->verification->verifiedAt)
) {
$isDebited = false;
$service->setSmsCookie('lpOrderBonusConfirm', $this->applyResponse->verification);
$checkId = $this->applyResponse->verification->checkId;
}
//если верификация не нужна
if (!isset($this->applyResponse->verification)) {
$isDebited = true;
}
/** @var OrderProduct $item */
$item = $this->applyResponse->order->items[$key];
$loyaltyHl->checkId = $checkId;
$loyaltyHl->isDebited = $isDebited;
$loyaltyHl->bonusCount = $item->bonusesChargeTotal;
}
}

View file

@ -18,6 +18,7 @@ IncludeModuleLangFile(__FILE__);
use Bitrix\Main\Event;
use Bitrix\Main\HttpRequest;
use Bitrix\Sale\Order;
use Intaro\RetailCrm\Component\Builder\Bitrix\LoyaltyDataBuilder;
use Intaro\RetailCrm\Component\ConfigProvider;
use Intaro\RetailCrm\Component\ServiceLocator;
use Intaro\RetailCrm\Repository\UserRepository;
@ -176,7 +177,10 @@ class EventsHandlers
if ($isNewOrder && $isLoyaltyOn) {
self::$disableSaleHandler = true;
$hlInfo = $orderLoyaltyDataService->addMainInfoToHl($order);
$hlInfoBuilder = new LoyaltyDataBuilder();
$hlInfoBuilder->setOrder($order);
$discountInput = isset($_POST['loyalty-discount-input'])
? (float) $_POST['loyalty-discount-input']
: 0;
@ -185,12 +189,9 @@ class EventsHandlers
//Если есть бонусы
if ($isBonusesIssetAndAvailable) {
$hlInfo = $loyaltyService->saveBonuses(
$order,
$hlInfo,
(int) $_POST['bonus-input']
$hlInfoBuilder->setApplyResponse(
$loyaltyService->applyBonusesInOrder($order, (int) $_POST['bonus-input'])
);
$loyaltyBonusMsg = (int) $_POST['bonus-input'];
}
@ -204,9 +205,11 @@ class EventsHandlers
$discountInput,
$loyaltyBonusMsg
);
$hlInfo = $orderLoyaltyDataService->addDiscountsToHl($calculateItemsInput, $hlInfo);
$hlInfoBuilder->setCalculateItemsInput($calculateItemsInput);
$hlInfoBuilder->build();
$hlInfo = $hlInfoBuilder->getResult();
\Bitrix\Main\Diag\Debug::writeToFile(json_encode($hlInfo), '', 'log.txt');
$orderLoyaltyDataService->saveLoyaltyInfoToHl($hlInfo);
self::$disableSaleHandler = false;

View file

@ -228,96 +228,6 @@ class OrderLoyaltyDataService
}
}
/**
* Добавляет оплату бонусами в модель записи Hl-блока
*
* @param array $calculateItemsInput
* @param OrderLoyaltyData[] $loyaltyHls
* @return array
*/
public function addDiscountsToHl(array $calculateItemsInput, array $loyaltyHls): array
{
foreach ($loyaltyHls as $loyaltyHl) {
$loyaltyHl->defaultDiscount = $calculateItemsInput[$loyaltyHl->basketItemPositionId]['SHOP_ITEM_DISCOUNT'];
}
return $loyaltyHls;
}
/**
* Создает модели данных для HL и добавляет в них основные данные
*
* @param \Bitrix\Sale\Order $order
* @return OrderLoyaltyData[]
*/
public function addMainInfoToHl(Order $order): array
{
$loyaltyHls = [];
try {
/** @var BasketItemBase $basketItem */
foreach ($order->getBasket() as $basketItem) {
$loyaltyHl = new OrderLoyaltyData();
$loyaltyHl->orderId = $order->getId();
$loyaltyHl->itemId = $basketItem->getProductId();
$loyaltyHl->basketItemPositionId = $basketItem->getId();
$loyaltyHl->quantity = $basketItem->getQuantity();
$loyaltyHl->name = $basketItem->getField('NAME');
$loyaltyHls[] = $loyaltyHl;
}
} catch (Exception $exception) {
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
}
return $loyaltyHls;
}
/**
* Добавляет оплату бонусами в модель записи Hl-блока
*
* @param OrderLoyaltyData[] $loyaltyHls
* @param \Intaro\RetailCrm\Model\Api\Response\Order\Loyalty\OrderLoyaltyApplyResponse $response
*
* @return array
*/
public function addBonusesToHl(
array $loyaltyHls,
OrderLoyaltyApplyResponse $response
): array {
/** @var \Intaro\RetailCrm\Service\CookieService $service */
$service = ServiceLocator::get(CookieService::class);
$isDebited = false;
$checkId = '';
//если верификация необходима, но не пройдена
if (
isset($response->verification, $response->verification->checkId)
&& !isset($response->verification->verifiedAt)
) {
$isDebited = false;
$service->setSmsCookie('lpOrderBonusConfirm', $response->verification);
$checkId = $response->verification->checkId;
}
//если верификация не нужна
if (!isset($response->verification)) {
$isDebited = true;
}
foreach ($loyaltyHls as $key => $loyaltyHl) {
/** @var OrderProduct $item */
$item = $response->order->items[$key];
$loyaltyHl->checkId = $checkId;
$loyaltyHl->isDebited = $isDebited;
$loyaltyHl->bonusCount = $item->bonusesChargeTotal;
}
return $loyaltyHls;
}
/**
* @param $personId
* @param $groupId