Skip to content

Commit d10d061

Browse files
committed
MC:32658:MyAccount :: Order Details :: Order Details by Order Number Taxes and Discounts
- Addressed code review fixes
1 parent 0b33f28 commit d10d061

File tree

3 files changed

+82
-95
lines changed

3 files changed

+82
-95
lines changed

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

Lines changed: 78 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,13 @@ public function resolve(
3232
/** @var OrderInterface $order */
3333
$order = $value['model'];
3434
$currency = $order->getOrderCurrencyCode();
35-
$extensionAttributes = $order->getExtensionAttributes();
36-
37-
$allAppliedTaxOnOrdersData = $this->getAllAppliedTaxesOnOrders(
38-
$extensionAttributes->getAppliedTaxes() ?? []
39-
);
40-
41-
$appliedShippingTaxesForItemsData = $this->getAppliedShippingTaxesForItems(
42-
$extensionAttributes->getItemAppliedTaxes() ?? []
43-
);
4435

4536
return [
4637
'base_grand_total' => ['value' => $order->getBaseGrandTotal(), 'currency' => $currency],
4738
'grand_total' => ['value' => $order->getGrandTotal(), 'currency' => $currency],
4839
'subtotal' => ['value' => $order->getSubtotal(), 'currency' => $currency],
4940
'total_tax' => ['value' => $order->getTaxAmount(), 'currency' => $currency],
50-
'taxes' => $this->getAppliedTaxesDetails($order, $allAppliedTaxOnOrdersData),
41+
'taxes' => $this->getAppliedTaxesDetails($order),
5142
'discounts' => $this->getDiscountDetails($order),
5243
'total_shipping' => ['value' => $order->getShippingAmount(), 'currency' => $currency],
5344
'shipping_handling' => [
@@ -63,7 +54,7 @@ public function resolve(
6354
'value' => $order->getShippingAmount(),
6455
'currency' => $currency
6556
],
66-
'taxes' => $this->getAppliedShippingTaxesDetails($order, $appliedShippingTaxesForItemsData),
57+
'taxes' => $this->getAppliedShippingTaxesDetails($order),
6758
'discounts' => $this->getShippingDiscountDetails($order),
6859
]
6960
];
@@ -72,66 +63,46 @@ public function resolve(
7263
/**
7364
* Retrieve applied taxes that apply to the order
7465
*
75-
* @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $appliedTaxes
66+
* @param OrderInterface $order
7667
* @return array
7768
*/
78-
private function getAllAppliedTaxesOnOrders(array $appliedTaxes): array
69+
private function getAllAppliedTaxesOnOrders(OrderInterface $order): array
7970
{
80-
$allAppliedTaxOnOrdersData = [];
71+
$extensionAttributes = $order->getExtensionAttributes();
72+
$appliedTaxes = $extensionAttributes->getAppliedTaxes() ?? [];
73+
$allAppliedTaxOnOrders = [];
8174
foreach ($appliedTaxes as $taxIndex => $appliedTaxesData) {
82-
$allAppliedTaxOnOrdersData[$taxIndex][$taxIndex] = [
75+
$allAppliedTaxOnOrders[$taxIndex] = [
8376
'title' => $appliedTaxesData->getDataByKey('title'),
8477
'percent' => $appliedTaxesData->getDataByKey('percent'),
8578
'amount' => $appliedTaxesData->getDataByKey('amount'),
8679
];
8780
}
88-
return $allAppliedTaxOnOrdersData;
81+
return $allAppliedTaxOnOrders;
8982
}
9083

9184
/**
92-
* Retrieve applied shipping taxes on items for the orders
93-
*
94-
* @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface $itemAppliedTaxes
95-
* @return array
96-
*/
97-
private function getAppliedShippingTaxesForItems(array $itemAppliedTaxes): array
98-
{
99-
$appliedShippingTaxesForItemsData = [];
100-
foreach ($itemAppliedTaxes as $taxItemIndex => $appliedTaxForItem) {
101-
if ($appliedTaxForItem->getType() === "shipping") {
102-
foreach ($appliedTaxForItem->getAppliedTaxes() ?? [] as $taxLineItem) {
103-
$taxItemIndexTitle = $taxLineItem->getDataByKey('title');
104-
$appliedShippingTaxesForItemsData[$taxItemIndex][$taxItemIndexTitle] = [
105-
'title' => $taxLineItem->getDataByKey('title'),
106-
'percent' => $taxLineItem->getDataByKey('percent'),
107-
'amount' => $taxLineItem->getDataByKey('amount')
108-
];
109-
}
110-
}
111-
}
112-
return $appliedShippingTaxesForItemsData;
113-
}
114-
115-
/**
116-
* Return information about an applied discount
85+
* Return taxes applied to the current order
11786
*
11887
* @param OrderInterface $order
11988
* @return array
12089
*/
121-
private function getShippingDiscountDetails(OrderInterface $order)
90+
private function getAppliedTaxesDetails(OrderInterface $order): array
12291
{
123-
$shippingDiscounts = [];
124-
if (!($order->getDiscountDescription() === null && $order->getShippingDiscountAmount() == 0)) {
125-
$shippingDiscounts[] =
126-
[
127-
'label' => $order->getDiscountDescription() ?? __('Discount'),
128-
'amount' => [
129-
'value' => $order->getShippingDiscountAmount(),
130-
'currency' => $order->getOrderCurrencyCode()
131-
]
132-
];
92+
$allAppliedTaxOnOrders = $this->getAllAppliedTaxesOnOrders($order);
93+
$taxes = [];
94+
foreach ($allAppliedTaxOnOrders as $appliedTaxes) {
95+
$appliedTaxesArray = [
96+
'rate' => $appliedTaxes['percent'] ?? 0,
97+
'title' => $appliedTaxes['title'] ?? null,
98+
'amount' => [
99+
'value' => $appliedTaxes['amount'] ?? 0,
100+
'currency' => $order->getOrderCurrencyCode()
101+
]
102+
];
103+
$taxes[] = $appliedTaxesArray;
133104
}
134-
return $shippingDiscounts;
105+
return $taxes;
135106
}
136107

137108
/**
@@ -140,74 +111,91 @@ private function getShippingDiscountDetails(OrderInterface $order)
140111
* @param OrderInterface $order
141112
* @return array
142113
*/
143-
private function getDiscountDetails(OrderInterface $order)
114+
private function getDiscountDetails(OrderInterface $order): array
144115
{
145-
$discounts = [];
116+
$orderDiscounts = [];
146117
if (!($order->getDiscountDescription() === null && $order->getDiscountAmount() == 0)) {
147-
$discounts[] = [
118+
$orderDiscounts[] = [
148119
'label' => $order->getDiscountDescription() ?? __('Discount'),
149120
'amount' => [
150-
'value' => $order->getDiscountAmount(),
121+
'value' => abs($order->getDiscountAmount()),
151122
'currency' => $order->getOrderCurrencyCode()
152123
]
153124
];
154125
}
155-
return $discounts;
126+
return $orderDiscounts;
156127
}
157128

158129
/**
159-
* Returns taxes applied to the current order
130+
* Retrieve applied shipping taxes on items for the orders
160131
*
161132
* @param OrderInterface $order
162-
* @param array $appliedTaxesArray
163133
* @return array
164134
*/
165-
private function getAppliedTaxesDetails(OrderInterface $order, array $appliedTaxesArray): array
135+
private function getAppliedShippingTaxesForItems(OrderInterface $order): array
166136
{
167-
$taxes = [];
168-
foreach ($appliedTaxesArray as $appliedTaxesKeyIndex => $appliedTaxes) {
169-
$appliedTaxesArray = [
170-
'title' => $appliedTaxes[$appliedTaxesKeyIndex]['title'] ?? null,
171-
'amount' => [
172-
'value' => $appliedTaxes[$appliedTaxesKeyIndex]['amount'] ?? 0,
173-
'currency' => $order->getOrderCurrencyCode()
174-
],
175-
];
176-
if (!empty($appliedTaxes[$appliedTaxesKeyIndex])) {
177-
$appliedTaxesArray['rate'] = $appliedTaxes[$appliedTaxesKeyIndex]['percent'] ?? null;
137+
$extensionAttributes = $order->getExtensionAttributes();
138+
$itemAppliedTaxes = $extensionAttributes->getItemAppliedTaxes() ?? [];
139+
$appliedShippingTaxesForItems = [];
140+
foreach ($itemAppliedTaxes as $appliedTaxForItem) {
141+
if ($appliedTaxForItem->getType() === "shipping") {
142+
foreach ($appliedTaxForItem->getAppliedTaxes() ?? [] as $taxLineItem) {
143+
$taxItemIndexTitle = $taxLineItem->getDataByKey('title');
144+
$appliedShippingTaxesForItems[$taxItemIndexTitle] = [
145+
'title' => $taxLineItem->getDataByKey('title'),
146+
'percent' => $taxLineItem->getDataByKey('percent'),
147+
'amount' => $taxLineItem->getDataByKey('amount')
148+
];
149+
}
178150
}
179-
$taxes[] = $appliedTaxesArray;
180151
}
181-
return $taxes;
152+
return $appliedShippingTaxesForItems;
182153
}
183154

184155
/**
185-
* Returns taxes applied to the current order
156+
* Return taxes applied to the current order
186157
*
187158
* @param OrderInterface $order
188-
* @param array $appliedShippingTaxesForItemsData
189159
* @return array
190160
*/
191161
private function getAppliedShippingTaxesDetails(
192-
OrderInterface $order,
193-
array $appliedShippingTaxesForItemsData
162+
OrderInterface $order
194163
): array {
164+
$appliedShippingTaxesForItems = $this->getAppliedShippingTaxesForItems($order);
195165
$shippingTaxes = [];
196-
foreach ($appliedShippingTaxesForItemsData as $appliedShippingTaxes) {
197-
foreach ($appliedShippingTaxes as $appliedShippingTax) {
198-
$appliedShippingTaxesArray = [
199-
'title' => $appliedShippingTax['title'] ?? null,
166+
foreach ($appliedShippingTaxesForItems as $appliedShippingTaxes) {
167+
$appliedShippingTaxesArray = [
168+
'rate' => $appliedShippingTaxes['percent'] ?? 0,
169+
'title' => $appliedShippingTaxes['title'] ?? null,
170+
'amount' => [
171+
'value' => $appliedShippingTaxes['amount'] ?? 0,
172+
'currency' => $order->getOrderCurrencyCode()
173+
]
174+
];
175+
$shippingTaxes[] = $appliedShippingTaxesArray;
176+
}
177+
return $shippingTaxes;
178+
}
179+
180+
/**
181+
* Return information about an applied discount
182+
*
183+
* @param OrderInterface $order
184+
* @return array
185+
*/
186+
private function getShippingDiscountDetails(OrderInterface $order): array
187+
{
188+
$shippingDiscounts = [];
189+
if (!($order->getDiscountDescription() === null && $order->getShippingDiscountAmount() == 0)) {
190+
$shippingDiscounts[] =
191+
[
192+
'label' => $order->getDiscountDescription() ?? __('Discount'),
200193
'amount' => [
201-
'value' => $appliedShippingTax['amount'] ?? 0,
194+
'value' => abs($order->getShippingDiscountAmount()),
202195
'currency' => $order->getOrderCurrencyCode()
203-
],
196+
]
204197
];
205-
if (!empty($appliedShippingTax)) {
206-
$appliedShippingTaxesArray['rate'] = $appliedShippingTax['percent'] ?? 0;
207-
}
208-
$shippingTaxes[] = $appliedShippingTaxesArray;
209-
}
210198
}
211-
return $shippingTaxes;
199+
return $shippingDiscounts;
212200
}
213201
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private function assertTotalsWithTaxesAndDiscounts(array $customerOrderItemTotal
200200
]
201201
],
202202
'discounts' => [
203-
0 => ['amount' => [ 'value' => -6, 'currency' =>'USD'],
203+
0 => ['amount' => [ 'value' => 6, 'currency' =>'USD'],
204204
'label' => 'Discount'
205205
]
206206
]
@@ -287,7 +287,7 @@ private function assertTotalsWithTaxesAndDiscountsWithTwoRules(array $customerOr
287287
]
288288
],
289289
'discounts' => [
290-
0 => ['amount' => [ 'value' => -6, 'currency' =>'USD'],
290+
0 => ['amount' => [ 'value' => 6, 'currency' =>'USD'],
291291
'label' => 'Discount'
292292
]
293293
]
@@ -911,7 +911,6 @@ private function assertTotalsAndShippingWithTaxes(array $customerOrderItemTotal)
911911
'amount_including_tax' => ['value' => 10.75],
912912
'amount_excluding_tax' => ['value' => 10],
913913
'total_amount' => ['value' => 10, 'currency' =>'USD'],
914-
915914
'taxes'=> [
916915
0 => [
917916
'amount'=>['value' => 0.75],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private function assertTotalsOnBundleProductWithTaxesAndDiscounts(array $custome
182182
'amount_excluding_tax' => ['value' => 20],
183183
'total_amount' => ['value' => 20],
184184
'discounts' => [
185-
0 => ['amount'=>['value'=>2],
185+
0 => ['amount'=>['value'=> 2],
186186
'label' => 'Discount'
187187
]
188188
],
@@ -195,7 +195,7 @@ private function assertTotalsOnBundleProductWithTaxesAndDiscounts(array $custome
195195
]
196196
],
197197
'discounts' => [
198-
0 => ['amount' => [ 'value' => -8, 'currency' =>'USD'],
198+
0 => ['amount' => [ 'value' => 8, 'currency' =>'USD'],
199199
'label' => 'Discount'
200200
]
201201
]

0 commit comments

Comments
 (0)