Magento – Division by zero in app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php on line 266

creditmemomagento-1.8

Apparently I've found a bug in Magento 1.7.0.2, 1.8.0.0 and 1.8.1.0

I'm creating an order on frontend with 2 different products. Qty ordered is greater than 1 for both products. Last test on 1.8.1.0 was with 2 simple products.
I invoice in admin only 1 product.
Than I click Credit Memo button and I get this:

The warning is from 1.8.1.0.

Warning: Division by zero  in app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php on line 266

#0 app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php(266): mageCoreErrorHandler(2, 'Division by zer...', '/nethosting/mag...', 266, Array)
#1 app/code/core/Mage/Sales/Model/Order/Creditmemo/Total/Subtotal.php(48): Mage_Sales_Model_Order_Creditmemo_Item->calcRowTotal()
#2 app/code/core/Mage/Sales/Model/Order/Creditmemo.php(330): Mage_Sales_Model_Order_Creditmemo_Total_Subtotal->collect(Object(Mage_Sales_Model_Order_Creditmemo))
#3 app/code/core/Mage/Sales/Model/Service/Order.php(240): Mage_Sales_Model_Order_Creditmemo->collectTotals()
#4 app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreditmemoController.php(138): Mage_Sales_Model_Service_Order->prepareCreditmemo(Array)
#5 app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreditmemoController.php(222): Mage_Adminhtml_Sales_Order_CreditmemoController->_initCreditmemo()
#6 app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Adminhtml_Sales_Order_CreditmemoController->newAction()
#7 app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('new')
#8 app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#9 app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#10 app/Mage.php(684): Mage_Core_Model_App->run(Array)
#11 index.php(87): Mage::run('', 'store')
#12 {main}

In Mage_Sales_Model_Order_Creditmemo_Item::calcRowTotal on line 265-266:

$availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
$rowTotal     = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty());

Ok, the second product/item has invoiced qty 0, such so available qty becomes 0.
A divide by 0 raises the warning above.
If I try to fix these lines locally than it will trigger another warning (devide by zero) in other part of the code (Tax.php).
I could try to 'fix' these in core files but I'm still not convinced it will work eventually.

Why all order items are considered in credit memo even if some of the items aren't invoiced ?
Anyone knows a good approach for a fix of this issue ?

I guess overall it's not a big deal since this is a warning and on production servers warnings are logged in var/log at most.

Thank you

Best Answer

I don't believe this is a common problem to Magento order processing, it may be specific to an order. Try some test orders on your dev box and see if you can refund them.

If you are really stuck and cannot find a solution, here is a comical way of doing it, the numbers should add up fine:

$rowTotal     = $creditmemo->roundPrice($rowTotal / ($availableQty * $this->getQty() + 0.00000000001));

For such a lousy solution you probably wouldn't want to be making a module, just copy the app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php to app/code/local/Mage/Sales/Model/Order/Creditmemo/Item.php and rely on Magento's factory methods to pick up your amended file.

You can do this for tax.php etc, then, if you are not using 'app/code/local/Mage' for anything else, remove those files once a proper solution is found (or) the problem orders are out the way.