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 Feb 4, 2022
1 parent ca07314 commit c64e178
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 @@ -182,9 +182,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 @@ -875,3 +875,53 @@ def test_no_export_bill(self):
with self.assertRaises(UserError) as ue:
self.run_wizard(invoice.id)
self.assertIn(invoice.name, ue.exception.args[0])

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 @@ -89,10 +89,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 c64e178

Please sign in to comment.