To create an invoice programmatically is not a difficult task. Today's tutorial will guide you step by step through the process. Below mentioned code and a snippet is used to generate the invoice programmatically in Magento 2.
<?php namespace MD\Invoice\Block; class Invoice extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Framework\DB\TransactionFactory $transactionFactory, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, array $data = [] ) { $this->orderRepository = $orderRepository; $this->invoiceService = $invoiceService; $this->transactionFactory = $transactionFactory; $this->messageManager = $messageManager; $this->invoiceSender = $invoiceSender; parent::__construct($context, $data); } /** * Create Invoice Based on Order Object * @param \Magento\Sales\Model\Order $order * @return $this */ public function generateInvoice($orderId){ try { $order = $this->orderRepository->get($orderId); if (!$order->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.')); } if(!$order->canInvoice()) { throw new \Magento\Framework\Exception\LocalizedException( __('The order does not allow an invoice to be created.') ); } $invoice = $this->invoiceService->prepareInvoice($order); if (!$invoice) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the invoice right now.')); } if (!$invoice->getTotalQty()) { throw new \Magento\Framework\Exception\LocalizedException( __('You can\'t create an invoice without products.') ); } $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE); $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(); // send invoice emails, If you want to stop mail disable below try/catch code try { $this->invoiceSender->send($invoice); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t send the invoice email right now.')); } } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); } return $invoice; }
You might just need to pass the order ID in the generateInvoice($orderId) function in the given code snippet.
Code will do the rest of the things like checking order ID, validate it, check if the invoice is applicable for the order, prepare the invoice, set qty for the invoice record, set offline capture mode, and finally create a transaction object for the invoice.
Now, call the function from the specific class:
$orderId = 25; $this->generateInvoice($orderId);
This is a great way to check for validation.
Now you can also create an invoice programmatically. Contact us if you need any assistance regarding it.
Also read: How to Get Invoice Comments List Programmatically in Magento 2?