From 1aa6464674214077910c528760f0e374405ddf47 Mon Sep 17 00:00:00 2001 From: "pawan.sharma" Date: Fri, 2 Sep 2022 15:12:13 +0530 Subject: [PATCH] block timesheet entry for user --- timesheet_entry_block/__init__.py | 2 + timesheet_entry_block/__manifest__.py | 18 ++++ timesheet_entry_block/models/__init__.py | 1 + timesheet_entry_block/models/timesheet.py | 97 +++++++++++++++++++ .../security/.~lock.ir.model.access.csv# | 1 + .../security/ir.model.access.csv | 4 + timesheet_entry_block/views/timesheet.xml | 50 ++++++++++ timesheet_entry_block/wizard/__init__.py | 4 + timesheet_entry_block/wizard/date_assign.py | 22 +++++ timesheet_entry_block/wizard/date_assign.xml | 36 +++++++ 10 files changed, 235 insertions(+) create mode 100755 timesheet_entry_block/__init__.py create mode 100644 timesheet_entry_block/__manifest__.py create mode 100755 timesheet_entry_block/models/__init__.py create mode 100755 timesheet_entry_block/models/timesheet.py create mode 100755 timesheet_entry_block/security/.~lock.ir.model.access.csv# create mode 100755 timesheet_entry_block/security/ir.model.access.csv create mode 100755 timesheet_entry_block/views/timesheet.xml create mode 100755 timesheet_entry_block/wizard/__init__.py create mode 100755 timesheet_entry_block/wizard/date_assign.py create mode 100755 timesheet_entry_block/wizard/date_assign.xml diff --git a/timesheet_entry_block/__init__.py b/timesheet_entry_block/__init__.py new file mode 100755 index 0000000..c536983 --- /dev/null +++ b/timesheet_entry_block/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard \ No newline at end of file diff --git a/timesheet_entry_block/__manifest__.py b/timesheet_entry_block/__manifest__.py new file mode 100644 index 0000000..7fd94c0 --- /dev/null +++ b/timesheet_entry_block/__manifest__.py @@ -0,0 +1,18 @@ +{ + 'name': 'Timesheet Entry Block', + 'version': '1.0.1', + 'summary': 'This module is block to users to fill timehseet entry', + 'description': 'This module is block to users to fill timehseet entry', + 'category': 'Employee', + 'author': 'SunArc Technologies', + 'website': 'www.sunarctechnologies.com', + 'license': 'LGPL-3', + 'depends': ['base','hr','hr_timesheet','project'], + 'data': [ + 'security/ir.model.access.csv', + 'wizard/date_assign.xml', + 'views/timesheet.xml', + ], + 'installable': True, + 'auto_install': False, +} \ No newline at end of file diff --git a/timesheet_entry_block/models/__init__.py b/timesheet_entry_block/models/__init__.py new file mode 100755 index 0000000..1b7bb1f --- /dev/null +++ b/timesheet_entry_block/models/__init__.py @@ -0,0 +1 @@ +from . import timesheet diff --git a/timesheet_entry_block/models/timesheet.py b/timesheet_entry_block/models/timesheet.py new file mode 100755 index 0000000..bfa2ddc --- /dev/null +++ b/timesheet_entry_block/models/timesheet.py @@ -0,0 +1,97 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError, AccessError, ValidationError +from datetime import datetime +from dateutil import relativedelta + + +class EmployeeInherit(models.Model): + _inherit = 'hr.employee' + + permission_block_history = fields.One2many(comodel_name="timesheet.block.history", inverse_name="employee_id", + string="History", required=False ) + +class EmployeePublicInherit(models.Model): + _inherit = 'hr.employee.public' + + permission_block_history = fields.One2many(comodel_name="timesheet.block.history", inverse_name="employee_id", + string="History", required=False ) + + +class TimesheetBlock(models.Model): + _name = 'timesheet.block.history' + + start_date = fields.Date('Start Date') + end_date = fields.Date('End Date') + reason = fields.Text('Reason') + user_id = fields.Many2one('res.users', string='Given By') + employee_id = fields.Many2one(comodel_name="hr.employee", string="Employee", required=False) + + +class TimehseetBlock(models.Model): + _inherit = 'account.analytic.line' + + @api.model + def create(self, values): + entry_start_date = datetime.strptime(str(values['start_datetime']),"%Y-%m-%d %H:%M:%S").date() + entry_end_date = datetime.strptime(values['end_datetime'],"%Y-%m-%d %H:%M:%S").date() + employee_object = self.env['hr.employee'].search([('user_id', '=', self.env.uid)]) + project_manager_group = self.env.ref('hr_timesheet.group_timesheet_manager') + if self.env.uid not in project_manager_group.users.ids: + permission_block_object = employee_object.permission_block_history.search( + [('employee_id', '=', employee_object.id)],order='create_date desc', limit=1) + if permission_block_object: + if (permission_block_object.start_date <= entry_start_date and permission_block_object.end_date >= entry_start_date) or ( + permission_block_object.start_date <= entry_end_date and permission_block_object.end_date >= entry_end_date): + raise AccessError('You are not allowed to create entry for this date!') + else: + pass + return super(TimehseetBlock, self).create(values) + + + def write(self, record): + 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 and 'end_datetime' not in record: + entry_start_date = datetime.strptime(str(record['start_datetime']),"%Y-%m-%d %H:%M:%S").date() + entry_end_date = datetime.strptime(str(self.end_datetime),"%Y-%m-%d %H:%M:%S").date() + elif 'start_datetime' not in record and 'end_datetime' in record: + entry_start_date = datetime.strptime(str(self.start_datetime), "%Y-%m-%d %H:%M:%S").date() + entry_end_date = datetime.strptime(str(record['end_datetime']), "%Y-%m-%d %H:%M:%S").date() + elif 'start_datetime' in record and 'end_datetime' in record: + entry_start_date = datetime.strptime(str(record['start_datetime']),"%Y-%m-%d %H:%M:%S").date() + entry_end_date = datetime.strptime(str(record['end_datetime']), "%Y-%m-%d %H:%M:%S").date() + else: + res_start_date = str(self.start_datetime).split('.', 1)[0] + res_end_date = str(self.end_datetime).split('.', 1)[0] + entry_start_date = datetime.strptime(str(res_start_date), '%Y-%m-%d %H:%M:%S').date() + entry_end_date = datetime.strptime(str(res_end_date), '%Y-%m-%d %H:%M:%S').date() + employee_object = self.env['hr.employee'].search([('user_id', '=', self.env.uid)]) + project_manager_group = self.env.ref('hr_timesheet.group_timesheet_manager') + if self.env.uid not in project_manager_group.users.ids: + if self.user_id.id == self.env.uid: + permission_block_object = employee_object.permission_block_history.search( + [('employee_id', '=', employee_object.id)], order='create_date desc', limit=1) + if permission_block_object: + if ( + permission_block_object.start_date <= entry_start_date and permission_block_object.end_date >= entry_start_date) or ( + permission_block_object.start_date <= entry_end_date and permission_block_object.end_date >= entry_end_date): + raise AccessError('You are not allowed to edit entry for this date!') + else: + pass + return super(TimehseetBlock, self).write(record) + + + @api.constrains('unit_amount') + def _check_unit_amount(self): + for rec in self: + if rec.unit_amount < 0.0: + raise AccessError('End date should be greater than start date!') + + @api.constrains('end_datetime') + def _check_end_datetime(self): + current_date = datetime.today().date() + for rec in self: + if rec.end_datetime: + 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!') + diff --git a/timesheet_entry_block/security/.~lock.ir.model.access.csv# b/timesheet_entry_block/security/.~lock.ir.model.access.csv# new file mode 100755 index 0000000..2c4b2d7 --- /dev/null +++ b/timesheet_entry_block/security/.~lock.ir.model.access.csv# @@ -0,0 +1 @@ +,sunarcmanish,sunarcmanish,20.07.2020 23:42,file:///home/sunarcmanish/.config/libreoffice/4; \ No newline at end of file diff --git a/timesheet_entry_block/security/ir.model.access.csv b/timesheet_entry_block/security/ir.model.access.csv new file mode 100755 index 0000000..d891528 --- /dev/null +++ b/timesheet_entry_block/security/ir.model.access.csv @@ -0,0 +1,4 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +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_entry_block/views/timesheet.xml b/timesheet_entry_block/views/timesheet.xml new file mode 100755 index 0000000..65584b1 --- /dev/null +++ b/timesheet_entry_block/views/timesheet.xml @@ -0,0 +1,50 @@ + + + + hr.employee.timesheet.block.inherit + hr.employee + + + + + +