CanShip.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Validation;
  7. use Magento\Sales\Api\Data\OrderInterface;
  8. use Magento\Sales\Model\Order;
  9. use Magento\Sales\Model\ValidatorInterface;
  10. /**
  11. * Class CanShip
  12. */
  13. class CanShip implements ValidatorInterface
  14. {
  15. /**
  16. * @param OrderInterface $entity
  17. * @return array
  18. */
  19. public function validate($entity)
  20. {
  21. $messages = [];
  22. if (!$this->isStateReadyForShipment($entity)) {
  23. $messages[] = __('A shipment cannot be created when an order has a status of %1', $entity->getStatus());
  24. } elseif (!$this->canShip($entity)) {
  25. $messages[] = __('The order does not allow a shipment to be created.');
  26. }
  27. return $messages;
  28. }
  29. /**
  30. * @param OrderInterface $order
  31. * @return bool
  32. */
  33. private function isStateReadyForShipment(OrderInterface $order)
  34. {
  35. if ($order->getState() === Order::STATE_PAYMENT_REVIEW ||
  36. $order->getState() === Order::STATE_HOLDED ||
  37. $order->getIsVirtual() ||
  38. $order->getState() === Order::STATE_CANCELED
  39. ) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. /**
  45. * @param OrderInterface $order
  46. * @return bool
  47. */
  48. private function canShip(OrderInterface $order)
  49. {
  50. /** @var \Magento\Sales\Model\Order\Item $item */
  51. foreach ($order->getItems() as $item) {
  52. if ($item->getQtyToShip() > 0 && !$item->getIsVirtual() && !$item->getLockedDoShip()) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. }