[FIX] stock_account, purchase: the standard price of a product may not be compatible with the decimal precision 'Account' or the coinage of the main currency, so we need to take care of that when creating the stock valuation entries

bzr revid: qdp-launchpad@openerp.com-20131104164939-jupkxy1zrqqq3lpq
This commit is contained in:
Quentin (OpenERP) 2013-11-04 17:49:39 +01:00
parent 400b50963c
commit b5645d2386
2 changed files with 12 additions and 8 deletions

View File

@ -662,7 +662,8 @@ class purchase_order(osv.osv):
if order_line.product_uom.id != order_line.product_id.uom_id.id: if order_line.product_uom.id != order_line.product_id.uom_id.id:
price_unit *= order_line.product_uom.factor price_unit *= order_line.product_uom.factor
if order.currency_id.id != order.company_id.currency_id.id: if order.currency_id.id != order.company_id.currency_id.id:
price_unit = self.pool.get('res.currency').compute(cr, uid, order.currency_id.id, order.company_id.currency_id.id, price_unit, context=context) #we don't round the price_unit, as we may want to store the standard price with more digits than allowed by the currency
price_unit = self.pool.get('res.currency').compute(cr, uid, order.currency_id.id, order.company_id.currency_id.id, price_unit, round=False, context=context)
return { return {
'name': order_line.name or '', 'name': order_line.name or '',

View File

@ -172,10 +172,14 @@ class stock_quant(osv.osv):
""" """
if context is None: if context is None:
context = {} context = {}
currency_obj = self.pool.get('res.currency')
if context.get('force_valuation_amount'): if context.get('force_valuation_amount'):
valuation_amount = context.get('force_valuation_amount') valuation_amount = context.get('force_valuation_amount')
else: else:
valuation_amount = quant.product_id.cost_method == 'real' and quant.cost or quant.product_id.standard_price valuation_amount = quant.product_id.cost_method == 'real' and quant.cost or quant.product_id.standard_price
#the standard_price of the product may be in another decimal precision, or not compatible with the coinage of
#the company currency... so we need to use round() before creating the accounting entries.
valuation_amount = currency_obj.round(cr, uid, quant.company_id.currency_id, valuation_amount * quant.qty)
partner_id = (move.picking_id.partner_id and self.pool.get('res.partner')._find_accounting_partner(move.picking_id.partner_id).id) or False partner_id = (move.picking_id.partner_id and self.pool.get('res.partner')._find_accounting_partner(move.picking_id.partner_id).id) or False
debit_line_vals = { debit_line_vals = {
'name': move.name, 'name': move.name,
@ -185,8 +189,8 @@ class stock_quant(osv.osv):
'ref': move.picking_id and move.picking_id.name or False, 'ref': move.picking_id and move.picking_id.name or False,
'date': time.strftime('%Y-%m-%d'), 'date': time.strftime('%Y-%m-%d'),
'partner_id': partner_id, 'partner_id': partner_id,
'debit': valuation_amount > 0 and valuation_amount * quant.qty or 0, 'debit': valuation_amount > 0 and valuation_amount or 0,
'credit': valuation_amount < 0 and -valuation_amount * quant.qty or 0, 'credit': valuation_amount < 0 and -valuation_amount or 0,
'account_id': debit_account_id, 'account_id': debit_account_id,
} }
credit_line_vals = { credit_line_vals = {
@ -197,12 +201,11 @@ class stock_quant(osv.osv):
'ref': move.picking_id and move.picking_id.name or False, 'ref': move.picking_id and move.picking_id.name or False,
'date': time.strftime('%Y-%m-%d'), 'date': time.strftime('%Y-%m-%d'),
'partner_id': partner_id, 'partner_id': partner_id,
'credit': valuation_amount > 0 and valuation_amount * quant.qty or 0, 'credit': valuation_amount > 0 and valuation_amount or 0,
'debit': valuation_amount < 0 and -valuation_amount * quant.qty or 0, 'debit': valuation_amount < 0 and -valuation_amount or 0,
'account_id': credit_account_id, 'account_id': credit_account_id,
} }
res = [(0, 0, debit_line_vals), (0, 0, credit_line_vals)] return [(0, 0, debit_line_vals), (0, 0, credit_line_vals)]
return res
def _create_account_move_line(self, cr, uid, quant, move, credit_account_id, debit_account_id, journal_id, context=None): def _create_account_move_line(self, cr, uid, quant, move, credit_account_id, debit_account_id, journal_id, context=None):
move_obj = self.pool.get('account.move') move_obj = self.pool.get('account.move')
@ -260,7 +263,7 @@ class stock_move(osv.osv):
new_std_price = move.price_unit new_std_price = move.price_unit
else: else:
# Get the standard price # Get the standard price
amount_unit = product.price_get('standard_price', context=ctx)[product.id] amount_unit = product.standard_price
new_std_price = ((amount_unit * product_avail) + (move.price_unit * move.product_qty)) / (product_avail + move.product_qty) new_std_price = ((amount_unit * product_avail) + (move.price_unit * move.product_qty)) / (product_avail + move.product_qty)
# Write the standard price, as SUPERUSER_ID because a warehouse manager may not have the right to write on products # Write the standard price, as SUPERUSER_ID because a warehouse manager may not have the right to write on products
product_obj.write(cr, SUPERUSER_ID, [product.id], {'standard_price': new_std_price}, context=context) product_obj.write(cr, SUPERUSER_ID, [product.id], {'standard_price': new_std_price}, context=context)