From dd782122974f29565d9f6f844e600e871c527758 Mon Sep 17 00:00:00 2001 From: ArnauCForgeFlow Date: Tue, 22 Oct 2024 17:20:54 +0200 Subject: [PATCH] [IMP] rma: hook _check_entire_pack --- rma/__init__.py | 1 + rma/__manifest__.py | 1 + rma/hooks.py | 78 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 rma/hooks.py diff --git a/rma/__init__.py b/rma/__init__.py index 79030e0e..1e997d12 100644 --- a/rma/__init__.py +++ b/rma/__init__.py @@ -3,3 +3,4 @@ from . import models from . import wizards +from .hooks import post_load_hook diff --git a/rma/__manifest__.py b/rma/__manifest__.py index 015ed11d..4d193fec 100644 --- a/rma/__manifest__.py +++ b/rma/__manifest__.py @@ -36,4 +36,5 @@ ], "installable": True, "application": True, + "post_load": "post_load_hook", } diff --git a/rma/hooks.py b/rma/hooks.py new file mode 100644 index 00000000..ad500a35 --- /dev/null +++ b/rma/hooks.py @@ -0,0 +1,78 @@ +# Copyright 2024 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.addons.stock.models.stock_picking import Picking + + +# flake8: noqa: C901 +def post_load_hook(): + def _check_entire_pack(self): + """This function check if entire packs are moved in the picking""" + for picking in self: + origin_packages = picking.move_line_ids.mapped("package_id") + for pack in origin_packages: + if picking._check_move_lines_map_quant_package(pack): + package_level_ids = picking.package_level_ids.filtered( + lambda pl: pl.package_id == pack + ) + move_lines_to_pack = picking.move_line_ids.filtered( + lambda ml: ml.package_id == pack + and not ml.result_package_id + and ml.state not in ("done", "cancel") + ) + if not package_level_ids: + package_location = ( + self._get_entire_pack_location_dest(move_lines_to_pack) + or picking.location_dest_id.id + ) + self.env["stock.package_level"].with_context( + bypass_reservation_update=package_location + ).create( + { + "picking_id": picking.id, + "package_id": pack.id, + "location_id": pack.location_id.id, + "location_dest_id": package_location, + "move_line_ids": [(6, 0, move_lines_to_pack.ids)], + "company_id": picking.company_id.id, + } + ) + # Propagate the result package in the next move for disposable packages only. + # Start Hook + if ( + pack.package_use == "disposable" + and pack.location_id.usage != "customer" + ): + # End Hook + move_lines_to_pack.with_context( + bypass_reservation_update=package_location + ).write({"result_package_id": pack.id}) + + else: + move_lines_in_package_level = move_lines_to_pack.filtered( + lambda ml: ml.move_id.package_level_id + ) + move_lines_without_package_level = ( + move_lines_to_pack - move_lines_in_package_level + ) + + if pack.package_use == "disposable": + ( + move_lines_in_package_level + | move_lines_without_package_level + ).result_package_id = pack + move_lines_in_package_level.result_package_id = pack + for ml in move_lines_in_package_level: + ml.package_level_id = ml.move_id.package_level_id.id + + move_lines_without_package_level.package_level_id = ( + package_level_ids[0] + ) + for pl in package_level_ids: + pl.location_dest_id = ( + self._get_entire_pack_location_dest(pl.move_line_ids) + or picking.location_dest_id.id + ) + + if not hasattr(Picking, "_check_entire_pack_original"): + Picking._check_entire_pack_original = Picking._check_entire_pack + Picking._check_entire_pack = _check_entire_pack