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