From 67853503f589eac0aef54a6f44cc0cc19d65a34e Mon Sep 17 00:00:00 2001 From: sergiocorato Date: Tue, 3 Jan 2023 16:09:48 +0100 Subject: [PATCH] [FIX] l10n_it_declaration_of_intent: amounts declarations validation --- .../models/account_move.py | 41 ++++++++++++++++--- .../tests/test_declaration_of_intent.py | 22 ++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/l10n_it_declaration_of_intent/models/account_move.py b/l10n_it_declaration_of_intent/models/account_move.py index 565e0f151493..59f690f453b3 100644 --- a/l10n_it_declaration_of_intent/models/account_move.py +++ b/l10n_it_declaration_of_intent/models/account_move.py @@ -97,6 +97,9 @@ def _post(self, soft=True): continue invoice.check_declarations_amounts(declarations) + declarations_used_amounts = invoice.get_declarations_used_amounts( + declarations + ) # Assign account move lines to declarations for each invoice # Get only lines with taxes @@ -105,11 +108,11 @@ def _post(self, soft=True): continue # Group lines by tax grouped_lines = self.get_move_lines_by_declaration(lines) - invoice.update_declarations(declarations, grouped_lines) + invoice.update_declarations(declarations_used_amounts, grouped_lines) return posted - def update_declarations(self, declarations, grouped_lines): + def update_declarations(self, declarations_used_amounts, grouped_lines): """ Update the declarations adding a new line representing this invoice. @@ -125,11 +128,14 @@ def update_declarations(self, declarations, grouped_lines): amount *= -1 # Select right declaration(s) if force_declaration: - declarations = [force_declaration] + declaration_id_to_amount_dict = {force_declaration.id: amount} else: - declarations = declarations + declaration_id_to_amount_dict = declarations_used_amounts - for declaration in declarations: + for declaration_id in declaration_id_to_amount_dict: + declaration = self.env[ + "l10n_it_declaration_of_intent.declaration" + ].browse(declaration_id) if tax not in declaration.taxes_ids: continue # avoid creating line with same invoice_id @@ -137,7 +143,15 @@ def update_declarations(self, declarations, grouped_lines): lambda line: line.invoice_id == self ).unlink() declaration.line_ids = [ - (0, 0, self._prepare_declaration_line(amount, lines, tax)), + ( + 0, + 0, + self._prepare_declaration_line( + declaration_id_to_amount_dict[declaration_id], + lines, + tax, + ), + ), ] # Link declaration to invoice self.declaration_of_intent_ids = [(4, declaration.id)] @@ -216,6 +230,21 @@ def get_declarations(self): ) return declarations + def get_declarations_used_amounts(self, declarations): + """Get used amount by declarations for this invoice.""" + self.ensure_one() + declarations_used_amounts = {} + sign = 1 if self.move_type in ["out_invoice", "in_invoice"] else -1 + for tax_line in self.line_ids.filtered("tax_ids"): + amount = sign * tax_line.price_subtotal + for declaration in declarations: + if declaration.id not in declarations_used_amounts: + declarations_used_amounts[declaration.id] = 0 + if any(tax in declaration.taxes_ids for tax in tax_line.tax_ids): + declarations_used_amounts[declaration.id] += amount + amount = 0.0 + return declarations_used_amounts + def check_declarations_amounts(self, declarations): """ Compare this invoice's tax amounts and `declarations` plafond. diff --git a/l10n_it_declaration_of_intent/tests/test_declaration_of_intent.py b/l10n_it_declaration_of_intent/tests/test_declaration_of_intent.py index c741a201f735..5ee7928628d8 100644 --- a/l10n_it_declaration_of_intent/tests/test_declaration_of_intent.py +++ b/l10n_it_declaration_of_intent/tests/test_declaration_of_intent.py @@ -523,3 +523,25 @@ def test_action_register_payment(self): else: payments = action["domain"][0][2] self.assertTrue(len(payments) > 1) + + def test_multiple_valid(self): + """ + Check multi declarations validation for same partner. + """ + declaration_model = self.env["l10n_it_declaration_of_intent.declaration"].sudo() + post_used_amount2 = self.declaration2.used_amount + post_used_amount3 = self.declaration3.used_amount + self.assertAlmostEqual(post_used_amount2, 0.0, 2) + self.assertAlmostEqual(post_used_amount3, 0.0, 2) + valid_declarations = declaration_model.get_valid( + type_d="out", partner_id=self.partner2.id, date=self.today_date + ) + self.assertEqual(valid_declarations, self.declaration2 | self.declaration3) + invoice6 = self._create_invoice( + "test_multiple_valid", self.partner2, tax=self.tax1 + ) + invoice6.action_post() + new_post_used_amount2 = self.declaration2.used_amount + new_post_used_amount3 = self.declaration3.used_amount + self.assertAlmostEqual(new_post_used_amount2, 900.0, 2) + self.assertAlmostEqual(new_post_used_amount3, 0.0, 2)