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"
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)
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):
inv_line_ obj = self.pool. get('stock. inventory. line') get('product. uom') get('product. product' )
if context is None:
context = {}
uom_obj = self.pool.
product_obj = self.pool.
for inv in self.browse(cr, uid, ids, context=context):
lines_ to_unlink = []
lines_ to_create = {}
lines = {}
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
if product_id in lines.keys():
#browse the new dict to find the duplicates, unlink the old lines and create a unique new one 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)
for product_id in lines.keys():
for location_id in lines[product_
return super(stock_ inventory, self).action_ confirm( cr, uid, ids, context=context)
I hope it can help you.
Regards.
Julien