9171cbc595e0cbb985c57e8cb887684da71d3319
[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     _inherit = ['mail.thread']
30     _description = "Change Product Quantity"
31     _columns = {
32         'product_id' : fields.many2one('product.product', 'Product'),
33         'new_quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True, help='This quantity is expressed in the Default Unit of Measure of the product.'),
34         'prodlot_id': fields.many2one('stock.production.lot', 'Serial Number', domain="[('product_id','=',product_id)]"),
35         'location_id': fields.many2one('stock.location', 'Location', required=True, domain="[('usage', '=', 'internal')]"),
36     }
37
38     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
39         if context is None: context = {}
40         fvg = super(stock_change_product_qty, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
41         product_id = context and context.get('active_id', False) or False
42
43         if view_type == 'form' and (context.get('active_model') == 'product.product') and product_id:
44             prod_obj = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
45             fvg['fields']['prodlot_id']['required'] =  prod_obj.track_production
46
47         return fvg
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             self.change_product_qty_send_note(cr, uid, [data.id], context)
104         return {}
105
106     def change_product_qty_send_note (self, cr, uid, ids, context=None):
107         prod_obj = self.pool.get('product.product')
108         location_obj = self.pool.get('stock.location')
109         prod_temp_obj = self.pool.get('product.template')
110         uom_obj = self.pool.get('product.uom')
111
112         for data in self.browse(cr, uid, ids, context=context):
113             for location in location_obj.browse(cr, uid, [data.location_id.id], context=context):
114                 location_name = location.name
115             for prod in prod_obj.browse(cr, uid, [data.product_id.id], context=context):
116                 for prod_temp in prod_temp_obj.browse(cr, uid, [prod.product_tmpl_id.id], context=context):
117                     for uom in uom_obj.browse(cr, uid, [prod_temp.uom_id.id], context=context):
118                         message = _("<b>Quantity has been changed</b> to <em>%s %s </em> for <em>%s</em> location.") % (data.new_quantity,uom.name,location_name)
119                         prod_obj.message_append_note(cr, uid, [prod.id], body=message, context=context)
120
121 stock_change_product_qty()
122
123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: