From 3389bca69346ad2b9dd1bc4ca1d9507244274242 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Date: Wed, 2 Mar 2022 11:35:08 +0100 Subject: [PATCH] [IMP] rma: Refactor all rma modules in order to consider using the correct price unit in moves Otherwise the inventory accounting will be completely wrong. --- rma_sale/models/__init__.py | 3 +- rma_sale/models/procurement.py | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 rma_sale/models/procurement.py diff --git a/rma_sale/models/__init__.py b/rma_sale/models/__init__.py index 162f1ebd..bcab3a0d 100644 --- a/rma_sale/models/__init__.py +++ b/rma_sale/models/__init__.py @@ -1,7 +1,8 @@ -# Copyright 2020 ForgeFlow S.L. +# Copyright 2020-2022 ForgeFlow S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) from . import sale_order_line from . import sale_order from . import rma_order_line from . import rma_order from . import rma_operation +from . import procurement diff --git a/rma_sale/models/procurement.py b/rma_sale/models/procurement.py new file mode 100644 index 00000000..07b291e6 --- /dev/null +++ b/rma_sale/models/procurement.py @@ -0,0 +1,50 @@ +# Copyright 2022 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo import models + + +class StockRule(models.Model): + _inherit = "stock.rule" + + def _get_stock_move_values( + self, + product_id, + product_qty, + product_uom, + location_id, + name, + origin, + company_id, + values, + ): + res = super(StockRule, self)._get_stock_move_values( + product_id, + product_qty, + product_uom, + location_id, + name, + origin, + company_id, + values, + ) + if "rma_line_id" in values: + line = values.get("rma_line_id") + if line.reference_move_id: + return res + if line.sale_line_id: + moves = line.sale_line_id.move_ids + if moves: + # TODO: Should we be smart in the choice of the move? + layers = moves.mapped("stock_valuation_layer_ids") + cost = layers[-1].unit_cost + res["price_unit"] = cost + elif line.account_move_line_id: + sale_lines = line.account_move_line_id.sale_line_ids + moves = sale_lines.mapped("move_ids") + if moves: + layers = moves.mapped("stock_valuation_layer_ids") + cost = layers[-1].unit_cost + # TODO: Should we be smart in the choice of the move? + res["price_unit"] = cost + return res