[REF] stock_account: refactoring of do_change_standard_price() + removal of TODO statements

bzr revid: qdp-launchpad@openerp.com-20140425121810-tp2caagu1rnzpgcg
This commit is contained in:
Quentin (OpenERP) 2014-04-25 14:18:10 +02:00
parent e077a96cdd
commit b96b904b2c
5 changed files with 46 additions and 159 deletions

View File

@ -119,9 +119,6 @@ class sale_order(osv.osv):
val['company_id'] = warehouse.company_id.id val['company_id'] = warehouse.company_id.id
return {'value': val} return {'value': val}
# FP Note: to change, take the picking related to the moves related to the
# procurements related to SO lines
def action_view_delivery(self, cr, uid, ids, context=None): def action_view_delivery(self, cr, uid, ids, context=None):
''' '''
This function returns an action that display existing delivery orders This function returns an action that display existing delivery orders
@ -150,10 +147,6 @@ class sale_order(osv.osv):
result['res_id'] = pick_ids and pick_ids[0] or False result['res_id'] = pick_ids and pick_ids[0] or False
return result return result
# TODO: FP Note: I guess it's better to do:
# if order_policy<>picking: super()
# else: call invoice_on_picking_method()
def action_invoice_create(self, cr, uid, ids, grouped=False, states=['confirmed', 'done', 'exception'], date_invoice = False, context=None): def action_invoice_create(self, cr, uid, ids, grouped=False, states=['confirmed', 'done', 'exception'], date_invoice = False, context=None):
move_obj = self.pool.get("stock.move") move_obj = self.pool.get("stock.move")
res = super(sale_order,self).action_invoice_create(cr, uid, ids, grouped=grouped, states=states, date_invoice = date_invoice, context=context) res = super(sale_order,self).action_invoice_create(cr, uid, ids, grouped=grouped, states=states, date_invoice = date_invoice, context=context)

View File

