From 79b5e779b1263512a9edac8c942206fef6f3aa0d Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Tue, 9 May 2023 09:32:19 +0000 Subject: [PATCH] [FIX] project_account, project_purchase, sale_project: default currency In the multi-company context, it is possible to create an analytic account without a defined company. The result is that the analytic account will not have a currency. A traceback will be triggered when calculating the profitability of a project (for example by changing its status when the 'sale_timesheet' module is installed). Solution: Use the currency of the project. Introduced by: 68b4ea69c173dd5894bcb42b99e6818f241f9734 opw-3292417 closes odoo/odoo#121143 X-original-commit: 90512da68f776459a8a65bed285f5a178f9dd77d Signed-off-by: Xavier Bol (xbo) Signed-off-by: Lefebvre Thomas (thle) --- addons/project_account/models/project_project.py | 8 ++++---- addons/project_purchase/models/project_project.py | 8 ++++---- addons/sale_project/models/project.py | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/addons/project_account/models/project_project.py b/addons/project_account/models/project_project.py index fee4ab4aead..9b155b93183 100644 --- a/addons/project_account/models/project_project.py +++ b/addons/project_account/models/project_project.py @@ -31,14 +31,14 @@ class Project(models.Model): self._cr.execute(query_string, query_param) bills_move_line_read = self._cr.dictfetchall() if bills_move_line_read: - # Get conversion rate from currencies to currency of analytic account - currency_ids = {bml['currency_id'] for bml in bills_move_line_read + [{'currency_id': self.analytic_account_id.currency_id.id}]} + # Get conversion rate from currencies to currency of the project + currency_ids = {bml['currency_id'] for bml in bills_move_line_read + [{'currency_id': self.currency_id.id}]} rates = self.env['res.currency'].browse(list(currency_ids))._get_rates(self.company_id, date.today()) - conversion_rates = {cid: rates[self.analytic_account_id.currency_id.id] / rate_from for cid, rate_from in rates.items()} + conversion_rates = {cid: rates[self.currency_id.id] / rate_from for cid, rate_from in rates.items()} amount_invoiced = amount_to_invoice = 0.0 move_ids = set() for moves_read in bills_move_line_read: - price_subtotal = self.analytic_account_id.currency_id.round(moves_read['price_subtotal'] * conversion_rates[moves_read['currency_id']]) + price_subtotal = self.currency_id.round(moves_read['price_subtotal'] * conversion_rates[moves_read['currency_id']]) analytic_contribution = moves_read['analytic_distribution'][str(self.analytic_account_id.id)] / 100. move_ids.add(moves_read['move_id']) if moves_read['parent_state'] == 'draft': diff --git a/addons/project_purchase/models/project_project.py b/addons/project_purchase/models/project_project.py index 929de3fa97e..6120a9d01c7 100644 --- a/addons/project_purchase/models/project_project.py +++ b/addons/project_purchase/models/project_project.py @@ -139,16 +139,16 @@ class Project(models.Model): with_action = with_action and self.user_has_groups('purchase.group_purchase_user, account.group_account_invoice, account.group_account_readonly') if purchase_order_line_read: - # Get conversion rate from currencies to currency of analytic account - currency_ids = {pol['currency_id'] for pol in purchase_order_line_read + [{'currency_id': self.analytic_account_id.currency_id.id}]} + # Get conversion rate from currencies to currency of the project + currency_ids = {pol['currency_id'] for pol in purchase_order_line_read + [{'currency_id': self.currency_id.id}]} rates = self.env['res.currency'].browse(list(currency_ids))._get_rates(self.company_id, date.today()) - conversion_rates = {cid: rates[self.analytic_account_id.currency_id.id] / rate_from for cid, rate_from in rates.items()} + conversion_rates = {cid: rates[self.currency_id.id] / rate_from for cid, rate_from in rates.items()} amount_invoiced = amount_to_invoice = 0.0 purchase_order_line_ids = [] for pol_read in purchase_order_line_read: purchase_order_line_invoice_line_ids.extend(pol_read['invoice_lines'].ids) - price_unit = self.analytic_account_id.currency_id.round(pol_read['price_unit'] * conversion_rates[pol_read['currency_id']]) + price_unit = self.currency_id.round(pol_read['price_unit'] * conversion_rates[pol_read['currency_id']]) analytic_contribution = pol_read['analytic_distribution'][str(self.analytic_account_id.id)] / 100. amount_invoiced -= price_unit * pol_read['qty_invoiced'] * analytic_contribution if pol_read['qty_invoiced'] > 0 else 0.0 if pol_read['qty_to_invoice'] > 0: diff --git a/addons/sale_project/models/project.py b/addons/sale_project/models/project.py index 2b98e3be450..ea011daee86 100755 --- a/addons/sale_project/models/project.py +++ b/addons/sale_project/models/project.py @@ -468,15 +468,15 @@ class Project(models.Model): invoices_move_line_read = self._cr.dictfetchall() if invoices_move_line_read: - # Get conversion rate from currencies to currency of analytic account - currency_ids = {iml['currency_id'] for iml in invoices_move_line_read + [{'currency_id': self.analytic_account_id.currency_id.id}]} + # Get conversion rate from currencies to currency of the project + currency_ids = {iml['currency_id'] for iml in invoices_move_line_read + [{'currency_id': self.currency_id.id}]} rates = self.env['res.currency'].browse(list(currency_ids))._get_rates(self.company_id, date.today()) - conversion_rates = {cid: rates[self.analytic_account_id.currency_id.id] / rate_from for cid, rate_from in rates.items()} + conversion_rates = {cid: rates[self.currency_id.id] / rate_from for cid, rate_from in rates.items()} move_ids = set() amount_invoiced = amount_to_invoice = 0.0 for moves_read in invoices_move_line_read: - price_subtotal = self.analytic_account_id.currency_id.round(moves_read['price_subtotal'] * conversion_rates[moves_read['currency_id']]) + price_subtotal = self.currency_id.round(moves_read['price_subtotal'] * conversion_rates[moves_read['currency_id']]) analytic_contribution = moves_read['analytic_distribution'][str(self.analytic_account_id.id)] / 100. move_ids.add(moves_read['move_id']) if moves_read['parent_state'] == 'draft':