|
| 1 | +from odoo.tests import Form |
| 2 | +from odoo.tests.common import TransactionCase |
| 3 | + |
| 4 | + |
| 5 | +class TestAccountAnalyticLine(TransactionCase): |
| 6 | + @classmethod |
| 7 | + def setUpClass(cls): |
| 8 | + super().setUpClass() |
| 9 | + |
| 10 | + # Create companies |
| 11 | + cls.company_1 = cls.env["res.company"].create( |
| 12 | + { |
| 13 | + "name": "Test Company 1", |
| 14 | + } |
| 15 | + ) |
| 16 | + cls.company_2 = cls.env["res.company"].create( |
| 17 | + { |
| 18 | + "name": "Test Company 2", |
| 19 | + } |
| 20 | + ) |
| 21 | + |
| 22 | + # Create projects |
| 23 | + cls.project_1 = cls.env["project.project"].create( |
| 24 | + { |
| 25 | + "name": "Test Project 1", |
| 26 | + "company_id": cls.company_1.id, |
| 27 | + "allow_timesheets": True, |
| 28 | + } |
| 29 | + ) |
| 30 | + cls.project_2 = cls.env["project.project"].create( |
| 31 | + { |
| 32 | + "name": "Test Project 2", |
| 33 | + "company_id": cls.company_2.id, |
| 34 | + "allow_timesheets": True, |
| 35 | + } |
| 36 | + ) |
| 37 | + cls.project_no_timesheet = cls.env["project.project"].create( |
| 38 | + { |
| 39 | + "name": "Project No Timesheet", |
| 40 | + "company_id": cls.company_1.id, |
| 41 | + "allow_timesheets": False, |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + # Create tasks |
| 46 | + cls.task_1 = cls.env["project.task"].create( |
| 47 | + { |
| 48 | + "name": "Test Task 1", |
| 49 | + "project_id": cls.project_1.id, |
| 50 | + "company_id": cls.company_1.id, |
| 51 | + } |
| 52 | + ) |
| 53 | + cls.task_2 = cls.env["project.task"].create( |
| 54 | + { |
| 55 | + "name": "Test Task 2", |
| 56 | + "project_id": cls.project_2.id, |
| 57 | + "company_id": cls.company_2.id, |
| 58 | + } |
| 59 | + ) |
| 60 | + cls.task_closed = cls.env["project.task"].create( |
| 61 | + { |
| 62 | + "name": "Closed Task", |
| 63 | + "project_id": cls.project_1.id, |
| 64 | + "company_id": cls.company_1.id, |
| 65 | + "state": "done", |
| 66 | + } |
| 67 | + ) |
| 68 | + cls.task_no_timesheet = cls.env["project.task"].create( |
| 69 | + { |
| 70 | + "name": "Task No Timesheet", |
| 71 | + "project_id": cls.project_no_timesheet.id, |
| 72 | + "company_id": cls.company_1.id, |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + def test_task_id_domain_with_project(self): |
| 77 | + """Test task_id domain when project_id is set""" |
| 78 | + analytic_line = self.env["account.analytic.line"].create( |
| 79 | + { |
| 80 | + "name": "Test Line", |
| 81 | + "project_id": self.project_1.id, |
| 82 | + "company_id": self.company_1.id, |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + # Use Form to test domain |
| 87 | + with Form(analytic_line) as line_form: |
| 88 | + # Should allow tasks from same project and company |
| 89 | + line_form.task_id = self.task_1 |
| 90 | + |
| 91 | + # Should not allow tasks from different project |
| 92 | + with self.assertRaises(AssertionError): |
| 93 | + line_form.task_id = self.task_2 |
| 94 | + |
| 95 | + # Should not allow closed tasks |
| 96 | + with self.assertRaises(AssertionError): |
| 97 | + line_form.task_id = self.task_closed |
| 98 | + |
| 99 | + # Should not allow tasks from projects without timesheets |
| 100 | + with self.assertRaises(AssertionError): |
| 101 | + line_form.task_id = self.task_no_timesheet |
| 102 | + |
| 103 | + def test_task_id_domain_without_project(self): |
| 104 | + """Test task_id domain when project_id is not set""" |
| 105 | + analytic_line = self.env["account.analytic.line"].create( |
| 106 | + { |
| 107 | + "name": "Test Line", |
| 108 | + "company_id": self.company_1.id, |
| 109 | + } |
| 110 | + ) |
| 111 | + |
| 112 | + # Use Form to test domain |
| 113 | + with Form(analytic_line) as line_form: |
| 114 | + # Should allow tasks from projects with timesheets |
| 115 | + line_form.task_id = self.task_1 |
| 116 | + |
| 117 | + # Should not allow tasks from different company |
| 118 | + with self.assertRaises(AssertionError): |
| 119 | + line_form.task_id = self.task_2 |
| 120 | + |
| 121 | + # Should not allow tasks from projects without timesheets |
| 122 | + with self.assertRaises(AssertionError): |
| 123 | + line_form.task_id = self.task_no_timesheet |
| 124 | + |
| 125 | + # Should not allow closed tasks |
| 126 | + with self.assertRaises(AssertionError): |
| 127 | + line_form.task_id = self.task_closed |
| 128 | + |
| 129 | + def test_task_id_company_consistency(self): |
| 130 | + """Test company consistency between task and analytic line""" |
| 131 | + # Create analytic line with company_1 |
| 132 | + analytic_line = self.env["account.analytic.line"].create( |
| 133 | + { |
| 134 | + "name": "Test Line", |
| 135 | + "company_id": self.company_1.id, |
| 136 | + } |
| 137 | + ) |
| 138 | + |
| 139 | + # Should be able to set task from same company |
| 140 | + analytic_line.task_id = self.task_1 |
| 141 | + self.assertEqual(analytic_line.task_id, self.task_1) |
| 142 | + |
| 143 | + # Should not be able to set task from different company |
| 144 | + with self.assertRaises(AssertionError): |
| 145 | + analytic_line.task_id = self.task_2 |
0 commit comments