11
11
use Magento \Framework \GraphQl \Config \Element \Field ;
12
12
use Magento \Framework \GraphQl \Query \ResolverInterface ;
13
13
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
- use Magento \Sales \Api \Data \OrderExtensionInterface ;
15
14
use Magento \Sales \Api \Data \OrderInterface ;
16
15
17
16
class OrderTotal implements ResolverInterface
@@ -35,9 +34,10 @@ public function resolve(
35
34
$ currency = $ order ->getOrderCurrencyCode ();
36
35
$ extensionAttributes = $ order ->getExtensionAttributes ();
37
36
38
- $ allAppliedTaxesForItemsData = $ this ->getAllAppliedTaxesForItems (
39
- $ extensionAttributes ->getItemAppliedTaxes () ?? []
37
+ $ allAppliedTaxOnOrdersData = $ this ->getAllAppliedTaxesOnOrders (
38
+ $ extensionAttributes ->getAppliedTaxes () ?? []
40
39
);
40
+
41
41
$ appliedShippingTaxesForItemsData = $ this ->getAppliedShippingTaxesForItems (
42
42
$ extensionAttributes ->getItemAppliedTaxes () ?? []
43
43
);
@@ -47,7 +47,7 @@ public function resolve(
47
47
'grand_total ' => ['value ' => $ order ->getGrandTotal (), 'currency ' => $ currency ],
48
48
'subtotal ' => ['value ' => $ order ->getSubtotal (), 'currency ' => $ currency ],
49
49
'total_tax ' => ['value ' => $ order ->getTaxAmount (), 'currency ' => $ currency ],
50
- 'taxes ' => $ this ->getAppliedTaxesDetails ($ order , $ allAppliedTaxesForItemsData ),
50
+ 'taxes ' => $ this ->getAppliedTaxesDetails ($ order , $ allAppliedTaxOnOrdersData ),
51
51
'discounts ' => $ this ->getDiscountDetails ($ order ),
52
52
'total_shipping ' => ['value ' => $ order ->getShippingAmount (), 'currency ' => $ currency ],
53
53
'shipping_handling ' => [
@@ -63,35 +63,33 @@ public function resolve(
63
63
'value ' => $ order ->getShippingAmount (),
64
64
'currency ' => $ currency
65
65
],
66
- 'taxes ' => $ this ->getAppliedTaxesDetails ($ order , $ appliedShippingTaxesForItemsData ),
66
+ 'taxes ' => $ this ->getAppliedShippingTaxesDetails ($ order , $ appliedShippingTaxesForItemsData ),
67
67
'discounts ' => $ this ->getShippingDiscountDetails ($ order ),
68
68
]
69
69
];
70
70
}
71
71
72
72
/**
73
- * Retrieve applied taxes that apply to items
73
+ * Retrieve applied taxes that apply to the order
74
74
*
75
- * @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $itemAppliedTaxes
75
+ * @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $appliedTaxes
76
76
* @return array
77
77
*/
78
- private function getAllAppliedTaxesForItems (array $ itemAppliedTaxes ): array
78
+ private function getAllAppliedTaxesOnOrders (array $ appliedTaxes ): array
79
79
{
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
+ ];
89
87
}
90
- return $ allAppliedTaxesForItemsData ;
88
+ return $ allAppliedTaxOnOrdersData ;
91
89
}
92
90
93
91
/**
94
- * Retrieve applied taxes that apply to shipping
92
+ * Retrieve applied shipping taxes on items for the orders
95
93
*
96
94
* @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface $itemAppliedTaxes
97
95
* @return array
@@ -100,9 +98,10 @@ private function getAppliedShippingTaxesForItems(array $itemAppliedTaxes): array
100
98
{
101
99
$ appliedShippingTaxesForItemsData = [];
102
100
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 ] = [
106
105
'title ' => $ taxLineItem ->getDataByKey ('title ' ),
107
106
'percent ' => $ taxLineItem ->getDataByKey ('percent ' ),
108
107
'amount ' => $ taxLineItem ->getDataByKey ('amount ' )
@@ -181,4 +180,32 @@ private function getAppliedTaxesDetails(OrderInterface $order, array $appliedTax
181
180
}
182
181
return $ taxes ;
183
182
}
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
+ }
184
211
}
0 commit comments