@@ -32,22 +32,13 @@ public function resolve(
32
32
/** @var OrderInterface $order */
33
33
$ order = $ value ['model ' ];
34
34
$ 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
- );
44
35
45
36
return [
46
37
'base_grand_total ' => ['value ' => $ order ->getBaseGrandTotal (), 'currency ' => $ currency ],
47
38
'grand_total ' => ['value ' => $ order ->getGrandTotal (), 'currency ' => $ currency ],
48
39
'subtotal ' => ['value ' => $ order ->getSubtotal (), 'currency ' => $ currency ],
49
40
'total_tax ' => ['value ' => $ order ->getTaxAmount (), 'currency ' => $ currency ],
50
- 'taxes ' => $ this ->getAppliedTaxesDetails ($ order, $ allAppliedTaxOnOrdersData ),
41
+ 'taxes ' => $ this ->getAppliedTaxesDetails ($ order ),
51
42
'discounts ' => $ this ->getDiscountDetails ($ order ),
52
43
'total_shipping ' => ['value ' => $ order ->getShippingAmount (), 'currency ' => $ currency ],
53
44
'shipping_handling ' => [
@@ -63,7 +54,7 @@ public function resolve(
63
54
'value ' => $ order ->getShippingAmount (),
64
55
'currency ' => $ currency
65
56
],
66
- 'taxes ' => $ this ->getAppliedShippingTaxesDetails ($ order, $ appliedShippingTaxesForItemsData ),
57
+ 'taxes ' => $ this ->getAppliedShippingTaxesDetails ($ order ),
67
58
'discounts ' => $ this ->getShippingDiscountDetails ($ order ),
68
59
]
69
60
];
@@ -72,66 +63,46 @@ public function resolve(
72
63
/**
73
64
* Retrieve applied taxes that apply to the order
74
65
*
75
- * @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $appliedTaxes
66
+ * @param OrderInterface $order
76
67
* @return array
77
68
*/
78
- private function getAllAppliedTaxesOnOrders (array $ appliedTaxes ): array
69
+ private function getAllAppliedTaxesOnOrders (OrderInterface $ order ): array
79
70
{
80
- $ allAppliedTaxOnOrdersData = [];
71
+ $ extensionAttributes = $ order ->getExtensionAttributes ();
72
+ $ appliedTaxes = $ extensionAttributes ->getAppliedTaxes () ?? [];
73
+ $ allAppliedTaxOnOrders = [];
81
74
foreach ($ appliedTaxes as $ taxIndex => $ appliedTaxesData ) {
82
- $ allAppliedTaxOnOrdersData [ $ taxIndex ] [$ taxIndex ] = [
75
+ $ allAppliedTaxOnOrders [$ taxIndex ] = [
83
76
'title ' => $ appliedTaxesData ->getDataByKey ('title ' ),
84
77
'percent ' => $ appliedTaxesData ->getDataByKey ('percent ' ),
85
78
'amount ' => $ appliedTaxesData ->getDataByKey ('amount ' ),
86
79
];
87
80
}
88
- return $ allAppliedTaxOnOrdersData ;
81
+ return $ allAppliedTaxOnOrders ;
89
82
}
90
83
91
84
/**
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
117
86
*
118
87
* @param OrderInterface $order
119
88
* @return array
120
89
*/
121
- private function getShippingDiscountDetails (OrderInterface $ order )
90
+ private function getAppliedTaxesDetails (OrderInterface $ order ): array
122
91
{
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 ;
133
104
}
134
- return $ shippingDiscounts ;
105
+ return $ taxes ;
135
106
}
136
107
137
108
/**
@@ -140,74 +111,91 @@ private function getShippingDiscountDetails(OrderInterface $order)
140
111
* @param OrderInterface $order
141
112
* @return array
142
113
*/
143
- private function getDiscountDetails (OrderInterface $ order )
114
+ private function getDiscountDetails (OrderInterface $ order ): array
144
115
{
145
- $ discounts = [];
116
+ $ orderDiscounts = [];
146
117
if (!($ order ->getDiscountDescription () === null && $ order ->getDiscountAmount () == 0 )) {
147
- $ discounts [] = [
118
+ $ orderDiscounts [] = [
148
119
'label ' => $ order ->getDiscountDescription () ?? __ ('Discount ' ),
149
120
'amount ' => [
150
- 'value ' => $ order ->getDiscountAmount (),
121
+ 'value ' => abs ( $ order ->getDiscountAmount () ),
151
122
'currency ' => $ order ->getOrderCurrencyCode ()
152
123
]
153
124
];
154
125
}
155
- return $ discounts ;
126
+ return $ orderDiscounts ;
156
127
}
157
128
158
129
/**
159
- * Returns taxes applied to the current order
130
+ * Retrieve applied shipping taxes on items for the orders
160
131
*
161
132
* @param OrderInterface $order
162
- * @param array $appliedTaxesArray
163
133
* @return array
164
134
*/
165
- private function getAppliedTaxesDetails (OrderInterface $ order, array $ appliedTaxesArray ): array
135
+ private function getAppliedShippingTaxesForItems (OrderInterface $ order ): array
166
136
{
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
+ }
178
150
}
179
- $ taxes [] = $ appliedTaxesArray ;
180
151
}
181
- return $ taxes ;
152
+ return $ appliedShippingTaxesForItems ;
182
153
}
183
154
184
155
/**
185
- * Returns taxes applied to the current order
156
+ * Return taxes applied to the current order
186
157
*
187
158
* @param OrderInterface $order
188
- * @param array $appliedShippingTaxesForItemsData
189
159
* @return array
190
160
*/
191
161
private function getAppliedShippingTaxesDetails (
192
- OrderInterface $ order ,
193
- array $ appliedShippingTaxesForItemsData
162
+ OrderInterface $ order
194
163
): array {
164
+ $ appliedShippingTaxesForItems = $ this ->getAppliedShippingTaxesForItems ($ order );
195
165
$ 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 ' ),
200
193
'amount ' => [
201
- 'value ' => $ appliedShippingTax [ ' amount ' ] ?? 0 ,
194
+ 'value ' => abs ( $ order -> getShippingDiscountAmount ()) ,
202
195
'currency ' => $ order ->getOrderCurrencyCode ()
203
- ],
196
+ ]
204
197
];
205
- if (!empty ($ appliedShippingTax )) {
206
- $ appliedShippingTaxesArray ['rate ' ] = $ appliedShippingTax ['percent ' ] ?? 0 ;
207
- }
208
- $ shippingTaxes [] = $ appliedShippingTaxesArray ;
209
- }
210
198
}
211
- return $ shippingTaxes ;
199
+ return $ shippingDiscounts ;
212
200
}
213
201
}
0 commit comments