Skip to content

Commit 0b33f28

Browse files
committed
MC:32658:MyAccount :: Order Details :: Order Details by Order Number Taxes and Discounts
- Added new test case and static fixes on code
1 parent 348e904 commit 0b33f28

File tree

5 files changed

+208
-56
lines changed

5 files changed

+208
-56
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,13 @@ private function getAppliedTaxesDetails(OrderInterface $order, array $appliedTax
188188
* @param array $appliedShippingTaxesForItemsData
189189
* @return array
190190
*/
191-
private function getAppliedShippingTaxesDetails(OrderInterface $order, array $appliedShippingTaxesForItemsData): array
192-
{
191+
private function getAppliedShippingTaxesDetails(
192+
OrderInterface $order,
193+
array $appliedShippingTaxesForItemsData
194+
): array {
193195
$shippingTaxes = [];
194-
foreach ($appliedShippingTaxesForItemsData as $appliedTaxesKeyIndex => $appliedShippingTaxes) {
195-
foreach ($appliedShippingTaxes as $key => $appliedShippingTax) {
196+
foreach ($appliedShippingTaxesForItemsData as $appliedShippingTaxes) {
197+
foreach ($appliedShippingTaxes as $appliedShippingTax) {
196198
$appliedShippingTaxesArray = [
197199
'title' => $appliedShippingTax['title'] ?? null,
198200
'amount' => [

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

Lines changed: 105 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,12 @@ public function testCustomerOrdersSimpleProductWithTaxesAndDiscounts()
169169
private function assertTotalsWithTaxesAndDiscounts(array $customerOrderItemTotal): void
170170
{
171171
$this->assertCount(1, $customerOrderItemTotal['taxes']);
172-
$expectedProductAndShippingTaxes = [4.05];
173-
$totalTaxes = [];
174-
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
175-
array_push($totalTaxes, $totalTaxFromResponse['amount']['value']);
176-
}
177-
foreach ($totalTaxes as $value) {
178-
$this->assertTrue(in_array($value, $expectedProductAndShippingTaxes));
179-
}
180-
foreach ($customerOrderItemTotal['taxes'] as $taxData) {
181-
$this->assertEquals('USD', $taxData['amount']['currency']);
182-
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
183-
$this->assertEquals(7.5, $taxData['rate']);
184-
}
172+
$taxData = $customerOrderItemTotal['taxes'][0];
173+
$this->assertEquals('USD', $taxData['amount']['currency']);
174+
$this->assertEquals(4.05, $taxData['amount']['value']);
175+
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
176+
$this->assertEquals(7.5, $taxData['rate']);
177+
185178
unset($customerOrderItemTotal['taxes']);
186179
$assertionMap = [
187180
'base_grand_total' => ['value' => 58.05, 'currency' =>'USD'],
@@ -215,6 +208,93 @@ private function assertTotalsWithTaxesAndDiscounts(array $customerOrderItemTotal
215208
$this->assertResponseFields($customerOrderItemTotal, $assertionMap);
216209
}
217210

211+
/**
212+
* Verify the customer order with tax, discount with shipping tax class set for calculation setting
213+
*
214+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
215+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
216+
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
217+
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_al.php
218+
* @magentoApiDataFixture Magento/SalesRule/_files/cart_rule_10_percent_off_with_discount_on_shipping.php
219+
* @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_calculation_shipping_excludeTax_order_display_settings.php
220+
*/
221+
public function testCustomerOrdersSimpleProductWithTaxesAndDiscountsWithTwoRules()
222+
{
223+
$quantity = 4;
224+
$sku = 'simple1';
225+
$cartId = $this->createEmptyCart();
226+
$this->addProductToCart($cartId, $quantity, $sku);
227+
$this->setBillingAddress($cartId);
228+
$shippingMethod = $this->setShippingAddress($cartId);
229+
$paymentMethod = $this->setShippingMethod($cartId, $shippingMethod);
230+
$this->setPaymentMethod($cartId, $paymentMethod);
231+
$orderNumber = $this->placeOrder($cartId);
232+
$customerOrderResponse = $this->getCustomerOrderQuery($orderNumber);
233+
// Asserting discounts on order item level
234+
$this->assertEquals(4, $customerOrderResponse[0]['items'][0]['discounts'][0]['amount']['value']);
235+
$this->assertEquals('USD', $customerOrderResponse[0]['items'][0]['discounts'][0]['amount']['currency']);
236+
$this->assertEquals('Discount', $customerOrderResponse[0]['items'][0]['discounts'][0]['label']);
237+
$customerOrderItem = $customerOrderResponse[0];
238+
$this->assertTotalsWithTaxesAndDiscountsWithTwoRules($customerOrderItem['total']);
239+
$this->deleteOrder();
240+
}
241+
242+
/**
243+
* @param array $customerOrderItemTotal
244+
*/
245+
private function assertTotalsWithTaxesAndDiscountsWithTwoRules(array $customerOrderItemTotal): void
246+
{
247+
$this->assertCount(2, $customerOrderItemTotal['taxes']);
248+
$taxData = $customerOrderItemTotal['taxes'][0];
249+
$this->assertEquals('USD', $taxData['amount']['currency']);
250+
$this->assertEquals(4.05, $taxData['amount']['value']);
251+
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
252+
$this->assertEquals(7.5, $taxData['rate']);
253+
254+
$secondTaxData = $customerOrderItemTotal['taxes'][1];
255+
$this->assertEquals('USD', $secondTaxData['amount']['currency']);
256+
$this->assertEquals(2.97, $secondTaxData['amount']['value']);
257+
$this->assertEquals('US-AL-*-Rate-1', $secondTaxData['title']);
258+
$this->assertEquals(5.5, $secondTaxData['rate']);
259+
260+
unset($customerOrderItemTotal['taxes']);
261+
$assertionMap = [
262+
'base_grand_total' => ['value' => 61.02, 'currency' =>'USD'],
263+
'grand_total' => ['value' => 61.02, 'currency' =>'USD'],
264+
'subtotal' => ['value' => 40, 'currency' =>'USD'],
265+
'total_tax' => ['value' => 7.02, 'currency' =>'USD'],
266+
'total_shipping' => ['value' => 20, 'currency' =>'USD'],
267+
'shipping_handling' => [
268+
'amount_including_tax' => ['value' => 22.6],
269+
'amount_excluding_tax' => ['value' => 20],
270+
'total_amount' => ['value' => 20, 'currency' =>'USD'],
271+
'discounts' => [
272+
0 => ['amount'=>['value'=> 2, 'currency' =>'USD'],
273+
'label' => 'Discount'
274+
]
275+
],
276+
'taxes'=> [
277+
0 => [
278+
'amount'=>['value' => 1.35],
279+
'title' => 'US-TEST-*-Rate-1',
280+
'rate' => 7.5
281+
],
282+
1 => [
283+
'amount'=>['value' => 0.99],
284+
'title' => 'US-AL-*-Rate-1',
285+
'rate' => 5.5
286+
]
287+
]
288+
],
289+
'discounts' => [
290+
0 => ['amount' => [ 'value' => -6, 'currency' =>'USD'],
291+
'label' => 'Discount'
292+
]
293+
]
294+
];
295+
$this->assertResponseFields($customerOrderItemTotal, $assertionMap);
296+
}
297+
218298
/**
219299
* @magentoApiDataFixture Magento/Customer/_files/customer.php
220300
* @magentoApiDataFixture Magento/GraphQl/Sales/_files/orders_with_customer.php
@@ -745,20 +825,12 @@ public function testCustomerOrderWithTaxesExcludedOnShipping()
745825
private function assertTotalsAndShippingWithExcludedTaxSetting($customerOrderItemTotal): void
746826
{
747827
$this->assertCount(1, $customerOrderItemTotal['taxes']);
748-
$expectedProductAndShippingTaxes = [2.25];
828+
$taxData = $customerOrderItemTotal['taxes'][0];
829+
$this->assertEquals('USD', $taxData['amount']['currency']);
830+
$this->assertEquals(2.25, $taxData['amount']['value']);
831+
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
832+
$this->assertEquals(7.5, $taxData['rate']);
749833

750-
$totalTaxes = [];
751-
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
752-
array_push($totalTaxes, $totalTaxFromResponse['amount']['value']);
753-
}
754-
foreach ($totalTaxes as $value) {
755-
$this->assertTrue(in_array($value, $expectedProductAndShippingTaxes));
756-
}
757-
foreach ($customerOrderItemTotal['taxes'] as $taxData) {
758-
$this->assertEquals('USD', $taxData['amount']['currency']);
759-
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
760-
$this->assertEquals(7.5, $taxData['rate']);
761-
}
762834
unset($customerOrderItemTotal['taxes']);
763835
$assertionMap = [
764836
'base_grand_total' => ['value' => 32.25, 'currency' =>'USD'],
@@ -820,19 +892,13 @@ public function testCustomerOrderWithTaxesIncludedOnShippingAndTotals()
820892
private function assertTotalsAndShippingWithTaxes(array $customerOrderItemTotal): void
821893
{
822894
$this->assertCount(1, $customerOrderItemTotal['taxes']);
823-
$expectedProductAndShippingTaxes = [2.25];
824-
$totalTaxes = [];
825-
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
826-
array_push($totalTaxes, $totalTaxFromResponse['amount']['value']);
827-
}
828-
foreach ($totalTaxes as $value) {
829-
$this->assertTrue(in_array($value, $expectedProductAndShippingTaxes));
830-
}
831-
foreach ($customerOrderItemTotal['taxes'] as $taxData) {
832-
$this->assertEquals('USD', $taxData['amount']['currency']);
833-
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
834-
$this->assertEquals(7.5, $taxData['rate']);
835-
}
895+
896+
$taxData = $customerOrderItemTotal['taxes'][0];
897+
$this->assertEquals('USD', $taxData['amount']['currency']);
898+
$this->assertEquals(2.25, $taxData['amount']['value']);
899+
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
900+
$this->assertEquals(7.5, $taxData['rate']);
901+
836902
unset($customerOrderItemTotal['taxes']);
837903
unset($customerOrderItemTotal['shipping_handling']['discounts']);
838904
$assertionMap = [

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,12 @@ public function testGetCustomerOrderBundleProductWithTaxesAndDiscounts()
164164
private function assertTotalsOnBundleProductWithTaxesAndDiscounts(array $customerOrderItemTotal): void
165165
{
166166
$this->assertCount(1, $customerOrderItemTotal['taxes']);
167-
$expectedProductAndShippingTaxes = [5.4];
168-
$totalTaxes = [];
169-
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
170-
array_push($totalTaxes, $totalTaxFromResponse['amount']['value']);
171-
}
172-
foreach ($totalTaxes as $value) {
173-
$this->assertTrue(in_array($value, $expectedProductAndShippingTaxes));
174-
}
175-
foreach ($customerOrderItemTotal['taxes'] as $taxData) {
176-
$this->assertEquals('USD', $taxData['amount']['currency']);
177-
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
178-
$this->assertEquals(7.5, $taxData['rate']);
179-
}
167+
$taxData = $customerOrderItemTotal['taxes'][0];
168+
$this->assertEquals('USD', $taxData['amount']['currency']);
169+
$this->assertEquals(5.4, $taxData['amount']['value']);
170+
$this->assertEquals('US-TEST-*-Rate-1', $taxData['title']);
171+
$this->assertEquals(7.5, $taxData['rate']);
172+
180173
unset($customerOrderItemTotal['taxes']);
181174
$assertionMap = [
182175
'base_grand_total' => ['value' => 77.4, 'currency' =>'USD'],
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Tax\Api\Data\TaxRateInterface;
8+
use Magento\Tax\Api\Data\TaxRuleInterface;
9+
use Magento\Tax\Api\TaxRateRepositoryInterface;
10+
use Magento\Tax\Api\TaxRuleRepositoryInterface;
11+
use Magento\Tax\Model\Calculation\Rate;
12+
use Magento\Tax\Model\Calculation\RateFactory;
13+
use Magento\Tax\Model\Calculation\RateRepository;
14+
use Magento\Tax\Model\Calculation\Rule;
15+
use Magento\Tax\Model\Calculation\RuleFactory;
16+
use Magento\Tax\Model\TaxRuleRepository;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\Framework\Api\DataObjectHelper;
19+
20+
$objectManager = Bootstrap::getObjectManager();
21+
/** @var DataObjectHelper $dataObjectHelper */
22+
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
23+
/** @var RateFactory $rateFactory */
24+
$rateFactory = $objectManager->get(RateFactory::class);
25+
/** @var RuleFactory $ruleFactory */
26+
$ruleFactory = $objectManager->get(RuleFactory::class);
27+
/** @var RateRepository $rateRepository */
28+
$rateRepository = $objectManager->get(TaxRateRepositoryInterface::class);
29+
/** @var TaxRuleRepository $ruleRepository */
30+
$ruleRepository = $objectManager->get(TaxRuleRepositoryInterface::class);
31+
/** @var Rate $rate */
32+
$rate = $rateFactory->create();
33+
$rateData = [
34+
Rate::KEY_COUNTRY_ID => 'US',
35+
Rate::KEY_REGION_ID => '1',
36+
Rate::KEY_POSTCODE => '*',
37+
Rate::KEY_CODE => 'US-AL-*-Rate-1',
38+
Rate::KEY_PERCENTAGE_RATE => '5.5',
39+
];
40+
$dataObjectHelper->populateWithArray($rate, $rateData, TaxRateInterface::class);
41+
$rateRepository->save($rate);
42+
43+
$rule = $ruleFactory->create();
44+
$ruleData = [
45+
Rule::KEY_CODE=> 'GraphQl Test Rule AL',
46+
Rule::KEY_PRIORITY => '0',
47+
Rule::KEY_POSITION => '0',
48+
Rule::KEY_CUSTOMER_TAX_CLASS_IDS => [3],
49+
Rule::KEY_PRODUCT_TAX_CLASS_IDS => [2],
50+
Rule::KEY_TAX_RATE_IDS => [$rate->getId()],
51+
];
52+
$dataObjectHelper->populateWithArray($rule, $ruleData, TaxRuleInterface::class);
53+
$ruleRepository->save($rule);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Tax\Api\TaxRateRepositoryInterface;
8+
use Magento\Tax\Api\TaxRuleRepositoryInterface;
9+
use Magento\Tax\Model\Calculation\Rate;
10+
use Magento\Tax\Model\Calculation\RateFactory;
11+
use Magento\Tax\Model\Calculation\RateRepository;
12+
use Magento\Tax\Model\Calculation\Rule;
13+
use Magento\Tax\Model\Calculation\RuleFactory;
14+
use Magento\Tax\Model\TaxRuleRepository;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\Tax\Model\ResourceModel\Calculation\Rate as RateResource;
17+
use Magento\Tax\Model\ResourceModel\Calculation\Rule as RuleResource;
18+
19+
$objectManager = Bootstrap::getObjectManager();
20+
/** @var RateFactory $rateFactory */
21+
$rateFactory = $objectManager->get(RateFactory::class);
22+
/** @var RuleFactory $ruleFactory */
23+
$ruleFactory = $objectManager->get(RuleFactory::class);
24+
/** @var RateRepository $rateRepository */
25+
$rateRepository = $objectManager->get(TaxRateRepositoryInterface::class);
26+
/** @var TaxRuleRepository $ruleRepository */
27+
$ruleRepository = $objectManager->get(TaxRuleRepositoryInterface::class);
28+
/** @var RateResource $rateResource */
29+
$rateResource = $objectManager->get(RateResource::class);
30+
/** @var RuleResource $ruleResource */
31+
$ruleResource = $objectManager->get(RuleResource::class);
32+
33+
$rate = $rateFactory->create();
34+
$rateResource->load($rate, 'US-AL-*-Rate-1', Rate::KEY_CODE);
35+
$rule = $ruleFactory->create();
36+
$ruleResource->load($rule, 'GraphQl Test Rule AL', Rule::KEY_CODE);
37+
$ruleRepository->delete($rule);
38+
$rateRepository->delete($rate);

0 commit comments

Comments
 (0)