From 38719ebf215b02790dd3a92afd10354d306c1518 Mon Sep 17 00:00:00 2001 From: BernatPForgeFlow Date: Thu, 17 Aug 2023 12:38:57 +0200 Subject: [PATCH 1/3] [IMP] rma: Auto-calculate package for serial products When returning or delivering a serial product from a RMA, we will calculate by default its package. Later, it can be modyfied by the user. --- rma/wizards/rma_make_picking.py | 86 +++++++++++++++++---------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/rma/wizards/rma_make_picking.py b/rma/wizards/rma_make_picking.py index 0b92b1d8..0526ce7f 100644 --- a/rma/wizards/rma_make_picking.py +++ b/rma/wizards/rma_make_picking.py @@ -210,49 +210,51 @@ class RmaMakePicking(models.TransientModel): else: pickings = self.mapped("item_ids.line_id")._get_in_pickings() action = self.item_ids.line_id.action_view_in_shipments() - if picking_type == "incoming": + # Force the reservation of the RMA specific lot for incoming shipments. + # FIXME: still needs fixing, not reserving appropriate serials. + for move in pickings.move_lines.filtered( + lambda x: x.state not in ("draft", "cancel", "done", "waiting") + and x.rma_line_id + and x.product_id.tracking in ("lot", "serial") + and x.rma_line_id.lot_id + ): # Force the reservation of the RMA specific lot for incoming shipments. - # FIXME: still needs fixing, not reserving appropriate serials. - for move in pickings.move_ids.filtered( - lambda x: x.state not in ("draft", "cancel", "done", "waiting") - and x.rma_line_id - and x.product_id.tracking in ("lot", "serial") - and x.rma_line_id.lot_id - ): - # Force the reservation of the RMA specific lot for incoming shipments. - move.move_line_ids.unlink() - if move.product_id.tracking == "serial": - move.write( - { - "lot_ids": [(6, 0, move.rma_line_id.lot_id.ids)], - } - ) - move.move_line_ids.write( - { - "reserved_uom_qty": 1, - "qty_done": 0, - } - ) - elif move.product_id.tracking == "lot": - if picking_type == "incoming": - qty = self.item_ids.filtered( - lambda x: x.line_id.id == move.rma_line_id.id - ).qty_to_receive - else: - qty = self.item_ids.filtered( - lambda x: x.line_id.id == move.rma_line_id.id - ).qty_to_deliver - move_line_data = move._prepare_move_line_vals() - move_line_data.update( - { - "lot_id": move.rma_line_id.lot_id.id, - "product_uom_id": move.product_id.uom_id.id, - "qty_done": 0, - "reserved_uom_qty": qty, - } - ) - move_line_model.create(move_line_data) - + move.move_line_ids.unlink() + if move.product_id.tracking == "serial": + move.write( + { + "lot_ids": [(6, 0, move.rma_line_id.lot_id.ids)], + } + ) + quant = self.env["stock.quant"]._gather( + move.product_id, move.location_id, lot_id=move.rma_line_id.lot_id + ) + move.move_line_ids.write( + { + "product_uom_qty": 1 if picking_type == "incoming" else 0, + "qty_done": 0, + "package_id": quant.package_id.id if quant.package_id else None, + } + ) + elif move.product_id.tracking == "lot": + if picking_type == "incoming": + qty = self.item_ids.filtered( + lambda x: x.line_id.id == move.rma_line_id.id + ).qty_to_receive + else: + qty = self.item_ids.filtered( + lambda x: x.line_id.id == move.rma_line_id.id + ).qty_to_deliver + move_line_data = move._prepare_move_line_vals() + move_line_data.update( + { + "lot_id": move.rma_line_id.lot_id.id, + "product_uom_id": move.product_id.uom_id.id, + "qty_done": 0, + "product_uom_qty": qty if picking_type == "incoming" else 0, + } + ) + move_line_model.create(move_line_data) pickings.with_context(force_no_bypass_reservation=True).action_assign() return action From 73b104175ab706988fea5b51e9cf5d8a5198c41e Mon Sep 17 00:00:00 2001 From: Christopher Ormaza Date: Tue, 26 Sep 2023 15:42:33 -0500 Subject: [PATCH 2/3] [FIX] quants can be more than one on _gather function result --- rma/wizards/rma_make_picking.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rma/wizards/rma_make_picking.py b/rma/wizards/rma_make_picking.py index 0526ce7f..6f036b11 100644 --- a/rma/wizards/rma_make_picking.py +++ b/rma/wizards/rma_make_picking.py @@ -226,14 +226,14 @@ class RmaMakePicking(models.TransientModel): "lot_ids": [(6, 0, move.rma_line_id.lot_id.ids)], } ) - quant = self.env["stock.quant"]._gather( + quants = self.env["stock.quant"]._gather( move.product_id, move.location_id, lot_id=move.rma_line_id.lot_id ) move.move_line_ids.write( { "product_uom_qty": 1 if picking_type == "incoming" else 0, "qty_done": 0, - "package_id": quant.package_id.id if quant.package_id else None, + "package_id": len(quants) == 1 and quants.package_id.id, } ) elif move.product_id.tracking == "lot": From e43156e0270114e105f41773232457bfcc52173d Mon Sep 17 00:00:00 2001 From: BernatPForgeFlow Date: Wed, 25 Oct 2023 11:17:12 +0200 Subject: [PATCH 3/3] [FIX] rma: Do not set 'Destination Package' if RMA package comes from 'Customers' location --- rma/models/__init__.py | 1 + rma/models/stock_quant_package.py | 14 ++++++++++++++ rma/wizards/rma_make_picking.py | 6 +++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 rma/models/stock_quant_package.py diff --git a/rma/models/__init__.py b/rma/models/__init__.py index 612e4aaa..a57381a1 100644 --- a/rma/models/__init__.py +++ b/rma/models/__init__.py @@ -2,6 +2,7 @@ from . import rma_order from . import rma_order_line from . import rma_operation from . import stock_move +from . import stock_quant_package from . import stock_warehouse from . import product from . import product_category diff --git a/rma/models/stock_quant_package.py b/rma/models/stock_quant_package.py new file mode 100644 index 00000000..4da62942 --- /dev/null +++ b/rma/models/stock_quant_package.py @@ -0,0 +1,14 @@ +# Copyright (C) 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo import models + + +class QuantPackage(models.Model): + _inherit = "stock.quant.package" + + def _allowed_to_move_between_transfers(self): + res = super(QuantPackage, self)._allowed_to_move_between_transfers() + return res and self.location_id != self.env.ref( + "stock.stock_location_customers" + ) diff --git a/rma/wizards/rma_make_picking.py b/rma/wizards/rma_make_picking.py index 6f036b11..637caea9 100644 --- a/rma/wizards/rma_make_picking.py +++ b/rma/wizards/rma_make_picking.py @@ -212,7 +212,7 @@ class RmaMakePicking(models.TransientModel): action = self.item_ids.line_id.action_view_in_shipments() # Force the reservation of the RMA specific lot for incoming shipments. # FIXME: still needs fixing, not reserving appropriate serials. - for move in pickings.move_lines.filtered( + for move in pickings.move_ids.filtered( lambda x: x.state not in ("draft", "cancel", "done", "waiting") and x.rma_line_id and x.product_id.tracking in ("lot", "serial") @@ -231,7 +231,7 @@ class RmaMakePicking(models.TransientModel): ) move.move_line_ids.write( { - "product_uom_qty": 1 if picking_type == "incoming" else 0, + "reserved_uom_qty": 1 if picking_type == "incoming" else 0, "qty_done": 0, "package_id": len(quants) == 1 and quants.package_id.id, } @@ -251,7 +251,7 @@ class RmaMakePicking(models.TransientModel): "lot_id": move.rma_line_id.lot_id.id, "product_uom_id": move.product_id.uom_id.id, "qty_done": 0, - "product_uom_qty": qty if picking_type == "incoming" else 0, + "reserved_uom_qty": qty if picking_type == "incoming" else 0, } ) move_line_model.create(move_line_data)