diff --git a/addons/account/models/account_bank_statement.py b/addons/account/models/account_bank_statement.py index cef61aba094..59e9a3ae3e0 100644 --- a/addons/account/models/account_bank_statement.py +++ b/addons/account/models/account_bank_statement.py @@ -663,7 +663,8 @@ class AccountBankStatementLine(models.Model): # Complete dicts to create both counterpart move lines and write-offs to_create = (counterpart_aml_dicts + new_aml_dicts) - ctx = dict(self._context, date=self.date) + company = self.company_id + date = self.date or fields.Date.today() for aml_dict in to_create: aml_dict['move_id'] = move.id aml_dict['partner_id'] = self.partner_id.id @@ -677,12 +678,12 @@ class AccountBankStatementLine(models.Model): aml_dict['credit'] = company_currency.round(aml_dict['credit'] / st_line_currency_rate) elif self.currency_id and st_line_currency_rate: # Statement is in foreign currency and the transaction is in another one - aml_dict['debit'] = statement_currency.with_context(ctx).compute(aml_dict['debit'] / st_line_currency_rate, company_currency) - aml_dict['credit'] = statement_currency.with_context(ctx).compute(aml_dict['credit'] / st_line_currency_rate, company_currency) + aml_dict['debit'] = statement_currency._convert(aml_dict['debit'] / st_line_currency_rate, company_currency, company, date) + aml_dict['credit'] = statement_currency._convert(aml_dict['credit'] / st_line_currency_rate, company_currency, company, date) else: # Statement is in foreign currency and no extra currency is given for the transaction - aml_dict['debit'] = st_line_currency.with_context(ctx).compute(aml_dict['debit'], company_currency) - aml_dict['credit'] = st_line_currency.with_context(ctx).compute(aml_dict['credit'], company_currency) + aml_dict['debit'] = st_line_currency._convert(aml_dict['debit'], company_currency, company, date) + aml_dict['credit'] = st_line_currency._convert(aml_dict['credit'], company_currency, company, date) elif statement_currency.id != company_currency.id: # Statement is in foreign currency but the transaction is in company currency prorata_factor = (aml_dict['debit'] - aml_dict['credit']) / self.amount_currency diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index db676eb6e7d..52c7f346734 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -59,9 +59,9 @@ class AccountInvoice(models.Model): amount_total_company_signed = self.amount_total amount_untaxed_signed = self.amount_untaxed if self.currency_id and self.company_id and self.currency_id != self.company_id.currency_id: - currency_id = self.currency_id.with_context(date=self.date_invoice) - amount_total_company_signed = currency_id.compute(self.amount_total, self.company_id.currency_id) - amount_untaxed_signed = currency_id.compute(self.amount_untaxed, self.company_id.currency_id) + currency_id = self.currency_id + amount_total_company_signed = currency_id._convert(self.amount_total, self.company_id.currency_id, self.company_id, self.date_invoice or fields.Date.today()) + amount_untaxed_signed = currency_id._convert(self.amount_untaxed, self.company_id.currency_id, self.company_id, self.date_invoice or fields.Date.today()) sign = self.type in ['in_refund', 'out_refund'] and -1 or 1 self.amount_total_company_signed = amount_total_company_signed * sign self.amount_total_signed = self.amount_total * sign @@ -114,8 +114,8 @@ class AccountInvoice(models.Model): if line.currency_id == self.currency_id: residual += line.amount_residual_currency if line.currency_id else line.amount_residual else: - from_currency = (line.currency_id and line.currency_id.with_context(date=line.date)) or line.company_id.currency_id.with_context(date=line.date) - residual += from_currency.compute(line.amount_residual, self.currency_id) + from_currency = line.currency_id or line.company_id.currency_id + residual += from_currency._convert(line.amount_residual, self.currency_id, line.company_id, line.date or fields.Date.today()) self.residual_company_signed = abs(residual_company_signed) * sign self.residual_signed = abs(residual) * sign self.residual = abs(residual) @@ -145,7 +145,8 @@ class AccountInvoice(models.Model): if line.currency_id and line.currency_id == self.currency_id: amount_to_show = abs(line.amount_residual_currency) else: - amount_to_show = line.company_id.currency_id.with_context(date=line.date).compute(abs(line.amount_residual), self.currency_id) + currency = line.company_id.currency_id + amount_to_show = currency._convert(abs(line.amount_residual), self.currency_id, self.company_id, line.date or fields.Date.today()) if float_is_zero(amount_to_show, precision_rounding=self.currency_id.rounding): continue info['content'].append({ @@ -189,8 +190,8 @@ class AccountInvoice(models.Model): if payment_currency_id and payment_currency_id == self.currency_id: amount_to_show = amount_currency else: - amount_to_show = payment.company_id.currency_id.with_context(date=self.date).compute(amount, - self.currency_id) + currency = payment.company_id.currency_id + amount_to_show = currency._convert(amount, self.currency_id, payment.company_id, self.date or fields.Date.today()) if float_is_zero(amount_to_show, precision_rounding=self.currency_id.rounding): continue payment_ref = payment.move_id.name @@ -920,8 +921,9 @@ class AccountInvoice(models.Model): self.ensure_one() credit_aml = self.env['account.move.line'].browse(credit_aml_id) if not credit_aml.currency_id and self.currency_id != self.company_id.currency_id: + amount_currency = self.company_id.currency_id._convert(credit_aml.balance, self.currency_id, self.company_id, credit_aml.date or fields.Date.today()) credit_aml.with_context(allow_amount_currency=True, check_move_validity=False).write({ - 'amount_currency': self.company_id.currency_id.with_context(date=credit_aml.date).compute(credit_aml.balance, self.currency_id), + 'amount_currency': amount_currency, 'currency_id': self.currency_id.id}) if credit_aml.payment_id: credit_aml.payment_id.write({'invoice_ids': [(4, self.id, None)]}) @@ -952,11 +954,12 @@ class AccountInvoice(models.Model): total_currency = 0 for line in invoice_move_lines: if self.currency_id != company_currency: - currency = self.currency_id.with_context(date=self._get_currency_rate_date() or fields.Date.context_today(self)) + currency = self.currency_id + date = self._get_currency_rate_date() or fields.Date.context_today(self) if not (line.get('currency_id') and line.get('amount_currency')): line['currency_id'] = currency.id line['amount_currency'] = currency.round(line['price']) - line['price'] = currency.compute(line['price'], company_currency) + line['price'] = currency._convert(line['price'], company_currency, self.company_id, date) else: line['currency_id'] = False line['amount_currency'] = False @@ -1106,7 +1109,7 @@ class AccountInvoice(models.Model): ctx['date'] = inv._get_currency_rate_date() for i, t in enumerate(totlines): if inv.currency_id != company_currency: - amount_currency = company_currency.with_context(ctx).compute(t[1], inv.currency_id) + amount_currency = company_currency._convert(t[1], inv.currency_id, inv.company_id, inv.date or inv.date_invoice or fields.Date.today()) else: amount_currency = False @@ -1443,7 +1446,9 @@ class AccountInvoiceLine(models.Model): self.price_subtotal = price_subtotal_signed = taxes['total_excluded'] if taxes else self.quantity * price self.price_total = taxes['total_included'] if taxes else self.price_subtotal if self.invoice_id.currency_id and self.invoice_id.currency_id != self.invoice_id.company_id.currency_id: - price_subtotal_signed = self.invoice_id.currency_id.with_context(date=self.invoice_id._get_currency_rate_date()).compute(price_subtotal_signed, self.invoice_id.company_id.currency_id) + currency = self.invoice_id.currency_id + date = self.invoice_id._get_currency_rate_date() + price_subtotal_signed = currency._convert(price_subtotal_signed, self.invoice_id.company_id.currency_id, self.company_id, date or fields.Date.today()) sign = self.invoice_id.type in ['in_refund', 'out_refund'] and -1 or 1 self.price_subtotal_signed = price_subtotal_signed * sign diff --git a/addons/account/models/account_journal_dashboard.py b/addons/account/models/account_journal_dashboard.py index d9cf58720ea..cdddf586126 100644 --- a/addons/account/models/account_journal_dashboard.py +++ b/addons/account/models/account_journal_dashboard.py @@ -189,7 +189,7 @@ class account_journal(models.Model): query_results_drafts = self.env.cr.dictfetchall() today = datetime.today() - query = """SELECT amount_total, currency_id AS currency, type FROM account_invoice WHERE journal_id = %s AND date <= %s AND state = 'open';""" + query = """SELECT amount_total, currency_id AS currency, type, date_invoice, company_id FROM account_invoice WHERE journal_id = %s AND date <= %s AND state = 'open';""" self.env.cr.execute(query, (self.id, today)) late_query_results = self.env.cr.dictfetchall() (number_waiting, sum_waiting) = self._count_results_and_sum_amounts(query_results_to_pay, currency) @@ -219,7 +219,7 @@ class account_journal(models.Model): data as its first element, and the arguments dictionary to use to run it as its second. """ - return ("""SELECT state, residual_signed as amount_total, currency_id AS currency, type + return ("""SELECT state, residual_signed as amount_total, currency_id AS currency, type, date_invoice, company_id FROM account_invoice WHERE journal_id = %(journal_id)s AND state = 'open';""", {'journal_id':self.id}) @@ -229,7 +229,7 @@ class account_journal(models.Model): gather the bills in draft state data, and the arguments dictionary to use to run it as its second. """ - return ("""SELECT state, amount_total, currency_id AS currency, type + return ("""SELECT state, amount_total, currency_id AS currency, type, date_invoice, company_id FROM account_invoice WHERE journal_id = %(journal_id)s AND state = 'draft';""", {'journal_id':self.id}) @@ -241,10 +241,11 @@ class account_journal(models.Model): rslt_sum = 0.0 for result in results_dict: cur = self.env['res.currency'].browse(result.get('currency')) + company = self.env['res.company'].browse(result.get('company_id')) rslt_count += 1 - type_factor = result.get('type') in ('in_refund', 'out_refund') and -1 or 1 - rslt_sum += type_factor * cur.compute(result.get('amount_total'), target_currency) + rslt_sum += type_factor * cur._convert( + result.get('amount_total'), target_currency, company, result.get('date') or fields.Date.today()) return (rslt_count, rslt_sum) @api.multi diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index ae79a3f9c1e..1daa6d25663 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -548,7 +548,7 @@ class AccountMoveLine(models.Model): for line in self: amount = line.amount_currency if line.currency_id and line.currency_id != line.company_currency_id: - amount = line.currency_id.with_context(date=line.date).compute(amount, line.company_currency_id) + amount = line.currency_id._convert(amount, line.company_currency_id, line.company_id, line.date or fields.Date.today()) line.debit = amount > 0 and amount or 0.0 line.credit = amount < 0 and -amount or 0.0 @@ -634,7 +634,8 @@ class AccountMoveLine(models.Model): company_currency = sm_debit_move.company_id.currency_id currency = sm_debit_move.currency_id or sm_credit_move.currency_id currency_date = sm_debit_move.currency_id and sm_credit_move.date or sm_debit_move.date - amount_reconcile_currency = company_currency.with_context(date=currency_date).compute(amount_reconcile, currency) + amount_reconcile_currency = company_currency._convert( + amount_reconcile, currency, sm_debit_move.company_id, currency_date or fields.Date.today()) currency = currency.id amount_reconcile = min(sm_debit_move.amount_residual, -sm_credit_move.amount_residual) @@ -897,11 +898,9 @@ class AccountMoveLine(models.Model): if self._context.get('skip_full_reconcile_check') == 'amount_currency_excluded': vals['amount_currency'] = 0.0 else: - ctx = {} - if 'date' in vals: - ctx['date'] = vals['date'] - vals['amount_currency'] = account.company_id.currency_id.with_context(ctx).compute(amount, account.currency_id) - + company = account.company_id + currency = account.company_id.currency_id + vals['amount_currency'] = currency._convert(amount, account.currency_id, company, vals.get('date') or fields.Date.today()) if not ok: raise UserError(_('You cannot use this general account in this journal, check the tab \'Entry Controls\' on the related journal.')) @@ -941,11 +940,10 @@ class AccountMoveLine(models.Model): } bank = self.env["account.bank.statement"].browse(vals.get('statement_id')) if bank.currency_id != bank.company_id.currency_id: - ctx = {} - if 'date' in vals: - ctx['date'] = vals['date'] + company = bank.company_id + bank_currency = bank.company_id.currency_id temp['currency_id'] = bank.currency_id.id - temp['amount_currency'] = bank.company_id.currency_id.with_context(ctx).compute(tax_vals['amount'], bank.currency_id, round=True) + temp['amount_currency'] = bank_currency._convert(tax_vals['amount'], bank.currency_id, company, vals.get('date') or fields.Date.today()) tax_lines_vals.append(temp) #Toggle the 'tax_exigible' field to False in case it is not yet given and the tax in 'tax_line_id' or one of @@ -1084,9 +1082,11 @@ class AccountMoveLine(models.Model): """ Helper function to compute value for fields debit/credit/amount_currency based on an amount and the currencies given in parameter""" amount_currency = False currency_id = False + date = self.env.context.get('date') or fields.Date.today() + company = self.env.context.get('company_id', self.env.user.company_id) if src_currency and src_currency != company_currency: amount_currency = amount - amount = src_currency.with_context(self._context).compute(amount, company_currency) + amount = src_currency._convert(amount, company_currency, company, date) currency_id = src_currency.id debit = amount > 0 and amount or 0.0 credit = amount < 0 and -amount or 0.0 diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py index c27c07e9231..0ef8ca940ec 100644 --- a/addons/account/models/account_payment.py +++ b/addons/account/models/account_payment.py @@ -201,7 +201,7 @@ class account_abstract_payment(models.AbstractModel): if payment_currency == currency: total += amount_total else: - total += payment_currency.with_context(date=self.payment_date).compute(amount_total, currency) + total += payment_currency._convert(amount_total, currency, self.env.user.company_id, self.payment_date or fields.Date.today()) return total @@ -594,7 +594,7 @@ class account_payment(models.Model): """ aml_obj = self.env['account.move.line'].with_context(check_move_validity=False) debit, credit, amount_currency, dummy = aml_obj.with_context(date=self.payment_date)._compute_amount_fields(amount, self.currency_id, self.company_id.currency_id) - amount_currency = self.destination_journal_id.currency_id and self.currency_id.with_context(date=self.payment_date).compute(amount, self.destination_journal_id.currency_id) or 0 + amount_currency = self.destination_journal_id.currency_id and self.currency_id._convert(amount, self.destination_journal_id.currency_id, self.company_id, self.payment_date or fields.Date.today()) or 0 dst_move = self.env['account.move'].create(self._get_move_vals(self.destination_journal_id)) @@ -691,7 +691,7 @@ class account_payment(models.Model): # If the journal has a currency specified, the journal item need to be expressed in this currency if self.journal_id.currency_id and self.currency_id != self.journal_id.currency_id: - amount = self.currency_id.with_context(date=self.payment_date).compute(amount, self.journal_id.currency_id) + amount = self.currency_id._convert(amount, self.journal_id.currency_id, self.company_id, self.payment_date or fields.Date.today()) debit, credit, amount_currency, dummy = self.env['account.move.line'].with_context(date=self.payment_date)._compute_amount_fields(amount, self.journal_id.currency_id, self.company_id.currency_id) vals.update({ 'amount_currency': amount_currency, diff --git a/addons/account/models/reconciliation_widget.py b/addons/account/models/reconciliation_widget.py index 9f1f4c5addd..8cd405ac5be 100644 --- a/addons/account/models/reconciliation_widget.py +++ b/addons/account/models/reconciliation_widget.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from odoo import api, models, _ +from odoo import api, fields, models, _ from odoo.exceptions import UserError from odoo.osv import expression from odoo.tools import pycompat @@ -530,8 +530,6 @@ class AccountReconciliation(models.AbstractModel): target_currency = target_currency or company_currency - ctx = context.copy() - ctx.update({'date': target_date or line.date}) # Use case: # Let's assume that company currency is in USD and that we have the 3 following move lines # Debit Credit Amount currency Currency @@ -571,8 +569,10 @@ class AccountReconciliation(models.AbstractModel): total_amount_currency = "" else: amount_currency = line.currency_id and amount_currency or amount - amount = company_currency.with_context(ctx).compute(amount, target_currency) - total_amount = company_currency.with_context(ctx).compute((line.debit - line.credit), target_currency) + company = line.account_id.company_id + date = target_date or line.date + amount = company_currency._convert(amount, target_currency, company, date) + total_amount = company_currency._convert((line.debit - line.credit), target_currency, company, date) total_amount_currency = line.currency_id and line.amount_currency or (line.debit - line.credit) ret_line['debit'] = amount > 0 and amount or 0 @@ -735,11 +735,13 @@ class AccountReconciliation(models.AbstractModel): # Create writeoff move lines if len(new_mv_line_dicts) > 0: company_currency = account_move_line[0].account_id.company_id.currency_id + company = account_move_line[0].account_id.company_id + date = fields.Date.today() writeoff_currency = account_move_line[0].currency_id or company_currency for mv_line_dict in new_mv_line_dicts: if writeoff_currency != company_currency: - mv_line_dict['debit'] = writeoff_currency.compute(mv_line_dict['debit'], company_currency) - mv_line_dict['credit'] = writeoff_currency.compute(mv_line_dict['credit'], company_currency) + mv_line_dict['debit'] = writeoff_currency._convert(mv_line_dict['debit'], company_currency, company, date) + mv_line_dict['credit'] = writeoff_currency._convert(mv_line_dict['credit'], company_currency, company, date) writeoff_lines += account_move_line._create_writeoff(mv_line_dict) (account_move_line + writeoff_lines).reconcile() diff --git a/addons/account/report/account_invoice_report.py b/addons/account/report/account_invoice_report.py index 624a60bab11..791d0820f63 100644 --- a/addons/account/report/account_invoice_report.py +++ b/addons/account/report/account_invoice_report.py @@ -15,18 +15,17 @@ class AccountInvoiceReport(models.Model): def _compute_amounts_in_user_currency(self): """Compute the amounts in the currency of the user """ - context = dict(self._context or {}) user_currency_id = self.env.user.company_id.currency_id currency_rate_id = self.env['res.currency.rate'].search([ ('rate', '=', 1), '|', ('company_id', '=', self.env.user.company_id.id), ('company_id', '=', False)], limit=1) base_currency_id = currency_rate_id.currency_id - ctx = context.copy() for record in self: - ctx['date'] = record.date - record.user_currency_price_total = base_currency_id.with_context(ctx).compute(record.price_total, user_currency_id) - record.user_currency_price_average = base_currency_id.with_context(ctx).compute(record.price_average, user_currency_id) - record.user_currency_residual = base_currency_id.with_context(ctx).compute(record.residual, user_currency_id) + date = record.date or fields.Date.today() + company = record.company_id + record.user_currency_price_total = base_currency_id._convert(record.price_total, user_currency_id, company, date) + record.user_currency_price_average = base_currency_id._convert(record.price_average, user_currency_id, company, date) + record.user_currency_residual = base_currency_id._convert(record.residual, user_currency_id, company, date) date = fields.Date(readonly=True) product_id = fields.Many2one('product.product', string='Product', readonly=True) diff --git a/addons/account_asset/models/account_asset.py b/addons/account_asset/models/account_asset.py index 58b42ecc219..1be9460e7e3 100644 --- a/addons/account_asset/models/account_asset.py +++ b/addons/account_asset/models/account_asset.py @@ -516,7 +516,8 @@ class AccountAssetDepreciationLine(models.Model): company_currency = line.asset_id.company_id.currency_id current_currency = line.asset_id.currency_id prec = company_currency.decimal_places - amount = current_currency.with_context(date=depreciation_date).compute(line.amount, company_currency) + amount = current_currency._convert( + line.amount, company_currency, line.asset_id.company_id, depreciation_date) asset_name = line.asset_id.name + ' (%s/%s)' % (line.sequence, len(line.asset_id.depreciation_line_ids)) move_line_1 = { 'name': asset_name, @@ -554,7 +555,8 @@ class AccountAssetDepreciationLine(models.Model): # Sum amount of all depreciation lines company_currency = line.asset_id.company_id.currency_id current_currency = line.asset_id.currency_id - amount += current_currency.compute(line.amount, company_currency) + company = line.asset_id.company_id + amount += current_currency._convert(line.amount, company_currency, company, fields.Date.today()) name = category_id.name + _(' (grouped)') move_line_1 = { diff --git a/addons/account_voucher/models/account_voucher.py b/addons/account_voucher/models/account_voucher.py index 73cbff6aa52..da8a01d2d39 100644 --- a/addons/account_voucher/models/account_voucher.py +++ b/addons/account_voucher/models/account_voucher.py @@ -176,9 +176,9 @@ class AccountVoucher(models.Model): def first_move_line_get(self, move_id, company_currency, current_currency): debit = credit = 0.0 if self.voucher_type == 'purchase': - credit = self._convert_amount(self.amount) + credit = self._convert(self.amount) elif self.voucher_type == 'sale': - debit = self._convert_amount(self.amount) + debit = self._convert(self.amount) if debit < 0.0: debit = 0.0 if credit < 0.0: credit = 0.0 sign = debit - credit < 0 and -1 or 1 @@ -220,7 +220,7 @@ class AccountVoucher(models.Model): return move @api.multi - def _convert_amount(self, amount): + def _convert(self, amount): ''' This function convert the amount given in company currency. It takes either the rate in the voucher (if the payment_rate_currency_id is relevant) either the rate encoded in the system. @@ -232,7 +232,7 @@ class AccountVoucher(models.Model): :rtype: float ''' for voucher in self: - return voucher.currency_id.compute(amount, voucher.company_id.currency_id) + return voucher.currency_id._convert(amount, voucher.company_id.currency_id, voucher.company_id, voucher.account_date) @api.multi def voucher_pay_now_payment_create(self): @@ -279,9 +279,7 @@ class AccountVoucher(models.Model): if not line.price_subtotal: continue # convert the amount set on the voucher line into the currency of the voucher's company - # this calls res_curreny.compute() with the right context, - # so that it will take either the rate on the voucher if it is relevant or will use the default behaviour - amount = self._convert_amount(line.price_unit*line.quantity) + amount = self._convert(line.price_unit*line.quantity) move_line = { 'journal_id': self.journal_id.id, 'name': line.name or '/', @@ -314,7 +312,7 @@ class AccountVoucher(models.Model): company_currency = voucher.journal_id.company_id.currency_id.id current_currency = voucher.currency_id.id or company_currency # we select the context to use accordingly if it's a multicurrency case or not - # But for the operations made by _convert_amount, we always need to give the date in the context + # But for the operations made by _convert, we always need to give the date in the context ctx = local_context.copy() ctx['date'] = voucher.account_date ctx['check_move_validity'] = False @@ -325,9 +323,9 @@ class AccountVoucher(models.Model): move_line = self.env['account.move.line'].with_context(ctx).create(voucher.with_context(ctx).first_move_line_get(move.id, company_currency, current_currency)) line_total = move_line.debit - move_line.credit if voucher.voucher_type == 'sale': - line_total = line_total - voucher._convert_amount(voucher.tax_amount) + line_total = line_total - voucher._convert(voucher.tax_amount) elif voucher.voucher_type == 'purchase': - line_total = line_total + voucher._convert_amount(voucher.tax_amount) + line_total = line_total + voucher._convert(voucher.tax_amount) # Create one move line per voucher line where amount is not 0.0 line_total = voucher.with_context(ctx).voucher_move_line_create(line_total, move.id, company_currency, current_currency) diff --git a/addons/analytic/models/analytic_account.py b/addons/analytic/models/analytic_account.py index d2e2a609e88..0deaa0049a5 100644 --- a/addons/analytic/models/analytic_account.py +++ b/addons/analytic/models/analytic_account.py @@ -98,7 +98,8 @@ class AccountAnalyticAccount(models.Model): data_credit = {account_id: 0.0 for account_id in account_ids} for account_amount in account_amounts: currency_id = account_amount['currency_id'][0] - amount = res_currency_obj.browse(currency_id).compute(account_amount['amount'], user_currency) + amount = res_currency_obj.browse(currency_id)._convert( + account_amount['amount'], user_currency, self.env.user.company_id, fields.Date.today()) if amount < 0.0: data_debit[account_amount['account_id'][0]] += amount else: diff --git a/addons/delivery/models/delivery_grid.py b/addons/delivery/models/delivery_grid.py index dddc300dfe2..1f2ccf0e10a 100644 --- a/addons/delivery/models/delivery_grid.py +++ b/addons/delivery/models/delivery_grid.py @@ -58,7 +58,8 @@ class ProviderGrid(models.Model): 'error_message': e.name, 'warning_message': False} if order.company_id.currency_id.id != order.pricelist_id.currency_id.id: - price_unit = order.company_id.currency_id.with_context(date=order.date_order).compute(price_unit, order.pricelist_id.currency_id) + price_unit = order.company_id.currency_id._convert( + price_unit, order.pricelist_id.currency_id, order.company_id, order.date_order or fields.Date.today()) return {'success': True, 'price': price_unit, @@ -82,7 +83,8 @@ class ProviderGrid(models.Model): quantity += qty total = (order.amount_total or 0.0) - total_delivery - total = order.currency_id.with_context(date=order.date_order).compute(total, order.company_id.currency_id) + total = order.currency_id._convert( + total, order.company_id.currency_id, order.company_id, order.date_order or fields.Date.today()) return self._get_price_from_picking(total, weight, volume, quantity) diff --git a/addons/event_sale/models/sale_order.py b/addons/event_sale/models/sale_order.py index 7a2dcd8a745..275199f53c4 100644 --- a/addons/event_sale/models/sale_order.py +++ b/addons/event_sale/models/sale_order.py @@ -62,4 +62,7 @@ class SaleOrderLine(models.Model): @api.onchange('event_ticket_id') def _onchange_event_ticket_id(self): - self.price_unit = (self.event_id.company_id or self.env.user.company_id).currency_id.compute(self.event_ticket_id.price, self.order_id.currency_id) + company = self.event_id.company_id or self.env.user.company_id + currency = company.currency_id + self.price_unit = currency._convert( + self.event_ticket_id.price, self.order_id.currency_id, self.order_id.company_id, self.order_id.date_order or fields.Date.today()) diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index ea97525caf4..dad6b3261b5 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -90,7 +90,9 @@ class HrExpense(models.Model): amount = 0 if expense.company_currency_id: date_expense = expense.date - amount = expense.currency_id.with_context(date=date_expense, company_id=expense.company_id.id).compute(expense.total_amount, expense.company_currency_id) + amount = expense.currency_id._convert( + expense.total_amount, expense.company_currency_id, + expense.company_id, date_expense or fields.Date.today()) expense.total_amount_company = amount @api.multi @@ -285,7 +287,8 @@ class HrExpense(models.Model): # taxes move lines for tax in taxes['taxes']: - price = expense.currency_id.with_context(date=account_date).compute(tax['amount'], company_currency) + price = expense.currency_id._convert( + tax['amount'], company_currency, expense.company_id, account_date) amount_currency = price if different_currency else False move_line_tax_values = { 'name': tax['name'], diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index d1d41829e7b..1c5db143e8a 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -114,7 +114,8 @@ class AccountAnalyticLine(models.Model): for timesheet in sudo_self: cost = timesheet.employee_id.timesheet_cost or 0.0 amount = -timesheet.unit_amount * cost - amount_converted = timesheet.employee_id.currency_id.compute(amount, timesheet.account_id.currency_id) + amount_converted = timesheet.employee_id.currency_id._convert( + amount, timesheet.account_id.currency_id, self.env.user.company_id, timesheet.date) result[timesheet.id].update({ 'amount': amount_converted, }) diff --git a/addons/point_of_sale/models/pos_order.py b/addons/point_of_sale/models/pos_order.py index 576793273ce..e7ed37c5ce6 100644 --- a/addons/point_of_sale/models/pos_order.py +++ b/addons/point_of_sale/models/pos_order.py @@ -1056,7 +1056,8 @@ class ReportSaleDetails(models.AbstractModel): taxes = {} for order in orders: if user_currency != order.pricelist_id.currency_id: - total += order.pricelist_id.currency_id.compute(order.amount_total, user_currency) + total += order.pricelist_id.currency_id._convert( + order.amount_total, user_currency, order.company_id, order.date_order or fields.Date.today()) else: total += order.amount_total currency = order.session_id.currency_id diff --git a/addons/product/models/product.py b/addons/product/models/product.py index e921f616a9b..db99180134c 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -523,7 +523,8 @@ class ProductProduct(models.Model): # Convert from current user company currency to asked one # This is right cause a field cannot be in more than one currency if currency: - prices[product.id] = product.currency_id.compute(prices[product.id], currency) + prices[product.id] = product.currency_id._convert( + prices[product.id], currency, product.company_id, fields.Date.today()) return prices diff --git a/addons/product/models/product_pricelist.py b/addons/product/models/product_pricelist.py index f81c6f9ee15..6f9aa137c18 100644 --- a/addons/product/models/product_pricelist.py +++ b/addons/product/models/product_pricelist.py @@ -206,7 +206,7 @@ class Pricelist(models.Model): if rule.base == 'pricelist' and rule.base_pricelist_id: price_tmp = rule.base_pricelist_id._compute_price_rule([(product, qty, partner)])[product.id][0] # TDE: 0 = price, 1 = rule - price = rule.base_pricelist_id.currency_id.compute(price_tmp, self.currency_id, round=False) + price = rule.base_pricelist_id.currency_id._convert(price_tmp, self.currency_id, self.env.user.company_id, date, round=False) else: # if base option is public price take sale price else cost price of product # price_compute returns the price in the context UoM, i.e. qty_uom_id @@ -241,7 +241,7 @@ class Pricelist(models.Model): break # Final price conversion into pricelist currency if suitable_rule and suitable_rule.compute_price != 'fixed' and suitable_rule.base != 'pricelist': - price = product.currency_id.compute(price, self.currency_id, round=False) + price = product.currency_id._convert(price, self.currency_id, self.env.user.company_id, date, round=False) results[product.id] = (price, suitable_rule and suitable_rule.id or False) diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 2df63cf1e66..0ae077ac423 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -397,6 +397,9 @@ class ProductTemplate(models.Model): # Thus, in order to compute the sale price from the cost for users not in this group # We fetch the standard price as the superuser templates = self.with_context(force_company=company and company.id or self._context.get('force_company', self.env.user.company_id.id)).sudo() + if not company: + company = self._context.get('force_company', self.env.user.company_id.id) + date = self.env.context.get('date') or fields.Date.today() prices = dict.fromkeys(self.ids, 0.0) for template in templates: @@ -408,7 +411,7 @@ class ProductTemplate(models.Model): # Convert from current user company currency to asked one # This is right cause a field cannot be in more than one currency if currency: - prices[template.id] = template.currency_id.compute(prices[template.id], currency) + prices[template.id] = template.currency_id._convert(prices[template.id], currency, company, date) return prices diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py index f2636e003ff..33ef682f3d9 100644 --- a/addons/purchase/models/account_invoice.py +++ b/addons/purchase/models/account_invoice.py @@ -45,6 +45,7 @@ class AccountInvoice(models.Model): taxes = line.taxes_id invoice_line_tax_ids = line.order_id.fiscal_position_id.map_tax(taxes, line.product_id, line.order_id.partner_id) invoice_line = self.env['account.invoice.line'] + date = self.date_invoice data = { 'purchase_line_id': line.id, 'name': line.order_id.name+': '+line.name, @@ -52,7 +53,8 @@ class AccountInvoice(models.Model): 'uom_id': line.product_uom.id, 'product_id': line.product_id.id, 'account_id': invoice_line.with_context({'journal_id': self.journal_id.id, 'type': 'in_invoice'})._default_account(), - 'price_unit': line.order_id.currency_id.with_context(date=self.date_invoice).compute(line.price_unit, self.currency_id, round=False), + 'price_unit': line.order_id.currency_id._convert( + line.price_unit, self.currency_id, line.company_id, date or fields.Date.today(), round=False), 'quantity': qty, 'discount': 0.0, 'account_analytic_id': line.account_analytic_id.id, @@ -99,7 +101,10 @@ class AccountInvoice(models.Model): def _onchange_currency_id(self): if self.currency_id: for line in self.invoice_line_ids.filtered(lambda r: r.purchase_line_id): - line.price_unit = line.purchase_id.currency_id.with_context(date=self.date_invoice).compute(line.purchase_line_id.price_unit, self.currency_id, round=False) + date = self.date_invoice or fields.Date.today() + company = self.company_id + line.price_unit = line.purchase_id.currency_id._convert( + line.purchase_line_id.price_unit, self.currency_id, company, date, round=False) @api.onchange('invoice_line_ids') def _onchange_origin(self): @@ -189,8 +194,8 @@ class AccountInvoice(models.Model): interim_account_price = valuation_price_unit * line_quantity if inv.currency_id.id != company_currency.id: # We express everyhting in the invoice currency - valuation_price_unit = company_currency.with_context(date=inv.date_invoice).compute(valuation_price_unit, inv.currency_id, round=False) - interim_account_price = company_currency.with_context(date=inv.date_invoice).compute(interim_account_price, inv.currency_id, round=False) + valuation_price_unit = company_currency._convert(valuation_price_unit, inv.currency_id, inv.company_id, inv.date_invoice or fields.Date.today(), round=False) + interim_account_price = company_currency._convert(interim_account_price, inv.currency_id, inv.company_id, inv.date_invoice or fields.Date.today(), round=False) invoice_cur_prec = inv.currency_id.decimal_places diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 40e0ee89c34..41368ff00ca 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -372,7 +372,8 @@ class PurchaseOrder(models.Model): # Deal with double validation process if order.company_id.po_double_validation == 'one_step'\ or (order.company_id.po_double_validation == 'two_step'\ - and order.amount_total < self.env.user.company_id.currency_id.compute(order.company_id.po_double_validation_amount, order.currency_id))\ + and order.amount_total < self.env.user.company_id.currency_id._convert( + order.company_id.po_double_validation_amount, order.currency_id, order.company_id, order.date_order or fields.Date.today()))\ or order.user_has_groups('purchase.group_purchase_manager'): order.button_approve() else: @@ -475,7 +476,7 @@ class PurchaseOrder(models.Model): 'sequence': max(line.product_id.seller_ids.mapped('sequence')) + 1 if line.product_id.seller_ids else 1, 'product_uom': line.product_uom.id, 'min_qty': 0.0, - 'price': self.currency_id.compute(line.price_unit, currency), + 'price': self.currency_id._convert(line.price_unit, currency, line.company_id, line.date_order or fields.Date.today()), 'currency_id': currency.id, 'delay': 0, } @@ -732,7 +733,8 @@ class PurchaseOrderLine(models.Model): if line.product_uom.id != line.product_id.uom_id.id: price_unit *= line.product_uom.factor / line.product_id.uom_id.factor if order.currency_id != order.company_id.currency_id: - price_unit = order.currency_id.compute(price_unit, order.company_id.currency_id, round=False) + price_unit = order.currency_id._convert( + price_unit, order.company_id.currency_id, self.company_id, self.date_order or fields.Date.today(), round=False) return price_unit @api.multi @@ -895,7 +897,8 @@ class PurchaseOrderLine(models.Model): price_unit = self.env['account.tax']._fix_tax_included_price_company(seller.price, self.product_id.supplier_taxes_id, self.taxes_id, self.company_id) if seller else 0.0 if price_unit and seller and self.order_id.currency_id and seller.currency_id != self.order_id.currency_id: - price_unit = seller.currency_id.compute(price_unit, self.order_id.currency_id) + price_unit = seller.currency_id._convert( + price_unit, self.order_id.currency_id, self.order_id.company_id, self.date_order or fields.Date.today()) if seller and self.product_uom and seller.product_uom != self.product_uom: price_unit = seller.product_uom._compute_price(price_unit, self.product_uom) @@ -997,7 +1000,8 @@ class ProcurementRule(models.Model): price_unit = self.env['account.tax']._fix_tax_included_price_company(seller.price, line.product_id.supplier_taxes_id, line.taxes_id, values['company_id']) if seller else 0.0 if price_unit and seller and line.order_id.currency_id and seller.currency_id != line.order_id.currency_id: - price_unit = seller.currency_id.compute(price_unit, line.order_id.currency_id) + price_unit = seller.currency_id._convert( + price_unit, line.order_id.currency_id, line.order_id.company_id, fields.Date.today()) return { 'product_qty': line.product_qty + procurement_uom_po_qty, @@ -1022,7 +1026,8 @@ class ProcurementRule(models.Model): price_unit = self.env['account.tax']._fix_tax_included_price_company(seller.price, product_id.supplier_taxes_id, taxes_id, values['company_id']) if seller else 0.0 if price_unit and seller and po.currency_id and seller.currency_id != po.currency_id: - price_unit = seller.currency_id.compute(price_unit, po.currency_id) + price_unit = seller.currency_id._convert( + price_unit, po.currency_id, po.company_id, po.date_order or fields.Date.today()) product_lang = product_id.with_context({ 'lang': partner.lang, diff --git a/addons/purchase/models/stock.py b/addons/purchase/models/stock.py index 6d06186b87d..1882170dab0 100644 --- a/addons/purchase/models/stock.py +++ b/addons/purchase/models/stock.py @@ -46,7 +46,8 @@ class StockMove(models.Model): if line.product_uom.id != line.product_id.uom_id.id: price_unit *= line.product_uom.factor / line.product_id.uom_id.factor if order.currency_id != order.company_id.currency_id: - price_unit = order.currency_id.compute(price_unit, order.company_id.currency_id, round=False) + price_unit = order.currency_id._convert( + price_unit, order.company_id.currency_id, order.company_id, order.date_order or fields.Date.today(), round=False) return price_unit return super(StockMove, self)._get_price_unit() diff --git a/addons/purchase/tests/test_purchase_order.py b/addons/purchase/tests/test_purchase_order.py index 94c9db5fba7..674fb087f0e 100644 --- a/addons/purchase/tests/test_purchase_order.py +++ b/addons/purchase/tests/test_purchase_order.py @@ -71,7 +71,7 @@ class TestPurchaseOrder(AccountingTestCase): seller = self.product_id_2._select_seller(partner_id=self.partner_id, quantity=2.0, date=self.po.date_planned, uom_id=self.product_id_2.uom_po_id) price_unit = seller.price if seller else 0.0 if price_unit and seller and self.po.currency_id and seller.currency_id != self.po.currency_id: - price_unit = seller.currency_id.compute(price_unit, self.po.currency_id) + price_unit = seller.currency_id._convert(price_unit, self.po.currency_id, self.po.company_id, self.po.date_order) self.assertEqual(price_unit, 250.0, 'Purchase: the price of the product for the supplier should be 250.0.') self.assertEqual(self.po.picking_count, 1, 'Purchase: one picking should be created"') diff --git a/addons/purchase/tests/test_stockvaluation.py b/addons/purchase/tests/test_stockvaluation.py index cb5e4b2040e..ec64b079cf7 100644 --- a/addons/purchase/tests/test_stockvaluation.py +++ b/addons/purchase/tests/test_stockvaluation.py @@ -4,6 +4,7 @@ import time from datetime import datetime +from odoo import fields from odoo.tests.common import TransactionCase from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT @@ -167,7 +168,9 @@ class TestStockValuation(TransactionCase): move1 = picking1.move_lines[0] # convert the price unit in the company currency - price_unit_usd = po1.currency_id.compute(po1.order_line.price_unit, po1.company_id.currency_id, round=True) + price_unit_usd = po1.currency_id._convert( + po1.order_line.price_unit, po1.company_id.currency_id, + self.env.user.company_id, fields.Date.today(), round=False) # the unit price of the move is the unit price of the purchase order line converted in # the company's currency @@ -181,7 +184,9 @@ class TestStockValuation(TransactionCase): 'company_id': po1.company_id.id, }) eur_currency._compute_current_rate() - price_unit_usd_new_rate = po1.currency_id.compute(po1.order_line.price_unit, po1.company_id.currency_id, round=True) + price_unit_usd_new_rate = po1.currency_id._convert( + po1.order_line.price_unit, po1.company_id.currency_id, + self.env.user.company_id, fields.Date.today(), round=False) # the new price_unit is lower than th initial because of the rate's change self.assertLess(price_unit_usd_new_rate, price_unit_usd) diff --git a/addons/sale/models/analytic.py b/addons/sale/models/analytic.py index c836f337c9e..c11f7a9de19 100644 --- a/addons/sale/models/analytic.py +++ b/addons/sale/models/analytic.py @@ -55,7 +55,8 @@ class AccountAnalyticLine(models.Model): price_unit = abs(self.amount / self.unit_amount) currency_id = self.company_id.currency_id if currency_id and currency_id != order.currency_id: - price_unit = currency_id.compute(price_unit, order.currency_id) + price_unit = currency_id._convert( + price_unit, order.currency_id, order.company_id, order.date_order or fields.Date.today()) return price_unit @api.multi diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index 0461dbadfdd..c2299c9d7eb 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -1092,7 +1092,10 @@ class SaleOrderLine(models.Model): context_partner = dict(self.env.context, partner_id=self.order_id.partner_id.id, date=self.order_id.date_order) base_price, currency_id = self.with_context(context_partner)._get_real_price_currency(self.product_id, rule_id, self.product_uom_qty, self.product_uom, self.order_id.pricelist_id.id) if currency_id != self.order_id.pricelist_id.currency_id.id: - base_price = self.env['res.currency'].browse(currency_id).with_context(context_partner).compute(base_price, self.order_id.pricelist_id.currency_id) + currency = self.env['res.currency'].browse(currency_id) + base_price = currency._convert( + base_price, self.order_id.pricelist_id.currency_id, + self.order_id.company_id, self.order_id.date_order or fields.Date.today()) # negative discounts (= surcharge) are included in the display price return max(base_price, final_price) @@ -1223,7 +1226,7 @@ class SaleOrderLine(models.Model): if currency_id.id == product_currency.id: cur_factor = 1.0 else: - cur_factor = currency_id._get_conversion_rate(product_currency, currency_id) + cur_factor = currency_id._get_conversion_rate(product_currency, currency_id, self.company_id, self.order_id.date_order) product_uom = self.env.context.get('uom') or product.uom_id.id if uom and uom.id != product_uom: @@ -1232,7 +1235,7 @@ class SaleOrderLine(models.Model): else: uom_factor = 1.0 - return product[field_name] * uom_factor * cur_factor, currency_id.id + return product[field_name] * uom_factor * cur_factor, currency_id def _get_protected_fields(self): return [ @@ -1258,7 +1261,10 @@ class SaleOrderLine(models.Model): if new_list_price != 0: if self.order_id.pricelist_id.currency_id.id != currency_id: # we need new_list_price in the same currency as price, which is in the SO's pricelist's currency - new_list_price = self.env['res.currency'].browse(currency_id).with_context(context_partner).compute(new_list_price, self.order_id.pricelist_id.currency_id) + currency = self.env['res.currency'].browse(currency_id) + new_list_price = currency._convert( + new_list_price, self.order_id.pricelist_id.currency_id, + self.order_id.company_id, self.order_id.date_order or fields.Date.today()) discount = (new_list_price - price) / new_list_price * 100 if discount > 0: self.discount = discount diff --git a/addons/sale_crm/models/crm_lead.py b/addons/sale_crm/models/crm_lead.py index b4eeb43f565..035d33dfbfd 100644 --- a/addons/sale_crm/models/crm_lead.py +++ b/addons/sale_crm/models/crm_lead.py @@ -23,7 +23,8 @@ class CrmLead(models.Model): if order.state in ('draft', 'sent', 'sale'): nbr += 1 if order.state not in ('draft', 'sent', 'cancel'): - total += order.currency_id.compute(order.amount_untaxed, company_currency) + total += order.currency_id._convert( + order.amount_untaxed, company_currency, order.company_id, order.date_order or fields.Date.today()) lead.sale_amount_total = total lead.sale_number = nbr diff --git a/addons/sale_margin/models/sale_order.py b/addons/sale_margin/models/sale_order.py index 57988de2ab9..bd1687a8e2a 100644 --- a/addons/sale_margin/models/sale_order.py +++ b/addons/sale_margin/models/sale_order.py @@ -17,9 +17,8 @@ class SaleOrderLine(models.Model): purchase_price = product_id.standard_price if product_uom_id != product_id.uom_id: purchase_price = product_id.uom_id._compute_price(purchase_price, product_uom_id) - ctx = self.env.context.copy() - ctx['date'] = order_id.date_order - price = frm_cur.with_context(ctx).compute(purchase_price, to_cur, round=False) + price = frm_cur._convert( + purchase_price, to_cur, order_id.company_id, order_id.date_order or fields.Date.today(), round=False) return price @api.model @@ -29,9 +28,8 @@ class SaleOrderLine(models.Model): purchase_price = product.standard_price if product_uom != product.uom_id: purchase_price = product.uom_id._compute_price(purchase_price, product_uom) - ctx = self.env.context.copy() - ctx['date'] = date - price = frm_cur.with_context(ctx).compute(purchase_price, to_cur, round=False) + price = frm_cur._convert( + purchase_price, to_cur, self.order_id.company_id, date or fields.Date.today(), round=False) return {'purchase_price': price} @api.onchange('product_id', 'product_uom') @@ -61,9 +59,13 @@ class SaleOrderLine(models.Model): currency = line.order_id.pricelist_id.currency_id price = line.purchase_price if not price: - from_cur = line.env.user.company_id.currency_id.with_context(date=line.order_id.date_order) - price = from_cur.compute(line.product_id.standard_price, currency, round=False) - + from_cur = line.env.user.company_id.currency_id + price = from_cur._convert( + line.product_id.standard_price, + currency, + line.order_id.company_id, + line.order_id.date_order or fields.Date.today(), round=False) + line.margin = currency.round(line.price_subtotal - (price * line.product_uom_qty)) diff --git a/addons/sale_payment/models/crm_team.py b/addons/sale_payment/models/crm_team.py index 411fa6a21ab..5ef72ff3ef1 100644 --- a/addons/sale_payment/models/crm_team.py +++ b/addons/sale_payment/models/crm_team.py @@ -30,7 +30,15 @@ class CrmTeam(models.Model): datum_currency = self.env['res.currency'].browse(datum['currency_id'][0]) if datum['state'] == 'authorized': team.authorized_payment_transactions_count += datum['__count'] - team.authorized_payment_transactions_amount += datum_currency.compute(datum['amount'], self.env.user.company_id.currency_id) + team.authorized_payment_transactions_amount += datum_currency._convert( + datum['amount'], + self.env.user.company_id.currency_id, + self.env.user.company_id, + fields.Date.today()) elif datum['state'] == 'pending': team.pending_payment_transactions_count += datum['__count'] - team.pending_payment_transactions_amount += datum_currency.compute(datum['amount'], self.env.user.company_id.currency_id) + team.pending_payment_transactions_amount += datum_currency._convert( + datum['amount'], + self.env.user.company_id.currency_id, + self.env.user.company_id, + fields.Date.today()) diff --git a/addons/sale_timesheet/models/account.py b/addons/sale_timesheet/models/account.py index 87ec0a10f2c..91bdab9921c 100644 --- a/addons/sale_timesheet/models/account.py +++ b/addons/sale_timesheet/models/account.py @@ -101,7 +101,8 @@ class AccountAnalyticLine(models.Model): analytic_account = timesheet.account_id # convert the unit of mesure into hours sale_price_hour = so_line.product_uom._compute_price(so_line.price_unit, timesheet_uom) - sale_price = so_line.currency_id.compute(sale_price_hour, analytic_account.currency_id) # amount from SO should be convert into analytic account currency + sale_price = so_line.currency_id._convert( + sale_price_hour, analytic_account.currency_id, so_line.company_id, fields.Date.today()) # amount from SO should be convert into analytic account currency # calculate the revenue on the timesheet if so_line.product_id.invoice_policy == 'delivery': @@ -121,7 +122,8 @@ class AccountAnalyticLine(models.Model): total_revenue_invoiced = sum(analytic_lines.mapped('timesheet_revenue')) # compute (new) revenue of current timesheet line values['timesheet_revenue'] = min( - analytic_account.currency_id.round(unit_amount * so_line.currency_id.compute(so_line.price_unit, analytic_account.currency_id) * (1-so_line.discount)), + analytic_account.currency_id.round(unit_amount * so_line.currency_id._convert( + so_line.price_unit, analytic_account.currency_id, so_line.company_id, fields.Date.today()) * (1-so_line.discount)), total_revenue_so - total_revenue_invoiced ) values['timesheet_invoice_type'] = 'billable_fixed' diff --git a/addons/sale_timesheet/models/account_invoice.py b/addons/sale_timesheet/models/account_invoice.py index ddc3fb0a3c9..45e655f252c 100644 --- a/addons/sale_timesheet/models/account_invoice.py +++ b/addons/sale_timesheet/models/account_invoice.py @@ -70,11 +70,19 @@ class AccountInvoice(models.Model): total_revenue_per_currency = dict.fromkeys(uninvoiced_timesheet_lines.mapped('currency_id').ids, 0.0) for index, timesheet_line in enumerate(uninvoiced_timesheet_lines.sorted(key=lambda ts: (ts.date, ts.id))): if index+1 != len(uninvoiced_timesheet_lines): - line_revenue = invoice_line.currency_id.compute(invoiced_price_per_hour, timesheet_line.currency_id) * timesheet_line.unit_amount + line_revenue = invoice_line.currency_id._convert( + invoiced_price_per_hour, timesheet_line.currency_id, + self.env.user.company_id, fields.Date.today()) * timesheet_line.unit_amount total_revenue_per_currency[timesheet_line.currency_id.id] += line_revenue else: # last line: add the difference to avoid rounding problem - total_revenue = sum([self.env['res.currency'].browse(currency_id).compute(amount, timesheet_line.currency_id) for currency_id, amount in total_revenue_per_currency.items()]) - line_revenue = invoice_line.currency_id.compute(invoice_line.price_subtotal, timesheet_line.currency_id) - total_revenue + total_revenue = sum([self.env['res.currency'].browse(currency_id)._convert( + amount, timesheet_line.currency_id, + self.env.user.company_id, fields.Date.today() + ) for currency_id, amount in total_revenue_per_currency.items()]) + line_revenue = invoice_line.currency_id._convert( + invoice_line.price_subtotal, timesheet_line.currency_id, + self.env.user.company_id, fields.Date.today() + ) - total_revenue timesheet_line.write({ 'timesheet_invoice_id': invoice.id, 'timesheet_revenue': timesheet_line.currency_id.round(line_revenue), @@ -93,16 +101,23 @@ class AccountInvoice(models.Model): for index, timesheet_line in enumerate(no_zero_timesheet_revenue.sorted(key=lambda ts: (ts.date, ts.id))): if index+1 != len(no_zero_timesheet_revenue): - price_subtotal_inv = invoice_line.currency_id.compute(invoice_line.price_subtotal, timesheet_line.currency_id) - price_subtotal_sol = timesheet_line.so_line.currency_id.compute(timesheet_line.so_line.price_subtotal, timesheet_line.currency_id) + price_subtotal_inv = invoice_line.currency_id._convert( + invoice_line.price_subtotal, timesheet_line.currency_id, self.env.user.company_id, fields.Date.today()) + price_subtotal_sol = timesheet_line.so_line.currency_id._convert( + timesheet_line.so_line.price_subtotal, timesheet_line.currency_id, self.env.user.company_id, fields.Date.today()) if not float_is_zero(price_subtotal_sol, precision_rounding=timesheet_line.currency_id.rounding): line_revenue = timesheet_line.timesheet_revenue * price_subtotal_inv / price_subtotal_sol total_revenue_per_currency[timesheet_line.currency_id.id] += line_revenue else: total_revenue_per_currency[timesheet_line.currency_id.id] += timesheet_line.timesheet_revenue else: # last line: add the difference to avoid rounding problem - last_price_subtotal_inv = invoice_line.currency_id.compute(invoice_line.price_subtotal, timesheet_line.currency_id) - total_revenue = sum([self.env['res.currency'].browse(currency_id).compute(amount, timesheet_line.currency_id) for currency_id, amount in total_revenue_per_currency.items()]) + last_price_subtotal_inv = invoice_line.currency_id._convert( + invoice_line.price_subtotal, timesheet_line.currency_id, + self.env.user.company_id, fields.Date.today()) + total_revenue = sum([self.env['res.currency'].browse(currency_id)._convert( + amount, timesheet_line.currency_id, + self.env.user.company_id, fields.Date.today() + ) for currency_id, amount in total_revenue_per_currency.items()]) line_revenue = last_price_subtotal_inv - total_revenue timesheet_line.write({ diff --git a/addons/stock_account/models/account_invoice.py b/addons/stock_account/models/account_invoice.py index 3a7be3a919d..c6d3880140e 100644 --- a/addons/stock_account/models/account_invoice.py +++ b/addons/stock_account/models/account_invoice.py @@ -100,7 +100,11 @@ class AccountInvoiceLine(models.Model): def _get_price(self, company_currency, price_unit): if self.invoice_id.currency_id.id != company_currency.id: - price = company_currency.with_context(date=self.invoice_id.date_invoice).compute(price_unit * self.quantity, self.invoice_id.currency_id) + price = company_currency._convert( + price_unit * self.quantity, + self.invoice_id.currency_id, + self.invoice_id.company_id, + self.invoice_id.date_invoice or fields.Date.today()) else: price = price_unit * self.quantity return self.invoice_id.currency_id.round(price) diff --git a/addons/website_event_sale/views/event_templates.xml b/addons/website_event_sale/views/event_templates.xml index 789ea5995b2..37f2b1f5656 100644 --- a/addons/website_event_sale/views/event_templates.xml +++ b/addons/website_event_sale/views/event_templates.xml @@ -39,7 +39,7 @@ -