From e73f7bc70522ba529de669f7ba48251a31a48c5e Mon Sep 17 00:00:00 2001 From: Jordi Ballester Alomar Date: Wed, 23 Nov 2022 15:26:03 +0100 Subject: [PATCH] [IMP] centralize the logic to get the correct cost of the RMA. --- rma_sale/__manifest__.py | 2 +- rma_sale/models/procurement.py | 28 +--------------------------- rma_sale/models/rma_order_line.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/rma_sale/__manifest__.py b/rma_sale/__manifest__.py index 19b47ce1..b3da6d36 100644 --- a/rma_sale/__manifest__.py +++ b/rma_sale/__manifest__.py @@ -3,7 +3,7 @@ { "name": "RMA Sale", - "version": "15.0.1.0.0", + "version": "15.0.1.0.1", "license": "LGPL-3", "category": "RMA", "summary": "Links RMA with Sales Orders", diff --git a/rma_sale/models/procurement.py b/rma_sale/models/procurement.py index 9fd83f17..bc9a29ee 100644 --- a/rma_sale/models/procurement.py +++ b/rma_sale/models/procurement.py @@ -33,31 +33,5 @@ class StockRule(models.Model): line = self.env["rma.order.line"].browse([line]) if line.reference_move_id: return res - if line.sale_line_id: - moves = line.sale_line_id.move_ids.filtered( - lambda x: x.state == "done" - and x.location_id.usage in ("internal", "supplier") - and x.location_dest_id.usage == "customer" - ) - if moves: - layers = moves.mapped("stock_valuation_layer_ids") - if layers: - price_unit = sum(layers.mapped("value")) / sum( - layers.mapped("quantity") - ) - res["price_unit"] = price_unit - elif line.account_move_line_id: - sale_lines = line.account_move_line_id.sale_line_ids - moves = sale_lines.mapped("move_ids").filtered( - lambda x: x.state == "done" - and x.location_id.usage in ("internal", "supplier") - and x.location_dest_id.usage == "customer" - ) - if moves: - layers = moves.mapped("stock_valuation_layer_ids") - if layers: - price_unit = sum(layers.mapped("value")) / sum( - layers.mapped("quantity") - ) - res["price_unit"] = price_unit + res["price_unit"] = line._get_price_unit() return res diff --git a/rma_sale/models/rma_order_line.py b/rma_sale/models/rma_order_line.py index eaa02830..51f1029d 100644 --- a/rma_sale/models/rma_order_line.py +++ b/rma_sale/models/rma_order_line.py @@ -215,3 +215,33 @@ class RmaOrderLine(models.Model): ): qty += sale_line.product_uom_qty return qty + + def _get_price_unit(self): + self.ensure_one() + price_unit = super(RmaOrderLine, self)._get_price_unit() + if self.sale_line_id: + moves = self.sale_line_id.move_ids.filtered( + lambda x: x.state == "done" + and x.location_id.usage in ("internal", "supplier") + and x.location_dest_id.usage == "customer" + ) + if moves: + layers = moves.sudo().mapped("stock_valuation_layer_ids") + if layers: + price_unit = sum(layers.mapped("value")) / sum( + layers.mapped("quantity") + ) + elif self.account_move_line_id: + sale_lines = self.account_move_line_id.sale_line_ids + moves = sale_lines.mapped("move_ids").filtered( + lambda x: x.state == "done" + and x.location_id.usage in ("internal", "supplier") + and x.location_dest_id.usage == "customer" + ) + if moves: + layers = moves.sudo().mapped("stock_valuation_layer_ids") + if layers: + price_unit = sum(layers.mapped("value")) / sum( + layers.mapped("quantity") + ) + return price_unit