From 22c779da228c628b2a0238d493307da86a83f2e1 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/__manifest__.py | 2 +- rma/models/rma_order_line.py | 18 ++++++- rma/models/stock_rule.py | 8 +-- rma_account/__manifest__.py | 2 +- rma_account/models/account_move.py | 7 --- rma_account/models/procurement.py | 34 ------------- rma_account/models/rma_order_line.py | 16 ++++++ rma_account/models/stock_move.py | 1 + .../tests/test_rma_account_unreconciled.py | 9 ---- rma_purchase/__manifest__.py | 2 +- rma_purchase/models/procurement.py | 49 ------------------- rma_purchase/models/rma_order_line.py | 22 +++++++++ rma_sale/__manifest__.py | 2 +- rma_sale/models/procurement.py | 28 +---------- rma_sale/models/rma_order_line.py | 30 ++++++++++++ 15 files changed, 91 insertions(+), 139 deletions(-) diff --git a/rma/__manifest__.py b/rma/__manifest__.py index 5e852428..2cb7bc58 100644 --- a/rma/__manifest__.py +++ b/rma/__manifest__.py @@ -3,7 +3,7 @@ { "name": "RMA (Return Merchandise Authorization)", - "version": "15.0.1.1.0", + "version": "15.0.1.1.1", "license": "LGPL-3", "category": "RMA", "summary": "Introduces the return merchandise authorization (RMA) process in odoo", diff --git a/rma/models/rma_order_line.py b/rma/models/rma_order_line.py index c0754908..34e8e574 100644 --- a/rma/models/rma_order_line.py +++ b/rma/models/rma_order_line.py @@ -303,7 +303,12 @@ class RmaOrderLine(models.Model): readonly=True, states={"draft": [("readonly", False)]}, ) - price_unit = fields.Monetary(readonly=True, states={"draft": [("readonly", False)]}) + price_unit = fields.Monetary( + string="Unit cost", + readonly=True, + states={"draft": [("readonly", False)]}, + help="Unit cost of the items under RMA", + ) in_shipment_count = fields.Integer( compute="_compute_in_shipment_count", string="# of Shipments" ) @@ -636,15 +641,24 @@ class RmaOrderLine(models.Model): ) return super(RmaOrderLine, self).create(vals) + def _get_price_unit(self): + """The price unit corresponds to the cost of that product""" + self.ensure_one() + if self.reference_move_id: + price_unit = self.reference_move_id.price_unit + else: + price_unit = self.product_id.with_company(self.company_id).standard_price + return price_unit + @api.onchange("product_id") def _onchange_product_id(self): result = {} if not self.product_id: return result self.uom_id = self.product_id.uom_id.id - self.price_unit = self.product_id.standard_price if not self.type: self.type = self._get_default_type() + self.price_unit = self._get_price_unit() if self.type == "customer": self.operation_id = ( self.product_id.rma_customer_operation_id diff --git a/rma/models/stock_rule.py b/rma/models/stock_rule.py index 678d971e..0c7119f5 100644 --- a/rma/models/stock_rule.py +++ b/rma/models/stock_rule.py @@ -36,11 +36,5 @@ class StockRule(models.Model): res["partner_id"] = line.delivery_address_id.id else: res["partner_id"] = line.rma_id.partner_id.id - # We are not checking the reference move here because if stock account - # is not installed, there is no way to know the cost of the stock move - # so better use the standard cost in this case. - company_id = res["company_id"] - company = self.env["res.company"].browse(company_id) - cost = product_id.with_company(company).standard_price - res["price_unit"] = cost + res["price_unit"] = line._get_price_unit() return res diff --git a/rma_account/__manifest__.py b/rma_account/__manifest__.py index eb22180c..012c6945 100644 --- a/rma_account/__manifest__.py +++ b/rma_account/__manifest__.py @@ -3,7 +3,7 @@ { "name": "RMA Account", - "version": "15.0.1.1.0", + "version": "15.0.1.1.1", "license": "LGPL-3", "category": "RMA", "summary": "Integrates RMA with Invoice Processing", diff --git a/rma_account/models/account_move.py b/rma_account/models/account_move.py index 361abdd8..e28f89a3 100644 --- a/rma_account/models/account_move.py +++ b/rma_account/models/account_move.py @@ -234,15 +234,8 @@ class AccountMoveLine(models.Model): and bool(l.move_id.reversed_entry_id) == is_line_reversing ) qty_refunded = sum( -<<<<<<< HEAD x.product_uom_id._compute_quantity(x.quantity, x.product_id.uom_id) for x in posted_invoice_lines -======= - [ - x.product_uom_id._compute_quantity(x.quantity, x.product_id.uom_id) - for x in posted_invoice_lines - ] ->>>>>>> 614c98c ([FIX] include anglo-saxon price unit calculation in refunds.) ) product = self.product_id.with_company(self.company_id).with_context( is_returned=is_line_reversing diff --git a/rma_account/models/procurement.py b/rma_account/models/procurement.py index 7a3ae703..e065cfe9 100644 --- a/rma_account/models/procurement.py +++ b/rma_account/models/procurement.py @@ -4,40 +4,6 @@ from odoo import fields, 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") - 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 - return res - - class ProcurementGroup(models.Model): _inherit = "procurement.group" diff --git a/rma_account/models/rma_order_line.py b/rma_account/models/rma_order_line.py index ece41b2e..0231446d 100644 --- a/rma_account/models/rma_order_line.py +++ b/rma_account/models/rma_order_line.py @@ -333,3 +333,19 @@ class RmaOrderLine(models.Model): ) # Reconcile. amls.reconcile() + + def _get_price_unit(self): + self.ensure_one() + price_unit = super(RmaOrderLine, self)._get_price_unit() + if self.reference_move_id: + move = self.reference_move_id + layers = move.sudo().stock_valuation_layer_ids + if layers: + price_unit = sum(layers.mapped("value")) / sum( + layers.mapped("quantity") + ) + price_unit = price_unit + elif self.account_move_line_id and self.type == "supplier": + # We get the cost from the original invoice line + price_unit = self.account_move_line_id.price_unit + return price_unit diff --git a/rma_account/models/stock_move.py b/rma_account/models/stock_move.py index 1eb1ef02..c5872fa4 100644 --- a/rma_account/models/stock_move.py +++ b/rma_account/models/stock_move.py @@ -20,6 +20,7 @@ class StockMove(models.Model): != self.product_id.categ_id.property_stock_valuation_account_id.id ): line[2]["rma_line_id"] = self.rma_line_id.id + return res def _account_entry_move(self, qty, description, svl_id, cost): res = super(StockMove, self)._account_entry_move(qty, description, svl_id, cost) diff --git a/rma_account_unreconciled/tests/test_rma_account_unreconciled.py b/rma_account_unreconciled/tests/test_rma_account_unreconciled.py index db403af3..9d1a43b4 100644 --- a/rma_account_unreconciled/tests/test_rma_account_unreconciled.py +++ b/rma_account_unreconciled/tests/test_rma_account_unreconciled.py @@ -54,13 +54,8 @@ class TestRmaAccountUnreconciled(TestRma): rma_line.action_rma_approve() self.assertFalse(rma_line.unreconciled) self.rma_customer_id.rma_line_ids.action_rma_to_approve() -<<<<<<< HEAD wizard = self.rma_make_pickingwith_context( **{ -======= - wizard = self.rma_make_picking.with_context( - { ->>>>>>> 614c98c ([FIX] include anglo-saxon price unit calculation in refunds.) "active_ids": self.rma_customer_id.rma_line_ids.ids, "active_model": "rma.order.line", "picking_type": "incoming", @@ -78,11 +73,7 @@ class TestRmaAccountUnreconciled(TestRma): rma_line._compute_unreconciled() self.assertTrue(rma_line.unreconciled) make_refund = self.rma_refund_wiz.with_context( -<<<<<<< HEAD **{ -======= - { ->>>>>>> 614c98c ([FIX] include anglo-saxon price unit calculation in refunds.) "customer": True, "active_ids": self.rma_customer_id.rma_line_ids.ids, "active_model": "rma.order.line", diff --git a/rma_purchase/__manifest__.py b/rma_purchase/__manifest__.py index 153e14d6..addcbc58 100644 --- a/rma_purchase/__manifest__.py +++ b/rma_purchase/__manifest__.py @@ -2,7 +2,7 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) { "name": "RMA Purchase", - "version": "15.0.1.0.0", + "version": "15.0.1.0.1", "category": "RMA", "summary": "RMA from PO", "license": "LGPL-3", diff --git a/rma_purchase/models/procurement.py b/rma_purchase/models/procurement.py index b551873e..e065cfe9 100644 --- a/rma_purchase/models/procurement.py +++ b/rma_purchase/models/procurement.py @@ -4,55 +4,6 @@ from odoo import fields, 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") - line = self.env["rma.order.line"].browse([line]) - if line.reference_move_id: - return res - if line.purchase_order_line_id: - moves = line.purchase_order_line_id.move_ids - 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 - elif line.account_move_line_id.purchase_line_id: - purchase_lines = line.account_move_line_id.purchase_line_id - moves = purchase_lines.mapped("move_ids") - 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 - return res - - class ProcurementGroup(models.Model): _inherit = "procurement.group" diff --git a/rma_purchase/models/rma_order_line.py b/rma_purchase/models/rma_order_line.py index 319ac080..b2dbc378 100644 --- a/rma_purchase/models/rma_order_line.py +++ b/rma_purchase/models/rma_order_line.py @@ -223,3 +223,25 @@ class RmaOrderLine(models.Model): ): qty += self.uom_id._compute_quantity(line.product_qty, line.product_uom) return qty + + def _get_price_unit(self): + self.ensure_one() + price_unit = super(RmaOrderLine, self)._get_price_unit() + if self.purchase_order_line_id: + moves = self.purchase_order_line_id.move_ids + 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.purchase_line_id: + purchase_lines = self.account_move_line_id.purchase_line_id + moves = purchase_lines.mapped("move_ids") + 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 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