summaryrefslogtreecommitdiff
path: root/addons/account/models/res_currency.py
diff options
context:
space:
mode:
authorwan <wan@odoo.com>2020-08-13 08:08:41 +0000
committerRaphael Collet <rco@odoo.com>2020-08-20 07:45:18 +0000
commitfa02f0ba2109f68d9f66bec9bc4033b3f0d0ae69 (patch)
tree84560d9d95db9d1566654bed6a72a4ff7cc0917d /addons/account/models/res_currency.py
parentf2ceef0e2f0efb2e47b5f595a80009184305a2c4 (diff)
[REF] account: use dynamic `_table_query` in invoice analysis
closes odoo/odoo#56080 Related: odoo/upgrade#1664 Related: odoo/enterprise#9914 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Diffstat (limited to 'addons/account/models/res_currency.py')
-rw-r--r--addons/account/models/res_currency.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/addons/account/models/res_currency.py b/addons/account/models/res_currency.py
index 8bf887cf521..29b4f9050ed 100644
--- a/addons/account/models/res_currency.py
+++ b/addons/account/models/res_currency.py
@@ -2,7 +2,6 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models, fields, _
-
from odoo.exceptions import UserError
@@ -36,3 +35,31 @@ class ResCurrency(models.Model):
self.ensure_one()
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):
+ ''' 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.
+ :return: The query representing the currency table.
+ '''
+
+ 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)
+ else:
+ companies = user_company
+ currency_rates = {user_currency.id: 1.0}
+
+ 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,
+ ))
+ 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)