diff --git a/l10n_it_fatturapa_import_zip/models/attachment.py b/l10n_it_fatturapa_import_zip/models/attachment.py index 10e6d8da8d2d..2182ba43da99 100644 --- a/l10n_it_fatturapa_import_zip/models/attachment.py +++ b/l10n_it_fatturapa_import_zip/models/attachment.py @@ -2,15 +2,20 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). import base64 +import logging import tempfile import zipfile from io import BytesIO from pathlib import Path +import lxml.etree as ET + from odoo import fields, models from odoo.addons.l10n_it_fatturapa_in.wizard import efattura +_logger = logging.getLogger(__name__) + def _extract_zip_file(directory, datas): """Extract the zip file having content `datas` to `directory`.""" @@ -19,6 +24,23 @@ def _extract_zip_file(directory, datas): zip_ref.extractall(directory) +def _is_xml_file(file_path): + """Check if the file at `file_path` is an XML file.""" + try: + # Attempt to parse the file as XML + parser = ET.XMLParser(recover=True) + root = ET.XML(file_path.read_bytes(), parser) + ET.tostring(root) + return True # Successfully parsed, it's an XML file + except Exception: + return False # Failed to parse, not an XML file + + +def _has_p7m_extension(file_path): + """Check if the file at `file_path` has a .p7m extension.""" + return file_path.suffix.lower() == ".p7m" + + class FatturaPAAttachmentImportZIP(models.Model): _name = "fatturapa.attachment.import.zip" _description = "E-bill ZIP import" @@ -137,29 +159,36 @@ def action_import(self): # we don't have the received date self.env.company.in_invoice_registration_date = "inv_date" - for xml_file in tmp_dir.glob("*"): - content = xml_file.read_bytes() - attach_vals = { - "name": xml_file.name, - "datas": base64.encodebytes(content), - "attachment_import_zip_id": self.id, - } - attachment = self.env["fatturapa.attachment.in"].create(attach_vals) - if attachment.xml_supplier_id == company_partner: - attachment.unlink() - attach_vals["state"] = "validated" - attachment = self.env["fatturapa.attachment.out"].create( - attach_vals - ) - wizard = ( - self.env["wizard.import.fatturapa"] - .with_context( - active_ids=attachment.ids, - active_model=attachment._name, + for xml_file in tmp_dir.rglob("*"): + # Process only files skipping non-XML/P7M files + if xml_file.is_file() and ( + _is_xml_file(xml_file) or _has_p7m_extension(xml_file) + ): + content = xml_file.read_bytes() + attach_vals = { + "name": xml_file.name, + "datas": base64.encodebytes(content), + "attachment_import_zip_id": self.id, + } + attachment = self.env["fatturapa.attachment.in"].create(attach_vals) + if attachment.xml_supplier_id == company_partner: + attachment.unlink() + attach_vals["state"] = "validated" + attachment = self.env["fatturapa.attachment.out"].create( + attach_vals + ) + wizard = ( + self.env["wizard.import.fatturapa"] + .with_context( + active_ids=attachment.ids, + active_model=attachment._name, + ) + .create({}) ) - .create({}) - ) - wizard.importFatturaPA() + _logger.info("Importing {}".format(xml_file)) + wizard.importFatturaPA() + else: + _logger.info("Skipping {}, not an XML/P7M file".format(xml_file)) self.env.company.in_invoice_registration_date = ( original_in_invoice_registration_date ) diff --git a/l10n_it_fatturapa_import_zip/readme/DESCRIPTION.rst b/l10n_it_fatturapa_import_zip/readme/DESCRIPTION.rst index 9c207b9098f3..7ce618ebfc8a 100644 --- a/l10n_it_fatturapa_import_zip/readme/DESCRIPTION.rst +++ b/l10n_it_fatturapa_import_zip/readme/DESCRIPTION.rst @@ -2,6 +2,10 @@ Questo modulo aggiunge una vista per importare diversi file XML di fatture elettroniche (OUT/IN) tramite file ZIP. +Il modulo importerà dal file ZIP solo file XML o P7M, anche se in subdirectory, ignorando file non riconosciuti. + **English** This module adds a view to import several XML e-invoice files (OUT/IN) via ZIP file. + +The module will import only XML or P7M files from the ZIP file, even if in subdirectories, ignoring unrecognized files. diff --git a/l10n_it_fatturapa_import_zip/tests/data/xml_import.zip b/l10n_it_fatturapa_import_zip/tests/data/xml_import.zip index 31e589cab602..f32f41b58d46 100644 Binary files a/l10n_it_fatturapa_import_zip/tests/data/xml_import.zip and b/l10n_it_fatturapa_import_zip/tests/data/xml_import.zip differ