Reputation: 107
I am creating invoice programmatically. As we are using using Fishbowl inventory and from that we directly generate shipment for orders in magento. So i am creating invoice automatically based on shipment.Now the invoice created successfully but order totals are not updating based on invoice amount.
Here is my code to create invoice from shipment items. I am using this function in sales_order_shipment_save_after event and using Purchase Order as payment method for the order so no capture for invoice.
public function autoInvoiceGenerate(Varien_Event_Observer $observer){
$shipment = $observer->getEvent()->getShipment();
$shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment->getIncrementId());
$shippedItems = $shipment->getAllItems();
$order = $shipment->getOrder();
$order = Mage::getModel('sales/order')->load($order->getId());
try{
if(!$order->canInvoice()) {
//$order->addStatusHistoryComment('Order cannot be invoiced.', false);
//$order->save();
return $this;
}
$invoiceQtys = array();
foreach($shippedItems as $item){
$invoiceQtys[$item->getOrderItemId()] = $item->getQty();
}
if(empty($invoiceQtys)){
return $this;
}
//START Handle Invoice
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($invoiceQtys);
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);
$invoice->sendEmail(true, '');
$order->addStatusHistoryComment('Invoice created for shipment #'.$shipment->getData('increment_id').'.', false);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}
Upvotes: 0
Views: 1372
Reputation: 76
A bit late but if you are still having this problem here my suggestion:
This works for me:
$order = Mage::getModel('sales/order')->load($orderId);
$invoice = Mage::getModel('sales/service_order', $order)
->prepareInvoice($itemsarray);
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(true);
$invoice->getOrder()->setIsInProcess(true);
$order->save();
Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder())
->save();
$invoice->save();
Upvotes: 1