13
13
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
14
use Magento \Sales \Api \Data \CreditmemoInterface ;
15
15
use Magento \Sales \Api \Data \OrderInterface ;
16
+ use Magento \SalesGraphQl \Model \SalesItem \ShippingTaxCalculator ;
17
+ use Magento \Tax \Api \OrderTaxManagementInterface ;
18
+ use Magento \Tax \Helper \Data as TaxHelper ;
16
19
17
20
/**
18
21
* Resolve credit memo totals information
19
22
*/
20
23
class CreditMemoTotal implements ResolverInterface
21
24
{
25
+ /**
26
+ * @var TaxHelper
27
+ */
28
+ private $ taxHelper ;
29
+
30
+ /**
31
+ * @var OrderTaxManagementInterface
32
+ */
33
+ private $ orderTaxManagement ;
34
+
35
+ /**
36
+ * @var ShippingTaxCalculator
37
+ */
38
+ private $ shippingTaxCalculator ;
39
+ /**
40
+ * @param OrderTaxManagementInterface $orderTaxManagement
41
+ * @param TaxHelper $taxHelper
42
+ * @param ShippingTaxCalculator $shippingTaxCalculator
43
+ */
44
+ public function __construct (
45
+ OrderTaxManagementInterface $ orderTaxManagement ,
46
+ TaxHelper $ taxHelper ,
47
+ ShippingTaxCalculator $ shippingTaxCalculator
48
+ ) {
49
+ $ this ->taxHelper = $ taxHelper ;
50
+ $ this ->orderTaxManagement = $ orderTaxManagement ;
51
+ $ this ->shippingTaxCalculator = $ shippingTaxCalculator ;
52
+ }
53
+
22
54
/**
23
55
* @inheritDoc
24
56
*/
@@ -42,32 +74,109 @@ public function resolve(
42
74
/** @var CreditmemoInterface $creditMemo */
43
75
$ creditMemo = $ value ['model ' ];
44
76
$ currency = $ orderModel ->getOrderCurrencyCode ();
45
-
77
+ $ baseCurrency = $ orderModel -> getBaseCurrencyCode ();
46
78
return [
47
- 'subtotal ' => [
48
- 'value ' => $ creditMemo ->getSubtotal (),
49
- 'currency ' => $ currency
50
- ],
51
- 'base_grand_total ' => [
52
- 'value ' => $ creditMemo ->getBaseGrandTotal (),
53
- 'currency ' => $ currency
54
- ],
55
- 'grand_total ' => [
56
- 'value ' => $ creditMemo ->getGrandTotal (),
57
- 'currency ' => $ currency
58
- ],
59
- 'total_tax ' => [
60
- 'value ' => $ creditMemo ->getTaxAmount (),
61
- 'currency ' => $ currency
62
- ],
63
- 'shipping_amount ' => [
64
- 'value ' => $ creditMemo ->getShippingAmount (),
65
- 'currency ' => $ currency
79
+ 'base_grand_total ' => ['value ' => $ creditMemo ->getBaseGrandTotal (), 'currency ' => $ baseCurrency ],
80
+ 'grand_total ' => ['value ' => $ creditMemo ->getGrandTotal (), 'currency ' => $ currency ],
81
+ 'subtotal ' => ['value ' => $ creditMemo ->getSubtotal (), 'currency ' => $ currency ],
82
+ 'total_tax ' => ['value ' => $ creditMemo ->getTaxAmount (), 'currency ' => $ currency ],
83
+ 'total_shipping ' => ['value ' => $ creditMemo ->getShippingAmount (), 'currency ' => $ currency ],
84
+ 'discounts ' => $ this ->getDiscountDetails ($ creditMemo ),
85
+ 'taxes ' => $ this ->formatTaxes (
86
+ $ orderModel ,
87
+ $ this ->taxHelper ->getCalculatedTaxes ($ creditMemo ),
88
+ ),
89
+ 'shipping_handling ' => [
90
+ 'amount_excluding_tax ' => [
91
+ 'value ' => $ creditMemo ->getShippingAmount () ?? 0 ,
92
+ 'currency ' => $ currency
93
+ ],
94
+ 'amount_including_tax ' => [
95
+ 'value ' => $ creditMemo ->getShippingInclTax () ?? 0 ,
96
+ 'currency ' => $ currency
97
+ ],
98
+ 'total_amount ' => [
99
+ 'value ' => $ creditMemo ->getShippingAmount () ?? 0 ,
100
+ 'currency ' => $ currency
101
+ ],
102
+ 'discounts ' => $ this ->getShippingDiscountDetails ($ creditMemo ),
103
+ 'taxes ' => $ this ->formatTaxes (
104
+ $ orderModel ,
105
+ $ this ->shippingTaxCalculator ->calculateShippingTaxes ($ orderModel , $ creditMemo ),
106
+ )
66
107
],
67
108
'adjustment ' => [
68
- 'value ' => $ creditMemo ->getAdjustment (),
109
+ 'value ' => abs ( $ creditMemo ->getAdjustment () ),
69
110
'currency ' => $ currency
70
111
],
71
112
];
72
113
}
114
+
115
+ /**
116
+ * Return information about an applied discount on shipping
117
+ *
118
+ * @param CreditmemoInterface $creditmemo
119
+ * @return array
120
+ */
121
+ private function getShippingDiscountDetails (CreditmemoInterface $ creditmemo )
122
+ {
123
+ $ shippingDiscounts = [];
124
+ if (!($ creditmemo ->getDiscountDescription () === null
125
+ && $ creditmemo ->getShippingDiscountTaxCompensationAmount () == 0 )) {
126
+ $ shippingDiscounts [] =
127
+ [
128
+ 'label ' => $ creditmemo ->getDiscountDescription () ?? __ ('Discount ' ),
129
+ 'amount ' => [
130
+ 'value ' => abs ($ creditmemo ->getShippingDiscountTaxCompensationAmount ()),
131
+ 'currency ' => $ creditmemo ->getOrderCurrencyCode ()
132
+ ]
133
+ ];
134
+ }
135
+ return $ shippingDiscounts ;
136
+ }
137
+
138
+ /**
139
+ * Return information about an applied discount
140
+ *
141
+ * @param CreditmemoInterface $creditmemo
142
+ * @return array
143
+ */
144
+ private function getDiscountDetails (CreditmemoInterface $ creditmemo )
145
+ {
146
+ $ discounts = [];
147
+ if (!($ creditmemo ->getDiscountDescription () === null && $ creditmemo ->getDiscountAmount () == 0 )) {
148
+ $ discounts [] = [
149
+ 'label ' => $ creditmemo ->getDiscountDescription () ?? __ ('Discount ' ),
150
+ 'amount ' => [
151
+ 'value ' => abs ($ creditmemo ->getDiscountAmount ()),
152
+ 'currency ' => $ creditmemo ->getOrderCurrencyCode ()
153
+ ]
154
+ ];
155
+ }
156
+ return $ discounts ;
157
+ }
158
+
159
+ /**
160
+ * Format applied taxes
161
+ *
162
+ * @param OrderInterface $order
163
+ * @param array $appliedTaxes
164
+ * @return array
165
+ */
166
+ private function formatTaxes (OrderInterface $ order , array $ appliedTaxes )
167
+ {
168
+ $ taxes = [];
169
+ foreach ($ appliedTaxes as $ appliedTax ) {
170
+ $ appliedTaxesArray = [
171
+ 'rate ' => $ appliedTax ['percent ' ] ?? 0 ,
172
+ 'title ' => $ appliedTax ['title ' ] ?? null ,
173
+ 'amount ' => [
174
+ 'value ' => $ appliedTax ['tax_amount ' ] ?? 0 ,
175
+ 'currency ' => $ order ->getOrderCurrencyCode ()
176
+ ]
177
+ ];
178
+ $ taxes [] = $ appliedTaxesArray ;
179
+ }
180
+ return $ taxes ;
181
+ }
73
182
}
0 commit comments