[IMP]stock: Improve the code for stock_physical_inventory.yml
[odoo/odoo.git] / addons / stock / wizard / stock_change_product_qty.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 import decimal_precision as dp
24 from tools.translate import _
25 import tools
26
27 class stock_change_product_qty(osv.osv_memory):
28     _name = "stock.change.product.qty"
29     _description = "Change Product Quantity"
30     _columns = {
31         'product_id' : fields.many2one('product.product', 'Product'),
32         'new_quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM'), required=True, help='This quantity is expressed in the Default UoM of the product.'),
33         'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot', domain="[('product_id','=',product_id)]"),
34         'location_id': fields.many2one('stock.location', 'Location', required=True, domain="[('usage', '=', 'internal')]"),
35     }
36
37     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
38         result = super(stock_change_product_qty, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
39         product_id = context and context.get('active_id', False) or False
40
41         if (context.get('active_model') == 'product.product') and product_id:
42             prod_obj = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
43             fields = result.get('fields', {})
44             if fields and (prod_obj.track_production == True) and (fields.get('prodlot_id')):
45                 result['fields']['prodlot_id']['required'] =  True
46             else:
47                 result['fields']['prodlot_id']['required'] = False
48         return result
49
50     def default_get(self, cr, uid, fields, context):
51         """ To get default values for the object.
52          @param self: The object pointer.
53          @param cr: A database cursor
54          @param uid: ID of the user currently logged in
55          @param fields: List of fields for which we want default values
56          @param context: A standard dictionary
57          @return: A dictionary which of fields with values.
58         """
59         product_id = context and context.get('active_id', False) or False
60         res = super(stock_change_product_qty, self).default_get(cr, uid, fields, context=context)
61
62         if 'new_quantity' in fields:
63             res.update({'new_quantity': 1})
64         if 'product_id' in fields:
65             res.update({'product_id': product_id})
66         return res
67
68     def change_product_qty(self, cr, uid, ids, context=None):
69         """ Changes the Product Quantity by making a Physical Inventory.
70         @param self: The object pointer.
71         @param cr: A database cursor
72         @param uid: ID of the user currently logged in
73         @param ids: List of IDs selected
74         @param context: A standard dictionary
75         @return:
76         """
77         if context is None:
78             context = {}
79
80         rec_id = context and context.get('active_id', False)
81         assert rec_id, _('Active ID is not set in Context')
82
83         inventry_obj = self.pool.get('stock.inventory')
84         inventry_line_obj = self.pool.get('stock.inventory.line')
85         prod_obj_pool = self.pool.get('product.product')
86
87         res_original = prod_obj_pool.browse(cr, uid, rec_id, context=context)
88         for data in self.browse(cr, uid, ids, context=context):
89             if data.new_quantity < 0:
90                 raise osv.except_osv(_('Warning!'), _('Quantity cannot be negative.'))
91             inventory_id = inventry_obj.create(cr , uid, {'name': _('INV: %s') % tools.ustr(res_original.name)}, context=context)
92             line_data ={
93                 'inventory_id' : inventory_id,
94                 'product_qty' : data.new_quantity,
95                 'location_id' : data.location_id.id,
96                 'product_id' : rec_id,
97                 'product_uom' : res_original.uom_id.id,
98                 'prod_lot_id' : data.prodlot_id.id
99             }
100             inventry_line_obj.create(cr , uid, line_data, context=context)
101
102             inventry_obj.action_confirm(cr, uid, [inventory_id], context=context)
103             inventry_obj.action_done(cr, uid, [inventory_id], context=context)
104
105         return {}
106
107 stock_change_product_qty()
108
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: