From dfcc0c40b1f9ca218cc62f3fbc23a43a7b93f63c Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 28 Dec 2016 14:01:11 +0100 Subject: [PATCH] [FIX] account, stock_account: anglo saxong cost price currency rounding When selling in a foreign currency, A double currency rate change, one in each way, was applied when creating the good cost move line of the invoice, which leaded to some precision loss when the decimal accuracy was set with a number of digits greater than the currencies digits rounding e.g. Product cost price: 52.56422 CAD to USD rate: 0.7383 The product cost price is always in the company currency while `_anglo_saxon_sale_move_lines` always returned the price in the currency of the invoice. A first conversion rate was applied from the company currency to the invoice currency. 52.56422 * 0.7383 = 38.808163625999995, rounded to 38.81 Then, when validating the invoice, and creating the move line, the price was then converted again, to the company currency, as the debit/credit of a move line is always in the company currency: 38.81 / 0.7383 = 52.566707300555336, rounded to 52.57 The cost price of the product in the good cost move line was therefore set to 52.57, insteaf of 52.56, because of this precision loss. With this revision, we avoid to do the currency rate conversion two times, in both ways, to avoid the precision loss. opw-702045 --- addons/account/models/account_invoice.py | 7 ++++--- addons/stock_account/stock_account.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index 3841f66e16d..8d5fc63154f 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -633,9 +633,10 @@ class AccountInvoice(models.Model): for line in invoice_move_lines: if self.currency_id != company_currency: currency = self.currency_id.with_context(date=self.date_invoice or fields.Date.context_today(self)) - line['currency_id'] = currency.id - line['amount_currency'] = currency.round(line['price']) - line['price'] = currency.compute(line['price'], company_currency) + if not ('currency_id' in line and 'amount_currency' in line): + line['currency_id'] = currency.id + line['amount_currency'] = currency.round(line['price']) + line['price'] = currency.compute(line['price'], company_currency) else: line['currency_id'] = False line['amount_currency'] = False diff --git a/addons/stock_account/stock_account.py b/addons/stock_account/stock_account.py index 1f054a8021f..24c87be86f5 100644 --- a/addons/stock_account/stock_account.py +++ b/addons/stock_account/stock_account.py @@ -83,13 +83,21 @@ class account_invoice(osv.osv): cacc = accounts['expense'].id if dacc and cacc: price_unit = i_line._get_anglo_saxon_price_unit() + if inv.currency_id.id != company_currency: + currency_id = inv.currency_id.id + amount_currency = self.env['account.invoice.line']._get_price(inv, company_currency, i_line, price_unit) + else: + currency_id = False + amount_currency = False return [ { 'type':'src', 'name': i_line.name[:64], 'price_unit': price_unit, 'quantity': i_line.quantity, - 'price': self.env['account.invoice.line']._get_price(inv, company_currency, i_line, price_unit), + 'price': price_unit * i_line.quantity, + 'currency_id': currency_id, + 'amount_currency': amount_currency, 'account_id':dacc, 'product_id':i_line.product_id.id, 'uom_id':i_line.uom_id.id, @@ -101,7 +109,9 @@ class account_invoice(osv.osv): 'name': i_line.name[:64], 'price_unit': price_unit, 'quantity': i_line.quantity, - 'price': -1 * self.env['account.invoice.line']._get_price(inv, company_currency, i_line, price_unit), + 'price': -1 * price_unit * i_line.quantity, + 'currency_id': currency_id, + 'amount_currency': -1 * amount_currency, 'account_id':cacc, 'product_id':i_line.product_id.id, 'uom_id':i_line.uom_id.id,