From eff7cc00563763f5cd06d452a7d48efeed0ad513 Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" Date: Mon, 25 Apr 2022 15:36:25 +0000 Subject: [PATCH] [FIX] mrp: start the workorder only once Steps to reproduce the bug: - Create a MO with work orders - Confirm the MO - Under the tab Work Orders > click on the button start multiple times quickly Problem: Several `mrp.workcentre.productivity` are created for the work order, whereas normally the workorder can have only one timer started (without date_end). Solution: - If the workorder already has a timer running, return true to stop the process - Add a constraint to block the user when he tries for example to create with an rpc call several `mrp.workcenter.productivity` without an end date for the same workoder opw-2802436 closes odoo/odoo#90094 X-original-commit: dd22195a75ec437e42424ee95ca9a41feeb48b0b Signed-off-by: William Henrotin (whe) Signed-off-by: Djamel Touati (otd) --- addons/mrp/i18n/mrp.pot | 6 ++++++ addons/mrp/models/mrp_workcenter.py | 6 ++++++ addons/mrp/models/mrp_workorder.py | 2 ++ addons/mrp/tests/test_order.py | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/addons/mrp/i18n/mrp.pot b/addons/mrp/i18n/mrp.pot index 668e4a24de1..7925dda7905 100644 --- a/addons/mrp/i18n/mrp.pot +++ b/addons/mrp/i18n/mrp.pot @@ -4373,6 +4373,12 @@ msgid "" "product form." msgstr "" +#. module: mrp +#: code:addons/mrp/models/mrp_workcenter.py:0 +#, python-format +msgid "The Workoder (%s) cannot be started twice!" +msgstr "" + #. module: mrp #: code:addons/mrp/models/mrp_bom.py:0 #, python-format diff --git a/addons/mrp/models/mrp_workcenter.py b/addons/mrp/models/mrp_workcenter.py index fa7cbff0ec6..cb61b063ff8 100644 --- a/addons/mrp/models/mrp_workcenter.py +++ b/addons/mrp/models/mrp_workcenter.py @@ -395,6 +395,12 @@ class MrpWorkcenterProductivity(models.Model): else: blocktime.duration = 0.0 + @api.constrains('workorder_id') + def _check_open_time_ids(self): + for workorder in self.workorder_id: + if len(workorder.time_ids.filtered(lambda t: t.date_start and not t.date_end)) > 1: + raise ValidationError(_('The Workoder (%s) cannot be started twice!', workorder.display_name)) + def button_block(self): self.ensure_one() self.workcenter_id.order_ids.end_all() diff --git a/addons/mrp/models/mrp_workorder.py b/addons/mrp/models/mrp_workorder.py index e3a7a811b60..e80f6314c94 100644 --- a/addons/mrp/models/mrp_workorder.py +++ b/addons/mrp/models/mrp_workorder.py @@ -552,6 +552,8 @@ class MrpWorkorder(models.Model): def button_start(self): self.ensure_one() + if any(time.date_start and not time.date_end for time in self.time_ids): + return True # As button_start is automatically called in the new view if self.state in ('done', 'cancel'): return True diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index 87591b6c72f..8872e36f02e 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -2754,3 +2754,23 @@ class TestMrpOrder(TestMrpCommon): self.assertEqual(mo.state, 'cancel', 'Manufacturing order should be cancelled.') self.assertEqual(wo.state, 'cancel', 'Workorders should be cancelled.') self.assertTrue(mo.workorder_ids.time_ids.date_end, 'The timers must stop after the cancellation of the MO') + + def test_starting_wo_twice(self): + """ + Check that the work order is started only once when clicking the start button several times. + """ + production_form = Form(self.env['mrp.production']) + production_form.bom_id = self.bom_2 + production_form.product_qty = 1 + production = production_form.save() + production_form = Form(production) + with production_form.workorder_ids.new() as wo: + wo.name = 'OP1' + wo.workcenter_id = self.workcenter_1 + wo.duration_expected = 40 + production = production_form.save() + production.action_confirm() + production.button_plan() + production.workorder_ids[0].button_start() + production.workorder_ids[0].button_start() + self.assertEqual(len(production.workorder_ids[0].time_ids.filtered(lambda t: t.date_start and not t.date_end)), 1)