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 tools.translate import _
25 class stock_fill_inventory(osv.osv_memory):
26 _name = "stock.fill.inventory"
27 _description = "Import Inventory"
29 'location_id': fields.many2one('stock.location', 'Location', required=True),
30 'recursive': fields.boolean("Include children",help="If checked, products contained in child locations of selected location will be included as well."),
31 'set_stock_zero': fields.boolean("Set to zero",help="If checked, all product quantities will be set to zero to help ensure a real physical inventory is done"),
33 def view_init(self, cr, uid, fields_list, context=None):
35 Creates view dynamically and adding fields at runtime.
36 @param self: The object pointer.
37 @param cr: A database cursor
38 @param uid: ID of the user currently logged in
39 @param context: A standard dictionary
40 @return: New arch of view with new columns.
44 res = super(stock_fill_inventory, self).view_init(cr, uid, fields_list, context=context)
45 if context.get('active_id', False):
46 stock = self.pool.get('stock.inventory').browse(cr, uid, context.get('active_id', False))
47 if stock.state=='done':
48 raise osv.except_osv(_('Error!'), _('Stock Inventory is done'))
51 def fill_inventory(self, cr, uid, ids, context=None):
52 """ To Import stock inventory according to products available in the selected locations.
53 @param self: The object pointer.
54 @param cr: A database cursor
55 @param uid: ID of the user currently logged in
56 @param ids: the ID or list of IDs if we want more than one
57 @param context: A standard dictionary
62 inventory_line_obj = self.pool.get('stock.inventory.line')
63 location_obj = self.pool.get('stock.location')
64 product_obj = self.pool.get('product.product')
65 stock_location_obj = self.pool.get('stock.location')
66 move_obj = self.pool.get('stock.move')
70 return {'type': 'ir.actions.act_window_close'}
71 fill_inventory = self.browse(cr, uid, ids, context=context)
74 if fill_inventory.recursive :
75 location_ids = location_obj.search(cr, uid, [('location_id',
76 'child_of', fill_inventory.location_id.id)])
77 for location in location_ids :
78 res = location_obj._product_get(cr, uid, location)
79 res_location[location] = res
81 context.update({'compute_child': False})
82 res = location_obj._product_get(cr, uid,
83 fill_inventory.location_id.id, context=context)
84 res_location[fill_inventory.location_id.id] = res
88 for location in res_location.keys():
91 if fill_inventory.recursive :
92 move_ids = move_obj.search(cr, uid, [('location_dest_id','child_of',location),('state','=','done')], context=context)
94 move_ids = move_obj.search(cr, uid, [('location_dest_id','=',location),('state','=','done')], context=context)
96 raise osv.except_osv(_('Message !'), _('No product in this location.'))
98 for move in move_obj.browse(cr, uid, move_ids, context=context):
99 lot_id = move.prodlot_id.id
100 prod_id = move.product_id.id
101 qty = move.product_qty
102 location_dest_id = move.location_dest_id.id
103 if datas.get((prod_id, lot_id)):
104 qty = datas[(prod_id, lot_id)]['product_qty'] + qty
105 datas[(prod_id, lot_id)] = {'product_id': prod_id, 'location_dest_id': location_dest_id, 'product_qty': qty, 'product_uom': move.product_id.uom_id.id, 'prodlot_id': lot_id}
106 res[location] = datas
108 for i in datas.items():
109 if(i[1]['product_qty']):
110 if fill_inventory.set_stock_zero:
111 i[1]['product_qty'] = 0
113 line_ids = inventory_line_obj.search(cr, uid,
114 [('inventory_id','=', context['active_ids']),
115 ('location_id','=', i[1]['location_dest_id']),
116 ('product_id','=', i[1]['product_id']),
117 ('product_uom','=', i[1]['product_uom']),
118 ('product_qty','=', i[1]['product_qty']),
119 ('prod_lot_id','=', i[1]['prodlot_id'])
121 if not len(line_ids):
123 'inventory_id': context['active_ids'][0],
124 'location_id': i[1]['location_dest_id'],
125 'product_id': i[1]['product_id'],
126 'product_uom': i[1]['product_uom'],
127 'product_qty': i[1]['product_qty'],
128 'prod_lot_id': i[1]['prodlot_id']
130 inventory_line_obj.create(cr, uid, inventory_line)
132 return {'type': 'ir.actions.act_window_close'}
134 stock_fill_inventory()
136 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: