From 8d37cf462badc25d911d3fa6d3382c6f7418904f Mon Sep 17 00:00:00 2001
From: "Touati Djamel (otd)" <otd@odoo.com>
Date: Wed, 1 Feb 2023 12:54:09 +0000
Subject: [PATCH] [FIX] repair: prevent cancelling a completed repair order

Steps to reproduce the bug:
- Create a repair:
    - Add any product to repair
    - Add any other product as part of the repair
    - Confirm the repair
    - Start the repair
    - End repair

Problem:
The cancel button becomes visible and clicking on it won't cancel the
moves, so it doesn't make sense to cancel a finished repair.

opw-3146606

closes odoo/odoo#111601

Signed-off-by: William Henrotin (whe) <whe@odoo.com>
---
 addons/repair/i18n/repair.pot        | 6 ++++++
 addons/repair/models/repair.py       | 2 ++
 addons/repair/views/repair_views.xml | 2 +-
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/addons/repair/i18n/repair.pot b/addons/repair/i18n/repair.pot
index 272032513e2b..49d429b120d0 100644
--- a/addons/repair/i18n/repair.pot
+++ b/addons/repair/i18n/repair.pot
@@ -1271,6 +1271,12 @@ msgstr ""
 msgid "You can not enter negative quantities."
 msgstr ""
 
+#. module: repair
+#: code:addons/repair/models/repair.py:0
+#, python-format
+msgid "You cannot cancel a completed repair order."
+msgstr ""
+
 #. module: repair
 #: code:addons/repair/models/repair.py:0 code:addons/repair/models/repair.py:0
 #, python-format
diff --git a/addons/repair/models/repair.py b/addons/repair/models/repair.py
index 5cba17254842..8ecba612c915 100644
--- a/addons/repair/models/repair.py
+++ b/addons/repair/models/repair.py
@@ -279,6 +279,8 @@ class Repair(models.Model):
         return True
 
     def action_repair_cancel(self):
+        if self.state == 'done':
+            raise UserError(_("You cannot cancel a completed repair order."))
         invoice_to_cancel = self.filtered(lambda repair: repair.invoice_id.state == 'draft').invoice_id
         if invoice_to_cancel:
             invoice_to_cancel.button_cancel()
diff --git a/addons/repair/views/repair_views.xml b/addons/repair/views/repair_views.xml
index 0e01aedcb843..ed9ce713ed09 100644
--- a/addons/repair/views/repair_views.xml
+++ b/addons/repair/views/repair_views.xml
@@ -38,7 +38,7 @@
                    <button name="print_repair_order" states="draft" string="Print Quotation" type="object"/>
                    <button name="action_repair_cancel_draft" states="cancel" string="Set to Draft" type="object"/>
                    <button name="action_repair_cancel" string="Cancel Repair" type="object" confirm="Draft invoices for this order will be cancelled. Do you confirm the action?" attrs="{'invisible':['|', ('state', '=', 'cancel'), ('invoice_state', '!=', 'draft')]}"/>
-                   <button name="action_repair_cancel" string="Cancel Repair" type="object" attrs="{'invisible': ['|', ('state','=', 'cancel',), ('invoice_state', '=', 'draft')]}"/>
+                   <button name="action_repair_cancel" string="Cancel Repair" type="object" attrs="{'invisible': ['|', ('state','in', ('cancel','done')), ('invoice_state', '=', 'draft')]}"/>
                    <field name="state" widget="statusbar" statusbar_visible="draft,confirmed,done"/>
                </header>
                <sheet string="Repairs order">
-- 
GitLab