10
10
use Magento \Framework \Exception \LocalizedException ;
11
11
use Magento \Framework \GraphQl \Config \Element \Field ;
12
12
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
13
- use Magento \GraphQl \Model \Query \ContextInterface ;
14
13
use Magento \Framework \GraphQl \Query \ResolverInterface ;
15
14
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
15
+ use Magento \GraphQl \Model \Query \ContextInterface ;
16
16
use Magento \Sales \Model \Order ;
17
17
18
18
class OrderTotal implements ResolverInterface
@@ -36,40 +36,117 @@ public function resolve(
36
36
throw new LocalizedException (__ ('"model" value should be specified ' ));
37
37
}
38
38
39
- /** @var Order $orderModel */
40
- $ orderModel = $ value ['model ' ];
41
- $ currency = $ orderModel ->getOrderCurrencyCode ();
42
- $ totals = [
43
- 'base_grand_total ' => ['value ' => $ orderModel ->getBaseGrandTotal (), 'currency ' => $ currency ],
44
- 'grand_total ' => ['value ' => $ orderModel ->getGrandTotal (), 'currency ' => $ currency ],
45
- 'subtotal ' => ['value ' => $ orderModel ->getSubtotal (), 'currency ' => $ currency ],
46
- 'total_tax ' => ['value ' => $ orderModel ->getTaxAmount (), 'currency ' => $ currency ],
47
- 'taxes ' => $ this ->getAppliedTaxes ($ orderModel ),
48
- 'total_shipping ' => ['value ' => $ orderModel ->getShippingAmount (), 'currency ' => $ currency ],
49
- 'shipping_handling ' => [
50
- 'amount_exc_tax ' => ['value ' => $ orderModel ->getShippingTaxAmount (), 'currency ' => $ currency ],
51
- 'amount_inc_tax ' => ['value ' => $ orderModel ->getShippingInclTax (), 'currency ' => $ currency ],
52
- 'total_amount ' => ['value ' => $ orderModel ->getBaseShippingAmount (), 'currency ' => $ currency ],
53
- 'taxes ' => $ this ->getAppliedTaxes ($ orderModel )
54
- ]
39
+ /** @var Order $order */
40
+ $ order = $ value ['model ' ];
41
+ $ currency = $ order ->getOrderCurrencyCode ();
42
+ $ extensionAttributes = $ order ->getExtensionAttributes ();
43
+ $ appliedTaxesForItems = $ extensionAttributes ->getItemAppliedTaxes ();
44
+ $ allAppliedTaxesForItemsData [] = [];
45
+ $ appliedShippingTaxesForItemsData [] = [];
46
+ if (!empty ($ appliedTaxesForItems )) {
47
+ foreach ($ appliedTaxesForItems as $ key => $ appliedTaxForItem ) {
48
+ $ appliedTaxType = $ appliedTaxForItem ->getType ();
49
+ $ taxLineItems = $ appliedTaxForItem ->getAppliedTaxes ();
50
+ foreach ($ taxLineItems as $ taxLineItem ) {
51
+ if ($ appliedTaxType === "shipping " ) {
52
+ $ appliedShippingTaxesForItemsData [$ key ]['title ' ] = $ taxLineItem ->getDataByKey ('title ' );
53
+ $ appliedShippingTaxesForItemsData [$ key ]['percent ' ] = $ taxLineItem ->getDataByKey ('percent ' );
54
+ $ appliedShippingTaxesForItemsData [$ key ]['amount ' ] = $ taxLineItem ->getDataByKey ('amount ' );
55
+ }
56
+ $ allAppliedTaxesForItemsData [$ key ]['title ' ] = $ taxLineItem ->getDataByKey ('title ' );
57
+ $ allAppliedTaxesForItemsData [$ key ]['percent ' ] = $ taxLineItem ->getDataByKey ('percent ' );
58
+ $ allAppliedTaxesForItemsData [$ key ]['amount ' ] = $ taxLineItem ->getDataByKey ('amount ' );
59
+ }
60
+ }
61
+ }
62
+
63
+ $ total = [
64
+ 'base_grand_total ' => ['value ' => $ order ->getBaseGrandTotal (), 'currency ' => $ currency ],
65
+ 'grand_total ' => ['value ' => $ order ->getGrandTotal (), 'currency ' => $ currency ],
66
+ 'subtotal ' => ['value ' => $ order ->getSubtotal (), 'currency ' => $ currency ],
67
+ 'total_tax ' => ['value ' => $ order ->getTaxAmount (), 'currency ' => $ currency ],
68
+ 'taxes ' => $ this ->getAppliedTaxesDetails ($ order , $ allAppliedTaxesForItemsData ),
69
+ 'discounts ' => $ this ->getDiscountDetails ($ order ),
70
+ 'total_shipping ' => ['value ' => $ order ->getShippingAmount (), 'currency ' => $ currency ],
71
+ 'shipping_handling ' => [
72
+ 'amount_excluding_tax ' => ['value ' => $ order ->getShippingAmount (), 'currency ' => $ order ->getOrderCurrencyCode ()],
73
+ 'amount_including_tax ' => ['value ' => $ order ->getShippingInclTax (), 'currency ' => $ currency ],
74
+ 'total_amount ' => ['value ' => $ order ->getBaseShippingAmount (), 'currency ' => $ currency ],
75
+ 'taxes ' => $ this ->getAppliedTaxesDetails ($ order , $ appliedShippingTaxesForItemsData ),
76
+ 'discounts ' => $ this ->getShippingDiscountDetails ($ order ),
77
+
78
+ ]
55
79
];
56
- return $ totals ;
80
+ return $ total ;
57
81
}
58
82
59
83
/**
60
- * Returns taxes applied to the current order
84
+ * Returns information about an applied discount
61
85
*
62
- * @param Order $orderModel
63
- * @return array
86
+ * @param Order $order
87
+ * @return array|null
64
88
*/
65
- private function getAppliedTaxes (Order $ orderModel ): array
89
+ private function getShippingDiscountDetails (Order $ order )
66
90
{
67
- $ taxes [] = [
68
- 'rate ' => $ orderModel ->getStoreToOrderRate (),
69
- 'title ' => $ orderModel ->getCustomerName (),
70
- 'amount ' => [ 'value ' => $ orderModel ->getTaxAmount (), 'currency ' => $ orderModel ->getOrderCurrencyCode ()
91
+ if ($ order ->getDiscountDescription () === null && $ order ->getShippingDiscountAmount () == 0 ) {
92
+ return null ;
93
+ }
94
+
95
+ $ shippingDiscounts [] =
96
+ [
97
+ 'label ' => $ order ->getDiscountDescription () ?? "null " ,
98
+ 'amount ' => [
99
+ 'value ' => $ order ->getShippingDiscountAmount (),
100
+ 'currency ' => $ order ->getOrderCurrencyCode ()
101
+ ]
102
+ ];
103
+ return $ shippingDiscounts ;
104
+ }
105
+
106
+ /**
107
+ * Returns information about an applied discount
108
+ *
109
+ * @param Order $order
110
+ * @return array|null
111
+ */
112
+ private function getDiscountDetails (Order $ order )
113
+ {
114
+ if ($ order ->getDiscountDescription () === null && $ order ->getDiscountAmount () == 0 ) {
115
+ return null ;
116
+ }
117
+
118
+ $ discounts [] = [
119
+ 'label ' => $ order ->getDiscountDescription () ?? "null " ,
120
+ 'amount ' => [
121
+ 'value ' => $ order ->getDiscountAmount (),
122
+ 'currency ' => $ order ->getOrderCurrencyCode ()
71
123
]
72
124
];
73
- return $ taxes ;
125
+ return $ discounts ;
126
+ }
127
+
128
+ /**
129
+ * Returns taxes applied to the current order
130
+ *
131
+ * @param Order $order
132
+ * @param array $appliedTaxesArray
133
+ * @return array|null
134
+ */
135
+ private function getAppliedTaxesDetails (Order $ order , array $ appliedTaxesArray ): array
136
+ {
137
+ if (empty ($ appliedTaxesArray )) {
138
+ $ taxes [] = null ;
139
+ } else {
140
+ foreach ($ appliedTaxesArray as $ appliedTaxes ) {
141
+ $ taxes [] = [
142
+ 'rate ' => $ appliedTaxes ['percent ' ] ?? 0 ,
143
+ 'title ' => $ appliedTaxes ['title ' ] ?? " " ,
144
+ 'amount ' => ['value ' => $ appliedTaxes ['amount ' ] ?? 0 , 'currency ' => $ order ->getOrderCurrencyCode ()
145
+ ]
146
+ ];
147
+ }
148
+ /** @var array $taxes */
149
+ return $ taxes ;
150
+ }
74
151
}
75
152
}
0 commit comments