[FIX] product_extended: price from bom is build for product templates
[odoo/odoo.git] / addons / product_extended / wizard / wizard_price.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (C) 2010-2011 OpenERP S.A. (<http://www.openerp.com>).
7 #    $Id$
8 #
9 #    This program is free software: you can redistribute it and/or modify
10 #    it under the terms of the GNU General Public License as published by
11 #    the Free Software Foundation, either version 3 of the License, or
12 #    (at your option) any later version.
13 #
14 #    This program is distributed in the hope that it will be useful,
15 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #    GNU General Public License for more details.
18 #
19 #    You should have received a copy of the GNU General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 ##############################################################################
23
24 from openerp.exceptions import except_orm
25 from openerp.osv import fields, osv
26 from openerp.tools.translate import _
27
28 class wizard_price(osv.osv):
29     _name = "wizard.price"
30     _description = "Compute price wizard"
31     _columns = {
32         'info_field': fields.text('Info', readonly=True), 
33         'real_time_accounting': fields.boolean("Generate accounting entries when real-time"),
34         'recursive': fields.boolean("Change prices of child BoMs too"),
35         }
36
37     def default_get(self, cr, uid, fields, context=None):
38         res = super(wizard_price, self).default_get(cr, uid, fields, context=context)
39         product_pool = self.pool.get('product.template')
40         product_obj = product_pool.browse(cr, uid, context.get('active_id', False))
41         if context is None:
42             context = {}
43         rec_id = context and context.get('active_id', False)
44         assert rec_id, _('Active ID is not set in Context.')
45         res['info_field'] = str(product_pool.compute_price(cr, uid, [], template_ids=[product_obj.id], test=True, context=context))
46         return res
47
48     def compute_from_bom(self, cr, uid, ids, context=None):
49         assert len(ids) == 1
50         if context is None:
51             context = {}
52         model = context.get('active_model')
53         if model != 'product.template':
54             raise except_orm(_('Wrong model!'), _('This wizard is build for product templates, while you are currently running it from a product variant.'))
55         rec_id = context and context.get('active_id', False)
56         assert rec_id, _('Active ID is not set in Context.')
57         prod_obj = self.pool.get('product.template')
58         res = self.browse(cr, uid, ids, context=context)
59         prod = prod_obj.browse(cr, uid, rec_id, context=context)
60         prod_obj.compute_price(cr, uid, [], template_ids=[prod.id], real_time_accounting=res[0].real_time_accounting, recursive=res[0].recursive, test=False, context=context)
61
62 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: