From 6ac2d67049431586df6b41c50554adcd58277839 Mon Sep 17 00:00:00 2001 From: Thierry Ducrest Date: Tue, 17 Oct 2023 12:07:00 +0200 Subject: [PATCH] shpfloor: use catch_error decorator in zp, spt --- shopfloor/services/single_pack_transfer.py | 11 +++++++- shopfloor/services/zone_picking.py | 25 +++++++++++++++---- shopfloor/tests/test_single_pack_transfer.py | 18 ------------- .../tests/test_single_pack_transfer_base.py | 18 +++++++++++++ 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/shopfloor/services/single_pack_transfer.py b/shopfloor/services/single_pack_transfer.py index 64e2bfd3ec..39eaa5bc02 100644 --- a/shopfloor/services/single_pack_transfer.py +++ b/shopfloor/services/single_pack_transfer.py @@ -6,6 +6,7 @@ from odoo.addons.base_rest.components.service import to_int from odoo.addons.component.core import Component +from odoo.addons.shopfloor_base.utils import catch_errors class SinglePackTransfer(Component): @@ -205,10 +206,18 @@ def _create_package_level(self, package): def _is_move_state_valid(self, moves): return all(move.state != "cancel" for move in moves) + def _validate_catch_error( + self, package_level_id, location_barcode, confirmation=False, message=None + ): + package_level = self.env["stock.package_level"].browse(package_level_id) + return self._response_for_scan_location( + package_level, message=message, confirmation_required=confirmation + ) + + @catch_errors(_validate_catch_error) def validate(self, package_level_id, location_barcode, confirmation=False): """Validate the transfer""" search = self._actions_for("search") - package_level = self.env["stock.package_level"].browse(package_level_id) if not package_level.exists(): return self._response_for_start( diff --git a/shopfloor/services/zone_picking.py b/shopfloor/services/zone_picking.py index d73283f13b..4c55e94fc2 100644 --- a/shopfloor/services/zone_picking.py +++ b/shopfloor/services/zone_picking.py @@ -10,6 +10,7 @@ from odoo.addons.base_rest.components.service import to_bool, to_int from odoo.addons.component.core import Component +from odoo.addons.shopfloor_base.utils import catch_errors from ..exceptions import ConcurentWorkOnTransfer from ..utils import to_float @@ -1075,6 +1076,21 @@ def _set_destination_update_quantity(self, move_line, quantity, barcode): return response return response + def _set_destination_catch_error( + self, + move_line_id, + barcode, + quantity, + confirmation=False, + handle_complete_mix_pack=False, + message=None, + ): + move_line = self.env["stock.move.line"].browse(move_line_id) + return self._response_for_set_line_destination( + move_line, message=message, confirmation_required=confirmation + ) + + @catch_errors(_set_destination_catch_error) # flake8: noqa: C901 def set_destination( self, @@ -1535,17 +1551,16 @@ def set_destination_all(self, barcode, confirmation=False): message = None buffer_lines = self._find_buffer_move_lines() if location: - error = None location_dest = buffer_lines.mapped("location_dest_id") # check if move lines share the same destination if len(location_dest) != 1: - error = self.msg_store.lines_different_dest_location() + message = self.msg_store.lines_different_dest_location() # check if the scanned location is allowed moves = buffer_lines.mapped("move_id") if not self.is_dest_location_valid(moves, location): - error = self.msg_store.location_not_allowed() - if error: - return self._set_destination_all_response(buffer_lines, message=error) + message = self.msg_store.location_not_allowed() + if message: + return self._set_destination_all_response(buffer_lines, message=message) # check if the destination location is not the expected one # - OK if the scanned destination is a child of the current # destination set on buffer lines diff --git a/shopfloor/tests/test_single_pack_transfer.py b/shopfloor/tests/test_single_pack_transfer.py index 08bb5bca41..7ab3c85156 100644 --- a/shopfloor/tests/test_single_pack_transfer.py +++ b/shopfloor/tests/test_single_pack_transfer.py @@ -2,8 +2,6 @@ # Copyright 2020 Akretion (http://www.akretion.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo.tests.common import Form - from .test_single_pack_transfer_base import SinglePackTransferCommonBase @@ -46,22 +44,6 @@ def setUpClassBaseData(cls, *args, **kwargs): lines=[(cls.product_a, 1), (cls.product_b, 1)] ) - @classmethod - def _create_initial_move(cls, lines): - """Create the move to satisfy the pre-condition before /start""" - picking_form = Form(cls.env["stock.picking"]) - picking_form.picking_type_id = cls.picking_type - picking_form.location_id = cls.stock_location - picking_form.location_dest_id = cls.shelf2 - for line in lines: - with picking_form.move_ids_without_package.new() as move: - move.product_id = line[0] - move.product_uom_qty = line[1] - picking = picking_form.save() - picking.action_confirm() - picking.action_assign() - return picking - def _simulate_started(self, package): """Replicate what the /start endpoint would do on the given package. diff --git a/shopfloor/tests/test_single_pack_transfer_base.py b/shopfloor/tests/test_single_pack_transfer_base.py index 2fd243f50e..e1f2ea3c17 100644 --- a/shopfloor/tests/test_single_pack_transfer_base.py +++ b/shopfloor/tests/test_single_pack_transfer_base.py @@ -1,6 +1,8 @@ # Copyright 2020 Camptocamp SA (http://www.camptocamp.com) # Copyright 2020 Akretion (http://www.akretion.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo.tests.common import Form + from .common import CommonCase @@ -28,3 +30,19 @@ def setUp(self): self.service = self.get_service( "single_pack_transfer", menu=self.menu, profile=self.profile ) + + @classmethod + def _create_initial_move(cls, lines): + """Create the move to satisfy the pre-condition before /start""" + picking_form = Form(cls.env["stock.picking"]) + picking_form.picking_type_id = cls.picking_type + picking_form.location_id = cls.stock_location + picking_form.location_dest_id = cls.shelf2 + for line in lines: + with picking_form.move_ids_without_package.new() as move: + move.product_id = line[0] + move.product_uom_qty = line[1] + picking = picking_form.save() + picking.action_confirm() + picking.action_assign() + return picking