Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditmemoDocumentFactory.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  private $creditmemoFactory;
20 
24  private $commentFactory;
25 
29  private $hydratorPool;
30 
34  private $orderRepository;
35 
44  public function __construct(
45  \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory,
46  \Magento\Sales\Api\Data\CreditmemoCommentInterfaceFactory $commentFactory,
47  \Magento\Framework\EntityManager\HydratorPool $hydratorPool,
48  \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
49  ) {
50  $this->creditmemoFactory = $creditmemoFactory;
51  $this->commentFactory = $commentFactory;
52  $this->hydratorPool = $hydratorPool;
53  $this->orderRepository = $orderRepository;
54  }
55 
63  private function getCreditmemoCreationData(
64  array $items = [],
66  ) {
67  $data = ['qtys' => []];
68  foreach ($items as $item) {
69  $data['qtys'][$item->getOrderItemId()] = $item->getQty();
70  }
71  if ($arguments) {
72  $hydrator = $this->hydratorPool->getHydrator(
73  \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface::class
74  );
75  $data = array_merge($hydrator->extract($arguments), $data);
76  }
77  return $data;
78  }
79 
88  private function attachComment(
89  \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo,
90  \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment,
91  $appendComment = false
92  ) {
93  $commentData = $this->hydratorPool->getHydrator(
94  \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface::class
95  )->extract($comment);
96  $comment = $this->commentFactory->create(['data' => $commentData]);
97  $comment->setParentId($creditmemo->getEntityId())
98  ->setStoreId($creditmemo->getStoreId())
99  ->setCreditmemo($creditmemo)
100  ->setIsCustomerNotified($appendComment);
101  $creditmemo->setComments([$comment]);
102  return $creditmemo;
103  }
104 
115  public function createFromOrder(
116  \Magento\Sales\Api\Data\OrderInterface $order,
117  array $items = [],
118  \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null,
119  $appendComment = false,
121  ) {
122  $data = $this->getCreditmemoCreationData($items, $arguments);
123  $creditmemo = $this->creditmemoFactory->createByOrder($order, $data);
124  if ($comment) {
125  $creditmemo = $this->attachComment($creditmemo, $comment, $appendComment);
126  }
127  return $creditmemo;
128  }
129 
139  public function createFromInvoice(
141  array $items = [],
142  \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null,
143  $appendComment = false,
145  ) {
146  $data = $this->getCreditmemoCreationData($items, $arguments);
148  $invoice->setOrder($this->orderRepository->get($invoice->getOrderId()));
149  $creditmemo = $this->creditmemoFactory->createByInvoice($invoice, $data);
150  if ($comment) {
151  $creditmemo = $this->attachComment($creditmemo, $comment, $appendComment);
152  }
153  return $creditmemo;
154  }
155 }
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55
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)
$invoice
__construct(\Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory, \Magento\Sales\Api\Data\CreditmemoCommentInterfaceFactory $commentFactory, \Magento\Framework\EntityManager\HydratorPool $hydratorPool, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository)
$arguments
$items