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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\Order;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\Data\InvoiceItemInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\ValidatorInterface;
/**
* Interface InvoiceValidatorInterface
*/
class InvoiceQuantityValidator implements ValidatorInterface
{
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* InvoiceValidator constructor.
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(OrderRepositoryInterface $orderRepository)
{
$this->orderRepository = $orderRepository;
}
/**
* @inheritdoc
*/
public function validate($invoice)
{
if ($invoice->getOrderId() === null) {
return [__('Order Id is required for invoice document')];
}
$order = $this->orderRepository->get($invoice->getOrderId());
return $this->checkQtyAvailability($invoice, $order);
}
/**
* Check qty availability
*
* @param InvoiceInterface $invoice
* @param OrderInterface $order
* @return array
*/
private function checkQtyAvailability(InvoiceInterface $invoice, OrderInterface $order)
{
$messages = [];
$qtys = $this->getInvoiceQty($invoice);
$totalQty = 0;
if ($qtys) {
/** @var \Magento\Sales\Model\Order\Item $orderItem */
foreach ($order->getItems() as $orderItem) {
if (isset($qtys[$orderItem->getId()])) {
if ($qtys[$orderItem->getId()] > $orderItem->getQtyToInvoice() && !$orderItem->isDummy()) {
$messages[] = __(
'The quantity to invoice must not be greater than the uninvoiced quantity'
. ' for product SKU "%1".',
$orderItem->getSku()
);
}
$totalQty += $qtys[$orderItem->getId()];
unset($qtys[$orderItem->getId()]);
}
}
}
if ($qtys) {
$messages[] = __('The invoice contains one or more items that are not part of the original order.');
} elseif ($totalQty <= 0) {
$messages[] = __("The invoice can't be created without products. Add products and try again.");
}
return $messages;
}
/**
* @param InvoiceInterface $invoice
* @return array
*/
private function getInvoiceQty(InvoiceInterface $invoice)
{
$qtys = [];
/** @var InvoiceItemInterface $item */
foreach ($invoice->getItems() as $item) {
$qtys[$item->getOrderItemId()] = $item->getQty();
}
return $qtys;
}
}