From e03b20ecbc95b0100ae31bad80988ec956570282 Mon Sep 17 00:00:00 2001 From: vnikolayev1 Date: Fri, 21 Jun 2024 17:09:27 +0300 Subject: [PATCH] [14.0][IMP] shipment_advice: Autovalidate - modified shipment_advice, so whenever we set stock move to done, and if autovalidate shipment advice is enabled in company settings - we set shipment advice to done - updated tests. --- shipment_advice/models/res_company.py | 3 ++ shipment_advice/models/res_config_settings.py | 3 ++ shipment_advice/models/shipment_advice.py | 21 +++++++++- shipment_advice/models/stock_move.py | 13 +++++++ shipment_advice/tests/common.py | 2 +- shipment_advice/tests/test_shipment_advice.py | 38 +++++++++++++++++++ shipment_advice/views/res_config_settings.xml | 11 ++++++ 7 files changed, 88 insertions(+), 3 deletions(-) diff --git a/shipment_advice/models/res_company.py b/shipment_advice/models/res_company.py index df8c1ad0..d36fd933 100644 --- a/shipment_advice/models/res_company.py +++ b/shipment_advice/models/res_company.py @@ -23,3 +23,6 @@ class ResCompany(models.Model): "deliveries will be shipped by several trucks." ), ) + shipment_advice_auto_validate = fields.Boolean( + string="Shipment Advice: Auto Validate" + ) diff --git a/shipment_advice/models/res_config_settings.py b/shipment_advice/models/res_config_settings.py index 1bb6a241..cd22363e 100644 --- a/shipment_advice/models/res_config_settings.py +++ b/shipment_advice/models/res_config_settings.py @@ -10,3 +10,6 @@ class ResConfigSettings(models.TransientModel): shipment_advice_outgoing_backorder_policy = fields.Selection( related="company_id.shipment_advice_outgoing_backorder_policy", readonly=False ) + shipment_advice_auto_validate = fields.Boolean( + related="company_id.shipment_advice_auto_validate", readonly=False + ) diff --git a/shipment_advice/models/shipment_advice.py b/shipment_advice/models/shipment_advice.py index bfa8f368..c9bf1ebd 100644 --- a/shipment_advice/models/shipment_advice.py +++ b/shipment_advice/models/shipment_advice.py @@ -432,10 +432,27 @@ def action_done(self): lambda m: m.state not in ("cancel", "done") and not m.quantity_done ) moves_to_unplan.shipment_advice_id = False - shipment.departure_date = fields.Datetime.now() - shipment.state = "done" + shipment._action_done() return True + def _action_done(self): + self.ensure_one() + self.write({"departure_date": fields.Datetime.now(), "state": "done"}) + + def _is_fully_done(self): + self.ensure_one() + for move in self.planned_move_ids: + if move.state not in ["cancel", "done"]: + return False + return True + + def validate_when_fully_done(self): + """set the shipment advice to done if all the planned_move_ids + are done or cancel""" + for shipment in self: + if shipment._is_fully_done(): + shipment._action_done() + def action_cancel(self): for shipment in self: if shipment.state not in ("confirmed", "in_progress"): diff --git a/shipment_advice/models/stock_move.py b/shipment_advice/models/stock_move.py index 0fe033d6..dcb68a70 100644 --- a/shipment_advice/models/stock_move.py +++ b/shipment_advice/models/stock_move.py @@ -12,6 +12,7 @@ class StockMove(models.Model): ondelete="set null", string="Planned shipment", index=True, + copy=False, ) def _plan_in_shipment(self, shipment_advice): @@ -23,3 +24,15 @@ def _prepare_merge_moves_distinct_fields(self): # Avoid having stock move assign to different shipment merged together res.append("shipment_advice_id") return res + + def _action_done(self, cancel_backorder=False): + res = super()._action_done(cancel_backorder=cancel_backorder) + shipment_advices = res.shipment_advice_id + for shipment_advice in shipment_advices: + if ( + shipment_advice.shipment_type != "incoming" + or not shipment_advice.company_id.shipment_advice_auto_validate + ): + continue + shipment_advice.validate_when_fully_done() + return res diff --git a/shipment_advice/tests/common.py b/shipment_advice/tests/common.py index 818e4788..ae3e2c5f 100644 --- a/shipment_advice/tests/common.py +++ b/shipment_advice/tests/common.py @@ -165,7 +165,7 @@ def _unplan_records_from_shipment(self, records, user=None): return wiz def _load_records_in_shipment(self, shipment_advice, records, user=None): - """Load pickings, move lines or package levels in the givent shipment.""" + """Load pickings, move lines or package levels in the given shipment.""" wiz_model = self.env["wizard.load.shipment"].with_context( active_model=records._name, active_ids=records.ids, diff --git a/shipment_advice/tests/test_shipment_advice.py b/shipment_advice/tests/test_shipment_advice.py index 6e6ad581..cb81a768 100644 --- a/shipment_advice/tests/test_shipment_advice.py +++ b/shipment_advice/tests/test_shipment_advice.py @@ -246,3 +246,41 @@ def test_shipment_advice_draft(self): def test_shipment_name(self): self.assertTrue("OUT" in self.shipment_advice_out.name) self.assertTrue("IN" in self.shipment_advice_in.name) + + def test_auto_validate_shipment_advice(self): + # Creating two pickings with two move lines + picking1 = self._prepare_picking_with_two_packages() + picking2 = self._prepare_picking_with_two_packages() + picking1_line1, picking1_line2 = picking1.move_line_ids + picking2_line1, picking2_line2 = picking2.move_line_ids + move_ids = (picking1.move_line_ids + picking2.move_line_ids).move_id + package_levels = ( + picking1.move_line_ids + picking2.move_line_ids + ).package_level_id + # Load packages from both pickings into shipment advice + self._in_progress_shipment_advice(self.shipment_advice_in) + self._load_records_in_shipment(self.shipment_advice_in, package_levels) + # Assign planned move_ids that are assigned to test function + self.shipment_advice_in.planned_move_ids = move_ids + self.assertEqual(picking1_line1.move_id.state, "assigned") + self.assertEqual(picking1_line2.move_id.state, "assigned") + self.assertEqual(picking2_line1.move_id.state, "assigned") + self.assertEqual(picking2_line2.move_id.state, "assigned") + self.assertEqual(self.shipment_advice_in.state, "in_progress") + # Setting autovalidate shipment advice company + self.shipment_advice_in.company_id.shipment_advice_auto_validate = True + # Assigning picking1 + picking1.action_assign() + self.assertEqual(picking1.state, "assigned") + # Completing picking, advice should still be in progress because of second picking + picking1._action_done() + self.assertEqual(picking2.state, "assigned") + self.assertEqual(picking1.state, "done") + self.assertEqual(self.shipment_advice_in.state, "in_progress") + # Assigning picking2 + picking2.action_assign() + self.assertEqual(picking2.state, "assigned") + # Completing picking2, advice should be done because all moves are done + picking2._action_done() + self.assertEqual(picking2.state, "done") + self.assertEqual(self.shipment_advice_in.state, "done") diff --git a/shipment_advice/views/res_config_settings.xml b/shipment_advice/views/res_config_settings.xml index 2b747f38..f30a794e 100644 --- a/shipment_advice/views/res_config_settings.xml +++ b/shipment_advice/views/res_config_settings.xml @@ -27,6 +27,17 @@ +
+
+ +
+
+
+