$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."'";
$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."'";
$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."'";
$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."'";
$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."'";
$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."'";
$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."'";
$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'";
$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'";
$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."'";
$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')";
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("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);