1

I can't actually see why, but my invoice code doesn't actually show the correct values for the totals when it get completed without errors.

The code is:

public function invoice($realOrderId){
$orderObj = Mage::getModel('sales/order')->loadByIncrementId($realOrderId);     
$invoice = Mage::getModel('sales/service_order', $orderObj)->prepareInvoice();
$invoice->addComment('Automatic invoice', false);
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$invoice->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
                   ->addObject($invoice)
                   ->addObject($invoice->getOrder());
$transactionSave->save();
    $invoice->sendEmail();
    $invoice->setEmailSent(true);
    $invoice->save();
}

The code works without generating errors but the totals in the order details doesn't show that the payment has been captured!

Any suggestion about what could be the issue?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Matteo
  • 1,107
  • 2
  • 27
  • 47
  • How incorrect are the totals? Are they off by the tax amount? (I think I had a similar experience) – clockworkgeek Jun 30 '11 at 20:19
  • The totals in the order page (admin view) are not taking in account the fact that the payment has been done, in the "Total paid" section they are listing 0,00 – Matteo Jul 01 '11 at 06:25
  • Sorry, that isn't what I thought. Perhaps the order object doesn't have a payment associated with it. Or the payment's `_canCapture` is false. Or the grand total isn't being copied to the invoice somehow. Come to think of it there are many possibilities. – clockworkgeek Jul 01 '11 at 10:01
  • I'm examinating that, as far as I can tell, _canCapture returns true, but yeah, seems that someone the totals aren't moved into the invoice, the tables show that the columns dedicated to the invoice totals are null even if the method register(); should handle it via the method pay(); defined in the same model, probably is something pretty stupid and obvious that block the correct procedure – Matteo Jul 01 '11 at 10:27

1 Answers1

3

Allright, I managed to make the code works as intended, here the corrected function incase someone else might need it:

public function invoice($realOrderId){
$orderObj = Mage::getModel('sales/order')->loadByIncrementId($realOrderId);
$invoice = Mage::getModel('sales/service_order', $orderObj)->prepareInvoice();
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
                   ->addObject($invoice)
                   ->addObject($invoice->getOrder());
$transactionSave->save();

$invoice->addComment('Automatic Invoice', false);
$invoice->sendEmail();
$invoice->setEmailSent(true);
$invoice->save();
$orderObj->addStatusHistoryComment(Mage::helper('<your helper name>')->__('Automatic Invoice: '.$invoice->getIncrementId()));
$orderObj->save();}

Enjoy.

Edit: Forgot to correct the code, to avoid "errors" when Invoicing virtual products, the status can be omitted when the code set a comment in the status history.

Matteo
  • 1,107
  • 2
  • 27
  • 47