Creating invoices in Magento can be done by script. The following code can be used in Modules or stand alone scripts to automate the creation of invoices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$order = Mage::getModel('sales/order')->loadByIncrementId('100000001'); 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_ONLINE); //Alternatively you can use //$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) { } |
Leave a reply