In this article, we will learn how to create invoices using custom console command in magento 2.
To create invoices using CLI please follow below steps:
Step 01: Create basic module
Create registration.php file underapp/code/[vendor_name]/[module_name]
(In my case, vendor_name is ‘BluethinkInc’ and module_name is ‘InvoiceCli’)
1 2 3 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'BluethinkInc_InvoiceCli', __DIR__); |
Now, create module.xml file under app/code/BluethinkInc/InvoiceCli/etc
1 2 3 4 5 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="BluethinkInc_InvoiceCli"/> </config> |
Step 02: Add dependency in di.xml
Create di.xml file under app/code/BluethinkInc/InvoiceCli/etc
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="Invoice" xsi:type="object">BluethinkInc\InvoiceCli\Console\Command\Invoice</item> </argument> </arguments> </type> </config> |
Step 03: Create Invoice.php file under
app/code/BluethinkInc/InvoiceCli/Console/Command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
<?php declare(strict_types=1); namespace BluethinkInc\InvoiceCli\Console\Command; use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\DB\Transaction; use Magento\Framework\App\ObjectManager; use Symfony\Component\Console\Command\Command; use Magento\Sales\Model\Service\InvoiceService; use Magento\Sales\Api\OrderRepositoryInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; class Invoice extends Command { private const ORDERID = 'orderId'; /** * Constructor * * @param State $state * @param Transaction $transaction * @param InvoiceSender $invoiceSender * @param InvoiceService $invoiceService * @param OrderRepositoryInterface $orderRepository */ public function __construct( State $state, Transaction $transaction, InvoiceSender $invoiceSender, InvoiceService $invoiceService, OrderRepositoryInterface $orderRepository ) { parent::__construct(); $this->state = $state; $this->transaction = $transaction; $this->invoiceSender = $invoiceSender; $this->invoiceService = $invoiceService; $this->orderRepository = $orderRepository; } /** * @inheritdoc */ protected function execute( InputInterface $input, OutputInterface $output ) { $this->state->setAreaCode(Area::AREA_ADMINHTML); $orderId = $input->getOption(self::ORDERID); $objectManager = ObjectManager::getInstance(); $isOrderExists = $objectManager->create('\Magento\Sales\Model\Order')- >loadByIncrementId($orderId); if ($orderId) { if ($isOrderExists->getId()) { $order = $this->orderRepository->get($orderId); if ($order->canInvoice()) { $invoice = $this->invoiceService->prepareInvoice($order); $invoice->register(); $invoice->save(); $transactionSave = $this->transaction->addObject( $invoice )->addObject( $invoice->getOrder() ); $transactionSave->save(); $this->invoiceSender->send($invoice); //send notification code $order->addStatusHistoryComment( __('Notified customer about invoice #%1.', $invoice->getId()) ) ->setIsCustomerNotified(true) ->save(); $output->writeln("<info>Invoice is generated for the order id : $orderId</info>"); } else { $output->writeln("<error>Invoice for this order is already generated.</error>"); } } else { $output->writeln("<error>Order-Id doesn't exist.</error>"); } } else { $output->writeln("<error>Please provide order id with the flag --orderId.</error>"); } } /** * @inheritdoc */ protected function configure() { $this->setName("create:invoice"); $this->setDescription("It will genereate invoice for given order id"); $this->addOption( self::ORDERID, null, InputOption::VALUE_REQUIRED, 'orderId' ); parent::configure(); } } |
That’s it!!!! Now you are able to create invoices from your console command.
Thanks for reading my blog. I hope you would learn a lot of things from this blog. Keep liking and sharing!!
bluethinkinc_blog
2023-03-07