[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 c4b906ef35
commit 3eb7483706
3 changed files with 23 additions and 50 deletions

View File

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