[IMP] point_of_sale: Converted Add Product wizard into osv_memory wizard
[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 import time
23 import ir
24 from osv.osv import except_osv
25 from osv import fields, osv
26 import netsvc
27 from tools.translate import _
28
29 class change_production_qty(osv.osv_memory):
30     _name = 'change.production.qty'
31     _description = 'Change Quantity of Products'
32     
33     _columns = {
34         'product_qty': fields.float('Product Qty', required=True),
35     }
36
37     def default_get(self, cr, uid, fields, context):
38         """ 
39              @summary: To get default values for the object.
40             
41              @param self: The object pointer.
42              @param cr: A database cursor
43              @param uid: ID of the user currently logged in
44              @param fields: List of fields for which we want default values 
45              @param context: A standard dictionary 
46              
47              @return: A dictionary which of fields with values. 
48         
49         """
50         record_id = context and context.get('record_id',False)
51         res = {}
52         prod_obj = self.pool.get('mrp.production')
53         prod = prod_obj.browse(cr, uid, record_id)
54         if prod.state in ('cancel', 'done'):
55             raise osv.except_osv(_('Warning !'), _('The production is in "%s" state. You can not change the production quantity anymore') % (prod.state).upper() )
56         if prod.state in ('draft'):
57             return res
58         if record_id:
59             res['product_qty'] = prod.product_qty
60         return res
61     
62     def change_prod_qty(self, cr, uid, ids, context):
63         """ 
64              @summary: Changes the Quantity of Product.
65             
66              @param self: The object pointer.
67              @param cr: A database cursor
68              @param uid: ID of the user currently logged in
69              @param ids: List of IDs selected 
70              @param context: A standard dictionary 
71              
72              @return:  
73         
74         """
75         record_id = context and context.get('record_id',False)
76         prod_obj = self.pool.get('mrp.production')
77         wiz_qty = self.browse(cr, uid, ids[0])
78         prod = prod_obj.browse(cr, uid,record_id)
79         prod_obj.write(cr, uid,prod.id, {'product_qty': wiz_qty.product_qty})
80         prod_obj.action_compute(cr, uid, [prod.id])
81     
82         move_lines_obj = self.pool.get('stock.move')
83         for move in prod.move_lines:
84             bom_point = prod.bom_id
85             bom_id = prod.bom_id.id
86             if not bom_point:
87                 bom_id = self.pool.get('mrp.bom')._bom_find(cr, uid, prod.product_id.id, prod.product_uom.id)
88                 if not bom_id:
89                     raise osv.except_osv(_('Error'), _("Couldn't find bill of material for product"))
90                 prod_obj.write(cr, uid, [prod.id], {'bom_id': bom_id})
91                 bom_point = self.pool.get('mrp.bom').browse(cr, uid, [bom_id])[0]
92     
93             if not bom_id:
94                 raise osv.except_osv(_('Error'), _("Couldn't find bill of material for product"))
95     
96             factor = prod.product_qty * prod.product_uom.factor / bom_point.product_uom.factor
97             res = self.pool.get('mrp.bom')._bom_explode(cr, uid, bom_point, factor / bom_point.product_qty, [])
98             for r in res[0]:
99                 if r['product_id']== move.product_id.id:
100                     move_lines_obj.write(cr, uid,move.id, {'product_qty' :  r['product_qty']})
101     
102         product_lines_obj = self.pool.get('mrp.production.product.line')
103     
104         for m in prod.move_created_ids:
105             move_lines_obj.write(cr, uid,m.id, {'product_qty': wiz_qty.product_qty})
106     
107         return {}
108     
109 change_production_qty()
110
111 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: