From 18fa1a1ff1c18ff7950797025ccc9042f6133279 Mon Sep 17 00:00:00 2001 From: Meritxell Abellan Date: Tue, 17 Dec 2024 13:32:48 +0100 Subject: [PATCH] [MIG] mrp_production_back_to_draft: Migration to 17.0 --- mrp_production_back_to_draft/__manifest__.py | 2 +- .../models/mrp_production.py | 18 +++++++++++++++++- .../tests/test_mrp_return_to_draft.py | 9 ++++----- .../views/mrp_production_views.xml | 2 +- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/mrp_production_back_to_draft/__manifest__.py b/mrp_production_back_to_draft/__manifest__.py index b9eeff503..56ee943a8 100644 --- a/mrp_production_back_to_draft/__manifest__.py +++ b/mrp_production_back_to_draft/__manifest__.py @@ -3,7 +3,7 @@ { "name": "MRP Production Back to Draft", - "version": "14.0.1.0.1", + "version": "17.0.1.0.0", "author": "ForgeFlow, Odoo Community Association (OCA)", "summary": "Allows to return to draft a confirmed or cancelled MO.", "website": "https://github.com/OCA/manufacture", diff --git a/mrp_production_back_to_draft/models/mrp_production.py b/mrp_production_back_to_draft/models/mrp_production.py index b78c85354..500d6e4a7 100644 --- a/mrp_production_back_to_draft/models/mrp_production.py +++ b/mrp_production_back_to_draft/models/mrp_production.py @@ -1,7 +1,7 @@ # Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import _, models +from odoo import _, api, models from odoo.exceptions import UserError @@ -22,3 +22,19 @@ class MrpProduction(models.Model): else: (rec.move_raw_ids + rec.move_finished_ids)._action_cancel() (rec.move_raw_ids + rec.move_finished_ids).write({"state": "draft"}) + if rec.state != "draft": + raise UserError( + _("Could not set the production order back to draft") + ) + + @api.depends("move_raw_ids.state", "move_finished_ids.state") + def _compute_state(self): + super()._compute_state() + for production in self: + if ( + production.state == "cancel" + and all(m.state == "draft" for m in production.move_raw_ids) + and all(m.state == "draft" for m in production.move_finished_ids) + ): + production.state = "draft" + return diff --git a/mrp_production_back_to_draft/tests/test_mrp_return_to_draft.py b/mrp_production_back_to_draft/tests/test_mrp_return_to_draft.py index 2a9067af2..1505b7598 100644 --- a/mrp_production_back_to_draft/tests/test_mrp_return_to_draft.py +++ b/mrp_production_back_to_draft/tests/test_mrp_return_to_draft.py @@ -7,13 +7,12 @@ from odoo.tests import common, tagged @tagged("post_install", "-at_install") -class TestMrpProductionAutovalidate(common.SavepointCase): +class TestMrpProductionAutovalidate(common.TransactionCase): @classmethod def setUpClass(cls): super().setUpClass() cls.user_admin = cls.env.ref("base.user_admin") cls.env = api.Environment(cls.cr, cls.user_admin.id, {}) - cls.env.user.tz = False # Make sure there's no timezone in user cls.warehouse = cls.env.ref("stock.warehouse0") cls.location = cls.env.ref("stock.stock_location_stock") cls.env.user.write( @@ -68,19 +67,19 @@ class TestMrpProductionAutovalidate(common.SavepointCase): self.prod_ti1, self.location, 2 ) self.assertEqual(self.mo_1.state, "draft") - self.mo_1._onchange_move_raw() + self.mo_1._compute_move_raw_ids() self.mo_1.action_confirm() self.assertEqual(self.mo_1.state, "confirmed") self.mo_1.action_return_to_draft() self.assertEqual(self.mo_1.state, "draft") - self.mo_1._onchange_move_raw() + self.mo_1._compute_move_raw_ids() self.mo_1.action_confirm() self.assertEqual(self.mo_1.state, "confirmed") self.mo_1.action_cancel() self.assertEqual(self.mo_1.state, "cancel") self.mo_1.action_return_to_draft() self.assertEqual(self.mo_1.state, "draft") - self.mo_1._onchange_move_raw() + self.mo_1._compute_move_raw_ids() self.mo_1.action_confirm() self.mo_1.qty_producing = 2 self.assertEqual(self.mo_1.state, "to_close") diff --git a/mrp_production_back_to_draft/views/mrp_production_views.xml b/mrp_production_back_to_draft/views/mrp_production_views.xml index 27cfe8b8b..9f6dca024 100644 --- a/mrp_production_back_to_draft/views/mrp_production_views.xml +++ b/mrp_production_back_to_draft/views/mrp_production_views.xml @@ -15,7 +15,7 @@ type="object" class="oe_stat_button" string="Return to Draft" - attrs="{'invisible': [('state', 'not in', ['confirmed', 'cancel'])]}" + invisible="state not in ['confirmed', 'cancel']" >