[FIX] stock: only apply transformations to form fields of stock.change.product.qty...
[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         fvg = 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 view_type == 'form' and (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             fvg['fields']['prodlot_id']['required'] =  prod_obj.track_production
44
45         return fvg
46
47     def default_get(self, cr, uid, fields, context):
48         """ To get default values for the object.
49          @param self: The object pointer.
50          @param cr: A database cursor
51          @param uid: ID of the user currently logged in
52          @param fields: List of fields for which we want default values
53          @param context: A standard dictionary
54          @return: A dictionary which of fields with values.
55         """
56         product_id = context and context.get('active_id', False) or False
57         res = super(stock_change_product_qty, self).default_get(cr, uid, fields, context=context)
58
59         if 'new_quantity' in fields:
60             res.update({'new_quantity': 1})
61         if 'product_id' in fields:
62             res.update({'product_id': product_id})
63         return res
64
65     def change_product_qty(self, cr, uid, ids, context=None):
66         """ Changes the Product Quantity by making a Physical Inventory.
67         @param self: The object pointer.
68         @param cr: A database cursor
69         @param uid: ID of the user currently logged in
70         @param ids: List of IDs selected
71         @param context: A standard dictionary
72         @return:
73         """
74         if context is None:
75             context = {}
76
77         rec_id = context and context.get('active_id', False)
78         assert rec_id, _('Active ID is not set in Context')
79
80         inventry_obj = self.pool.get('stock.inventory')
81         inventry_line_obj = self.pool.get('stock.inventory.line')
82         prod_obj_pool = self.pool.get('product.product')
83
84         res_original = prod_obj_pool.browse(cr, uid, rec_id, context=context)
85         for data in self.browse(cr, uid, ids, context=context):
86             if data.new_quantity < 0:
87                 raise osv.except_osv(_('Warning!'), _('Quantity cannot be negative.'))
88             inventory_id = inventry_obj.create(cr , uid, {'name': _('INV: %s') % tools.ustr(res_original.name)}, context=context)
89             line_data ={
90                 'inventory_id' : inventory_id,
91                 'product_qty' : data.new_quantity,
92                 'location_id' : data.location_id.id,
93                 'product_id' : rec_id,
94                 'product_uom' : res_original.uom_id.id,
95                 'prod_lot_id' : data.prodlot_id.id
96             }
97             inventry_line_obj.create(cr , uid, line_data, context=context)
98
99             inventry_obj.action_confirm(cr, uid, [inventory_id], context=context)
100             inventry_obj.action_done(cr, uid, [inventory_id], context=context)
101
102         return {}
103
104 stock_change_product_qty()
105
106 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: