Skip to content

Commit

Permalink
[ADD][REF] shipment_advice
Browse files Browse the repository at this point in the history
- modified shipment_advice, so it has validate_when_fully_done method that sets shipment to done if its moves are in "done" state, updated tests.
  • Loading branch information
vnikolayev1 committed May 29, 2024
1 parent 6c247ac commit 476b56b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 19 deletions.
25 changes: 19 additions & 6 deletions shipment_advice/models/shipment_advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,27 @@ def action_done(self):
# set to fullfill the need => validate
picking._action_done()
# Unplan moves that were not loaded and validated
moves_to_unplan = self.loaded_move_line_ids.move_id.filtered(
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"):
Expand Down
2 changes: 1 addition & 1 deletion shipment_advice/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _plan_records_in_shipment(self, shipment_advice, 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,
Expand Down
92 changes: 80 additions & 12 deletions shipment_advice/tests/test_shipment_advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class TestShipmentAdvice(Common):
def setUpClass(cls):
super().setUpClass()

def _prepare_picking_with_two_packages(self):
def _prepare_picking_with_three_packages(self):
# Prepare packages & products
package2 = self.env["stock.quant.package"].create({"name": "PKG_OUT2"})
package3 = self.env["stock.quant.package"].create({"name": "PKG_OUT3"})
package4 = self.env["stock.quant.package"].create({"name": "PKG_OUT4"})
self.env["stock.quant"]._update_available_quantity(
self.product_out2,
self.picking_type_out.default_location_src_id,
Expand All @@ -28,6 +29,12 @@ def _prepare_picking_with_two_packages(self):
5,
package_id=package3,
)
self.env["stock.quant"]._update_available_quantity(
self.product_out1,
self.picking_type_out.default_location_src_id,
5,
package_id=package4,
)
# Prepare moves (belonging to the same transfer)
group = self.env["procurement.group"].create({})
move_product_out2_2 = self._create_move(
Expand All @@ -38,6 +45,10 @@ def _prepare_picking_with_two_packages(self):
self.picking_type_out, self.product_out3, 5, group
)
self.assertEqual(move_product_out3_2.move_line_ids.package_id, package3)
move_product_out4_2 = self._create_move(
self.picking_type_out, self.product_out1, 5, group
)
self.assertEqual(move_product_out4_2.move_line_ids.package_id, package4)
return move_product_out2_2.picking_id

def test_shipment_advice_confirm(self):
Expand Down Expand Up @@ -156,8 +167,8 @@ def test_multi_shipment_advice_done_backorder_policy_disabled(self):
shipment_advice_out2 = self.env["shipment.advice"].create(
{"shipment_type": "outgoing"}
)
picking = self._prepare_picking_with_two_packages()
line1, line2 = picking.move_line_ids
picking = self._prepare_picking_with_three_packages()
line1, line2, line3 = picking.move_line_ids
# Load first package in the first shipment advice
pl1 = line1.package_level_id
self._in_progress_shipment_advice(self.shipment_advice_out)
Expand All @@ -166,12 +177,16 @@ def test_multi_shipment_advice_done_backorder_policy_disabled(self):
self.shipment_advice_out.action_done()
self.assertEqual(self.shipment_advice_out.state, "done")
self.assertEqual(picking.state, "assigned")
# Load second package in the second shipment advice
# Load first package in the second shipment advice
pl2 = line2.package_level_id
self._in_progress_shipment_advice(shipment_advice_out2)
self._load_records_in_shipment(shipment_advice_out2, pl2)
# Validate the second shipment advice: delivery order has now been validated
pl3 = line3.package_level_id
# Load second package in the second shipment advice
self._load_records_in_shipment(shipment_advice_out2, pl3)
# We only call action done when all packages loaded
shipment_advice_out2.action_done()
# Validate the second shipment advice: delivery order has now been validated
self.assertEqual(shipment_advice_out2.state, "done")
self.assertEqual(picking.state, "done")

Expand Down Expand Up @@ -206,30 +221,34 @@ def test_shipment_advice_done_backorder_policy_enabled(self):

def test_assign_lines_to_multiple_shipment_advices(self):
"""Assign lines of a transfer to different shipment advices.
1. Load two packages in two different shipment advices
1. Load one and two packages in two different shipment advices
2. Validate the first shipment advice: delivery order is not yet validated
3. Validate the second shipment advice: delivery order is now well validated
"""
# Prepare a transfer to load in two shipment advices
shipment_advice_out2 = self.env["shipment.advice"].create(
{"shipment_type": "outgoing"}
)
picking = self._prepare_picking_with_two_packages()
line1, line2 = picking.move_line_ids
# Load packages in different shipment advices
picking = self._prepare_picking_with_three_packages()
line1, line2, line3 = picking.move_line_ids
# Load one package in shipment advice 1
pl1 = line1.package_level_id
self._in_progress_shipment_advice(self.shipment_advice_out)
self._load_records_in_shipment(self.shipment_advice_out, pl1)
# Load two packages in shipment advice 2
pl2 = line2.package_level_id
self._in_progress_shipment_advice(shipment_advice_out2)
self._load_records_in_shipment(shipment_advice_out2, pl2)
# Validate the first shipment advice: delivery order hasn't been validated
pl3 = line3.package_level_id
self._load_records_in_shipment(shipment_advice_out2, pl3)
# Setting shipment advice 1 to done, checking if picking is still not done
self.shipment_advice_out.action_done()
# Validate the first shipment advice: delivery order hasn't been validated
self.assertEqual(self.shipment_advice_out.state, "done")
self.assertEqual(picking.state, "assigned")
# Validate the second shipment advice: delivery order has now been validated
# We only call action done when all packages loaded
shipment_advice_out2.action_done()
# Validate the second shipment advice: delivery order has now been validated
self.assertEqual(shipment_advice_out2.state, "done")
self.assertEqual(picking.state, "done")

Expand All @@ -246,3 +265,52 @@ 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_shipment_advice_recieve_partial_move_backorder(self):
picking = self.move_product_in1.picking_id
# Plan a move
self._plan_records_in_shipment(self.shipment_advice_in, self.move_product_in1)
self._in_progress_shipment_advice(self.shipment_advice_in)
# Partially receive the move and create a backorder
for ml in self.move_product_in1.move_line_ids:
ml.qty_done = ml.product_uom_qty - 2
wizard = (
self.env["stock.backorder.confirmation"]
.with_context(button_validate_picking_ids=picking.ids)
.create({"pick_ids": [(6, 0, picking.ids)]})
)
wizard.process()
# New move created should not be included in the processing shipment
backorder_picking = self.env["stock.picking"].search(
[("backorder_id", "=", picking.id)]
)
self.assertFalse(backorder_picking.move_lines.shipment_advice_id)

def test_validate_shipment_advice_when_fully_done(self):
shipment_advice_out2 = self.env["shipment.advice"].create(
{"shipment_type": "outgoing"}
)
picking = self._prepare_picking_with_three_packages()
line1, line2, line3 = picking.move_line_ids
# Load first package in shipment advice 2
pl2 = line2.package_level_id
self._in_progress_shipment_advice(shipment_advice_out2)
self._load_records_in_shipment(shipment_advice_out2, pl2)
pl3 = line3.package_level_id
# Load second package in the second shipment advice
self._load_records_in_shipment(shipment_advice_out2, pl3)
# Assign planned move_ids that are assigned to test function
shipment_advice_out2.planned_move_ids = (
shipment_advice_out2.loaded_move_line_ids.move_id
)
# Validating if moves in "incorrect" states do not trigger _action_done()
shipment_advice_out2.validate_when_fully_done()
self.assertEqual(shipment_advice_out2.state, "in_progress")
# Validating if moves in one "incorrect" state do not trigger _action_done()
shipment_advice_out2.planned_move_ids[0].state = "done"
shipment_advice_out2.validate_when_fully_done()
self.assertEqual(shipment_advice_out2.state, "in_progress")
# Validating if all moves in "correct" state trigger _action_done()
shipment_advice_out2.planned_move_ids[1].state = "done"
shipment_advice_out2.validate_when_fully_done()
self.assertEqual(shipment_advice_out2.state, "done")

0 comments on commit 476b56b

Please sign in to comment.