Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / mrp / wizard / change_production_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 openerp.osv import fields, osv
23 from openerp.tools.translate import _
24 import openerp.addons.decimal_precision as dp
25
26 class change_production_qty(osv.osv_memory):
27     _name = 'change.production.qty'
28     _description = 'Change Quantity of Products'
29
30     _columns = {
31         'product_qty': fields.float('Product Qty', digits_compute=dp.get_precision('Product Unit of Measure'), required=True),
32     }
33
34     def default_get(self, cr, uid, fields, context=None):
35         """ To get default values for the object.
36         @param self: The object pointer.
37         @param cr: A database cursor
38         @param uid: ID of the user currently logged in
39         @param fields: List of fields for which we want default values
40         @param context: A standard dictionary
41         @return: A dictionary which of fields with values.
42         """
43         if context is None:
44             context = {}
45         res = super(change_production_qty, self).default_get(cr, uid, fields, context=context)
46         prod_obj = self.pool.get('mrp.production')
47         prod = prod_obj.browse(cr, uid, context.get('active_id'), context=context)
48         if 'product_qty' in fields:
49             res.update({'product_qty': prod.product_qty})
50         return res
51
52     def _update_product_to_produce(self, cr, uid, prod, qty, context=None):
53         move_lines_obj = self.pool.get('stock.move')
54         for m in prod.move_created_ids:
55             move_lines_obj.write(cr, uid, [m.id], {'product_uom_qty': qty})
56
57     def change_prod_qty(self, cr, uid, ids, context=None):
58         """
59         Changes the Quantity of Product.
60         @param self: The object pointer.
61         @param cr: A database cursor
62         @param uid: ID of the user currently logged in
63         @param ids: List of IDs selected
64         @param context: A standard dictionary
65         @return:
66         """
67         record_id = context and context.get('active_id',False)
68         assert record_id, _('Active Id not found')
69         prod_obj = self.pool.get('mrp.production')
70         bom_obj = self.pool.get('mrp.bom')
71         move_obj = self.pool.get('stock.move')
72         for wiz_qty in self.browse(cr, uid, ids, context=context):
73             prod = prod_obj.browse(cr, uid, record_id, context=context)
74             prod_obj.write(cr, uid, [prod.id], {'product_qty': wiz_qty.product_qty})
75             prod_obj.action_compute(cr, uid, [prod.id])
76
77             for move in prod.move_lines:
78                 bom_point = prod.bom_id
79                 bom_id = prod.bom_id.id
80                 if not bom_point:
81                     bom_id = bom_obj._bom_find(cr, uid, product_id=prod.product_id.id, context=context)
82                     if not bom_id:
83                         raise osv.except_osv(_('Error!'), _("Cannot find bill of material for this product."))
84                     prod_obj.write(cr, uid, [prod.id], {'bom_id': bom_id})
85                     bom_point = bom_obj.browse(cr, uid, [bom_id])[0]
86
87                 if not bom_id:
88                     raise osv.except_osv(_('Error!'), _("Cannot find bill of material for this product."))
89
90                 factor = prod.product_qty * prod.product_uom.factor / bom_point.product_uom.factor
91                 product_details, workcenter_details = \
92                     bom_obj._bom_explode(cr, uid, bom_point, prod.product_id, factor / bom_point.product_qty, [], context=context)
93                 for r in product_details:
94                     if r['product_id'] == move.product_id.id:
95                         move_obj.write(cr, uid, [move.id], {'product_uom_qty': r['product_qty']})
96             if prod.move_prod_id:
97                 move_obj.write(cr, uid, [prod.move_prod_id.id], {'product_uom_qty' :  wiz_qty.product_qty})
98             self._update_product_to_produce(cr, uid, prod, wiz_qty.product_qty, context=context)
99         return {}
100
101
102 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: