odoo18/addons_extensions/cwf_timesheet/models/timesheet.py

86 lines
3.5 KiB
Python

from odoo import models, fields, api
from odoo.exceptions import ValidationError, UserError
from datetime import timedelta
from odoo import _
class CwfTimesheet(models.Model):
_name = 'cwf.timesheet'
_description = 'CWF Weekly Timesheet'
name = fields.Char(string='Week Name', required=True)
department_id = fields.Many2one('hr.department', string='Department')
week_start_date = fields.Date(string='Week Start Date', required=True)
week_end_date = fields.Date(string='Week End Date', required=True)
total_hours = fields.Float(string='Total Hours', required=True)
status = fields.Selection([
('draft', 'Draft'),
('submitted', 'Submitted')
], default='draft', string='Status')
lines = fields.One2many('cwf.timesheet.line','week_id')
def send_timesheet_update_email(self):
template = self.env.ref('cwf_timesheet.email_template_timesheet_update')
# Ensure that we have a valid employee email
current_date = fields.Date.from_string(self.week_start_date)
end_date = fields.Date.from_string(self.week_end_date)
if current_date > end_date:
raise UserError('The start date cannot be after the end date.')
# Get all employees in the department
employees = self.env['hr.employee'].search([('department_id', '=', self.department_id.id)])
# Loop through each day of the week and create timesheet lines for each employee
while current_date <= end_date:
for employee in employees:
self.env['cwf.timesheet.line'].create({
'week_id': self.id,
'employee_id': employee.id,
'week_day':current_date,
})
current_date += timedelta(days=1)
self.status = 'submitted'
for employee in employees:
if employee.work_email:
email_values = {
'email_to': employee.work_email,
'body_html': template.body_html, # Email body from template
'subject': 'Timesheet Update Notification',
}
template.send_mail(self.id, email_values=email_values, force_send=True)
class CwfTimesheetLine(models.Model):
_name = 'cwf.timesheet.line'
_description = 'CWF Weekly Timesheet Lines'
employee_id = fields.Many2one('hr.employee', string='Employee')
week_id = fields.Many2one('cwf.timesheet', 'Week')
week_day = fields.Date(string='Date')
check_in_date = fields.Datetime(string='Checkin')
check_out_date = fields.Datetime(string='Checkout ')
is_updated = fields.Boolean('Attendance Updated')
state_type = fields.Selection([('draft','Draft'),('holiday', 'Holiday'),('time_off','Time Off'),('present','Present')], default='draft')
def action_submit(self):
if self.state_type == 'draft' or not self.state_type:
raise ValidationError(_('State type should not Draft or Empty'))
if self.state_type not in ['holiday','time_off'] and not (self.check_in_date or self.check_out_date):
raise ValidationError(_('Please enter Check details'))
self._update_attendance()
def _update_attendance(self):
attendance_obj = self.env['hr.attendance']
for record in self:
attendance_obj.sudo().create({
'employee_id': record.employee_id.id,
'check_in': record.check_in_date,
'check_out': record.check_out_date,
})
record.is_updated = True