[FIX]remove last console log
[odoo/odoo.git] / addons / product_extended / product_extended.py
1 ##############################################################################
2 #    
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
5 #    Copyright (C) 2010-2011 OpenERP S.A. (<http://www.openerp.com>).
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
23 from openerp.osv import osv
24
25
26
27
28 class product_product(osv.osv):
29     _name = 'product.product'
30     _inherit = 'product.product'
31
32
33     def compute_price(self, cr, uid, ids, recursive=False, test=False, real_time_accounting = False, context=None):
34         '''
35         Will return test dict when the test = False
36         Multiple ids at once?
37         testdict is used to inform the user about the changes to be made
38         '''
39         testdict = {}
40         for prod_id in ids:
41             bom_obj = self.pool.get('mrp.bom')
42             bom_ids = bom_obj.search(cr, uid, [('bom_id', '=', False), ('product_id','=', prod_id), ('bom_lines', '!=', False)], context=context)
43             if bom_ids:
44                 bom_id = bom_ids[0]
45                 # In recursive mode, it will first compute the prices of child boms
46                 if recursive:
47                     #Search the products that are components of this bom of prod_id
48                     boms = bom_obj.search(cr, uid, [('bom_id', '=', bom_id)], context=context)
49                     #Call compute_price on these subproducts
50                     prod_set = set([x.product_id.id for x in bom_obj.browse(cr, uid, boms, context=context)])
51                     res = self.compute_price(cr, uid, list(prod_set), recursive=recursive, test=test, real_time_accounting = real_time_accounting, context=context)
52                     if test: 
53                         testdict.update(res)
54                 #Use calc price to calculate and put the price on the product of the BoM if necessary
55                 price = self._calc_price(cr, uid, bom_obj.browse(cr, uid, bom_id, context=context), test=test, real_time_accounting = real_time_accounting, context=context)
56                 if test:
57                     testdict.update({prod_id : price})
58         if test:
59             return testdict
60         else:
61             return True
62
63
64     def _calc_price(self, cr, uid, bom, test = False, real_time_accounting=False, context=None):
65         if context is None:
66             context={}
67         price = 0
68         uom_obj = self.pool.get("product.uom")
69         if bom.bom_lines:
70             for sbom in bom.bom_lines:
71                 my_qty = sbom.bom_lines and 1.0 or sbom.product_qty
72                 price += uom_obj._compute_price(cr, uid, sbom.product_id.uom_id.id, sbom.product_id.standard_price, sbom.product_uom.id) * my_qty
73
74         if bom.routing_id:
75             for wline in bom.routing_id.workcenter_lines:
76                 wc = wline.workcenter_id
77                 cycle = wline.cycle_nbr
78                 hour = (wc.time_start + wc.time_stop + cycle * wc.time_cycle) *  (wc.time_efficiency or 1.0)
79                 price += wc.costs_cycle * cycle + wc.costs_hour * hour
80                 price = self.pool.get('product.uom')._compute_price(cr,uid,bom.product_uom.id, price, bom.product_id.uom_id.id)
81         
82         #Convert on product UoM quantities
83         if price > 0:
84             price = uom_obj._compute_price(cr, uid, bom.product_uom.id, price / bom.product_qty, bom.product_id.uom_id.id)
85             product = self.pool.get("product.product").browse(cr, uid, bom.product_id.id, context=context)
86         if not test:
87             if (product.valuation != "real_time" or not real_time_accounting):
88                 self.write(cr, uid, [bom.product_id.id], {'standard_price' : price}, context=context)
89             else:
90                 #Call wizard function here
91                 wizard_obj = self.pool.get("stock.change.standard.price")
92                 ctx = context.copy()
93                 ctx.update({'active_id': bom.product_id.id})
94                 wiz_id = wizard_obj.create(cr, uid, {'new_price': price}, context=ctx)
95                 wizard_obj.change_price(cr, uid, [wiz_id], context=ctx)
96         return price
97
98 product_product()
99
100 class product_bom(osv.osv):
101     _inherit = 'mrp.bom'
102             
103     _columns = {
104         'standard_price': fields.related('product_id','standard_price',type="float",relation="product.product",string="Standard Price",store=False)
105     }
106
107 product_bom()
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
109