Comment 7 for bug 1197467

Revision history for this message
Weste (julien-weste) wrote :

Hi,

I had the same problems as Lionel and wrote a function to handle the duplicates. It works only in a unique inventory (it won't do anything if there are duplicates in different inventories, even if they have the same date)
If the function find several lines with same product, lot and location, it deletes those lines and create a new one summing the quantities of the deleted ones.

Here is the code:

# -*- coding: utf-8 -*-

from openerp.osv import fields, osv, orm

class stock_inventory(osv.osv):
    _inherit = "stock.inventory"
    _name = "stock.inventory"

    def action_confirm(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        inv_line_obj = self.pool.get('stock.inventory.line')
        uom_obj = self.pool.get('product.uom')
        product_obj = self.pool.get('product.product')

        for inv in self.browse(cr, uid, ids, context=context):
            lines = {}
            lines_to_unlink = []
            lines_to_create = {}

            for line in inv.inventory_line_id:
                product_id = line.product_id.id
                location_id = line.location_id.id
                prod_lot_id = line.prod_lot_id.id
                product_qty = line.product_qty
                product_uom = line.product_uom.id

                #copy the inventory.lines line by line in a new dict, merging the duplicates
                if product_id in lines.keys():
                    if location_id in lines[product_id].keys():
                        if prod_lot_id in lines[product_id][location_id].keys():
                            if product_uom in lines[product_id][location_id][prod_lot_id]['qty'].keys():
                                lines[product_id][location_id][prod_lot_id]['qty'][product_uom] += product_qty
                            else:
                                lines[product_id][location_id][prod_lot_id]['qty'][product_uom] = product_qty
                            lines[product_id][location_id][prod_lot_id]['ids'] += [line.id]
                        else:
                            lines[product_id][location_id][prod_lot_id] = {'qty': {product_uom: product_qty}, 'ids': [line.id]}
                    else:
                        lines[product_id][location_id] = {prod_lot_id: {'qty': {product_uom: product_qty}, 'ids': [line.id]}}
                else:
                    lines[product_id]={location_id: {prod_lot_id: {'qty': {product_uom: product_qty}, 'ids': [line.id]}}}

            #browse the new dict to find the duplicates, unlink the old lines and create a unique new one
            for product_id in lines.keys():
                for location_id in lines[product_id].keys():
                    for prod_lot_id in lines[product_id][location_id].keys():
                        if len(lines[product_id][location_id][prod_lot_id]['ids']) > 1:
                            inv_line_obj.unlink(cr, uid,
                                                lines[product_id][location_id][prod_lot_id]['ids'],
                                                context = context)
                            product_uom = product_obj.browse(cr, uid, [product_id], context = context)[0].product_tmpl_id.uom_id
                            amount = 0
                            for uom in lines[product_id][location_id][prod_lot_id]['qty'].keys():
                                from_uom = uom_obj.browse(cr, uid, [uom], context = context)[0]
                                amount += uom_obj._compute_qty_obj(cr, uid, from_uom,
                lines[product_id][location_id][prod_lot_id]['qty'][uom],
                product_uom, context=context)
                            values = {
                                'inventory_id': inv.id,
                                'location_id': location_id,
                                'product_id': product_id,
                                'product_uom': product_uom.id,
                                'product_qty': amount,
                                'prod_lot_id': prod_lot_id,
                                    }
                            inv_line_obj.create(cr, uid, values, context = context)

        return super(stock_inventory, self).action_confirm(cr, uid, ids, context=context)

I hope it can help you.

Regards.
Julien