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
16
+ /**
17
+ * Resolve order totals taxes and discounts for order
18
+ */
17
19
class OrderTotal implements ResolverInterface
18
20
{
19
21
/**
@@ -33,21 +35,13 @@ public function resolve(
33
35
/** @var OrderInterface $order */
34
36
$ order = $ value ['model ' ];
35
37
$ currency = $ order ->getOrderCurrencyCode ();
36
- $ extensionAttributes = $ order ->getExtensionAttributes ();
37
-
38
- $ allAppliedTaxesForItemsData = $ this ->getAllAppliedTaxesForItems (
39
- $ extensionAttributes ->getItemAppliedTaxes () ?? []
40
- );
41
- $ appliedShippingTaxesForItemsData = $ this ->getAppliedShippingTaxesForItems (
42
- $ extensionAttributes ->getItemAppliedTaxes () ?? []
43
- );
44
38
45
39
return [
46
40
'base_grand_total ' => ['value ' => $ order ->getBaseGrandTotal (), 'currency ' => $ currency ],
47
41
'grand_total ' => ['value ' => $ order ->getGrandTotal (), 'currency ' => $ currency ],
48
42
'subtotal ' => ['value ' => $ order ->getSubtotal (), 'currency ' => $ currency ],
49
43
'total_tax ' => ['value ' => $ order ->getTaxAmount (), 'currency ' => $ currency ],
50
- 'taxes ' => $ this ->getAppliedTaxesDetails ($ order, $ allAppliedTaxesForItemsData ),
44
+ 'taxes ' => $ this ->getAppliedTaxesDetails ($ order ),
51
45
'discounts ' => $ this ->getDiscountDetails ($ order ),
52
46
'total_shipping ' => ['value ' => $ order ->getShippingAmount (), 'currency ' => $ currency ],
53
47
'shipping_handling ' => [
@@ -63,54 +57,55 @@ public function resolve(
63
57
'value ' => $ order ->getShippingAmount (),
64
58
'currency ' => $ currency
65
59
],
66
- 'taxes ' => $ this ->getAppliedTaxesDetails ($ order, $ appliedShippingTaxesForItemsData ),
60
+ 'taxes ' => $ this ->getAppliedShippingTaxesDetails ($ order ),
67
61
'discounts ' => $ this ->getShippingDiscountDetails ($ order ),
68
62
]
69
63
];
70
64
}
71
65
72
66
/**
73
- * Retrieve applied taxes that apply to items
67
+ * Retrieve applied taxes that apply to the order
74
68
*
75
- * @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $itemAppliedTaxes
69
+ * @param OrderInterface $order
76
70
* @return array
77
71
*/
78
- private function getAllAppliedTaxesForItems ( array $ itemAppliedTaxes ): array
72
+ private function getAllAppliedTaxesOnOrders ( OrderInterface $ order ): array
79
73
{
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
- }
74
+ $ extensionAttributes = $ order -> getExtensionAttributes () ;
75
+ $ appliedTaxes = $ extensionAttributes -> getAppliedTaxes () ?? [];
76
+ $ allAppliedTaxOnOrders = [];
77
+ foreach ( $ appliedTaxes as $ taxIndex => $ appliedTaxesData ) {
78
+ $ allAppliedTaxOnOrders [ $ taxIndex ] = [
79
+ ' title ' => $ appliedTaxesData ->getDataByKey ('title ' ),
80
+ ' percent ' => $ appliedTaxesData ->getDataByKey ('percent ' ),
81
+ ' amount ' => $ appliedTaxesData -> getDataByKey ( ' amount ' ),
82
+ ];
89
83
}
90
- return $ allAppliedTaxesForItemsData ;
84
+ return $ allAppliedTaxOnOrders ;
91
85
}
92
86
93
87
/**
94
- * Retrieve applied taxes that apply to shipping
88
+ * Return taxes applied to the current order
95
89
*
96
- * @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface $itemAppliedTaxes
90
+ * @param OrderInterface $order
97
91
* @return array
98
92
*/
99
- private function getAppliedShippingTaxesForItems ( array $ itemAppliedTaxes ): array
93
+ private function getAppliedTaxesDetails ( OrderInterface $ order ): array
100
94
{
101
- $ appliedShippingTaxesForItemsData = [];
102
- foreach ($ itemAppliedTaxes as $ taxItemIndex => $ appliedTaxForItem ) {
103
- foreach ($ appliedTaxForItem ->getAppliedTaxes () ?? [] as $ taxLineItem ) {
104
- if ($ appliedTaxForItem ->getType () === "shipping " ) {
105
- $ appliedShippingTaxesForItemsData [$ taxItemIndex ][$ taxItemIndex ] = [
106
- 'title ' => $ taxLineItem ->getDataByKey ('title ' ),
107
- 'percent ' => $ taxLineItem ->getDataByKey ('percent ' ),
108
- 'amount ' => $ taxLineItem ->getDataByKey ('amount ' )
109
- ];
110
- }
111
- }
95
+ $ allAppliedTaxOnOrders = $ this ->getAllAppliedTaxesOnOrders ($ order );
96
+ $ taxes = [];
97
+ foreach ($ allAppliedTaxOnOrders as $ appliedTaxes ) {
98
+ $ appliedTaxesArray = [
99
+ 'rate ' => $ appliedTaxes ['percent ' ] ?? 0 ,
100
+ 'title ' => $ appliedTaxes ['title ' ] ?? null ,
101
+ 'amount ' => [
102
+ 'value ' => $ appliedTaxes ['amount ' ] ?? 0 ,
103
+ 'currency ' => $ order ->getOrderCurrencyCode ()
104
+ ]
105
+ ];
106
+ $ taxes [] = $ appliedTaxesArray ;
112
107
}
113
- return $ appliedShippingTaxesForItemsData ;
108
+ return $ taxes ;
114
109
}
115
110
116
111
/**
@@ -119,66 +114,91 @@ private function getAppliedShippingTaxesForItems(array $itemAppliedTaxes): array
119
114
* @param OrderInterface $order
120
115
* @return array
121
116
*/
122
- private function getShippingDiscountDetails (OrderInterface $ order )
117
+ private function getDiscountDetails (OrderInterface $ order ): array
123
118
{
124
- $ shippingDiscounts = [];
125
- if (!($ order ->getDiscountDescription () === null && $ order ->getShippingDiscountAmount () == 0 )) {
126
- $ shippingDiscounts [] =
127
- [
128
- 'label ' => $ order ->getDiscountDescription () ?? __ ('Discount ' ),
129
- 'amount ' => [
130
- 'value ' => $ order ->getShippingDiscountAmount (),
131
- 'currency ' => $ order ->getOrderCurrencyCode ()
132
- ]
133
- ];
119
+ $ orderDiscounts = [];
120
+ if (!($ order ->getDiscountDescription () === null && $ order ->getDiscountAmount () == 0 )) {
121
+ $ orderDiscounts [] = [
122
+ 'label ' => $ order ->getDiscountDescription () ?? __ ('Discount ' ),
123
+ 'amount ' => [
124
+ 'value ' => abs ($ order ->getDiscountAmount ()),
125
+ 'currency ' => $ order ->getOrderCurrencyCode ()
126
+ ]
127
+ ];
134
128
}
135
- return $ shippingDiscounts ;
129
+ return $ orderDiscounts ;
136
130
}
137
131
138
132
/**
139
- * Return information about an applied discount
133
+ * Retrieve applied shipping taxes on items for the orders
140
134
*
141
135
* @param OrderInterface $order
142
136
* @return array
143
137
*/
144
- private function getDiscountDetails (OrderInterface $ order )
138
+ private function getAppliedShippingTaxesForItems (OrderInterface $ order ): array
145
139
{
146
- $ discounts = [];
147
- if (!($ order ->getDiscountDescription () === null && $ order ->getDiscountAmount () == 0 )) {
148
- $ discounts [] = [
149
- 'label ' => $ order ->getDiscountDescription () ?? __ ('Discount ' ),
140
+ $ extensionAttributes = $ order ->getExtensionAttributes ();
141
+ $ itemAppliedTaxes = $ extensionAttributes ->getItemAppliedTaxes () ?? [];
142
+ $ appliedShippingTaxesForItems = [];
143
+ foreach ($ itemAppliedTaxes as $ appliedTaxForItem ) {
144
+ if ($ appliedTaxForItem ->getType () === "shipping " ) {
145
+ foreach ($ appliedTaxForItem ->getAppliedTaxes () ?? [] as $ taxLineItem ) {
146
+ $ taxItemIndexTitle = $ taxLineItem ->getDataByKey ('title ' );
147
+ $ appliedShippingTaxesForItems [$ taxItemIndexTitle ] = [
148
+ 'title ' => $ taxLineItem ->getDataByKey ('title ' ),
149
+ 'percent ' => $ taxLineItem ->getDataByKey ('percent ' ),
150
+ 'amount ' => $ taxLineItem ->getDataByKey ('amount ' )
151
+ ];
152
+ }
153
+ }
154
+ }
155
+ return $ appliedShippingTaxesForItems ;
156
+ }
157
+
158
+ /**
159
+ * Return taxes applied to the current order
160
+ *
161
+ * @param OrderInterface $order
162
+ * @return array
163
+ */
164
+ private function getAppliedShippingTaxesDetails (
165
+ OrderInterface $ order
166
+ ): array {
167
+ $ appliedShippingTaxesForItems = $ this ->getAppliedShippingTaxesForItems ($ order );
168
+ $ shippingTaxes = [];
169
+ foreach ($ appliedShippingTaxesForItems as $ appliedShippingTaxes ) {
170
+ $ appliedShippingTaxesArray = [
171
+ 'rate ' => $ appliedShippingTaxes ['percent ' ] ?? 0 ,
172
+ 'title ' => $ appliedShippingTaxes ['title ' ] ?? null ,
150
173
'amount ' => [
151
- 'value ' => $ order -> getDiscountAmount () ,
174
+ 'value ' => $ appliedShippingTaxes [ ' amount ' ] ?? 0 ,
152
175
'currency ' => $ order ->getOrderCurrencyCode ()
153
176
]
154
177
];
178
+ $ shippingTaxes [] = $ appliedShippingTaxesArray ;
155
179
}
156
- return $ discounts ;
180
+ return $ shippingTaxes ;
157
181
}
158
182
159
183
/**
160
- * Returns taxes applied to the current order
184
+ * Return information about an applied discount
161
185
*
162
186
* @param OrderInterface $order
163
- * @param array $appliedTaxesArray
164
187
* @return array
165
188
*/
166
- private function getAppliedTaxesDetails (OrderInterface $ order, array $ appliedTaxesArray ): array
189
+ private function getShippingDiscountDetails (OrderInterface $ order ): array
167
190
{
168
- $ taxes = [];
169
- foreach ($ appliedTaxesArray as $ appliedTaxesKeyIndex => $ appliedTaxes ) {
170
- $ appliedTaxesArray = [
171
- 'title ' => $ appliedTaxes [$ appliedTaxesKeyIndex ]['title ' ] ?? null ,
172
- 'amount ' => [
173
- 'value ' => $ appliedTaxes [$ appliedTaxesKeyIndex ]['amount ' ] ?? 0 ,
174
- 'currency ' => $ order ->getOrderCurrencyCode ()
175
- ],
176
- ];
177
- if (!empty ($ appliedTaxes [$ appliedTaxesKeyIndex ])) {
178
- $ appliedTaxesArray ['rate ' ] = $ appliedTaxes [$ appliedTaxesKeyIndex ]['percent ' ] ?? null ;
179
- }
180
- $ taxes [] = $ appliedTaxesArray ;
191
+ $ shippingDiscounts = [];
192
+ if (!($ order ->getDiscountDescription () === null && $ order ->getShippingDiscountAmount () == 0 )) {
193
+ $ shippingDiscounts [] =
194
+ [
195
+ 'label ' => $ order ->getDiscountDescription () ?? __ ('Discount ' ),
196
+ 'amount ' => [
197
+ 'value ' => abs ($ order ->getShippingDiscountAmount ()),
198
+ 'currency ' => $ order ->getOrderCurrencyCode ()
199
+ ]
200
+ ];
181
201
}
182
- return $ taxes ;
202
+ return $ shippingDiscounts ;
183
203
}
184
204
}
0 commit comments