csrtechnew.ohctech.in/check_leave.php
2025-04-14 13:28:09 +05:30

71 lines
2.2 KiB
PHP

<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
// Get and sanitize input
$batch_id = isset($_POST['batch_id']) ? mysqli_real_escape_string($conn, $_POST['batch_id']) : '';
$beneficiary_id = isset($_POST['beneficiary_id']) ? mysqli_real_escape_string($conn, $_POST['beneficiary_id']) : '';
$start_date = isset($_POST['date']) ? $_POST['date'] : '';
// Validate date
$date = DateTime::createFromFormat('Y-m-d', $start_date);
if ($date === false) {
$data = array('status' => 400, 'message' => 'Invalid date format.');
echo json_encode($data);
exit;
}
// Set to the first day of the month
$date->setDate($date->format('Y'), $date->format('m'), 1);
$new_date = $date->format('Y-m-d');
// Query to check attendance
$select_query = "SELECT COUNT(*) as leave_count
FROM beneficiary_attendence a
LEFT JOIN attendance_batch b ON a.pti_id = b.id
WHERE b.attendence_date >= '$new_date'
AND b.attendence_date <= '$start_date'
AND b.batch_id = '$batch_id'
AND a.beneficiary_id = '$beneficiary_id'";
error_log("check_query".$select_query);
;
// // Handle query error
if (!$result = mysqli_query($conn, $select_query)) {
$data = array('status' => 500, 'message' => 'Query error: ' . mysqli_error($conn));
echo json_encode($data);
exit;
}
// Initialize data array
$data = array();
// Fetch and return data
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$data['status'] = 200;
$leave_limit = getFieldFromTable('value', 'config', 'key_name', 'LEAVE_LIMIT');
$data['leave'] = (int)$row['leave_count']; // Cast to integer
$data['leave_limit'] = $leave_limit; // Cast to integer
error_log("check_leave".(int)$row['leave_count']);
$data['message'] = "Attendance data found!";
} else {
// No data found
$data['status'] = 404;
$data['leave'] = 0; // Explicitly set leave to 0
$data['message'] = "No attendance leave data found.";
}
// Log the data for debugging
error_log("Data: " . print_r($data, true));
// Return JSON response
echo json_encode($data);
// Close database connection (optional)
mysqli_close($conn);
?>