# -*- coding: utf-8 -*- from datetime import datetime, time, timedelta from dateutil import relativedelta from odoo.addons.resource.models.resource import float_to_time, HOURS_PER_DAY from odoo.osv import expression from odoo import api, fields, models, _ from odoo.exceptions import UserError, ValidationError ,Warning class ProjectTask(models.Model): _inherit = "project.task" phase_id = fields.Many2one('project.phase', string='Project Phase', domain="[('project_id','=',project_id)]") phase_hours = fields.Float("phase total hours",related="phase_id.estimated_hours") user_ids = fields.Many2many('res.users', 'project_task_users', 'task_id', 'users_id', string="Employees") weight = fields.Float(string='Weight', ) task_progress = fields.Float(string='Task Progress') maximum_rate = fields.Float(string='Maximum Rate', default=1) allowed_internal_user_ids = fields.Many2many('res.users', 'project_task_allowed_internal_users_rel', string="Allowed Internal Users", default=lambda self: self.env.user, domain=[('share', '=', False)]) allowed_portal_user_ids = fields.Many2many('res.users', 'project_task_allowed_portal_users_rel', string="Allowed Portal Users", domain=[('share', '=', True)]) @api.constrains('task_progress' , 'weight') def _check_task_weight_progress(self): for record in self: if record.task_progress < 0 or record.task_progress > 100: raise ValidationError(_("The task progress must be between 0 and 100.")) if record.weight < 0 or record.weight > 100: raise ValidationError(_("The weight must be between 0 and 100.")) @api.onchange('weight') def _onchange_weight(self): done_task = self.env['project.task'].search([('phase_id', '=', self.phase_id.id),('id','!=',self._origin.id)]) weight_done = done_task.mapped('weight') sum_weight = sum(weight_done) + self.weight if sum_weight > 100: raise ValidationError(_("The total weights of the tasks for the stage must not exceed 100")) @api.onchange('project_id') def _onchange_project_id(self): for task in self: self.phase_id = False @api.constrains("phase_hours", "planned_hours") def _check_dept_hours(self): for record in self: plan_hours = sum(record.phase_id.mapped("task_ids").mapped("planned_hours")) if plan_hours > record.phase_hours: raise ValidationError( _("Total planned hours for all tasks in stage %s must not exceed Total stage hours %s") % ( record.phase_id.display_name, record.phase_hours))