diff --git a/rma/tests/test_rma.py b/rma/tests/test_rma.py index 08a0bf3b..6b9883f5 100644 --- a/rma/tests/test_rma.py +++ b/rma/tests/test_rma.py @@ -20,6 +20,7 @@ class TestRma(common.SavepointCase): cls.rma_line = cls.env["rma.order.line"] cls.rma_op = cls.env["rma.operation"] cls.product_product_model = cls.env["product.product"] + cls.res_users_model = cls.env["res.users"] cls.rma_cust_replace_op_id = cls.env.ref("rma.rma_operation_customer_replace") cls.rma_sup_replace_op_id = cls.env.ref("rma.rma_operation_supplier_replace") cls.rma_ds_replace_op_id = cls.env.ref("rma.rma_operation_ds_replace") @@ -31,6 +32,7 @@ class TestRma(common.SavepointCase): cls.product_2 = cls._create_product("PT2") cls.product_3 = cls._create_product("PT3") cls.uom_unit = cls.env.ref("uom.product_uom_unit") + cls.company = cls.env.company cls.env.user.company_id.group_rma_delivery_address = True cls.env.user.company_id.group_rma_lines = True @@ -41,6 +43,22 @@ class TestRma(common.SavepointCase): cls.customer_location = cls.env.ref("stock.stock_location_customers") cls.supplier_location = cls.env.ref("stock.stock_location_suppliers") cls.product_uom_id = cls.env.ref("uom.product_uom_unit") + cls.g_rma_customer_user = cls.env.ref("rma.group_rma_customer_user") + cls.g_rma_manager = cls.env.ref("rma.group_rma_manager") + cls.g_rma_supplier_user = cls.env.ref("rma.group_rma_supplier_user") + cls.g_stock_user = cls.env.ref("stock.group_stock_user") + cls.g_stock_manager = cls.env.ref("stock.group_stock_manager") + + cls.rma_basic_user = cls._create_user( + "rma worker", + [cls.g_stock_user, cls.g_rma_customer_user, cls.g_rma_supplier_user], + cls.company, + ) + cls.rma_manager_user = cls._create_user( + "rma manager", + [cls.g_stock_manager, cls.g_rma_manager], + cls.company, + ) # Customer RMA: products2move = [(cls.product_1, 3), (cls.product_2, 5), (cls.product_3, 2)] cls.rma_customer_id = cls._create_rma_from_move( @@ -59,6 +77,21 @@ class TestRma(common.SavepointCase): products2move, "supplier", cls.env.ref("base.res_partner_2"), dropship=False ) + @classmethod + def _create_user(cls, login, groups, company): + group_ids = [group.id for group in groups] + user = cls.res_users_model.create( + { + "name": login, + "login": login, + "email": "example@yourcompany.com", + "company_id": company.id, + "company_ids": [(4, company.id)], + "groups_id": [(6, 0, group_ids)], + } + ) + return user + @classmethod def _create_product_category( cls, rma_approval_policy, rma_customer_operation_id, rma_supplier_operation_id diff --git a/rma_account_unreconciled/tests/test_rma_account_unreconciled.py b/rma_account_unreconciled/tests/test_rma_account_unreconciled.py new file mode 100644 index 00000000..27082e0d --- /dev/null +++ b/rma_account_unreconciled/tests/test_rma_account_unreconciled.py @@ -0,0 +1,120 @@ +# Copyright 2022 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + + +from odoo.addons.rma.tests.test_rma import TestRma + + +class TestRmaAccountUnreconciled(TestRma): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.rma_refund_wiz = cls.env["rma.refund"] + cls.g_account_manager = cls.env.ref("account.group_account_manager") + cls.rma_manager_user_account = cls._create_user( + "rma manager account", + [cls.g_stock_manager, cls.g_rma_manager, cls.g_account_manager], + cls.company, + ) + for categ in cls.rma_customer_id.with_user(cls.rma_manager_user_account).mapped( + "rma_line_ids.product_id.categ_id" + ): + categ.write( + { + "property_valuation": "real_time", + "property_cost_method": "fifo", + } + ) + categ.property_stock_account_input_categ_id.write( + { + "reconcile": True, + } + ) + categ.property_stock_account_output_categ_id.write( + { + "reconcile": True, + } + ) + for product in cls.rma_customer_id.with_user( + cls.rma_manager_user_account + ).mapped("rma_line_ids.product_id"): + product.write( + { + "standard_price": 10.0, + } + ) + + def test_unreconciled_moves(self): + for rma_line in self.rma_customer_id.rma_line_ids: + rma_line.write( + { + "refund_policy": "received", + } + ) + rma_line.action_rma_approve() + self.assertFalse(rma_line.unreconciled) + self.rma_customer_id.rma_line_ids.action_rma_to_approve() + wizard = self.rma_make_picking.with_context( + { + "active_ids": self.rma_customer_id.rma_line_ids.ids, + "active_model": "rma.order.line", + "picking_type": "incoming", + "active_id": 1, + } + ).create({}) + wizard._create_picking() + res = self.rma_customer_id.rma_line_ids.action_view_in_shipments() + picking = self.env["stock.picking"].browse(res["res_id"]) + picking.action_assign() + for mv in picking.move_lines: + mv.quantity_done = mv.product_uom_qty + picking.button_validate() + for rma_line in self.rma_customer_id.rma_line_ids: + rma_line._compute_unreconciled() + self.assertTrue(rma_line.unreconciled) + make_refund = self.rma_refund_wiz.with_context( + { + "customer": True, + "active_ids": self.rma_customer_id.rma_line_ids.ids, + "active_model": "rma.order.line", + } + ).create( + { + "description": "Test refund", + } + ) + for item in make_refund.item_ids: + item.write( + { + "qty_to_refund": item.product_qty, + } + ) + make_refund.invoice_refund() + self.rma_customer_id.with_user( + self.rma_manager_user_account + ).rma_line_ids.refund_line_ids.move_id.filtered( + lambda x: x.state != "posted" + ).action_post() + for rma_line in self.rma_customer_id.rma_line_ids: + rma_line._compute_unreconciled() + self.assertTrue(rma_line.unreconciled) + + self.assertEqual( + self.env["rma.order.line"].search_count( + [ + ("type", "=", "customer"), + ("unreconciled", "=", True), + ("rma_id", "=", self.rma_customer_id.id), + ] + ), + 3, + ) + for rma_line in self.rma_customer_id.rma_line_ids: + aml_domain = rma_line.sudo().action_view_unreconciled().get("domain") + aml_lines = ( + aml_domain and self.env["account.move.line"].search(aml_domain) or False + ) + if aml_lines: + aml_lines.reconcile() + rma_line._compute_unreconciled() + self.assertFalse(rma_line.unreconciled)