Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvoiceQuantityValidator.php
Go to the documentation of this file.
1 <?php
8 
14 
19 {
23  private $orderRepository;
24 
29  public function __construct(OrderRepositoryInterface $orderRepository)
30  {
31  $this->orderRepository = $orderRepository;
32  }
33 
37  public function validate($invoice)
38  {
39  if ($invoice->getOrderId() === null) {
40  return [__('Order Id is required for invoice document')];
41  }
42  $order = $this->orderRepository->get($invoice->getOrderId());
43  return $this->checkQtyAvailability($invoice, $order);
44  }
45 
53  private function checkQtyAvailability(InvoiceInterface $invoice, OrderInterface $order)
54  {
55  $messages = [];
56  $qtys = $this->getInvoiceQty($invoice);
57 
58  $totalQty = 0;
59  if ($qtys) {
61  foreach ($order->getItems() as $orderItem) {
62  if (isset($qtys[$orderItem->getId()])) {
63  if ($qtys[$orderItem->getId()] > $orderItem->getQtyToInvoice() && !$orderItem->isDummy()) {
64  $messages[] = __(
65  'The quantity to invoice must not be greater than the uninvoiced quantity'
66  . ' for product SKU "%1".',
67  $orderItem->getSku()
68  );
69  }
70  $totalQty += $qtys[$orderItem->getId()];
71  unset($qtys[$orderItem->getId()]);
72  }
73  }
74  }
75  if ($qtys) {
76  $messages[] = __('The invoice contains one or more items that are not part of the original order.');
77  } elseif ($totalQty <= 0) {
78  $messages[] = __("The invoice can't be created without products. Add products and try again.");
79  }
80  return $messages;
81  }
82 
87  private function getInvoiceQty(InvoiceInterface $invoice)
88  {
89  $qtys = [];
91  foreach ($invoice->getItems() as $item) {
92  $qtys[$item->getOrderItemId()] = $item->getQty();
93  }
94  return $qtys;
95  }
96 }
__construct(OrderRepositoryInterface $orderRepository)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$orderItem
Definition: order.php:30
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55
__()
Definition: __.php:13
$invoice