Skip to content

Commit

Permalink
Merge PR #3467 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by SirAionTech
  • Loading branch information
OCA-git-bot committed Jul 9, 2024
2 parents c780b71 + 6785350 commit d1dbeaf
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
41 changes: 35 additions & 6 deletions l10n_it_declaration_of_intent/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -125,19 +128,30 @@ 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
declaration.line_ids.filtered(
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)]
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions l10n_it_declaration_of_intent/tests/test_declaration_of_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d1dbeaf

Please sign in to comment.