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

add real LP bonus calculate

This commit is contained in:
Sergey Chazov 2020-10-26 18:29:18 +03:00
parent 3610fdf1f9
commit 5e4dfda6e7
6 changed files with 60 additions and 31 deletions

View file

@ -2989,6 +2989,22 @@ class ApiClient
);
}
/**
* @param array $request
* @return \RetailCrm\Response\ApiResponse
*/
public function loyaltyOrderCalculate(array $request): ApiResponse
{
return $this->client->makeRequest(
"/loyalty/calculate",
Client::METHOD_POST,
[
'site'=> json_encode($request['site']),
'order'=> json_encode($request['order'])
]
);
}
/**
* @return \RetailCrm\Response\ApiResponse
*/

View file

@ -2,6 +2,7 @@
use Bitrix\Currency\CurrencyLangTable;
use Bitrix\Main\Loader;
use Bitrix\Main\LoaderException;
use Intaro\RetailCrm\Component\ConfigProvider;
use Intaro\RetailCrm\Service\LoyaltyService;
@ -15,36 +16,37 @@ if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) {
* @var SaleOrderAjax $component
*/
Loader::includeModule('intaro.retailcrm');
$arResult['LOYALTY_STATUS'] = ConfigProvider::getLoyaltyProgramStatus();
try {
Loader::includeModule('intaro.retailcrm');
} catch (LoaderException $exception) {
AddMessage2Log($exception->getMessage());
}
$arResult['LOYALTY_STATUS'] = ConfigProvider::getLoyaltyProgramStatus();
$arResult['PERSONAL_LOYALTY_STATUS'] = LoyaltyService::getLoyaltyPersonalStatus();
//TODO Закомментированно до появления реального апи
//TODO добавить проверку на участие покупателя в программе лояльности (таска 68813)
/*$service = new LoyaltyService();
$calculate = $service->calculateBonus($arResult['BASKET_ITEMS'], $arResult['DISCOUNT_PRICE'], $arResult['DISCOUNT_PERCENT']);
if ($arResult['LOYALTY_STATUS'] === 'Y' && $arResult['PERSONAL_LOYALTY_STATUS'] === true) {
$service = new LoyaltyService();
$calculate = $service->calculateBonus($arResult['BASKET_ITEMS'], $arResult['DISCOUNT_PRICE'], $arResult['DISCOUNT_PERCENT']);
if ($calculate->success) {
$arResult['AVAILABLE_BONUSES'] = $calculate->order->bonusesChargeTotal;
$arResult['TOTAL_BONUSES_COUNT'] = $calculate->order->loyaltyAccount->amount;
$arResult['LP_CALCULATE_SUCCESS'] = $calculate->success;
$arResult['WILL_BE_CREDITED'] = $calculate->order->bonusesCreditTotal;
}*/
//TODO убрать заглушку после появления реальных методов
$arResult['LP_CALCULATE_SUCCESS'] = true;
$arResult['AVAILABLE_BONUSES'] = 300; //доступно
$arResult['TOTAL_BONUSES_COUNT'] = 600; //всего на счету
$arResult['WILL_BE_CREDITED'] = 245; //будет начислено
$component = $this->__component;
$component::scaleImages($arResult['JS_DATA'], $arParams['SERVICES_IMAGES_SCALING']);
if ($calculate->success) {
$arResult['AVAILABLE_BONUSES'] = $calculate->order->bonusesChargeTotal;
$arResult['TOTAL_BONUSES_COUNT'] = $calculate->order->loyaltyAccount->amount;
$arResult['LP_CALCULATE_SUCCESS'] = $calculate->success;
$arResult['WILL_BE_CREDITED'] = $calculate->order->bonusesCreditTotal;
}
$currency = CurrencyLangTable::query()
->setSelect(['FORMAT_STRING'])
->where([
['CURRENCY', '=', RetailcrmConfigProvider::getCurrencyOrDefault()],
['LID', '=', 'LANGUAGE_ID'],
])
->fetch();
$arResult['BONUS_CURRENCY'] = $currency['FORMAT_STRING'];
$component = $this->__component;
$component::scaleImages($arResult['JS_DATA'], $arParams['SERVICES_IMAGES_SCALING']);
$currency = CurrencyLangTable::query()
->setSelect(['FORMAT_STRING'])
->where([
['CURRENCY', '=', RetailcrmConfigProvider::getCurrencyOrDefault()],
['LID', '=', 'LANGUAGE_ID'],
])
->fetch();
$arResult['BONUS_CURRENCY'] = $currency['FORMAT_STRING'];
}

View file

@ -75,7 +75,7 @@ trait LoyaltyTrait
public function loyaltyCalculate(LoyaltyCalculateRequest $request): ?LoyaltyCalculateResponse
{
$serialized = Serializer::serializeArray($request);
$response = $this->client->loyaltyOrderApply($serialized);
$response = $this->client->loyaltyOrderCalculate($serialized);
return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyCalculateResponse::class);
}

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class OrderProduct
*

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class PriceType
*

View file

@ -25,10 +25,14 @@ use CUser;
use Exception;
use Intaro\RetailCrm\Component\Factory\ClientFactory;
use Intaro\RetailCrm\Component\ServiceLocator;
use Intaro\RetailCrm\Model\Api\PriceType;
use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountActivateRequest;
use Intaro\RetailCrm\Model\Api\Request\Loyalty\LoyaltyCalculateRequest;
use Intaro\RetailCrm\Model\Api\Request\Order\Loyalty\OrderLoyaltyApplyRequest;
use Intaro\RetailCrm\Model\Api\SerializedOrder;
use Intaro\RetailCrm\Model\Api\SerializedOrderProduct;
use Intaro\RetailCrm\Model\Api\SerializedOrderProductOffer;
use Intaro\RetailCrm\Model\Api\SerializedRelationCustomer;
use Intaro\RetailCrm\Repository\PaySystemActionRepository;
/**
@ -99,8 +103,10 @@ class LoyaltyService
public function calculateBonus(array $basketItems, int $discountPrice, int $discountPercent)
{
global $USER;
$request = new LoyaltyCalculateRequest();
$request->order = new SerializedOrder();
$request->order->customer = new SerializedRelationCustomer();
$request->order->customer->id = $USER->GetID();
$request->order->customer->externalId = $USER->GetID();
@ -129,6 +135,7 @@ class LoyaltyService
}
$product->initialPrice = $item['PRICE'];
$product->offer = new SerializedOrderProductOffer();
$product->offer->externalId = $item['ID'];
$product->offer->id = $item['ID'];
$product->offer->xmlId = $item['XML_ID'];
@ -143,7 +150,7 @@ class LoyaltyService
]
)
->fetch();
$product->priceType = new PriceType();
$product->priceType->code = $price['NAME'];
} catch (ObjectPropertyException | ArgumentException | SystemException $e) {
AddMessage2Log('GroupTable query error: ' . $e->getMessage());