# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class Project(models.Model):
    _inherit = "project.project"

    subtask_project_id = fields.Many2one(
        'project.project', string='Sub-task Project', ondelete="restrict",
        help="Choosing a sub-tasks project will both enable sub-tasks and set their default project (possibly the project itself)")
    allow_timesheets = fields.Boolean("Allow timesheets", default=True)

class Task(models.Model):
    _inherit = "project.task"

    @api.multi
    def _get_subtask_count(self):
        for task in self:
            task.subtask_count = self.search_count([('id', 'child_of', task.id), ('id', '!=', task.id)])

    @api.depends('stage_id', 'timesheet_ids.unit_amount', 'planned_hours', 'child_ids.stage_id',
                 'child_ids.planned_hours', 'child_ids.effective_hours', 'child_ids.children_hours', 'child_ids.timesheet_ids.unit_amount')
    def _hours_get(self):
        for task in self.sorted(key='id', reverse=True):
            children_hours = 0
            for child_task in task.child_ids:
                if child_task.stage_id and child_task.stage_id.fold:
                    children_hours += child_task.effective_hours + child_task.children_hours
                else:
                    children_hours += max(child_task.planned_hours, child_task.effective_hours + child_task.children_hours)

            task.children_hours = children_hours
            task.effective_hours = sum(task.timesheet_ids.mapped('unit_amount'))
            task.remaining_hours = task.planned_hours - task.effective_hours - task.children_hours
            task.total_hours = max(task.planned_hours, task.effective_hours)
            task.total_hours_spent = task.effective_hours + task.children_hours
            task.delay_hours = max(-task.remaining_hours, 0.0)

            if task.stage_id and task.stage_id.fold:
                task.progress = 100.0
            elif (task.planned_hours > 0.0):
                task.progress = round(100.0 * (task.effective_hours + task.children_hours) / task.planned_hours, 2)
            else:
                task.progress = 0.0

    remaining_hours = fields.Float(compute='_hours_get', store=True, string='Remaining Hours', help="Total remaining time, can be re-estimated periodically by the assignee of the task.")
    effective_hours = fields.Float(compute='_hours_get', store=True, string='Hours Spent', help="Computed using the sum of the task work done.")
    total_hours = fields.Float(compute='_hours_get', store=True, string='Total', help="Computed as: Time Spent + Remaining Time.")
    total_hours_spent = fields.Float(compute='_hours_get', store=True, string='Total Hours', help="Computed as: Time Spent + Sub-tasks Hours.")
    progress = fields.Float(compute='_hours_get', store=True, string='Working Time Recorded', group_operator="avg")
    delay_hours = fields.Float(compute='_hours_get', store=True, string='Delay Hours', help="Computed as difference between planned hours by the project manager and the total hours of the task.")
    children_hours = fields.Float(compute='_hours_get', store=True, string='Sub-tasks Hours', help="Sum of the planned hours of all sub-tasks (when a sub-task is closed or its spent hours exceed its planned hours, spent hours are counted instead)")
    timesheet_ids = fields.One2many('account.analytic.line', 'task_id', 'Timesheets')

    parent_id = fields.Many2one('project.task', string='Parent Task')
    child_ids = fields.One2many('project.task', 'parent_id', string="Sub-tasks")
    subtask_project_id = fields.Many2one('project.project', related="project_id.subtask_project_id", string='Sub-task Project', readonly=True)
    subtask_count = fields.Integer(compute='_get_subtask_count', type='integer', string="Sub-task count")

    _constraints = [(models.BaseModel._check_recursion, 'Circular references are not permitted between tasks and sub-tasks', ['parent_id'])]

    @api.constrains('parent_id')
    def _check_subtask_project(self):
        for task in self:
            if task.parent_id and task.parent_id.project_id and task.project_id and task.project_id != task.parent_id.project_id.subtask_project_id:
                raise UserError(_("You can't define a parent task if its project is not correctly configured. The sub-task's project of the parent task's project should be this task's project"))

@@ -4498,8 +4498,379 @@
 ject%22))%0A
+%0A    @api.model%0A    def create(self, vals):%0A        context = dict(self.env.context)%0A        # Remove default_parent_id to avoid a confusion in get_record_data%0A        if context.get('default_parent_id', False):%0A            vals%5B'parent_id'%5D = context.pop('default_parent_id', None)%0A        task = super(Task, self.with_context(context)).create(vals)%0A        return task%0A

create a sub task When creating a sub task, the field default_parent_id is set in the context. When a sub task is created, a mail.compose.message was created with the same context. So the default parent_id set in the context (for a project.task record) was used to create the message and an error was raised in function get_record_data.