[FIX] stock: apply better 6.0 bugfix for #771377
[odoo/odoo.git] / addons / stock / wizard / stock_change_standard_price.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 import decimal_precision as dp
25
26 class change_standard_price(osv.osv_memory):
27     _name = "stock.change.standard.price"
28     _description = "Change Standard Price"
29     _columns = {
30         'new_price': fields.float('Price', required=True, digits_compute=dp.get_precision('Account'),
31                                   help="If cost price is increased, stock variation account will be debited "
32                                         "and stock output account will be credited with the value = (difference of amount * quantity available).\n"
33                                         "If cost price is decreased, stock variation account will be creadited and stock input account will be debited."),
34         'stock_account_input':fields.many2one('account.account', 'Stock Input Account'),
35         'stock_account_output':fields.many2one('account.account', 'Stock Output Account'),
36         'stock_journal':fields.many2one('account.journal', 'Stock journal', required=True),
37         'enable_stock_in_out_acc':fields.boolean('Enable Related Account',),
38     }
39
40     def default_get(self, cr, uid, fields, context=None):
41         """ To get default values for the object.
42          @param self: The object pointer.
43          @param cr: A database cursor
44          @param uid: ID of the user currently logged in
45          @param fields: List of fields for which we want default values
46          @param context: A standard dictionary
47          @return: A dictionary which of fields with values.
48         """
49         if context is None:
50             context = {}
51         product_pool = self.pool.get('product.product')
52         product_obj = product_pool.browse(cr, uid, context.get('active_id', False))
53         res = super(change_standard_price, self).default_get(cr, uid, fields, context=context)
54
55         accounts = product_pool.get_product_accounts(cr, uid, context.get('active_id', False), context={})
56
57         price = product_obj.standard_price
58
59         if 'new_price' in fields:
60             res.update({'new_price': price})
61         if 'stock_account_input' in fields:
62             res.update({'stock_account_input': accounts['stock_account_input']})
63         if 'stock_account_output' in fields:
64             res.update({'stock_account_output': accounts['stock_account_output']})
65         if 'stock_journal' in fields:
66             res.update({'stock_journal': accounts['stock_journal']})
67         if 'enable_stock_in_out_acc' in fields:
68             res.update({'enable_stock_in_out_acc': True})
69
70         return res
71
72     def onchange_price(self, cr, uid, ids, new_price, context=None):
73         """ Sets stock input and output account according to the difference
74             of old price and new price.
75         @param self: The object pointer.
76         @param cr: A database cursor
77         @param uid: ID of the user currently logged in
78         @param ids: List of IDs selected
79         @param new_price: Changed price
80         @param context: A standard dictionary
81         @return: Dictionary of values
82         """
83         if context is None:
84             context = {}
85         product_obj = self.pool.get('product.product').browse(cr, uid, context.get('active_id', False), context=context)
86         price = product_obj.standard_price
87         diff = price - new_price
88         if diff > 0 :
89             return {'value' : {'enable_stock_in_out_acc':True}}
90         else :
91             return {'value' : {'enable_stock_in_out_acc':False}}
92
93     def change_price(self, cr, uid, ids, context=None):
94         """ Changes the Standard Price of Product.
95             And creates an account move accordingly.
96         @param self: The object pointer.
97         @param cr: A database cursor
98         @param uid: ID of the user currently logged in
99         @param ids: List of IDs selected
100         @param context: A standard dictionary
101         @return:
102         """
103         if context is None:
104             context = {}
105         rec_id = context and context.get('active_id', False)
106         assert rec_id, _('Active ID is not set in Context')
107         prod_obj = self.pool.get('product.product')
108         res = self.browse(cr, uid, ids, context=context)
109         datas = {
110             'new_price' : res[0].new_price,
111             'stock_output_account' : res[0].stock_account_output.id,
112             'stock_input_account' : res[0].stock_account_input.id,
113             'stock_journal' : res[0].stock_journal.id
114         }
115         prod_obj.do_change_standard_price(cr, uid, [rec_id], datas, context)
116         return {'type': 'ir.actions.act_window_close'}
117
118 change_standard_price()