diff --git a/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py b/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py index 8d1bcd7577..80f4b89df1 100644 --- a/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py +++ b/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py @@ -75,7 +75,8 @@ def action_process(self): for line in self.line_ids.filtered("template_id"): try: records += line.action_process() - except Exception: + except Exception as e: + logger.info(e) if not self.env.context.get("skip_template_not_found_error"): raise action = { @@ -190,7 +191,8 @@ def _process_set_value_form(self, _form, field_name, value): else: try: setattr(_form, field_name, value) - except Exception: + except Exception as e: + logger.info(e) self._add_log_error_text(field_name, value) def _process_form(self): @@ -207,14 +209,18 @@ def _process_form(self): # appropriate (fiscal position). extra_vals = {} for key in list(ctx.keys()): - if key.startswith("default_"): + # It is important to skip this field, when using fetchmail it + # is defined as default_ but it does not exist and there + # would be an error preventing the record from being created. + if key.startswith("default_") and key != "default_fetchmail_server_id": field_name = key.replace("default_", "") field_value = ctx[key] if model._fields[field_name].type == "many2one": field_value = model[field_name].browse(field_value) try: setattr(model_form, field_name, field_value) - except Exception: + except Exception as e: + logger.info(e) extra_vals[field_name] = ctx[key] # Set the values of the header in Form header_values = template._get_field_header_values(text) @@ -237,7 +243,8 @@ def _process_form(self): child_field_value = child_fixed_values[field_name] try: setattr(line_form, field_name, child_field_value) - except Exception: + except Exception as e: + logger.info(e) self._add_log_error_text(field_name, child_field_value) # set the values of any line for field_name in list(line.keys()): diff --git a/base_import_pdf_by_template_account/models/__init__.py b/base_import_pdf_by_template_account/models/__init__.py index 9c0a421385..22c08d4985 100644 --- a/base_import_pdf_by_template_account/models/__init__.py +++ b/base_import_pdf_by_template_account/models/__init__.py @@ -1 +1,2 @@ from . import account_move +from . import ir_attachment diff --git a/base_import_pdf_by_template_account/models/ir_attachment.py b/base_import_pdf_by_template_account/models/ir_attachment.py new file mode 100644 index 0000000000..6662f550b0 --- /dev/null +++ b/base_import_pdf_by_template_account/models/ir_attachment.py @@ -0,0 +1,30 @@ +# Copyright 2025 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import models + + +class IrAttachment(models.Model): + _inherit = "ir.attachment" + + # TODO: Remove in v19 because _extend_with_attachments() method is properly done + # in account.document.import.mixin + def _unwrap_edi_attachments(self): + to_process = super()._unwrap_edi_attachments() + if len(to_process) > 1 and any(a["type"] == "pdf" for a in to_process): + attachment = to_process[0]["attachment"] + if attachment.res_model and attachment.res_id: + record = self.env[attachment.res_model].browse(attachment.res_id) + template_model = self.env["base.import.pdf.template"] + if "company_id" in record._fields: + template_model = template_model.with_company(record.company_id.id) + total_templates = template_model.search_count( + [("model", "=", record._name)] + ) + if total_templates > 0: + # Define a sort_weight=1 to have a higher priority in the + # _extend_with_attachments() method when doing the decoder + for to_process_item in to_process: + if to_process_item["type"] == "pdf": + to_process_item["sort_weight"] = 1 + to_process.sort(key=lambda x: x["sort_weight"]) + return to_process