0% found this document useful (0 votes)
39 views3 pages

Admin Qry

Uploaded by

gmccruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Admin Qry

Uploaded by

gmccruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?

php

class ADMIN {

// Method to get all employees


function get_allemployees($db) {
$rows = array();
try {
$stmt1 = $db->prepare("SELECT * FROM `employees` ORDER BY `last_name`
ASC");
$stmt1->execute();
while ($row = $stmt1->fetch(PDO::FETCH_ASSOC)) {
$rows[] = $row;
}
} catch (PDOException $e) {
return "Error fetching employees: " . $e->getMessage();
}
return $rows;
}

// Method to insert a new employee


function ins_employee($db, $empno, $deptno, $lastname, $firstname, $middlename,
$suffixname, $barcodeno, $status, $image) {
try {
$stmt1 = $db->prepare("INSERT INTO `employees`(`employee_no`,
`dept_unit`, `last_name`, `first_name`, `middle_name`, `suffix_name`, `barcode_id`,
`status`, `image`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt1->execute(array($empno, $deptno, $lastname, $firstname,
$middlename, $suffixname, $barcodeno, $status, $image));
} catch (PDOException $e) {
return "Error inserting employee: " . $e->getMessage();
}
}

// Method to update an employee


function upd_employee($db, $empno, $deptno, $lastname, $firstname, $middlename,
$suffixname, $barcodeno, $status, $image) {
try {
$stmt1 = $db->prepare("UPDATE `employees` SET `dept_unit` = ?,
`last_name` = ?, `first_name` = ?, `middle_name` = ?, `suffix_name` = ?,
`barcode_id` = ?, `status` = ?, `image` = ? WHERE `employee_no` = ?");
$stmt1->execute(array($deptno, $lastname, $firstname, $middlename,
$suffixname, $barcodeno, $status, $image, $empno));
} catch (PDOException $e) {
return "Error updating employee: " . $e->getMessage();
}
}

// Method to delete an employee


function del_employee($db, $empno) {
try {
$stmt1 = $db->prepare("DELETE FROM `employees` WHERE `employee_no`
= ?");
$stmt1->execute(array($empno));
} catch (PDOException $e) {
return "Error deleting employee: " . $e->getMessage();
}
}
// Method to get employee details by employee number
function get_employee_by_number($db, $empno) {
try {
$stmt1 = $db->prepare("SELECT * FROM `employees` WHERE `employee_no`
= ?");
$stmt1->execute(array($empno));
return $stmt1->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return "Error fetching employee details: " . $e->getMessage();
}
}

// Method to get attendance records for today


function get_attendance_for_today($db) {
$todayDate = date('Y-m-d'); // Get today's date

try {
$stmt = $db->prepare("SELECT CONCAT(employees.first_name, ' ',
employees.last_name) AS fullname,
employees.dept_unit, attendance.time_in,
attendance.time_out
FROM attendance
JOIN employees ON attendance.employee_no =
employees.employee_no
WHERE DATE(attendance.time_in) = ?");
$stmt->execute(array($todayDate));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return "Error fetching today's attendance: " . $e->getMessage();
}
}

// Method to get attendance records for a specific date


function get_attendance_for_date($db, $attendanceDate) {
try {
$stmt = $db->prepare("SELECT CONCAT(employees.first_name, ' ',
employees.last_name) AS fullname,
employees.dept_unit, attendance.time_in,
attendance.time_out
FROM attendance
JOIN employees ON attendance.employee_no =
employees.employee_no
WHERE DATE(attendance.time_in) = ?");
$stmt->execute(array($attendanceDate));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return "Error fetching attendance for specified date: " . $e-
>getMessage();
}
}

// Method to check if an employee has already timed in today


function check_if_timed_in_today($db, $employeeNo) {
$todayDate = date('Y-m-d'); // Get today's date

try {
$stmt = $db->prepare("SELECT COUNT(*) FROM attendance
WHERE employee_no = ? AND DATE(time_in) = ?");
$stmt->execute(array($employeeNo, $todayDate));
return $stmt->fetchColumn() > 0; // Returns true if count > 0
} catch (PDOException $e) {
return "Error checking time in status: " . $e->getMessage();
}
}

// Method to record time in, ensuring no duplicate entries for the same day
function time_in($db, $employeeNo) {
// Check if the employee has already timed in today
if ($this->check_if_timed_in_today($db, $employeeNo)) {
return "Already timed in for today";
}

$timeIn = date('Y-m-d H:i:s'); // Current timestamp

try {
$stmt = $db->prepare("INSERT INTO attendance (employee_no, time_in)
VALUES (?, ?)");
$stmt->execute(array($employeeNo, $timeIn));
return $timeIn; // Return full timestamp
} catch (PDOException $e) {
return "Error recording time in: " . $e->getMessage();
}
}

// Method to record time out


function time_out($db, $employeeNo) {
$timeOut = date('Y-m-d H:i:s'); // Current timestamp

try {
$stmt = $db->prepare("UPDATE attendance SET time_out = ? WHERE
employee_no = ? AND DATE(time_in) = ? AND time_out IS NULL");
$stmt->execute(array($timeOut, $employeeNo, date('Y-m-d')));
return $timeOut; // Return full timestamp
} catch (PDOException $e) {
return "Error recording time out: " . $e->getMessage();
}
}
}

?>

You might also like