Skip to content

Commit 5754745

Browse files
committed
MAGE-1205: handle tax config
1 parent fa06ecb commit 5754745

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

Service/Insights/EventProcessor.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use Magento\Sales\Model\Order;
1212
use Magento\Sales\Model\Order\Item as OrderItem;
1313
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Tax\Model\Config as TaxConfig;
1415

1516
class EventProcessor implements EventProcessorInterface
1617
{
1718
/** @var string */
1819
protected const NO_QUERY_ID_KEY = '__NO_QUERY_ID__';
1920

2021
public function __construct(
22+
protected TaxConfig $taxConfig,
2123
protected ?InsightsClient $client = null,
2224
protected ?string $userToken = null,
2325
protected ?string $authenticatedUserToken = null,
@@ -268,7 +270,9 @@ protected function getQuoteItemDiscount(Item $item): float
268270
*/
269271
protected function getOrderItemSalePrice(OrderItem $item): float
270272
{
271-
return floatval($item->getPriceInclTax()) - $this->getOrderItemCartDiscount($item);
273+
return $this->taxConfig->priceIncludesTax($this->storeManager->getStore()->getId()) ?
274+
floatval($item->getPriceInclTax()) - $this->getOrderItemCartDiscount($item):
275+
floatval($item->getPrice()) - $this->getOrderItemCartDiscount($item);
272276
}
273277

274278
/**
@@ -286,7 +290,9 @@ protected function getOrderItemCartDiscount(OrderItem $item): float
286290
*/
287291
protected function getOrderItemDiscount(OrderItem $item): float
288292
{
289-
$itemDiscount = floatval($item->getOriginalPrice()) - floatval($item->getPriceInclTax());
293+
$itemDiscount = $this->taxConfig->priceIncludesTax($this->storeManager->getStore()->getId()) ?
294+
floatval($item->getOriginalPrice()) - floatval($item->getPriceInclTax()) :
295+
floatval($item->getOriginalPrice()) - floatval($item->getPrice());
290296
return $itemDiscount + $this->getOrderItemCartDiscount($item);
291297
}
292298

@@ -303,7 +309,7 @@ protected function getObjectDataForPurchase(array $items): array
303309
return array_map(function($item) {
304310
return [
305311
'price' => $this->getOrderItemSalePrice($item),
306-
'discount' => $this->getOrderItemDiscount($item),
312+
'discount' => max(0, $this->getOrderItemDiscount($item)),
307313
'quantity' => intval($item->getQtyOrdered())
308314
];
309315
}, $items);

Test/Unit/Service/EventProcessorTest.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
use Algolia\AlgoliaSearch\Api\InsightsClient;
66
use Algolia\AlgoliaSearch\Service\Insights\EventProcessor;
77
use Magento\Sales\Model\Order\Item as OrderItem;
8+
use Magento\Store\Model\Store;
89
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Tax\Model\Config as TaxConfig;
911
use PHPUnit\Framework\TestCase;
1012

1113
class EventProcessorTest extends TestCase
1214
{
15+
protected ?TaxConfig $taxConfig;
1316
protected ?InsightsClient $client;
1417
protected ?string $userToken;
1518
protected ?string $authenticatedUserToken;
@@ -23,7 +26,13 @@ protected function setUp(): void
2326
$this->authenticatedUserToken = 'authenticated-foo';
2427
$this->storeManager = $this->createMock(StoreManagerInterface::class);
2528

29+
$store = $this->createMock(Store::class);
30+
$store->method('getId')->willReturn(1);
31+
$this->storeManager->method('getStore')->willReturn($store);
32+
$this->taxConfig = $this->createMock(TaxConfig::class);
33+
2634
$this->eventProcessor = new EventProcessorTestable(
35+
$this->taxConfig,
2736
$this->client,
2837
$this->userToken,
2938
$this->authenticatedUserToken,
@@ -34,8 +43,10 @@ protected function setUp(): void
3443
/**
3544
* @dataProvider orderItemsProvider
3645
*/
37-
public function testObjectDataForPurchase($orderItemsData, $expectedResult, $expectedTotalRevenue): void
46+
public function testObjectDataForPurchase($priceIncludesTax, $orderItemsData, $expectedResult, $expectedTotalRevenue): void
3847
{
48+
$this->taxConfig->method('priceIncludesTax')->willReturn($priceIncludesTax);
49+
3950
$orderItems = [];
4051

4152
foreach ($orderItemsData as $orderItemData) {
@@ -61,8 +72,10 @@ public function orderItemsProvider(): array
6172
{
6273
return [
6374
[ // One item
75+
'priceIncludesTax' => true,
6476
'orderItemsData' => [
6577
[
78+
'getPrice' => 32.00,
6679
'getPriceInclTax' => 32.00,
6780
'getOriginalPrice' => 32.00,
6881
'getDiscountAmount' => 0.00,
@@ -78,9 +91,31 @@ public function orderItemsProvider(): array
7891
],
7992
'expectedTotalRevenue' => 32.00
8093
],
94+
[ // One item (tax excluded)
95+
'priceIncludesTax' => false,
96+
'orderItemsData' => [
97+
[
98+
'getPrice' => 25.00,
99+
'getPriceInclTax' => 32.00,
100+
'getOriginalPrice' => 25.00,
101+
'getDiscountAmount' => 0.00,
102+
'getQtyOrdered' => 1,
103+
]
104+
],
105+
'expectedResult' => [
106+
[
107+
'price' => 25.00,
108+
'discount' => 0.00,
109+
'quantity' => 1,
110+
]
111+
],
112+
'expectedTotalRevenue' => 25.00
113+
],
81114
[ // One item with discount
115+
'priceIncludesTax' => true,
82116
'orderItemsData' => [
83117
[
118+
'getPrice' => 32.00,
84119
'getPriceInclTax' => 32.00,
85120
'getOriginalPrice' => 32.00,
86121
'getDiscountAmount' => 7.00,
@@ -96,15 +131,38 @@ public function orderItemsProvider(): array
96131
],
97132
'expectedTotalRevenue' => 25.00
98133
],
134+
[ // One item with discount (tax excluded)
135+
'priceIncludesTax' => false,
136+
'orderItemsData' => [
137+
[
138+
'getPrice' => 25.00,
139+
'getPriceInclTax' => 32.00,
140+
'getOriginalPrice' => 25.00,
141+
'getDiscountAmount' => 7.00,
142+
'getQtyOrdered' => 1,
143+
]
144+
],
145+
'expectedResult' => [
146+
[
147+
'price' => 18.00,
148+
'discount' => 7.00,
149+
'quantity' => 1,
150+
]
151+
],
152+
'expectedTotalRevenue' => 18.00
153+
],
99154
[ // Two items
155+
'priceIncludesTax' => true,
100156
'orderItemsData' => [
101157
[
158+
'getPrice' => 32.00,
102159
'getPriceInclTax' => 32.00,
103160
'getOriginalPrice' => 32.00,
104161
'getDiscountAmount' => 7.00,
105162
'getQtyOrdered' => 1,
106163
],
107164
[
165+
'getPrice' => 32.00,
108166
'getPriceInclTax' => 32.00,
109167
'getOriginalPrice' => 32.00,
110168
'getDiscountAmount' => 0.00,

0 commit comments

Comments
 (0)