From 150562ea657d78684dd017226cd3ed53f0488693 Mon Sep 17 00:00:00 2001 From: "pawan.sharma" Date: Mon, 29 Aug 2022 17:00:58 +0530 Subject: [PATCH] add timesheet validations --- timesheet_block/models/timesheet.py | 55 +++++++++++--------- timesheet_block/security/ir.model.access.csv | 4 +- timesheet_block/views/timesheet.xml | 2 +- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/timesheet_block/models/timesheet.py b/timesheet_block/models/timesheet.py index 4104c42..fda0dbc 100755 --- a/timesheet_block/models/timesheet.py +++ b/timesheet_block/models/timesheet.py @@ -54,7 +54,6 @@ class TimehseetBlock(models.Model): permission_untill = False if len(permission_list) != 0: permission_untill = datetime.strptime(str(max(permission_list)), "%Y-%m-%d").date() - # permission_untill = datetime.strptime(max(permission_list), "%Y-%m-%d").date() if permission_untill: if permission_untill < entry_date: raise AccessError('You can not create the backdate entry please contact to HR') @@ -62,40 +61,48 @@ class TimehseetBlock(models.Model): raise AccessError('You can not create the backdate entry please contact to HR') return super(TimehseetBlock, self).create(values) + def write(self, record): current_date = datetime.today().date() - if 'start_datetime' in record: - entry_date = datetime.strptime(record['start_datetime'],"%Y-%m-%d %H:%M:%S").date() - else: - res_date = str(self.start_datetime).split('.', 1)[0] - entry_date = datetime.strptime(str(res_date), '%Y-%m-%d %H:%M:%S').date() + if 'start_datetime' in record or 'end_datetime' in record or 'project_id' in record or 'sub_project' in record or 'task_id' in record or 'description' in record: + if 'start_datetime' in record: + entry_date = datetime.strptime(record['start_datetime'],"%Y-%m-%d %H:%M:%S").date() + else: + res_date = str(self.start_datetime).split('.', 1)[0] + entry_date = datetime.strptime(str(res_date), '%Y-%m-%d %H:%M:%S').date() - if current_date != entry_date: - employee_object = self.env['hr.employee'].search([('user_id', '=', self.env.uid)]) - if employee_object.allow_to_edit != 'always' or employee_object.allow_to_edit == False: - permission_list = [] - permission_object = employee_object.permission_history.search([('permission_type', '=', 'edit'),('employee_id', '=', employee_object.id)]) - for permission in permission_object: - permission_list.append(permission.date) - permission_untill =False - if len(permission_list) != 0: - permission_untill = datetime.strptime(str(max(permission_list)), "%Y-%m-%d").date() - # project = False - if self.user_id.id == self.env.uid: - if permission_untill: - if permission_untill < entry_date: + if current_date != entry_date: + employee_object = self.env['hr.employee'].search([('user_id', '=', self.env.uid)]) + if employee_object.allow_to_edit != 'always' or employee_object.allow_to_edit == False: + permission_list = [] + permission_object = employee_object.permission_history.search([('permission_type', '=', 'edit'),('employee_id', '=', employee_object.id)]) + for permission in permission_object: + permission_list.append(permission.date) + permission_untill =False + if len(permission_list) != 0: + permission_untill = datetime.strptime(str(max(permission_list)), "%Y-%m-%d").date() + # project = False + if self.user_id.id == self.env.uid: + if permission_untill: + if permission_untill < entry_date: + raise AccessError( + 'You can not edit your backdate entry please connect to HR/Department Head/Project Manager') + else: raise AccessError( 'You can not edit your backdate entry please connect to HR/Department Head/Project Manager') - else: - raise AccessError( - 'You can not edit your backdate entry please connect to HR/Department Head/Project Manager') return super(TimehseetBlock, self).write(record) - @api.onchange('end_datetime') + + @api.onchange('end_datetime' ,'start_datetime') def _onchange_end_datetime(self): if self.end_datetime: current_date = datetime.today().date() end_date = datetime.strptime(str(self.end_datetime), "%Y-%m-%d %H:%M:%S").date() if end_date > current_date: raise AccessError('End date cannot be a future date!') + if self.end_datetime and self.start_datetime: + end_date = datetime.strptime(str(self.end_datetime), "%Y-%m-%d %H:%M:%S").date() + start_date = datetime.strptime(str(self.start_datetime), "%Y-%m-%d %H:%M:%S").date() + if start_date > end_date: + raise AccessError('End date should be greater than start date!') diff --git a/timesheet_block/security/ir.model.access.csv b/timesheet_block/security/ir.model.access.csv index 956ddad..d891528 100755 --- a/timesheet_block/security/ir.model.access.csv +++ b/timesheet_block/security/ir.model.access.csv @@ -1,2 +1,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_timesheet_permission_history_user,access.timesheet.block.history.user,model_timesheet_block_history,,1,1,0,0 +access_timesheet_permission_history_manager,access.timesheet.block.history.manager,model_timesheet_block_history,hr_timesheet.group_timesheet_manager,1,1,1,1 +access_timesheet_permission_history_user,access.timesheet.block.history.user,model_timesheet_block_history,hr_timesheet.group_hr_timesheet_user,1,1,1,0 +access_back_date_assign_user,access.back.date.assign.user,model_back_date_assign,hr_timesheet.group_timesheet_manager,1,1,1,1 diff --git a/timesheet_block/views/timesheet.xml b/timesheet_block/views/timesheet.xml index 72abff0..b2b9751 100755 --- a/timesheet_block/views/timesheet.xml +++ b/timesheet_block/views/timesheet.xml @@ -20,7 +20,7 @@ type="action" groups="hr.group_hr_manager" class="oe_highlight"/> - +