Skip to content

Commit ba4aaf0

Browse files
committed
Merge branch 'ACP2E-92' of https://github.com/magento-l3/magento2ce into ACP2E-92
2 parents ea66a7d + e92c33d commit ba4aaf0

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

app/code/Magento/Sales/Model/Order/Creditmemo/Validation/QuantityValidator.php

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,13 @@ public function validate($entity)
7474

7575
$totalQuantity = 0;
7676
foreach ($entity->getItems() as $item) {
77-
if (!isset($orderItemsById[$item->getOrderItemId()])) {
78-
$messages[] = __(
79-
'The creditmemo contains product SKU "%1" that is not part of the original order.',
80-
$item->getSku()
81-
);
82-
continue;
83-
}
84-
$orderItem = $orderItemsById[$item->getOrderItemId()];
85-
86-
if ($this->isValidRefundQty($orderItem->getIsQtyDecimal(), $item->getQty())) {
87-
$messages[] =__(
88-
'You cannot use decimal quantity to refund item "%1".',
89-
$orderItem->getSku()
90-
);
91-
continue;
92-
}
93-
94-
if (!$this->canRefundItem($orderItem, $item->getQty(), $invoiceQtysRefundLimits) ||
95-
!$this->isQtyAvailable($orderItem, $item->getQty())
96-
) {
97-
$messages[] =__(
98-
'The quantity to creditmemo must not be greater than the unrefunded quantity'
99-
. ' for product SKU "%1".',
100-
$orderItem->getSku()
101-
);
102-
} else {
103-
$totalQuantity += $item->getQty();
104-
}
77+
$this->calculateTotalQuantity(
78+
$orderItemsById,
79+
$messages,
80+
$totalQuantity,
81+
$item,
82+
$invoiceQtysRefundLimits
83+
);
10584
}
10685

10786
if ($entity->getGrandTotal() <= 0) {
@@ -128,6 +107,50 @@ private function isValidRefundQty($isQtyDecimal, float $itemQty): bool
128107
return false;
129108
}
130109

110+
/**
111+
* Calculate total quantity.
112+
*
113+
* @param array $orderItemsById
114+
* @param array &$messages
115+
* @param int &$totalQuantity
116+
* @param Magento\Sales\Api\Data\CreditmemoItemInterface|mixed $item
117+
* @param array $invoiceQtysRefundLimits
118+
* @return void
119+
*/
120+
private function calculateTotalQuantity(
121+
array $orderItemsById,
122+
array &$messages,
123+
int &$totalQuantity,
124+
$item,
125+
array $invoiceQtysRefundLimits
126+
) {
127+
if (!isset($orderItemsById[$item->getOrderItemId()])) {
128+
$messages[] = __(
129+
'The creditmemo contains product SKU "%1" that is not part of the original order.',
130+
$item->getSku()
131+
);
132+
} else {
133+
$orderItem = $orderItemsById[$item->getOrderItemId()];
134+
135+
if ($this->isValidRefundQty($orderItem->getIsQtyDecimal(), $item->getQty())) {
136+
$messages[] =__(
137+
'You cannot use decimal quantity to refund item "%1".',
138+
$orderItem->getSku()
139+
);
140+
} elseif (!$this->canRefundItem($orderItem, $item->getQty(), $invoiceQtysRefundLimits) ||
141+
!$this->isQtyAvailable($orderItem, $item->getQty())
142+
) {
143+
$messages[] =__(
144+
'The quantity to creditmemo must not be greater than the unrefunded quantity'
145+
. ' for product SKU "%1".',
146+
$orderItem->getSku()
147+
);
148+
} else {
149+
$totalQuantity += $item->getQty();
150+
}
151+
}
152+
}
153+
131154
/**
132155
* We can have problem with float in php (on some server $a=762.73;$b=762.73; $a-$b!=0)
133156
* for this we have additional diapason for 0

0 commit comments

Comments
 (0)