Creating Invoice programmatically

If you placed order using Purchase Order there is no Invoice created in backend
you can crate invoice programmatically in success.phtml with code below
add it at the bottom of success.phtml
(can be updated for any other payment method etc.)

<?php 
$order = new Mage_Sales_Model_Order();
 $order_id = $this->escapeHtml($this->getOrderId()) ;
$order->loadByIncrementId($order_id);

 $payment_method = $order->getPayment()->getMethodInstance()->getCode();

 if($payment_method=='purchaseorder'){
 
    if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW) {
 
            try {
                if(!$order->canInvoice()) {
                    $order->addStatusHistoryComment('PO Order cannot be invoiced .', false);
                    $order->save();  
                }
 
                //START Handle Invoice
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
 
                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                $invoice->register();
 
                $invoice->getOrder()->setCustomerNoteNotify(false);          
                $invoice->getOrder()->setIsInProcess(true);
                $order->addStatusHistoryComment('Automatically INVOICED PO.', false);
 
                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($invoice)
                    ->addObject($invoice->getOrder());
 
                $transactionSave->save();
                //END Handle Invoice
 
               
            } catch (Exception $e) {
                $order->addStatusHistoryComment('PO: Exception occurred during automatically Invoice PO Order action. Exception message: '.$e->getMessage(), false);
                $order->save();
            }                
        }       
        }  
?>