1350 lines
51 KiB
Python
1350 lines
51 KiB
Python
"""
|
|
HR app forms with healthcare-focused validation and user experience.
|
|
"""
|
|
|
|
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils import timezone
|
|
from datetime import date, timedelta
|
|
from decimal import Decimal
|
|
|
|
from .models import (
|
|
Employee, Department, Schedule, ScheduleAssignment,
|
|
TimeEntry, PerformanceReview, TrainingRecord, TrainingPrograms,
|
|
TrainingSession, ProgramModule, ProgramPrerequisite,
|
|
TrainingAttendance, TrainingAssessment, TrainingCertificates
|
|
)
|
|
|
|
|
|
class EmployeeForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating employees.
|
|
"""
|
|
|
|
class Meta:
|
|
model = Employee
|
|
fields = [
|
|
'first_name', 'last_name', 'father_name', 'grandfather_name',
|
|
'email', 'phone', 'mobile_phone', 'address_line_1', 'address_line_2',
|
|
'city', 'postal_code', 'country', 'date_of_birth',
|
|
'gender', 'marital_status', 'department', 'job_title',
|
|
'employment_type', 'employment_status', 'hire_date', 'termination_date',
|
|
'supervisor', 'standard_hours_per_week', 'fte_percentage',
|
|
'hourly_rate', 'annual_salary', 'license_number', 'license_expiry_date',
|
|
'emergency_contact_name', 'emergency_contact_relationship',
|
|
'emergency_contact_phone', 'notes'
|
|
]
|
|
widgets = {
|
|
'first_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'First name'
|
|
}),
|
|
'last_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Last name'
|
|
}),
|
|
'father_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Father name'
|
|
}),
|
|
'grandfather_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Grandfather name'
|
|
}),
|
|
'email': forms.EmailInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'employee@hospital.com'
|
|
}),
|
|
'phone': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': '+1-555-123-4567'
|
|
}),
|
|
'mobile_phone': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': '+1-555-123-4567'
|
|
}),
|
|
'address_line_1': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Street address'
|
|
}),
|
|
'address_line_2': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Apt, Suite, etc.'
|
|
}),
|
|
'city': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'City'
|
|
}),
|
|
'state': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'State/Province'
|
|
}),
|
|
'postal_code': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Postal/ZIP code'
|
|
}),
|
|
'country': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Country'
|
|
}),
|
|
'department': forms.Select(attrs={'class': 'form-control'}),
|
|
'job_title': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Job title/position'
|
|
}),
|
|
'employment_status': forms.Select(attrs={'class': 'form-control'}),
|
|
'employment_type': forms.Select(attrs={'class': 'form-control'}),
|
|
'hire_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'termination_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'annual_salary': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'hourly_rate': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'supervisor': forms.Select(attrs={'class': 'form-control'}),
|
|
'emergency_contact_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Emergency contact name'
|
|
}),
|
|
'emergency_contact_relationship': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Relationship to employee'
|
|
}),
|
|
'emergency_contact_phone': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Emergency contact phone'
|
|
}),
|
|
'date_of_birth': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'gender': forms.Select(attrs={'class': 'form-control'}),
|
|
'marital_status': forms.Select(attrs={'class': 'form-control'}),
|
|
'standard_hours_per_week': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.5',
|
|
'min': '0',
|
|
'max': '168'
|
|
}),
|
|
'fte_percentage': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '1',
|
|
'min': '0',
|
|
'max': '100'
|
|
}),
|
|
'license_number': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Professional license number'
|
|
}),
|
|
'license_expiry_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'certifications': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'List of certifications'
|
|
}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Additional notes'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'employment_status': 'Current employment status',
|
|
'employment_type': 'Full-time, part-time, contract, etc.',
|
|
'supervisor': 'Direct supervisor (if any)',
|
|
'annual_salary': 'Annual salary (for salaried employees)',
|
|
'hourly_rate': 'Hourly rate (for hourly employees)',
|
|
'fte_percentage': 'Full-time equivalent percentage (0-100)',
|
|
'license_number': 'Professional license number (if applicable)',
|
|
'license_expiry_date': 'License expiration date (if applicable)',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter department and supervisor by tenant
|
|
self.fields['department'].queryset = Department.objects.filter(
|
|
tenant=user.tenant
|
|
).order_by('name')
|
|
|
|
self.fields['supervisor'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
# Exclude current instance from supervisor choices
|
|
if self.instance.pk:
|
|
self.fields['supervisor'].queryset = self.fields['supervisor'].queryset.exclude(
|
|
pk=self.instance.pk
|
|
)
|
|
|
|
# def clean_employee_number(self):
|
|
# employee_number = self.cleaned_data.get('employee_number')
|
|
# if employee_number:
|
|
# # Check for uniqueness within tenant (excluding current instance)
|
|
# queryset = Employee.objects.filter(employee_number=employee_number)
|
|
# if self.instance.pk:
|
|
# queryset = queryset.exclude(pk=self.instance.pk)
|
|
#
|
|
# if queryset.exists():
|
|
# raise ValidationError('Employee ID must be unique.')
|
|
#
|
|
# return employee_number
|
|
|
|
def clean_hire_date(self):
|
|
hire_date = self.cleaned_data.get('hire_date')
|
|
if hire_date and hire_date > timezone.now().date():
|
|
raise ValidationError('Hire date cannot be in the future.')
|
|
return hire_date
|
|
|
|
def clean_termination_date(self):
|
|
termination_date = self.cleaned_data.get('termination_date')
|
|
hire_date = self.cleaned_data.get('hire_date')
|
|
|
|
if termination_date:
|
|
if hire_date and termination_date < hire_date:
|
|
raise ValidationError('Termination date cannot be before hire date.')
|
|
|
|
return termination_date
|
|
|
|
def clean_date_of_birth(self):
|
|
date_of_birth = self.cleaned_data.get('date_of_birth')
|
|
if date_of_birth:
|
|
# Check minimum age (typically 16 for employment)
|
|
min_age_date = timezone.now().date() - timedelta(days=16*365)
|
|
if date_of_birth > min_age_date:
|
|
raise ValidationError('Employee must be at least 16 years old.')
|
|
|
|
# Check maximum age (reasonable limit)
|
|
max_age_date = timezone.now().date() - timedelta(days=100*365)
|
|
if date_of_birth < max_age_date:
|
|
raise ValidationError('Please verify the date of birth.')
|
|
|
|
return date_of_birth
|
|
|
|
def clean_license_expiry_date(self):
|
|
license_expiry_date = self.cleaned_data.get('license_expiry_date')
|
|
license_number = self.cleaned_data.get('license_number')
|
|
|
|
if license_number and not license_expiry_date:
|
|
raise ValidationError('License expiry date is required when license number is provided.')
|
|
|
|
return license_expiry_date
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
employment_type = cleaned_data.get('employment_type')
|
|
annual_salary = cleaned_data.get('annual_salary')
|
|
hourly_rate = cleaned_data.get('hourly_rate')
|
|
|
|
# Validate salary/hourly rate based on employment type
|
|
if employment_type == 'FULL_TIME' and not annual_salary:
|
|
self.add_error('annual_salary', 'Annual salary is required for full-time employees.')
|
|
|
|
if employment_type == 'PART_TIME' and not hourly_rate:
|
|
self.add_error('hourly_rate', 'Hourly rate is required for part-time employees.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
# ===========================
|
|
# Training Program Forms
|
|
# ===========================
|
|
|
|
class TrainingProgramForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating training programs.
|
|
"""
|
|
|
|
class Meta:
|
|
model = TrainingPrograms
|
|
fields = [
|
|
'name', 'description', 'program_type', 'program_provider',
|
|
'instructor', 'start_date', 'end_date', 'duration_hours',
|
|
'cost', 'is_certified', 'validity_days', 'notify_before_days'
|
|
]
|
|
widgets = {
|
|
'name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Training program name'
|
|
}),
|
|
'description': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Program description'
|
|
}),
|
|
'program_type': forms.Select(attrs={'class': 'form-control'}),
|
|
'program_provider': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Training provider/organization'
|
|
}),
|
|
'instructor': forms.Select(attrs={'class': 'form-control'}),
|
|
'start_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'end_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'duration_hours': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.5',
|
|
'min': '0'
|
|
}),
|
|
'cost': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'is_certified': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
'validity_days': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': '1'
|
|
}),
|
|
'notify_before_days': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': '1'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'program_type': 'Type of training program',
|
|
'duration_hours': 'Program duration in hours',
|
|
'cost': 'Program cost per participant',
|
|
'is_certified': 'Check if this program provides certification',
|
|
'validity_days': 'Certificate validity period in days',
|
|
'notify_before_days': 'Days before expiry to send notification',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter instructor by tenant
|
|
self.fields['instructor'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
start_date = cleaned_data.get('start_date')
|
|
end_date = cleaned_data.get('end_date')
|
|
is_certified = cleaned_data.get('is_certified')
|
|
validity_days = cleaned_data.get('validity_days')
|
|
|
|
if start_date and end_date:
|
|
if end_date < start_date:
|
|
self.add_error('end_date', 'End date cannot be before start date.')
|
|
|
|
if is_certified and not validity_days:
|
|
self.add_error('validity_days', 'Validity period is required for certified programs.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class TrainingSessionForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating training sessions.
|
|
"""
|
|
|
|
class Meta:
|
|
model = TrainingSession
|
|
fields = [
|
|
'program', 'title', 'instructor', 'delivery_method',
|
|
'start_at', 'end_at', 'location', 'capacity',
|
|
'cost_override', 'hours_override'
|
|
]
|
|
widgets = {
|
|
'program': forms.Select(attrs={'class': 'form-control'}),
|
|
'title': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Session title (optional)'
|
|
}),
|
|
'instructor': forms.Select(attrs={'class': 'form-control'}),
|
|
'delivery_method': forms.Select(attrs={'class': 'form-control'}),
|
|
'start_at': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'end_at': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'location': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Training location'
|
|
}),
|
|
'capacity': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': '1'
|
|
}),
|
|
'cost_override': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'hours_override': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.5',
|
|
'min': '0'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'title': 'Optional session title (defaults to program name)',
|
|
'delivery_method': 'How the training will be delivered',
|
|
'capacity': 'Maximum number of participants',
|
|
'cost_override': 'Override program cost for this session',
|
|
'hours_override': 'Override program hours for this session',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter program and instructor by tenant
|
|
self.fields['program'].queryset = TrainingPrograms.objects.filter(
|
|
tenant=user.tenant
|
|
).order_by('name')
|
|
|
|
self.fields['instructor'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
start_at = cleaned_data.get('start_at')
|
|
end_at = cleaned_data.get('end_at')
|
|
|
|
if start_at and end_at:
|
|
if end_at <= start_at:
|
|
self.add_error('end_at', 'End time must be after start time.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class ProgramModuleForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating program modules.
|
|
"""
|
|
|
|
class Meta:
|
|
model = ProgramModule
|
|
fields = ['program', 'title', 'order', 'hours']
|
|
widgets = {
|
|
'program': forms.Select(attrs={'class': 'form-control'}),
|
|
'title': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Module title'
|
|
}),
|
|
'order': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': '1'
|
|
}),
|
|
'hours': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.5',
|
|
'min': '0'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'order': 'Module order within the program',
|
|
'hours': 'Module duration in hours',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter program by tenant
|
|
self.fields['program'].queryset = TrainingPrograms.objects.filter(
|
|
tenant=user.tenant
|
|
).order_by('name')
|
|
|
|
|
|
class TrainingAttendanceForm(forms.ModelForm):
|
|
"""
|
|
Form for marking training attendance.
|
|
"""
|
|
|
|
class Meta:
|
|
model = TrainingAttendance
|
|
fields = ['enrollment', 'checked_in_at', 'checked_out_at', 'status', 'notes']
|
|
widgets = {
|
|
'enrollment': forms.Select(attrs={'class': 'form-control'}),
|
|
'checked_in_at': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'checked_out_at': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'status': forms.Select(attrs={'class': 'form-control'}),
|
|
'notes': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Attendance notes'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'status': 'Attendance status',
|
|
'notes': 'Additional notes about attendance',
|
|
}
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
checked_in_at = cleaned_data.get('checked_in_at')
|
|
checked_out_at = cleaned_data.get('checked_out_at')
|
|
status = cleaned_data.get('status')
|
|
|
|
if status in ['PRESENT', 'LATE'] and not checked_in_at:
|
|
self.add_error('checked_in_at', 'Check-in time is required for present/late status.')
|
|
|
|
if checked_in_at and checked_out_at:
|
|
if checked_out_at <= checked_in_at:
|
|
self.add_error('checked_out_at', 'Check-out time must be after check-in time.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class TrainingAssessmentForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating training assessments.
|
|
"""
|
|
|
|
class Meta:
|
|
model = TrainingAssessment
|
|
fields = ['enrollment', 'name', 'max_score', 'score', 'passed', 'taken_at', 'notes']
|
|
widgets = {
|
|
'enrollment': forms.Select(attrs={'class': 'form-control'}),
|
|
'name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Assessment name'
|
|
}),
|
|
'max_score': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'score': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'passed': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
'taken_at': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Assessment notes'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'max_score': 'Maximum possible score',
|
|
'score': 'Actual score achieved',
|
|
'passed': 'Check if assessment was passed',
|
|
}
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
max_score = cleaned_data.get('max_score')
|
|
score = cleaned_data.get('score')
|
|
|
|
if max_score and score and score > max_score:
|
|
self.add_error('score', 'Score cannot exceed maximum score.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class TrainingCertificateForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating training certificates.
|
|
"""
|
|
|
|
class Meta:
|
|
model = TrainingCertificates
|
|
fields = [
|
|
'program', 'employee', 'enrollment', 'certificate_name',
|
|
'certificate_number', 'certification_body', 'expiry_date',
|
|
'signed_by'
|
|
]
|
|
widgets = {
|
|
'program': forms.Select(attrs={'class': 'form-control'}),
|
|
'employee': forms.Select(attrs={'class': 'form-control'}),
|
|
'enrollment': forms.Select(attrs={'class': 'form-control'}),
|
|
'certificate_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Certificate name'
|
|
}),
|
|
'certificate_number': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Certificate number'
|
|
}),
|
|
'certification_body': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Issuing organization'
|
|
}),
|
|
'expiry_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'signed_by': forms.Select(attrs={'class': 'form-control'}),
|
|
}
|
|
help_texts = {
|
|
'certificate_number': 'Unique certificate identifier',
|
|
'certification_body': 'Organization that issued the certificate',
|
|
'expiry_date': 'Certificate expiration date',
|
|
'signed_by': 'Authority who signed the certificate',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter by tenant
|
|
self.fields['program'].queryset = TrainingPrograms.objects.filter(
|
|
tenant=user.tenant,
|
|
is_certified=True
|
|
).order_by('name')
|
|
|
|
self.fields['employee'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
self.fields['enrollment'].queryset = TrainingRecord.objects.filter(
|
|
employee__tenant=user.tenant,
|
|
status='COMPLETED',
|
|
passed=True
|
|
).order_by('-completion_date')
|
|
|
|
self.fields['signed_by'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
enrollment = cleaned_data.get('enrollment')
|
|
program = cleaned_data.get('program')
|
|
employee = cleaned_data.get('employee')
|
|
|
|
if enrollment and program and enrollment.program != program:
|
|
self.add_error('enrollment', 'Selected enrollment does not match the program.')
|
|
|
|
if enrollment and employee and enrollment.employee != employee:
|
|
self.add_error('enrollment', 'Selected enrollment does not match the employee.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
# ===========================
|
|
# Search and Filter Forms
|
|
# ===========================
|
|
|
|
class TrainingSearchForm(forms.Form):
|
|
"""
|
|
Form for searching and filtering training records.
|
|
"""
|
|
search = forms.CharField(
|
|
required=False,
|
|
widget=forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Search training records...'
|
|
})
|
|
)
|
|
employee = forms.ModelChoiceField(
|
|
queryset=Employee.objects.none(),
|
|
required=False,
|
|
widget=forms.Select(attrs={'class': 'form-control'})
|
|
)
|
|
program = forms.ModelChoiceField(
|
|
queryset=TrainingPrograms.objects.none(),
|
|
required=False,
|
|
widget=forms.Select(attrs={'class': 'form-control'})
|
|
)
|
|
status = forms.ChoiceField(
|
|
choices=[('', 'All Statuses')] + list(TrainingRecord.TrainingStatus.choices),
|
|
required=False,
|
|
widget=forms.Select(attrs={'class': 'form-control'})
|
|
)
|
|
program_type = forms.ChoiceField(
|
|
choices=[('', 'All Types')] + list(TrainingPrograms.TrainingType.choices),
|
|
required=False,
|
|
widget=forms.Select(attrs={'class': 'form-control'})
|
|
)
|
|
start_date = forms.DateField(
|
|
required=False,
|
|
widget=forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
})
|
|
)
|
|
end_date = forms.DateField(
|
|
required=False,
|
|
widget=forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
})
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
tenant = kwargs.pop('tenant', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if tenant:
|
|
self.fields['employee'].queryset = Employee.objects.filter(
|
|
tenant=tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
self.fields['program'].queryset = TrainingPrograms.objects.filter(
|
|
tenant=tenant
|
|
).order_by('name')
|
|
|
|
|
|
class DepartmentForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating departments.
|
|
"""
|
|
|
|
class Meta:
|
|
model = Department
|
|
fields = [
|
|
'name', 'code', 'description', 'department_type',
|
|
'parent_department', 'department_head', 'annual_budget',
|
|
'cost_center', 'location', 'is_active', 'notes'
|
|
]
|
|
widgets = {
|
|
'name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Department name'
|
|
}),
|
|
'department_code': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Unique department code'
|
|
}),
|
|
'description': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Department description'
|
|
}),
|
|
'department_type': forms.Select(attrs={'class': 'form-control'}),
|
|
'parent_department': forms.Select(attrs={'class': 'form-control'}),
|
|
'department_head': forms.Select(attrs={'class': 'form-control'}),
|
|
'annual_budget': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'cost_center': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Cost center code'
|
|
}),
|
|
'location': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Department location'
|
|
}),
|
|
'is_active': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Additional notes'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'department_code': 'Unique code to identify this department',
|
|
'department_head': 'Department manager (optional)',
|
|
'annual_budget': 'Annual budget for this department',
|
|
'parent_department': 'Parent department (if any)',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter parent department and department head by tenant
|
|
self.fields['parent_department'].queryset = Department.objects.filter(
|
|
tenant=user.tenant
|
|
).order_by('name')
|
|
|
|
self.fields['department_head'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
# Exclude current instance from parent department choices
|
|
if self.instance.pk:
|
|
self.fields['parent_department'].queryset = self.fields['parent_department'].queryset.exclude(
|
|
pk=self.instance.pk
|
|
)
|
|
|
|
def clean_department_code(self):
|
|
department_code = self.cleaned_data.get('department_code')
|
|
if department_code:
|
|
# Check for uniqueness within tenant (excluding current instance)
|
|
queryset = Department.objects.filter(department_code=department_code)
|
|
if self.instance.pk:
|
|
queryset = queryset.exclude(pk=self.instance.pk)
|
|
|
|
if queryset.exists():
|
|
raise ValidationError('Department code must be unique.')
|
|
|
|
return department_code
|
|
|
|
|
|
class ScheduleForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating schedules.
|
|
"""
|
|
|
|
class Meta:
|
|
model = Schedule
|
|
fields = [
|
|
'employee', 'name', 'description', 'schedule_type',
|
|
'effective_date', 'end_date', 'schedule_pattern',
|
|
'is_active', 'notes'
|
|
]
|
|
widgets = {
|
|
'employee': forms.Select(attrs={'class': 'form-control'}),
|
|
'name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Schedule name'
|
|
}),
|
|
'description': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Schedule description'
|
|
}),
|
|
'schedule_type': forms.Select(attrs={'class': 'form-control'}),
|
|
'effective_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'end_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'schedule_pattern': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 5,
|
|
'placeholder': '{"monday": {"start": "09:00", "end": "17:00"}, ...}'
|
|
}),
|
|
'is_active': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Schedule notes or instructions'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'schedule_type': 'Type of schedule (regular, rotating, etc.)',
|
|
'effective_date': 'Date when this schedule becomes effective',
|
|
'end_date': 'Date when this schedule ends (leave blank for indefinite)',
|
|
'schedule_pattern': 'JSON configuration for schedule pattern',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter employee by tenant
|
|
self.fields['employee'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
def clean_end_date(self):
|
|
end_date = self.cleaned_data.get('end_date')
|
|
effective_date = self.cleaned_data.get('effective_date')
|
|
|
|
if end_date and effective_date:
|
|
if end_date < effective_date:
|
|
raise ValidationError('End date cannot be before effective date.')
|
|
|
|
return end_date
|
|
|
|
|
|
class ScheduleAssignmentForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating schedule assignments.
|
|
"""
|
|
|
|
class Meta:
|
|
model = ScheduleAssignment
|
|
fields = [
|
|
'schedule', 'assignment_date', 'start_time', 'end_time',
|
|
'shift_type', 'department', 'location', 'status',
|
|
'break_minutes', 'lunch_minutes', 'notes'
|
|
]
|
|
widgets = {
|
|
'schedule': forms.Select(attrs={'class': 'form-control'}),
|
|
'assignment_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'start_time': forms.TimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'time'
|
|
}),
|
|
'end_time': forms.TimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'time'
|
|
}),
|
|
'shift_type': forms.Select(attrs={'class': 'form-control'}),
|
|
'department': forms.Select(attrs={'class': 'form-control'}),
|
|
'location': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Specific location'
|
|
}),
|
|
'status': forms.Select(attrs={'class': 'form-control'}),
|
|
'break_minutes': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': '0',
|
|
'max': '480' # 8 hours max
|
|
}),
|
|
'lunch_minutes': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'min': '0',
|
|
'max': '480' # 8 hours max
|
|
}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 2,
|
|
'placeholder': 'Assignment notes'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'break_minutes': 'Break duration in minutes',
|
|
'lunch_minutes': 'Lunch duration in minutes',
|
|
'shift_type': 'Type of shift',
|
|
'status': 'Current status of the assignment',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter schedule and department by tenant
|
|
self.fields['schedule'].queryset = Schedule.objects.filter(
|
|
employee__tenant=user.tenant,
|
|
is_active=True
|
|
).order_by('-effective_date')
|
|
|
|
self.fields['department'].queryset = Department.objects.filter(
|
|
tenant=user.tenant,
|
|
is_active=True
|
|
).order_by('name')
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
start_time = cleaned_data.get('start_time')
|
|
end_time = cleaned_data.get('end_time')
|
|
|
|
if start_time and end_time:
|
|
# Handle overnight shifts
|
|
if end_time < start_time:
|
|
# This is valid for overnight shifts
|
|
pass
|
|
elif end_time == start_time:
|
|
self.add_error('end_time', 'End time cannot be the same as start time.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class TimeEntryForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating time entries.
|
|
"""
|
|
|
|
class Meta:
|
|
model = TimeEntry
|
|
fields = [
|
|
'employee', 'work_date', 'clock_in_time', 'clock_out_time',
|
|
'break_start_time', 'break_end_time', 'lunch_start_time',
|
|
'lunch_end_time', 'entry_type', 'department', 'location',
|
|
'status', 'notes'
|
|
]
|
|
widgets = {
|
|
'employee': forms.Select(attrs={'class': 'form-control'}),
|
|
'work_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'clock_in_time': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'clock_out_time': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'break_start_time': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'break_end_time': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'lunch_start_time': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'lunch_end_time': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'entry_type': forms.Select(attrs={'class': 'form-control'}),
|
|
'department': forms.Select(attrs={'class': 'form-control'}),
|
|
'location': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Work location'
|
|
}),
|
|
'status': forms.Select(attrs={'class': 'form-control'}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 2,
|
|
'placeholder': 'Time entry notes'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'work_date': 'Date of work',
|
|
'clock_in_time': 'Clock in date and time',
|
|
'clock_out_time': 'Clock out date and time',
|
|
'entry_type': 'Type of time entry',
|
|
'status': 'Current status of the time entry',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter employee and department by tenant
|
|
self.fields['employee'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
self.fields['department'].queryset = Department.objects.filter(
|
|
tenant=user.tenant,
|
|
is_active=True
|
|
).order_by('name')
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
clock_in_time = cleaned_data.get('clock_in_time')
|
|
clock_out_time = cleaned_data.get('clock_out_time')
|
|
break_start_time = cleaned_data.get('break_start_time')
|
|
break_end_time = cleaned_data.get('break_end_time')
|
|
lunch_start_time = cleaned_data.get('lunch_start_time')
|
|
lunch_end_time = cleaned_data.get('lunch_end_time')
|
|
|
|
if clock_in_time and clock_out_time:
|
|
if clock_out_time <= clock_in_time:
|
|
self.add_error('clock_out_time', 'Clock out time must be after clock in time.')
|
|
|
|
# Check for reasonable shift length (max 24 hours)
|
|
duration = clock_out_time - clock_in_time
|
|
if duration.total_seconds() > 24 * 3600:
|
|
self.add_error('clock_out_time', 'Shift duration cannot exceed 24 hours.')
|
|
|
|
# Validate break times
|
|
if break_start_time and not break_end_time:
|
|
self.add_error('break_end_time', 'Break end time is required if break start time is provided.')
|
|
|
|
if break_end_time and not break_start_time:
|
|
self.add_error('break_start_time', 'Break start time is required if break end time is provided.')
|
|
|
|
if break_start_time and break_end_time:
|
|
if break_end_time <= break_start_time:
|
|
self.add_error('break_end_time', 'Break end time must be after break start time.')
|
|
|
|
if clock_in_time and break_start_time < clock_in_time:
|
|
self.add_error('break_start_time', 'Break start time must be after clock in time.')
|
|
|
|
if clock_out_time and break_end_time > clock_out_time:
|
|
self.add_error('break_end_time', 'Break end time must be before clock out time.')
|
|
|
|
# Validate lunch times
|
|
if lunch_start_time and not lunch_end_time:
|
|
self.add_error('lunch_end_time', 'Lunch end time is required if lunch start time is provided.')
|
|
|
|
if lunch_end_time and not lunch_start_time:
|
|
self.add_error('lunch_start_time', 'Lunch start time is required if lunch end time is provided.')
|
|
|
|
if lunch_start_time and lunch_end_time:
|
|
if lunch_end_time <= lunch_start_time:
|
|
self.add_error('lunch_end_time', 'Lunch end time must be after lunch start time.')
|
|
|
|
if clock_in_time and lunch_start_time < clock_in_time:
|
|
self.add_error('lunch_start_time', 'Lunch start time must be after clock in time.')
|
|
|
|
if clock_out_time and lunch_end_time > clock_out_time:
|
|
self.add_error('lunch_end_time', 'Lunch end time must be before clock out time.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class PerformanceReviewForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating performance reviews.
|
|
"""
|
|
|
|
class Meta:
|
|
model = PerformanceReview
|
|
fields = [
|
|
'employee', 'review_period_start', 'review_period_end',
|
|
'review_date', 'review_type', 'overall_rating',
|
|
'competency_ratings', 'goals_achieved', 'goals_not_achieved',
|
|
'future_goals', 'strengths', 'areas_for_improvement',
|
|
'development_plan', 'training_recommendations',
|
|
'employee_comments', 'employee_signature_date',
|
|
'status', 'notes'
|
|
]
|
|
widgets = {
|
|
'employee': forms.Select(attrs={'class': 'form-control'}),
|
|
'review_period_start': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'review_period_end': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'review_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'review_type': forms.Select(attrs={'class': 'form-control'}),
|
|
'overall_rating': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.1',
|
|
'min': '1',
|
|
'max': '5'
|
|
}),
|
|
'competency_ratings': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 5,
|
|
'placeholder': '{"communication": 4, "teamwork": 5, ...}'
|
|
}),
|
|
'goals_achieved': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Goals achieved during review period'
|
|
}),
|
|
'goals_not_achieved': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Goals not achieved during review period'
|
|
}),
|
|
'future_goals': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Goals for next review period'
|
|
}),
|
|
'strengths': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Employee strengths'
|
|
}),
|
|
'areas_for_improvement': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Areas for improvement'
|
|
}),
|
|
'development_plan': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Professional development plan'
|
|
}),
|
|
'training_recommendations': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Training recommendations'
|
|
}),
|
|
'employee_comments': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Employee comments'
|
|
}),
|
|
'employee_signature_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'status': forms.Select(attrs={'class': 'form-control'}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Additional notes'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'review_type': 'Type of performance review',
|
|
'overall_rating': 'Overall rating (1-5)',
|
|
'competency_ratings': 'Individual competency ratings as JSON',
|
|
'status': 'Current status of the review',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter employee by tenant
|
|
self.fields['employee'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
review_period_start = cleaned_data.get('review_period_start')
|
|
review_period_end = cleaned_data.get('review_period_end')
|
|
review_date = cleaned_data.get('review_date')
|
|
status = cleaned_data.get('status')
|
|
employee_signature_date = cleaned_data.get('employee_signature_date')
|
|
|
|
if review_period_start and review_period_end:
|
|
if review_period_end < review_period_start:
|
|
self.add_error('review_period_end', 'Review period end date must be after start date.')
|
|
|
|
if review_date and review_period_end:
|
|
if review_date < review_period_end:
|
|
self.add_error('review_date', 'Review date should be on or after the review period end date.')
|
|
|
|
if status == 'ACKNOWLEDGED' and not employee_signature_date:
|
|
self.add_error('employee_signature_date', 'Employee signature date is required for acknowledged reviews.')
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class TrainingRecordForm(forms.ModelForm):
|
|
"""
|
|
Form for creating and updating training records.
|
|
"""
|
|
|
|
class Meta:
|
|
model = TrainingRecord
|
|
fields = [
|
|
'employee', 'program', 'session', 'started_at', 'completion_date',
|
|
'status', 'credits_earned', 'score', 'passed', 'notes', 'cost_paid'
|
|
]
|
|
widgets = {
|
|
'employee': forms.Select(attrs={'class': 'form-control'}),
|
|
'program': forms.Select(attrs={'class': 'form-control'}),
|
|
'session': forms.Select(attrs={'class': 'form-control'}),
|
|
'started_at': forms.DateTimeInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'datetime-local'
|
|
}),
|
|
'completion_date': forms.DateInput(attrs={
|
|
'class': 'form-control',
|
|
'type': 'date'
|
|
}),
|
|
'status': forms.Select(attrs={'class': 'form-control'}),
|
|
'credits_earned': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.1',
|
|
'min': '0'
|
|
}),
|
|
'score': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0',
|
|
'max': '100'
|
|
}),
|
|
'passed': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
'cost_paid': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'step': '0.01',
|
|
'min': '0'
|
|
}),
|
|
'notes': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'rows': 3,
|
|
'placeholder': 'Training notes and comments'
|
|
}),
|
|
}
|
|
help_texts = {
|
|
'program': 'Training program',
|
|
'session': 'Specific training session (optional)',
|
|
'credits_earned': 'Continuing education credits earned',
|
|
'cost_paid': 'Amount paid for training',
|
|
'score': 'Score achieved (0-100)',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if user and hasattr(user, 'tenant'):
|
|
# Filter by tenant
|
|
self.fields['employee'].queryset = Employee.objects.filter(
|
|
tenant=user.tenant,
|
|
employment_status='ACTIVE'
|
|
).order_by('last_name', 'first_name')
|
|
|
|
self.fields['program'].queryset = TrainingPrograms.objects.filter(
|
|
tenant=user.tenant
|
|
).order_by('name')
|
|
|
|
self.fields['session'].queryset = TrainingSession.objects.filter(
|
|
program__tenant=user.tenant
|
|
).order_by('-start_at')
|
|
|
|
# Make session optional
|
|
self.fields['session'].required = False
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
started_at = cleaned_data.get('started_at')
|
|
completion_date = cleaned_data.get('completion_date')
|
|
status = cleaned_data.get('status')
|
|
passed = cleaned_data.get('passed')
|
|
session = cleaned_data.get('session')
|
|
program = cleaned_data.get('program')
|
|
|
|
if completion_date and started_at:
|
|
if completion_date < started_at.date():
|
|
self.add_error('completion_date', 'Completion date cannot be before start date.')
|
|
|
|
if status == 'COMPLETED' and not completion_date:
|
|
self.add_error('completion_date', 'Completion date is required for completed training.')
|
|
|
|
if passed and status not in ['COMPLETED']:
|
|
self.add_error('passed', 'Training must be completed to mark as passed.')
|
|
|
|
if session and program and session.program != program:
|
|
self.add_error('session', 'Selected session does not belong to the selected program.')
|
|
|
|
return cleaned_data
|