Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreationQuantityValidator.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Sales\Api\Data\OrderInterfaceFactory;
15 
20 {
24  private $orderItemRepository;
25 
29  private $context;
30 
36  public function __construct(OrderItemRepositoryInterface $orderItemRepository, $context = null)
37  {
38  $this->orderItemRepository = $orderItemRepository;
39  $this->context = $context;
40  }
41 
45  public function validate($entity)
46  {
47  try {
48  $orderItem = $this->orderItemRepository->get($entity->getOrderItemId());
49  if (!$this->isItemPartOfContextOrder($orderItem)) {
50  return [__('The creditmemo contains product item that is not part of the original order.')];
51  }
52  } catch (NoSuchEntityException $e) {
53  return [__('The creditmemo contains product item that is not part of the original order.')];
54  }
55 
56  if (!$this->isQtyAvailable($orderItem, $entity->getQty())) {
57  return [__('The quantity to refund must not be greater than the unrefunded quantity.')];
58  }
59 
60  return [];
61  }
62 
68  private function isQtyAvailable(Item $orderItem, $qty)
69  {
70  return $qty <= $orderItem->getQtyToRefund() || $orderItem->isDummy();
71  }
72 
77  private function isItemPartOfContextOrder(OrderItemInterface $orderItem)
78  {
79  return $this->context instanceof OrderInterface && $this->context->getEntityId() === $orderItem->getOrderId();
80  }
81 }
$orderItem
Definition: order.php:30
__()
Definition: __.php:13
__construct(OrderItemRepositoryInterface $orderItemRepository, $context=null)
$entity
Definition: element.phtml:22