[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 3b86ea4b9c
commit b0f3dd97e5
12 changed files with 88 additions and 127 deletions

View File

@@ -3,7 +3,7 @@
{
"name": "RMA (Return Merchandise Authorization)",
"version": "14.0.1.1.0",
"version": "14.0.1.1.1",
"license": "LGPL-3",
"category": "RMA",
"summary": "Introduces the return merchandise authorization (RMA) process "

View File

@@ -308,7 +308,10 @@ class RmaOrderLine(models.Model):
states={"draft": [("readonly", False)]},
)
price_unit = fields.Monetary(
string="Price Unit", readonly=True, states={"draft": [("readonly", False)]}
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"
@@ -651,15 +654,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

View File

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

View File

@@ -3,7 +3,7 @@
{
"name": "RMA Account",
"version": "14.0.1.0.0",
"version": "14.0.1.0.1",
"license": "LGPL-3",
"category": "RMA",
"summary": "Integrates RMA with Invoice Processing",

View File

@@ -4,44 +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:
layers = move.stock_valuation_layer_ids
price_unit = sum(layers.mapped("value")) / sum(
layers.mapped("quantity")
)
res["price_unit"] = price_unit
return res
class ProcurementGroup(models.Model):
_inherit = "procurement.group"

View File

@@ -336,3 +336,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

View File

@@ -2,7 +2,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
{
"name": "RMA Purchase",
"version": "14.0.1.0.0",
"version": "14.0.1.0.1",
"category": "RMA",
"summary": "RMA from PO",
"license": "LGPL-3",

View File

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

View File

@@ -228,3 +228,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

View File

@@ -3,7 +3,7 @@
{
"name": "RMA Sale",
"version": "14.0.1.0.0",
"version": "14.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

@@ -218,3 +218,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