From aa25b8416ab601a7b339e59728250f9d663dbc48 Mon Sep 17 00:00:00 2001 From: Christopher Ormaza Date: Tue, 4 Oct 2022 09:24:37 -0500 Subject: [PATCH] [FIX] get price unit with all related layers on sale moves --- rma_account/models/procurement.py | 8 ++++++-- rma_sale/models/procurement.py | 26 ++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/rma_account/models/procurement.py b/rma_account/models/procurement.py index 7a3ae703..517cda76 100644 --- a/rma_account/models/procurement.py +++ b/rma_account/models/procurement.py @@ -33,8 +33,12 @@ class StockRule(models.Model): line = self.env["rma.order.line"].browse([line]) move = line.reference_move_id if move and move.stock_valuation_layer_ids: - cost = move.stock_valuation_layer_ids[-1].unit_cost - res["price_unit"] = cost + layers = move.stock_valuation_layer_ids + price_unit = sum(layers.mapped("value")) / sum( + layers.mapped("quantity") + ) + + res["price_unit"] = price_unit return res diff --git a/rma_sale/models/procurement.py b/rma_sale/models/procurement.py index f4a21038..9fd83f17 100644 --- a/rma_sale/models/procurement.py +++ b/rma_sale/models/procurement.py @@ -34,20 +34,30 @@ class StockRule(models.Model): if line.reference_move_id: return res if line.sale_line_id: - moves = line.sale_line_id.move_ids + 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: - # TODO: Should we be smart in the choice of the move? layers = moves.mapped("stock_valuation_layer_ids") if layers: - cost = layers[-1].unit_cost - res["price_unit"] = cost + 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") + 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: - cost = layers[-1].unit_cost - # TODO: Should we be smart in the choice of the move? - res["price_unit"] = cost + price_unit = sum(layers.mapped("value")) / sum( + layers.mapped("quantity") + ) + res["price_unit"] = price_unit return res