Skip to content

Commit 6b8781e

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-30749' into 2.4-develop-pr108
2 parents b18a387 + a7f3be9 commit 6b8781e

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\Model\Order\Creditmemo\Validation;
79

10+
use Magento\Framework\Pricing\PriceCurrencyInterface;
811
use Magento\Sales\Api\Data\CreditmemoInterface;
912
use Magento\Sales\Api\Data\OrderInterface;
1013
use Magento\Sales\Api\Data\OrderItemInterface;
1114
use Magento\Sales\Api\InvoiceRepositoryInterface;
1215
use Magento\Sales\Api\OrderRepositoryInterface;
13-
use Magento\Sales\Model\Order;
1416
use Magento\Sales\Model\Order\Creditmemo;
1517
use Magento\Sales\Model\Order\Item;
1618
use Magento\Sales\Model\ValidatorInterface;
1719

1820
/**
19-
* Class QuantityValidator
21+
* Creditmemo QuantityValidator
2022
*/
2123
class QuantityValidator implements ValidatorInterface
2224
{
@@ -31,7 +33,7 @@ class QuantityValidator implements ValidatorInterface
3133
private $invoiceRepository;
3234

3335
/**
34-
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
36+
* @var PriceCurrencyInterface
3537
*/
3638
private $priceCurrency;
3739

@@ -40,12 +42,12 @@ class QuantityValidator implements ValidatorInterface
4042
*
4143
* @param OrderRepositoryInterface $orderRepository
4244
* @param InvoiceRepositoryInterface $invoiceRepository
43-
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
45+
* @param PriceCurrencyInterface $priceCurrency
4446
*/
4547
public function __construct(
4648
OrderRepositoryInterface $orderRepository,
4749
InvoiceRepositoryInterface $invoiceRepository,
48-
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
50+
PriceCurrencyInterface $priceCurrency
4951
) {
5052
$this->orderRepository = $orderRepository;
5153
$this->invoiceRepository = $invoiceRepository;
@@ -96,7 +98,7 @@ public function validate($entity)
9698

9799
if ($entity->getGrandTotal() <= 0) {
98100
$messages[] = __('The credit memo\'s total must be positive.');
99-
} elseif ($totalQuantity <= 0 && !$this->canRefundShipping($order)) {
101+
} elseif ($totalQuantity < 0 && !$this->canRefundShipping($order)) {
100102
$messages[] = __('You can\'t create a creditmemo without products.');
101103
}
102104

@@ -117,6 +119,8 @@ private function canRefundShipping(OrderInterface $order)
117119
}
118120

119121
/**
122+
* Invoice qty refund limits
123+
*
120124
* @param CreditmemoInterface $creditmemo
121125
* @param OrderInterface $order
122126
* @return array
@@ -156,6 +160,8 @@ private function getInvoiceQtysRefundLimits(CreditmemoInterface $creditmemo, Ord
156160
}
157161

158162
/**
163+
* Get order items
164+
*
159165
* @param OrderInterface $order
160166
* @return OrderItemInterface[]
161167
*/
@@ -170,6 +176,8 @@ private function getOrderItems(OrderInterface $order)
170176
}
171177

172178
/**
179+
* Check is qty available
180+
*
173181
* @param Item $orderItem
174182
* @param int $qty
175183
* @return bool
@@ -182,12 +190,12 @@ private function isQtyAvailable(Item $orderItem, $qty)
182190
/**
183191
* Check if order item can be refunded
184192
*
185-
* @param \Magento\Sales\Model\Order\Item $item
193+
* @param Item $item
186194
* @param double $qty
187195
* @param array $invoiceQtysRefundLimits
188196
* @return bool
189197
*/
190-
private function canRefundItem(\Magento\Sales\Model\Order\Item $item, $qty, array $invoiceQtysRefundLimits)
198+
private function canRefundItem(Item $item, $qty, array $invoiceQtysRefundLimits)
191199
{
192200
if ($item->isDummy()) {
193201
return $this->canRefundDummyItem($item, $qty, $invoiceQtysRefundLimits);
@@ -199,11 +207,11 @@ private function canRefundItem(\Magento\Sales\Model\Order\Item $item, $qty, arra
199207
/**
200208
* Check if no dummy order item can be refunded
201209
*
202-
* @param \Magento\Sales\Model\Order\Item $item
210+
* @param Item $item
203211
* @param array $invoiceQtysRefundLimits
204212
* @return bool
205213
*/
206-
private function canRefundNoDummyItem(\Magento\Sales\Model\Order\Item $item, array $invoiceQtysRefundLimits = [])
214+
private function canRefundNoDummyItem(Item $item, array $invoiceQtysRefundLimits = [])
207215
{
208216
if ($item->getQtyToRefund() < 0) {
209217
return false;
@@ -215,12 +223,14 @@ private function canRefundNoDummyItem(\Magento\Sales\Model\Order\Item $item, arr
215223
}
216224

217225
/**
226+
* Check can refund dummy item
227+
*
218228
* @param Item $item
219229
* @param int $qty
220230
* @param array $invoiceQtysRefundLimits
221231
* @return bool
222232
*/
223-
private function canRefundDummyItem(\Magento\Sales\Model\Order\Item $item, $qty, array $invoiceQtysRefundLimits)
233+
private function canRefundDummyItem(Item $item, $qty, array $invoiceQtysRefundLimits)
224234
{
225235
if ($item->getHasChildren()) {
226236
foreach ($item->getChildrenItems() as $child) {
@@ -236,16 +246,15 @@ private function canRefundDummyItem(\Magento\Sales\Model\Order\Item $item, $qty,
236246
}
237247

238248
/**
249+
* Check can refund request qty
250+
*
239251
* @param Item $item
240252
* @param int $qty
241253
* @param array $invoiceQtysRefundLimits
242254
* @return bool
243255
*/
244-
private function canRefundRequestedQty(
245-
\Magento\Sales\Model\Order\Item $item,
246-
$qty,
247-
array $invoiceQtysRefundLimits
248-
) {
256+
private function canRefundRequestedQty(Item $item, $qty, array $invoiceQtysRefundLimits)
257+
{
249258
return $qty === null ? $this->canRefundNoDummyItem($item, $invoiceQtysRefundLimits) : $qty > 0;
250259
}
251260
}

app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/Validation/QuantityValidatorTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ public function testValidateWithWrongItemId()
147147
'The creditmemo contains product SKU "%1" that is not part of the original order.',
148148
$creditmemoItemSku
149149
),
150-
__('You can\'t create a creditmemo without products.')
151150
],
152151
$this->validator->validate($creditmemoMock)
153152
);
@@ -229,6 +228,15 @@ public function dataProviderForValidateQty()
229228
'total' => 15,
230229
'expected' => []
231230
],
231+
[
232+
'orderId' => 1,
233+
'orderItemId' => 1,
234+
'qtyToRequest' => 0,
235+
'qtyToRefund' => 0,
236+
'sku',
237+
'total' => 15,
238+
'expected' => []
239+
],
232240
[
233241
'orderId' => 1,
234242
'orderItemId' => 1,

0 commit comments

Comments
 (0)