Skip to content

Commit

Permalink
[FIX] l10n_it_fatturapa_out: max number of invoices for FatturaPA
Browse files Browse the repository at this point in the history
Add a configuration parameter in company setting to limit the number of
invoices while creating an XML.

Fixes OCA#2544
Forward port of OCA#2545

Co-authored-by: SimoRubi <[email protected]>
  • Loading branch information
TheMule71 and SimoRubi committed Oct 21, 2024
1 parent 1747779 commit 5a35be1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
4 changes: 3 additions & 1 deletion l10n_it_fatturapa_out/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class ResPartner(models.Model):
max_invoice_in_xml = fields.Integer(
string="Max Invoice # in XML",
default=lambda self: self.env.company.max_invoice_in_xml,
help="Maximum number of invoices to group in a single " "XML file. 0=Unlimited",
help="Maximum number of invoices to group in a single XML file."
"0=Unlimited. If this is 0, then the number configured "
"in the account settings is considered.",
)

@api.constrains("max_invoice_in_xml")
Expand Down
11 changes: 9 additions & 2 deletions l10n_it_fatturapa_out/tests/fatturapa_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,16 @@ def set_sequences(
seq_date = inv_seq._create_date_range_seq(dt)
seq_date.number_next_actual = invoice_number

def run_wizard(self, invoice_id):
def run_wizard(self, invoice_ids):
"""
Execute the export wizard on the invoices having ID `invoice_ids`.
:param invoice_ids: integer or list of integers
:return: result of export wizard
"""
if not isinstance(invoice_ids, list):
invoice_ids = [invoice_ids]
wizard = self.wizard_model.create({})
return wizard.with_context({"active_ids": [invoice_id]}).exportFatturaPA()
return wizard.with_context({"active_ids": invoice_ids}).exportFatturaPA()

def set_e_invoice_file_id(self, e_invoice, file_name):
# We need this because file name is random and we can't predict it
Expand Down
50 changes: 50 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 @@ -1199,3 +1199,53 @@ def test_18_xml_export(self):
# XML doc to be validated
xml_content = base64.decodebytes(attachment.datas)
self.check_content(xml_content, "IT06363391001_00018.xml")

def test_max_invoice_in_xml(self):
invoice1_form = Form(
self.env["account.move"].with_context({"default_move_type": "out_invoice"})
)
invoice1_form.partner_id = self.res_partner_fatturapa_0
with invoice1_form.line_ids.new() as line_form:
line_form.product_id = self.product_product_10
line_form.account_id = self.a_sale
line_form.tax_ids.clear()
line_form.tax_ids.add(self.tax_22)
invoice1 = invoice1_form.save()
invoice2 = invoice1.copy()
invoice3 = invoice1.copy()

invoice1.action_post()
invoice2.action_post()
invoice3.action_post()
invoices = invoice1 | invoice2 | invoice3

# partner limited, company limited (expect 3 xml attachments)
self.res_partner_fatturapa_0.max_invoice_in_xml = 1
self.env.company.max_invoice_in_xml = 2
res = self.run_wizard(invoices.ids)
attachments = self.attach_model.search(res["domain"])
self.assertEqual(len(attachments), 3)
attachments.unlink()

# partner limited, company unlimited (expect 3 xml attachments)
self.res_partner_fatturapa_0.max_invoice_in_xml = 1
self.env.company.max_invoice_in_xml = 0
res = self.run_wizard(invoices.ids)
attachments = self.attach_model.search(res["domain"])
self.assertEqual(len(attachments), 3)
attachments.unlink()

# partner unlimited, company limited (expect 2 xml attachments)
self.res_partner_fatturapa_0.max_invoice_in_xml = 0
self.env.company.max_invoice_in_xml = 2
res = self.run_wizard(invoices.ids)
attachments = self.attach_model.search(res["domain"])
self.assertEqual(len(attachments), 2)
attachments.unlink()

# partner unlimited, company unlimited (expect 1 xml attachment)
self.res_partner_fatturapa_0.max_invoice_in_xml = 0
self.env.company.max_invoice_in_xml = 0
res = self.run_wizard(invoices.ids)
attachments = self.attach_model.browse(res["res_id"])
self.assertEqual(len(attachments), 1)
9 changes: 5 additions & 4 deletions l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ def split_list(my_list, size):
res[invoice.partner_id] = []
res[invoice.partner_id].append(invoice.id)
for partner_id in res.keys():
if partner_id.max_invoice_in_xml:
res[partner_id] = list(
split_list(res[partner_id], partner_id.max_invoice_in_xml)
)
max_invoice_in_xml = (
partner_id.max_invoice_in_xml or self.env.company.max_invoice_in_xml
)
if max_invoice_in_xml:
res[partner_id] = list(split_list(res[partner_id], max_invoice_in_xml))
else:
res[partner_id] = [res[partner_id]]
# The returned dictionary contains a plain res.partner object as key
Expand Down

0 comments on commit 5a35be1

Please sign in to comment.