From 700f99428dc71e8362ace0aded9f92845fb5fb5b Mon Sep 17 00:00:00 2001 From: Jordi Ballester Alomar Date: Tue, 29 Nov 2022 08:07:01 +0100 Subject: [PATCH] [IMP] calculate refund unit price --- rma_account/__manifest__.py | 2 +- rma_account/models/account_move.py | 50 ++++++++++++++++++------- rma_account/tests/test_rma_account.py | 46 +++++++++++++---------- rma_account/views/account_move_view.xml | 27 ++++++++++++- rma_account/wizards/rma_refund.py | 17 ++++++++- 5 files changed, 105 insertions(+), 37 deletions(-) diff --git a/rma_account/__manifest__.py b/rma_account/__manifest__.py index 012c6945..7c81beae 100644 --- a/rma_account/__manifest__.py +++ b/rma_account/__manifest__.py @@ -3,7 +3,7 @@ { "name": "RMA Account", - "version": "15.0.1.1.1", + "version": "15.0.1.2.0", "license": "LGPL-3", "category": "RMA", "summary": "Integrates RMA with Invoice Processing", diff --git a/rma_account/models/account_move.py b/rma_account/models/account_move.py index e28f89a3..4ce2146d 100644 --- a/rma_account/models/account_move.py +++ b/rma_account/models/account_move.py @@ -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", [])] @@ -196,11 +213,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", @@ -209,7 +234,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", diff --git a/rma_account/tests/test_rma_account.py b/rma_account/tests/test_rma_account.py index b05ef925..1bfb9370 100644 --- a/rma_account/tests/test_rma_account.py +++ b/rma_account/tests/test_rma_account.py @@ -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): @@ -230,18 +232,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): - """Test filling a RMA (line) from an 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() + def test_05_fill_rma_from_supplier_inv_line(self): + """Test filling a RMA (line) from a invoice line.""" + 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}) @@ -285,15 +289,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) diff --git a/rma_account/views/account_move_view.xml b/rma_account/views/account_move_view.xml index 2153188a..d6ba3d50 100644 --- a/rma_account/views/account_move_view.xml +++ b/rma_account/views/account_move_view.xml @@ -8,14 +8,38 @@
+
+ + + + + + + @@ -33,6 +57,7 @@ + diff --git a/rma_account/wizards/rma_refund.py b/rma_account/wizards/rma_refund.py index 7c2b878b..dfe569fb 100644 --- a/rma_account/wizards/rma_refund.py +++ b/rma_account/wizards/rma_refund.py @@ -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,