Skip to content

Commit 0c35a96

Browse files
committed
[IMP] rma_sale: Avoid computing RMA move unnecessarily
In the RMA sale wizard, the move is computed after the wizard line is generated. This is inefficient since the line is created after iterating through the moves related to the sale order. Assigning the move to the wizard line prevents another issue where all RMAs would be linked to a single move if the sale order line is associated with multiple moves.
1 parent 7637b8d commit 0c35a96

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

rma_sale/models/sale.py

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def _prepare_rma_wizard_line_vals(self, data):
3434
"sale_line_id": data["sale_line_id"].id,
3535
"uom_id": data["uom"].id,
3636
"picking_id": data["picking"] and data["picking"].id,
37+
"move_id": data.get("move") and data.get("move").id or False,
3738
}
3839

3940
def action_create_rma(self):
@@ -162,6 +163,7 @@ def _get_chained_moves(_moves, done_moves=None):
162163
"uom": move.product_uom,
163164
"picking": move.picking_id,
164165
"sale_line_id": self,
166+
"move": move,
165167
}
166168
)
167169
else:

rma_sale/wizard/sale_order_rma_wizard.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2020 Tecnativa - Ernesto Tejeda
22
# Copyright 2022 Tecnativa - Víctor Martínez
3+
# Copyright 2024 ACSONE SA/NV
34
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
45

56
from odoo import SUPERUSER_ID, _, api, fields, models
@@ -145,7 +146,7 @@ class SaleOrderLineRmaWizard(models.TransientModel):
145146
string="Delivery order",
146147
domain="[('id', 'in', allowed_picking_ids)]",
147148
)
148-
move_id = fields.Many2one(comodel_name="stock.move", compute="_compute_move_id")
149+
move_id = fields.Many2one(comodel_name="stock.move")
149150
operation_id = fields.Many2one(
150151
comodel_name="rma.operation",
151152
string="Requested operation",
@@ -169,21 +170,6 @@ def onchange_product_id(self):
169170
self.picking_id = False
170171
self.uom_id = self.product_id.uom_id
171172

172-
@api.depends("picking_id")
173-
def _compute_move_id(self):
174-
for record in self:
175-
move_id = False
176-
if record.picking_id:
177-
move_id = record.picking_id.move_ids.filtered(
178-
lambda r: (
179-
r.sale_line_id == record.sale_line_id
180-
and r.sale_line_id.product_id == record.product_id
181-
and r.sale_line_id.order_id == record.order_id
182-
and r.state == "done"
183-
)
184-
)
185-
record.move_id = move_id
186-
187173
@api.depends("order_id")
188174
def _compute_allowed_product_ids(self):
189175
for record in self:

rma_sale/wizard/sale_order_rma_wizard_views.xml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
options="{'no_create': True}"
2727
/>
2828
<field name="allowed_picking_ids" invisible="1" />
29+
<field name="move_id" invisible="1" />
2930
<field
3031
name="picking_id"
3132
options="{'no_create': True}"

rma_sale_lot/models/sale_order_line.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ def _get_qty_done_by_product_lot(self, moves):
2020
("move_id.scrapped", "=", False),
2121
],
2222
["qty_done:sum"],
23-
["product_id", "lot_id"],
23+
["product_id", "lot_id", "move_id"],
2424
lazy=False,
2525
):
26+
move_id = group.get("move_id")[0] if group.get("move_id") else False
2627
lot_id = group.get("lot_id")[0] if group.get("lot_id") else False
2728
product_id = group.get("product_id")[0]
2829
qty_done = group.get("qty_done")
29-
res[(product_id, lot_id)] += qty_done
30+
res[(product_id, move_id, lot_id)] += qty_done
3031
return res
3132

3233
def prepare_sale_rma_data(self):
@@ -38,19 +39,16 @@ def prepare_sale_rma_data(self):
3839
moves = self.get_delivery_move()
3940
data = []
4041
qty_done_by_product_lot = self._get_qty_done_by_product_lot(moves)
41-
for (product_id, lot_id), qty_done in qty_done_by_product_lot.items():
42+
for (product_id, move_id, lot_id), qty_done in qty_done_by_product_lot.items():
4243
data.append(
43-
self._prepare_sale_rma_data_line(moves, product_id, lot_id, qty_done)
44+
self._prepare_sale_rma_data_line(move_id, product_id, lot_id, qty_done)
4445
)
4546
return data
4647

47-
def _prepare_sale_rma_data_line(self, moves, product_id, lot_id, qty_done):
48-
moves = moves.move_line_ids.filtered(
49-
lambda ml, p_id=product_id, l_id=lot_id: ml.product_id.id == p_id
50-
and ml.lot_id.id == l_id
51-
).move_id
48+
def _prepare_sale_rma_data_line(self, move_id, product_id, lot_id, qty_done):
49+
move = self.env["stock.move"].browse(move_id)
5250
quantity = qty_done
53-
for returned_move in moves.returned_move_ids.filtered(
51+
for returned_move in move.returned_move_ids.filtered(
5452
lambda r: r.state in ["partially_available", "assigned", "done"]
5553
):
5654
if (
@@ -63,13 +61,14 @@ def _prepare_sale_rma_data_line(self, moves, product_id, lot_id, qty_done):
6361
elif returned_move.state == "done":
6462
quantity -= returned_move.product_qty
6563
quantity = float_round(
66-
quantity, precision_rounding=moves.product_id.uom_id.rounding
64+
quantity, precision_rounding=move.product_id.uom_id.rounding
6765
)
6866
return {
69-
"product": moves.product_id,
67+
"product": move.product_id,
7068
"quantity": quantity,
71-
"uom": moves.product_uom,
72-
"picking": moves.picking_id[0],
69+
"uom": move.product_uom,
70+
"picking": move.picking_id,
7371
"sale_line_id": self,
7472
"lot_id": lot_id,
73+
"move": move,
7574
}

0 commit comments

Comments
 (0)