Skip to content

Commit 6ef2b46

Browse files
committed
Merge branch 'MC-32658-tax' of github.com:magento-honey-badgers/magento2ce into MC-32658-tax
2 parents b72d24a + fe88f47 commit 6ef2b46

File tree

3 files changed

+167
-11
lines changed

3 files changed

+167
-11
lines changed

app/code/Magento/SalesGraphQl/Model/Resolver/OrderItem/DataProvider.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Sales\Api\Data\OrderItemInterface;
1515
use Magento\Sales\Api\OrderItemRepositoryInterface;
1616
use Magento\Sales\Api\OrderRepositoryInterface;
17+
use Magento\Sales\Model\Order;
1718

1819
/**
1920
* Data provider for order items
@@ -137,6 +138,7 @@ private function fetch()
137138
'product_sku' => $orderItem->getSku(),
138139
'product_url_key' => $associatedProduct ? $associatedProduct->getUrlKey() : null,
139140
'product_type' => $orderItem->getProductType(),
141+
'discounts' => $this->getDiscountDetails($associatedOrder, $orderItem),
140142
'product_sale_price' => [
141143
'value' => $orderItem->getPrice(),
142144
'currency' => $associatedOrder->getOrderCurrencyCode()
@@ -158,6 +160,7 @@ private function fetch()
158160
'product_sku' => $orderItem->getSku(),
159161
'product_url_key' => $associatedProduct ? $associatedProduct->getUrlKey() : null,
160162
'product_type' => $orderItem->getProductType(),
163+
'discounts' => $this->getDiscountDetails($associatedOrder, $orderItem),
161164
'product_sale_price' => [
162165
'value' => $orderItem->getPrice(),
163166
'currency' => $associatedOrder->getOrderCurrencyCode()
@@ -172,7 +175,6 @@ private function fetch()
172175
'quantity_returned' => $orderItem->getQtyReturned(),
173176
];
174177
}
175-
176178
}
177179

178180
return $this->orderItemList;
@@ -230,4 +232,26 @@ function ($orderItem) {
230232
}
231233
return $orderList;
232234
}
235+
236+
/**
237+
* Returns information about an applied discount
238+
*
239+
* @param OrderInterface $associatedOrder
240+
* @param OrderItemInterface $orderItem
241+
* @return array|null
242+
*/
243+
private function getDiscountDetails(OrderInterface $associatedOrder, OrderItemInterface $orderItem)
244+
{
245+
if ($associatedOrder->getDiscountDescription() === null && $orderItem->getDiscountAmount() == 0
246+
&& $associatedOrder->getDiscountAmount() == 0
247+
) {
248+
return null;
249+
}
250+
251+
$discounts [] = [
252+
'label' => $associatedOrder->getDiscountDescription() ?? "null",
253+
'amount' => ['value' => $orderItem->getDiscountAmount() ?? 0, 'currency' => $associatedOrder->getOrderCurrencyCode()]
254+
];
255+
return $discounts;
256+
}
233257
}

