0) {
// Update existing record
$sql = "UPDATE temperature_summary SET
min_temp = '$min_temp',
max_temp = '$max_temp',
avg_temp = '$avg_temp'
WHERE sys_service_id = '$sys_service_id' AND temp_date = '$temp_date'";
} else {
// Insert new record
$sql = "INSERT INTO temperature_summary (sys_service_id, min_temp, max_temp, avg_temp, temp_date)
VALUES ('$sys_service_id', '$min_temp', '$max_temp', '$avg_temp', '$temp_date')";
}
if (mysqli_query($conn, $sql)) {
echo json_encode(array('success' => true, 'message' => 'Record saved successfully'));
} else {
echo json_encode(array('success' => false, 'message' => 'Error: ' . mysqli_error($conn)));
}
exit;
}
// Handle fetch data request
if (isset($_POST['action']) && $_POST['action'] == 'fetch') {
$start_date = mysqli_real_escape_string($conn, $_POST['start_date']);
$end_date = mysqli_real_escape_string($conn, $_POST['end_date']);
$sys_service_id = mysqli_real_escape_string($conn, $_POST['sys_service_id']);
// Check if all filters are provided
if (empty($start_date) || empty($end_date) || empty($sys_service_id)) {
echo json_encode(array('error' => true, 'message' => 'All three filters (Start Date, End Date, and Service ID) are required'));
exit;
}
// Get existing records from database
$sql = "SELECT * FROM temperature_summary
WHERE sys_service_id = '$sys_service_id'
AND temp_date BETWEEN '$start_date' AND '$end_date'
ORDER BY temp_date ASC";
error_log("SQL Query: " . $sql);
$result = mysqli_query($conn, $sql);
if (!$result) {
echo json_encode(array('error' => true, 'message' => mysqli_error($conn)));
exit;
}
// Create array of existing dates
$existing_data = array();
$existing_dates = array();
while ($row = mysqli_fetch_assoc($result)) {
$existing_data[] = $row;
$existing_dates[] = $row['temp_date'];
}
// Generate all dates in the range
$all_dates = array();
$current = strtotime($start_date);
$end = strtotime($end_date);
while ($current <= $end) {
$all_dates[] = date('Y-m-d', $current);
$current = strtotime('+1 day', $current);
}
// Find missing dates
$missing_dates = array_diff($all_dates, $existing_dates);
// Create dummy records for missing dates
$complete_data = array();
// Add existing records
foreach ($existing_data as $row) {
$complete_data[] = $row;
}
// Add dummy records for missing dates
foreach ($missing_dates as $date) {
$dummy_record = array(
'id' => 'new_' . $date, // Temporary ID for new records
'sys_service_id' => $sys_service_id,
'min_temp' => '0.00000000',
'max_temp' => '0.00000000',
'avg_temp' => '0.00000000',
'temp_date' => $date,
'is_dummy' => true // Flag to identify dummy records
);
$complete_data[] = $dummy_record;
}
// Sort complete data by date
usort($complete_data, function ($a, $b) {
return strtotime($a['temp_date']) - strtotime($b['temp_date']);
});
echo json_encode(array('data' => $complete_data, 'sql' => $sql));
exit;
}
// Handle Excel download
if (isset($_POST['action']) && $_POST['action'] == 'excel_download') {
$start_date = mysqli_real_escape_string($conn, $_POST['start_date']);
$end_date = mysqli_real_escape_string($conn, $_POST['end_date']);
$sys_service_id = mysqli_real_escape_string($conn, $_POST['sys_service_id']);
// Check if all filters are provided
if (empty($start_date) || empty($end_date) || empty($sys_service_id)) {
echo json_encode(array('error' => true, 'message' => 'All three filters are required'));
exit;
}
// Get ONLY existing records from database (no dummy records)
$sql = "SELECT * FROM temperature_summary
WHERE sys_service_id = '$sys_service_id'
AND temp_date BETWEEN '$start_date' AND '$end_date'
ORDER BY temp_date ASC";
$result = mysqli_query($conn, $sql);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
// Generate HTML table for Excel
$html = '
Temperature Summary Export
| Service ID |
Min Temp |
Max Temp |
Avg Temp |
Date |
';
if (count($data) > 0) {
foreach ($data as $row) {
// $html .= '
// | ' . htmlspecialchars($row['sys_service_id']) . ' |
// ' . htmlspecialchars(number_format((float) $row['min_temp'], 1, '.', '')) . ' |
// ' . htmlspecialchars(number_format((float) $row['max_temp'], 1, '.', '')) . ' |
// ' . htmlspecialchars(number_format((float) $row['avg_temp'], 1, '.', '')) . ' |
// ' . htmlspecialchars($row['temp_date']) . ' |
//
';
$html .= '
| ' . htmlspecialchars($row['sys_service_id']) . ' |
' . htmlspecialchars($row['min_temp']) . ' |
' . htmlspecialchars($row['max_temp']) . ' |
' . htmlspecialchars($row['avg_temp']) . ' |
' . htmlspecialchars($row['temp_date']) . ' |
';
}
} else {
$html .= '| No records found in database for the selected criteria |
';
}
$html .= '
';
// Set headers for Excel download
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="temperature_summary_' . date('Y-m-d_H-i-s') . '.xls"');
header('Cache-Control: max-age=0');
echo $html;
exit;
}
?>
Temperature Summary
Temperature Summary
| Service ID |
Min Temp |
Max Temp |
Avg Temp |
Date |
Actions |
| Please apply all three filters to search data |