Skip to content

Commit bf28167

Browse files
committed
MC:32658:MyAccount :: Order Details :: Order Details by Order Number Taxes and Discounts
- Added changes on taxes and related taxes tests
1 parent 5d3d5c7 commit bf28167

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

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

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14-
use Magento\Sales\Api\Data\OrderExtensionInterface;
1514
use Magento\Sales\Api\Data\OrderInterface;
1615

1716
class OrderTotal implements ResolverInterface
@@ -35,9 +34,10 @@ public function resolve(
3534
$currency = $order->getOrderCurrencyCode();
3635
$extensionAttributes = $order->getExtensionAttributes();
3736

38-
$allAppliedTaxesForItemsData = $this->getAllAppliedTaxesForItems(
39-
$extensionAttributes->getItemAppliedTaxes() ?? []
37+
$allAppliedTaxOnOrdersData = $this->getAllAppliedTaxesOnOrders(
38+
$extensionAttributes->getAppliedTaxes() ?? []
4039
);
40+
4141
$appliedShippingTaxesForItemsData = $this->getAppliedShippingTaxesForItems(
4242
$extensionAttributes->getItemAppliedTaxes() ?? []
4343
);
@@ -47,7 +47,7 @@ public function resolve(
4747
'grand_total' => ['value' => $order->getGrandTotal(), 'currency' => $currency],
4848
'subtotal' => ['value' => $order->getSubtotal(), 'currency' => $currency],
4949
'total_tax' => ['value' => $order->getTaxAmount(), 'currency' => $currency],
50-
'taxes' => $this->getAppliedTaxesDetails($order, $allAppliedTaxesForItemsData),
50+
'taxes' => $this->getAppliedTaxesDetails($order, $allAppliedTaxOnOrdersData),
5151
'discounts' => $this->getDiscountDetails($order),
5252
'total_shipping' => ['value' => $order->getShippingAmount(), 'currency' => $currency],
5353
'shipping_handling' => [
@@ -63,35 +63,33 @@ public function resolve(
6363
'value' => $order->getShippingAmount(),
6464
'currency' => $currency
6565
],
66-
'taxes' => $this->getAppliedTaxesDetails($order, $appliedShippingTaxesForItemsData),
66+
'taxes' => $this->getAppliedShippingTaxesDetails($order, $appliedShippingTaxesForItemsData),
6767
'discounts' => $this->getShippingDiscountDetails($order),
6868
]
6969
];
7070
}
7171

7272
/**
73-
* Retrieve applied taxes that apply to items
73+
* Retrieve applied taxes that apply to the order
7474
*
75-
* @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $itemAppliedTaxes
75+
* @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $appliedTaxes
7676
* @return array
7777
*/
78-
private function getAllAppliedTaxesForItems(array $itemAppliedTaxes): array
78+
private function getAllAppliedTaxesOnOrders(array $appliedTaxes): array
7979
{
80-
$allAppliedTaxesForItemsData = [];
81-
foreach ($itemAppliedTaxes as $taxItemIndex => $appliedTaxForItem) {
82-
foreach ($appliedTaxForItem->getAppliedTaxes() ?? [] as $taxLineItem) {
83-
$allAppliedTaxesForItemsData[$taxItemIndex][$taxItemIndex] = [
84-
'title' => $taxLineItem->getDataByKey('title'),
85-
'percent' => $taxLineItem->getDataByKey('percent'),
86-
'amount' => $taxLineItem->getDataByKey('amount'),
87-
];
88-
}
80+
$allAppliedTaxOnOrdersData = [];
81+
foreach ($appliedTaxes as $taxIndex => $appliedTaxesData) {
82+
$allAppliedTaxOnOrdersData[$taxIndex][$taxIndex] = [
83+
'title' => $appliedTaxesData->getDataByKey('title'),
84+
'percent' => $appliedTaxesData->getDataByKey('percent'),
85+
'amount' => $appliedTaxesData->getDataByKey('amount'),
86+
];
8987
}
90-
return $allAppliedTaxesForItemsData;
88+
return $allAppliedTaxOnOrdersData;
9189
}
9290

9391
/**
94-
* Retrieve applied taxes that apply to shipping
92+
* Retrieve applied shipping taxes on items for the orders
9593
*
9694
* @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface $itemAppliedTaxes
9795
* @return array
@@ -100,9 +98,10 @@ private function getAppliedShippingTaxesForItems(array $itemAppliedTaxes): array
10098
{
10199
$appliedShippingTaxesForItemsData = [];
102100
foreach ($itemAppliedTaxes as $taxItemIndex => $appliedTaxForItem) {
103-
foreach ($appliedTaxForItem->getAppliedTaxes() ?? [] as $taxLineItem) {
104-
if ($appliedTaxForItem->getType() === "shipping") {
105-
$appliedShippingTaxesForItemsData[$taxItemIndex][$taxItemIndex] = [
101+
if ($appliedTaxForItem->getType() === "shipping") {
102+
foreach ($appliedTaxForItem->getAppliedTaxes() ?? [] as $taxLineItem) {
103+
$taxItemIndexTitle = $taxLineItem->getDataByKey('title');
104+
$appliedShippingTaxesForItemsData[$taxItemIndex][$taxItemIndexTitle] = [
106105
'title' => $taxLineItem->getDataByKey('title'),
107106
'percent' => $taxLineItem->getDataByKey('percent'),
108107
'amount' => $taxLineItem->getDataByKey('amount')
@@ -181,4 +180,32 @@ private function getAppliedTaxesDetails(OrderInterface $order, array $appliedTax
181180
}
182181
return $taxes;
183182
}
183+
184+
/**
185+
* Returns taxes applied to the current order
186+
*
187+
* @param OrderInterface $order
188+
* @param array $appliedShippingTaxesForItemsData
189+
* @return array
190+
*/
191+
private function getAppliedShippingTaxesDetails(OrderInterface $order, array $appliedShippingTaxesForItemsData): array
192+
{
193+
$shippingTaxes = [];
194+
foreach ($appliedShippingTaxesForItemsData as $appliedTaxesKeyIndex => $appliedShippingTaxes) {
195+
foreach ($appliedShippingTaxes as $key => $appliedShippingTax) {
196+
$appliedShippingTaxesArray = [
197+
'title' => $appliedShippingTax['title'] ?? null,
198+
'amount' => [
199+
'value' => $appliedShippingTax['amount'] ?? 0,
200+
'currency' => $order->getOrderCurrencyCode()
201+
],
202+
];
203+
if (!empty($appliedShippingTax)) {
204+
$appliedShippingTaxesArray['rate'] = $appliedShippingTax['percent'] ?? 0;
205+
}
206+
$shippingTaxes[] = $appliedShippingTaxesArray;
207+
}
208+
}
209+
return $shippingTaxes;
210+
}
184211
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ public function testCustomerOrdersSimpleProductWithTaxesAndDiscounts()
168168
*/
169169
private function assertTotalsWithTaxesAndDiscounts(array $customerOrderItemTotal): void
170170
{
171-
$this->assertCount(2, $customerOrderItemTotal['taxes']);
172-
$expectedProductAndShippingTaxes = [2.7, 1.35];
171+
$this->assertCount(1, $customerOrderItemTotal['taxes']);
172+
$expectedProductAndShippingTaxes = [4.05];
173173
$totalTaxes = [];
174174
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
175175
array_push($totalTaxes, $totalTaxFromResponse['amount']['value']);
@@ -744,8 +744,8 @@ public function testCustomerOrderWithTaxesExcludedOnShipping()
744744
*/
745745
private function assertTotalsAndShippingWithExcludedTaxSetting($customerOrderItemTotal): void
746746
{
747-
$this->assertCount(2, $customerOrderItemTotal['taxes']);
748-
$expectedProductAndShippingTaxes = [1.5, 0.75];
747+
$this->assertCount(1, $customerOrderItemTotal['taxes']);
748+
$expectedProductAndShippingTaxes = [2.25];
749749

750750
$totalTaxes = [];
751751
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
@@ -819,8 +819,8 @@ public function testCustomerOrderWithTaxesIncludedOnShippingAndTotals()
819819
*/
820820
private function assertTotalsAndShippingWithTaxes(array $customerOrderItemTotal): void
821821
{
822-
$this->assertCount(2, $customerOrderItemTotal['taxes']);
823-
$expectedProductAndShippingTaxes = [1.5, 0.75];
822+
$this->assertCount(1, $customerOrderItemTotal['taxes']);
823+
$expectedProductAndShippingTaxes = [2.25];
824824
$totalTaxes = [];
825825
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
826826
array_push($totalTaxes, $totalTaxFromResponse['amount']['value']);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ public function testGetCustomerOrderBundleProductWithTaxesAndDiscounts()
163163
*/
164164
private function assertTotalsOnBundleProductWithTaxesAndDiscounts(array $customerOrderItemTotal): void
165165
{
166-
$this->assertCount(2, $customerOrderItemTotal['taxes']);
167-
$expectedProductAndShippingTaxes = [4.05, 1.35];
166+
$this->assertCount(1, $customerOrderItemTotal['taxes']);
167+
$expectedProductAndShippingTaxes = [5.4];
168168
$totalTaxes = [];
169169
foreach ($customerOrderItemTotal['taxes'] as $totalTaxFromResponse) {
170170
array_push($totalTaxes, $totalTaxFromResponse['amount']['value']);

0 commit comments

Comments
 (0)