[FIX] stock : Optimized the code to Import Inventory

lp bug: https://launchpad.net/bugs/725908 fixed

bzr revid: ach@tinyerp.com-20110418073110-acrvwbus2q9kp09u
This commit is contained in:
Anup (OpenERP) 2011-04-18 13:01:10 +05:30
commit 739992f9c3
1 changed files with 58 additions and 50 deletions

View File

@ -39,14 +39,18 @@ class stock_fill_inventory(osv.osv_memory):
@param context: A standard dictionary
@return: New arch of view with new columns.
"""
if context==None:
context={}
res = super(stock_fill_inventory, self).view_init(cr, uid, fields_list, context=context)
if context is None:
context = {}
super(stock_fill_inventory, self).view_init(cr, uid, fields_list, context=context)
if len(context['active_ids']) > 1:
raise osv.except_osv(_('Error!'), _('You can not perform this operation on more than one Stock Inventories'))
if context.get('active_id', False):
stock = self.pool.get('stock.inventory').browse(cr, uid, context.get('active_id', False))
if stock.state=='done':
raise osv.except_osv(_('Error!'), _('Stock Inventory is done'))
True
raise osv.except_osv(_('Error!'), _('Stock Inventory is already Validated.'))
def fill_inventory(self, cr, uid, ids, context=None):
""" To Import stock inventory according to products available in the selected locations.
@ -58,61 +62,65 @@ class stock_fill_inventory(osv.osv_memory):
@return:
"""
if context is None:
context = {}
context = {}
inventory_line_obj = self.pool.get('stock.inventory.line')
location_obj = self.pool.get('stock.location')
product_obj = self.pool.get('product.product')
stock_location_obj = self.pool.get('stock.location')
if ids and len(ids):
ids = ids[0]
else:
return {'type': 'ir.actions.act_window_close'}
move_obj = self.pool.get('stock.move')
ids = ids[0]
fill_inventory = self.browse(cr, uid, ids, context=context)
res = {}
res_location = {}
if fill_inventory.recursive :
location_ids = location_obj.search(cr, uid, [('location_id',
'child_of', fill_inventory.location_id.id)])
for location in location_ids :
res = location_obj._product_get(cr, uid, location)
res_location[location] = res
'child_of', [fill_inventory.location_id.id])], order="id")
else:
context.update({'compute_child': False})
res = location_obj._product_get(cr, uid,
fill_inventory.location_id.id, context=context)
res_location[fill_inventory.location_id.id] = res
product_ids = []
for location in res_location.keys():
res = res_location[location]
for product_id in res.keys():
prod = product_obj.browse(cr, uid, product_id, context=context)
uom = prod.uom_id.id
context.update(uom=uom, compute_child=False)
amount = stock_location_obj._product_get(cr, uid,
location, [product_id], context=context)[product_id]
if(amount):
if fill_inventory.set_stock_zero:
amount = 0
line_ids=inventory_line_obj.search(cr, uid,
[('inventory_id', '=', context['active_ids']),
('location_id', '=', location),
('product_id', '=', product_id),
('product_uom', '=', uom),
('product_qty', '=', amount)])
if not len(line_ids):
inventory_line = {
'inventory_id': context['active_ids'][0],
'location_id': location,
'product_id': product_id,
'product_uom': uom,
'product_qty': amount
}
inventory_line_obj.create(cr, uid, inventory_line)
product_ids.append(product_id)
location_ids = [fill_inventory.location_id.id]
res = {}
flag = False
for location in location_ids:
datas = {}
res[location] = {}
move_ids = move_obj.search(cr, uid, [('location_dest_id','=',location),('state','=','done')], context=context)
for move in move_obj.browse(cr, uid, move_ids, context=context):
lot_id = move.prodlot_id.id
prod_id = move.product_id.id
qty = move.product_qty
if datas.get((prod_id, lot_id)):
qty += datas[(prod_id, lot_id)]['product_qty']
datas[(prod_id, lot_id)] = {'product_id': prod_id, 'location_id': location, 'product_qty': qty, 'product_uom': move.product_id.uom_id.id, 'prod_lot_id': lot_id}
if datas:
flag = True
res[location] = datas
if not flag:
raise osv.except_osv(_('Warning !'), _('No product in this location.'))
for stock_move in res.values():
for stock_move_details in stock_move.values():
stock_move_details.update({'inventory_id': context['active_ids'][0]})
domain = []
if fill_inventory.set_stock_zero:
stock_move_details.update({'product_qty': 0})
for field, value in stock_move_details.items():
domain.append((field, '=', value))
line_ids = inventory_line_obj.search(cr, uid, domain, context=context)
if not line_ids:
inventory_line_obj.create(cr, uid, stock_move_details, context=context)
if(len(product_ids) == 0):
raise osv.except_osv(_('Message !'), _('No product in this location.'))
return {'type': 'ir.actions.act_window_close'}
stock_fill_inventory()