Developing an invoice is very easy as default way of magento. We will use Order converter.
Create full invoice using below code.
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 |
<?php namespace CWM\Invoice\Controller; class CreateInvoice extends Magento\Framework\App\Action\Action { /** * @var \Magento\Sales\Api\OrderRepositoryInterface */ protected $_orderRepository; /** * @var \Magento\Sales\Model\Service\InvoiceService */ protected $_invoiceService; /** * @var \Magento\Framework\DB\Transaction */ protected $_transaction; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Framework\DB\Transaction $transaction ) { $this->_orderRepository = $orderRepository; $this->_invoiceService = $invoiceService; $this->_transaction = $transaction; parent::__construct($context); } /** * order invoice controller. * * @return \Magento\Framework\View\Result\Page */ public function execute() { $orderId = 1; // entity_id of an order for which need to generate an invoice $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(); } } } |
If need to create partial invoice, use below code.
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 |
<?php namespace CWM\Invoice\Controller; class CreateInvoice extends Magento\Framework\App\Action\Action { /** * @var \Magento\Sales\Api\OrderRepositoryInterface */ protected $_orderRepository; /** * @var \Magento\Sales\Model\Service\InvoiceService */ protected $_invoiceService; /** * @var \Magento\Framework\DB\Transaction */ protected $_transaction; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Framework\DB\Transaction $transaction ) { $this->_orderRepository = $orderRepository; $this->_invoiceService = $invoiceService; $this->_transaction = $transaction; parent::__construct($context); } /** * Create Partial invoice controller. * * @return \Magento\Framework\View\Result\Page */ public function execute() { $orderId = 1; //entity_id of an order for which need to generate an invoice $order = $this->_orderRepository->get($orderId); if($order->canInvoice()) { $itemsArray = ['120' => 3]; // here 120 is order item id and 3 is it's quantity to be invoice $shippingAmount = '5.00'; $subTotal = '200.00'; $baseSubtotal = '200.00'; $grandTotal = '200.00'; $baseGrandTotal = '200.00'; $invoice = $this->_invoiceService->prepareInvoice($order, $itemsArray); $invoice->setShippingAmount($shippingAmount); $invoice->setSubtotal($subTotal); $invoice->setBaseSubtotal($baseSubtotal); $invoice->setGrandTotal($grandTotal); $invoice->setBaseGrandTotal($baseGrandTotal); $invoice->register(); $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(); } } } |