[IMP] stock inventory: spellchecking + moved button column in inv. lines o2m
[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 from osv import fields, osv
23 from tools.translate import _
24
25 class stock_fill_inventory(osv.osv_memory):
26     _name = "stock.fill.inventory"
27     _description = "Import Inventory"
28     _columns = {
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"),
32     }
33
34     def fill_inventory(self, cr, uid, ids, context):
35         """ To Import stock inventory according to products available in the selected locations.
36         @param self: The object pointer.
37         @param cr: A database cursor
38         @param uid: ID of the user currently logged in
39         @param ids: the ID or list of IDs if we want more than one
40         @param context: A standard dictionary
41         @return:
42         """
43         inventory_line_obj = self.pool.get('stock.inventory.line')
44         location_obj = self.pool.get('stock.location')
45         product_obj = self.pool.get('product.product')
46         stock_location_obj = self.pool.get('stock.location')
47         for fill_inventory in self.browse(cr, uid, ids):
48             res = {}
49             res_location = {}
50             if fill_inventory.recursive :
51                 location_ids = location_obj.search(cr, uid, [('location_id',
52                                  'child_of', fill_inventory.location_id.id)])
53                 for location in location_ids :
54                     res = location_obj._product_get(cr, uid, location)
55                     res_location[location] = res
56             if fill_inventory.set_stock_zero:
57                 loc = fill_inventory.location_id.id
58                 cr.execute('select distinct location_id,product_id \
59                             from stock_inventory_line \
60                             where inventory_id=%s', (context['active_id'],))
61                 inv = cr.fetchall()
62                 cr.execute('select distinct product_id from stock_move where \
63                             location_dest_id=%s or location_id=%s', (loc, loc,))
64                 stock = cr.fetchall()
65                 for s in stock:
66                     if (loc, s[0]) not in inv:
67                         p = product_obj.browse(cr, uid, s[0])
68                         inventory_line_obj.create(cr, uid, {
69                             'inventory_id': context['active_id'],
70                             'location_id': loc,
71                             'product_id': s[0],
72                             'product_uom': p.uom_id.id,
73                             'product_qty': 0.0,
74                             })                    
75             else:
76                 context.update({'compute_child': False})
77                 res = location_obj._product_get(cr, uid,
78                             fill_inventory.location_id.id, context=context)
79                 res_location[fill_inventory.location_id.id] = res
80     
81         product_ids = []
82         for location in res_location.keys():
83             res = res_location[location]
84             for product_id in res.keys():
85                 prod = product_obj.browse(cr, uid, [product_id])[0]
86                 uom = prod.uom_id.id
87                 context.update(uom=uom, compute_child=False)
88                 amount = stock_location_obj._product_get(cr, uid,
89                          location, [product_id], context=context)[product_id]
90
91                 if(amount):
92                     line_ids=inventory_line_obj.search(cr, uid,
93                         [('inventory_id', '=', context['active_ids']),
94                          ('location_id', '=', location),
95                          ('product_id', '=', product_id),
96                          ('product_uom', '=', uom),
97                         ('product_qty', '=', amount)])
98                     if not len(line_ids):
99                         inventory_line = {
100                             'inventory_id': context['active_ids'][0],
101                             'location_id': location,
102                             'product_id': product_id,
103                             'product_uom': uom,
104                             'product_qty': amount
105                         }
106                         inventory_line_obj.create(cr, uid, inventory_line)
107                     product_ids.append(product_id)
108
109         if(len(product_ids) == 0):
110             raise osv.except_osv(_('Message !'), _('No product in this location.'))
111         return {}
112
113 stock_fill_inventory()
114
115 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: