Update project_task.py

This commit is contained in:
zainab2097 2024-10-10 16:28:06 +03:00 committed by GitHub
parent 54ce329636
commit 011cf7e25f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 27 additions and 27 deletions

View File

@ -4,27 +4,28 @@ from dateutil import relativedelta
from odoo.addons.resource.models.resource import float_to_time, HOURS_PER_DAY from odoo.addons.resource.models.resource import float_to_time, HOURS_PER_DAY
from odoo.osv import expression from odoo.osv import expression
from odoo import api, fields, models, _ from odoo import api, fields, models, _
from odoo.exceptions import UserError, ValidationError ,Warning from odoo.exceptions import UserError, ValidationError, Warning
class ProjectTask(models.Model): class ProjectTask(models.Model):
_inherit = "project.task" _inherit = "project.task"
phase_id = fields.Many2one('project.phase', string='Project Phase', domain="[('project_id','=',project_id)]") 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") phase_hours = fields.Float("phase total hours", related="phase_id.estimated_hours")
user_ids = fields.Many2many('res.users', 'project_task_users', user_ids = fields.Many2many('res.users', 'project_task_users',
'task_id', 'users_id', string="Employees") 'task_id', 'users_id', string="Employees")
weight = fields.Float(string='Weight', ) weight = fields.Float(string='Weight', )
task_progress = fields.Float(string='Task Progress') task_progress = fields.Float(string='Task Progress')
maximum_rate = fields.Float(string='Maximum Rate', default=1) 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='')
@api.constrains('task_progress' , 'weight') 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='')
@api.constrains('task_progress', 'weight')
def _check_task_weight_progress(self): def _check_task_weight_progress(self):
for record in self: for record in self:
if record.task_progress < 0 or record.task_progress > 100: if record.task_progress < 0 or record.task_progress > 100:
@ -33,39 +34,38 @@ class ProjectTask(models.Model):
raise ValidationError(_("The weight must be between 0 and 100.")) raise ValidationError(_("The weight must be between 0 and 100."))
def write(self, vals): def write(self, vals):
res = super(project, self).write(vals) res = super(ProjectTask, self).write(vals)
if 'stage_id' in vals and vals.get('stage_id'): if 'stage_id' in vals and vals.get('stage_id'):
manager_users = self.env.ref('project.group_project_manager').users manager_users = self.env.ref('project.group_project_manager').users
department_manager_users = self.env.ref('project_base.group_project_department_manager').users department_manager_users = self.env.ref('project_base.group_project_department_manager').users
# Combine both user sets # Combine both user sets
user_ids = manager_users | department_manager_users user_ids = manager_users | department_manager_users
for task in self: for task in self:
if task.stage_id.is_closed: if task.stage_id.is_closed:
task.env['mail.message'].create({ task.env['mail.message'].create({
'message_type': "notification", 'message_type': "notification",
'body': _("Task %s is done for project %s and needs your action") % ( 'body': _("Task %s is done for project %s and needs your action") % (
task.name, task.project_id.name or ""), task.name, task.project_id.name or ""),
'subject': _("Done Task"), 'subject': _("Done Task"),
'partner_ids': [(6, 0, user_ids.mapped('partner_id').ids)], 'partner_ids': [(6, 0, user_ids.mapped('partner_id').ids)],
'notification_ids': [(0, 0, {'res_partner_id': user.partner_id.id, 'notification_type': 'inbox'}) 'notification_ids': [
for user in user_ids if user_ids], (0, 0, {'res_partner_id': user.partner_id.id, 'notification_type': 'inbox'})
for user in user_ids if user_ids],
'model': task._name, 'model': task._name,
'res_id': task.id, 'res_id': task.id,
'author_id': self.env.user.partner_id and self.env.user.partner_id.id 'author_id': self.env.user.partner_id and self.env.user.partner_id.id
}) })
# self._send_state_change_notification()
return res return res
@api.onchange('weight') @api.onchange('weight')
def _onchange_weight(self): def _onchange_weight(self):
done_task = self.env['project.task'].search([('phase_id', '=', self.phase_id.id),('id','!=',self._origin.id)]) done_task = self.env['project.task'].search(
[('phase_id', '=', self.phase_id.id), ('id', '!=', self._origin.id)])
weight_done = done_task.mapped('weight') weight_done = done_task.mapped('weight')
sum_weight = sum(weight_done) + self.weight sum_weight = sum(weight_done) + self.weight
if sum_weight > 100: if sum_weight > 100:
raise ValidationError(_("The total weights of the tasks for the stage must not exceed 100")) raise ValidationError(_("The total weights of the tasks for the stage must not exceed 100"))
@api.onchange('project_id') @api.onchange('project_id')
def _onchange_project_id(self): def _onchange_project_id(self):
for task in self: for task in self:
@ -80,4 +80,4 @@ class ProjectTask(models.Model):
_("Total planned hours for all tasks in stage %s must not exceed Total stage hours %s") % ( _("Total planned hours for all tasks in stage %s must not exceed Total stage hours %s") % (
record.phase_id.display_name, record.phase_hours)) record.phase_id.display_name, record.phase_hours))