[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,2 @@
*.*~
*pyc

View File

@@ -0,0 +1,60 @@
===================================
helpdesk_transfer_timesheet_to_task
===================================
Automatically transfer timesheets from a ticket to its linked task.
This module extends the ``helpdesk_mgmt_timesheet`` module by automatically
transferring timesheet entries when a task is linked to a ticket. It provides:
* An onchange handler on the ``task_id`` field of helpdesk tickets.
* When a task is linked to a ticket, all timesheets from the ticket are
automatically assigned to the task.
* Protection against double invoicing: timesheets that have already been
invoiced (i.e., have a ``timesheet_invoice_id``) are NOT transferred to the
task and remain on the ticket.
This is particularly useful when a ticket is converted to a task and you want
to keep the time tracking history on the task for project management and
invoicing purposes.
# Installation
Use Odoo normal module installation procedure to install
``helpdesk_transfer_timesheet_to_task``.
This module depends on:
* ``helpdesk_mgmt``: provides the base helpdesk ticket functionality.
* ``helpdesk_mgmt_project``: provides the link between tickets and projects
(including the ``task_id`` field on tickets).
* ``helpdesk_mgmt_timesheet``: provides timesheet functionality on tickets.
* ``sale_timesheet``: provides the invoicing link on timesheets
(``timesheet_invoice_id`` field).
# Known issues / Roadmap
None.
# Bug Tracker
Bugs are tracked on [our issues website](https://git.elabore.coop/Elabore/helpdesk-tools/issues). In case of
trouble, please check there if your issue has already been
reported. If you spotted it first, help us smashing it by providing a
detailed and welcomed feedback.
# Credits
## Contributors
- Quentin Mondot
## Funders
The development of this module has been financially supported by:
- Elabore (https://elabore.coop)
## Maintainer
This module is maintained by Elabore.

View File

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

View File

@@ -0,0 +1,23 @@
# Copyright 2024 Quentin Mondot
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "helpdesk_transfer_timesheet_to_task",
"version": "18.0.1.0.0",
"author": "Elabore",
"website": "https://elabore.coop",
"maintainer": "Quentin Mondot",
"license": "AGPL-3",
"category": "Tools",
"summary": "This module assigns timesheets to the task linked to a ticket.",
"depends": [
"base",
"helpdesk_mgmt",
"helpdesk_mgmt_project",
"helpdesk_mgmt_timesheet",
"sale_timesheet"
],
"data": [],
"installable": True,
"auto_install": False,
"application": False,
}

View File

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

View File

@@ -0,0 +1,12 @@
from odoo import models, api, Command
class HelpdeskTicket(models.Model):
_inherit = "helpdesk.ticket"
@api.onchange("task_id")
def _onchange_task_id(self):
for record in self:
if record.timesheet_ids and record.task_id:
not_yet_invoiced_timesheet_ids = [t.id for t in record.timesheet_ids if not t.timesheet_invoice_id]
record.task_id.timesheet_ids = [Command.link(t_id) for t_id in not_yet_invoiced_timesheet_ids]

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)