Skip to content

Commit

Permalink
[ADD][IMP] shipment_advice
Browse files Browse the repository at this point in the history
- 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, covered backorder.
  • Loading branch information
vnikolayev1 committed Jun 20, 2024
1 parent 6c247ac commit 0a8d74a
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions shipment_advice/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ class ResCompany(models.Model):
"deliveries will be shipped by several trucks."
),
)
shipment_advice_auto_validate = fields.Boolean(string="Shipment Advice: Auto Validate")
4 changes: 4 additions & 0 deletions shipment_advice/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ 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
)
21 changes: 19 additions & 2 deletions shipment_advice/models/shipment_advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
10 changes: 10 additions & 0 deletions shipment_advice/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ class StockMove(models.Model):
def _plan_in_shipment(self, shipment_advice):
"""Plan the moves into the given shipment advice."""
self.shipment_advice_id = shipment_advice

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
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
50 changes: 50 additions & 0 deletions shipment_advice/tests/test_shipment_advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,53 @@ 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):
shipment_advice_in2 = self.env["shipment.advice"].create(
{"shipment_type": "incoming"}
)
picking = self._prepare_picking_with_two_packages()
line1, line2 = picking.move_line_ids
# Load first package in shipment advice 2
pl1 = line1.package_level_id
self._in_progress_shipment_advice(self.shipment_advice_in)
self._in_progress_shipment_advice(shipment_advice_in2)
self.assertEqual(self.shipment_advice_in.state, "in_progress")
self._load_records_in_shipment(self.shipment_advice_in, pl1)
pl2 = line2.package_level_id
# Load second package into shipment advice
self._load_records_in_shipment(shipment_advice_in2, pl2)
# Assign planned move_ids that are assigned to test function
self.shipment_advice_in.planned_move_ids = line1.move_id
self.shipment_advice_in.planned_move_ids = line2.move_id
self.assertEqual(line1.move_id.state, "assigned")
self.assertEqual(line2.move_id.state, "assigned")
self.assertEqual(self.shipment_advice_in.state, "in_progress")
self.assertEqual(shipment_advice_in2.state, "in_progress")
# Setting autovalidate shipment advice company
self.shipment_advice_in.company_id.shipment_advice_auto_validate = True
# Assigning picking
picking.action_assign()
self.assertEqual(picking.state, "assigned")
# Making one line to not done to create backorder
picking.move_lines[0].move_line_ids[0].qty_done = 0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertEqual(
picking.move_line_ids.move_id.shipment_advice_id.state, "done")
# Testing backorder
backorder = self.env['stock.picking'].search([('backorder_id', '=', picking.id)])
shipment_advice_in3 = self.env["shipment.advice"].create(
{"shipment_type": "incoming"}
)
back_line1 = backorder.move_line_ids
self._in_progress_shipment_advice(shipment_advice_in3)
self._load_records_in_shipment(
shipment_advice_in3, back_line1.package_level_id)
shipment_advice_in3.planned_move_ids = back_line1.move_id
self.assertEqual(back_line1.move_id.state, "assigned")
self.assertEqual(shipment_advice_in3.state, "in_progress")
backorder._action_done()
self.assertEqual(backorder.state, "done")
self.assertEqual(
backorder.move_line_ids.move_id.shipment_advice_id.state, "done")
11 changes: 11 additions & 0 deletions shipment_advice/views/res_config_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
<field name="shipment_advice_outgoing_backorder_policy" />
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="shipment_advice_auto_validate"/>
</div>
<div class="o_setting_right_pane">
<label for="shipment_advice_auto_validate"/>
<div class="text-muted">
Validates shipment advice automatically whenever stock move is done
</div>
</div>
</div>
</xpath>
</field>
</record>
Expand Down

0 comments on commit 0a8d74a

Please sign in to comment.