0

I'm trying to find a way to print an order's invoice programatically. For that I tried to get the url that Magento uses in backend to print an invoice but it didn't help as it uses a key that I couldn't find a way to retrieve it. I'm now trying another solution, is to code a controller function that renders the invoice pdf :

public function printOrderAction() {

        $order_id=json_decode( $this->getRequest()->getParam('order_id'));

        $orderObject = Mage::getModel('sales/order')->load($order_id);

        $invoiceCollection = $orderObject->getInvoiceCollection();
        foreach($invoiceCollection as $inv) {
            $invoice = $inv;
        }

        $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($invoice));
        $this->_prepareDownloadResponse('invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(), 'application/pdf');
    }

This function, when triggering it, gives that error Not valid template file:frontend\base\default\template\payment/info/pdf/checkmo.phtml

Is there any solution for that error? Or perhaps that there is an other simpler way to get the order's invoice ?

3
  • you are tring to generate order invoice programetically? Dec 30, 2016 at 13:20
  • @DhirenVasoya exactly . Any idea ? Dec 30, 2016 at 14:29
  • I added answer, please check and if it work them accept, so other get help in community. Dec 31, 2016 at 3:11

1 Answer 1

0

Here are code to generate invoice from the order.

    $order = Mage::getModel("sales/order")->load($order_id);

try 
{
    if(!$order->canInvoice())
    {
      Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
    }

    $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_OFFLINE);
    $invoice->register();
    $transactionSave = Mage::getModel('core/resource_transaction')
                     ->addObject($invoice)
                     ->addObject($invoice->getOrder());

    $transactionSave->save();
}
catch (Mage_Core_Exception $e) 
{
    // YOUR EXCEPTION HANDELING
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.