88namespace Magento \SalesGraphQl \Model \Resolver ;
99
1010use Magento \Framework \Exception \LocalizedException ;
11+ use Magento \Framework \Exception \NoSuchEntityException ;
1112use Magento \Framework \GraphQl \Config \Element \Field ;
1213use Magento \Framework \GraphQl \Query \ResolverInterface ;
1314use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
1415use Magento \Sales \Api \Data \OrderInterface ;
16+ use Magento \Tax \Api \OrderTaxManagementInterface ;
1517
1618/**
1719 * Resolve order totals taxes and discounts for order
1820 */
1921class OrderTotal implements ResolverInterface
2022{
23+
24+ /**
25+ * OrderTotal Constructor
26+ *
27+ * @param OrderTaxManagementInterface $orderTaxManagement
28+ */
29+ public function __construct (
30+ private readonly OrderTaxManagementInterface $ orderTaxManagement ,
31+ ) {
32+ }
33+
2134 /**
2235 * @inheritDoc
2336 */
@@ -35,10 +48,12 @@ public function resolve(
3548 /** @var OrderInterface $order */
3649 $ order = $ value ['model ' ];
3750 $ currency = $ order ->getOrderCurrencyCode ();
38- $ baseCurrency = $ order ->getBaseCurrencyCode ();
3951
4052 return [
41- 'base_grand_total ' => ['value ' => $ order ->getBaseGrandTotal (), 'currency ' => $ baseCurrency ],
53+ 'base_grand_total ' => [
54+ 'value ' => $ order ->getBaseGrandTotal (),
55+ 'currency ' => $ order ->getBaseCurrencyCode ()
56+ ],
4257 'grand_total ' => ['value ' => $ order ->getGrandTotal (), 'currency ' => $ currency ],
4358 'subtotal ' => ['value ' => $ order ->getSubtotal (), 'currency ' => $ currency ],
4459 'subtotal_incl_tax ' => ['value ' => $ order ->getSubtotalInclTax (), 'currency ' => $ currency ],
@@ -72,44 +87,40 @@ public function resolve(
7287 *
7388 * @param OrderInterface $order
7489 * @return array
90+ * @throws NoSuchEntityException
7591 */
7692 private function getAllAppliedTaxesOnOrders (OrderInterface $ order ): array
7793 {
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 ) => [
8396 'title ' => $ appliedTaxesData ->getDataByKey ('title ' ),
8497 'percent ' => $ appliedTaxesData ->getDataByKey ('percent ' ),
8598 'amount ' => $ appliedTaxesData ->getDataByKey ('amount ' ),
86- ];
87- }
88- return $ allAppliedTaxOnOrders ;
99+ ],
100+ $ this -> orderTaxManagement -> getOrderTaxDetails ( $ order -> getEntityId ())-> getAppliedTaxes ()
101+ ) ;
89102 }
90103
91104 /**
92105 * Return taxes applied to the current order
93106 *
94107 * @param OrderInterface $order
95108 * @return array
109+ * @throws NoSuchEntityException
96110 */
97111 private function getAppliedTaxesDetails (OrderInterface $ order ): array
98112 {
99- $ allAppliedTaxOnOrders = $ this ->getAllAppliedTaxesOnOrders ($ order );
100- $ taxes = [];
101- foreach ($ allAppliedTaxOnOrders as $ appliedTaxes ) {
102- $ appliedTaxesArray = [
113+ return array_map (
114+ fn ($ appliedTaxes ) => [
103115 'rate ' => $ appliedTaxes ['percent ' ] ?? 0 ,
104116 'title ' => $ appliedTaxes ['title ' ] ?? null ,
105117 'amount ' => [
106118 'value ' => $ appliedTaxes ['amount ' ] ?? 0 ,
107119 'currency ' => $ order ->getOrderCurrencyCode ()
108120 ]
109- ];
110- $ taxes [] = $ appliedTaxesArray ;
111- }
112- return $ taxes ;
121+ ],
122+ $ this ->getAllAppliedTaxesOnOrders ($ order )
123+ );
113124 }
114125
115126 /**
0 commit comments