From 0bc6e43289a8997665bc9b47cb41d7863688da58 Mon Sep 17 00:00:00 2001
From: Sergio Zanchetta <primes2h@gmail.com>
Date: Wed, 15 Jan 2025 11:29:49 +0100
Subject: [PATCH] [FIX] l10n_it_account_stamp: stamp invoice line deleted on
 reset to draft

---
 l10n_it_account_stamp/models/account_move.py  | 13 ++++---
 .../tests/test_account_stamp_invoicing.py     | 38 +++++++++----------
 .../views/account_move_view.xml               |  8 ++--
 3 files changed, 29 insertions(+), 30 deletions(-)

diff --git a/l10n_it_account_stamp/models/account_move.py b/l10n_it_account_stamp/models/account_move.py
index f301e02b9b43..03beed74dae7 100644
--- a/l10n_it_account_stamp/models/account_move.py
+++ b/l10n_it_account_stamp/models/account_move.py
@@ -14,9 +14,9 @@ class AccountMove(models.Model):
         compute="_compute_tax_stamp",
         store=True,
     )
-    tax_stamp_line_present = fields.Boolean(
+    tax_stamp_invoice_line_present = fields.Boolean(
         string="Stamp line is present in invoice",
-        compute="_compute_tax_stamp_line_present",
+        compute="_compute_tax_stamp_invoice_line_present",
     )
     auto_compute_stamp = fields.Boolean(
         related="company_id.tax_stamp_product_id.auto_compute"
@@ -60,7 +60,7 @@ def _compute_tax_stamp(self):
                 if invoice.manually_apply_tax_stamp:
                     invoice.tax_stamp = True
 
-    def add_tax_stamp_line(self):
+    def add_tax_stamp_invoice_line(self):
         for inv in self:
             if not inv.tax_stamp:
                 raise UserError(_("Tax stamp is not applicable"))
@@ -84,7 +84,6 @@ def add_tax_stamp_line(self):
             invoice_line_vals = {
                 "move_id": inv.id,
                 "product_id": stamp_product_id.id,
-                "is_stamp_line": True,
                 "name": stamp_product_id.description_sale,
                 "sequence": 99999,
                 "account_id": stamp_account.id,
@@ -108,9 +107,11 @@ def is_tax_stamp_line_present(self):
         "invoice_line_ids.product_id",
         "invoice_line_ids.product_id.is_stamp",
     )
-    def _compute_tax_stamp_line_present(self):
+    def _compute_tax_stamp_invoice_line_present(self):
         for invoice in self:
-            invoice.tax_stamp_line_present = invoice.is_tax_stamp_product_present()
+            invoice.tax_stamp_invoice_line_present = (
+                invoice.is_tax_stamp_product_present()
+            )
 
     def is_tax_stamp_product_present(self):
         product_stamp = self.invoice_line_ids.filtered(
diff --git a/l10n_it_account_stamp/tests/test_account_stamp_invoicing.py b/l10n_it_account_stamp/tests/test_account_stamp_invoicing.py
index 3dceb6e89739..6dd911d262a2 100644
--- a/l10n_it_account_stamp/tests/test_account_stamp_invoicing.py
+++ b/l10n_it_account_stamp/tests/test_account_stamp_invoicing.py
@@ -84,7 +84,7 @@ def test_keep_lines_description(self):
         invoice.action_post()
 
         # Add stamp and check that edited description is kept
-        invoice.add_tax_stamp_line()
+        invoice.add_tax_stamp_invoice_line()
         self.assertEqual(invoice.invoice_line_ids[0].name, edited_descr)
 
     def test_amount_total_changing_currency(self):
@@ -102,27 +102,25 @@ def test_amount_total_changing_currency(self):
         invoice.action_post()
         self.assertEqual(total, invoice.amount_total)
 
-    def test_tax_stamp_line_button(self):
-        """Stamp fields show when stamp is added with the button to the invoice."""
-        # Arrange: Create an invoice eligible for tax stamp but without it
-        stamp_tax = self.tax_id
-        invoice = self.init_invoice(
-            "out_invoice",
-            taxes=stamp_tax,
-            amounts=[
-                100,
-            ],
+    def test_reset_invoice_to_draft(self):
+        """Reset an invoice to draft and check that relative tax stamp accounting lines
+        has been deleted."""
+        invoice = first(
+            self.invoices.filtered(lambda inv: inv.move_type == "out_invoice")
         )
-        # pre-condition
-        self.assertTrue(invoice.tax_stamp)
-        self.assertFalse(invoice.tax_stamp_line_present)
 
-        # Act
-        invoice.add_tax_stamp_line()
+        self.assertEqual(len(invoice), 1)
+        self.assertEqual(len(invoice.invoice_line_ids), 2)
+
+        invoice.invoice_line_ids[0].write({"tax_ids": [(6, 0, [self.tax_id.id])]})
+        invoice.action_post()
 
-        # Assert
-        self.assertTrue(invoice.tax_stamp_line_present)
+        self.assertEqual(
+            len(invoice.line_ids.filtered(lambda line: line.is_stamp_line)), 2
+        )
 
-        # Resetting to draft removes the stamp
         invoice.button_draft()
-        self.assertFalse(invoice.tax_stamp_line_present)
+
+        self.assertEqual(
+            len(invoice.line_ids.filtered(lambda line: line.is_stamp_line)), 0
+        )
diff --git a/l10n_it_account_stamp/views/account_move_view.xml b/l10n_it_account_stamp/views/account_move_view.xml
index a6de558a64c5..e1b08769eaae 100644
--- a/l10n_it_account_stamp/views/account_move_view.xml
+++ b/l10n_it_account_stamp/views/account_move_view.xml
@@ -27,7 +27,7 @@
                     name="manually_apply_tax_stamp"
                     attrs="{'invisible': [('auto_compute_stamp', '=', True)]}"
                 />
-                <field name="tax_stamp_line_present" invisible="1" />
+                <field name="tax_stamp_invoice_line_present" invisible="1" />
             </xpath>
             <field name="narration" position="before">
                 <div
@@ -40,13 +40,13 @@
                         alt="Tax stamp"
                     />
                     <span
-                        attrs="{'invisible': [('tax_stamp_line_present', '=', True)]}"
+                        attrs="{'invisible': [('tax_stamp_invoice_line_present', '=', True)]}"
                     >
                         <span attrs="{'invisible': [('state', 'not in', 'draft')]}">
                             <button
                                 type="object"
                                 string="Charge stamp to customer"
-                                name="add_tax_stamp_line"
+                                name="add_tax_stamp_invoice_line"
                             />
                         </span>
                         <span attrs="{'invisible': [('state', '=', 'draft')]}">
@@ -54,7 +54,7 @@
                         </span>
                     </span>
                     <span
-                        attrs="{'invisible': [('tax_stamp_line_present', '=', False)]}"
+                        attrs="{'invisible': [('tax_stamp_invoice_line_present', '=', False)]}"
                     >
                         Stamp charged to customer
                     </span>