@@ -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}
0 commit comments