Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project_task_analytic_lines/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
'name': 'Project Task Analytic Lines',
'version': '14.0.1.2.1',
'version': '14.0.1.3.0',
'author': 'Numigi',
'maintainer': 'Numigi',
'website': 'https://numigi.com/',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# © Numigi (tm) and all its contributors (https://numigi.com/r/home)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging

_logger = logging.getLogger(__name__)


def migrate(cr, version):
"""
Post-migration script to update the project_id on existing analytic lines.
It uses raw SQL for better performance on large tables.
"""
if not version:
return

_logger.info("Starting the update of project_id on analytic lines...")

# Set project_id from task_id if available
cr.execute("""
UPDATE account_analytic_line aal
SET project_id = pt.project_id
FROM project_task pt
WHERE aal.project_id IS NULL
AND aal.task_id = pt.id
AND pt.project_id IS NOT NULL
""")

# Set project_id from origin_task_id if task_id did not provide a project
cr.execute("""
UPDATE account_analytic_line aal
SET project_id = pt.project_id
FROM project_task pt
WHERE aal.project_id IS NULL
AND aal.origin_task_id = pt.id
AND pt.project_id IS NOT NULL
""")

_logger.info("Analytic lines successfully updated.")
9 changes: 9 additions & 0 deletions project_task_analytic_lines/models/account_analytic_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class AnalyticLine(models.Model):
"project.task", "Origin Task", ondelete="restrict", index=True
)

@api.depends('task_id', 'task_id.project_id', 'origin_task_id',
'origin_task_id.project_id')
def _compute_project_id(self):
for line in self.filtered(lambda line: not line.project_id):
if line.task_id.project_id:
line.project_id = line.task_id.project_id
if not line.task_id.project_id and line.origin_task_id.project_id:
line.project_id = line.origin_task_id.project_id

@api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
args = args or []
Expand Down
10 changes: 8 additions & 2 deletions project_task_analytic_lines/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# © Numigi (tm) and all its contributors (https://numigi.com/r/home)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

Expand All @@ -14,7 +15,11 @@ def setUpClass(cls):
"login": "account_user",
"email": "account_user@test.com",
"groups_id": [
(4, cls.env.ref("account.group_account_invoice").id)],
(4, cls.env.ref("account.group_account_invoice").id),
(4, cls.env.ref("analytic.group_analytic_accounting").id),
(4, cls.env.ref("project.group_project_manager").id),
(4, cls.env.ref("hr_timesheet.group_timesheet_manager").id),
],
}
)

Expand Down Expand Up @@ -110,4 +115,5 @@ def _get_invoice_line_vals(cls, **kwargs):

def _validate_invoice(self):
self.invoice.invoice_date = '2023-01-01'
self.invoice.sudo(self.account_user).action_post()
# In Odoo 14, use .with_user(user) instead of the deprecated .sudo(user)
self.invoice.with_user(self.account_user).action_post()