[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 95b1d179a2
commit 22c779da22
15 changed files with 91 additions and 139 deletions

View File

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

View File

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

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": "15.0.1.1.0",
"version": "15.0.1.1.1",
"license": "LGPL-3",
"category": "RMA",
"summary": "Integrates RMA with Invoice Processing",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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

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

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