InvoiceRefundCreationArguments.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesInventory\Model\Plugin\Order\Validation;
  7. use Magento\Sales\Api\Data\InvoiceInterface;
  8. use Magento\Sales\Model\Order\Validation\RefundInvoiceInterface;
  9. use Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface;
  10. use Magento\Sales\Api\Data\CreditmemoInterface;
  11. use Magento\Sales\Api\Data\OrderInterface;
  12. use Magento\SalesInventory\Model\Order\ReturnValidator;
  13. use Magento\Sales\Model\ValidatorResultInterface;
  14. /**
  15. * Class CreditmemoCreationArguments
  16. */
  17. class InvoiceRefundCreationArguments
  18. {
  19. /**
  20. * @var ReturnValidator
  21. */
  22. private $returnValidator;
  23. /**
  24. * InvoiceRefundCreationArguments constructor.
  25. * @param ReturnValidator $returnValidator
  26. */
  27. public function __construct(
  28. ReturnValidator $returnValidator
  29. ) {
  30. $this->returnValidator = $returnValidator;
  31. }
  32. /**
  33. * @param RefundInvoiceInterface $refundInvoiceValidator
  34. * @param ValidatorResultInterface $validationResults
  35. * @param InvoiceInterface $invoice
  36. * @param OrderInterface $order
  37. * @param CreditmemoInterface $creditmemo
  38. * @param array $items
  39. * @param bool $isOnline
  40. * @param bool $notify
  41. * @param bool $appendComment
  42. * @param \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface|null $comment
  43. * @param \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface|null $arguments
  44. * @return ValidatorResultInterface
  45. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  46. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  47. */
  48. public function afterValidate(
  49. RefundInvoiceInterface $refundInvoiceValidator,
  50. ValidatorResultInterface $validationResults,
  51. InvoiceInterface $invoice,
  52. OrderInterface $order,
  53. CreditmemoInterface $creditmemo,
  54. array $items = [],
  55. $isOnline = false,
  56. $notify = false,
  57. $appendComment = false,
  58. \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null,
  59. \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null
  60. ) {
  61. if ($this->isReturnToStockItems($arguments)) {
  62. return $validationResults;
  63. }
  64. /** @var int[] $returnToStockItems */
  65. $returnToStockItems = $arguments->getExtensionAttributes()->getReturnToStockItems();
  66. $validationMessage = $this->returnValidator->validate($returnToStockItems, $creditmemo);
  67. if ($validationMessage) {
  68. $validationResults->addMessage($validationMessage);
  69. }
  70. return $validationResults;
  71. }
  72. /**
  73. * @param CreditmemoCreationArgumentsInterface|null $arguments
  74. * @return bool
  75. */
  76. private function isReturnToStockItems($arguments)
  77. {
  78. return $arguments === null
  79. || $arguments->getExtensionAttributes() === null
  80. || $arguments->getExtensionAttributes()->getReturnToStockItems() === null;
  81. }
  82. }