Employee Attendance Excel Controller Documentation
File: /controllers/EmployeeAttendanceExcelController.php
Purpose: Manages bulk employee attendance processing through Excel import with automated calculations and salary impact
Last Updated: December 20, 2024
Total Functions: 12+
Lines of Code: ~709
---
๐ Overview
The Employee Attendance Excel Controller is a comprehensive bulk attendance processing system that handles Excel file imports and automated attendance calculations. It provides:
- โข Multiple Excel format support for different attendance systems
- โข Automated late/absence detection and penalty calculation
- โข Overtime and extra hours calculation with bonus processing
- โข Integration with payroll system for salary adjustments
- โข Employee matching by code with validation
- โข Attendance system rule application
- โข Daily attendance history management
- โข CURL-based integration with employee personal records
- โข Date range processing for bulk attendance imports
Primary Functions
- โ Multi-format Excel attendance import (3 formats supported)
- โ Automated late penalty calculation based on attendance rules
- โ Overtime and extra hours bonus calculation
- โ Absent employee detection for date ranges
- โ Integration with employee attendance systems
- โ Salary impact processing via CURL to employee personal
- โ Daily attendance history tracking
- โ Employee validation and matching
- โ Transaction-safe bulk processing
- โ Date validation and range processing
Related Controllers
- โข employeeAttendance.php - Real-time attendance tracking
- โข employeeAttendanceSystems.php - Attendance system rules
- โข employeePersonalController.php - Employee financial records
- โข employeeController.php - Employee management
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **employeeclosedayhistory** | Daily attendance summaries | id, employeeid, day, attendanceTime, departureTime, latePeriod, lateHasPermission, lateDiscount, isAbsent, absentDiscount, status | |
| **employeeattendance** | Individual attendance logs | employeeattendanceid, empid, theImage, sysdate, fingerid, rfid, accessType, syncToServer | |
| **employee** | Employee master data | employeeId, empCode, employeeName, employeesubgroupid, employeegroupid, conditions, branchid, worthExtra, Hourlyrate, salary fields |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **employeeattendancesystem** | Attendance rules and penalties | id, name, penalty fields, vacation settings | |
| **employeeattendancesystemweek** | Weekly schedule definitions | id, employee_id, attendancedayen, attendancetime, departuretime, chosseday |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **youtubelink** | Tutorial video links | youtubelinkid, title, url | |
| **user** | System users | userid, username |
๐ Key Functions
1. addFromExcel() - Excel Import and Processing Engine
Location: Line 156
Purpose: Parse Excel attendance files and process attendance data with automated calculations
Function Signature:
function addFromExcel() {
global $employeeEX, $employeeCloseDayHistory, $smarty;
}
Process Flow:
1. File Upload and Parsing:
- Upload Excel file to temporary directory
- Detect file type and create appropriate reader
- Parse worksheet data row by row
2. Multi-Format Support:
- Format 1: Employee dept/name/code + datetime
- Format 2: Employee code/name/dept + date + time range
- Format 3: Employee dept/name/code + datetime + check-in/out status
3. Employee Matching and Validation:
- Match employees by employee code
- Validate against active employee records
- Apply branch filtering if applicable
4. Attendance System Integration:
- Load weekly schedules for each employee
- Apply attendance system rules and penalties
- Calculate late minutes and overtime hours
5. Automated Calculations:
- Late penalty calculation based on minutes late
- Extra hours bonus calculation for overtime
- Absent employee detection for date ranges
Excel Format Processing:
if ($exceltype == 1) {
$empDept = $rowData[$col++];
$empName = $rowData[$col++];
$empid = $rowData[$col++];
$dateTime = $rowData[$col++];
$dateArr = explode(' ', $dateTime, 2);
$date = $dateArr[0];
$time = date('H:i:s', strtotime($dateArr[1]));
}
Late Penalty Calculation:
$lateMinutes = ceil($diffAttend / 60);
if ($lateMinutes > 0 && $lateMinutes <= 30) {
$row['delayDiscount'] = $employeeData['halfHourLateWithoutPermissionDisount'];
} else if ($lateMinutes > 30 && $lateMinutes <= 60) {
$row['delayDiscount'] = $employeeData['hourLateWithoutPermissionDisount'];
} else if ($lateMinutes > 60 && $lateMinutes <= 90) {
$row['delayDiscount'] = $employeeData['hourAndHalfLateWithoutPermissionDisount'];
} // ... continues for all time brackets
---
2. addEmployeesAttendance() - Bulk Attendance Processing
Location: Line 104
Purpose: Process bulk attendance updates from Excel import data with transaction safety
Process Flow:
1. Transaction Initialization: Start database transaction
2. Bulk Processing: Iterate through employee attendance data
3. Validation: Check date validity for each record
4. Attendance Update: Call closeday() function for each entry
5. Salary Impact: Apply penalties/bonuses to employee records
6. Extra Bonus Processing: Handle overtime bonuses separately
7. Transaction Commit: Commit all changes or rollback on error
Key Processing Loop:
foreach ($empitr as $itr) {
$day = filter_input(INPUT_POST, 'date_' . $itr);
$empid = (int) filter_input(INPUT_POST, 'empid_' . $itr);
$attendanceTime = filter_input(INPUT_POST, 'attendanceTime' . $empid . '_' . $itr);
$departureTime = filter_input(INPUT_POST, 'departureTime' . $empid . '_' . $itr);
if (isRealDate(date_format(date_create($day), 'Y-m-d'))) {
$employeeCloseDayHistory = closeday($empid, $id, $day, $attendanceTime, $departureTime,
$latePeriod, $lateHasPermission, $lateDiscount,
$isAbsent, $absentHasPermission, $absentDiscount);
affectOnSalary($employeeCloseDayHistory, $oldEmployeeCloseDayHistory);
// Handle extra bonus for overtime
$extraBonus = (float) filter_input(INPUT_POST, 'extraBonus' . $empid . '_' . $itr);
if($extraBonus > 0) {
curlAddEmployeePersonal($empid, $oldEmployeeCloseDayHistory->day, $extraBonus, 13);
}
}
}
---
3. add() - Single Employee Attendance Creation
Location: Line 461
Purpose: Create or update attendance record for a single employee on a specific date
Function Signature:
function add($empid, $date, $attend_time = null, $depart_time = null)
Process Flow:
1. Date Processing: Format and validate date
2. Daily Initialization: Create daily records if first attendance
3. Attendance Logging: Create attendance records in database
4. History Update: Update daily attendance history
5. Status Management: Set absence status based on attendance data
Attendance Logic:
$isAbsent = 1;
$absenceDiscount = $employee->dayAbsenceWithoutPermissionDisount;
if ($attend_time || $depart_time) {
$isAbsent = 0;
$absenceDiscount = 0;
}
---
4. employeeAttendanceSystem() - System Configuration Builder
Location: Line 526
Purpose: Build attendance system configuration object for specific employee
Function Signature:
function employeeAttendanceSystem($emp, $attendanceTime, $departureTime)
Process Flow:
1. Create system configuration object
2. Set working hours (attendance/departure times)
3. Copy all penalty rates from employee record
4. Return JSON-encoded configuration
Configuration Building:
$sys = new stdClass();
$sys->attendanceTime = $attendanceTime;
$sys->departureTime = $departureTime;
$sys->halfHourLateWithPermissionDisount = $emp['halfHourLateWithPermissionDisount'];
// ... all penalty fields copied
$attendanceSystem = json_encode($sys);
return $attendanceSystem;
---
5. affectOnSalary() - Salary Impact Management
Location: Line 551
Purpose: Manage salary impact when attendance records change
Function Signature:
function affectOnSalary($newEmployeeCloseDayHistory, $oldEmployeeCloseDayHistory)
Process Flow:
1. Change Detection: Compare old vs new attendance data
2. Cleanup: Remove old salary impacts if changes detected
3. New Impact: Apply new penalties/bonuses to salary
4. Type-based Processing: Handle late penalties vs absence penalties separately
Impact Logic:
if ($newEmployeeCloseDayHistory->isAbsent == 0) { // Not absent
// Apply late penalty (type 5)
curlAddEmployeePersonal($newEmployeeCloseDayHistory->employeeid,
$newEmployeeCloseDayHistory->day,
$newEmployeeCloseDayHistory->lateDiscount, 5);
} elseif ($newEmployeeCloseDayHistory->isAbsent == 1) { // Absent
// Apply absence penalty (type 9)
curlAddEmployeePersonal($newEmployeeCloseDayHistory->employeeid,
$newEmployeeCloseDayHistory->day,
$newEmployeeCloseDayHistory->absentDiscount, 9);
}
---
6. closeday() - Daily Attendance Closure
Location: Line 591
Purpose: Finalize daily attendance record with all calculations
Function Signature:
function closeday($empid, $closedDayHistoryId, $day, $attend_time, $depart_time,
$latePeriod = 0, $lateHasPermission = 0, $lateDiscount = 0,
$isAbsent = 0, $absentHasPermission = 0, $absentDiscount = 0)
Process Flow:
1. Load existing daily history record
2. Update all attendance fields
3. Set status to 1 (affects applied)
4. Save record to database
5. Return updated history object
---
7. curlAddEmployeePersonal() - Salary System Integration
Location: Line 642
Purpose: Add salary adjustments via CURL to employee personal system
Function Signature:
function curlAddEmployeePersonal($empid, $day, $discountVal, $type)
Process Flow:
1. Validation: Skip if discount value is 0
2. Data Preparation: Build POST data array with session info
3. CURL Request: Send to employeePersonalController.php
4. Response Handling: Process response (logging available)
Integration Types:
- โข Type 5: Late penalties
- โข Type 9: Absence penalties
- โข Type 13: Extra hour bonuses
CURL Implementation:
$post = [
'curlpost' => 1,
'sessionlist' => json_encode($_SESSION),
'fromCtrl' => 'EmployeeAttendanceExcelController',
'empName' => $empid,
'empValue' => $discountVal,
'employeepersonneldate' => $dayAsDateTime,
'type' => $type,
'userid' => $_SESSION['userid'],
'saveid' => $_SESSION["saveid"],
'dbname' => $_SESSION["dbname"],
];
$url = 'http://' . $_SERVER['HTTP_HOST'] . explode('controllers', $_SERVER['REQUEST_URI'])[0] .
'controllers/employeePersonalController.php?do=add';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
---
๐ Workflows
Workflow 1: Excel Import Processing
---
Workflow 2: Bulk Attendance Update
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) or `do=show` | Default action | Display Excel upload form | |
| `do=addfromexcel` | `addFromExcel()` | Process uploaded Excel file | |
| `do=addEmployeesAttendance` | Bulk processing | Process attendance updates with transaction | |
| `do=success` | Success page | Display success message | |
| `do=error` | Error page | Display error message |
Excel Upload (do=addfromexcel):
- โข
attendancessheet- Excel file upload ($_FILES) - โข
exceltype- Format type (1, 2, or 3)
Bulk Update (do=addEmployeesAttendance):
- โข
empIds[]- Array of employee iteration IDs - โข For each employee/date combination:
- date_{itr} - Attendance date
- empid_{itr} - Employee ID
- attendanceTime{empid}_{itr} - Arrival time
- departureTime{empid}_{itr} - Departure time
- id{empid}_{itr} - Daily history record ID
- latePeriod{empid}_{itr} - Minutes late
- lateHasPermission{empid}_{itr} - Permission flag
- lateDiscount{empid}_{itr} - Late penalty amount
- absentHasPermission{empid}_{itr} - Absence permission flag
- absentDiscount{empid}_{itr} - Absence penalty amount
- absent{empid}_{itr} - Absence flag
- extraBonus{empid}_{itr} - Overtime bonus amount
---
๐งฎ Calculation Methods
Late Penalty Tiered Structure
if ($lateMinutes > 0 && $lateMinutes <= 30) {
$row['delayDiscount'] = $employeeData['halfHourLateWithoutPermissionDisount'];
} else if ($lateMinutes > 30 && $lateMinutes <= 60) {
$row['delayDiscount'] = $employeeData['hourLateWithoutPermissionDisount'];
} else if ($lateMinutes > 60 && $lateMinutes <= 90) {
$row['delayDiscount'] = $employeeData['hourAndHalfLateWithoutPermissionDisount'];
} else if ($lateMinutes > 90 && $lateMinutes <= 120) {
$row['delayDiscount'] = $employeeData['twoHoursLateWithoutPermissionDisount'];
} else if ($lateMinutes > 120) {
$row['delayDiscount'] = $employeeData['moreThanTwoHoursLateWithoutPermissionDisount'];
}
Overtime Bonus Calculation
if ($employeeData['worthExtra'] == 1 && $diffDeparture > 0) {
$extraHours = floor($diffDeparture / (60*60));
$row['extraHoursTime'] = $extraHours;
$row['extraBonus'] = $extraHours * $employeeData['Hourlyrate'];
}
Working Hours Calculation
if ($row['depart_time'] && $row['attend_time']) {
$datetime1 = new DateTime($row['attend_time']);
$datetime2 = new DateTime($row['depart_time']);
$interval = $datetime1->diff($datetime2);
$row['hoursWorked'] = $interval->format('%h:%i');
}
Date Range Generation
function getDatesBetween($startDate, $endDate, $format = 'Y-m-d') {
$dates = [];
$begin = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = new DateInterval('P1D');
$dateRange = new DatePeriod($begin, $interval, $end->modify('+1 day'));
foreach ($dateRange as $date) {
$dates[] = $date->format($format);
}
return $dates;
}
---
๐ Security & Permissions
Authentication
- โข Required for All Actions:
include_once("../public/authentication.php") - โข Transaction Safety: Database transactions with rollback capability
File Upload Security
- โข Temporary Storage: Files stored in dedicated upload directory
- โข Cleanup: Uploaded files deleted after processing
- โข Type Validation: PHPExcel validates file format
Input Validation
- โข Type Casting: All numeric inputs cast appropriately
- โข Date Validation:
isRealDate()function validates date formats - โข SQL Injection: DAO layer provides parameterized queries
CURL Security
- โข Session Validation: Session data passed with CURL requests
- โข Internal URLs: CURL requests only to internal controllers
- โข Error Handling: CURL errors caught and handled gracefully
---
๐ Performance Considerations
Excel Processing Optimization
1. Memory Usage: Large Excel files may exceed memory limits
2. Processing Time: Complex calculations per row can be slow
3. File Cleanup: Temporary files deleted after processing
Database Performance
1. Bulk Operations: Transaction-wrapped for consistency
2. Index Requirements:
- employee(empCode, conditions, branchid)
- employeeclosedayhistory(employeeid, day, del)
- employeeattendancesystemweek(employee_id, attendancedayen)
Known Performance Issues
- โข CURL Overhead: Individual CURL requests for each salary adjustment
- โข No Batching: Employee personal updates not batched
- โข Large Date Ranges: Processing months of data can be slow
---
๐ Common Issues & Troubleshooting
1. Excel Import Fails
Issue: Excel file not processed or data not extracted
Causes:
- โข Unsupported Excel format
- โข Missing PHPExcel library
- โข File permissions issues
Debug:
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
echo "Detected file type: " . $inputFileType;
if (!file_exists($inputFileName)) {
echo "File not uploaded properly";
}
2. Employee Not Found by Code
Issue: Employees show as invalid or not matched
Causes:
- โข Employee code mismatch between Excel and database
- โข Employee marked as deleted (conditions != 0)
- โข Branch filtering excluding employees
Debug:
SELECT employeeId, empCode, employeeName, conditions, branchid
FROM employee
WHERE empCode IN ('code1', 'code2', ...);
3. Salary Impacts Not Applied
Issue: Penalties/bonuses not reflecting in payroll
Causes:
- โข CURL request failing
- โข employeePersonalController.php not accessible
- โข Session data corruption
Debug:
// Add to curlAddEmployeePersonal function
echo "CURL Response: " . $response;
echo "CURL Error: " . curl_error($ch);
4. Date Range Processing Errors
Issue: Some dates skipped or invalid
Causes:
- โข Date format inconsistencies in Excel
- โข Timezone issues
- โข Invalid date values
Fix:
function isRealDate($date) {
if (false === strtotime($date)) {
return false;
}
list($year, $month, $day) = explode('-', $date);
return checkdate($month, $day, $year);
}
---
๐งช Testing Scenarios
Test Case 1: Multi-Format Excel Import
1. Create test Excel files for all 3 formats
2. Include various employee codes, dates, and times
3. Test with valid and invalid employee codes
4. Verify data extraction and employee matching
5. Check automated calculation accuracy
Test Case 2: Late Penalty Calculation
1. Create attendance records with various late minutes
2. Test all penalty tiers (15min, 30min, 1hr, etc.)
3. Verify permission vs non-permission penalties
4. Check salary impact integration
5. Test edge cases (exactly on time, very late)
Test Case 3: Overtime Bonus Processing
1. Set up employees with worthExtra = 1
2. Create attendance with overtime hours
3. Verify extra hours calculation
4. Check bonus amount = hours * hourly rate
5. Confirm bonus applied to employee personal
Test Case 4: Absent Employee Detection
1. Create date range with some employee absences
2. Verify absent employees identified correctly
3. Check work day vs non-work day detection
4. Test absence penalty application
5. Validate daily history record creation
Test Case 5: Transaction Safety
1. Create scenario that will cause error mid-processing
2. Verify database rollback occurs
3. Check no partial data saved
4. Test error handling and user notification
5. Verify system state consistency
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข employeeAttendance.md - Real-time attendance
- โข employeeAttendanceSystems.md - System configuration
- โข employeePersonalController.md - Payroll integration
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur