[FIX] correct css computation to remove unnecessary toolbar
[odoo/odoo.git] / addons / product_extended / product_extended.py
1 ##############################################################################
2 #    
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2014 OpenERP S.A. (<http://www.openerp.com>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
18 #
19 ##############################################################################
20
21 from openerp.osv import fields
22 from openerp.osv import osv
23
24
25 class product_product(osv.osv):
26     _name = 'product.product'
27     _inherit = 'product.product'
28
29
30     def compute_price(self, cr, uid, ids, recursive=False, test=False, real_time_accounting = False, context=None):
31         '''
32         Will return test dict when the test = False
33         Multiple ids at once?
34         testdict is used to inform the user about the changes to be made
35         '''
36         testdict = {}
37         for prod_id in ids:
38             bom_obj = self.pool.get('mrp.bom')
39             bom_ids = bom_obj.search(cr, uid, [('product_id','=', prod_id), ('bom_line_ids', '!=', False)], context=context)
40             if bom_ids:
41                 bom_id = bom_ids[0]
42                 # In recursive mode, it will first compute the prices of child boms
43                 if recursive:
44                     #Search the products that are components of this bom of prod_id
45                     boms = bom_obj.search(cr, uid, [('bom_id', '=', bom_id)], context=context)
46                     #Call compute_price on these subproducts
47                     prod_set = set([x.product_id.id for x in bom_obj.browse(cr, uid, boms, context=context)])
48                     res = self.compute_price(cr, uid, list(prod_set), recursive=recursive, test=test, real_time_accounting = real_time_accounting, context=context)
49                     if test: 
50                         testdict.update(res)
51                 #Use calc price to calculate and put the price on the product of the BoM if necessary
52                 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)
53                 if test:
54                     testdict.update({prod_id : price})
55         if test:
56             return testdict
57         else:
58             return True
59
60
61     def _calc_price(self, cr, uid, bom, test = False, real_time_accounting=False, context=None):
62         if context is None:
63             context={}
64         price = 0
65         uom_obj = self.pool.get("product.uom")
66         if bom.bom_line_ids:
67             for sbom in bom.bom_line_ids:
68                 my_qty = sbom.bom_line_ids and 1.0 or sbom.product_qty
69                 price += uom_obj._compute_price(cr, uid, sbom.product_id.uom_id.id, sbom.product_id.standard_price, sbom.product_uom.id) * my_qty
70
71         if bom.routing_id:
72             for wline in bom.routing_id.workcenter_lines:
73                 wc = wline.workcenter_id
74                 cycle = wline.cycle_nbr
75                 hour = (wc.time_start + wc.time_stop + cycle * wc.time_cycle) *  (wc.time_efficiency or 1.0)
76                 price += wc.costs_cycle * cycle + wc.costs_hour * hour
77                 price = self.pool.get('product.uom')._compute_price(cr,uid,bom.product_uom.id, price, bom.product_id.uom_id.id)
78         
79         #Convert on product UoM quantities
80         if price > 0:
81             price = uom_obj._compute_price(cr, uid, bom.product_uom.id, price / bom.product_qty, bom.product_id.uom_id.id)
82             product = self.pool.get("product.product").browse(cr, uid, bom.product_id.id, context=context)
83         if not test:
84             if (product.valuation != "real_time" or not real_time_accounting):
85                 self.write(cr, uid, [bom.product_id.id], {'standard_price' : price}, context=context)
86             else:
87                 #Call wizard function here
88                 wizard_obj = self.pool.get("stock.change.standard.price")
89                 ctx = context.copy()
90                 ctx.update({'active_id': bom.product_id.id})
91                 wiz_id = wizard_obj.create(cr, uid, {'new_price': price}, context=ctx)
92                 wizard_obj.change_price(cr, uid, [wiz_id], context=ctx)
93         return price
94
95 product_product()
96
97 class product_bom(osv.osv):
98     _inherit = 'mrp.bom'
99             
100     _columns = {
101         'standard_price': fields.related('product_tmpl_id','standard_price',type="float",relation="product.product",string="Standard Price",store=False)
102     }
103
104 product_bom()
105 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
106