@ -44,6 +44,15 @@ class product_product(osv.osv):
journal_id = product_obj.categ_id.property_stock_journal and product_obj.categ_id.property_stock_journal.id or False journal_id = product_obj.categ_id.property_stock_journal and product_obj.categ_id.property_stock_journal.id or False
account_valuation = product_obj.categ_id.property_stock_valuation_account_id and product_obj.categ_id.property_stock_valuation_account_id.id or False account_valuation = product_obj.categ_id.property_stock_valuation_account_id and product_obj.categ_id.property_stock_valuation_account_id.id or False
if not all([stock_input_acc, stock_output_acc, account_valuation, journal_id]):
raise osv.except_osv(_('Error!'), _('''One of the following information is missing on the product or product category and prevents the accounting valuation entries to be created:
Product: %s
Stock Input Account: %s
Stock Output Account: %s
Stock Valuation Account: %s
Stock Journal: %s
''') % (product_obj.name, stock_input_acc, stock_output_acc, account_valuation, journal_id))
return { return {
'stock_account_input': stock_input_acc, 'stock_account_input': stock_input_acc,
'stock_account_output': stock_output_acc, 'stock_account_output': stock_output_acc,
@ -51,119 +60,59 @@ class product_product(osv.osv):
'property_stock_valuation_account_id': account_valuation 'property_stock_valuation_account_id': account_valuation
} }
# FP Note:;too complex, not good, should be implemented at quant level TODO def do_change_standard_price(self, cr, uid, ids, new_price, context=None):
def do_change_standard_price(self, cr, uid, ids, datas, context=None): """ Changes the Standard Price of Product and creates an account move accordingly."""
""" Changes the Standard Price of Product and creates an account move accordingly.
@param datas : dict. contain default datas like new_price, stock_output_account, stock_input_account, stock_journal
@param context: A standard dictionary
@return:
"""
location_obj = self.pool.get('stock.location') location_obj = self.pool.get('stock.location')
move_obj = self.pool.get('account.move') move_obj = self.pool.get('account.move')
move_line_obj = self.pool.get('account.move.line') move_line_obj = self.pool.get('account.move.line')
if context is None: if context is None:
context = {} context = {}
user_company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
new_price = datas.get('new_price', 0.0) loc_ids = location_obj.search(cr, uid, [('usage', '=', 'internal'), ('company_id', '=', user_company_id)])
stock_output_acc = datas.get('stock_output_account', False)
stock_input_acc = datas.get('stock_input_account', False)
journal_id = datas.get('stock_journal', False)
product_obj=self.browse(cr, uid, ids, context=context)[0]
account_valuation = product_obj.categ_id.property_stock_valuation_account_id
account_valuation_id = account_valuation and account_valuation.id or False
if not account_valuation_id: raise osv.except_osv(_('Error!'), _('Specify valuation Account for Product Category: %s.') % (product_obj.categ_id.name))
move_ids = []
loc_ids = location_obj.search(cr, uid,[('usage','=','internal')])
for rec_id in ids: for rec_id in ids:
datas = self.get_product_accounts(cr, uid, rec_id, context=context)
for location in location_obj.browse(cr, uid, loc_ids, context=context): for location in location_obj.browse(cr, uid, loc_ids, context=context):
c = context.copy() c = context.copy()
c.update({ c.update({'location': location.id, 'compute_child': False})
'location': location.id,
'compute_child': False
})
product = self.browse(cr, uid, rec_id, context=c) product = self.browse(cr, uid, rec_id, context=c)
qty = product.qty_available
diff = product.standard_price - new_price diff = product.standard_price - new_price
if not diff: raise osv.except_osv(_('Error!'), _("No difference between standard price and new price!")) if not diff:
raise osv.except_osv(_('Error!'), _("No difference between standard price and new price!"))
qty = product.qty_available
if qty: if qty:
company_id = location.company_id and location.company_id.id or False
if not company_id: raise osv.except_osv(_('Error!'), _('Please specify company in Location.'))
#
# Accounting Entries # Accounting Entries
# move_vals = {
if not journal_id: 'journal_id': datas['stock_journal'],
journal_id = product.categ_id.property_stock_journal and product.categ_id.property_stock_journal.id or False 'company_id': location.company_id.id,
if not journal_id: }
raise osv.except_osv(_('Error!'), move_id = move_obj.create(cr, uid, move_vals, context=context)
_('Please define journal '\
'on the product category: "%s" (id: %d).') % \
(product.categ_id.name,
product.categ_id.id,))
move_id = move_obj.create(cr, uid, {
'journal_id': journal_id,
'company_id': company_id
})
move_ids.append(move_id)
if diff > 0: if diff > 0:
if not stock_input_acc:
stock_input_acc = product.\
property_stock_account_input.id
if not stock_input_acc:
stock_input_acc = product.categ_id.\
property_stock_account_input_categ.id
if not stock_input_acc:
raise osv.except_osv(_('Error!'),
_('Please define stock input account for this product: "%s" (id: %d).') % \
(product.name,
product.id,))
amount_diff = qty * diff amount_diff = qty * diff
move_line_obj.create(cr, uid, { debit_account_id = datas['stock_account_input']
'name': product.name, credit_account_id = datas['property_stock_valuation_account_id']
'account_id': stock_input_acc, else:
amount_diff = qty * -diff
debit_account_id = datas['property_stock_valuation_account_id']
credit_account_id = datas['stock_account_output']
move_line_obj.create(cr, uid, {
'name': _('Standard Price changed'),
'account_id': debit_account_id,
'debit': amount_diff, 'debit': amount_diff,
'credit': 0,
'move_id': move_id, 'move_id': move_id,
}) }, context=context)
move_line_obj.create(cr, uid, { move_line_obj.create(cr, uid, {
'name': product.categ_id.name, 'name': _('Standard Price changed'),
'account_id': account_valuation_id, 'account_id': credit_account_id,
'debit': 0,
'credit': amount_diff, 'credit': amount_diff,
'move_id': move_id 'move_id': move_id
}) }, context=context)
elif diff < 0:
if not stock_output_acc:
stock_output_acc = product.\
property_stock_account_output.id
if not stock_output_acc:
stock_output_acc = product.categ_id.\
property_stock_account_output_categ.id
if not stock_output_acc:
raise osv.except_osv(_('Error!'),
_('Please define stock output account ' \
'for this product: "%s" (id: %d).') % \
(product.name,
product.id,))
amount_diff = qty * -diff
move_line_obj.create(cr, uid, {
'name': product.name,
'account_id': stock_output_acc,
'credit': amount_diff,
'move_id': move_id
})
move_line_obj.create(cr, uid, {
'name': product.categ_id.name,
'account_id': account_valuation_id,
'debit': amount_diff,
'move_id': move_id
})
self.write(cr, uid, rec_id, {'standard_price': new_price}) self.write(cr, uid, rec_id, {'standard_price': new_price})
return True
return move_ids
_columns = { _columns = {
'valuation': fields.property(type='selection', selection=[('manual_periodic', 'Periodical (manual)'), 'valuation': fields.property(type='selection', selection=[('manual_periodic', 'Periodical (manual)'),

View File

@ -177,15 +177,6 @@ class stock_quant(osv.osv):
acc_valuation = accounts.get('property_stock_valuation_account_id', False) acc_valuation = accounts.get('property_stock_valuation_account_id', False)
journal_id = accounts['stock_journal'] journal_id = accounts['stock_journal']
if not all([acc_src, acc_dest, acc_valuation, journal_id]):
raise osv.except_osv(_('Error!'), _('''One of the following information is missing on the product or product category and prevents the accounting valuation entries to be created:
Product: %s
Stock Input Account: %s
Stock Output Account: %s
Stock Valuation Account: %s
Stock Journal: %s
''') % (move.product_id.name, acc_src, acc_dest, acc_valuation, journal_id))
return journal_id, acc_src, acc_dest, acc_valuation return journal_id, acc_src, acc_dest, acc_valuation
def _prepare_account_move_line(self, cr, uid, move, qty, cost, credit_account_id, debit_account_id, context=None): def _prepare_account_move_line(self, cr, uid, move, qty, cost, credit_account_id, debit_account_id, context=None):

View File

@ -28,13 +28,9 @@ class change_standard_price(osv.osv_memory):
_description = "Change Standard Price" _description = "Change Standard Price"
_columns = { _columns = {
'new_price': fields.float('Price', required=True, digits_compute=dp.get_precision('Product Price'), 'new_price': fields.float('Price', required=True, digits_compute=dp.get_precision('Product Price'),
help="If cost price is increased, stock variation account will be debited " 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" "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."), "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',),
} }
def default_get(self, cr, uid, fields, context=None): def default_get(self, cr, uid, fields, context=None):
@ -52,45 +48,12 @@ class change_standard_price(osv.osv_memory):
product_obj = product_pool.browse(cr, uid, context.get('active_id', False)) product_obj = product_pool.browse(cr, uid, context.get('active_id', False))
res = super(change_standard_price, self).default_get(cr, uid, fields, context=context) res = super(change_standard_price, self).default_get(cr, uid, fields, context=context)
accounts = product_pool.get_product_accounts(cr, uid, context.get('active_id', False), context={})
price = product_obj.standard_price price = product_obj.standard_price
if 'new_price' in fields: if 'new_price' in fields:
res.update({'new_price': price}) res.update({'new_price': price})
if 'stock_account_input' in fields:
res.update({'stock_account_input': accounts['stock_account_input']})
if 'stock_account_output' in fields:
res.update({'stock_account_output': accounts['stock_account_output']})
if 'stock_journal' in fields:
res.update({'stock_journal': accounts['stock_journal']})
if 'enable_stock_in_out_acc' in fields:
res.update({'enable_stock_in_out_acc': True})
return res return res
# onchange_price function is not used anywhere
def onchange_price(self, cr, uid, ids, new_price, context=None):
""" 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
"""
if context is None:
context = {}
product_obj = self.pool.get('product.product').browse(cr, uid, context.get('active_id', False), context=context)
price = product_obj.standard_price
diff = price - new_price
if diff > 0 :
return {'value' : {'enable_stock_in_out_acc':True}}
else :
return {'value' : {'enable_stock_in_out_acc':False}}
def change_price(self, cr, uid, ids, context=None): def change_price(self, cr, uid, ids, context=None):
""" Changes the Standard Price of Product. """ Changes the Standard Price of Product.
And creates an account move accordingly. And creates an account move accordingly.
@ -107,14 +70,7 @@ class change_standard_price(osv.osv_memory):
assert rec_id, _('Active ID is not set in Context.') assert rec_id, _('Active ID is not set in Context.')
prod_obj = self.pool.get('product.product') prod_obj = self.pool.get('product.product')
res = self.browse(cr, uid, ids, context=context) res = self.browse(cr, uid, ids, context=context)
datas = { prod_obj.do_change_standard_price(cr, uid, [rec_id], res[0].new_price, context)
'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)
return {'type': 'ir.actions.act_window_close'} return {'type': 'ir.actions.act_window_close'}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -119,8 +119,6 @@ class stock_landed_cost(osv.osv):
accounts = product_obj.get_product_accounts(cr, uid, line.product_id.id, context=context) accounts = product_obj.get_product_accounts(cr, uid, line.product_id.id, context=context)
debit_account_id = accounts['property_stock_valuation_account_id'] debit_account_id = accounts['property_stock_valuation_account_id']
credit_account_id = cost_product.property_account_expense and cost_product.property_account_expense.id or cost_product.categ_id.property_account_expense_categ.id credit_account_id = cost_product.property_account_expense and cost_product.property_account_expense.id or cost_product.categ_id.property_account_expense_categ.id
if not debit_account_id:
raise osv.except_osv(_('Error!'), _('Please configure Stock Input Account for product: %s.') % (line.product_id.name))
if not credit_account_id: if not credit_account_id:
raise osv.except_osv(_('Error!'), _('Please configure Stock Expense Account for product: %s.') % (cost_product.name)) raise osv.except_osv(_('Error!'), _('Please configure Stock Expense Account for product: %s.') % (cost_product.name))
return self._create_account_move_line(cr, uid, line, move_id, credit_account_id, debit_account_id, context=context) return self._create_account_move_line(cr, uid, line, move_id, credit_account_id, debit_account_id, context=context)