Update Employee functionalites: change contract id input to dropdown menu in create/edit page
This commit is contained in:
@@ -6184,23 +6184,26 @@ def create_employee():
|
|||||||
contract_id = request.form.get('contract_id', '1').strip()
|
contract_id = request.form.get('contract_id', '1').strip()
|
||||||
|
|
||||||
# Validate required fields
|
# Validate required fields
|
||||||
if not all([employee_id, first_name, last_name]):
|
if not all([employee_id, first_name, last_name, contract_id]):
|
||||||
flash('Employee ID, First Name, and Last Name are required.', 'error')
|
flash('Employee ID, First Name, Last Name, and Project are required.', 'error')
|
||||||
return render_template('create_employee.html')
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('create_employee.html', projects=projects)
|
||||||
|
|
||||||
# Validate employee ID is numeric
|
# Validate employee ID is numeric
|
||||||
try:
|
try:
|
||||||
employee_id_int = int(employee_id)
|
employee_id_int = int(employee_id)
|
||||||
contract_id_int = int(contract_id)
|
contract_id_int = int(contract_id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
flash('Employee ID and Contract ID must be numeric.', 'error')
|
flash('Employee ID must be numeric and Project must be selected.', 'error')
|
||||||
return render_template('create_employee.html')
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('create_employee.html', projects=projects)
|
||||||
|
|
||||||
# Check if employee ID already exists
|
# Check if employee ID already exists
|
||||||
existing_employee = Employee.query.filter_by(id=employee_id_int).first()
|
existing_employee = Employee.query.filter_by(id=employee_id_int).first()
|
||||||
if existing_employee:
|
if existing_employee:
|
||||||
flash(f'Employee with ID {employee_id} already exists.', 'error')
|
flash(f'Employee with ID {employee_id} already exists.', 'error')
|
||||||
return render_template('create_employee.html')
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('create_employee.html', projects=projects)
|
||||||
|
|
||||||
# Create new employee
|
# Create new employee
|
||||||
new_employee = Employee(
|
new_employee = Employee(
|
||||||
@@ -6214,9 +6217,11 @@ def create_employee():
|
|||||||
db.session.add(new_employee)
|
db.session.add(new_employee)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
# Log employee creation
|
# Log employee creation with project info
|
||||||
try:
|
try:
|
||||||
logger_handler.logger.info(f"Admin user {session['username']} created new employee: {employee_id_int} - {first_name} {last_name}")
|
project = Project.query.get(contract_id_int)
|
||||||
|
project_name = project.name if project else f"Project {contract_id_int}"
|
||||||
|
logger_handler.logger.info(f"Admin user {session['username']} created new employee: {employee_id_int} - {first_name} {last_name} assigned to {project_name}")
|
||||||
except Exception as log_error:
|
except Exception as log_error:
|
||||||
print(f"⚠️ Logging error (non-critical): {log_error}")
|
print(f"⚠️ Logging error (non-critical): {log_error}")
|
||||||
|
|
||||||
@@ -6227,9 +6232,12 @@ def create_employee():
|
|||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
logger_handler.log_database_error('employee_creation', e)
|
logger_handler.log_database_error('employee_creation', e)
|
||||||
flash('Failed to create employee. Please try again.', 'error')
|
flash('Failed to create employee. Please try again.', 'error')
|
||||||
return render_template('create_employee.html')
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('create_employee.html', projects=projects)
|
||||||
|
|
||||||
return render_template('create_employee.html')
|
# GET request - load the form with projects
|
||||||
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('create_employee.html', projects=projects)
|
||||||
|
|
||||||
@app.route('/employees/<int:employee_index>/edit', methods=['GET', 'POST'])
|
@app.route('/employees/<int:employee_index>/edit', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
@@ -6249,23 +6257,26 @@ def edit_employee(employee_index):
|
|||||||
contract_id = request.form.get('contract_id', '1').strip()
|
contract_id = request.form.get('contract_id', '1').strip()
|
||||||
|
|
||||||
# Validate required fields
|
# Validate required fields
|
||||||
if not all([employee_id, first_name, last_name]):
|
if not all([employee_id, first_name, last_name, contract_id]):
|
||||||
flash('Employee ID, First Name, and Last Name are required.', 'error')
|
flash('Employee ID, First Name, Last Name, and Project are required.', 'error')
|
||||||
return render_template('edit_employee.html', employee=employee)
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('edit_employee.html', employee=employee, projects=projects)
|
||||||
|
|
||||||
# Validate numeric fields
|
# Validate numeric fields
|
||||||
try:
|
try:
|
||||||
employee_id_int = int(employee_id)
|
employee_id_int = int(employee_id)
|
||||||
contract_id_int = int(contract_id)
|
contract_id_int = int(contract_id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
flash('Employee ID and Contract ID must be numeric.', 'error')
|
flash('Employee ID must be numeric and Project must be selected.', 'error')
|
||||||
return render_template('edit_employee.html', employee=employee)
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('edit_employee.html', employee=employee, projects=projects)
|
||||||
|
|
||||||
# Check if employee ID already exists (but not for this employee)
|
# Check if employee ID already exists (but not for this employee)
|
||||||
existing_employee = Employee.query.filter_by(id=employee_id_int).first()
|
existing_employee = Employee.query.filter_by(id=employee_id_int).first()
|
||||||
if existing_employee and existing_employee.index != employee.index:
|
if existing_employee and existing_employee.index != employee.index:
|
||||||
flash(f'Employee with ID {employee_id} already exists.', 'error')
|
flash(f'Employee with ID {employee_id} already exists.', 'error')
|
||||||
return render_template('edit_employee.html', employee=employee)
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('edit_employee.html', employee=employee, projects=projects)
|
||||||
|
|
||||||
# Store original values for logging
|
# Store original values for logging
|
||||||
original_data = {
|
original_data = {
|
||||||
@@ -6285,16 +6296,20 @@ def edit_employee(employee_index):
|
|||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
# Log employee update
|
# Log employee update with project info
|
||||||
try:
|
try:
|
||||||
logger_handler.logger.info(f"Admin user {session['username']} updated employee: {employee_index} - {first_name} {last_name}")
|
project = Project.query.get(contract_id_int)
|
||||||
|
project_name = project.name if project else f"Project {contract_id_int}"
|
||||||
|
logger_handler.logger.info(f"Admin user {session['username']} updated employee: {employee_index} - {first_name} {last_name} assigned to {project_name}")
|
||||||
except Exception as log_error:
|
except Exception as log_error:
|
||||||
print(f"Warning: Logging error (non-critical): {log_error}")
|
print(f"⚠️ Logging error (non-critical): {log_error}")
|
||||||
|
|
||||||
flash(f'Employee "{first_name} {last_name}" updated successfully.', 'success')
|
flash(f'Employee "{first_name} {last_name}" updated successfully.', 'success')
|
||||||
return redirect(url_for('employees'))
|
return redirect(url_for('employees'))
|
||||||
|
|
||||||
return render_template('edit_employee.html', employee=employee)
|
# GET request - load the form with projects
|
||||||
|
projects = Project.query.filter_by(active_status=True).order_by(Project.name).all()
|
||||||
|
return render_template('edit_employee.html', employee=employee, projects=projects)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/employees.css') }}">
|
<link rel="stylesheet" href="{{ url_for('static', filename='css/employees.css') }}">
|
||||||
<style>
|
<style>
|
||||||
.form-container {
|
.form-container {
|
||||||
max-width: 600px;
|
max-width: 800px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
background: #ffffff;
|
background: #ffffff;
|
||||||
border-radius: 0.75rem;
|
border-radius: 0.75rem;
|
||||||
@@ -72,7 +72,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.form-input {
|
.form-input {
|
||||||
width: 100%;
|
width: 50%;
|
||||||
padding: 0.75rem 1rem;
|
padding: 0.75rem 1rem;
|
||||||
border: 2px solid #e2e8f0;
|
border: 2px solid #e2e8f0;
|
||||||
border-radius: 0.5rem;
|
border-radius: 0.5rem;
|
||||||
@@ -91,6 +91,14 @@
|
|||||||
border-color: #dc2626;
|
border-color: #dc2626;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.form-input select {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-input option {
|
||||||
|
padding: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
.form-help {
|
.form-help {
|
||||||
font-size: 0.875rem;
|
font-size: 0.875rem;
|
||||||
color: #6b7280;
|
color: #6b7280;
|
||||||
@@ -173,19 +181,25 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="contract_id" class="form-label">Contract ID</label>
|
<label for="contract_id" class="form-label">Project</label>
|
||||||
<input
|
<select
|
||||||
type="number"
|
|
||||||
id="contract_id"
|
id="contract_id"
|
||||||
name="contract_id"
|
name="contract_id"
|
||||||
class="form-input"
|
class="form-input"
|
||||||
min="1"
|
required
|
||||||
value="{{ request.form.get('contract_id', '1') }}"
|
|
||||||
placeholder="Enter contract ID"
|
|
||||||
>
|
>
|
||||||
|
<option value="">-- Select Project --</option>
|
||||||
|
{% for project in projects %}
|
||||||
|
<option
|
||||||
|
value="{{ project.id }}"
|
||||||
|
{% if request.form.get('contract_id') == project.id|string or (not request.form.get('contract_id') and project.id == 1) %}selected{% endif %}
|
||||||
|
>
|
||||||
|
{{ project.name }}
|
||||||
|
</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
<div class="form-help">
|
<div class="form-help">
|
||||||
Default is 1 if not specified
|
Select the project this employee will be assigned to
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -319,6 +333,13 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
this.value = this.value.replace(/^0+/, '');
|
this.value = this.value.replace(/^0+/, '');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const contractIdSelect = document.getElementById('contract_id');
|
||||||
|
contractIdSelect.addEventListener('change', function() {
|
||||||
|
if (this.value) {
|
||||||
|
this.classList.remove('error');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/>
|
/>
|
||||||
<style>
|
<style>
|
||||||
.form-container {
|
.form-container {
|
||||||
max-width: 600px;
|
max-width: 800px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
background: #ffffff;
|
background: #ffffff;
|
||||||
border-radius: 0.75rem;
|
border-radius: 0.75rem;
|
||||||
@@ -95,7 +95,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.form-input {
|
.form-input {
|
||||||
width: 100%;
|
width: 50%;
|
||||||
padding: 0.75rem 1rem;
|
padding: 0.75rem 1rem;
|
||||||
border: 2px solid #e2e8f0;
|
border: 2px solid #e2e8f0;
|
||||||
border-radius: 0.5rem;
|
border-radius: 0.5rem;
|
||||||
@@ -202,17 +202,26 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="contract_id" class="form-label">Contract ID</label>
|
<label for="contract_id" class="form-label">Project</label>
|
||||||
<input
|
<select
|
||||||
type="number"
|
|
||||||
id="contract_id"
|
id="contract_id"
|
||||||
name="contract_id"
|
name="contract_id"
|
||||||
class="form-input"
|
class="form-input"
|
||||||
min="1"
|
required
|
||||||
value="{{ request.form.get('contract_id', employee.contractId) }}"
|
>
|
||||||
placeholder="Enter contract ID"
|
<option value="">-- Select Project --</option>
|
||||||
/>
|
{% for project in projects %}
|
||||||
<div class="form-help">Default is 1 if not specified</div>
|
<option
|
||||||
|
value="{{ project.id }}"
|
||||||
|
{% if request.form.get('contract_id') == project.id|string or (not request.form.get('contract_id') and employee.contractId == project.id) %}selected{% endif %}
|
||||||
|
>
|
||||||
|
{{ project.name }}
|
||||||
|
</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<div class="form-help">
|
||||||
|
Select the project this employee will be assigned to
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -392,23 +401,20 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Contract ID validation
|
const contractIdSelect = document.getElementById("contract_id");
|
||||||
const contractIdInput = document.getElementById("contract_id");
|
contractIdSelect.addEventListener("change", function () {
|
||||||
contractIdInput.addEventListener("input", function () {
|
if (this.value) {
|
||||||
// Remove any non-numeric characters
|
this.classList.remove("error");
|
||||||
this.value = this.value.replace(/[^0-9]/g, "");
|
// Remove the yellow highlight for changes
|
||||||
|
this.style.borderLeft = "";
|
||||||
// Set default value if empty
|
|
||||||
if (!this.value) {
|
|
||||||
this.value = "1";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove leading zeros
|
|
||||||
if (this.value.length > 1) {
|
|
||||||
this.value = this.value.replace(/^0+/, "");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!contractIdSelect.value) {
|
||||||
|
contractIdSelect.classList.add("error");
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Highlight changed fields
|
// Highlight changed fields
|
||||||
function highlightChanges() {
|
function highlightChanges() {
|
||||||
Object.keys(originalValues).forEach((key) => {
|
Object.keys(originalValues).forEach((key) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user