Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Why qty hardcoded 1 in $orderItem->isDummy() in prepareInvoice() and prepareShipment()? 
 
KalpeshMehta
Jr. Member
 
Avatar
Total Posts:  9
Joined:  2011-02-26
India
 

Hello Developers,

While creating invoice for orders having same configurable product with multiple quantities, there seems a bug (or it may be because of some logic behind it?)

Okay, when preparing invoice and shipment, it calls prepareInvoice() and prepareShipment() methods (and also in few others like creditmemo) in Mage/Sales/Model/Service/Order.php (line 94 and 131), where it checks if the order item is dummy ($orderItem->isDummy() is for simple product associated with configurable parent I think), the qty is hardcoded to 1. Why?

It results in the simple product’s quantity being 1 everytime in sales_flat_invoice_item table where it has configurable parent.

I am using Magento version 1.5.0.1 CE

 
Magento Community Magento Community
Magento Community
Magento Community
 
KalpeshMehta
Jr. Member
 
Avatar
Total Posts:  9
Joined:  2011-02-26
India
 

I just noticed in Magento 1.6 version, this has been changed

from
$qty = 1;

to
$qty = $orderItem->getQtyOrdered() ? $orderItem->getQtyOrdered() : 1;

So it was a bug. But again, this is also a bug.

If suppose we create multiple invoices and shipments, it checks only \"quantity ordered\” and not the current \"quantity to invoice\”, and so will mess there.

E.g. I have ordered 3 items for some configurable product, I had to create 2 shipments as per customer requirement,
first with 1 invoice/shipment and other with 2 invoices/shipments

So here it will insert 3 quantity in both the transaction, as it is getting the quantity from qty_ordered

 
Magento Community Magento Community
Magento Community
Magento Community
 
KalpeshMehta
Jr. Member
 
Avatar
Total Posts:  9
Joined:  2011-02-26
India
 

Okay, just for anyone who faces same problem in Magento 1.5 and 1.6 versions, here is a fix

Replace prepareInvoice method in Mage/Sales/Model/Service/Order.php with below:

public function prepareInvoice($qtys = array())
    
{
        $invoice 
$this->_convertor->toInvoice($this->_order);
        
$totalQty 0;
        foreach (
$this->_order->getAllItems() as $orderItem{
            
if (!$this->_canInvoiceItem($orderItem$qtys)) {
                
continue;
            
}
            $item 
$this->_convertor->itemToInvoiceItem($orderItem);
            if (
$orderItem->isDummy()) {
                
//changed from $qty = 1 to below value to fix correct quantity of invoice.. EDITING starts
                
if(is_object($orderItem->getParentItem()) && $orderItem->getParentItemId() == $confId{
                    $qty 
$qty//we will get old configurable's quantity here, and hence not updating it for simple item
                
else {                    
                    $qty 
1;
                
}
                
//EDITING ends
            
else {
                
if (isset($qtys[$orderItem->getId()])) {
                    $qty 
= (float) $qtys[$orderItem->getId()];
                
elseif (!count($qtys)) {
                    $qty 
$orderItem->getQtyToInvoice();
                
else {
                    
continue;
                
}
                
//getting configurable product's id, to later match with it's simple product's parent id.. ADDED below line
                
$confId $orderItem->getItemId();
            
}
            $totalQty 
+= $qty;
            
$item->setQty($qty);
            
$invoice->addItem($item);
        
}
        $invoice
->setTotalQty($totalQty);
        
$invoice->collectTotals();
        
$this->_order->getInvoiceCollection()->addItem($invoice);
        return 
$invoice;
    
}

The same procedure can be done for prepareShipment method if needed.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jonathan123
Sr. Member
 
Total Posts:  215
Joined:  2008-09-14
 

I’m not 100% sure that this is related, but I have a similar issue that was solved another way.

We upgraded from Magento 1.5.1 to 1.7.0.2. After the upgrade we noticed that when creating a Credit Memo for an order that it calculated the amount to add back to inventory all wrong. Strangely enough it invoiced the items correctly, it was only on Credit Memos that the quantities got screwed up.

We have a site with bundle products containing multiples of simple items, for example we keep our inventory for Widget A by using a simple item, but sell it in packages of 5 or 25. So we have a Bundle X which contains 5 of Widget A, and Bundle Y which contains 25 of Widget A.

When we would issue a Credit Memo for example for 10 of Bundle Y, which would be 250 of Widget A, the Credit Memo would indicate that the quantity being return to stock was correct, but the actual inventory adjustment would be for 2500 units of Widget A.

The bug (AFAIK this is in fact a bug) is in app/code/core/Mage/CatalogInventory/Model/Observer.php, function refundOrderInventory.
The offending code:

$qty $parentItem ? ($parentItem->getQty() * $item->getQty()) : $item->getQty();

changed to:

$qty $item->getQty();

And this solved the problem for us.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top