Skip to content

Commit 8721abd

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'origin/MC-32658-tax' into MC-20636
2 parents b2726bb + a8844ae commit 8721abd

13 files changed

+1526
-51
lines changed

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

Lines changed: 23 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
@@ -129,7 +130,6 @@ private function fetch()
129130
/** @var OrderInterface $associatedOrder */
130131
$associatedOrder = $orderList[$orderItem->getOrderId()];
131132
$itemOptions = $this->optionsProcessor->getItemOptions($orderItem);
132-
133133
$this->orderItemList[$orderItem->getItemId()] = [
134134
'id' => base64_encode($orderItem->getItemId()),
135135
'associatedProduct' => $associatedProduct,
@@ -208,4 +208,26 @@ function ($orderItem) {
208208
}
209209
return $orderList;
210210
}
211+
212+
/**
213+
* Returns information about an applied discount
214+
*
215+
* @param OrderInterface $associatedOrder
216+
* @param OrderItemInterface $orderItem
217+
* @return array|null
218+
*/
219+
private function getDiscountDetails(OrderInterface $associatedOrder, OrderItemInterface $orderItem)
220+
{
221+
if ($associatedOrder->getDiscountDescription() === null && $orderItem->getDiscountAmount() == 0
222+
&& $associatedOrder->getDiscountAmount() == 0
223+
) {
224+
return null;
225+
}
226+
227+
$discounts [] = [
228+
'label' => $associatedOrder->getDiscountDescription() ?? "null",
229+
'amount' => ['value' => $orderItem->getDiscountAmount() ?? 0, 'currency' => $associatedOrder->getOrderCurrencyCode()]
230+
];
231+
return $discounts;
232+
}
211233
}

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

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
13-
use Magento\GraphQl\Model\Query\ContextInterface;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
1514
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\GraphQl\Model\Query\ContextInterface;
1616
use Magento\Sales\Model\Order;
1717

1818
class OrderTotal implements ResolverInterface
@@ -36,40 +36,117 @@ public function resolve(
3636
throw new LocalizedException(__('"model" value should be specified'));
3737
}
3838

39-
/** @var Order $orderModel */
40-
$orderModel = $value['model'];
41-
$currency = $orderModel->getOrderCurrencyCode();
42-
$totals = [
43-
'base_grand_total' => ['value' => $orderModel->getBaseGrandTotal(), 'currency' => $currency],
44-
'grand_total' => ['value' => $orderModel->getGrandTotal(), 'currency' => $currency],
45-
'subtotal' => ['value' => $orderModel->getSubtotal(), 'currency' => $currency],
46-
'total_tax' => ['value' => $orderModel->getTaxAmount(), 'currency' => $currency],
47-
'taxes' => $this->getAppliedTaxes($orderModel),
48-
'total_shipping' => ['value' => $orderModel->getShippingAmount(), 'currency' => $currency],
49-
'shipping_handling' => [
50-
'amount_exc_tax' => ['value' => $orderModel->getShippingTaxAmount(), 'currency' => $currency],
51-
'amount_inc_tax' => ['value' => $orderModel->getShippingInclTax(), 'currency' => $currency],
52-
'total_amount' => ['value' => $orderModel->getBaseShippingAmount(), 'currency' => $currency],
53-
'taxes' => $this->getAppliedTaxes($orderModel)
54-
]
39+
/** @var Order $order */
40+
$order = $value['model'];
41+
$currency = $order->getOrderCurrencyCode();
42+
$extensionAttributes = $order->getExtensionAttributes();
43+
$appliedTaxesForItems = $extensionAttributes->getItemAppliedTaxes();
44+
$allAppliedTaxesForItemsData[] = [];
45+
$appliedShippingTaxesForItemsData[] = [];
46+
if (!empty($appliedTaxesForItems)) {
47+
foreach ($appliedTaxesForItems as $key => $appliedTaxForItem) {
48+
$appliedTaxType = $appliedTaxForItem->getType();
49+
$taxLineItems = $appliedTaxForItem->getAppliedTaxes();
50+
foreach ($taxLineItems as $taxLineItem) {
51+
if ($appliedTaxType === "shipping") {
52+
$appliedShippingTaxesForItemsData[$key]['title'] = $taxLineItem->getDataByKey('title');
53+
$appliedShippingTaxesForItemsData[$key]['percent'] = $taxLineItem->getDataByKey('percent');
54+
$appliedShippingTaxesForItemsData[$key]['amount'] = $taxLineItem->getDataByKey('amount');
55+
}
56+
$allAppliedTaxesForItemsData[$key]['title'] = $taxLineItem->getDataByKey('title');
57+
$allAppliedTaxesForItemsData[$key]['percent'] = $taxLineItem->getDataByKey('percent');
58+
$allAppliedTaxesForItemsData[$key]['amount'] = $taxLineItem->getDataByKey('amount');
59+
}
60+
}
61+
}
62+
63+
$total = [
64+
'base_grand_total' => ['value' => $order->getBaseGrandTotal(), 'currency' => $currency],
65+
'grand_total' => ['value' => $order->getGrandTotal(), 'currency' => $currency],
66+
'subtotal' => ['value' => $order->getSubtotal(), 'currency' => $currency],
67+
'total_tax' => ['value' => $order->getTaxAmount(), 'currency' => $currency],
68+
'taxes' => $this->getAppliedTaxesDetails($order, $allAppliedTaxesForItemsData),
69+
'discounts' => $this->getDiscountDetails($order),
70+
'total_shipping' => ['value' => $order->getShippingAmount(), 'currency' => $currency],
71+
'shipping_handling' => [
72+
'amount_excluding_tax' => ['value' => $order->getShippingAmount(), 'currency' => $order->getOrderCurrencyCode()],
73+
'amount_including_tax' => ['value' => $order->getShippingInclTax(), 'currency' => $currency],
74+
'total_amount' => ['value' => $order->getBaseShippingAmount(), 'currency' => $currency],
75+
'taxes' => $this->getAppliedTaxesDetails($order, $appliedShippingTaxesForItemsData),
76+
'discounts' => $this->getShippingDiscountDetails($order),
77+
78+
]
5579
];
56-
return $totals;
80+
return $total;
5781
}
5882

