[IMP] centralize the logic to get the correct cost of the RMA.

This commit is contained in:
Jordi Ballester Alomar
2022-11-23 15:26:03 +01:00
parent c4fed05926
commit e73f7bc705
3 changed files with 32 additions and 28 deletions

View File

@@ -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",

View File

@@ -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

View File

@@ -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