Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] rma, add support to handle package used on customer moves #499

Open
wants to merge 3 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
}
78 changes: 78 additions & 0 deletions rma/hooks.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions rma/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from . import res_partner
from . import res_company
from . import res_config_settings
from . import stock_package_level
4 changes: 4 additions & 0 deletions rma/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ def _default_date_rma(self):
string="Under Warranty?", readonly=True, states={"draft": [("readonly", False)]}
)

def _get_stock_move_reference(self):
self.ensure_one()
return self.reference_move_id

def _prepare_rma_line_from_stock_move(self, sm, lot=False):
if not self.type:
self.type = self._get_default_type()
Expand Down
19 changes: 19 additions & 0 deletions rma/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,22 @@ def _update_reserved_quantity(
def _prepare_merge_moves_distinct_fields(self):
res = super()._prepare_merge_moves_distinct_fields()
return res + ["rma_line_id"]

def _prepare_procurement_values(self):
self.ensure_one()
res = super(StockMove, self)._prepare_procurement_values()
res["rma_line_id"] = self.rma_line_id.id
return res


class StockMoveLine(models.Model):

_inherit = "stock.move.line"

def _should_bypass_reservation(self, location):
res = super(StockMoveLine, self)._should_bypass_reservation(location)
if self.env.context.get(
"force_no_bypass_reservation"
) and location.usage not in ("customer", "supplier"):
return False
return res
20 changes: 20 additions & 0 deletions rma/models/stock_package_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from odoo import models


class StockPackageLevel(models.Model):

_inherit = "stock.package_level"

def write(self, values):
ctx = self.env.context.copy()
if (
len(self) == 1
and "location_dest_id" in values
and self.location_dest_id.id == values.get("location_dest_id")
):
ctx.update(
{
"bypass_reservation_update": True,
}
)
return super(StockPackageLevel, self.with_context(**ctx)).write(values)
Loading
Loading