odex25_standard/odex25_project/project_base/models/project_task.py

96 lines
4.5 KiB
Python

# -*- 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)])
isdone= fields.Boolean(string='',compute='_compute_stage')
@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))
@api.depends('stage_id', 'stage_id.is_closed')
def _compute_stage(self):
self.isdone = False
for rec in self:
if rec.stage_id.is_closed:
# Get users from both groups
manager_users = self.env.ref('project.group_project_manager').users
department_manager_users = self.env.ref('project_base.group_project_department_manager').users
# Combine both user sets
user_ids = manager_users | department_manager_users
# Ensure project exists and is valid
if rec.project_id:
project_no = rec.project_id.project_no or "Unknown Project No"
else:
project_no = "Unknown Project"
# Create a mail notification for users in both groups
rec.env['mail.message'].create({
'message_type': "notification",
'body': _("Task %s is done for project %s and needs your action") % (rec.name, project_no),
'subject': _("Done Task"),
'partner_ids': [(6, 0, user_ids.mapped('partner_id').ids)],
'notification_ids': [(0, 0, {
'res_partner_id': user.partner_id.id,
'notification_type': 'inbox'
}) for user in user_ids if user.partner_id],
'model': rec._name,
'res_id': rec.id if rec.id else False, # Ensure res_id exists
'author_id': rec.env.user.partner_id.id if rec.env.user.partner_id else False,
'email_from': rec.env.user.partner_id.email if rec.env.user.partner_id else 'noreply@company.com'
})