[MERGE] lp:~openerp-dev/openobject-addons/trunk-wiz-remove-btn-highlight-tch
[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     # onchange_price function is not used anywhere 
73     def onchange_price(self, cr, uid, ids, new_price, context=None):
74         """ Sets stock input and output account according to the difference
75             of old price and new price.
76         @param self: The object pointer.
77         @param cr: A database cursor
78         @param uid: ID of the user currently logged in
79         @param ids: List of IDs selected
80         @param new_price: Changed price
81         @param context: A standard dictionary
82         @return: Dictionary of values
83         """
84         if context is None:
85             context = {}
86         product_obj = self.pool.get('product.product').browse(cr, uid, context.get('active_id', False), context=context)
87         price = product_obj.standard_price
88         diff = price - new_price
89         if diff > 0 :
90             return {'value' : {'enable_stock_in_out_acc':True}}
91         else :
92             return {'value' : {'enable_stock_in_out_acc':False}}
93
94     def change_price(self, cr, uid, ids, context=None):
95         """ Changes the Standard Price of Product.
96             And creates an account move accordingly.
97         @param self: The object pointer.
98         @param cr: A database cursor
99         @param uid: ID of the user currently logged in
100         @param ids: List of IDs selected
101         @param context: A standard dictionary
102         @return:
103         """
104         if context is None:
105             context = {}
106         rec_id = context and context.get('active_id', False)
107         assert rec_id, _('Active ID is not set in Context')
108         prod_obj = self.pool.get('product.product')
109         res = self.browse(cr, uid, ids, context=context)
110         datas = {
111             'new_price' : res[0].new_price,
112             'stock_output_account' : res[0].stock_account_output.id,
113             'stock_input_account' : res[0].stock_account_input.id,
114             'stock_journal' : res[0].stock_journal.id
115         }
116         prod_obj.do_change_standard_price(cr, uid, [rec_id], datas, context)
117         return {'type': 'ir.actions.act_window_close'}
118
119 change_standard_price()
120
121 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: