forked from ConvertGroupsAS/magento2-patches
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPatch-Magento_Sales-creditmemo-partial-tax-refund.patch
104 lines (101 loc) · 5.61 KB
/
Patch-Magento_Sales-creditmemo-partial-tax-refund.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
--- Model/Order/Creditmemo.php
+++ Model/Order/Creditmemo.php
@@ -532,25 +532,15 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
*/
public function isLast()
{
- $items = $this->getAllItems();
- foreach ($items as $item) {
- if (!$item->isLast()) {
- return false;
- }
- }
-
- if (empty($items)) {
- $order = $this->getOrder();
- if ($order) {
- foreach ($order->getItems() as $orderItem) {
- if ($orderItem->canRefund()) {
- return false;
- }
- }
- }
- }
-
- return true;
+ // Patch: HOYERCARE-171
+ $order = $this->getOrder();
+ $sum = $order->getGrandTotal()
+ - $order->getTotalCanceled()
+ - $order->getTotalRefunded()
+ - $this->getDiscountAmount()
+ - $this->getSubtotalInclTax()
+ - $this->getShippingInclTax();
+ return !($sum > 0.01);
}
/**
--- Model/Order/Creditmemo/Total/Tax.php
+++ Model/Order/Creditmemo/Total/Tax.php
@@ -141,26 +141,48 @@ class Tax extends AbstractTotal
$baseTotalDiscountTaxCompensation += $baseShippingDiscountTaxCompensationAmount;
}
- $allowedTax = $order->getTaxInvoiced() - $order->getTaxRefunded() - $creditmemo->getTaxAmount();
- $allowedBaseTax = $order->getBaseTaxInvoiced() - $order->getBaseTaxRefunded() - $creditmemo->getBaseTaxAmount();
- $allowedDiscountTaxCompensation = $order->getDiscountTaxCompensationInvoiced() +
- $order->getShippingDiscountTaxCompensationAmount() -
- $order->getDiscountTaxCompensationRefunded() -
- $order->getShippingDiscountTaxCompensationRefunded() -
- $creditmemo->getDiscountTaxCompensationAmount() -
- $creditmemo->getShippingDiscountTaxCompensationAmount();
- $allowedBaseDiscountTaxCompensation = $order->getBaseDiscountTaxCompensationInvoiced() +
- $order->getBaseShippingDiscountTaxCompensationAmnt() -
- $order->getBaseDiscountTaxCompensationRefunded() -
- $order->getBaseShippingDiscountTaxCompensationRefunded() -
- $creditmemo->getBaseShippingDiscountTaxCompensationAmnt() -
- $creditmemo->getBaseDiscountTaxCompensationAmount();
+ // Patch: HOYERCARE-171
+ $invoiceTaxRefunded = $invoiceBaseTaxRefunded = 0;
+ if ($invoice) {
+ $collection = $creditmemo->getCollection();
+ $collection->addFieldToFilter(
+ \Magento\Sales\Api\Data\CreditmemoInterface::INVOICE_ID,
+ $invoice->getId()
+ );
+ $discountTaxCompensationRefunded = $baseDiscountTaxCompensationRefunded = 0;
+ /** @var \Magento\Sales\Model\Order\Creditmemo $invoiceCreditmemo */
+ foreach ($collection as $invoiceCreditmemo) {
+ $invoiceTaxRefunded += $invoiceCreditmemo->getTaxAmount();
+ $invoiceBaseTaxRefunded += $invoiceCreditmemo->getBaseTaxAmount();
+ $discountTaxCompensationRefunded += $invoiceCreditmemo->getDiscountTaxCompensationAmount();
+ $baseDiscountTaxCompensationRefunded += $invoiceCreditmemo->getBaseDiscountTaxCompensationAmount();
+ }
+ $allowedTax = $invoice->getTaxAmount() - $invoiceTaxRefunded;
+ $allowedBaseTax = $invoice->getBaseTaxAmount() - $invoiceBaseTaxRefunded;
+ $allowedDiscountTaxCompensation = $invoice->getDiscountTaxCompensationAmount() - $discountTaxCompensationRefunded;
+ $allowedBaseDiscountTaxCompensation = $invoice->getBaseDiscountTaxCompensationAmount() - $baseDiscountTaxCompensationRefunded;
+ } else {
+ $allowedTax = $order->getTaxInvoiced() - $order->getTaxRefunded() - $creditmemo->getTaxAmount();
+ $allowedBaseTax = $order->getBaseTaxInvoiced() - $order->getBaseTaxRefunded() - $creditmemo->getBaseTaxAmount();
+ $allowedDiscountTaxCompensation = $order->getDiscountTaxCompensationInvoiced() +
+ $order->getShippingDiscountTaxCompensationAmount() -
+ $order->getDiscountTaxCompensationRefunded() -
+ $order->getShippingDiscountTaxCompensationRefunded() -
+ $creditmemo->getDiscountTaxCompensationAmount() -
+ $creditmemo->getShippingDiscountTaxCompensationAmount();
+ $allowedBaseDiscountTaxCompensation = $order->getBaseDiscountTaxCompensationInvoiced() +
+ $order->getBaseShippingDiscountTaxCompensationAmnt() -
+ $order->getBaseDiscountTaxCompensationRefunded() -
+ $order->getBaseShippingDiscountTaxCompensationRefunded() -
+ $creditmemo->getBaseShippingDiscountTaxCompensationAmnt() -
+ $creditmemo->getBaseDiscountTaxCompensationAmount();
+ }
if ($creditmemo->isLast() && !$isPartialShippingRefunded) {
$totalTax = $allowedTax;
$baseTotalTax = $allowedBaseTax;
- $totalDiscountTaxCompensation = $allowedDiscountTaxCompensation;
- $baseTotalDiscountTaxCompensation = $allowedBaseDiscountTaxCompensation;
+ $totalDiscountTaxCompensation = $order->getDiscountTaxCompensationAmount() - $order->getDiscountTaxCompensationRefunded();
+ $baseTotalDiscountTaxCompensation = $order->getBaseDiscountTaxCompensationAmount() - $order->getBaseDiscountTaxCompensationRefunded();
} else {
$totalTax = min($allowedTax, $totalTax);
$baseTotalTax = min($allowedBaseTax, $baseTotalTax);