Skip to content

Commit

Permalink
[FIX] shipment_advice: Refactor action_done use just one record inste…
Browse files Browse the repository at this point in the history
…ad of the recordset
  • Loading branch information
mt-software-de committed Jul 11, 2024
1 parent cda1ae7 commit 18ba93d
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions shipment_advice/models/shipment_advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,42 @@ def _lock_records(self, records):
sql = "SELECT id FROM %s WHERE ID IN %%s FOR UPDATE" % records._table
self.env.cr.execute(sql, (tuple(records.ids),), log_exceptions=False)

def action_done(self):
def _close_pickings(self):
"""Validate transfers (create backorders for unprocessed lines)"""
self.ensure_one()
wiz_model = self.env["stock.backorder.confirmation"]
pickings = self.env["stock.picking"]
create_backorder = True
if self.shipment_type == "incoming":
self._lock_records(self.planned_picking_ids)
pickings = self.planned_picking_ids
else:
self._lock_records(self.loaded_picking_ids)
pickings = self.to_validate_picking_ids
create_backorder = (
self.company_id.shipment_advice_outgoing_backorder_policy
== "create_backorder"
)
for picking in pickings:
if picking.state in ("cancel", "done"):
continue
if picking._check_backorder():
if not create_backorder:
continue
wiz = wiz_model.create({})
wiz.pick_ids = picking
wiz.with_context(button_validate_picking_ids=picking.ids).process()
else:
picking._action_done()

def _unplan_loaded_moves(self):
"""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

def action_done(self):
shipment_advice_ids_to_validate = []
self = self.with_context(shipment_advice_ignore_auto_close=True)
for shipment in self:
Expand All @@ -391,50 +425,9 @@ def action_done(self):
shipment.name
)
)
# Validate transfers (create backorders for unprocessed lines)
if shipment.shipment_type == "incoming":
self._lock_records(self.planned_picking_ids)
for picking in self.planned_picking_ids:
if picking.state in ("cancel", "done"):
continue
if picking._check_backorder():
wiz = wiz_model.create({})
wiz.pick_ids = picking
wiz.with_context(
button_validate_picking_ids=picking.ids
).process()
else:
picking._action_done()
else:
backorder_policy = (
shipment.company_id.shipment_advice_outgoing_backorder_policy
)
self._lock_records(self.loaded_picking_ids)
if backorder_policy == "create_backorder":
for picking in self.to_validate_picking_ids:
if picking.state in ("cancel", "done"):
continue
if picking._check_backorder():
wiz = wiz_model.create({})
wiz.pick_ids = picking
wiz.with_context(
button_validate_picking_ids=picking.ids
).process()
else:
picking._action_done()
else:
for picking in self.to_validate_picking_ids:
if picking.state in ("cancel", "done"):
continue
if not picking._check_backorder():
# no backorder needed means that all qty_done are
# 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._close_pickings()
if shipment.shipment_type == "outgoing":
shipment._unplan_loaded_moves()
shipment_advice_ids_to_validate.append(shipment.id)
if shipment_advice_ids_to_validate:
self.browse(shipment_advice_ids_to_validate)._action_done()
Expand Down

0 comments on commit 18ba93d

Please sign in to comment.