Skip to content

Commit

Permalink
Merge PR #3959 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by TheMule71
  • Loading branch information
OCA-git-bot committed Feb 23, 2024
2 parents 448a273 + fb0a0b2 commit 54cbae1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
73 changes: 51 additions & 22 deletions l10n_it_fatturapa_import_zip/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`."""
Expand All @@ -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"
Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 4 additions & 0 deletions l10n_it_fatturapa_import_zip/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Binary file modified l10n_it_fatturapa_import_zip/tests/data/xml_import.zip
Binary file not shown.

0 comments on commit 54cbae1

Please sign in to comment.