diff --git a/addons/hr_timesheet/models/project.py b/addons/hr_timesheet/models/project.py index c4404ae1592..c44381ef79a 100644 --- a/addons/hr_timesheet/models/project.py +++ b/addons/hr_timesheet/models/project.py @@ -84,7 +84,7 @@ class Task(models.Model): _name = "project.task" _inherit = ["project.task", "timer.mixin"] - analytic_account_active = fields.Boolean("Analytic Account", related='project_id.analytic_account_id.active', readonly=True) + analytic_account_active = fields.Boolean("Active Analytic Account", compute='_compute_analytic_account_active') allow_timesheets = fields.Boolean("Allow timesheets", related='project_id.allow_timesheets', help="Timesheets can be logged on this task.", readonly=True) remaining_hours = fields.Float("Remaining Hours", compute='_compute_remaining_hours', store=True, readonly=True, help="Total remaining time, can be re-estimated periodically by the assignee of the task.") effective_hours = fields.Float("Hours Spent", compute='_compute_effective_hours', compute_sudo=True, store=True, help="Computed using the sum of the task work done.") @@ -133,6 +133,12 @@ class Task(models.Model): 'display_timer_resume': resume, }) + @api.depends('project_id.analytic_account_id.active') + def _compute_analytic_account_active(self): + """ Overridden in sale_timesheet """ + for task in self: + task.analytic_account_active = task.project_id.analytic_account_id.active + @api.depends('timesheet_ids.unit_amount') def _compute_effective_hours(self): for task in self: diff --git a/addons/sale_timesheet/models/account.py b/addons/sale_timesheet/models/account.py index 733b36c20ab..7bba1729ecb 100644 --- a/addons/sale_timesheet/models/account.py +++ b/addons/sale_timesheet/models/account.py @@ -68,6 +68,11 @@ class AccountAnalyticLine(models.Model): @api.model def _timesheet_preprocess(self, values): + if values.get('task_id') and not values.get('account_id'): + task = self.env['project.task'].browse(values.get('task_id')) + if task.analytic_account_id: + values['account_id'] = task.analytic_account_id.id + values['company_id'] = task.analytic_account_id.company_id.id values = super(AccountAnalyticLine, self)._timesheet_preprocess(values) # task implies so line (at create) if 'task_id' in values and not values.get('so_line') and values.get('employee_id'): diff --git a/addons/sale_timesheet/models/project.py b/addons/sale_timesheet/models/project.py index 90e69b533e7..a4a050d9649 100644 --- a/addons/sale_timesheet/models/project.py +++ b/addons/sale_timesheet/models/project.py @@ -127,6 +127,7 @@ class ProjectTask(models.Model): # override sale_order_id and make it computed stored field instead of regular field. sale_order_id = fields.Many2one(compute='_compute_sale_order_id', store=True, readonly=False) + analytic_account_id = fields.Many2one('account.analytic.account', related='sale_order_id.analytic_account_id') billable_type = fields.Selection([ ('task_rate', 'At Task Rate'), ('employee_rate', 'At Employee Rate'), @@ -161,6 +162,12 @@ class ProjectTask(models.Model): 'message': message }} + @api.depends('analytic_account_id.active') + def _compute_analytic_account_active(self): + super()._compute_analytic_account_active() + for task in self: + task.analytic_account_active = task.analytic_account_active or task.analytic_account_id.active + @api.depends('sale_line_id', 'project_id', 'billable_type') def _compute_sale_order_id(self): for task in self: diff --git a/addons/sale_timesheet/views/project_task_views.xml b/addons/sale_timesheet/views/project_task_views.xml index 055ddb56dc2..b5c7aa3caba 100644 --- a/addons/sale_timesheet/views/project_task_views.xml +++ b/addons/sale_timesheet/views/project_task_views.xml @@ -81,6 +81,9 @@ + + +