Skip to content

Commit

Permalink
[IMP] rma: hook _check_entire_pack
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnauCForgeFlow committed Oct 22, 2024
1 parent 55ccd74 commit 0cfba0a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions rma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

from . import models
from . import wizards
from .hooks import post_load_hook
1 change: 1 addition & 0 deletions rma/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
],
"installable": True,
"application": True,
"post_load": "post_load_hook",
}
79 changes: 79 additions & 0 deletions rma/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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
!= self.env.ref("stock.stock_location_customers")
):
# 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

0 comments on commit 0cfba0a

Please sign in to comment.