5
5
*/
6
6
namespace Magento \Sales \Model \Order \Creditmemo \Total ;
7
7
8
+ use Magento \Sales \Model \Order \Creditmemo ;
9
+
8
10
/**
9
11
* Collects credit memo taxes.
10
12
*/
11
13
class Tax extends AbstractTotal
12
14
{
13
15
/**
14
- * Collects credit memo taxes.
15
- *
16
- * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
17
- * @return $this
18
- *
16
+ * {@inheritdoc}
19
17
* @SuppressWarnings(PHPMD.NPathComplexity)
20
18
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
21
19
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
22
20
*/
23
- public function collect (\ Magento \ Sales \ Model \ Order \ Creditmemo $ creditmemo )
21
+ public function collect (Creditmemo $ creditmemo )
24
22
{
25
23
$ shippingTaxAmount = 0 ;
26
24
$ baseShippingTaxAmount = 0 ;
27
25
$ totalTax = 0 ;
28
26
$ baseTotalTax = 0 ;
29
27
$ totalDiscountTaxCompensation = 0 ;
30
28
$ baseTotalDiscountTaxCompensation = 0 ;
31
-
32
29
$ order = $ creditmemo ->getOrder ();
33
30
34
- /** @var $item \Magento\Sales\Model\Order\Creditmemo\Item */
35
31
foreach ($ creditmemo ->getAllItems () as $ item ) {
36
32
$ orderItem = $ item ->getOrderItem ();
37
33
if ($ orderItem ->isDummy () || $ item ->getQty () <= 0 ) {
38
34
continue ;
39
35
}
36
+
40
37
$ orderItemTax = (double )$ orderItem ->getTaxInvoiced ();
41
38
$ baseOrderItemTax = (double )$ orderItem ->getBaseTaxInvoiced ();
42
39
$ orderItemQty = (double )$ orderItem ->getQtyInvoiced ();
43
40
44
41
if ($ orderItemQty ) {
45
- /**
46
- * Check item tax amount
47
- */
48
-
49
- $ tax = $ orderItemTax - $ orderItem ->getTaxRefunded ();
50
- $ baseTax = $ baseOrderItemTax - $ orderItem ->getBaseTaxRefunded ();
51
- $ discountTaxCompensation = $ orderItem ->getDiscountTaxCompensationInvoiced () -
52
- $ orderItem ->getDiscountTaxCompensationRefunded ();
53
- $ baseDiscountTaxCompensation = $ orderItem ->getBaseDiscountTaxCompensationInvoiced () -
54
- $ orderItem ->getBaseDiscountTaxCompensationRefunded ();
42
+ /** Check item tax amount */
43
+ $ tax = ($ orderItemTax - $ orderItem ->getTaxRefunded ());
44
+ $ baseTax = ($ baseOrderItemTax - $ orderItem ->getBaseTaxRefunded ());
45
+ $ discountTaxCompensation = ($ orderItem ->getDiscountTaxCompensationInvoiced ()
46
+ - $ orderItem ->getDiscountTaxCompensationRefunded ());
47
+ $ baseDiscountTaxCompensation = ($ orderItem ->getBaseDiscountTaxCompensationInvoiced ()
48
+ - $ orderItem ->getBaseDiscountTaxCompensationRefunded ());
55
49
if (!$ item ->isLast ()) {
56
- $ availableQty = $ orderItemQty - $ orderItem ->getQtyRefunded ();
50
+ $ availableQty = ( $ orderItemQty - $ orderItem ->getQtyRefunded () );
57
51
$ tax = $ creditmemo ->roundPrice ($ tax / $ availableQty * $ item ->getQty ());
58
- $ baseTax = $ creditmemo ->roundPrice ($ baseTax / $ availableQty * $ item ->getQty (), 'base ' );
59
- $ discountTaxCompensation =
60
- $ creditmemo ->roundPrice ($ discountTaxCompensation / $ availableQty * $ item ->getQty ());
61
- $ baseDiscountTaxCompensation =
62
- $ creditmemo ->roundPrice ($ baseDiscountTaxCompensation / $ availableQty * $ item ->getQty (), 'base ' );
52
+ $ baseTax = $ creditmemo ->roundPrice (($ baseTax / $ availableQty * $ item ->getQty ()), 'base ' );
53
+ $ discountTaxCompensation = $ creditmemo ->roundPrice (
54
+ $ discountTaxCompensation / $ availableQty * $ item ->getQty ()
55
+ );
56
+ $ baseDiscountTaxCompensation = $ creditmemo ->roundPrice (
57
+ $ baseDiscountTaxCompensation / $ availableQty * $ item ->getQty (),
58
+ 'base '
59
+ );
63
60
}
64
61
65
62
$ item ->setTaxAmount ($ tax );
@@ -77,14 +74,14 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
77
74
$ isPartialShippingRefunded = false ;
78
75
$ baseOrderShippingAmount = (float )$ order ->getBaseShippingAmount ();
79
76
if ($ invoice = $ creditmemo ->getInvoice ()) {
80
- //recalculate tax amounts in case if refund shipping value was changed
77
+ // recalculate tax amounts in case if refund shipping value was changed
81
78
if ($ baseOrderShippingAmount && $ creditmemo ->getBaseShippingAmount () !== null ) {
82
- $ taxFactor = $ creditmemo ->getBaseShippingAmount () / $ baseOrderShippingAmount ;
83
- $ shippingTaxAmount = $ invoice ->getShippingTaxAmount () * $ taxFactor ;
84
- $ baseShippingTaxAmount = $ invoice ->getBaseShippingTaxAmount () * $ taxFactor ;
85
- $ totalDiscountTaxCompensation += $ invoice ->getShippingDiscountTaxCompensationAmount () * $ taxFactor ;
86
- $ baseTotalDiscountTaxCompensation +=
87
- $ invoice -> getBaseShippingDiscountTaxCompensationAmnt () * $ taxFactor ;
79
+ $ taxFactor = ( $ creditmemo ->getBaseShippingAmount () / $ baseOrderShippingAmount) ;
80
+ $ shippingTaxAmount = ( $ invoice ->getShippingTaxAmount () * $ taxFactor) ;
81
+ $ baseShippingTaxAmount = ( $ invoice ->getBaseShippingTaxAmount () * $ taxFactor) ;
82
+ $ totalDiscountTaxCompensation += ( $ invoice ->getShippingDiscountTaxCompensationAmount () * $ taxFactor) ;
83
+ $ baseTotalDiscountTaxCompensation += $ invoice -> getBaseShippingDiscountTaxCompensationAmnt ()
84
+ * $ taxFactor ;
88
85
$ shippingTaxAmount = $ creditmemo ->roundPrice ($ shippingTaxAmount );
89
86
$ baseShippingTaxAmount = $ creditmemo ->roundPrice ($ baseShippingTaxAmount , 'base ' );
90
87
$ totalDiscountTaxCompensation = $ creditmemo ->roundPrice ($ totalDiscountTaxCompensation );
@@ -97,61 +94,52 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
97
94
}
98
95
} else {
99
96
$ orderShippingAmount = $ order ->getShippingAmount ();
100
-
101
97
$ baseOrderShippingRefundedAmount = $ order ->getBaseShippingRefunded ();
102
-
103
98
$ shippingTaxAmount = 0 ;
104
99
$ baseShippingTaxAmount = 0 ;
105
100
$ shippingDiscountTaxCompensationAmount = 0 ;
106
101
$ baseShippingDiscountTaxCompensationAmount = 0 ;
107
-
108
- $ shippingDelta = $ baseOrderShippingAmount - $ baseOrderShippingRefundedAmount ;
102
+ $ shippingDelta = ($ baseOrderShippingAmount - $ baseOrderShippingRefundedAmount );
109
103
110
104
if ($ shippingDelta > $ creditmemo ->getBaseShippingAmount ()) {
111
- $ part = $ creditmemo ->getShippingAmount () / $ orderShippingAmount ;
112
- $ basePart = $ creditmemo ->getBaseShippingAmount () / $ baseOrderShippingAmount ;
113
- $ shippingTaxAmount = $ order ->getShippingTaxAmount () * $ part ;
114
- $ baseShippingTaxAmount = $ order ->getBaseShippingTaxAmount () * $ basePart ;
105
+ $ part = ( $ creditmemo ->getShippingAmount () / $ orderShippingAmount) ;
106
+ $ basePart = ( $ creditmemo ->getBaseShippingAmount () / $ baseOrderShippingAmount) ;
107
+ $ shippingTaxAmount = ( $ order ->getShippingTaxAmount () * $ part) ;
108
+ $ baseShippingTaxAmount = ( $ order ->getBaseShippingTaxAmount () * $ basePart) ;
115
109
$ shippingDiscountTaxCompensationAmount = $ order ->getShippingDiscountTaxCompensationAmount () * $ part ;
116
- $ baseShippingDiscountTaxCompensationAmount =
117
- $ order -> getBaseShippingDiscountTaxCompensationAmnt () * $ basePart ;
110
+ $ baseShippingDiscountTaxCompensationAmount = $ order -> getBaseShippingDiscountTaxCompensationAmnt ()
111
+ * $ basePart ;
118
112
$ shippingTaxAmount = $ creditmemo ->roundPrice ($ shippingTaxAmount );
119
113
$ baseShippingTaxAmount = $ creditmemo ->roundPrice ($ baseShippingTaxAmount , 'base ' );
120
- $ shippingDiscountTaxCompensationAmount =
121
- $ creditmemo ->roundPrice ($ shippingDiscountTaxCompensationAmount );
122
- $ baseShippingDiscountTaxCompensationAmount =
123
- $ creditmemo ->roundPrice ($ baseShippingDiscountTaxCompensationAmount , 'base ' );
114
+ $ shippingDiscountTaxCompensationAmount = $ creditmemo ->roundPrice (
115
+ $ shippingDiscountTaxCompensationAmount
116
+ );
117
+ $ baseShippingDiscountTaxCompensationAmount = $ creditmemo ->roundPrice (
118
+ $ baseShippingDiscountTaxCompensationAmount ,
119
+ 'base '
120
+ );
124
121
if ($ part < 1 && $ order ->getShippingTaxAmount () > 0 ) {
125
122
$ isPartialShippingRefunded = true ;
126
123
}
127
124
} elseif ($ shippingDelta == $ creditmemo ->getBaseShippingAmount ()) {
128
125
$ shippingTaxAmount = $ order ->getShippingTaxAmount () - $ order ->getShippingTaxRefunded ();
129
126
$ baseShippingTaxAmount = $ order ->getBaseShippingTaxAmount () - $ order ->getBaseShippingTaxRefunded ();
130
- $ shippingDiscountTaxCompensationAmount = $ order ->getShippingDiscountTaxCompensationAmount () -
131
- $ order ->getShippingDiscountTaxCompensationRefunded ();
132
- $ baseShippingDiscountTaxCompensationAmount = $ order ->getBaseShippingDiscountTaxCompensationAmnt () -
133
- $ order ->getBaseShippingDiscountTaxCompensationRefunded ();
127
+ $ shippingDiscountTaxCompensationAmount = $ order ->getShippingDiscountTaxCompensationAmount ()
128
+ - $ order ->getShippingDiscountTaxCompensationRefunded ();
129
+ $ baseShippingDiscountTaxCompensationAmount = $ order ->getBaseShippingDiscountTaxCompensationAmnt ()
130
+ - $ order ->getBaseShippingDiscountTaxCompensationRefunded ();
134
131
}
132
+
135
133
$ totalTax += $ shippingTaxAmount ;
136
134
$ baseTotalTax += $ baseShippingTaxAmount ;
137
135
$ totalDiscountTaxCompensation += $ shippingDiscountTaxCompensationAmount ;
138
136
$ baseTotalDiscountTaxCompensation += $ baseShippingDiscountTaxCompensationAmount ;
139
137
}
140
138
141
- $ allowedTax = $ order ->getTaxInvoiced () - $ order ->getTaxRefunded () - $ creditmemo ->getTaxAmount ();
142
- $ allowedBaseTax = $ order ->getBaseTaxInvoiced () - $ order ->getBaseTaxRefunded () - $ creditmemo ->getBaseTaxAmount ();
143
- $ allowedDiscountTaxCompensation = $ order ->getDiscountTaxCompensationInvoiced () +
144
- $ order ->getShippingDiscountTaxCompensationAmount () -
145
- $ order ->getDiscountTaxCompensationRefunded () -
146
- $ order ->getShippingDiscountTaxCompensationRefunded () -
147
- $ creditmemo ->getDiscountTaxCompensationAmount () -
148
- $ creditmemo ->getShippingDiscountTaxCompensationAmount ();
149
- $ allowedBaseDiscountTaxCompensation = $ order ->getBaseDiscountTaxCompensationInvoiced () +
150
- $ order ->getBaseShippingDiscountTaxCompensationAmnt () -
151
- $ order ->getBaseDiscountTaxCompensationRefunded () -
152
- $ order ->getBaseShippingDiscountTaxCompensationRefunded () -
153
- $ creditmemo ->getBaseShippingDiscountTaxCompensationAmnt () -
154
- $ creditmemo ->getBaseDiscountTaxCompensationAmount ();
139
+ $ allowedTax = $ this ->calculateAllowedTax ($ creditmemo );
140
+ $ allowedBaseTax = $ this ->calculateAllowedBaseTax ($ creditmemo );
141
+ $ allowedDiscountTaxCompensation = $ this ->calculateAllowedDiscountTaxCompensation ($ creditmemo );
142
+ $ allowedBaseDiscountTaxCompensation = $ this ->calculateAllowedBaseDiscountTaxCompensation ($ creditmemo );
155
143
156
144
if ($ creditmemo ->isLast () && !$ isPartialShippingRefunded ) {
157
145
$ totalTax = $ allowedTax ;
@@ -161,10 +149,11 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
161
149
} else {
162
150
$ totalTax = min ($ allowedTax , $ totalTax );
163
151
$ baseTotalTax = min ($ allowedBaseTax , $ baseTotalTax );
164
- $ totalDiscountTaxCompensation =
165
- min ($ allowedDiscountTaxCompensation , $ totalDiscountTaxCompensation );
166
- $ baseTotalDiscountTaxCompensation =
167
- min ($ allowedBaseDiscountTaxCompensation , $ baseTotalDiscountTaxCompensation );
152
+ $ totalDiscountTaxCompensation = min ($ allowedDiscountTaxCompensation , $ totalDiscountTaxCompensation );
153
+ $ baseTotalDiscountTaxCompensation = min (
154
+ $ allowedBaseDiscountTaxCompensation ,
155
+ $ baseTotalDiscountTaxCompensation
156
+ );
168
157
}
169
158
170
159
$ creditmemo ->setTaxAmount ($ creditmemo ->getTaxAmount () + $ totalTax );
@@ -177,9 +166,91 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
177
166
178
167
$ creditmemo ->setGrandTotal ($ creditmemo ->getGrandTotal () + $ totalTax + $ totalDiscountTaxCompensation );
179
168
$ creditmemo ->setBaseGrandTotal (
180
- $ creditmemo ->getBaseGrandTotal () +
181
- $ baseTotalTax + $ baseTotalDiscountTaxCompensation
169
+ $ creditmemo ->getBaseGrandTotal () + $ baseTotalTax + $ baseTotalDiscountTaxCompensation
182
170
);
183
171
return $ this ;
172
+
173
+ }
174
+
175
+ /**
176
+ * Calculate allowed to Credit Memo tax amount
177
+ *
178
+ * @param Creditmemo $creditMemo
179
+ * @return float
180
+ */
181
+ private function calculateAllowedTax (Creditmemo $ creditMemo ): float
182
+ {
183
+ $ invoice = $ creditMemo ->getInvoice ();
184
+ $ order = $ creditMemo ->getOrder ();
185
+ $ amount = $ invoice !== null ? $ invoice ->getTaxAmount () : $ order ->getTaxInvoiced ();
186
+
187
+ return (float ) $ amount - $ order ->getTaxRefunded () - $ creditMemo ->getTaxAmount ();
188
+ }
189
+
190
+ /**
191
+ * Calculate allowed to Credit Memo tax amount in the base currency
192
+ *
193
+ * @param Creditmemo $creditMemo
194
+ * @return float
195
+ */
196
+ private function calculateAllowedBaseTax (Creditmemo $ creditMemo ): float
197
+ {
198
+ $ invoice = $ creditMemo ->getInvoice ();
199
+ $ order = $ creditMemo ->getOrder ();
200
+ $ amount = $ invoice !== null ? $ invoice ->getBaseTaxAmount () : $ order ->getBaseTaxInvoiced ();
201
+
202
+ return (float ) $ amount - $ order ->getBaseTaxRefunded () - $ creditMemo ->getBaseTaxAmount ();
203
+ }
204
+
205
+ /**
206
+ * Calculate allowed to Credit Memo discount tax compensation amount
207
+ *
208
+ * @param Creditmemo $creditMemo
209
+ * @return float
210
+ */
211
+ private function calculateAllowedDiscountTaxCompensation (Creditmemo $ creditMemo ): float
212
+ {
213
+ $ invoice = $ creditMemo ->getInvoice ();
214
+ $ order = $ creditMemo ->getOrder ();
215
+
216
+ if ($ invoice ) {
217
+ $ amount = $ invoice ->getDiscountTaxCompensationAmount ()
218
+ + $ invoice ->getShippingDiscountTaxCompensationAmount ();
219
+ } else {
220
+ $ amount = $ order ->getDiscountTaxCompensationInvoiced ()
221
+ + $ order ->getShippingDiscountTaxCompensationAmount ();
222
+ }
223
+
224
+ return (float ) $ amount
225
+ - $ order ->getDiscountTaxCompensationRefunded ()
226
+ - $ order ->getShippingDiscountTaxCompensationRefunded ()
227
+ - $ creditMemo ->getDiscountTaxCompensationAmount ()
228
+ - $ creditMemo ->getShippingDiscountTaxCompensationAmount ();
229
+ }
230
+
231
+ /**
232
+ * Calculate allowed to Credit Memo discount tax compensation amount in the base currency
233
+ *
234
+ * @param Creditmemo $creditMemo
235
+ * @return float
236
+ */
237
+ private function calculateAllowedBaseDiscountTaxCompensation (Creditmemo $ creditMemo ): float
238
+ {
239
+ $ invoice = $ creditMemo ->getInvoice ();
240
+ $ order = $ creditMemo ->getOrder ();
241
+
242
+ if ($ invoice ) {
243
+ $ amount = $ invoice ->getBaseDiscountTaxCompensationAmount ()
244
+ + $ invoice ->getBaseShippingDiscountTaxCompensationAmnt ();
245
+ } else {
246
+ $ amount = $ order ->getBaseDiscountTaxCompensationInvoiced ()
247
+ + $ order ->getBaseShippingDiscountTaxCompensationAmnt ();
248
+ }
249
+
250
+ return (float ) $ amount
251
+ - $ order ->getBaseDiscountTaxCompensationRefunded ()
252
+ - $ order ->getBaseShippingDiscountTaxCompensationRefunded ()
253
+ - $ creditMemo ->getBaseShippingDiscountTaxCompensationAmnt ()
254
+ - $ creditMemo ->getBaseDiscountTaxCompensationAmount ();
184
255
}
185
256
}
0 commit comments