[FIX] Removed all code and XML mentions of "finnished", as it's written "finished...
[odoo/odoo.git] / addons / mrp / product.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 osv import fields, osv
23 from tools.translate import _
24
25
26 class product_product(osv.osv):
27     _inherit = "product.product"    
28
29     def get_product_accounts(self, cr, uid, product_id, context={}):
30         """ To get the stock input account, stock output account and stock journal related to product.
31         @param product_id: product id            
32         @return: dictionary which contains information regarding stock input account, stock output account and stock journal
33         """           
34         product_obj = self.pool.get('product.product').browse(cr, uid, product_id, False)
35         res = super(product_product,self).get_product_accounts(cr, uid, product_id, context)
36         stock_input_acc = product_obj.property_stock_account_input and product_obj.property_stock_account_input.id or False 
37         if not stock_input_acc:
38             stock_input_acc = product_obj.categ_id.property_stock_account_input_categ and product_obj.categ_id.property_stock_account_input_categ.id or False
39         
40         stock_output_acc = product_obj.property_stock_account_output and product_obj.property_stock_account_output.id or False
41         if not stock_output_acc:
42             stock_output_acc = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id or False
43
44         journal_id = product_obj.categ_id.property_stock_journal and product_obj.categ_id.property_stock_journal.id or False
45         
46         res.update({'stock_account_input': stock_input_acc})
47         res.update({'stock_account_output': stock_output_acc})
48         res.update({'stock_journal': journal_id})  
49         
50         return res
51     
52
53     _columns = {
54         "bom_ids": fields.one2many('mrp.bom', 'product_id','Bill of Materials'),
55     }
56
57     def do_change_standard_price(self, cr, uid, ids, datas, context={}):
58         """ Changes the Standard Price of Product and parent products and creates an account move accordingly.
59         @param datas: dict. contain default datas like new_price, stock_output_account, stock_input_account, stock_journal
60         @param context: A standard dictionary
61         @return:
62         """
63         res = super(product_product, self).do_change_standard_price(cr, uid, ids, datas, context=context)
64         bom_obj = self.pool.get('mrp.bom')
65         change = context.get('change_parent_price', False)
66         def _compute_price(bom):
67             price = 0.0
68             if bom.bom_id and change:
69                 if bom.bom_id.bom_lines:
70                     for bom_line in bom.bom_id.bom_lines:
71                         prod_price = self.read(cr, uid, bom_line.product_id.id, ['standard_price'])['standard_price']
72                         price += bom_line.product_qty * prod_price
73
74                     accounts = self.get_product_accounts(cr, uid, bom.bom_id.product_id.id, context)
75
76                     datas = {
77                         'new_price': price,
78                         'stock_output_account': accounts['stock_account_output'],
79                         'stock_input_account': accounts['stock_account_input'],
80                         'stock_journal': accounts['stock_journal']
81                     }
82                     super(product_product, self).do_change_standard_price(cr, uid, [bom.bom_id.product_id.id], datas, context)
83                 _compute_price(bom.bom_id)
84             return price
85
86         bom_ids = bom_obj.search(cr, uid, [('product_id', 'in', ids)])
87
88         for bom in bom_obj.browse(cr, uid, bom_ids):
89             _compute_price(bom)
90
91 product_product()