diff --git a/addons/account/models/res_currency.py b/addons/account/models/res_currency.py index e1de9912d54..024faf59d3a 100644 --- a/addons/account/models/res_currency.py +++ b/addons/account/models/res_currency.py @@ -43,30 +43,28 @@ class ResCurrency(models.Model): return bool(self.env['account.move.line'].search_count(['|', ('currency_id', '=', self.id), ('company_currency_id', '=', self.id)])) @api.model - def _get_query_currency_table(self, options): + def _get_query_currency_table(self, company_ids, conversion_date): ''' Construct the currency table as a mapping company -> rate to convert the amount to the user's company currency in a multi-company/multi-currency environment. The currency_table is a small postgresql table construct with VALUES. - :param options: The report options. + :param company_ids: list of company ids + :param conversion_date: date, used to determine the currency rate between the individual companies and the user's company :return: The query representing the currency table. ''' + companies = self.env['res.company'].browse(company_ids) user_company = self.env.company - user_currency = user_company.currency_id - if options.get('multi_company', False): - companies = self.env.companies - conversion_date = options['date']['date_to'] - currency_rates = companies.mapped('currency_id')._get_rates(user_company, conversion_date) + if companies == user_company: + currency_rates = {user_company.currency_id.id: 1.0} else: - companies = user_company - currency_rates = {user_currency.id: 1.0} + currency_rates = companies.mapped('currency_id')._get_rates(user_company, conversion_date) conversion_rates = [] for company in companies: conversion_rates.extend(( company.id, currency_rates[user_company.currency_id.id] / currency_rates[company.currency_id.id], - user_currency.decimal_places, + user_company.currency_id.decimal_places, )) query = '(VALUES %s) AS currency_table(company_id, rate, precision)' % ','.join('(%s, %s, %s)' for i in companies) return self.env.cr.mogrify(query, conversion_rates).decode(self.env.cr.connection.encoding) diff --git a/addons/account/report/account_invoice_report.py b/addons/account/report/account_invoice_report.py index 19e19eb7b0d..a4f5ee9ba3a 100644 --- a/addons/account/report/account_invoice_report.py +++ b/addons/account/report/account_invoice_report.py @@ -120,7 +120,7 @@ class AccountInvoiceReport(models.Model): LEFT JOIN res_partner commercial_partner ON commercial_partner.id = move.commercial_partner_id JOIN {currency_table} ON currency_table.company_id = line.company_id '''.format( - currency_table=self.env['res.currency']._get_query_currency_table({'multi_company': True, 'date': {'date_to': fields.Date.today()}}), + currency_table=self.env['res.currency']._get_query_currency_table(self.env.companies.ids, fields.Date.today()) ) @api.model diff --git a/addons/pos_sale/report/sale_report.py b/addons/pos_sale/report/sale_report.py index 0eec84f92d1..19d4610ab6b 100644 --- a/addons/pos_sale/report/sale_report.py +++ b/addons/pos_sale/report/sale_report.py @@ -106,11 +106,7 @@ class SaleReport(models.Model): LEFT JOIN pos_config config ON config.id = session.config_id JOIN {currency_table} ON currency_table.company_id = pos.company_id """.format( - currency_table=self.env['res.currency']._get_query_currency_table( - { - 'multi_company': True, - 'date': {'date_to': fields.Date.today()} - }), + currency_table=self.env['res.currency']._get_query_currency_table(self.env.companies.ids, fields.Date.today()) ) def _where_pos(self): diff --git a/addons/purchase/report/purchase_report.py b/addons/purchase/report/purchase_report.py index 7aac1f26d82..039f165e43e 100644 --- a/addons/purchase/report/purchase_report.py +++ b/addons/purchase/report/purchase_report.py @@ -106,7 +106,7 @@ class PurchaseReport(models.Model): left join uom_uom product_uom on (product_uom.id=t.uom_id) left join {currency_table} ON currency_table.company_id = po.company_id """.format( - currency_table=self.env['res.currency']._get_query_currency_table({'multi_company': True, 'date': {'date_to': fields.Date.today()}}), + currency_table=self.env['res.currency']._get_query_currency_table(self.env.companies.ids, fields.Date.today()) ) return from_str diff --git a/addons/sale/report/sale_report.py b/addons/sale/report/sale_report.py index ab8936f75d1..9f54f2d66e5 100644 --- a/addons/sale/report/sale_report.py +++ b/addons/sale/report/sale_report.py @@ -177,11 +177,7 @@ class SaleReport(models.Model): LEFT JOIN uom_uom u2 ON u2.id=t.uom_id JOIN {currency_table} ON currency_table.company_id = s.company_id """.format( - currency_table=self.env['res.currency']._get_query_currency_table( - { - 'multi_company': True, - 'date': {'date_to': fields.Date.today()} - }), + currency_table=self.env['res.currency']._get_query_currency_table(self.env.companies.ids, fields.Date.today()) ) def _where_sale(self):