fixed bug with calculating loyalty discount
This commit is contained in:
parent
50353bb810
commit
e9a9922d6d
4 changed files with 62 additions and 43 deletions
|
@ -36,11 +36,9 @@ class OrderProduct extends AbstractApiModel
|
|||
/**
|
||||
* Внешние идентификаторы позиции в заказе
|
||||
*
|
||||
* @deprecated
|
||||
*
|
||||
* @var array $externalIds
|
||||
*
|
||||
* @Mapping\Type("array")
|
||||
* @Mapping\Type("array<Intaro\RetailCrm\Model\Api\CodeValueModel>")
|
||||
* @Mapping\SerializedName("externalIds")
|
||||
*/
|
||||
public $externalIds;
|
||||
|
@ -64,7 +62,17 @@ class OrderProduct extends AbstractApiModel
|
|||
* @Mapping\SerializedName("initialPrice")
|
||||
*/
|
||||
public $initialPrice;
|
||||
|
||||
|
||||
/**
|
||||
* Количество
|
||||
*
|
||||
* @var float $quantity
|
||||
*
|
||||
* @Mapping\Type("float")
|
||||
* @Mapping\SerializedName("quantity")
|
||||
*/
|
||||
public $quantity;
|
||||
|
||||
/**
|
||||
* Итоговая денежная скидка на единицу товара c учетом всех скидок на товар и заказ
|
||||
*
|
||||
|
|
|
@ -54,8 +54,6 @@ class OrderLoyaltyData
|
|||
*/
|
||||
public $itemId;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ID проверочного кода
|
||||
*
|
||||
|
|
|
@ -106,33 +106,51 @@ class OrderLoyaltyDataRepository extends AbstractRepository
|
|||
|
||||
/**
|
||||
* @param $orderId
|
||||
* @return OrderLoyaltyData[]|null
|
||||
*
|
||||
* @return OrderLoyaltyData[]
|
||||
*/
|
||||
public function getProductsByOrderId($orderId): ?array
|
||||
public function getProductsByOrderId($orderId): array
|
||||
{
|
||||
$products = $this->getHlRowByOrderId($orderId);
|
||||
|
||||
if (count($products) === 0 || false === $products) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($products as $product) {
|
||||
$result[$product['UF_ITEM_POS_ID']]
|
||||
= Deserializer::deserializeArray($product, OrderLoyaltyData::class);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $orderId
|
||||
*
|
||||
* @return array|false //fetchAll может вернуть false
|
||||
*/
|
||||
private function getHlRowByOrderId(int $orderId)
|
||||
{
|
||||
try {
|
||||
if ($this->dataManager === null) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
||||
$products = $this->dataManager::query()->setSelect(['*'])->where('UF_ORDER_ID', '=', $orderId)->fetchAll();
|
||||
|
||||
if ($products === false || count($products) === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($products as $product) {
|
||||
$result[] = Deserializer::deserializeArray($product, OrderLoyaltyData::class);
|
||||
}
|
||||
|
||||
return $result;
|
||||
return $this->dataManager::query()
|
||||
->setSelect(['*'])
|
||||
->where('UF_ORDER_ID', '=', $orderId)
|
||||
->fetchAll();
|
||||
|
||||
} catch (SystemException | Exception $exception) {
|
||||
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData $position
|
||||
* @return bool
|
||||
|
|
|
@ -30,7 +30,8 @@ use Intaro\RetailCrm\Component\Constants;
|
|||
use Intaro\RetailCrm\Component\Factory\ClientFactory;
|
||||
use Intaro\RetailCrm\Component\Handlers\EventsHandlers;
|
||||
use Intaro\RetailCrm\Component\ServiceLocator;
|
||||
use Intaro\RetailCrm\Model\Api\OrderProduct;
|
||||
use Intaro\RetailCrm\Model\Api\CodeValueModel;
|
||||
use Intaro\RetailCrm\Model\Api\Order\OrderProduct;
|
||||
use Intaro\RetailCrm\Model\Api\Response\Order\Loyalty\OrderLoyaltyApplyResponse;
|
||||
use Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData;
|
||||
use Intaro\RetailCrm\Repository\OrderLoyaltyDataRepository;
|
||||
|
@ -160,7 +161,7 @@ class OrderLoyaltyDataService
|
|||
|
||||
$response = $client->getOrder($orderId);
|
||||
|
||||
if ($response === null) {
|
||||
if ($response === null || !is_array($response->order->items)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -172,25 +173,19 @@ class OrderLoyaltyDataService
|
|||
}
|
||||
|
||||
$repository = new OrderLoyaltyDataRepository();
|
||||
$items = $repository->getProductsByOrderId($orderId);
|
||||
|
||||
$bitrixDiscounts = 0;
|
||||
$totalPrice = 0;
|
||||
$totalBasePrice = 0;
|
||||
|
||||
/** @var BasketItemBase $basketItem */
|
||||
foreach ($order->getBasket() as $basketItem) {
|
||||
$totalPrice += $basketItem->getPrice() * $basketItem->getQuantity();
|
||||
$totalBasePrice += $basketItem->getBasePrice() * $basketItem->getQuantity();
|
||||
$bitrixItems = $repository->getProductsByOrderId($orderId);
|
||||
$loyaltyDiscount = 0;
|
||||
|
||||
/** @var OrderProduct $item */
|
||||
foreach ($response->order->items as $item) {
|
||||
/** @var CodeValueModel $itemBitrixId */
|
||||
$itemBitrixId = $item->externalIds[0];
|
||||
/** @var OrderLoyaltyData $bitrixItem */
|
||||
$bitrixItem = $bitrixItems[$itemBitrixId->value];
|
||||
|
||||
$loyaltyDiscount += ($item->discountTotal - $bitrixItem->defaultDiscount) * $item->quantity;
|
||||
}
|
||||
|
||||
/** @var OrderLoyaltyData $item */
|
||||
foreach ($items as $item) {
|
||||
$bitrixDiscounts += $item->defaultDiscount * $item->quantity;
|
||||
}
|
||||
|
||||
$loyaltyDiscount = $totalBasePrice - $totalPrice - $bitrixDiscounts;
|
||||
|
||||
|
||||
$this->saveBonusAndDiscToOrderProps(
|
||||
$order->getPropertyCollection(),
|
||||
$loyaltyDiscount ?? 0.0,
|
||||
|
|
Loading…
Add table
Reference in a new issue