1111use Magento \Framework \GraphQl \Config \Element \Field ;
1212use Magento \Framework \GraphQl \Query \ResolverInterface ;
1313use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14- use Magento \Sales \Api \Data \OrderExtensionInterface ;
1514use Magento \Sales \Api \Data \OrderInterface ;
1615
16+ /**
17+ * Resolve order totals taxes and discounts for order
18+ */
1719class OrderTotal implements ResolverInterface
1820{
1921 /**
@@ -33,21 +35,13 @@ public function resolve(
3335 /** @var OrderInterface $order */
3436 $ order = $ value ['model ' ];
3537 $ currency = $ order ->getOrderCurrencyCode ();
36- $ extensionAttributes = $ order ->getExtensionAttributes ();
37-
38- $ allAppliedTaxesForItemsData = $ this ->getAllAppliedTaxesForItems (
39- $ extensionAttributes ->getItemAppliedTaxes () ?? []
40- );
41- $ appliedShippingTaxesForItemsData = $ this ->getAppliedShippingTaxesForItems (
42- $ extensionAttributes ->getItemAppliedTaxes () ?? []
43- );
4438
4539 return [
4640 'base_grand_total ' => ['value ' => $ order ->getBaseGrandTotal (), 'currency ' => $ currency ],
4741 'grand_total ' => ['value ' => $ order ->getGrandTotal (), 'currency ' => $ currency ],
4842 'subtotal ' => ['value ' => $ order ->getSubtotal (), 'currency ' => $ currency ],
4943 'total_tax ' => ['value ' => $ order ->getTaxAmount (), 'currency ' => $ currency ],
50- 'taxes ' => $ this ->getAppliedTaxesDetails ($ order, $ allAppliedTaxesForItemsData ),
44+ 'taxes ' => $ this ->getAppliedTaxesDetails ($ order ),
5145 'discounts ' => $ this ->getDiscountDetails ($ order ),
5246 'total_shipping ' => ['value ' => $ order ->getShippingAmount (), 'currency ' => $ currency ],
5347 'shipping_handling ' => [
@@ -63,54 +57,55 @@ public function resolve(
6357 'value ' => $ order ->getShippingAmount (),
6458 'currency ' => $ currency
6559 ],
66- 'taxes ' => $ this ->getAppliedTaxesDetails ($ order, $ appliedShippingTaxesForItemsData ),
60+ 'taxes ' => $ this ->getAppliedShippingTaxesDetails ($ order ),
6761 'discounts ' => $ this ->getShippingDiscountDetails ($ order ),
6862 ]
6963 ];
7064 }
7165
7266 /**
73- * Retrieve applied taxes that apply to items
67+ * Retrieve applied taxes that apply to the order
7468 *
75- * @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[] $itemAppliedTaxes
69+ * @param OrderInterface $order
7670 * @return array
7771 */
78- private function getAllAppliedTaxesForItems ( array $ itemAppliedTaxes ): array
72+ private function getAllAppliedTaxesOnOrders ( OrderInterface $ order ): array
7973 {
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+ ];
8983 }
90- return $ allAppliedTaxesForItemsData ;
84+ return $ allAppliedTaxOnOrders ;
9185 }
9286
9387 /**
94- * Retrieve applied taxes that apply to shipping
88+ * Return taxes applied to the current order
9589 *
96- * @param \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface $itemAppliedTaxes
90+ * @param OrderInterface $order
9791 * @return array
9892 */
99- private function getAppliedShippingTaxesForItems ( array $ itemAppliedTaxes ): array
93+ private function getAppliedTaxesDetails ( OrderInterface $ order ): array
10094 {
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 ;
112107 }
113- return $ appliedShippingTaxesForItemsData ;
108+ return $ taxes ;
114109 }
115110
116111 /**
@@ -119,66 +114,91 @@ private function getAppliedShippingTaxesForItems(array $itemAppliedTaxes): array
119114 * @param OrderInterface $order
120115 * @return array
121116 */
122- private function getShippingDiscountDetails (OrderInterface $ order )
117+ private function getDiscountDetails (OrderInterface $ order ): array
123118 {
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+ ];
134128 }
135- return $ shippingDiscounts ;
129+ return $ orderDiscounts ;
136130 }
137131
138132 /**
139- * Return information about an applied discount
133+ * Retrieve applied shipping taxes on items for the orders
140134 *
141135 * @param OrderInterface $order
142136 * @return array
143137 */
144- private function getDiscountDetails (OrderInterface $ order )
138+ private function getAppliedShippingTaxesForItems (OrderInterface $ order ): array
145139 {
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 ,
150173 'amount ' => [
151- 'value ' => $ order -> getDiscountAmount () ,
174+ 'value ' => $ appliedShippingTaxes [ ' amount ' ] ?? 0 ,
152175 'currency ' => $ order ->getOrderCurrencyCode ()
153176 ]
154177 ];
178+ $ shippingTaxes [] = $ appliedShippingTaxesArray ;
155179 }
156- return $ discounts ;
180+ return $ shippingTaxes ;
157181 }
158182
159183 /**
160- * Returns taxes applied to the current order
184+ * Return information about an applied discount
161185 *
162186 * @param OrderInterface $order
163- * @param array $appliedTaxesArray
164187 * @return array
165188 */
166- private function getAppliedTaxesDetails (OrderInterface $ order, array $ appliedTaxesArray ): array
189+ private function getShippingDiscountDetails (OrderInterface $ order ): array
167190 {
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+ ];
181201 }
182- return $ taxes ;
202+ return $ shippingDiscounts ;
183203 }
184204}
0 commit comments