ESH/shift_auto_update.php
2024-10-23 18:28:06 +05:30

271 lines
12 KiB
PHP

<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
$sql_ohc = "select ohc_type_id from ohc_type where `ohc_category`!='OHC' ";
if ($result_ohc = mysqli_query($GLOBALS['conn'], $sql_ohc)) {
while ($row_ohc = mysqli_fetch_array($result_ohc)) {
error_log("Running for shift update ----- Start Running shift ---- Update for OHC:" . $row_ohc['ohc_type_id']);
shift_update($row_ohc['ohc_type_id']);
error_log("Running for shift update end");
}
} else {
error_log("Failed to get ohc for shift update");
}
function shift_update($ohc)
{
begin();
// first checking if any shift is running or not
$sql = "select a.*,date(a.start_date_time) as date from shift_status_details a where current_status='R' and ohc_location_id='" . $ohc . "'";
$result = @mysqli_query($GLOBALS['conn'], $sql);
error_log("rows counting for checking current running shift " . mysqli_num_rows($result));
if (!$row = @mysqli_fetch_assoc($result)) {
error_log("error in query " . mysqli_error($GLOBALS['conn']) . " query " . $sql);
rollback();
}
$shift_detail_id = $row['shift_details_id'];
$shift_running = $row['shift_id'];
$shift_name = getFieldFromTable('status_name', 'shift_status', 'shift_status_id', $shift_running);
$date = $row['record_date'];
$next_shift = "";
$next_shift_id = "";
if (strtoupper($shift_name) == "A") {
$next_shift = "B";
} else if (strtoupper($shift_name == "B")) {
$next_shift = "C";
} else if (strtoupper($shift_name == "C")) {
$next_shift = "A";
}
$next_shift_id = getFieldFromTable('shift_status_id', 'shift_status', 'status_name', $next_shift);
error_log("shift id running already " . $shift_running . " shift name " . $shift_name . " date " . $date);
error_log("next shift " . $next_shift . " next shift id " . $next_shift_id);
// if shift is running than close this shift and start next shift for that ohc and update the
// item shift stock
if ($shift_running != '' || $shift_running != null || $shift_running != 0) {
$query = "update shift_status_details set end_date_time=NOW(),current_status='C' where current_status='R' and ohc_location_id='" . $ohc . "' ";
error_log("query for shift update " . $query);
if (!$result = @mysqli_query($GLOBALS['conn'], $query)) {
error_log("Failed to update Shift Status:" . mysqli_error($GLOBALS['conn']) . ". Failed query:" . $query);
rollback();
}
updateItemStockDispensaryShiftBalance($ohc, $shift_running, $date);
// save communication details
$start_date_time = getFieldFromTable('start_date_time', 'shift_status_details', 'shift_details_id', $shift_detail_id);
$end_date_time = getFieldFromTable('end_date_time', 'shift_status_details', 'shift_details_id', $shift_detail_id);
$data_arr = array();
// for injury
$sql_injury = "select count(*) as total from employee_appointment where appointment_type = 'I' and date(appointment_date)>= date('$start_date_time') and time(appointment_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "'";
$result_sql_injury = @mysqli_query($GLOBALS['conn'], $sql_injury);
error_log("sql_injury : " . $sql_injury);
$row_injury = @mysqli_fetch_assoc($result_sql_injury);
$injury_total = $row_injury['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Any AOD case"');
$data_arr[$com_id] = $injury_total;
// for opd
$sql_not_injury = "select count(*) as total from employee_appointment where appointment_type = 'O' and date(appointment_date)>= date('$start_date_time') and time(appointment_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "'";
$result_sql_not_injury = @mysqli_query($GLOBALS['conn'], $sql_not_injury);
error_log("sql_not_injury: " . $sql_not_injury);
$row_not_injury = @mysqli_fetch_assoc($result_sql_not_injury);
$opd_total = $row_not_injury['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Any OPD Case"');
$data_arr[$com_id] = $opd_total;
// for detention
$sql_serious = "select count(*) as total from employee_appointment where is_detention = 'Y' and date(appointment_date)>= date('$start_date_time') and time(appointment_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "'";
error_log("emergency_query:" . $sql_serious);
$result_serious = mysqli_query($GLOBALS['conn'], $sql_serious);
$row_serious = mysqli_fetch_array($result_serious);
$detention_total = $row_serious['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Detention Case"');
$data_arr[$com_id] = $detention_total;
// for referral
$sql_referral = "select count(*) as total from employee_appointment where (referral!= '' and referral IS NOT NULL) and date(appointment_date)>= date('$start_date_time') and time(appointment_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "'";
$result_referral = mysqli_query($GLOBALS['conn'], $sql_referral);
$row_referral = mysqli_fetch_array($result_referral);
error_log("sql_referral" . $sql_referral);
$referral_total = $row_referral['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Any Referral Cases"');
$data_arr[$com_id] = $referral_total;
//for emergency
$sql_followup = "select count(*) as total from employee_appointment where IsEmergency= '1' and date(appointment_date)>= date('$start_date_time') and time(appointment_date) >= time('$start_date_time') and ohc_type_id='" . $ohc . "'";
$result_followup = mysqli_query($GLOBALS['conn'], $sql_followup);
$row_followup = mysqli_fetch_array($result_followup);
error_log("serious : " . $sql_followup);
$emergency_total = $row_followup['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Follow-up of serious/referred cases"');
$data_arr[$com_id] = $emergency_total;
// for fitness
$sql_sickness1 = "select count(*) as total from sickness where fitness_status = 'APPROVED' and date(sickness_date)>= date('$start_date_time') and time(sickness_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "'";
$result_sickness1 = mysqli_query($GLOBALS['conn'], $sql_sickness1);
$row_sickness1 = mysqli_fetch_array($result_sickness1);
error_log("sql_sickness1: " . $sql_sickness1);
$fitness_total = $row_sickness1['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Fitness Certificate"');
$data_arr[$com_id] = $fitness_total;
// for health medical
$sql_medical = "select count(*) as total from checkup_form where date(checkup_date)>= date('$start_date_time') and time(checkup_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "'";
$result_medical = mysqli_query($GLOBALS['conn'], $sql_medical);
$row_medical = mysqli_fetch_array($result_medical);
error_log("sql_ime_long" . $sql_medical);
$medical_total = $row_medical['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Health Surveillance"');
$data_arr[$com_id] = $medical_total;
// food handler
$sql_food = "select count(*) as total from questionaire_master_response where date(screen_date)>= date('$start_date_time') and time(screen_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "' and form_type='chs'";
$result_food = mysqli_query($GLOBALS['conn'], $sql_food);
$row_food = mysqli_fetch_array($result_food);
error_log("sql_food" . $sql_food);
$food_total = $row_food['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Food Handler"');
$data_arr[$com_id] = $food_total;
// kitchen
$sql_kitchen = "select count(*) as total from questionaire_master_response where date(screen_date)>= date('$start_date_time') and time(screen_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "' and form_type='khs'";
$result_kitchen = mysqli_query($GLOBALS['conn'], $sql_kitchen);
$row_kitchen = mysqli_fetch_array($result_kitchen);
error_log("sql_kitchen" . $sql_kitchen);
$kitchen_total = $row_kitchen['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Kitchen Hygiene"');
$data_arr[$com_id] = $kitchen_total;
// unfit cases
$sql_sickness = "select count(*) as total from sickness where fitness_status != 'APPROVED' and date(sickness_date)>= date('$start_date_time') and time(sickness_date) between time('$start_date_time') and time('$end_date_time') and ohc_type_id='" . $ohc . "'";
$result_sickness = mysqli_query($GLOBALS['conn'], $sql_sickness);
$row_sickness = mysqli_fetch_array($result_sickness);
error_log("sql_sickness: " . $sql_sickness);
$unfit_total = $row_sickness['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Any Unfit cases"');
$data_arr[$com_id] = $unfit_total;
// for covid
$sql_covid = "select count(*) as total from covid_screening_details where date(test_date)>= date('$start_date_time') and time(last_modified) between time('$start_date_time') and time('$end_date_time')";
$result_covid = mysqli_query($GLOBALS['conn'], $sql_covid);
$row_covid = mysqli_fetch_array($result_covid);
error_log("covid sql: " . $sql_covid);
$covid_total = $row_covid['total'];
$com_id = getTableFieldValue('communication', 'communication_id', 'com_item', '"Covid 19 Screening"');
$data_arr[$com_id] = $covid_total;
$keys = array_keys($data_arr);
// now insert into table all data
for ($i = 0; $i < sizeof($data_arr); $i++) {
$insert_comm = "insert into communication_details set communication_id ='" . $keys[$i] . "',shift_details_id = '".$shift_detail_id."' ,details ='" . $data_arr[$keys[$i]] . "',comm_date = now(),ohc_location_id='" . $ohc . "' ";
error_log("communication save query " . $insert_comm);
if (!$result_comm = mysqli_query($GLOBALS['conn'], $insert_comm)) {
error_log("error while saving communication details " . mysqli_error($GLOBALS['conn']));
rollback();
}
}
// end
// now start the next shift for that ohc
$start = "insert into shift_status_details set record_date=NOW(),shift_id='" . $next_shift_id . "',start_date_time=NOW(),current_status='R',ohc_location_id='" . $ohc . "' ";
error_log("query for next shift start" . $query);
if (!$result = mysqli_query($GLOBALS['conn'], $start)) {
error_log("Failed to start new Shift:" . mysqli_error($GLOBALS['conn']) . ". Failed query:" . $start);
rollback();
}
} else {
// no shift is running than compare curr time with all shifts start time and end time
$query = "select * from shift_status where start_time <= CURTIME() AND end_time >= CURTIME()";
$result = mysqli_query($GLOBALS['conn'], $query);
$row = mysqli_fetch_assoc($result);
$next_shift = $row['status_name'];
$next_shift_id = $row['shift_status_id'];
error_log("next shift " . $next_shift . " next shift id " . $next_shift_id);
$start = "insert into shift_status_details set record_date=NOW(),shift_id='" . $next_shift_id . "',start_date_time=NOW(),current_status='R',ohc_location_id='" . $ohc . "' ";
error_log("query for starting a new shift when no shift is running " . $start);
if (!$result = mysqli_query($GLOBALS['conn'], $start)) {
error_log("Failed to start new Shift:" . mysqli_error($GLOBALS['conn']) . ". Failed query:" . $start);
rollback();
}
}
commit();
}