2010-03-16 13:34:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields, osv
|
|
|
|
from tools.translate import _
|
2010-09-23 06:37:18 +00:00
|
|
|
import decimal_precision as dp
|
2010-03-16 13:34:19 +00:00
|
|
|
|
|
|
|
class change_standard_price(osv.osv_memory):
|
|
|
|
_name = "stock.change.standard.price"
|
|
|
|
_description = "Change Standard Price"
|
|
|
|
_columns = {
|
2010-09-23 06:37:18 +00:00
|
|
|
'new_price': fields.float('Price', required=True, digits_compute=dp.get_precision('Account'),
|
2010-08-13 12:20:05 +00:00
|
|
|
help="If cost price is increased, stock variation account will be debited "
|
|
|
|
"and stock output account will be credited with the value = (difference of amount * quantity available).\n"
|
|
|
|
"If cost price is decreased, stock variation account will be creadited and stock input account will be debited."),
|
|
|
|
'stock_account_input':fields.many2one('account.account', 'Stock Input Account'),
|
|
|
|
'stock_account_output':fields.many2one('account.account', 'Stock Output Account'),
|
|
|
|
'stock_journal':fields.many2one('account.journal', 'Stock journal', required=True),
|
|
|
|
'enable_stock_in_out_acc':fields.boolean('Enable Related Account',),
|
2010-03-16 13:34:19 +00:00
|
|
|
}
|
2010-04-06 16:32:10 +00:00
|
|
|
|
2010-11-22 10:37:53 +00:00
|
|
|
def default_get(self, cr, uid, fields, context=None):
|
2010-05-26 12:59:30 +00:00
|
|
|
""" To get default values for the object.
|
2010-03-25 08:28:43 +00:00
|
|
|
@param self: The object pointer.
|
|
|
|
@param cr: A database cursor
|
|
|
|
@param uid: ID of the user currently logged in
|
2010-04-06 16:32:10 +00:00
|
|
|
@param fields: List of fields for which we want default values
|
|
|
|
@param context: A standard dictionary
|
|
|
|
@return: A dictionary which of fields with values.
|
|
|
|
"""
|
2010-11-22 10:37:53 +00:00
|
|
|
if not context:
|
|
|
|
context = {}
|
2010-05-05 10:01:17 +00:00
|
|
|
product_pool = self.pool.get('product.product')
|
|
|
|
product_obj = product_pool.browse(cr, uid, context.get('active_id', False))
|
2010-04-06 16:32:10 +00:00
|
|
|
res = super(change_standard_price, self).default_get(cr, uid, fields, context=context)
|
|
|
|
|
2010-05-05 10:01:17 +00:00
|
|
|
accounts = product_pool.get_product_accounts(cr, uid, context.get('active_id', False), context={})
|
2010-03-25 08:28:43 +00:00
|
|
|
|
|
|
|
price = product_obj.standard_price
|
2010-04-06 16:32:10 +00:00
|
|
|
|
2010-03-19 07:20:12 +00:00
|
|
|
if 'new_price' in fields:
|
2010-03-25 08:28:43 +00:00
|
|
|
res.update({'new_price': price})
|
2010-03-25 11:00:02 +00:00
|
|
|
if 'stock_account_input' in fields:
|
2010-04-06 16:32:10 +00:00
|
|
|
res.update({'stock_account_input': accounts['stock_account_input']})
|
2010-03-25 11:00:02 +00:00
|
|
|
if 'stock_account_output' in fields:
|
2010-04-06 16:32:10 +00:00
|
|
|
res.update({'stock_account_output': accounts['stock_account_output']})
|
2010-03-25 11:00:02 +00:00
|
|
|
if 'stock_journal' in fields:
|
2010-04-06 16:32:10 +00:00
|
|
|
res.update({'stock_journal': accounts['stock_journal']})
|
2010-03-25 08:57:49 +00:00
|
|
|
if 'enable_stock_in_out_acc' in fields:
|
2010-04-06 16:32:10 +00:00
|
|
|
res.update({'enable_stock_in_out_acc': True})
|
|
|
|
|
2010-03-16 13:34:19 +00:00
|
|
|
return res
|
2010-04-06 16:32:10 +00:00
|
|
|
|
2010-11-22 10:37:53 +00:00
|
|
|
def onchange_price(self, cr, uid, ids, new_price, context=None):
|
2010-05-26 12:59:30 +00:00
|
|
|
""" Sets stock input and output account according to the difference
|
|
|
|
of old price and new price.
|
|
|
|
@param self: The object pointer.
|
|
|
|
@param cr: A database cursor
|
|
|
|
@param uid: ID of the user currently logged in
|
|
|
|
@param ids: List of IDs selected
|
|
|
|
@param new_price: Changed price
|
|
|
|
@param context: A standard dictionary
|
|
|
|
@return: Dictionary of values
|
|
|
|
"""
|
2010-11-22 10:37:53 +00:00
|
|
|
if not context:
|
|
|
|
context = {}
|
|
|
|
product_obj = self.pool.get('product.product').browse(cr, uid, context.get('active_id', False), context=context)
|
2010-03-25 08:28:43 +00:00
|
|
|
price = product_obj.standard_price
|
|
|
|
diff = price - new_price
|
2010-04-06 16:32:10 +00:00
|
|
|
if diff > 0 :
|
2010-03-25 08:57:49 +00:00
|
|
|
return {'value' : {'enable_stock_in_out_acc':True}}
|
2010-03-26 09:25:02 +00:00
|
|
|
else :
|
|
|
|
return {'value' : {'enable_stock_in_out_acc':False}}
|
2010-04-06 16:32:10 +00:00
|
|
|
|
2010-11-22 10:37:53 +00:00
|
|
|
def change_price(self, cr, uid, ids, context=None):
|
2010-05-26 12:59:30 +00:00
|
|
|
""" Changes the Standard Price of Product.
|
|
|
|
And creates an account move accordingly.
|
|
|
|
@param self: The object pointer.
|
|
|
|
@param cr: A database cursor
|
|
|
|
@param uid: ID of the user currently logged in
|
|
|
|
@param ids: List of IDs selected
|
|
|
|
@param context: A standard dictionary
|
|
|
|
@return:
|
2010-03-16 13:34:19 +00:00
|
|
|
"""
|
2010-11-22 10:37:53 +00:00
|
|
|
if not context:
|
|
|
|
context = {}
|
2010-03-16 13:34:19 +00:00
|
|
|
rec_id = context and context.get('active_id', False)
|
2010-04-30 15:00:49 +00:00
|
|
|
assert rec_id, _('Active ID is not set in Context')
|
|
|
|
prod_obj = self.pool.get('product.product')
|
2010-11-22 10:37:53 +00:00
|
|
|
res = self.browse(cr, uid, ids, context=context)
|
2010-04-30 15:00:49 +00:00
|
|
|
datas = {
|
|
|
|
'new_price' : res[0].new_price,
|
|
|
|
'stock_output_account' : res[0].stock_account_output.id,
|
|
|
|
'stock_input_account' : res[0].stock_account_input.id,
|
|
|
|
'stock_journal' : res[0].stock_journal.id
|
|
|
|
}
|
|
|
|
prod_obj.do_change_standard_price(cr, uid, [rec_id], datas, context)
|
2010-04-06 16:15:10 +00:00
|
|
|
return {}
|
2010-03-16 13:34:19 +00:00
|
|
|
|
|
|
|
change_standard_price()
|