Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReturnToStockInvoice.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  private $returnProcessor;
17 
21  private $creditmemoRepository;
22 
26  private $orderRepository;
27 
31  private $invoiceRepository;
32 
36  private $stockConfiguration;
37 
46  public function __construct(
47  \Magento\SalesInventory\Model\Order\ReturnProcessor $returnProcessor,
48  \Magento\Sales\Api\CreditmemoRepositoryInterface $creditmemoRepository,
49  \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
50  \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository,
51  \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
52  ) {
53  $this->returnProcessor = $returnProcessor;
54  $this->creditmemoRepository = $creditmemoRepository;
55  $this->orderRepository = $orderRepository;
56  $this->invoiceRepository = $invoiceRepository;
57  $this->stockConfiguration = $stockConfiguration;
58  }
59 
73  public function afterExecute(
74  \Magento\Sales\Api\RefundInvoiceInterface $refundService,
75  $resultEntityId,
76  $invoiceId,
77  array $items = [],
78  $isOnline = false,
79  $notify = false,
80  $appendComment = false,
81  \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null,
82  \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null
83  ) {
84  $invoice = $this->invoiceRepository->get($invoiceId);
85  $order = $this->orderRepository->get($invoice->getOrderId());
86 
87  $returnToStockItems = [];
88  if ($arguments !== null
89  && $arguments->getExtensionAttributes() !== null
90  && $arguments->getExtensionAttributes()->getReturnToStockItems() !== null
91  ) {
92  $returnToStockItems = $arguments->getExtensionAttributes()->getReturnToStockItems();
93  }
94  $isAutoReturn = $this->stockConfiguration->isAutoReturnEnabled();
95  if ($isAutoReturn || !empty($returnToStockItems)) {
96  $creditmemo = $this->creditmemoRepository->get($resultEntityId);
97  $this->returnProcessor->execute($creditmemo, $order, $returnToStockItems, $isAutoReturn);
98  }
99  return $resultEntityId;
100  }
101 }
$invoiceRepository
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55
$invoice
$arguments
afterExecute(\Magento\Sales\Api\RefundInvoiceInterface $refundService, $resultEntityId, $invoiceId, array $items=[], $isOnline=false, $notify=false, $appendComment=false, \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment=null, \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments=null)
__construct(\Magento\SalesInventory\Model\Order\ReturnProcessor $returnProcessor, \Magento\Sales\Api\CreditmemoRepositoryInterface $creditmemoRepository, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository, \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration)
$items