← Back to team overview

openerp-community-reviewer team mailing list archive

[Bug 1197467] Re: duplicate inventory lines produce wrong posted inventories

 

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

-- 
You received this bug notification because you are a member of Stock and
Logistic Core Editors, which is subscribed to the bug report.
https://bugs.launchpad.net/bugs/1197467

Title:
  duplicate inventory lines produce wrong posted inventories

Status in OpenERP Addons (modules):
  In Progress

Bug description:
  When you enter an inventory with several lines containing the same (product,location,prodlot) tuple, the posted inventory is not consistent : all lines are processes independently of each others, but they all relate to the same initial quantity.
  As a consequence, differences are found even when the total of all the "duplicate" lines is right. Please see the attached screencast for an illustration of the problem, where I can make the stock wrong twice even though I entered correct quantities, split in 2 lines.

  The simplest solution would be to forbid "duplicate" inventory lines
  with the same (product,location,prodlot) tuple.

  Another possible solution would be to let users insert duplicates, but
  sum them up before computing the difference with the initial stock.
  This would be interesting as it would allow users to enter products
  one by one (possibly with a barcode scanner).

  Lionel Sausin.

To manage notifications about this bug go to:
https://bugs.launchpad.net/openobject-addons/+bug/1197467/+subscriptions