Magento 2 : How to create invoice programatically?

Our team has been busy doing integration with JDA (formerly RedPrairie) from last few months. You might know it is quite popular warehouse management system (WMS) which allows you to sync catalog and transaction data between both Magento 2 and JDA systems.

As part of the JDA integration, we were fetching order status data back from JDA and creating invoice in Magento 2 programatically.

We did our research and managed to find the best of way of achieving this so we thought we should follow with our Magento 2 Developers.

Let’s crack on with the implementation, here is the Model class which allows you to create invoice automatically in Magento 2 -:

<?php /** * @var MagentoSalesModelResourceModelOrderInvoiceCollectionFactory */
protected $_invoiceCollectionFactory; /** * @var MagentoSalesApiInvoiceRepositoryInterface */
protected $_invoiceRepository; /**
* @var MagentoSalesModelServiceInvoiceService
*/
protected $_invoiceService; /** * @var MagentoFrameworkDBTransactionFactory */
protected $_transactionFactory; /**
* @var MagentoSalesApiOrderRepositoryInterface
*/
protected $_orderRepository; /**
* @param MagentoSalesModelResourceModelOrderInvoiceCollectionFactory $invoiceCollectionFactory
* @param MagentoSalesModelServiceInvoiceService $invoiceService
* @param MagentoFrameworkDBTransactionFactory $transactionFactory
* @param MagentoSalesApiInvoiceRepositoryInterface $invoiceRepository
* @param MagentoSalesApiOrderRepositoryInterface $orderRepository
*/
public function __construct( MagentoSalesModelResourceModelOrderInvoiceCollectionFactory $invoiceCollectionFactory, MagentoSalesModelServiceInvoiceService $invoiceService, MagentoFrameworkDBTransactionFactory $transactionFactory, MagentoSalesApiInvoiceRepositoryInterface $invoiceRepository, MagentoSalesApiOrderRepositoryInterface $orderRepository ) { $this->_invoiceCollectionFactory = $invoiceCollectionFactory; $this->_invoiceService = $invoiceService; $this->_transactionFactory = $transactionFactory; $this->_invoiceRepository = $invoiceRepository; $this->_orderRepository = $orderRepository;
} /** * @param int $orderId * @return MagentoSalesModelInvoice $invoice */
protected function createInvoice($orderId)
{ try { $order = $this->_orderRepository->get($orderId); if ($order){ $invoices = $this->_invoiceCollectionFactory->create() ->addAttributeToFilter('order_id', array('eq' => $order->getId())); $invoices->getSelect()->limit(1); if ((int)$invoices->count() !== 0) { $invoices = $invoices->getFirstItem(); $invoice = $this->_invoiceRepository->get($invoices->getId()); return $invoice; } if(!$order->canInvoice()) { return null; } $invoice = $this->_invoiceService->prepareInvoice($order); $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::CAPTURE_ONLINE); $invoice->register(); $invoice->getOrder()->setCustomerNoteNotify(false); $invoice->getOrder()->setIsInProcess(true); $order->addStatusHistoryComment(__('Automatically INVOICED'), false); $transactionSave = $this->_transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder()); $transactionSave->save(); $return $invoice; } } catch (Exception $e) { throw new MagentoFrameworkExceptionLocalizedException( __($e->getMessage()) ); }
}

There are two main functions prepareInvoice of MagentoSalesModelServiceInvoiceService class which prepare invoice and addObject of MagentoFrameworkDBTransactionFactory class which helps to create invoice and associate the invoice with original order in Magento 2.

That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Discover more from WHO WILL CARE eCommerce

Subscribe now to keep reading and get access to the full archive.

Continue reading