From 0e601b8b0a9f03e15352640abce64a7f82ccddd8 Mon Sep 17 00:00:00 2001 From: "Nicolas (vin)" Date: Fri, 18 Feb 2022 15:53:48 +0000 Subject: [PATCH] [FIX] account_edi_facturx: fix upload with inactive currency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Retroactive fix for an issue in 15.0+ In 15.0+, importing an invoice with an inactive currency will raise a traceback. Before this it was handled by not setting the correct currency on the invoice if not active. With this change, we'll raise an error instead of creating an invoice with the wrong currency. closes odoo/odoo#85678 X-original-commit: 14c61c2563efeed1ca14294da32e2eb6d1855c5b Signed-off-by: Quentin De Paoli Signed-off-by: William André (wan) --- addons/account_edi/i18n/account_edi.pot | 14 +++++++++++++ .../account_edi/models/account_edi_format.py | 21 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/addons/account_edi/i18n/account_edi.pot b/addons/account_edi/i18n/account_edi.pot index 50b339d330b..b376c6ab2aa 100644 --- a/addons/account_edi/i18n/account_edi.pot +++ b/addons/account_edi/i18n/account_edi.pot @@ -112,6 +112,12 @@ msgstr "" msgid "Display Name" msgstr "" +#. module: account_edi +#: code:addons/account_edi/models/account_edi_format.py:0 +#, python-format +msgid "Display the currency" +msgstr "" + #. module: account_edi #: model_terms:ir.ui.view,arch_db:account_edi.view_move_form_inherit #: model_terms:ir.ui.view,arch_db:account_edi.view_payment_form_inherit @@ -377,6 +383,14 @@ msgstr "" msgid "The aggregated state of all the EDIs with web-service of this move" msgstr "" +#. module: account_edi +#: code:addons/account_edi/models/account_edi_format.py:0 +#, python-format +msgid "" +"The currency (%s) of the document you are uploading is not active in this database.\n" +"Please activate it and update the currency rate if needed before trying again to import." +msgstr "" + #. module: account_edi #: model:ir.model.fields,help:account_edi.field_account_edi_document__attachment_id msgid "" diff --git a/addons/account_edi/models/account_edi_format.py b/addons/account_edi/models/account_edi_format.py index 2271410e637..bc60c24a4df 100644 --- a/addons/account_edi/models/account_edi_format.py +++ b/addons/account_edi/models/account_edi_format.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import models, fields, api +from odoo import models, fields, api, _ from odoo.tools.pdf import OdooPdfFileReader from odoo.osv import expression from odoo.tools import html_escape +from odoo.exceptions import RedirectWarning from lxml import etree import base64 @@ -410,6 +411,8 @@ class AccountEdiFormat(models.Model): file_data['pdf_reader'].stream.close() else: res = edi_format._create_invoice_from_binary(file_data['filename'], file_data['content'], file_data['extension']) + except RedirectWarning as rw: + raise rw except Exception as e: _logger.exception("Error importing attachment \"%s\" as invoice with format \"%s\"", file_data['filename'], edi_format.name, str(e)) if res: @@ -584,7 +587,21 @@ class AccountEdiFormat(models.Model): :param code: The code of the currency. :returns: A currency or an empty recordset if not found. ''' - return self.env['res.currency'].search([('name', '=', code.upper())], limit=1) + currency = self.env['res.currency'].with_context(active_test=False).search([('name', '=', code.upper())], limit=1) + if currency and not currency.active: + error_msg = _('The currency (%s) of the document you are uploading is not active in this database.\n' + 'Please activate it and update the currency rate if needed before trying again to import.', + currency.name) + error_action = { + 'view_mode': 'form', + 'res_model': 'res.currency', + 'type': 'ir.actions.act_window', + 'target': 'new', + 'res_id': currency.id, + 'views': [[False, 'form']] + } + raise RedirectWarning(error_msg, error_action, _('Display the currency')) + return currency #################################################### # Other helpers