[IMP] rma: Refactor all rma modules in order to consider using the correct price unit in moves

Otherwise the inventory accounting will be completely wrong.
This commit is contained in:
Jordi Ballester
2022-03-02 11:35:08 +01:00
parent f5f5f5e53c
commit 56cf851fe8
26 changed files with 212 additions and 58 deletions

View File

@@ -279,10 +279,10 @@ class TestAccountMoveLineRmaOrderLine(common.SavepointCase):
}
)
make_refund.invoice_refund()
rma_line.refund_line_ids.move_id.filtered(
rma_line.move_line_ids.move_id.filtered(
lambda x: x.state != "posted"
).action_post()
for aml in rma_line.refund_line_ids.move_id.filtered(
for aml in rma_line.move_line_ids.move_id.filtered(
lambda x: x.move_type in ("out_refund", "in_refund")
).invoice_line_ids:
if aml.product_id == rma_line.product_id and aml.move_id:

View File

@@ -35,9 +35,10 @@ class StockRule(models.Model):
res["partner_id"] = line.delivery_address_id.id
else:
res["partner_id"] = line.rma_id.partner_id.id
dest_loc = self.env["stock.location"].browse([res["location_dest_id"]])[0]
if dest_loc.usage == "internal":
res["price_unit"] = line.price_unit
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
return res

View File

@@ -1,7 +1,5 @@
# Copyright 2017 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import rma_order
from . import rma_order_line
from . import rma_operation
from . import account_move
from . import procurement

View File

@@ -168,7 +168,7 @@ class AccountMoveLine(models.Model):
rma_line_id = fields.Many2one(
comodel_name="rma.order.line",
string="RMA line refund",
string="RMA line",
ondelete="set null",
help="This will contain the rma line that originated the refund line",
help="This will contain the rma line that originated this line",
)

View File

@@ -0,0 +1,48 @@
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
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")
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"
rma_id = fields.Many2one(
comodel_name="rma.order", string="RMA", ondelete="set null"
)
rma_line_id = fields.Many2one(
comodel_name="rma.order.line", string="RMA line", ondelete="set null"
)

View File

@@ -9,7 +9,9 @@ class RmaOrder(models.Model):
def _compute_invoice_refund_count(self):
for rec in self:
invoices = rec.mapped("rma_line_ids.refund_line_ids.move_id")
invoices = rec.mapped("rma_line_ids.move_line_ids.move_id").filtered(
lambda m: m.move_type in ["in_refund", "out_refund"]
)
rec.invoice_refund_count = len(invoices)
def _compute_invoice_count(self):

View File

@@ -16,19 +16,20 @@ class RmaOrderLine(models.Model):
return self.env["res.partner"]
@api.depends(
"refund_line_ids", "refund_line_ids.move_id.state", "refund_policy", "type"
"move_line_ids", "move_line_ids.move_id.state", "refund_policy", "type"
)
def _compute_qty_refunded(self):
for rec in self:
rec.qty_refunded = sum(
rec.refund_line_ids.filtered(
rec.move_line_ids.filtered(
lambda i: i.move_id.state in ("posted")
and i.move_id.move_type in ["out_refund", "in_refund"]
).mapped("quantity")
)
@api.depends(
"refund_line_ids",
"refund_line_ids.move_id.state",
"move_line_ids",
"move_line_ids.move_id.state",
"refund_policy",
"move_ids",
"move_ids.state",
@@ -47,7 +48,11 @@ class RmaOrderLine(models.Model):
def _compute_refund_count(self):
for rec in self:
rec.refund_count = len(rec.refund_line_ids.mapped("move_id"))
rec.refund_count = len(
rec.move_line_ids.mapped("move_id").filtered(
lambda m: m.move_type in ["out_refund", "in_refund"]
)
)
invoice_address_id = fields.Many2one(
"res.partner",
@@ -68,7 +73,7 @@ class RmaOrderLine(models.Model):
readonly=True,
states={"draft": [("readonly", False)]},
)
refund_line_ids = fields.One2many(
move_line_ids = fields.One2many(
comodel_name="account.move.line",
inverse_name="rma_line_id",
string="Refund Lines",
@@ -272,12 +277,14 @@ class RmaOrderLine(models.Model):
}
def action_view_refunds(self):
move_ids = self.mapped("refund_line_ids.move_id").ids
moves = self.mapped("move_line_ids.move_id").filtered(
lambda m: m.move_type in ["out_refund", "in_refund"]
)
form_view_ref = self.env.ref("account.view_move_form", False)
tree_view_ref = self.env.ref("account.view_move_tree", False)
return {
"domain": [("id", "in", move_ids)],
"domain": [("id", "in", moves.ids)],
"name": "Refunds",
"res_model": "account.move",
"type": "ir.actions.act_window",

View File

@@ -220,7 +220,7 @@ class TestRmaAccount(common.SingleTransactionCase):
{"customer": True, "active_ids": rma.ids, "active_model": "rma.order.line"}
).create({"description": "Test refund"})
make_refund.invoice_refund()
rma.refund_line_ids.move_id.action_post()
rma.move_line_ids.move_id.action_post()
rma._compute_refund_count()
self.assertEqual(rma.refund_count, 1)
self.assertEqual(rma.qty_to_refund, 0.0)
@@ -258,8 +258,8 @@ class TestRmaAccount(common.SingleTransactionCase):
{"customer": True, "active_ids": rma.ids, "active_model": "rma.order.line"}
).create({"description": "Test refund"})
make_refund.invoice_refund()
rma.refund_line_ids.move_id.action_post()
rma.move_line_ids.move_id.action_post()
rma._compute_refund_count()
self.assertEqual(
self.operation_1.refund_journal_id, rma.refund_line_ids.journal_id
self.operation_1.refund_journal_id, rma.move_line_ids.journal_id
)

View File

@@ -80,7 +80,13 @@ class RmaRefund(models.TransientModel):
for wizard in self:
first = self.item_ids[0]
values = self._prepare_refund(wizard, first.line_id)
new_refund = self.env["account.move"].create(values)
default_move_type = (
"in_refund" if first.line_id.type == "supplier" else "out_refund",
)
account_move_model = self.env["account.move"].with_context(
default_move_type=default_move_type
)
new_refund = account_move_model.create(values)
return new_refund
def invoice_refund(self):
@@ -106,17 +112,8 @@ class RmaRefund(models.TransientModel):
@api.model
def prepare_refund_line(self, item):
accounts = item.product.product_tmpl_id._get_product_accounts()
if item.line_id.type == "customer":
account = accounts["income"]
else:
account = accounts["expense"]
if not account:
raise ValidationError(_("Accounts are not configured for this product."))
values = {
"name": item.line_id.name or item.rma_id.name,
"account_id": account.id,
"price_unit": item.line_id.price_unit,
"product_uom_id": item.line_id.uom_id.id,
"product_id": item.product.id,

View File

@@ -1,5 +1,2 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import models
from . import wizards

View File

@@ -1,13 +1,12 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
{
"name": "RMA Purchase",
"version": "14.0.1.0.0",
"category": "RMA",
"summary": "RMA from PO",
"license": "LGPL-3",
"author": "Eficent, Odoo Community Association (OCA)",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"website": "https://github.com/ForgeFlow/stock-rma",
"depends": ["rma_account", "purchase"],
"data": [
@@ -19,4 +18,5 @@
"wizards/rma_add_purchase.xml",
],
"installable": True,
"auto_install": True,
}

View File

@@ -1,7 +1,6 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import rma_order
from . import rma_order_line
from . import purchase_order
from . import purchase_order_line
from . import rma_operation
from . import procurement

View File

@@ -0,0 +1,61 @@
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
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")
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")
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")
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"
rma_id = fields.Many2one(
comodel_name="rma.order", string="RMA", ondelete="set null"
)
rma_line_id = fields.Many2one(
comodel_name="rma.order.line", string="RMA line", ondelete="set null"
)

View File

@@ -1,4 +1,4 @@
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, models

View File

@@ -1,4 +1,4 @@
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models

View File

@@ -1,4 +1,4 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import _, api, fields, models

View File

@@ -1,4 +1,4 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models

View File

@@ -1,4 +1,4 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import _, api, fields, models

View File

@@ -1,4 +1 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import test_rma_purchase

View File

@@ -1,4 +1,4 @@
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo.fields import Datetime

View File

@@ -1,6 +1,3 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import rma_make_picking
from . import rma_add_purchase
from . import rma_order_line_make_purchase_order

View File

@@ -1,6 +1,5 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

View File

@@ -1,4 +1,4 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models

View File

@@ -1,5 +1,5 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from datetime import datetime

View File

@@ -1,7 +1,8 @@
# Copyright 2020 ForgeFlow S.L.
# Copyright 2020-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import sale_order_line
from . import sale_order
from . import rma_order_line
from . import rma_order
from . import rma_operation
from . import procurement

View File

@@ -0,0 +1,50 @@
# Copyright 2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import 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")
if line.reference_move_id:
return res
if line.sale_line_id:
moves = line.sale_line_id.move_ids
if moves:
# TODO: Should we be smart in the choice of the move?
layers = moves.mapped("stock_valuation_layer_ids")
cost = layers[-1].unit_cost
res["price_unit"] = cost
elif line.account_move_line_id:
sale_lines = line.account_move_line_id.sale_line_ids
moves = sale_lines.mapped("move_ids")
if moves:
layers = moves.mapped("stock_valuation_layer_ids")
cost = layers[-1].unit_cost
# TODO: Should we be smart in the choice of the move?
res["price_unit"] = cost
return res