From 749b223503b5356ce24a06b5a624a4f2169648cc Mon Sep 17 00:00:00 2001 From: louck Date: Wed, 15 Mar 2023 14:38:18 +0100 Subject: [PATCH] Added a check on picking when going in "picking ongoing" stage --- commown_devices/data/project_task.xml | 12 ++- commown_devices/models/project_task.py | 22 ++++- .../models/wizard_project_task_picking.py | 11 ++- commown_devices/tests/test_project_task.py | 85 +++++++++++++++++-- 4 files changed, 116 insertions(+), 14 deletions(-) diff --git a/commown_devices/data/project_task.xml b/commown_devices/data/project_task.xml index 00def956f..07f700e7f 100644 --- a/commown_devices/data/project_task.xml +++ b/commown_devices/data/project_task.xml @@ -2,12 +2,16 @@ - - Diagnostic stage + + Diagnostic stage - - Resiliated stage + + Picking ongoing stage + + + + Resiliated stage diff --git a/commown_devices/models/project_task.py b/commown_devices/models/project_task.py index e49554b03..d1486314f 100644 --- a/commown_devices/models/project_task.py +++ b/commown_devices/models/project_task.py @@ -6,6 +6,8 @@ "commown_devices.resiliated_stage", ] +CHECK_PICKING_ASSIGNED_STAGE_XML_IDS = "commown_devices.sup_picking_ongoing_stage" + class ProjectTask(models.Model): _inherit = "project.task" @@ -144,7 +146,7 @@ def onchange_lot_id(self): self.contract_id = contracts.id @api.constrains("stage_id") - def onchange_stage_id_prevent_contract_resiliation_with_device(self): + def _check_stage_id_prevent_contract_resiliation_with_device(self): if self.stage_id and self.contract_id and self.contract_id.quant_nb > 0: check_stage_ids = tuple( self.env.ref(ref).id for ref in CHECK_CONTRACT_QUANT_NB_STAGE_XML_IDS @@ -158,6 +160,24 @@ def onchange_stage_id_prevent_contract_resiliation_with_device(self): % self.contract_id.quant_nb ) + @api.constrains("stage_id") + def _check_assigned_picking(self): + if self.stage_id and self.contract_id: + picking_ongoing_stage = self.env.ref(CHECK_PICKING_ASSIGNED_STAGE_XML_IDS) + if ( + self.stage_id.id == picking_ongoing_stage.id + and not self.contract_id.picking_ids.filtered( + lambda p, task=self: p._assigned() + and p.origin == task.get_id_name() + ) + ): + raise Warning( + _( + "This task can not be moved forward. There are no picking " + "linked to this task." + ) + ) + def action_scrap_device(self): scrap_loc = self.env.ref("stock.stock_location_scrapped") diff --git a/commown_devices/models/wizard_project_task_picking.py b/commown_devices/models/wizard_project_task_picking.py index 59873dbdc..8f21f9132 100644 --- a/commown_devices/models/wizard_project_task_picking.py +++ b/commown_devices/models/wizard_project_task_picking.py @@ -265,9 +265,12 @@ class ProjectTaskNoTrackingOutwardPickingWizard(models.TransientModel): ) @api.multi - def create_picking(self, do_transfer=False): + def create_picking(self): return self.task_id.contract_id.send_device_tracking_none( - self.variant_id, date=self.date, do_transfer=do_transfer + self.variant_id, + origin=self.task_id.get_id_name(), + date=self.date, + do_transfer=False, ) @@ -283,11 +286,11 @@ class ProjectTaskNoTrackingInwardPickingWizard(models.TransientModel): ) @api.multi - def create_picking(self, do_transfer=False): + def create_picking(self): return self.task_id.contract_id.receive_device_tracking_none( self.variant_id, self.env.ref("commown_devices.stock_location_devices_to_check"), origin=self.task_id.get_id_name(), date=self.date, - do_transfer=do_transfer, + do_transfer=False, ) diff --git a/commown_devices/tests/test_project_task.py b/commown_devices/tests/test_project_task.py index 232eb76cc..2661e8cc3 100644 --- a/commown_devices/tests/test_project_task.py +++ b/commown_devices/tests/test_project_task.py @@ -2,10 +2,21 @@ from odoo.exceptions import ValidationError +from ..models.common import do_new_transfer from .common import DeviceAsAServiceTC class ProjectTaskPickingTC(DeviceAsAServiceTC): + def _create_xml_id(self, record, name): + return self.env["ir.model.data"].create( + { + "module": "commown_devices", + "name": name, + "model": record._name, + "res_id": record.id, + } + ) + def setUp(self): super().setUp() @@ -39,6 +50,28 @@ def setUp(self): "contract_id": self.c1.id, } ) # for checks on stage change tests + self.task_test_checks2 = self.env["project.task"].create( + { + "name": "test task 2", + "project_id": self.project.id, + "contract_id": self.c2.id, + } + ) # for checks on stage change tests + + self.ongoing_stage = self.env["project.task.type"].create( + {"name": "Test Ongoing Stage"} + ) + self._create_xml_id(self.ongoing_stage, "sup_picking_ongoing_stage") + + # Create stage to assign xml_ids so constrains on stage_id pass + t1, t2 = self.env["project.task.type"].create( + [ + {"name": "t1"}, + {"name": "t2"}, + ] + ) + self._create_xml_id(t1, "diagnostic_stage") + self._create_xml_id(t2, "resiliated_stage") # Create a unused product and an unused service self.env["product.template"].create( @@ -390,10 +423,12 @@ def test_outward_inward_notracking(self): ) .location_id ) - wizard = self.env["project.task.notracking.outward.picking.wizard"].create( - {"task_id": self.task.id, "date": date, "variant_id": pt_variant.id} - ) - wizard.create_picking(do_transfer=True) + wizard_outward = self.env[ + "project.task.notracking.outward.picking.wizard" + ].create({"task_id": self.task.id, "date": date, "variant_id": pt_variant.id}) + + picking = wizard_outward.create_picking() + do_new_transfer(picking, date) client_location = ( self.env["stock.quant"] @@ -410,7 +445,10 @@ def test_outward_inward_notracking(self): wizard_inward = self.env[ "project.task.notracking.inward.picking.wizard" ].create({"task_id": self.task.id, "date": date, "variant_id": pt_variant.id}) - wizard_inward.create_picking(do_transfer=True) + + picking = wizard_inward.create_picking() + do_new_transfer(picking, date) + final_location = ( self.env["stock.quant"] .search( @@ -425,3 +463,40 @@ def test_outward_inward_notracking(self): final_location, self.env.ref("commown_devices.stock_location_devices_to_check"), ) + + def test_change_task_check(self): + with self.assertRaises(ValidationError) as err: + self.task_test_checks.stage_id = self.ongoing_stage + self.assertEqual( + "Error while validating constraint\n\nThis task can not be moved forward. There are no picking linked to this task.\n", + err.exception.name, + ) + with self.assertRaises(ValidationError) as err2: + self.task_test_checks2.stage_id = self.ongoing_stage + self.assertEqual( + "Error while validating constraint\n\nThis task can not be moved forward. There are no picking linked to this task.\n", + err2.exception.name, + ) + + module = self.nontracked_product.product_variant_id + lot = self.storable_product.product_variant_id + + quant = self.env["stock.quant"].search( + [ + ("product_id", "=", lot.id), + ("quantity", ">", 0), + ("location_id.complete_name", "ilike", "WH/STOCK/"), + ] + )[0] + + self.task_test_checks.contract_id.send_device_tracking_none( + module, + origin=self.task_test_checks.get_id_name(), + ) + self.task_test_checks.stage_id = self.ongoing_stage + + self.task_test_checks2.contract_id.send_device( + quant, + origin=self.task_test_checks2.get_id_name(), + ) + self.task_test_checks2.stage_id = self.ongoing_stage