Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generate programmatically invoices of the orders and their pdf consecutively in magento?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.54k
    Comment on it

    In Magento lets see if we are required to generate invoices of the orders and their pdf consecutively from admin panel.
    how we can do it:

    1. Create a file config.xml in an etc folder in our module.
    2. Then Create a controller folder in which add Adminhtml folder which contains the file OrderController.php.
    3. Then Create a Block folder in which add Adminhtml folder which contains the file Grid.php.


    Now in the Block Grid.php add the below code in it:

    <?php
    
    class Namespace_MyModule_Block_Adminhtml_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid {
    
        protected function _prepareMassaction()
        {
            parent::_prepareMassaction();
    
            $this->getMassactionBlock()->addItem('Invoice & pdf', array(
                'label'=> Mage::helper('sales')->__('Invoice & pdf'),
                'url'  => $this->getUrl('*/order/invoice'),
                ));
    
            return $this;
        }
    }

    In the above code we created a Mass Action Invoice in the sales->order grid for that purpose we extended the file grid.php from
    Mage_Adminhtml_Block_Sales_Order_Grid.
    Then we are creating a mass action with the label 'Invoice & pdf' and URL of our controller.
    The url can be defined with the syntax as :

    $this->getMassactionBlock()->addItem('Invoice', array(
                'label'=> Mage::helper('sales')->__('Invoice'),
                'url'  => $this->getUrl('MyModule/controller/Action'),
                ));


    this way the new mass action will be appeared on the drop down in orders grid in the admin panel.
    Now in the controller OrderController.php add the below code in it:
     

    <?php
    
    require_once('Mage/Adminhtml/controllers/Sales/OrderController.php');
    
    class Namespace_MyModule_Adminhtml_OrderController extends Mage_Adminhtml_Sales_OrderController{
    
        public function invoiceAction()
        {
    
            $request = $this->getRequest();
            $ids = $request->getParam('order_ids');
            
    
            foreach ($ids as $order_id) {
    
                $order = Mage::getModel("sales/order")->load($order_id);
    
                try {
                    $orderState = $order->getState();
    
    
                    if($order->canInvoice())
                    {   
    
                        $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
    
                        if (!$invoice->getTotalQty()) {
                            Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
                        }
                    
                        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
                        $invoice->register();
                        $transactionSave = Mage::getModel('core/resource_transaction')
                        ->addObject($invoice)
                        ->addObject($invoice->getOrder());
                        
                        $transactionSave->save();
                        Mage::getSingleton('core/session')->addSuccess('Done invoicing.');
    
                    }else{
                 
                        Mage::getSingleton('core/session')->addError('Cannot create an invoice.');
                        $this->_redirect('*/sales_order/index');
                    }
         
                }
                catch (Mage_Core_Exception $e) {
                        Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
                }
    
            }
    
            $orderIds = $ids;
                $flag = false;
                if (!empty($orderIds)) {
                    foreach ($orderIds as $orderId) {
                        $invoices = Mage::getResourceModel('sales/order_invoice_collection')
                        ->setOrderFilter($orderId)
                        ->load();
                        if ($invoices->getSize() > 0) {
                            $flag = true;
                            if (!isset($pdf)) {
                                $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                             } else {
                                $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                                $pdf->pages = array_merge($pdf->pages, $pages->pages);
                            }
                        }
                    }
                    if ($flag) {
                        return $this->_prepareDownloadResponse(
                            'invoice' . Mage::getSingleton('core/date')->date('Y-m-d_H-i-s') . '.pdf', $pdf->render(),
                            'application/pdf'
                            );
                    } else {
                        $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
                        $this->_redirect('*/*/');
                    }
                }
                $this->_redirect('*/*/')
       }   
    }

    In the above code, in invoice action, we have received the id from the order grid on submit the mass action invoice and then in the for each loop, we did the generating invoice work.
    Then we go for the pdf generation of that invoices for that we did collected the invoice ids and in the foreach loop we loaded the invoice of the orders ids afterwards after checking wheather the size of the invoice is greater than zero with the method getSize(), then we passed these invoices to sales/order_pdf_invoice model which contains the getPdf method. Further on we prepared the pdf.  
    In this way programmatically we can generate invoices and their pdf consecutively from orders.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: