CanInvoice.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 CanInvoice
  12. */
  13. class CanInvoice implements ValidatorInterface
  14. {
  15. /**
  16. * Validate
  17. *
  18. * @param OrderInterface $entity
  19. * @return array
  20. */
  21. public function validate($entity)
  22. {
  23. $messages = [];
  24. if (!$this->isStateReadyForInvoice($entity)) {
  25. $messages[] = __('An invoice cannot be created when an order has a status of %1', $entity->getStatus());
  26. } elseif (!$this->canInvoice($entity)) {
  27. $messages[] = __('The order does not allow an invoice to be created.');
  28. }
  29. return $messages;
  30. }
  31. /**
  32. * Is state ready for invoice
  33. *
  34. * @param OrderInterface $order
  35. * @return bool
  36. */
  37. private function isStateReadyForInvoice(OrderInterface $order)
  38. {
  39. if ($order->getState() === Order::STATE_PAYMENT_REVIEW ||
  40. $order->getState() === Order::STATE_HOLDED ||
  41. $order->getState() === Order::STATE_CANCELED ||
  42. $order->getState() === Order::STATE_COMPLETE ||
  43. $order->getState() === Order::STATE_CLOSED
  44. ) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * Can invoice
  51. *
  52. * @param OrderInterface $order
  53. * @return bool
  54. */
  55. private function canInvoice(OrderInterface $order)
  56. {
  57. /** @var \Magento\Sales\Model\Order\Item $item */
  58. foreach ($order->getItems() as $item) {
  59. if ($item->getQtyToInvoice() > 0 && !$item->getLockedDoInvoice()) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. }