creditmemoFactory = $creditmemoFactory; $this->commentFactory = $commentFactory; $this->hydratorPool = $hydratorPool; $this->orderRepository = $orderRepository; } /** * Get array with original data for new Creditmemo document * * @param \Magento\Sales\Api\Data\CreditmemoItemCreationInterface[] $items * @param \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface|null $arguments * @return array */ private function getCreditmemoCreationData( array $items = [], \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null ) { $data = ['qtys' => []]; foreach ($items as $item) { $data['qtys'][$item->getOrderItemId()] = $item->getQty(); } if ($arguments) { $hydrator = $this->hydratorPool->getHydrator( \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface::class ); $data = array_merge($hydrator->extract($arguments), $data); } return $data; } /** * Attach comment to the Creditmemo document. * * @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo * @param \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment * @param bool $appendComment * @return \Magento\Sales\Api\Data\CreditmemoInterface */ private function attachComment( \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo, \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment, $appendComment = false ) { $commentData = $this->hydratorPool->getHydrator( \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface::class )->extract($comment); $comment = $this->commentFactory->create(['data' => $commentData]); $comment->setParentId($creditmemo->getEntityId()) ->setStoreId($creditmemo->getStoreId()) ->setCreditmemo($creditmemo) ->setIsCustomerNotified($appendComment); $creditmemo->setComments([$comment]); $creditmemo->setCustomerNote($comment->getComment()); $creditmemo->setCustomerNoteNotify($appendComment); return $creditmemo; } /** * Create new Creditmemo * * @param \Magento\Sales\Api\Data\OrderInterface $order * @param \Magento\Sales\Api\Data\CreditmemoItemCreationInterface[] $items * @param \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface|null $comment * @param bool|null $appendComment * @param \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface|null $arguments * @return \Magento\Sales\Api\Data\CreditmemoInterface * @since 100.1.3 */ public function createFromOrder( \Magento\Sales\Api\Data\OrderInterface $order, array $items = [], \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null, $appendComment = false, \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null ) { $data = $this->getCreditmemoCreationData($items, $arguments); $creditmemo = $this->creditmemoFactory->createByOrder($order, $data); if ($comment) { $creditmemo = $this->attachComment($creditmemo, $comment, $appendComment); } return $creditmemo; } /** * Create credit memo from invoice * * @param \Magento\Sales\Api\Data\InvoiceInterface $invoice * @param \Magento\Sales\Api\Data\CreditmemoItemCreationInterface[] $items * @param \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface|null $comment * @param bool|null $appendComment * @param \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface|null $arguments * @return \Magento\Sales\Api\Data\CreditmemoInterface * @since 100.1.3 */ public function createFromInvoice( \Magento\Sales\Api\Data\InvoiceInterface $invoice, array $items = [], \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null, $appendComment = false, \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null ) { $data = $this->getCreditmemoCreationData($items, $arguments); /** @var $invoice \Magento\Sales\Model\Order\Invoice */ $invoice->setOrder($this->orderRepository->get($invoice->getOrderId())); $creditmemo = $this->creditmemoFactory->createByInvoice($invoice, $data); if ($comment) { $creditmemo = $this->attachComment($creditmemo, $comment, $appendComment); } return $creditmemo; } }