[ADD, REF]: Converted all simple wizards to osv_memory wizards in stock
[odoo/odoo.git] / addons / stock / wizard / stock_fill_inventory.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import wizard
23 import netsvc
24 import pooler
25
26 import time
27 from osv import fields, osv
28 from tools.translate import _
29 from service import web_services
30 from tools.misc import UpdateableStr, UpdateableDict
31
32 class stock_fill_inventory(osv.osv_memory):
33     _name = "stock.fill.inventory"
34     _description = "Fill Inventory"
35     _columns = {
36             'location_id': fields.many2one('stock.location', 'Location', required=True), 
37             'recursive': fields.boolean("Include all childs for the location"), 
38             }
39
40     def _fill_inventory(self, cr, uid, ids, context):
41         inventory_line_obj = self.pool.get('stock.inventory.line')
42         location_obj = self.pool.get('stock.location')
43         for fill_inventory in self.browse(cr, uid, ids):
44             res={}
45             res_location = {}
46             if fill_inventory.recursive :
47                 location_ids = location_obj.search(cr, uid, [('location_id', 
48                                  'child_of', fill_inventory.location_id.id)])
49                 for location in location_ids :
50                     res=location_obj._product_get(cr, uid, location)
51                     res_location[location]=res
52             else:
53                 context.update({'compute_child':False})
54                 res=location_obj._product_get(cr, uid, fill_inventory.location_id.id, context=context)
55                 res_location[fill_inventory.location_id.id]=res
56
57                 product_ids=[]
58                 for location in res_location.keys():
59                     res=res_location[location]
60                     for product_id in res.keys():
61                         prod = self.pool.get('product.product').browse(cr, uid, [product_id])[0]
62                         uom = prod.uom_id.id
63                         context.update({'uom': uom})
64                         amount=self.pool.get('stock.location')._product_get(cr, uid, 
65                                  location, [product_id], context=context)[product_id]
66
67                         if(amount):
68                             line_ids=inventory_line_obj.search(cr, uid, 
69                                 [('inventory_id', '=', context['active_ids']), 
70                                  ('location_id', '=', location), 
71                                  ('product_id', '=', product_id), 
72                                  ('product_uom', '=', uom), 
73                                 ('product_qty', '=', amount)])
74                             if not len(line_ids):
75                                 inventory_line={'inventory_id': context['active_ids'][0], 
76                                                     'location_id': location, 
77                                                     'product_id': product_id, 
78                                                     'product_uom': uom, 
79                                                     'product_qty': amount}
80                                 inventory_line_obj.create(cr, uid, inventory_line)
81                             product_ids.append(product_id)
82
83                 if(len(product_ids)==0):
84                     raise osv.except_osv(_('Message !'), _('No product in this location.'))
85                 return {}
86
87 stock_fill_inventory()
88
89 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: