Skip to content

Commit

Permalink
[IMP] shopfloor: Refactor _write_destination_on_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-software-de committed Jan 15, 2025
1 parent 5c67bb1 commit f9be0e8
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 115 deletions.
19 changes: 19 additions & 0 deletions shopfloor/actions/stock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 Camptocamp SA (http://www.camptocamp.com)
# Copyright 2025 Michael Tietz (MT Software) <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import _, fields
from odoo.tools.float_utils import float_round
Expand Down Expand Up @@ -228,3 +229,21 @@ def no_putaway_available(self, picking_types, move_lines):
# when no putaway is found, the move line destination stays the
# default's of the picking type
return any(line.location_dest_id in base_locations for line in move_lines)

def _lock_lines(self, lines):
self._actions_for("lock").for_update(lines)

def _set_destination_on_lines(
self, lines, location_dest, unload=False, set_src_location=False
):
lines.package_level_id.location_dest_id = location_dest
lines.location_dest_id = location_dest

def _unload_package(self, lines):
lines.result_package_id = False

def set_locations_and_unload_lines(self, lines, location_dest, unload=False):
self._lock_lines(lines)
self._set_destination_on_lines(lines, location_dest)
if unload:
self._unload_package(lines)
15 changes: 6 additions & 9 deletions shopfloor/services/cluster_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,11 @@ def set_destination_all(self, picking_batch_id, barcode, confirmation=None):
return self._unload_end(batch, completion_info_popup=completion_info_popup)

def _unload_write_destination_on_lines(self, lines, location):
lines.write({"shopfloor_unloaded": True, "location_dest_id": location.id})
lines.package_level_id.location_dest_id = location
stock = self._actions_for("stock")
stock.set_locations_and_unload_lines(
lines, location, self.work.menu.unload_package_at_destination
)
lines.write({"shopfloor_unloaded": True})
for line in lines:
# We set the picking to done only when the last line is
# unloaded to avoid backorders.
Expand All @@ -1158,8 +1161,6 @@ def _unload_write_destination_on_lines(self, lines, location):
picking_lines = picking.mapped("move_line_ids")
if all(line.shopfloor_unloaded for line in picking_lines):
picking._action_done()
if self.work.menu.unload_package_at_destination:
lines.result_package_id = False

def _unload_end(self, batch, completion_info_popup=None):
"""Try to close the batch if all transfers are done.
Expand Down Expand Up @@ -1279,15 +1280,11 @@ def unload_scan_destination(
batch, package, lines, barcode, confirmation=confirmation
)

def _lock_lines(self, lines):
"""Lock move lines"""
self._actions_for("lock").for_update(lines)

def _unload_scan_destination_lines(
self, batch, package, lines, barcode, confirmation=None
):
# Lock move lines that will be updated
self._lock_lines(lines)
self._actions_for("lock").for_update(lines)
first_line = fields.first(lines)
scanned_location = self._actions_for("search").location_from_scan(barcode)
if not scanned_location:
Expand Down
5 changes: 2 additions & 3 deletions shopfloor/services/location_content_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,9 @@ def _find_transfer_move_lines(self, location):
)
return lines

# hook used in module shopfloor_checkout_sync
def _write_destination_on_lines(self, lines, location, package=None):
lines.location_dest_id = location
lines.package_level_id.location_dest_id = location
stock = self._actions_for("stock")
stock.set_locations_and_unload_lines(lines, location)
if package:
lines.result_package_id = package

Expand Down
5 changes: 4 additions & 1 deletion shopfloor/services/single_pack_transfer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2020-2021 Camptocamp SA (http://www.camptocamp.com)
# Copyright 2020-2021 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# Copyright 2020 Akretion (http://www.akretion.com)
# Copyright 2025 Michael Tietz (MT Software) <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields

Expand Down Expand Up @@ -268,8 +269,10 @@ def _router_validate_success(self, package_level):
def _set_destination_and_done(self, package_level, scanned_location):
# when writing the destination on the package level, it writes
# on the move lines
package_level.location_dest_id = scanned_location
stock = self._actions_for("stock")
stock.set_locations_and_unload_lines(
package_level.move_line_ids, scanned_location
)
stock.put_package_level_in_move(package_level)
stock.validate_moves(package_level.move_line_ids.move_id)

Expand Down
15 changes: 5 additions & 10 deletions shopfloor/services/zone_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ def _set_destination_package(self, move_line, quantity, package):
)
return (package_changed, response)
stock = self._actions_for("stock")
self._lock_lines(move_line)
stock._lock_lines(move_line)
try:
stock.mark_move_line_as_picked(
move_line, quantity, package, check_user=True
Expand Down Expand Up @@ -1591,11 +1591,10 @@ def set_destination_all(self, barcode, confirmation=None):
return self._set_destination_all_response(buffer_lines, message=message)

def _write_destination_on_lines(self, lines, location):
self._lock_lines(lines)
lines.location_dest_id = location
lines.package_level_id.location_dest_id = location
if self.work.menu.unload_package_at_destination:
lines.result_package_id = False
stock = self._actions_for("stock")
stock.set_locations_and_unload_lines(
lines, location, unload=self.work.menu.unload_package_at_destination
)

def unload_split(self):
"""Indicates that now the buffer must be treated line per line
Expand Down Expand Up @@ -1676,10 +1675,6 @@ def unload_scan_pack(self, package_id, barcode):
unload_single_message=self.msg_store.barcode_no_match(package.name),
)

def _lock_lines(self, lines):
"""Lock move lines"""
self._actions_for("lock").for_update(lines)

def unload_set_destination(self, package_id, barcode, confirmation=None):
"""Scan the final destination for move lines in the buffer with the
destination package
Expand Down
1 change: 0 additions & 1 deletion shopfloor_checkout_sync/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import actions
from . import services
1 change: 1 addition & 0 deletions shopfloor_checkout_sync/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import checkout_sync
from . import stock
7 changes: 2 additions & 5 deletions shopfloor_checkout_sync/actions/checkout_sync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 Camptocamp SA
# Copyright 2025 Michael Tietz (MT Software) <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

from odoo.addons.component.core import Component
Expand Down Expand Up @@ -27,8 +28,4 @@ def _all_lines_to_lock(self, lines):
return lines

def _sync_checkout(self, lines, location):
moves = lines.mapped("move_id")
if self._has_to_sync_destination(lines):
dest_pickings = moves._moves_to_sync_checkout()
all_moves = self.env["stock.move"].union(*dest_pickings.values())
all_moves.sync_checkout_destination(location)
lines.move_id.sync_checkout_destination(location)
18 changes: 18 additions & 0 deletions shopfloor_checkout_sync/actions/stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2025 Michael Tietz (MT Software) <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.addons.component.core import Component


class StockAction(Component):
_inherit = "shopfloor.stock.action"

def _set_destination_on_lines(self, lines, location_dest):
checkout_sync = self._actions_for("checkout.sync")
checkout_sync._sync_checkout(lines, location_dest)
super()._set_destination_on_lines(lines, location_dest)

def set_locations_and_unload_lines(self, lines, location_dest, unload=False):
checkout_sync = self._actions_for("checkout.sync")
checkout_sync._all_lines_to_lock(lines)
super().set_locations_and_unload_lines(lines, location_dest, unload)
1 change: 1 addition & 0 deletions shopfloor_checkout_sync/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Guewen Baconnier <[email protected]>
* `Trobz <https://trobz.com>`_:
* Michael Tietz (MT Software) <[email protected]>
3 changes: 0 additions & 3 deletions shopfloor_checkout_sync/services/__init__.py

This file was deleted.

19 changes: 0 additions & 19 deletions shopfloor_checkout_sync/services/cluster_picking.py

This file was deleted.

19 changes: 0 additions & 19 deletions shopfloor_checkout_sync/services/location_content_transfer.py

This file was deleted.

19 changes: 0 additions & 19 deletions shopfloor_checkout_sync/services/zone_picking.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from odoo.addons.base_rest.components.service import to_int
from odoo.addons.component.core import Component
from odoo.addons.component.exception import NoComponentError
from odoo.addons.shopfloor.utils import to_float

_logger = logging.getLogger("shopfloor.services.single_product_transfer")
Expand Down Expand Up @@ -612,31 +611,9 @@ def _set_quantity__check_location(
move_line, message=message, asking_confirmation=confirmation or None
)

def _lock_lines(self, lines):
self._actions_for("lock").for_update(lines)

def _write_destination_on_lines(self, lines, location):
# TODO
# '_write_destination_on_lines' is implemented in:
#
# - 'location_content_transfer'
# - 'zone_picking'
# - 'cluster_picking' (but it is called '_unload_write_destination_on_lines')
#
# And all of them has a different implementation,
# To refactor later.
try:
# TODO lose dependency on 'shopfloor_checkout_sync' to avoid having
# yet another glue module. In the long term we should make
# 'shopfloor_checkout_sync' use events and trash the overrides made
# on all scenarios.
checkout_sync = self._actions_for("checkout.sync")
except NoComponentError:
self._lock_lines(lines)
else:
self._lock_lines(checkout_sync._all_lines_to_lock(lines))
checkout_sync._sync_checkout(lines, location)
lines.location_dest_id = location
stock = self._actions_for("stock")
stock.set_locations_and_unload_lines(lines, location)

def _set_quantity__post_move(self, move_line, location, confirmation=None):
# TODO qty_done = 0: transfer_no_qty_done
Expand Down Expand Up @@ -864,7 +841,7 @@ def set_quantity(self, selected_line_id, barcode, quantity, confirmation=None):
# TODO Should probably return to scan_product or scan_location?
return self._response_for_set_quantity(move_line)

self._lock_lines(move_line)
self._actions_for("stock")._lock_lines(move_line)
if move_line.state == "done":
message = self.msg_store.move_already_done()
return self._response_for_set_quantity(move_line, message=message)
Expand Down

0 comments on commit f9be0e8

Please sign in to comment.