Skip to content

Commit

Permalink
[FIX] l10n_it_fatturapa_import_zip correctly imports all XML files in…
Browse files Browse the repository at this point in the history
… zip
  • Loading branch information
rlucia committed Feb 14, 2024
1 parent 6f8f988 commit 1c7423d
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions l10n_it_fatturapa_import_zip/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import base64
import tempfile
import xml.etree.ElementTree as ET
import zipfile

from io import BytesIO
from pathlib import Path

Expand Down Expand Up @@ -125,6 +127,15 @@ def _compute_invoices_data(self):
),
)

def is_xml_file(self, file_path):
"""Check if the file at `file_path` is an XML file."""
try:
# Attempt to parse the file as XML
ET.parse(file_path)
return True # Successfully parsed, it's an XML file
except ET.ParseError:
return False # Failed to parse, not an XML file

def action_import(self):
self.ensure_one()
company_partner = self.env.company.partner_id
Expand All @@ -137,29 +148,31 @@ 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("*"):
# Skip directories and non-XML files
if xml_file.is_file() and self.is_xml_file(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()
wizard.importFatturaPA()
self.env.company.in_invoice_registration_date = (
original_in_invoice_registration_date
)
Expand Down

0 comments on commit 1c7423d

Please sign in to comment.