Skip to content

Commit

Permalink
[FIX] l10n_it_fatturapa_out: Max invoice fallback on configured value
Browse files Browse the repository at this point in the history
Co-authored-by:  Simone Rubino <[email protected]>
  • Loading branch information
SimoRubi and SirAionTech committed Dec 27, 2023
1 parent 6cfa37e commit 4b6c621
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
5 changes: 4 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,10 @@ 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.\n"
"If this is 0, then the number configured "
"in the account settings is considered.",
)

@api.constrains("max_invoice_in_xml")
Expand Down
68 changes: 68 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 @@ -938,3 +938,71 @@ def test_validate_invoice(self):
invoice.action_post()

self.assertEqual(invoice.state, "posted")

def _get_multiple_invoices(self, partner, invoices_number=2):
"""Create `invoices_number` invoices for `partner`."""
invoices = self.invoice_model.browse()
for _ in range(invoices_number):
invoices |= self.init_invoice(
"out_invoice",
partner=partner,
amounts=[
100,
],
)
invoices.action_post()
return invoices

def test_max_invoice_number_unlimited(self):
"""Check that when both partner and company do not have any max value,
only one attachment is created."""

# pre-condition: partner and company do not have any max value
company = self.company
self.assertEqual(company.max_invoice_in_xml, 0)
partner = self.res_partner_fatturapa_0
self.assertEqual(partner.max_invoice_in_xml, 0)

# Create two invoices
invoices = self._get_multiple_invoices(partner)
self.run_wizard(invoices.ids)

# Check that only one attachment is created
attachments_nbr = len(invoices.mapped("fatturapa_attachment_out_id"))
self.assertEqual(attachments_nbr, 1)

def test_max_invoice_number_partner(self):
"""Check that when partner has a max value, company value is ignored and
many attachments are created."""

# pre-condition: partner has a value
company = self.company
self.assertEqual(company.max_invoice_in_xml, 0)
partner = self.res_partner_fatturapa_0
partner.max_invoice_in_xml = 1

# Create two invoices
invoices = self._get_multiple_invoices(partner)
self.run_wizard(invoices.ids)

# Check that two attachments are created
attachments_nbr = len(invoices.mapped("fatturapa_attachment_out_id"))
self.assertEqual(attachments_nbr, 2)

def test_max_invoice_number_company(self):
"""Check that when company has a max value and partner does not,
many attachments are created."""

# pre-condition: only company has a value
company = self.company
company.max_invoice_in_xml = 1
partner = self.res_partner_fatturapa_0
self.assertEqual(partner.max_invoice_in_xml, 0)

# Create two invoices
invoices = self._get_multiple_invoices(partner)
self.run_wizard(invoices.ids)

# Check that two attachments are created
attachments_nbr = len(invoices.mapped("fatturapa_attachment_out_id"))
self.assertEqual(attachments_nbr, 2)
10 changes: 6 additions & 4 deletions l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ def split_list(my_list, size):
if invoice.partner_id not in res:
res[invoice.partner_id] = []
res[invoice.partner_id].append(invoice.id)

company = self.env.company
company_max_invoice = company.max_invoice_in_xml
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 = partner_id.max_invoice_in_xml or company_max_invoice
if max_invoice:
res[partner_id] = list(split_list(res[partner_id], max_invoice))
else:
res[partner_id] = [res[partner_id]]
# The returned dictionary contains a plain res.partner object as key
Expand Down

0 comments on commit 4b6c621

Please sign in to comment.