From 4b6c621d4096204f298de4048f0309bf3e810f6b Mon Sep 17 00:00:00 2001 From: SimoRubi Date: Tue, 7 Dec 2021 12:25:00 +0100 Subject: [PATCH] [FIX] l10n_it_fatturapa_out: Max invoice fallback on configured value Co-authored-by: Simone Rubino --- l10n_it_fatturapa_out/models/partner.py | 5 +- .../tests/test_fatturapa_xml_validation.py | 68 +++++++++++++++++++ .../wizard/wizard_export_fatturapa.py | 10 +-- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/l10n_it_fatturapa_out/models/partner.py b/l10n_it_fatturapa_out/models/partner.py index 4df12cad811e..f0803b7db5e3 100644 --- a/l10n_it_fatturapa_out/models/partner.py +++ b/l10n_it_fatturapa_out/models/partner.py @@ -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") diff --git a/l10n_it_fatturapa_out/tests/test_fatturapa_xml_validation.py b/l10n_it_fatturapa_out/tests/test_fatturapa_xml_validation.py index e957358c5a45..1024da3d12dd 100644 --- a/l10n_it_fatturapa_out/tests/test_fatturapa_xml_validation.py +++ b/l10n_it_fatturapa_out/tests/test_fatturapa_xml_validation.py @@ -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) diff --git a/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py b/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py index f4279a6f8a28..03ce07d13d35 100644 --- a/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py +++ b/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py @@ -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