5983
/**
60-
* Returns taxes applied to the current order
84+
* Returns information about an applied discount
6185
*
62-
* @param Order $orderModel
63-
* @return array
86+
* @param Order $order
87+
* @return array|null
6488
*/
65-
private function getAppliedTaxes(Order $orderModel): array
89+
private function getShippingDiscountDetails(Order $order)
6690
{
67-
$taxes[] = [
68-
'rate' => $orderModel->getStoreToOrderRate(),
69-
'title' => $orderModel->getCustomerName(),
70-
'amount' => [ 'value' => $orderModel->getTaxAmount(), 'currency' => $orderModel->getOrderCurrencyCode()
91+
if ($order->getDiscountDescription() === null && $order->getShippingDiscountAmount() == 0) {
92+
return null;
93+
}
94+
95+
$shippingDiscounts [] =
96+
[
97+
'label' => $order->getDiscountDescription() ?? "null",
98+
'amount' => [
99+
'value' => $order->getShippingDiscountAmount(),
100+
'currency' => $order->getOrderCurrencyCode()
101+
]
102+
];
103+
return $shippingDiscounts;
104+
}
105+
106+
/**
107+
* Returns information about an applied discount
108+
*
109+
* @param Order $order
110+
* @return array|null
111+
*/
112+
private function getDiscountDetails(Order $order)
113+
{
114+
if ($order->getDiscountDescription() === null && $order->getDiscountAmount() == 0) {
115+
return null;
116+
}
117+
118+
$discounts [] = [
119+
'label' => $order->getDiscountDescription() ?? "null",
120+
'amount' => [
121+
'value' => $order->getDiscountAmount(),
122+
'currency' => $order->getOrderCurrencyCode()
71123
]
72124
];
73-
return $taxes;
125+
return $discounts;
126+
}
127+
128+
/**
129+
* Returns taxes applied to the current order
130+
*
131+
* @param Order $order
132+
* @param array $appliedTaxesArray
133+
* @return array|null
134+
*/
135+
private function getAppliedTaxesDetails(Order $order, array $appliedTaxesArray): array
136+
{
137+
if (empty($appliedTaxesArray)) {
138+
$taxes [] = null;
139+
} else {
140+
foreach ($appliedTaxesArray as $appliedTaxes) {
141+
$taxes[] = [
142+
'rate' => $appliedTaxes['percent'] ?? 0,
143+
'title' => $appliedTaxes['title'] ?? " ",
144+
'amount' => ['value' => $appliedTaxes['amount'] ?? 0 , 'currency' => $order->getOrderCurrencyCode()
145+
]
146+
];
147+
}
148+
/** @var array $taxes */
149+
return $taxes;
150+
}
74151
}
75152
}

0 commit comments

Comments
 (0)