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

153 lines
5.6 KiB
PHP

<?php
include('includes/config/config.php');
include('includes/auth/auth.php');
include('includes/functions.php');
include('access.php');
error_reporting(E_ERROR | E_PARSE);
// Connect to mysqli database
$page = 1; // The current page
$sortname = 'pid'; // Sort column
$sortorder = 'asc'; // Sort order
$qtype = ''; // Search column
$query = ''; // Search string
$rp = 10; // Rows per page default
// Get posted data
if (isset($_POST['page'])) {
$page = mysqli_real_escape_string($conn, $_POST['page']);
}
if (isset($_POST['sortname'])) {
$sortname = mysqli_real_escape_string($conn, $_POST['sortname']);
}
if (isset($_POST['sortorder'])) {
$sortorder = mysqli_real_escape_string($conn, $_POST['sortorder']);
}
if (isset($_POST['qtype'])) {
$qtype = mysqli_real_escape_string($conn, $_POST['qtype']);
}
if (isset($_POST['query'])) {
$query = mysqli_real_escape_string($conn, $_POST['query']);
}
if (isset($_POST['rp'])) {
$rp = mysqli_real_escape_string($conn, $_POST['rp']);
}
$hasReadAccess = isAccessible($_SESSION['RoleId'], $menu_key, 'R');
$hasWriteAccess = isAccessible($_SESSION['RoleId'], $menu_key, 'W');
$hasExecuteAccess = isAccessible($_SESSION['RoleId'], $menu_key, 'E');
$ohc_type_id = $_SESSION['current_ohcttype'];
// Setup sort and search SQL using posted data
$sortSql = "ORDER BY $sortname $sortorder";
// $searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%".trim($query)."%') " : '';
if ($qtype != '' && $query != '') {
if ($qtype == 'batch_id') {
$searchSql = "WHERE batch_id IN (SELECT batch_id FROM training_batch_master WHERE batch_name LIKE '%" . trim($query) . "%') and ohc_type_id = '$ohc_type_id'";
} elseif ($qtype == 'beneficiary_id') {
$searchSql = "WHERE beneficiary_id IN (SELECT id FROM patient_master WHERE patient_name LIKE '%" . trim($query) . "%') and ohc_type_id = '$ohc_type_id'";
} else {
$searchSql = "WHERE UPPER($qtype) LIKE UPPER('%" . trim($query) . "%') and ohc_type_id = '$ohc_type_id'";
}
} else {
$searchSql = "WHERE ohc_type_id = '$ohc_type_id'";
}
// Get total count of records
$sql = "SELECT COUNT(*) FROM leave_primary $searchSql";
error_log('test_surch : ' . $searchSql);
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$total = $row[0];
// Setup paging SQL
$pageStart = ($page - 1) * $rp;
$limitSql = "LIMIT $pageStart, $rp";
// Return JSON data
$data = array();
$data['page'] = $page;
$data['total'] = $total;
$data['rows'] = array();
$sql1 = "SELECT * FROM leave_primary $searchSql $sortSql $limitSql";
error_log('test_surch : ' . $sql1);
$results_section = mysqli_query($conn, $sql1);
$count = ($page - 1) * $rp + 1;
while ($row1 = mysqli_fetch_assoc($results_section)) {
$pid = $row1['pid'];
$batch_id = $row1['batch_id'];
$beneficiary_id = $row1['beneficiary_id'];
$view_link = "";
$edit_link = "";
$delete_link = "";
$links = "";
$batch_name = getFieldFromTable('batch_name', 'training_batch_master', 'batch_id', $batch_id);
$beneficiary_name = getFieldFromTable('patient_name', 'patient_master', 'id', $beneficiary_id);
// Leave Count
$leave_query = "SELECT * FROM leave_child WHERE pid = '" . $row1['pid'] . "' ORDER BY apply_date";
$leave_result = mysqli_query($conn, $leave_query);
$status = "NA";
$leave_count = 0;
$calculation = '';
while ($row2 = mysqli_fetch_assoc($leave_result)) {
// Get the status
$status = $row2['status'];
// Check if status is either 'Forword For Approvel' or 'Approved'
if ($row2['status'] == 'Forward For Approval' || $row2['status'] == 'Approved') {
// Check if from_date is valid and to_date is not empty or a placeholder
if (!empty($row2['from_date']) && $row2['from_date'] !== '0000-00-00' &&
!empty($row2['to_date']) && $row2['to_date'] !== '0000-00-00') {
$from_date = new DateTime($row2['from_date']); // Create DateTime objects
$to_date = new DateTime($row2['to_date']);
// Calculate the difference in days
$difference = $to_date->diff($from_date)->days;
$leave_count += $difference + 1; // Accumulate the total leave count
// Build the calculation string
$calculation .= ($difference + 1) . '+'; // Correct concatenation
} else {
// Append "NA" if dates are not valid
$calculation .= 'NA+'; // Append "NA" and a plus sign
}
}
}
// Trim the last '+' from the calculation string if needed
if (!empty($calculation) && substr($calculation, -1) === '+') {
$calculation = rtrim($calculation, '+'); // Remove the trailing '+'
}
$links = "";
// if ($hasReadAccess) {
// $links .= "<a href=\"#\" class=\"grey\" onclick=\"open_section('$batch_id','$beneficiary_id', 'V');\"><i class=\"ace-icon fa fa-eye bigger-130\"></i></a>";
// }
if ($hasWriteAccess) {
$links .= "&nbsp;&nbsp;&nbsp;<a href=\"#\" class=\"blue\" onclick=\"open_section('$batch_id','$beneficiary_id', 'V');\"><i class=\"ace-icon fa fa-edit bigger-130\"></i></a>";
}
if ($hasExecuteAccess) {
$links .= "&nbsp;&nbsp;&nbsp;<a href=\"#\" class=\"red\" onclick=\"delete_section('$pid');\"><i class=\"ace-icon fa fa-trash-o bigger-130\"></i></a>";
}
if ($hasExecuteAccess) {
$links .= "&nbsp;&nbsp;&nbsp;<a target=\"_blank\" href=\"leave_card.php?pid=$pid\" class=\"red\" title=\"Fees Receipt\"><i class=\"ace-icon fa fa-file-pdf-o\" style=\"font-size: 15px;\"></i></a>";
}
$data['rows'][] = array(
'cell' => array($links, $count++, $batch_name, $beneficiary_name,$status,$leave_count)
);
}
echo json_encode($data);