Skip to content

[14.0][FIX/IMP]l10n_it_account & l10n_it_fatturapa_out: Nicer XML Validation errors #4266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions l10n_it_account/tools/account_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


def encode_for_export(string_to_encode, max_chars, encoding="latin"):
if not string_to_encode:
return ""
return (
reg_whitespace.sub(" ", string_to_encode)
.encode(encoding, errors="replace")
Expand Down
52 changes: 52 additions & 0 deletions l10n_it_fatturapa_out/tests/test_fatturapa_xml_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

import base64
import re
from unittest.mock import Mock

from psycopg2 import IntegrityError

import odoo
from odoo import fields
from odoo.exceptions import UserError
from odoo.tests import Form, tagged
Expand Down Expand Up @@ -886,6 +888,56 @@ def test_no_tax_fail(self):
)
self.assertEqual(ue.exception.args[0], error_message)

def test_partner_no_address_fail(self):
"""
- create an XML invoice where the customer has no address or city

expect to fail with a proper message
"""
invoice = self._create_invoice()
invoice.partner_id.street = False
invoice.partner_id.city = False
invoice._post()
wizard = self.wizard_model.create({})
with self.assertRaises(UserError) as ue:
wizard.with_context({"active_ids": [invoice.id]}).exportFatturaPA()
error_msg = ue.exception.args[0]
error_fragments = (
f"Error processing invoice(s) {invoice.name}",
"Indirizzo",
"Comune",
"Activate debug mode to see the full error",
)
for fragment in error_fragments:
self.assertIn(fragment, error_msg)

try:
# Enter debug mode and add details
odoo.http._request_stack.push(
Mock(
db=self.env.cr.dbname,
env=self.env,
debug=True,
website=False, # compatibility with website module
is_frontend=False,
)
)
wizard = self.wizard_model.create({})
with self.assertRaises(UserError) as ue:
wizard.with_context({"active_ids": [invoice.id]}).exportFatturaPA()
debug_error_msg = ue.exception.args[0]
debug_error_fragments = (
"Full error follows",
"Reason: value doesn't match any pattern of",
"p{IsBasicLatin}",
"<Comune xmlns:ns1",
)
for fragment in error_fragments[:-1] + debug_error_fragments:
self.assertIn(fragment, debug_error_msg)
finally:
# Remove from the stack to not interfere with other tests
odoo.http._request_stack.pop()

def test_multicompany_fail(self):
"""
- create two invoices in two different companies
Expand Down
21 changes: 20 additions & 1 deletion l10n_it_fatturapa_out/wizard/efattura.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,26 @@ def to_xml(self, env):
# i controlli precedenti dovrebbero escludere errori di sintassi XML
# with open("/tmp/fatturaout.xml", "wb") as o:
# o.write(etree.tostring(root, xml_declaration=True, encoding="utf-8"))
raise UserError("\n".join(str(e) for e in errors))
# Print error paths, as they can be helpful even for non-technical people
error_path_string = "\n- ".join(
err.path[err.path.index(":") + 1 :] for err in errors
)
error_msg = _(
"Error processing invoice(s) %(invoices)s.\n\n"
"Errors in the following fields:\n- %(error_path_string)s\n\n",
invoices=", ".join(
inv.display_name for inv in template_values["invoices"]
),
error_path_string=error_path_string,
)
# add details in debug mode
if env.user.user_has_groups("base.group_no_one"):
error_msg += _("Full error follows:\n\n") + "\n".join(
str(e) for e in errors
)
else:
error_msg += _("Activate debug mode to see the full error.")
raise UserError(error_msg)
content = etree.tostring(root, xml_declaration=True, encoding="utf-8")
return content

Expand Down
Loading