app/code/Magento/SalesGraphQl/Model/Resolver/OrderTotal.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function resolve(
8888
*/
8989
private function getShippingDiscountDetails(Order $order)
9090
{
91-
if ($order->getDiscountDescription() === null && $order->getShippingDiscountAmount() === 0) {
91+
if ($order->getDiscountDescription() === null && $order->getShippingDiscountAmount() == 0) {
9292
return null;
9393
}
9494

@@ -111,13 +111,16 @@ private function getShippingDiscountDetails(Order $order)
111111
*/
112112
private function getDiscountDetails(Order $order)
113113
{
114-
if ($order->getDiscountDescription() === null && $order->getDiscountAmount() === 0) {
114+
if ($order->getDiscountDescription() === null && $order->getDiscountAmount() == 0) {
115115
return null;
116116
}
117117

118118
$discounts [] = [
119119
'label' => $order->getDiscountDescription() ?? "null",
120-
'amount' => ['value' => $order->getDiscountAmount(), 'currency' => $order->getOrderCurrencyCode()]
120+
'amount' => [
121+
'value' => $order->getDiscountAmount(),
122+
'currency' => $order->getOrderCurrencyCode()
123+
]
121124
];
122125
return $discounts;
123126
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Sales/RetrieveOrdersByOrderNumberTest.php

Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Catalog\Model\Product;
1313
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1414
use Magento\Framework\Api\SearchCriteriaBuilder;
15+
use Magento\Framework\Exception\AuthenticationException;
1516
use Magento\Integration\Api\CustomerTokenServiceInterface;
1617
use Magento\Sales\Api\OrderRepositoryInterface;
1718
use Magento\Sales\Model\Order;
@@ -247,6 +248,7 @@ public function testGetMatchingCustomerOrders()
247248
$this->assertArrayHasKey('total_count', $response['customer']['orders']);
248249
$this->assertEquals(6, $response['customer']['orders']['total_count']);
249250
}
251+
250252
/**
251253
* @magentoApiDataFixture Magento/Customer/_files/customer.php
252254
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
@@ -470,6 +472,7 @@ public function testGetCustomerOrdersWithWrongCustomer()
470472

471473
/**
472474
* @param String $orderNumber
475+
* @throws AuthenticationException
473476
* @dataProvider dataProviderIncorrectOrder
474477
* @magentoApiDataFixture Magento/Customer/_files/customer.php
475478
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
@@ -568,7 +571,7 @@ public function dataProviderIncorrectOrder(): array
568571
* @param String $orderNumber
569572
* @param String $store
570573
* @param int $expectedCount
571-
* @throws \Magento\Framework\Exception\AuthenticationException
574+
* @throws AuthenticationException
572575
* @dataProvider dataProviderMultiStores
573576
* @magentoApiDataFixture Magento/Customer/_files/customer.php
574577
* @magentoApiDataFixture Magento/GraphQl/Sales/_files/two_orders_with_order_items_two_storeviews.php
@@ -689,11 +692,136 @@ public function testCustomerOrderWithTaxesAndDiscountsOnShippingAndTotal()
689692
$this->setPaymentMethod($cartId, $paymentMethod);
690693
$orderNumber = $this->placeOrder($cartId);
691694
$customerOrderResponse = $this->getCustomerOrderQuery($orderNumber);
695+
// Asserting discounts on order item level
696+
$this->assertEquals(
697+
4,
698+
$customerOrderResponse[0]['items'][0]['discounts'][0]['amount']['value']
699+
);
700+
$this->assertEquals(
701+
'USD',
702+
$customerOrderResponse[0]['items'][0]['discounts'][0]['amount']['currency']
703+
);
704+
$this->assertEquals(
705+
'null',
706+
$customerOrderResponse[0]['items'][0]['discounts'][0]['label']
707+
);
692708
$customerOrderItem = $customerOrderResponse[0];
693-
//TODO: once discounts are calculated, order Totals can be verified
709+
$this->assertTotalsWithTaxesAndDiscountsOnShippingAndTotal($customerOrderItem);
694710
$this->deleteOrder();
695711
}
696712

713+
/**
714+
* Assert order totals including shipping_handling and taxes
715+
*
716+
* @param array $customerOrderItem
717+
*/
718+
private function assertTotalsWithTaxesAndDiscountsOnShippingAndTotal(array $customerOrderItem): void
719+
{
720+
$this->assertEquals(
721+
58.05,
722+
$customerOrderItem['total']['base_grand_total']['value']
723+
);
724+
725+
$this->assertEquals(
726+
58.05,
727+
$customerOrderItem['total']['grand_total']['value']
728+
);
729+
$this->assertEquals(
730+
40,
731+
$customerOrderItem['total']['subtotal']['value']
732+
);
733+
$this->assertEquals(
734+
4.05,
735+
$customerOrderItem['total']['total_tax']['value']
736+
);
737+
738+
$this->assertEquals(
739+
20,
740+
$customerOrderItem['total']['total_shipping']['value']
741+
);
742+
$this->assertEquals(
743+
1.35,
744+
$customerOrderItem['total']['taxes'][0]['amount']['value']
745+
);
746+
$this->assertEquals(
747+
'USD',
748+
$customerOrderItem['total']['taxes'][0]['amount']['currency']
749+
);
750+
$this->assertEquals(
751+
'US-TEST-*-Rate-1',
752+
$customerOrderItem['total']['taxes'][0]['title']
753+
);
754+
$this->assertEquals(
755+
7.5,
756+
$customerOrderItem['total']['taxes'][0]['rate']
757+
);
758+
$this->assertEquals(
759+
2.7,
760+
$customerOrderItem['total']['taxes'][1]['amount']['value']
761+
);
762+
$this->assertEquals(
763+
'USD',
764+
$customerOrderItem['total']['taxes'][1]['amount']['currency']
765+
);
766+
$this->assertEquals(
767+
'US-TEST-*-Rate-1',
768+
$customerOrderItem['total']['taxes'][1]['title']
769+
);
770+
$this->assertEquals(
771+
7.5,
772+
$customerOrderItem['total']['taxes'][1]['rate']
773+
);
774+
$this->assertEquals(
775+
21.5,
776+
$customerOrderItem['total']['shipping_handling']['amount_including_tax']['value']
777+
);
778+
$this->assertEquals(
779+
20,
780+
$customerOrderItem['total']['shipping_handling']['amount_excluding_tax']['value']
781+
);
782+
$this->assertEquals(
783+
20,
784+
$customerOrderItem['total']['shipping_handling']['total_amount']['value']
785+
);
786+
787+
$this->assertEquals(
788+
1.35,
789+
$customerOrderItem['total']['shipping_handling']['taxes'][0]['amount']['value']
790+
);
791+
$this->assertEquals(
792+
'US-TEST-*-Rate-1',
793+
$customerOrderItem['total']['shipping_handling']['taxes'][0]['title']
794+
);
795+
$this->assertEquals(
796+
7.5,
797+
$customerOrderItem['total']['shipping_handling']['taxes'][0]['rate']
798+
);
799+
$this->assertEquals(
800+
2,
801+
$customerOrderItem['total']['shipping_handling']['discounts'][0]['amount']['value']
802+
);
803+
$this->assertEquals(
804+
'USD',
805+
$customerOrderItem['total']['shipping_handling']['discounts'][0]['amount']['currency']
806+
);
807+
$this->assertEquals(
808+
'null',
809+
$customerOrderItem['total']['shipping_handling']['discounts'][0]['label']
810+
);
811+
$this->assertEquals(
812+
-6,
813+
$customerOrderItem['total']['discounts'][0]['amount']['value']
814+
);
815+
$this->assertEquals(
816+
'USD',
817+
$customerOrderItem['total']['discounts'][0]['amount']['currency']
818+
);
819+
$this->assertEquals(
820+
'null',
821+
$customerOrderItem['total']['discounts'][0]['label']
822+
);
823+
}
824+
697825
/**
698826
* Verify that the customer order has the tax information on shipping and totals
699827
* @magentoApiDataFixture Magento/Customer/_files/customer.php
@@ -865,7 +993,7 @@ private function addProductToCart(string $cartId, float $qty, string $sku): void
865993
* @param string $sku
866994
* @param int $optionId
867995
* @param int $selectionId
868-
* @throws \Magento\Framework\Exception\AuthenticationException
996+
* @throws AuthenticationException
869997
*/
870998
public function addBundleProductToCart(string $cartId, float $qty, string $sku, int $optionId1, int $selectionId1,int $optionId2, int $selectionId2)
871999
{
@@ -1100,22 +1228,24 @@ private function getCustomerOrderQuery($orderNumber):array
11001228
number
11011229
order_date
11021230
status
1103-
items{product_name product_sku quantity_ordered}
1231+
items{product_name product_sku quantity_ordered discounts {amount{value currency} label}}
11041232
total {
11051233
base_grand_total{value currency}
11061234
grand_total{value currency}
11071235
total_tax{value}
11081236
subtotal { value currency }
11091237
taxes {amount{value currency} title rate}
1238+
discounts {amount{value currency} label}
11101239
total_shipping{value}
11111240
shipping_handling
11121241
{
11131242
amount_including_tax{value}
11141243
amount_excluding_tax{value}
11151244
total_amount{value currency}
11161245
taxes {amount{value} title rate}
1246+
discounts {amount{value currency} label}
11171247
}
1118-
discounts {amount{value currency} label}
1248+
11191249
}
11201250
}
11211251
}
@@ -1137,7 +1267,7 @@ private function getCustomerOrderQuery($orderNumber):array
11371267
*
11381268
* @param $orderNumber
11391269
* @return mixed
1140-
* @throws \Magento\Framework\Exception\AuthenticationException
1270+
* @throws AuthenticationException
11411271
*/
11421272
private function getCustomerOrderQueryBundleProduct($orderNumber)
11431273
{
@@ -1182,7 +1312,6 @@ private function getCustomerOrderQueryBundleProduct($orderNumber)
11821312
total_amount{value}
11831313
taxes {amount{value} title rate}
11841314
}
1185-
11861315
}
11871316
}
11881317
}

0 commit comments

Comments
 (0)