5
5
*/
6
6
namespace Magento \Sales \Model \Order \Creditmemo \Total ;
7
7
8
+ use Magento \Sales \Api \Data \CreditmemoInterface ;
8
9
use Magento \Sales \Model \Order \Creditmemo ;
10
+ use Magento \Sales \Model \Order \Invoice ;
11
+ use Magento \Sales \Model \ResourceModel \Order \Invoice as ResourceInvoice ;
9
12
10
13
/**
11
14
* Collects credit memo taxes.
12
15
*/
13
16
class Tax extends AbstractTotal
14
17
{
18
+ /**
19
+ * @var ResourceInvoice
20
+ */
21
+ private $ resourceInvoice ;
22
+
23
+ /**
24
+ * @param ResourceInvoice $resourceInvoice
25
+ * @param array $data
26
+ */
27
+ public function __construct (ResourceInvoice $ resourceInvoice , array $ data = [])
28
+ {
29
+ $ this ->resourceInvoice = $ resourceInvoice ;
30
+ parent ::__construct ($ data );
31
+ }
32
+
15
33
/**
16
34
* {@inheritdoc}
17
35
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -182,8 +200,12 @@ private function calculateAllowedTax(Creditmemo $creditMemo): float
182
200
{
183
201
$ invoice = $ creditMemo ->getInvoice ();
184
202
$ order = $ creditMemo ->getOrder ();
185
- $ amount = $ invoice !== null ? $ invoice ->getTaxAmount ()
186
- : $ order ->getTaxInvoiced () - $ order ->getTaxRefunded ();
203
+ if ($ invoice !== null ) {
204
+ $ amount = $ invoice ->getTaxAmount ()
205
+ - $ this ->calculateInvoiceRefundedAmount ($ invoice , CreditmemoInterface::TAX_AMOUNT );
206
+ } else {
207
+ $ amount = $ order ->getTaxInvoiced () - $ order ->getTaxRefunded ();
208
+ }
187
209
188
210
return (float ) $ amount - $ creditMemo ->getTaxAmount ();
189
211
}
@@ -198,8 +220,13 @@ private function calculateAllowedBaseTax(Creditmemo $creditMemo): float
198
220
{
199
221
$ invoice = $ creditMemo ->getInvoice ();
200
222
$ order = $ creditMemo ->getOrder ();
201
- $ amount = $ invoice !== null ? $ invoice ->getBaseTaxAmount ()
202
- : $ order ->getBaseTaxInvoiced () - $ order ->getBaseTaxRefunded ();
223
+
224
+ if ($ invoice !== null ) {
225
+ $ amount = $ invoice ->getBaseTaxAmount ()
226
+ - $ this ->calculateInvoiceRefundedAmount ($ invoice , CreditmemoInterface::BASE_TAX_AMOUNT );
227
+ } else {
228
+ $ amount = $ order ->getBaseTaxInvoiced () - $ order ->getBaseTaxRefunded ();
229
+ }
203
230
204
231
return (float ) $ amount - $ creditMemo ->getBaseTaxAmount ();
205
232
}
@@ -217,7 +244,14 @@ private function calculateAllowedDiscountTaxCompensation(Creditmemo $creditMemo)
217
244
218
245
if ($ invoice ) {
219
246
$ amount = $ invoice ->getDiscountTaxCompensationAmount ()
220
- + $ invoice ->getShippingDiscountTaxCompensationAmount ();
247
+ + $ invoice ->getShippingDiscountTaxCompensationAmount ()
248
+ - $ this ->calculateInvoiceRefundedAmount (
249
+ $ invoice ,
250
+ CreditmemoInterface::DISCOUNT_TAX_COMPENSATION_AMOUNT
251
+ ) - $ this ->calculateInvoiceRefundedAmount (
252
+ $ invoice ,
253
+ CreditmemoInterface::SHIPPING_DISCOUNT_TAX_COMPENSATION_AMOUNT
254
+ );
221
255
} else {
222
256
$ amount = $ order ->getDiscountTaxCompensationInvoiced ()
223
257
+ $ order ->getShippingDiscountTaxCompensationAmount ()
@@ -243,7 +277,14 @@ private function calculateAllowedBaseDiscountTaxCompensation(Creditmemo $creditM
243
277
244
278
if ($ invoice ) {
245
279
$ amount = $ invoice ->getBaseDiscountTaxCompensationAmount ()
246
- + $ invoice ->getBaseShippingDiscountTaxCompensationAmnt ();
280
+ + $ invoice ->getBaseShippingDiscountTaxCompensationAmnt ()
281
+ - $ this ->calculateInvoiceRefundedAmount (
282
+ $ invoice ,
283
+ CreditmemoInterface::BASE_DISCOUNT_TAX_COMPENSATION_AMOUNT
284
+ ) - $ this ->calculateInvoiceRefundedAmount (
285
+ $ invoice ,
286
+ CreditmemoInterface::BASE_SHIPPING_DISCOUNT_TAX_COMPENSATION_AMNT
287
+ );
247
288
} else {
248
289
$ amount = $ order ->getBaseDiscountTaxCompensationInvoiced ()
249
290
+ $ order ->getBaseShippingDiscountTaxCompensationAmnt ()
@@ -255,4 +296,20 @@ private function calculateAllowedBaseDiscountTaxCompensation(Creditmemo $creditM
255
296
- $ creditMemo ->getBaseShippingDiscountTaxCompensationAmnt ()
256
297
- $ creditMemo ->getBaseDiscountTaxCompensationAmount ();
257
298
}
299
+
300
+ /**
301
+ * Calculate refunded amount for invoice
302
+ *
303
+ * @param Invoice $invoice
304
+ * @param string $field
305
+ * @return float
306
+ */
307
+ private function calculateInvoiceRefundedAmount (Invoice $ invoice , string $ field ): float
308
+ {
309
+ if (empty ($ invoice ->getId ())) {
310
+ return 0 ;
311
+ }
312
+
313
+ return $ this ->resourceInvoice ->calculateRefundedAmount ((int )$ invoice ->getId (), $ field );
314
+ }
258
315
}
0 commit comments