[MIG] helpdesk_transfer_timesheet_to_task: migrate to 18.0

This commit is contained in:
Stéphan Sainléger
2026-03-17 21:45:49 +01:00
parent 48185f0124
commit bf91f01387
8 changed files with 282 additions and 0 deletions

View File

@@ -0,0 +1 @@
from . import test_helpdesk_ticket

View File

@@ -0,0 +1,182 @@
from odoo.tests.common import TransactionCase
class TestHelpdeskTicket(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.HelpdeskTicket = cls.env["helpdesk.ticket"]
cls.ProjectTask = cls.env["project.task"]
cls.AccountAnalyticLine = cls.env["account.analytic.line"]
cls.AccountMove = cls.env["account.move"]
cls.Project = cls.env["project.project"]
cls.project = cls.Project.create(
{"name": "Test project", "allow_timesheets": True}
)
cls.analytic_account = cls.project.analytic_account_id
# Create a sample task
cls.task = cls.ProjectTask.create(
{"name": "Sample Task", "project_id": cls.project.id}
)
def test_timesheet_added_to_linked_task(self):
# Create a ticket
ticket = self.HelpdeskTicket.create(
{"name": "Test Ticket", "description": "My ticket"}
)
# Associate a task with the ticket
ticket.task_id = self.task.id
# Log time on the ticket
timesheet = self.AccountAnalyticLine.create(
{
"name": "Time Entry",
"ticket_id": ticket.id,
"unit_amount": 1.0,
"account_id": self.analytic_account.id,
}
)
timesheet.onchange_ticket_id()
# Check that timesheet is linked to the task
self.assertIn(timesheet, self.task.timesheet_ids)
def test_timesheet_added_to_new_task(self):
# Create a ticket
ticket = self.HelpdeskTicket.create(
{"name": "Test Ticket", "description": "My ticket"}
)
# Log time on the ticket
timesheet = self.AccountAnalyticLine.create(
{
"name": "Time Entry",
"ticket_id": ticket.id,
"unit_amount": 1.0,
"account_id": self.analytic_account.id,
}
)
# Associate a task with the ticket
ticket.task_id = self.task.id
ticket._onchange_task_id()
# Check that timesheet is linked to the task
self.assertIn(timesheet, self.task.timesheet_ids)
def test_timesheet_for_task_linked_to_several_tickets(self):
# Create a first ticket
ticket_1 = self.HelpdeskTicket.create(
{"name": "Test Ticket 1", "description": "My 1st ticket"}
)
# Log time on the first ticket
first_timesheet = self.AccountAnalyticLine.create(
{
"name": "First Time Entry",
"ticket_id": ticket_1.id,
"unit_amount": 1.0,
"account_id": self.analytic_account.id,
}
)
# Associate a task to the first ticket
ticket_1.task_id = self.task.id
ticket_1._onchange_task_id()
# Create a second ticket
ticket_2 = self.HelpdeskTicket.create(
{"name": "Test Ticket 2", "description": "My 2nd ticket"}
)
# Log time on the second ticket
second_timesheet = self.AccountAnalyticLine.create(
{
"name": "Second Time Entry",
"ticket_id": ticket_2.id,
"unit_amount": 2.0,
"account_id": self.analytic_account.id,
}
)
# Associate the same task to the second ticket
ticket_2.task_id = self.task.id
ticket_2._onchange_task_id()
# Check that both timesheets are linked to the task
self.assertIn(first_timesheet, self.task.timesheet_ids)
self.assertIn(second_timesheet, self.task.timesheet_ids)
def test_timesheet_moved_between_tasks(self):
# Create a second task
task_2 = self.ProjectTask.create(
{"name": "Second Task", "project_id": self.project.id}
)
# Create a ticket
ticket = self.HelpdeskTicket.create(
{"name": "Test Ticket", "description": "My ticket"}
)
# Log time on the ticket
timesheet = self.AccountAnalyticLine.create(
{
"name": "Time Entry for Moving",
"ticket_id": ticket.id,
"unit_amount": 1.0,
"account_id": self.analytic_account.id,
}
)
# Associate the first task with the ticket
ticket.task_id = self.task.id
ticket._onchange_task_id()
# Associate the second task with the ticket
ticket.task_id = task_2.id
ticket._onchange_task_id()
# Check if timesheet is moved to the second task
self.assertNotIn(timesheet, self.task.timesheet_ids)
self.assertIn(timesheet, task_2.timesheet_ids)
def test_timesheet_already_invoiced_are_not_moved(self):
journal = self.env["account.journal"].create(
{"name": "My journal", "code": "test", "type": "bank"}
)
account_move = self.AccountMove.create(
{"move_type": "entry", "journal_id": journal.id}
)
# Create a ticket
ticket = self.HelpdeskTicket.create(
{"name": "Test Ticket", "description": "My ticket"}
)
# Log already invoiced timesheet on the ticket
already_invoiced_timesheet = self.AccountAnalyticLine.create(
{
"name": "Already Invoiced Time Entry",
"ticket_id": ticket.id,
"unit_amount": 1.0,
"account_id": self.analytic_account.id,
"timesheet_invoice_id": account_move.id,
}
)
# Log not invoiced timesheet on the ticket
not_invoiced_timesheet = self.AccountAnalyticLine.create(
{
"name": "Not Invoiced Time Entry",
"ticket_id": ticket.id,
"unit_amount": 2.0,
"account_id": self.analytic_account.id,
}
)
# Associate a task with the ticket
ticket.task_id = self.task.id
ticket._onchange_task_id()
# Check already invoiced timesheet has not been moved
self.assertNotIn(already_invoiced_timesheet, self.task.timesheet_ids)
self.assertIn(not_invoiced_timesheet, self.task.timesheet_ids)