diff --git a/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py b/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py index f45f4e3a5..7ef8e7007 100644 --- a/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py +++ b/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py @@ -1,74 +1,45 @@ # Copyright 2019 Kitti Upariphutthiphong +# Copyright 2024 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import fields from odoo.exceptions import UserError, ValidationError -from odoo.tests import common -from odoo.tests.common import Form +from odoo.tests.common import Form, tagged +from odoo.tools import mute_logger +from odoo.addons.hr_expense.tests.common import TestExpenseCommon -class TestHrExpenseAdvanceClearing(common.TransactionCase): + +@tagged("-at_install", "post_install") +class TestHrExpenseAdvanceClearing(TestExpenseCommon): @classmethod - def setUpClass(cls): - super().setUpClass() - company = cls.env.ref("base.main_company") - cls.company_2 = cls.env["res.company"].create({"name": "Company 2"}) - cls.journal_bank = cls.env["account.journal"].search( - [("type", "=", "bank")], limit=1 - ) - cls.product = cls.env["product.product"].create( - {"name": "Service 1", "type": "service"} - ) - tax_group = cls.env["account.tax.group"].create( - {"name": "Tax Group 1", "sequence": 1} - ) - cls.tax = cls.env["account.tax"].create( - { - "name": "Tax 10.0%", - "amount": 10.0, - "amount_type": "percent", - "type_tax_use": "purchase", - "company_id": company.id, - "tax_group_id": tax_group.id, - "price_include": True, - } - ) - employee_home = cls.env["res.partner"].create({"name": "Employee Home Address"}) - cls.employee = cls.env["hr.employee"].create( - {"name": "Employee A", "address_home_id": employee_home.id} - ) - advance_account = cls.env["account.account"].create( - { - "code": "154000", - "name": "Employee Advance", - "account_type": "asset_current", - "reconcile": True, - } - ) - cls.account_sales = cls.env["account.account"].create( - { - "code": "X1020", - "name": "Product Sales - (test)", - "account_type": "asset_current", - } - ) + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + advance_account = cls.company_data["default_account_assets"] + advance_account.reconcile = True cls.emp_advance = cls.env.ref("hr_expense_advance_clearing.product_emp_advance") cls.emp_advance.property_account_expense_id = advance_account + cls.product_a.standard_price = 0 # Create advance expense 1,000 cls.advance = cls._create_expense_sheet( - cls, "Advance 1,000", cls.employee, cls.emp_advance, 1000.0, advance=True + cls, + "Advance 1,000", + cls.expense_employee, + cls.emp_advance, + 1000.0, + advance=True, ) # Create clearing expense 1,000 cls.clearing_equal = cls._create_expense_sheet( - cls, "Buy service 1,000", cls.employee, cls.product, 1000.0 + cls, "Buy service 1,000", cls.expense_employee, cls.product_a, 1000.0 ) # Create clearing expense 1,200 cls.clearing_more = cls._create_expense_sheet( - cls, "Buy service 1,200", cls.employee, cls.product, 1200.0 + cls, "Buy service 1,200", cls.expense_employee, cls.product_a, 1200.0 ) # Create clearing expense 800 cls.clearing_less = cls._create_expense_sheet( - cls, "Buy service 800", cls.employee, cls.product, 800.0 + cls, "Buy service 800", cls.expense_employee, cls.product_a, 800.0 ) def _create_expense( @@ -120,7 +91,7 @@ def _register_payment(self, move_id, amount, ctx=False, hr_return_advance=False) ctx["hr_return_advance"] = hr_return_advance PaymentWizard = self.env["account.payment.register"] with Form(PaymentWizard.with_context(**ctx)) as f: - f.journal_id = self.journal_bank + f.journal_id = self.company_data["default_journal_bank"] f.payment_date = fields.Date.today() f.amount = amount payment_wizard = f.save() @@ -136,31 +107,37 @@ def test_0_test_constraints(self): with self.assertRaises(ValidationError): expense = self._create_expense( "Advance 1,000", - self.employee, + self.expense_employee, self.emp_advance, 1.0, advance=True, ) - expense.account_id = self.account_sales.id + expense.account_id = self.company_data["default_account_payable"] expense._check_advance() # Advance Sheet should not have > 1 expense lines with self.assertRaises(ValidationError): expense = self._create_expense( - "Buy service 1,000", self.employee, self.product, 1.0 + "Buy service 1,000", self.expense_employee, self.product_a, 1.0 ) self.advance.write({"expense_line_ids": [(4, expense.id)]}) # Advance Expense's product, must not has tax involved with self.assertRaises(ValidationError): - self.emp_advance.supplier_taxes_id |= self.tax + self.emp_advance.supplier_taxes_id |= self.company_data[ + "default_tax_purchase" + ] expense = self._create_expense( - "Advance 1,000", self.employee, self.emp_advance, 1.0, advance=True + "Advance 1,000", + self.expense_employee, + self.emp_advance, + 1.0, + advance=True, ) self.emp_advance.supplier_taxes_id = False # Remove tax bf proceed # Advance Expense, must not paid by company with self.assertRaises(ValidationError): expense = self._create_expense( "Advance 1,000", - self.employee, + self.expense_employee, self.emp_advance, 1.0, advance=True, @@ -170,20 +147,25 @@ def test_0_test_constraints(self): with self.assertRaises(ValidationError): expense = self._create_expense( "Advance 1,000", - self.employee, + self.expense_employee, self.emp_advance, 1.0, advance=True, ) - expense.product_id = self.product.id + expense.product_id = self.product_a expense._check_advance() # Advance Expense's product must have account configured with self.assertRaises(ValidationError): self.emp_advance.property_account_expense_id = False expense = self._create_expense( - "Advance 1,000", self.employee, self.emp_advance, 1.0, advance=True + "Advance 1,000", + self.expense_employee, + self.emp_advance, + 1.0, + advance=True, ) + @mute_logger("odoo.models.unlink") def test_1_clear_equal_advance(self): """When clear equal advance, all set""" # ------------------ Advance -------------------------- @@ -220,10 +202,10 @@ def test_1_clear_equal_advance(self): len(clearing_dict["domain"][0][2]), self.advance.clearing_count ) # Check advance from employee - self.assertEqual(self.employee.advance_count, 1) - clearing_document = self.employee.action_open_advance_clearing() + self.assertEqual(self.expense_employee.advance_count, 1) + clearing_document = self.expense_employee.action_open_advance_clearing() self.assertEqual( - len(clearing_document["domain"][0][2]), self.employee.advance_count + len(clearing_document["domain"][0][2]), self.expense_employee.advance_count ) # Check back state move in advance after create clearing with self.assertRaises(UserError): @@ -233,6 +215,7 @@ def test_1_clear_equal_advance(self): with self.assertRaises(UserError): self.advance.account_move_id._reverse_moves() + @mute_logger("odoo.models.unlink") def test_2_clear_more_than_advance(self): """When clear more than advance, do pay more""" # ------------------ Advance -------------------------- @@ -255,6 +238,7 @@ def test_2_clear_more_than_advance(self): self._register_payment(self.clearing_more.account_move_id, 200.0) self.assertEqual(self.clearing_more.state, "done") + @mute_logger("odoo.models.unlink") def test_3_clear_less_than_advance(self): """When clear less than advance, do return advance""" # ------------------ Advance -------------------------- @@ -310,10 +294,11 @@ def test_3_clear_less_than_advance(self): ) self.assertEqual(len(payment), 1) + @mute_logger("odoo.models.unlink") def test_4_clearing_product_advance(self): """When select clearing product on advance""" # ------------------ Advance -------------------------- - self.advance.expense_line_ids.clearing_product_id = self.product + self.advance.expense_line_ids.clearing_product_id = self.product_a self.advance.action_submit_sheet() self.advance.approve_expense_sheets() self.advance.action_sheet_move_create() @@ -323,7 +308,7 @@ def test_4_clearing_product_advance(self): # ------------------ Clearing -------------------------- with Form(self.env["hr.expense.sheet"]) as sheet: sheet.name = "Test Clearing" - sheet.employee_id = self.employee + sheet.employee_id = self.expense_employee ex_sheet = sheet.save() ex_sheet.advance_sheet_id = self.advance self.assertEqual(len(ex_sheet.expense_line_ids), 0) diff --git a/hr_expense_cancel/tests/test_hr_expense_cancel.py b/hr_expense_cancel/tests/test_hr_expense_cancel.py index b7a0cd924..4b870fb21 100644 --- a/hr_expense_cancel/tests/test_hr_expense_cancel.py +++ b/hr_expense_cancel/tests/test_hr_expense_cancel.py @@ -1,76 +1,52 @@ # Copyright 2019 Tecnativa - Ernesto Tejeda +# Copyright 2024 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo.exceptions import UserError -from odoo.tests.common import Form, TransactionCase - - -class TestHrExpenseCancel(TransactionCase): - def setUp(self): - super().setUp() - self.partner = self.env["res.partner"].create({"name": "Test partner"}) - self.payment_obj = self.env["account.payment"] - self.account_payment_register = self.env["account.payment.register"] - self.payment_journal = self.env["account.journal"].search( - [("type", "in", ["cash", "bank"])], limit=1 - ) - - self.main_company = company = self.env.ref("base.main_company") - self.expense_journal = self.env["account.journal"].create( - { - "name": "Purchase Journal - Test", - "code": "HRTPJ", - "type": "purchase", - "company_id": company.id, - } - ) - - self.expense_sheet = self.env["hr.expense.sheet"].create( - { - "employee_id": self.ref("hr.employee_admin"), - "name": "Expense test", - "journal_id": self.expense_journal.id, - } - ) - self.expense_sheet.approve_expense_sheets() - - self.expense = self.env["hr.expense"].create( - { - "name": "Expense test", - "employee_id": self.ref("hr.employee_admin"), - "product_id": self.ref( - "hr_expense.expense_product_travel_accommodation" - ), - "total_amount": 10, - "sheet_id": self.expense_sheet.id, - } +from odoo.tests.common import Form, tagged + +from odoo.addons.hr_expense.tests.common import TestExpenseCommon + + +@tagged("-at_install", "post_install") +class TestHrExpenseCancel(TestExpenseCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + # Create expense + sheet + approve + expense_form = Form( + cls.env["hr.expense"].with_context( + default_product_id=cls.product_a.id, + default_employee_id=cls.expense_employee.id, + ) ) - - def _get_payment_wizard(self, expense_sheet): - action = expense_sheet.action_register_payment() - ctx = action.get("context") - with Form( - self.account_payment_register.with_context(**ctx), - view="account.view_account_payment_register_form", - ) as f: - f.journal_id = self.payment_journal - f.amount = expense_sheet.total_amount - register_payment = f.save() - return register_payment + expense_form.name = "Test expense" + cls.expense = expense_form.save() + res = cls.expense.action_submit_expenses() + sheet_form = Form(cls.env[res["res_model"]].with_context(**res["context"])) + cls.expense_sheet = sheet_form.save() + cls.expense_sheet.approve_expense_sheets() + + def _get_payment_wizard(self): + res = self.expense_sheet.action_register_payment() + register_form = Form(self.env[res["res_model"]].with_context(**res["context"])) + register_form.journal_id = self.company_data["default_journal_bank"] + register_form.amount = self.expense_sheet.total_amount + return register_form.save() def test_action_cancel_posted(self): self.expense_sheet.action_sheet_move_create() - self.assertFalse(len(self.expense_sheet.payment_ids), 1) self.assertTrue(self.expense_sheet.account_move_id) - self.expense_sheet.action_cancel() - self.assertFalse(self.expense_sheet.payment_ids) self.assertFalse(self.expense_sheet.account_move_id) def test_action_cancel_no_update_posted(self): - journals = self.payment_journal | self.expense_journal + journals = ( + self.company_data["default_journal_bank"] + | self.company_data["default_journal_purchase"] + ) journals.write({"restrict_mode_hash_table": True}) self.test_action_cancel_company_account() with self.assertRaises(UserError): @@ -85,11 +61,8 @@ def test_action_cancel_company_account(self): def test_action_cancel_own_account(self): self.expense_sheet.action_sheet_move_create() - - payment_wizard = self._get_payment_wizard(self.expense_sheet) + payment_wizard = self._get_payment_wizard() payment_wizard.action_create_payments() - self.assertTrue(self.expense_sheet.payment_ids) - self.expense_sheet.action_cancel() # assertFalse(payment.exist) self.assertFalse(self.expense_sheet.payment_ids.state != "cancel") diff --git a/hr_expense_invoice/tests/test_hr_expense_invoice.py b/hr_expense_invoice/tests/test_hr_expense_invoice.py index d2677dd1d..9b2280490 100644 --- a/hr_expense_invoice/tests/test_hr_expense_invoice.py +++ b/hr_expense_invoice/tests/test_hr_expense_invoice.py @@ -35,22 +35,6 @@ def setUpClass(cls, chart_template_ref=None): "invoice_date": fields.Date.today(), } ) - cls.expense_employee.address_home_id.write( - { - "bank_ids": [ - ( - 0, - 0, - { - "acc_number": "FR20 1242 1242 1242 1242 1242 124", - }, - ) - ], - } - ) - cls.expense_employee.bank_account_id = ( - cls.expense_employee.address_home_id.bank_ids - ) cls.expense = cls.env["hr.expense"].create( { "name": "Expense test", diff --git a/hr_expense_payment/tests/test_hr_expense_payment.py b/hr_expense_payment/tests/test_hr_expense_payment.py index 8b7286e38..1faf9f2a9 100644 --- a/hr_expense_payment/tests/test_hr_expense_payment.py +++ b/hr_expense_payment/tests/test_hr_expense_payment.py @@ -1,84 +1,59 @@ # Copyright 2019 Tecnativa - Ernesto Tejeda # Copyright 2021 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# Copyright 2024 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo.tests.common import Form, TransactionCase +from odoo.tests import Form, tagged -from ..hooks import post_init_hook - - -class TestHrExpensePayment(TransactionCase): - def setUp(self): - super().setUp() - self.account_payment_register = self.env["account.payment.register"] - self.payment_journal = self.env["account.journal"].search( - [("type", "in", ["cash", "bank"])], limit=1 - ) +from odoo.addons.hr_expense.tests.common import TestExpenseCommon - company = self.env.ref("base.main_company") - self.expense_journal = self.env["account.journal"].create( - { - "name": "Purchase Journal - Test", - "code": "HRTPJ", - "type": "purchase", - "company_id": company.id, - } - ) +from ..hooks import post_init_hook - self.expense_sheet = self.env["hr.expense.sheet"].create( - { - "employee_id": self.ref("hr.employee_admin"), - "name": "Expense test", - "journal_id": self.expense_journal.id, - } - ) - self.expense_sheet.approve_expense_sheets() - self.expense = self.env["hr.expense"].create( - { - "name": "Expense test", - "employee_id": self.ref("hr.employee_admin"), - "product_id": self.ref("hr_expense.expense_product_meal"), - "total_amount": 1000, - "sheet_id": self.expense_sheet.id, - } +@tagged("-at_install", "post_install") +class TestHrExpensePayment(TestExpenseCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + # Create expense + sheet + approve + expense_form = Form( + cls.env["hr.expense"].with_context( + default_product_id=cls.product_a.id, + default_employee_id=cls.expense_employee.id, + ) ) - - def _get_payment_wizard(self, expense_sheet): - action = expense_sheet.action_register_payment() - ctx = action.get("context") - with Form( - self.account_payment_register.with_context(**ctx), - view="account.view_account_payment_register_form", - ) as f: - f.journal_id = self.payment_journal - f.amount = self.expense_sheet.total_amount - register_payment = f.save() - return register_payment + expense_form.name = "Test expense" + cls.expense = expense_form.save() + res = cls.expense.action_submit_expenses() + sheet_form = Form(cls.env[res["res_model"]].with_context(**res["context"])) + cls.expense_sheet = sheet_form.save() + cls.expense_sheet.approve_expense_sheets() + + def _get_payment_wizard(self): + res = self.expense_sheet.action_register_payment() + register_form = Form(self.env[res["res_model"]].with_context(**res["context"])) + register_form.journal_id = self.company_data["default_journal_bank"] + register_form.amount = self.expense_sheet.total_amount + return register_form.save() def test_post_init_hook(self): self.expense_sheet.action_sheet_move_create() - payment_wizard = self._get_payment_wizard(self.expense_sheet) + payment_wizard = self._get_payment_wizard() payment_wizard.action_create_payments() - payment = self.expense_sheet.payment_ids - self.assertEqual(len(payment), 1) self.assertEqual(len(payment.expense_sheet_ids), 1) - payment.expense_sheet_ids = False # Recompute many2one payment = self.expense_sheet.payment_ids - self.assertFalse(payment) self.assertFalse(payment.expense_sheet_ids) post_init_hook(self.env.cr, self.registry) - self.assertEqual(len(self.expense_sheet.payment_ids), 1) def test_get_payment_vals(self): self.expense_sheet.action_sheet_move_create() - payment_wizard = self._get_payment_wizard(self.expense_sheet) + payment_wizard = self._get_payment_wizard() self.assertFalse(self.expense_sheet.payment_ids) payment_wizard.action_create_payments() self.assertEqual(len(self.expense_sheet.payment_ids), 1) diff --git a/hr_expense_tier_validation/tests/test_hr_expense_tier_validation.py b/hr_expense_tier_validation/tests/test_hr_expense_tier_validation.py index 4afff53cc..8ac768ebe 100644 --- a/hr_expense_tier_validation/tests/test_hr_expense_tier_validation.py +++ b/hr_expense_tier_validation/tests/test_hr_expense_tier_validation.py @@ -1,38 +1,27 @@ # Copyright 2019 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# Copyright 2024 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo.exceptions import ValidationError -from odoo.tests.common import Form, TransactionCase +from odoo.tests.common import Form, tagged +from odoo.addons.hr_expense.tests.common import TestExpenseCommon -class TestHrExpenseTierValidation(TransactionCase): - def setUp(self): - super().setUp() - self.tier_def_obj = self.env["tier.definition"] - self.expense_sheet_model = self.env["hr.expense.sheet"] - self.partner = self.env.ref("base.res_partner_2") - # Create users: - group_ids = self.env.ref("base.group_system").ids - self.test_user_1 = self.env["res.users"].create( - {"name": "John", "login": "test1", "groups_id": [(6, 0, group_ids)]} - ) + +@tagged("-at_install", "post_install") +class TestHrExpenseTierValidation(TestExpenseCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + cls.tier_def_obj = cls.env["tier.definition"] # Create tier validation - self.tier_def_obj.create( + cls.tier_def_obj.create( { - "model_id": self.env["ir.model"] - .search([("model", "=", "hr.expense.sheet")]) - .id, + "model_id": cls.env.ref("hr_expense.model_hr_expense_sheet").id, "review_type": "individual", - "reviewer_id": self.test_user_1.id, + "reviewer_id": cls.expense_user_manager.id, } ) - employee_home = self.env["res.partner"].create( - {"name": "Employee Home Address"} - ) - self.employee = self.env["hr.employee"].create( - {"name": "Employee A", "address_home_id": employee_home.id} - ) - self.product_1 = self.env.ref("product.product_product_1") def _create_expense( self, @@ -56,14 +45,14 @@ def test_get_tier_validation_model_names(self): def test_edit_value_expense(self): expense = self._create_expense( "Test - Expense", - self.employee, - self.product_1, + self.expense_employee, + self.product_a, ) sheet_dict = expense.action_submit_expenses() sheet_dict = sheet_dict["context"] with Form(self.env["hr.expense.sheet"]) as sheet: sheet.name = (sheet_dict["default_name"],) - sheet.employee_id = self.employee + sheet.employee_id = self.expense_employee sheet = sheet.save() sheet.expense_line_ids = [(6, 0, expense.id)] self.assertEqual(sheet.state, "draft") @@ -75,17 +64,16 @@ def test_edit_value_expense(self): sheet.request_validation() self.assertTrue(sheet) sheet.invalidate_model() - # tier validation but state still submit self.assertEqual(sheet.state, "submit") # not allow edit expense when under validation with self.assertRaises(ValidationError): - sheet.review_ids = [(6, 0, self.test_user_1.ids)] + sheet.review_ids = [(6, 0, self.expense_user_manager.ids)] with Form(sheet) as s: s.name = "New name" with self.assertRaises(ValidationError): with Form(expense) as exp: exp.name = "Change name" # Test change message follower - message = expense.write({"message_follower_ids": self.partner.ids}) - self.assertEqual(message, True) + message = expense._message_subscribe(self.partner_a.ids) + self.assertTrue(message, True)