8
8
namespace Magento \SalesGraphQl \Model \Resolver ;
9
9
10
10
use Magento \Framework \Exception \LocalizedException ;
11
+ use Magento \Framework \Exception \NoSuchEntityException ;
11
12
use Magento \Framework \GraphQl \Config \Element \Field ;
12
13
use Magento \Framework \GraphQl \Query \ResolverInterface ;
13
14
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
15
use Magento \Sales \Api \Data \OrderInterface ;
16
+ use Magento \Tax \Api \OrderTaxManagementInterface ;
15
17
16
18
/**
17
19
* Resolve order totals taxes and discounts for order
18
20
*/
19
21
class OrderTotal implements ResolverInterface
20
22
{
23
+
24
+ /**
25
+ * OrderTotal Constructor
26
+ *
27
+ * @param OrderTaxManagementInterface $orderTaxManagement
28
+ */
29
+ public function __construct (
30
+ private readonly OrderTaxManagementInterface $ orderTaxManagement ,
31
+ ) {
32
+ }
33
+
21
34
/**
22
35
* @inheritDoc
23
36
*/
@@ -35,10 +48,12 @@ public function resolve(
35
48
/** @var OrderInterface $order */
36
49
$ order = $ value ['model ' ];
37
50
$ currency = $ order ->getOrderCurrencyCode ();
38
- $ baseCurrency = $ order ->getBaseCurrencyCode ();
39
51
40
52
return [
41
- 'base_grand_total ' => ['value ' => $ order ->getBaseGrandTotal (), 'currency ' => $ baseCurrency ],
53
+ 'base_grand_total ' => [
54
+ 'value ' => $ order ->getBaseGrandTotal (),
55
+ 'currency ' => $ order ->getBaseCurrencyCode ()
56
+ ],
42
57
'grand_total ' => ['value ' => $ order ->getGrandTotal (), 'currency ' => $ currency ],
43
58
'subtotal ' => ['value ' => $ order ->getSubtotal (), 'currency ' => $ currency ],
44
59
'subtotal_incl_tax ' => ['value ' => $ order ->getSubtotalInclTax (), 'currency ' => $ currency ],
@@ -72,44 +87,40 @@ public function resolve(
72
87
*
73
88
* @param OrderInterface $order
74
89
* @return array
90
+ * @throws NoSuchEntityException
75
91
*/
76
92
private function getAllAppliedTaxesOnOrders (OrderInterface $ order ): array
77
93
{
78
- $ extensionAttributes = $ order ->getExtensionAttributes ();
79
- $ appliedTaxes = $ extensionAttributes ->getAppliedTaxes () ?? [];
80
- $ allAppliedTaxOnOrders = [];
81
- foreach ($ appliedTaxes as $ taxIndex => $ appliedTaxesData ) {
82
- $ allAppliedTaxOnOrders [$ taxIndex ] = [
94
+ return array_map (
95
+ fn ($ appliedTaxesData ) => [
83
96
'title ' => $ appliedTaxesData ->getDataByKey ('title ' ),
84
97
'percent ' => $ appliedTaxesData ->getDataByKey ('percent ' ),
85
98
'amount ' => $ appliedTaxesData ->getDataByKey ('amount ' ),
86
- ];
87
- }
88
- return $ allAppliedTaxOnOrders ;
99
+ ],
100
+ $ this -> orderTaxManagement -> getOrderTaxDetails ( $ order -> getEntityId ())-> getAppliedTaxes ()
101
+ ) ;
89
102
}
90
103
91
104
/**
92
105
* Return taxes applied to the current order
93
106
*
94
107
* @param OrderInterface $order
95
108
* @return array
109
+ * @throws NoSuchEntityException
96
110
*/
97
111
private function getAppliedTaxesDetails (OrderInterface $ order ): array
98
112
{
99
- $ allAppliedTaxOnOrders = $ this ->getAllAppliedTaxesOnOrders ($ order );
100
- $ taxes = [];
101
- foreach ($ allAppliedTaxOnOrders as $ appliedTaxes ) {
102
- $ appliedTaxesArray = [
113
+ return array_map (
114
+ fn ($ appliedTaxes ) => [
103
115
'rate ' => $ appliedTaxes ['percent ' ] ?? 0 ,
104
116
'title ' => $ appliedTaxes ['title ' ] ?? null ,
105
117
'amount ' => [
106
118
'value ' => $ appliedTaxes ['amount ' ] ?? 0 ,
107
119
'currency ' => $ order ->getOrderCurrencyCode ()
108
120
]
109
- ];
110
- $ taxes [] = $ appliedTaxesArray ;
111
- }
112
- return $ taxes ;
121
+ ],
122
+ $ this ->getAllAppliedTaxesOnOrders ($ order )
123
+ );
113
124
}
114
125
115
126
/**
0 commit comments