From 1d171ecb6d0be6db56c7dde2827aa341d42f6624 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 --- .../tests/test_rma_stock_account_purchase.py | 63 ++++++++++++++++++- rma_purchase/wizards/__init__.py | 1 + rma_purchase/wizards/rma_refund.py | 26 ++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 rma_purchase/wizards/rma_refund.py diff --git a/rma_purchase/tests/test_rma_stock_account_purchase.py b/rma_purchase/tests/test_rma_stock_account_purchase.py index fe7d21d3..199edb56 100644 --- a/rma_purchase/tests/test_rma_stock_account_purchase.py +++ b/rma_purchase/tests/test_rma_stock_account_purchase.py @@ -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.assertEqual(len(bill.invoice_line_ids), 1) + self.assertEqual(bill.invoice_line_ids.rma_line_id, rma_line) + self.assertEqual(bill.invoice_line_ids.price_unit, pol_1.price_unit) + self.assertEqual(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"))) diff --git a/rma_purchase/wizards/__init__.py b/rma_purchase/wizards/__init__.py index 632e2e5b..707ce12a 100644 --- a/rma_purchase/wizards/__init__.py +++ b/rma_purchase/wizards/__init__.py @@ -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 diff --git a/rma_purchase/wizards/rma_refund.py b/rma_purchase/wizards/rma_refund.py new file mode 100644 index 00000000..d3ccb392 --- /dev/null +++ b/rma_purchase/wizards/rma_refund.py @@ -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