Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generate invoice programmatically in magento ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.07k
    Comment on it

    In magento lets see, if we are required to generate invoices of the orders from admin panel.
    how we can do it:

    1.  Create a file config.xml in 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', array(
                'label'=> Mage::helper('sales')->__('Invoice'),
                '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 lable Invoice and url the 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 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.'));
                }
    
            }
       }   
    }

    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.
    In the if condition $order->canInvoice() we checked for the state of the order to check if we can generate the invoice or not, if it is in pending state.
    Then we checked for the quantity of the products if not zero with this $invoice->getTotalQty() and then proceed to generate.
    In this way, we can programmatically generate an invoices 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: