Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditmemoService.php
Go to the documentation of this file.
1 <?php
8 
14 {
19 
23  protected $commentRepository;
24 
29 
33  protected $filterBuilder;
34 
39 
43  protected $priceCurrency;
44 
48  protected $eventManager;
49 
53  private $resource;
54 
58  private $refundAdapter;
59 
63  private $orderRepository;
64 
68  private $invoiceRepository;
69 
79  public function __construct(
81  \Magento\Sales\Api\CreditmemoCommentRepositoryInterface $creditmemoCommentRepository,
83  \Magento\Framework\Api\FilterBuilder $filterBuilder,
86  \Magento\Framework\Event\ManagerInterface $eventManager
87  ) {
88  $this->creditmemoRepository = $creditmemoRepository;
89  $this->commentRepository = $creditmemoCommentRepository;
90  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
91  $this->filterBuilder = $filterBuilder;
92  $this->creditmemoNotifier = $creditmemoNotifier;
93  $this->priceCurrency = $priceCurrency;
94  $this->eventManager = $eventManager;
95  }
96 
105  public function cancel($id)
106  {
107  throw new \Magento\Framework\Exception\LocalizedException(__('You can not cancel Credit Memo'));
108  }
109 
116  public function getCommentsList($id)
117  {
118  $this->searchCriteriaBuilder->addFilters(
119  [$this->filterBuilder->setField('parent_id')->setValue($id)->setConditionType('eq')->create()]
120  );
121  $searchCriteria = $this->searchCriteriaBuilder->create();
122  return $this->commentRepository->getList($searchCriteria);
123  }
124 
131  public function notify($id)
132  {
133  $creditmemo = $this->creditmemoRepository->get($id);
134  return $this->creditmemoNotifier->notify($creditmemo);
135  }
136 
145  public function refund(
146  \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo,
147  $offlineRequested = false
148  ) {
149  $this->validateForRefund($creditmemo);
150  $creditmemo->setState(\Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED);
151 
152  $connection = $this->getResource()->getConnection('sales');
153  $connection->beginTransaction();
154  try {
155  $invoice = $creditmemo->getInvoice();
156  if ($invoice && !$offlineRequested) {
157  $invoice->setIsUsedForRefund(true);
158  $invoice->setBaseTotalRefunded(
159  $invoice->getBaseTotalRefunded() + $creditmemo->getBaseGrandTotal()
160  );
161  $creditmemo->setInvoiceId($invoice->getId());
162  $this->getInvoiceRepository()->save($creditmemo->getInvoice());
163  }
164  $order = $this->getRefundAdapter()->refund(
165  $creditmemo,
166  $creditmemo->getOrder(),
167  !$offlineRequested
168  );
169  $this->getOrderRepository()->save($order);
170  $this->creditmemoRepository->save($creditmemo);
171  $connection->commit();
172  } catch (\Exception $e) {
173  $connection->rollBack();
174  throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
175  }
176 
177  return $creditmemo;
178  }
179 
185  protected function validateForRefund(\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo)
186  {
187  if ($creditmemo->getId() && $creditmemo->getState() != \Magento\Sales\Model\Order\Creditmemo::STATE_OPEN) {
188  throw new \Magento\Framework\Exception\LocalizedException(
189  __('We cannot register an existing credit memo.')
190  );
191  }
192 
193  $baseOrderRefund = $this->priceCurrency->round(
194  $creditmemo->getOrder()->getBaseTotalRefunded() + $creditmemo->getBaseGrandTotal()
195  );
196  if ($baseOrderRefund > $this->priceCurrency->round($creditmemo->getOrder()->getBaseTotalPaid())) {
197  $baseAvailableRefund = $creditmemo->getOrder()->getBaseTotalPaid()
198  - $creditmemo->getOrder()->getBaseTotalRefunded();
199 
200  throw new \Magento\Framework\Exception\LocalizedException(
201  __(
202  'The most money available to refund is %1.',
203  $creditmemo->getOrder()->formatBasePrice($baseAvailableRefund)
204  )
205  );
206  }
207  return true;
208  }
209 
215  private function getRefundAdapter()
216  {
217  if ($this->refundAdapter === null) {
219  ->get(\Magento\Sales\Model\Order\RefundAdapterInterface::class);
220  }
221  return $this->refundAdapter;
222  }
223 
229  private function getResource()
230  {
231  if ($this->resource === null) {
233  ->get(\Magento\Framework\App\ResourceConnection::class);
234  }
235  return $this->resource;
236  }
237 
243  private function getOrderRepository()
244  {
245  if ($this->orderRepository === null) {
246  $this->orderRepository = \Magento\Framework\App\ObjectManager::getInstance()
247  ->get(\Magento\Sales\Api\OrderRepositoryInterface::class);
248  }
249  return $this->orderRepository;
250  }
251 
257  private function getInvoiceRepository()
258  {
259  if ($this->invoiceRepository === null) {
260  $this->invoiceRepository = \Magento\Framework\App\ObjectManager::getInstance()
261  ->get(\Magento\Sales\Api\InvoiceRepositoryInterface::class);
262  }
263  return $this->invoiceRepository;
264  }
265 }
validateForRefund(\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo)
$id
Definition: fieldset.phtml:14
refund(\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo, $offlineRequested=false)
__construct(\Magento\Sales\Api\CreditmemoRepositoryInterface $creditmemoRepository, \Magento\Sales\Api\CreditmemoCommentRepositoryInterface $creditmemoCommentRepository, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\Api\FilterBuilder $filterBuilder, \Magento\Sales\Model\Order\CreditmemoNotifier $creditmemoNotifier, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, \Magento\Framework\Event\ManagerInterface $eventManager)
$order
Definition: order.php:55
__()
Definition: __.php:13
$searchCriteria
$invoice
$connection
Definition: bulk.php:13