1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from osv import fields, osv
23 from service import web_services
24 from tools.misc import UpdateableStr, UpdateableDict
25 from tools.translate import _
32 class stock_fill_inventory(osv.osv_memory):
33 _name = "stock.fill.inventory"
34 _description = "Fill Inventory"
36 'location_id': fields.many2one('stock.location', 'Location', required=True),
37 'recursive': fields.boolean("Include all children for the location"),
40 def fill_inventory(self, cr, uid, ids, context):
41 """ To fill stock inventory according to products available in the selected locations.
42 @param self: The object pointer.
43 @param cr: A database cursor
44 @param uid: ID of the user currently logged in
45 @param ids: the ID or list of IDs if we want more than one
46 @param context: A standard dictionary
49 inventory_line_obj = self.pool.get('stock.inventory.line')
50 location_obj = self.pool.get('stock.location')
51 product_obj = self.pool.get('product.product')
52 stock_location_obj = self.pool.get('stock.location')
53 for fill_inventory in self.browse(cr, uid, ids):
56 if fill_inventory.recursive :
57 location_ids = location_obj.search(cr, uid, [('location_id',
58 'child_of', fill_inventory.location_id.id)])
59 for location in location_ids :
60 res = location_obj._product_get(cr, uid, location)
61 res_location[location] = res
63 context.update({'compute_child': False})
64 res = location_obj._product_get(cr, uid,
65 fill_inventory.location_id.id, context=context)
66 res_location[fill_inventory.location_id.id] = res
69 for location in res_location.keys():
70 res = res_location[location]
71 for product_id in res.keys():
72 prod = product_obj.browse(cr, uid, [product_id])[0]
74 context.update({'uom': uom})
75 amount = stock_location_obj._product_get(cr, uid,
76 location, [product_id], context=context)[product_id]
79 line_ids=inventory_line_obj.search(cr, uid,
80 [('inventory_id', '=', context['active_ids']),
81 ('location_id', '=', location),
82 ('product_id', '=', product_id),
83 ('product_uom', '=', uom),
84 ('product_qty', '=', amount)])
86 inventory_line = {'inventory_id': context['active_ids'][0],
87 'location_id': location,
88 'product_id': product_id,
90 'product_qty': amount}
91 inventory_line_obj.create(cr, uid, inventory_line)
92 product_ids.append(product_id)
94 if(len(product_ids) == 0):
95 raise osv.except_osv(_('Message !'), _('No product in this location.'))
98 stock_fill_inventory()
100 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: