227 lines
10 KiB
PHP
227 lines
10 KiB
PHP
<?php
|
|
// ini_set('MAX_EXECUTION_TIME', 3600);
|
|
include('includes/config/config.php');
|
|
include('includes/auth/auth.php');
|
|
include('includes/functions.php');
|
|
include('access.php');
|
|
error_reporting(E_ERROR | E_PARSE);
|
|
begin();
|
|
$response = array();
|
|
|
|
// Get the current time
|
|
$current_time = strtotime(date('H:i'));
|
|
|
|
$start_time = strtotime('08:00');
|
|
$end_time = strtotime('17:00');
|
|
|
|
if ($current_time >= $start_time && $current_time <= $end_time) {
|
|
|
|
$query_initial_key_value = "insert into checkup_form_key_value set checkup_form_id=?, checkup_form_key=?,checkup_form_value=?";
|
|
|
|
$update_query_key_value = "update checkup_form_key_value set checkup_form_value=?";
|
|
$endquery_key_value = " where checkup_form_id = ? and checkup_form_key=? ";
|
|
|
|
|
|
|
|
$stmt_insert_query_key_value = $conn->prepare($query_initial_key_value);
|
|
$stmt_update_query_key_value = $conn->prepare($update_query_key_value . $endquery_key_value);
|
|
|
|
|
|
// taking unique checkup ids where data is not processed
|
|
|
|
$query = "select b.checkup_id from device_result a left join device_order b on a.accession_number = b.accession_number where a.is_processed = 'N' group by b.checkup_id";
|
|
|
|
error_log("query " . $query);
|
|
|
|
if ($result = mysqli_query($conn, $query)) {
|
|
|
|
while ($row = mysqli_fetch_assoc($result)) {
|
|
|
|
$checkup_id = $row['checkup_id'];
|
|
|
|
error_log("for checkup id " . $checkup_id);
|
|
|
|
$add_param_ids = getFieldFromTable('add_param_ids', 'checkup_form', 'checkup_id', $checkup_id);
|
|
|
|
error_log("additional param ids " . $add_param_ids);
|
|
|
|
$add_online_testcodes = getAdditionalOnlineTestCodes($add_param_ids);
|
|
|
|
$checkup_section_ids = getFieldFromTable('checkup_section_ids', 'checkup_form', 'checkup_id', $checkup_id);
|
|
|
|
$section_arr = explode(",", $checkup_section_ids);
|
|
|
|
error_log("checkup section ids " . print_r($section_arr, true));
|
|
|
|
$query_param = "select b.online_testcode,b.device_id,b.accession_number,b.device_name,a.test_result,b.checkup_id,d.checkup_parameter_id from device_result a left join device_order b on a.accession_number=b.accession_number and a.online_testcode=b.online_testcode left join device_param_map c on c.device_param_code=a.online_testcode and c.device_id=b.device_id left join checkup_parameter d on d.checkup_parameter_id=c.checkup_parameter_id where b.checkup_id='" . $checkup_id . "' and d.checkup_form_section_id in (" . implode(',', $section_arr) . ") ";
|
|
|
|
error_log("query to get code and result from device result " . $query_param);
|
|
|
|
$result_param = mysqli_query($conn, $query_param);
|
|
|
|
while ($row_param = mysqli_fetch_assoc($result_param)) {
|
|
|
|
|
|
$param_id = $row_param['checkup_parameter_id'];
|
|
|
|
$actual_param_name = getTableFieldValue('checkup_parameter', 'column_name', 'checkup_parameter_id', $param_id);
|
|
|
|
error_log("actual param name in our table " . $actual_param_name);
|
|
// check if data exist in checkup form key value
|
|
|
|
if ($actual_param_name != '') {
|
|
|
|
$device_name = $row_param['device_name'];
|
|
|
|
$test_result = trim($row_param['test_result']);
|
|
|
|
$isExist = "select * from checkup_form_key_value where checkup_form_id='" . $checkup_id . "' and checkup_form_key = '" . $actual_param_name . "'";
|
|
|
|
error_log("exist or not already " . $isExist);
|
|
|
|
$resultExist = mysqli_query($conn, $isExist);
|
|
$numRowsExist = mysqli_num_rows($resultExist);
|
|
|
|
// update case it is
|
|
if ($numRowsExist > 0) {
|
|
|
|
if ($test_result != null && $test_result != '') {
|
|
$stmt_update_query_key_value->bind_param("sss", $test_result, $checkup_id, $actual_param_name);
|
|
$stmt_update_query_key_value->execute();
|
|
if ($stmt_update_query_key_value->errno) {
|
|
error_log("Some error while saving key value data: " . $stmt_update_query_key_value->error);
|
|
rollback();
|
|
}
|
|
}
|
|
}
|
|
// insert case it is
|
|
else {
|
|
if ($test_result != null && $test_result != '') {
|
|
$stmt_insert_query_key_value->bind_param("sss", $checkup_id, $actual_param_name, $test_result);
|
|
$stmt_insert_query_key_value->execute();
|
|
if ($stmt_insert_query_key_value->errno) {
|
|
error_log("Some error while saving key value data: " . $stmt_insert_query_key_value->error);
|
|
rollback();
|
|
}
|
|
}
|
|
|
|
$accession_number = $row_param['accession_number'];
|
|
$online_testcode = $row_param['online_testcode'];
|
|
$device_name = $row_param['device_name'];
|
|
|
|
updateDeviceEntry($accession_number, $online_testcode, $device_name);
|
|
}
|
|
}
|
|
|
|
if (!empty($add_param_ids)) {
|
|
updateAdditionalParamsData($add_param_ids, $checkup_id, $stmt_insert_query_key_value, $stmt_update_query_key_value);
|
|
}
|
|
}
|
|
|
|
$remaining_marked_complete = "update device_result set is_processed='Y' where date(insert_datetime) < date(curdate()-2)";
|
|
if (!$result_marked_complete = mysqli_query($conn, $remaining_marked_complete)) {
|
|
error_log("error in updating remaining extra results " . mysqli_error($conn));
|
|
}
|
|
|
|
$response['success'] = true;
|
|
$response['message'] = "Processing completed successfully.";
|
|
}
|
|
}
|
|
}
|
|
|
|
function updateAdditionalParamsData($add_param_ids, $checkup_id, $stmt_insert_query_key_value, $stmt_update_query_key_value)
|
|
{
|
|
$add_param_ids = explode(",", $add_param_ids);
|
|
|
|
foreach ($add_param_ids as $param_id) {
|
|
error_log("inside loop to update additional parameter data");
|
|
|
|
$actual_param_name = getTableFieldValue('checkup_parameter', 'column_name', 'checkup_parameter_id', $param_id);
|
|
|
|
error_log("actual param name in our table " . $actual_param_name);
|
|
|
|
$add_online_testcode = getFieldFromTable('device_param_code', 'device_param_map', 'checkup_parameter_id', $param_id);
|
|
|
|
$query_param = "select b.online_testcode,b.device_id,b.accession_number,b.device_name,a.test_result,b.checkup_id from device_result a left join device_order b on a.accession_number = b.accession_number and a.online_testcode = b.online_testcode where b.checkup_id='" . $checkup_id . "' and b.online_testcode='" . $add_online_testcode . "'";
|
|
|
|
error_log("query to get code and result from device result " . $query_param);
|
|
|
|
$result_param = mysqli_query($GLOBALS['conn'], $query_param);
|
|
|
|
$row_param = mysqli_fetch_assoc($result_param);
|
|
|
|
if ($actual_param_name != '' && $add_online_testcode != '') {
|
|
|
|
$device_name = $row_param['device_name'];
|
|
|
|
$test_result = trim($row_param['test_result']);
|
|
|
|
$isExist = "select * from checkup_form_key_value where checkup_form_id='" . $checkup_id . "' and checkup_form_key = '" . $actual_param_name . "'";
|
|
|
|
error_log("exist or not already " . $isExist);
|
|
|
|
$resultExist = mysqli_query($GLOBALS['conn'], $isExist);
|
|
$numRowsExist = mysqli_num_rows($resultExist);
|
|
|
|
// update case it is
|
|
if ($numRowsExist > 0) {
|
|
if ($test_result != null && $test_result != '') {
|
|
$stmt_update_query_key_value->bind_param("sss", $test_result, $checkup_id, $actual_param_name);
|
|
$stmt_update_query_key_value->execute();
|
|
if ($stmt_update_query_key_value->errno) {
|
|
error_log("Some error while saving key value data: " . $stmt_update_query_key_value->error);
|
|
rollback();
|
|
}
|
|
}
|
|
}
|
|
// insert case it is
|
|
else {
|
|
if ($test_result != null && $test_result != '') {
|
|
$stmt_insert_query_key_value->bind_param("sss", $checkup_id, $actual_param_name, $test_result);
|
|
$stmt_insert_query_key_value->execute();
|
|
if ($stmt_insert_query_key_value->errno) {
|
|
error_log("Some error while saving key value data: " . $stmt_insert_query_key_value->error);
|
|
rollback();
|
|
}
|
|
}
|
|
}
|
|
|
|
$accession_number = $row_param['accession_number'];
|
|
$online_testcode = $add_online_testcode;
|
|
$device_name = $device_name;
|
|
|
|
updateDeviceEntry($accession_number, $online_testcode, $device_name);
|
|
|
|
error_log("loop end to update additional parameter data");
|
|
}
|
|
}
|
|
}
|
|
|
|
function getAdditionalOnlineTestCodes($add_param_ids)
|
|
{
|
|
$sql = "select group_concat(device_param_code) as code from device_param_map where checkup_parameter_id in ($add_param_ids)";
|
|
|
|
error_log("query to get additional device online testcode query " . $sql);
|
|
|
|
$result = mysqli_query($GLOBALS['conn'], $sql);
|
|
$row = mysqli_fetch_assoc($result);
|
|
return $row['code'];
|
|
}
|
|
|
|
function updateDeviceEntry($accession_number, $online_testcode, $device_name)
|
|
{
|
|
$query = "update device_result set is_processed='Y' where accession_number='" . $accession_number . "' and online_testcode='" . $online_testcode . "' and device_name='" . $device_name . "'";
|
|
|
|
error_log("device result update query " . $query);
|
|
|
|
if (!$result = mysqli_query($GLOBALS['conn'], $query)) {
|
|
$response['success'] = false;
|
|
$response['message'] = "Something went wrong while updating the device result.";
|
|
error_log("error " . mysqli_error($GLOBALS['conn']));
|
|
rollback();
|
|
}
|
|
}
|
|
|
|
commit();
|
|
echo json_encode($response);
|