[IMP] calculate refund unit price

This commit is contained in:
Jordi Ballester Alomar
2022-11-29 08:07:01 +01:00
parent 5850708b83
commit 1eea77c649
11 changed files with 303 additions and 40 deletions

View File

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

View File

@@ -9,9 +9,15 @@ class AccountMove(models.Model):
_inherit = "account.move"
@api.depends("line_ids.rma_line_ids")
def _compute_rma_count(self):
def _compute_used_in_rma_count(self):
for inv in self:
rmas = self.mapped("line_ids.rma_line_ids")
inv.used_in_rma_count = len(rmas)
@api.depends("line_ids.rma_line_id")
def _compute_rma_count(self):
for inv in self:
rmas = self.mapped("line_ids.rma_line_id")
inv.rma_count = len(rmas)
def _prepare_invoice_line_from_rma_line(self, rma_line):
@@ -19,22 +25,26 @@ class AccountMove(models.Model):
qty = rma_line.qty_to_refund
if float_compare(qty, 0.0, precision_rounding=rma_line.uom_id.rounding) <= 0:
qty = 0.0
# Todo fill taxes from somewhere
data = {
"move_id": self.id,
"product_uom_id": rma_line.uom_id.id,
"product_id": rma_line.product_id.id,
"price_unit": rma_line.company_id.currency_id.with_context(
date=self.date
).compute(rma_line.price_unit, self.currency_id, round=False),
"price_unit": rma_line.company_id.currency_id._convert(
rma_line._get_price_unit(),
self.currency_id,
self.company_id,
self.date,
round=False,
),
"quantity": qty,
"discount": 0.0,
"rma_line_ids": [(4, rma_line.id)],
"rma_line_id": rma_line.id,
"sequence": sequence + 1,
}
return data
def _post_process_invoice_line_from_rma_line(self, new_line, rma_line):
new_line.rma_line_id = rma_line
new_line.name = "%s: %s" % (
self.add_rma_line_id.name,
new_line._get_computed_name(),
@@ -58,17 +68,16 @@ class AccountMove(models.Model):
self._post_process_invoice_line_from_rma_line(
new_line, self.add_rma_line_id
)
line = new_line._convert_to_write(
{name: new_line[name] for name in new_line._cache}
)
# Compute invoice_origin.
origins = set(self.line_ids.mapped("rma_line_id.name"))
self.invoice_origin = ",".join(list(origins))
self.add_rma_line_id = False
self._onchange_currency()
return line
rma_count = fields.Integer(compute="_compute_rma_count", string="# of RMA")
used_in_rma_count = fields.Integer(
compute="_compute_used_in_rma_count", string="# of Used in RMA"
)
add_rma_line_id = fields.Many2one(
comodel_name="rma.order.line",
@@ -77,7 +86,15 @@ class AccountMove(models.Model):
help="Create a refund in based on an existing rma_line",
)
def action_view_used_in_rma(self):
rmas = self.mapped("line_ids.rma_line_ids")
return self._prepare_action_view_rma(rmas)
def action_view_rma(self):
rmas = self.mapped("line_ids.rma_line_id")
return self._prepare_action_view_rma(rmas)
def _prepare_action_view_rma(self, rmas):
if self.move_type in ["in_invoice", "in_refund"]:
action = self.env.ref("rma.action_rma_supplier_lines")
form_view = self.env.ref("rma.view_rma_line_supplier_form", False)
@@ -85,7 +102,7 @@ class AccountMove(models.Model):
action = self.env.ref("rma.action_rma_customer_lines")
form_view = self.env.ref("rma.view_rma_line_form", False)
result = action.sudo().read()[0]
rma_ids = self.mapped("line_ids.rma_line_ids").ids
rma_ids = rmas.ids
# choose the view_mode accordingly
if not rma_ids:
result["domain"] = [("id", "in", [])]
@@ -176,11 +193,19 @@ class AccountMoveLine(models.Model):
else:
return super(AccountMoveLine, self).name_get()
def _compute_rma_count(self):
def _compute_used_in_rma_count(self):
for invl in self:
rma_lines = invl.mapped("rma_line_ids")
invl.used_in_rma_line_count = len(rma_lines)
def _compute_rma_count(self):
for invl in self:
rma_lines = invl.mapped("rma_line_id")
invl.rma_line_count = len(rma_lines)
used_in_rma_line_count = fields.Integer(
compute="_compute_used_in_rma_line_count", string="# of RMA"
)
rma_line_count = fields.Integer(compute="_compute_rma_count", string="# of RMA")
rma_line_ids = fields.One2many(
comodel_name="rma.order.line",
@@ -189,7 +214,6 @@ class AccountMoveLine(models.Model):
readonly=True,
help="This will contain the RMA lines for the invoice line",
)
rma_line_id = fields.Many2one(
comodel_name="rma.order.line",
string="RMA line",

View File

@@ -2,7 +2,9 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import fields
from odoo.fields import Date
from odoo.tests import common
from odoo.tests.common import Form
class TestRmaAccount(common.SingleTransactionCase):
@@ -226,18 +228,20 @@ class TestRmaAccount(common.SingleTransactionCase):
self.assertEqual(rma.qty_to_refund, 0.0)
self.assertEqual(rma.qty_refunded, 2.0)
def test_05_fill_rma_from_inv_line(self):
def test_05_fill_rma_from_supplier_inv_line(self):
"""Test filling a RMA (line) from a invoice line."""
rma = self.rma_line_obj.new(
{
"partner_id": self.inv_customer.partner_id.id,
"account_move_line_id": self.inv_supplier.line_ids.ids[0],
}
)
self.assertFalse(rma.product_id)
rma._onchange_account_move_line_id()
with Form(
self.rma_line_obj.with_context(default_type="supplier")
) as rma_line_form:
rma_line_form.partner_id = self.inv_supplier.partner_id
rma_line_form.account_move_line_id = self.inv_supplier.line_ids[0]
rma = rma_line_form.save()
self.assertEqual(rma.product_id, self.product_1)
self.assertEqual(rma.product_qty, 3.0)
# Remember that the test is SingleTransactionCase.
# This supplier invoice has been referenced in 3 RMA lines.
self.assertEqual(self.inv_supplier.used_in_rma_count, 3)
self.assertEqual(self.inv_supplier.rma_count, 0)
def test_06_default_journal(self):
self.operation_1.write({"refund_journal_id": self.journal_sale.id})
@@ -277,15 +281,17 @@ class TestRmaAccount(common.SingleTransactionCase):
rma_1 = self.rma_group_supplier.rma_line_ids.filtered(
lambda r: r.product_id == self.product_1
)
inv = self.inv_obj.with_context(
{
"default_move_type": "in_refund",
"default_partner_id": self.inv_supplier.partner_id,
}
).create({"add_rma_line_id": rma_1})
line = inv.on_change_add_rma_line_id()
inv.invoice_line_ids = [(0, 0, line)]
inv_product = inv.invoice_line_ids.filtered(
with Form(
self.env["account.move"].with_context(default_move_type="in_refund")
) as bill_form:
bill_form.partner_id = rma_1.partner_id
bill_form.invoice_date = Date.today()
bill_form.add_rma_line_id = rma_1
bill = bill_form.save()
bill.action_post()
bill_product = bill.invoice_line_ids.filtered(
lambda x: x.product_id == self.product_1
).mapped("product_id")
self.assertEqual(rma_1.product_id.id, inv_product.id)
self.assertEqual(rma_1.product_id.id, bill_product.id)
self.assertEqual(bill.rma_count, 1)
self.assertEqual(bill.used_in_rma_count, 0)

View File

@@ -8,14 +8,38 @@
<div name="button_box" position="inside">
<button
type="object"
name="action_view_rma"
name="action_view_used_in_rma"
class="oe_stat_button"
icon="fa-eject"
attrs="{'invisible': [('used_in_rma_count', '=', 0)]}"
groups="rma.group_rma_customer_user,rma.group_rma_supplier_user"
>
<field name="used_in_rma_count" widget="statinfo" string="RMA" />
</button>
<button
type="object"
name="action_view_rma"
class="oe_stat_button"
icon="fa-list"
attrs="{'invisible': [('rma_count', '=', 0)]}"
groups="rma.group_rma_customer_user,rma.group_rma_supplier_user"
>
<field name="rma_count" widget="statinfo" string="RMA" />
</button>
</div>
<!-- Add link to rma_line_id to account.move.line -->
<xpath
expr="//field[@name='invoice_line_ids']/tree/field[@name='company_id']"
position="after"
>
<field name="rma_line_id" invisible="1" />
</xpath>
<xpath
expr="//field[@name='line_ids']/tree/field[@name='company_id']"
position="after"
>
<field name="rma_line_id" invisible="1" />
</xpath>
</field>
</record>
@@ -33,6 +57,7 @@
<notebook attrs="{'invisible': [('rma_line_count', '=', 0)]}">
<page string="RMA Lines">
<field name="rma_line_ids" />
<field name="rma_line_id" />
</page>
</notebook>
</field>

View File

@@ -110,11 +110,24 @@ class RmaRefund(models.TransientModel):
result["res_id"] = new_invoice.id
return result
def _get_refund_price_unit(self, rma):
price_unit = rma.price_unit
# If this references a previous invoice/bill, use the same unit price
if rma.account_move_line_id:
price_unit = rma.account_move_line_id.price_unit
return price_unit
def _get_refund_currency(self, rma):
currency = rma.currency_id
if rma.account_move_line_id:
currency = rma.account_move_line_id.currency_id
return currency
@api.model
def prepare_refund_line(self, item):
values = {
"name": item.line_id.name or item.rma_id.name,
"price_unit": item.line_id.price_unit,
"price_unit": self._get_refund_price_unit(item.line_id),
"product_uom_id": item.line_id.uom_id.id,
"product_id": item.product.id,
"rma_line_id": item.line_id.id,
@@ -143,7 +156,7 @@ class RmaRefund(models.TransientModel):
"journal_id": journal.id,
"fiscal_position_id": rma_line.partner_id.property_account_position_id.id,
"state": "draft",
"currency_id": rma_line.currency_id.id,
"currency_id": self._get_refund_currency(rma_line),
"date": wizard.date,
"invoice_date": wizard.date_invoice,
"partner_id": rma_line.invoice_address_id.id or rma_line.partner_id.id,

View File

@@ -1,7 +1,7 @@
# Copyright 2017-22 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo.fields import Datetime
from odoo.fields import Date, Datetime
from odoo.tests.common import Form
# pylint: disable=odoo-addons-relative-import
@@ -14,6 +14,9 @@ class TestRmaStockAccountPurchase(TestRmaStockAccount):
super(TestRmaStockAccountPurchase, cls).setUpClass()
cls.pol_model = cls.env["purchase.order.line"]
cls.po_model = cls.env["purchase.order"]
cls.rma_operation_supplier_refund = cls.env.ref(
"rma_account.rma_operation_supplier_refund"
)
def test_01_cost_from_po_move(self):
"""
@@ -79,3 +82,61 @@ class TestRmaStockAccountPurchase(TestRmaStockAccount):
rma_line.refund_line_ids.move_id.action_post()
account_move = rma_line.mapped("refund_line_ids.move_id")
self.check_accounts_used(account_move, credit_account="grni")
def test_02_return_and_refund_ref_po(self):
"""
Purchase a product.
Then create an RMA to return it and get the refund from the supplier
"""
self.product_fifo_1.standard_price = 1234
po = self.po_model.create(
{
"partner_id": self.partner_id.id,
}
)
pol_1 = self.pol_model.create(
{
"name": self.product_fifo_1.name,
"order_id": po.id,
"product_id": self.product_fifo_1.id,
"product_qty": 20.0,
"product_uom": self.product_fifo_1.uom_id.id,
"price_unit": 100.0,
"date_planned": Datetime.now(),
}
)
po.button_confirm()
self._do_picking(po.picking_ids)
self.product_fifo_1.standard_price = 1234 # this should not be taken
supplier_view = self.env.ref("rma_purchase.view_rma_line_form")
rma_line = Form(
self.rma_line.with_context(supplier=1).with_user(self.rma_basic_user),
view=supplier_view.id,
)
rma_line.partner_id = po.partner_id
rma_line.purchase_order_line_id = pol_1
rma_line.price_unit = 4356
rma_line.operation_id = self.rma_operation_supplier_refund
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
self._deliver_rma(rma_line)
with Form(
self.env["account.move"].with_context(default_move_type="in_refund")
) as bill_form:
bill_form.partner_id = rma_line.partner_id
bill_form.invoice_date = Date.today()
bill_form.add_rma_line_id = rma_line
bill = bill_form.save()
bill.action_post()
self.assertEquals(len(bill.invoice_line_ids), 1)
self.assertEquals(bill.invoice_line_ids.rma_line_id, rma_line)
self.assertEquals(bill.invoice_line_ids.price_unit, pol_1.price_unit)
self.assertEquals(bill.invoice_line_ids.quantity, 20)
grni_amls = self.env["account.move.line"].search(
[
("account_id", "=", self.account_grni.id),
("rma_line_id", "=", rma_line.id),
]
)
self.assertEqual(sum(grni_amls.mapped("balance")), 0.0)
self.assertTrue(all(grni_amls.mapped("reconciled")))

View File

@@ -1,3 +1,4 @@
from . import rma_make_picking
from . import rma_add_purchase
from . import rma_order_line_make_purchase_order
from . import rma_refund

View File

@@ -0,0 +1,26 @@
# Copyright 22 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import models
class RmaRefund(models.TransientModel):
_inherit = "rma.refund"
def _get_refund_price_unit(self, rma):
price_unit = super(RmaRefund, self)._get_refund_price_unit(rma)
if rma.type == "supplier":
if rma.account_move_line_id:
price_unit = rma.account_move_line_id.price_unit
elif rma.purchase_order_line_id:
price_unit = rma.purchase_order_line_id.price_unit
return price_unit
def _get_refund_currency(self, rma):
currency = super(RmaRefund, self)._get_refund_currency(rma)
if rma.type == "supplier":
if rma.account_move_line_id:
currency = rma.account_move_line_id.currency_id
elif rma.purchase_order_line_id:
currency = rma.purchase_order_line_id.currency_id
return currency

View File

@@ -11,6 +11,9 @@ class TestRmaStockAccountSale(TestRmaStockAccount):
@classmethod
def setUpClass(cls):
super(TestRmaStockAccountSale, cls).setUpClass()
cls.operation_receive_refund = cls.env.ref(
"rma_account.rma_operation_customer_refund"
)
customer1 = cls.env["res.partner"].create({"name": "Customer 1"})
cls.product_fifo_1.standard_price = 1234
cls._create_inventory(cls.product_fifo_1, 20.0, cls.env.ref("rma.location_rma"))
@@ -72,3 +75,87 @@ class TestRmaStockAccountSale(TestRmaStockAccount):
self.check_accounts_used(
account_move, debit_account="inventory", credit_account="gdni"
)
def test_02_return_and_refund_ref_so(self):
"""
Sell a product. Create a customer invoice.
Then create an RMA to return it and refund to the customer
"""
customer_view = self.env.ref("rma_sale.view_rma_line_form")
so_line = self.so1.order_line.filtered(
lambda r: r.product_id == self.product_fifo_1
)
rma_line = Form(
self.rma_line.with_context(customer=1).with_user(self.rma_basic_user),
view=customer_view.id,
)
rma_line.partner_id = self.so1.partner_id
rma_line.sale_line_id = so_line
rma_line.operation_id = self.operation_receive_refund
rma_line.price_unit = 4356 # This should never be used
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
self._receive_rma(rma_line)
make_refund = self.rma_refund_wiz.with_context(
{
"customer": True,
"active_ids": rma_line.ids,
"active_model": "rma.order.line",
}
).create({"description": "Test refund"})
make_refund.item_ids.qty_to_refund = 20
make_refund.invoice_refund()
refund = rma_line.refund_line_ids.move_id
refund.action_post()
self.assertEqual(refund.invoice_line_ids[0].price_unit, so_line.price_unit)
self.assertEqual(refund.invoice_line_ids[0].currency_id, so_line.currency_id)
gdni_amls = self.env["account.move.line"].search(
[
("account_id", "=", self.account_gdni.id),
("rma_line_id", "=", rma_line.id),
]
)
self.assertEqual(sum(gdni_amls.mapped("balance")), 0.0)
self.assertTrue(all(gdni_amls.mapped("reconciled")))
def test_03_return_and_refund_ref_inv(self):
"""
Sell a product. Then create an RMA to return it and refund to the customer
"""
customer_invoice = self.so1._create_invoices()
customer_view = self.env.ref("rma_sale.view_rma_line_form")
so_line = self.so1.order_line.filtered(
lambda r: r.product_id == self.product_fifo_1
)
rma_line = Form(
self.rma_line.with_context(customer=1).with_user(self.rma_basic_user),
view=customer_view.id,
)
rma_line.partner_id = self.so1.partner_id
rma_line.account_move_line_id = customer_invoice.invoice_line_ids[0]
rma_line.operation_id = self.operation_receive_refund
rma_line.price_unit = 4356 # This should never be used
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
self._receive_rma(rma_line)
make_refund = self.rma_refund_wiz.with_context(
{
"customer": True,
"active_ids": rma_line.ids,
"active_model": "rma.order.line",
}
).create({"description": "Test refund"})
make_refund.item_ids.qty_to_refund = 20
make_refund.invoice_refund()
refund = rma_line.refund_line_ids.move_id
refund.action_post()
self.assertEqual(refund.invoice_line_ids[0].price_unit, so_line.price_unit)
self.assertEqual(refund.invoice_line_ids[0].currency_id, so_line.currency_id)
gdni_amls = self.env["account.move.line"].search(
[
("account_id", "=", self.account_gdni.id),
("rma_line_id", "=", rma_line.id),
]
)
self.assertEqual(sum(gdni_amls.mapped("balance")), 0.0)
self.assertTrue(all(gdni_amls.mapped("reconciled")))

View File

@@ -1,6 +1,3 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import rma_order_line_make_sale_order
from . import rma_make_picking
from . import rma_refund

View File

@@ -12,6 +12,29 @@ class RmaRefund(models.TransientModel):
res["sale_line_id"] = line.sale_line_id.id
return res
def _get_refund_price_unit(self, rma):
price_unit = super(RmaRefund, self)._get_refund_price_unit(rma)
if rma.type == "customer":
if rma.account_move_line_id:
price_unit = rma.account_move_line_id.price_unit
elif rma.sale_line_id:
price_unit = rma.sale_line_id.price_unit
else:
# Fall back to the sale price if no reference is found.
price_unit = rma.product_id.with_company(rma.company_id).lst_price
return price_unit
def _get_refund_currency(self, rma):
currency = rma.currency_id
if rma.type == "customer":
if rma.account_move_line_id:
currency = rma.account_move_line_id.currency_id
elif rma.sale_line_id:
currency = rma.sale_line_id.currency_id
else:
currency = rma.company_id.currency_id
return currency
class RmaRefundItem(models.TransientModel):
_inherit = "rma.refund.item"