diff --git a/rma_account/models/rma_operation.py b/rma_account/models/rma_operation.py index 1e984810..bf21ab55 100644 --- a/rma_account/models/rma_operation.py +++ b/rma_account/models/rma_operation.py @@ -1,7 +1,7 @@ # Copyright 2017 ForgeFlow S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) -from odoo import fields, models +from odoo import api, fields, models class RmaOperation(models.Model): @@ -17,3 +17,26 @@ class RmaOperation(models.Model): string="Refund Policy", default="no", ) + + refund_journal_id = fields.Many2one( + comodel_name="account.journal", + string="Refund Account Journal", + domain="[('id', 'in', valid_refund_journal_ids)]", + ) + + valid_refund_journal_ids = fields.Many2many( + comodel_name="account.journal", + compute="_compute_domain_valid_journal", + ) + + @api.onchange("type") + def _compute_domain_valid_journal(self): + for rec in self: + if rec.type == "customer": + rec.valid_refund_journal_ids = self.env["account.journal"].search( + [("type", "=", "sale")] + ) + else: + rec.valid_refund_journal_ids = self.env["account.journal"].search( + [("type", "=", "purchase")] + ) diff --git a/rma_account/tests/test_rma_account.py b/rma_account/tests/test_rma_account.py index c6ffc803..b970ed34 100644 --- a/rma_account/tests/test_rma_account.py +++ b/rma_account/tests/test_rma_account.py @@ -26,14 +26,28 @@ class TestRmaAccount(common.SingleTransactionCase): cls.cust_refund_op = cls.env.ref("rma_account.rma_operation_customer_refund") cls.sup_refund_op = cls.env.ref("rma_account.rma_operation_supplier_refund") cls.company_id = cls.env.user.company_id + # Create partners customer1 = customer1_obj.create({"name": "Customer 1"}) supplier1 = customer1_obj.create({"name": "Supplier 1"}) + # Create Journal + cls.journal_sale = cls.env["account.journal"].create( + { + "name": "Test Sales Journal", + "code": "tSAL", + "type": "sale", + "company_id": cls.company_id.id, + } + ) + # Create RMA group and operation: cls.rma_group_customer = cls.rma_obj.create( {"partner_id": customer1.id, "type": "customer"} ) + cls.rma_group_customer_2 = cls.rma_obj.create( + {"partner_id": customer1.id, "type": "customer"} + ) cls.rma_group_supplier = cls.rma_obj.create( {"partner_id": supplier1.id, "type": "supplier"} ) @@ -224,3 +238,28 @@ class TestRmaAccount(common.SingleTransactionCase): rma._onchange_account_move_line_id() self.assertEqual(rma.product_id, self.product_1) self.assertEqual(rma.product_qty, 3.0) + + def test_06_default_journal(self): + self.operation_1.write({"refund_journal_id": self.journal_sale.id}) + add_inv = self.rma_add_invoice_wiz.with_context( + { + "customer": True, + "active_ids": self.rma_group_customer_2.id, + "active_model": "rma.order", + } + ).create({"line_ids": [(6, 0, self.inv_customer.invoice_line_ids.ids)]}) + add_inv.add_lines() + rma = self.rma_group_customer_2.rma_line_ids.filtered( + lambda r: r.product_id == self.product_2 + ) + rma.action_rma_to_approve() + rma.action_rma_approve() + make_refund = self.rma_refund_wiz.with_context( + {"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.post() + rma._compute_refund_count() + self.assertEqual( + self.operation_1.refund_journal_id, rma.refund_line_ids.journal_id + ) diff --git a/rma_account/views/rma_operation_view.xml b/rma_account/views/rma_operation_view.xml index 58695947..6d2cdbb0 100644 --- a/rma_account/views/rma_operation_view.xml +++ b/rma_account/views/rma_operation_view.xml @@ -18,6 +18,8 @@ + + diff --git a/rma_account/wizards/rma_refund.py b/rma_account/wizards/rma_refund.py index eccc6077..b4347fc4 100644 --- a/rma_account/wizards/rma_refund.py +++ b/rma_account/wizards/rma_refund.py @@ -128,7 +128,9 @@ class RmaRefund(models.TransientModel): @api.model def _prepare_refund(self, wizard, rma_line): # origin_invoices = self._get_invoice(rma_line) - if rma_line.type == "customer": + if rma_line.operation_id.refund_journal_id: + journal = rma_line.operation_id.refund_journal_id + elif rma_line.type == "customer": journal = self.env["account.journal"].search( [("type", "=", "sale")], limit=1 )