CreditmemoDocumentFactory.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. /**
  8. * Class CreditmemoDocumentFactory
  9. *
  10. * @api
  11. * @since 100.1.3
  12. */
  13. class CreditmemoDocumentFactory
  14. {
  15. /**
  16. * @var \Magento\Sales\Model\Order\CreditmemoFactory
  17. */
  18. private $creditmemoFactory;
  19. /**
  20. * @var \Magento\Sales\Api\Data\CreditmemoCommentInterfaceFactory
  21. */
  22. private $commentFactory;
  23. /**
  24. * @var \Magento\Framework\EntityManager\HydratorPool
  25. */
  26. private $hydratorPool;
  27. /**
  28. * @var \Magento\Sales\Api\OrderRepositoryInterface
  29. */
  30. private $orderRepository;
  31. /**
  32. * CreditmemoDocumentFactory constructor.
  33. *
  34. * @param \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory
  35. * @param \Magento\Sales\Api\Data\CreditmemoCommentInterfaceFactory $commentFactory
  36. * @param \Magento\Framework\EntityManager\HydratorPool $hydratorPool
  37. * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
  38. */
  39. public function __construct(
  40. \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory,
  41. \Magento\Sales\Api\Data\CreditmemoCommentInterfaceFactory $commentFactory,
  42. \Magento\Framework\EntityManager\HydratorPool $hydratorPool,
  43. \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
  44. ) {
  45. $this->creditmemoFactory = $creditmemoFactory;
  46. $this->commentFactory = $commentFactory;
  47. $this->hydratorPool = $hydratorPool;
  48. $this->orderRepository = $orderRepository;
  49. }
  50. /**
  51. * Get array with original data for new Creditmemo document
  52. *
  53. * @param \Magento\Sales\Api\Data\CreditmemoItemCreationInterface[] $items
  54. * @param \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface|null $arguments
  55. * @return array
  56. */
  57. private function getCreditmemoCreationData(
  58. array $items = [],
  59. \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null
  60. ) {
  61. $data = ['qtys' => []];
  62. foreach ($items as $item) {
  63. $data['qtys'][$item->getOrderItemId()] = $item->getQty();
  64. }
  65. if ($arguments) {
  66. $hydrator = $this->hydratorPool->getHydrator(
  67. \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface::class
  68. );
  69. $data = array_merge($hydrator->extract($arguments), $data);
  70. }
  71. return $data;
  72. }
  73. /**
  74. * Attach comment to the Creditmemo document.
  75. *
  76. * @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo
  77. * @param \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment
  78. * @param bool $appendComment
  79. * @return \Magento\Sales\Api\Data\CreditmemoInterface
  80. */
  81. private function attachComment(
  82. \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo,
  83. \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment,
  84. $appendComment = false
  85. ) {
  86. $commentData = $this->hydratorPool->getHydrator(
  87. \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface::class
  88. )->extract($comment);
  89. $comment = $this->commentFactory->create(['data' => $commentData]);
  90. $comment->setParentId($creditmemo->getEntityId())
  91. ->setStoreId($creditmemo->getStoreId())
  92. ->setCreditmemo($creditmemo)
  93. ->setIsCustomerNotified($appendComment);
  94. $creditmemo->setComments([$comment]);
  95. $creditmemo->setCustomerNote($comment->getComment());
  96. $creditmemo->setCustomerNoteNotify($appendComment);
  97. return $creditmemo;
  98. }
  99. /**
  100. * Create new Creditmemo
  101. *
  102. * @param \Magento\Sales\Api\Data\OrderInterface $order
  103. * @param \Magento\Sales\Api\Data\CreditmemoItemCreationInterface[] $items
  104. * @param \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface|null $comment
  105. * @param bool|null $appendComment
  106. * @param \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface|null $arguments
  107. * @return \Magento\Sales\Api\Data\CreditmemoInterface
  108. * @since 100.1.3
  109. */
  110. public function createFromOrder(
  111. \Magento\Sales\Api\Data\OrderInterface $order,
  112. array $items = [],
  113. \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null,
  114. $appendComment = false,
  115. \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null
  116. ) {
  117. $data = $this->getCreditmemoCreationData($items, $arguments);
  118. $creditmemo = $this->creditmemoFactory->createByOrder($order, $data);
  119. if ($comment) {
  120. $creditmemo = $this->attachComment($creditmemo, $comment, $appendComment);
  121. }
  122. return $creditmemo;
  123. }
  124. /**
  125. * Create credit memo from invoice
  126. *
  127. * @param \Magento\Sales\Api\Data\InvoiceInterface $invoice
  128. * @param \Magento\Sales\Api\Data\CreditmemoItemCreationInterface[] $items
  129. * @param \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface|null $comment
  130. * @param bool|null $appendComment
  131. * @param \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface|null $arguments
  132. * @return \Magento\Sales\Api\Data\CreditmemoInterface
  133. * @since 100.1.3
  134. */
  135. public function createFromInvoice(
  136. \Magento\Sales\Api\Data\InvoiceInterface $invoice,
  137. array $items = [],
  138. \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null,
  139. $appendComment = false,
  140. \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null
  141. ) {
  142. $data = $this->getCreditmemoCreationData($items, $arguments);
  143. /** @var $invoice \Magento\Sales\Model\Order\Invoice */
  144. $invoice->setOrder($this->orderRepository->get($invoice->getOrderId()));
  145. $creditmemo = $this->creditmemoFactory->createByInvoice($invoice, $data);
  146. if ($comment) {
  147. $creditmemo = $this->attachComment($creditmemo, $comment, $appendComment);
  148. }
  149. return $creditmemo;
  150. }
  151. }