Files
controls-web/data/milldata.php
2026-02-17 12:44:37 -06:00

650 lines
20 KiB
PHP

<?php // phpcs:ignoreFile
/**
* Mill Data Viewer - Simple PHP interface for viewing extracted mill production data
*
* Requirements:
* - PHP 7.4+ with sqlsrv extension
* - SQL Server with MillDataReports and MillDataMetrics tables
*
* Usage:
* 1. Configure database connection below
* 2. Place in web server directory
* 3. Access via browser: http://localhost/milldata_viewer.php
*/
// ============================================================================
// Database Configuration
// ============================================================================
$config = [
'server' => '192.168.0.16',
'database' => 'lasucaai',
'username' => 'lasucaai',
'password' => 'is413#dfslw',
];
// ============================================================================
// Database Connection
// ============================================================================
function getConnection($config) {
$connectionOptions = [
"Database" => $config['database'],
"Uid" => $config['username'],
"PWD" => $config['password'],
"TrustServerCertificate" => true,
"Encrypt" => false,
];
$conn = sqlsrv_connect($config['server'], $connectionOptions);
if ($conn === false) {
die("Connection failed: " . print_r(sqlsrv_errors(), true));
}
return $conn;
}
// ============================================================================
// Data Queries
// ============================================================================
function getSourceFiles($conn) {
$sql = "SELECT DISTINCT SourceFileName,
MIN(BeginningDate) as BeginningDate,
MAX(EndingDate) as EndingDate,
MAX(ProcessedAt) as ProcessedAt
FROM dbo.MillDataReports
GROUP BY SourceFileName
ORDER BY MAX(ProcessedAt) DESC";
$stmt = sqlsrv_query($conn, $sql);
$files = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$files[] = $row;
}
return $files;
}
function getMillsForFile($conn, $sourceFileName) {
$sql = "SELECT ReportId, MillName
FROM dbo.MillDataReports
WHERE SourceFileName = ?
ORDER BY MillName";
$stmt = sqlsrv_query($conn, $sql, [$sourceFileName]);
if ($stmt === false) {
return [];
}
$mills = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$mills[] = $row;
}
return $mills;
}
function getReports($conn) {
$sql = "SELECT ReportId, SourceFileName, MillName, ReportTitle,
BeginningDate, EndingDate, CropDays, ProcessedAt
FROM dbo.MillDataReports
ORDER BY ProcessedAt DESC";
$stmt = sqlsrv_query($conn, $sql);
$reports = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$reports[] = $row;
}
return $reports;
}
function getMetrics($conn, $reportId, $search = '', $category = '') {
$params = [$reportId];
$sql = "SELECT MetricId, ItemNumber, MetricName, RunValue, RunValueNumeric,
ToDateValue, ToDateValueNumeric, Unit, Category
FROM dbo.MillDataMetrics
WHERE ReportId = ?";
if (!empty($search)) {
$sql .= " AND MetricName LIKE ?";
$params[] = '%' . $search . '%';
}
if (!empty($category)) {
$sql .= " AND Category = ?";
$params[] = $category;
}
$sql .= " ORDER BY MetricId";
$stmt = sqlsrv_query($conn, $sql, $params);
$metrics = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$metrics[] = $row;
}
return $metrics;
}
function getCategories($conn) {
$sql = "SELECT DISTINCT Category FROM dbo.MillDataMetrics WHERE Category IS NOT NULL ORDER BY Category";
$stmt = sqlsrv_query($conn, $sql);
$categories = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$categories[] = $row['Category'];
}
return $categories;
}
function getReportStats($conn) {
$sql = "SELECT
COUNT(DISTINCT r.ReportId) as TotalReports,
COUNT(m.MetricId) as TotalMetrics,
MIN(r.BeginningDate) as EarliestDate,
MAX(r.EndingDate) as LatestDate
FROM dbo.MillDataReports r
LEFT JOIN dbo.MillDataMetrics m ON r.ReportId = m.ReportId";
$stmt = sqlsrv_query($conn, $sql);
return sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
}
// ============================================================================
// Main Logic
// ============================================================================
$conn = getConnection($config);
$selectedFile = isset($_GET['file']) ? trim($_GET['file']) : '';
$selectedReportId = isset($_GET['report']) ? intval($_GET['report']) : 0;
$searchTerm = isset($_GET['search']) ? trim($_GET['search']) : '';
$selectedCategory = isset($_GET['category']) ? trim($_GET['category']) : '';
$sourceFiles = getSourceFiles($conn);
$categories = getCategories($conn);
$stats = getReportStats($conn);
// Default to first file if none selected
if (empty($selectedFile) && !empty($sourceFiles)) {
$selectedFile = $sourceFiles[0]['SourceFileName'];
}
// Get mills for selected file
$millsForFile = !empty($selectedFile) ? getMillsForFile($conn, $selectedFile) : [];
// Default to first mill if no report selected
if ($selectedReportId === 0 && !empty($millsForFile)) {
$selectedReportId = $millsForFile[0]['ReportId'];
}
// Validate report belongs to selected file
$validReport = false;
foreach ($millsForFile as $m) {
if ($m['ReportId'] == $selectedReportId) {
$validReport = true;
break;
}
}
if (!$validReport && !empty($millsForFile)) {
$selectedReportId = $millsForFile[0]['ReportId'];
}
$reports = getReports($conn);
$metrics = $selectedReportId ? getMetrics($conn, $selectedReportId, $searchTerm, $selectedCategory) : [];
// Find selected report details
$selectedReport = null;
foreach ($reports as $r) {
if ($r['ReportId'] == $selectedReportId) {
$selectedReport = $r;
break;
}
}
// Find selected file details
$selectedFileInfo = null;
foreach ($sourceFiles as $f) {
if ($f['SourceFileName'] == $selectedFile) {
$selectedFileInfo = $f;
break;
}
}
sqlsrv_close($conn);
// Format date helper
function formatDate($date) {
if ($date instanceof DateTime) {
return $date->format('m/d/Y');
}
return $date ?? '-';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mill Data Viewer</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
background: #f5f5f5;
color: #333;
line-height: 1.6;
}
.container {
max-width: 1400px;
margin: 0 auto;
padding: 20px;
}
header {
background: linear-gradient(135deg, #2c5530 0%, #4a7c50 100%);
color: white;
padding: 20px;
margin-bottom: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
header h1 {
font-size: 1.8rem;
margin-bottom: 5px;
}
header p {
opacity: 0.9;
font-size: 0.95rem;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.stat-card {
background: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
text-align: center;
}
.stat-card .value {
font-size: 1.8rem;
font-weight: bold;
color: #2c5530;
}
.stat-card .label {
font-size: 0.85rem;
color: #666;
}
.layout {
display: grid;
grid-template-columns: 300px 1fr;
gap: 20px;
}
@media (max-width: 900px) {
.layout {
grid-template-columns: 1fr;
}
}
.sidebar {
background: white;
border-radius: 8px;
padding: 15px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
height: fit-content;
}
.sidebar h2 {
font-size: 1rem;
color: #666;
margin-bottom: 10px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.report-list {
list-style: none;
}
.report-list li {
margin-bottom: 8px;
}
.report-list a {
display: block;
padding: 10px 12px;
background: #f8f8f8;
border-radius: 6px;
text-decoration: none;
color: #333;
transition: all 0.2s;
border-left: 3px solid transparent;
}
.report-list a:hover {
background: #e8f5e9;
border-left-color: #4a7c50;
}
.report-list a.active {
background: #e8f5e9;
border-left-color: #2c5530;
font-weight: 500;
}
.report-list .date {
font-size: 0.8rem;
color: #888;
}
.main-content {
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.report-header {
margin-bottom: 20px;
padding-bottom: 15px;
border-bottom: 1px solid #eee;
}
.report-header h2 {
color: #2c5530;
margin-bottom: 5px;
}
.report-meta {
display: flex;
gap: 20px;
flex-wrap: wrap;
font-size: 0.9rem;
color: #666;
}
.filters {
display: flex;
gap: 10px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.filters input, .filters select {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 6px;
font-size: 0.9rem;
}
.filters input {
flex: 1;
min-width: 200px;
}
.filters button {
padding: 8px 20px;
background: #2c5530;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 0.9rem;
}
.filters button:hover {
background: #4a7c50;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 0.9rem;
}
th {
background: #f8f8f8;
padding: 12px 10px;
text-align: left;
font-weight: 600;
color: #555;
border-bottom: 2px solid #ddd;
position: sticky;
top: 0;
}
td {
padding: 10px;
border-bottom: 1px solid #eee;
}
tr:hover {
background: #fafafa;
}
.item-num {
font-weight: 600;
color: #2c5530;
white-space: nowrap;
}
.metric-name {
max-width: 300px;
}
.value-cell {
text-align: right;
font-family: 'Consolas', 'Monaco', monospace;
white-space: nowrap;
}
.value-cell.positive {
color: #2e7d32;
}
.value-cell.negative {
color: #c62828;
}
.category-badge {
display: inline-block;
padding: 2px 8px;
background: #e8f5e9;
color: #2c5530;
border-radius: 12px;
font-size: 0.75rem;
white-space: nowrap;
}
.no-data {
text-align: center;
padding: 40px;
color: #888;
}
.table-container {
max-height: 600px;
overflow-y: auto;
}
.results-count {
font-size: 0.85rem;
color: #666;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>🏭 Mill Data Viewer</h1>
<p>Sugar Mill Production Report Data</p>
</header>
<div class="stats">
<div class="stat-card">
<div class="value"><?= $stats['TotalReports'] ?? 0 ?></div>
<div class="label">Reports</div>
</div>
<div class="stat-card">
<div class="value"><?= number_format($stats['TotalMetrics'] ?? 0) ?></div>
<div class="label">Metrics</div>
</div>
<div class="stat-card">
<div class="value"><?= formatDate($stats['EarliestDate']) ?></div>
<div class="label">Earliest</div>
</div>
<div class="stat-card">
<div class="value"><?= formatDate($stats['LatestDate']) ?></div>
<div class="label">Latest</div>
</div>
</div>
<div class="layout">
<aside class="sidebar">
<h2>Reports</h2>
<ul class="report-list">
<?php foreach ($sourceFiles as $file): ?>
<li>
<a href="?file=<?= urlencode($file['SourceFileName']) ?>"
class="<?= $file['SourceFileName'] == $selectedFile ? 'active' : '' ?>">
<?= htmlspecialchars($file['SourceFileName']) ?>
<div class="date">
<?= formatDate($file['BeginningDate']) ?> - <?= formatDate($file['EndingDate']) ?>
</div>
</a>
</li>
<?php endforeach; ?>
<?php if (empty($sourceFiles)): ?>
<li class="no-data">No reports found</li>
<?php endif; ?>
</ul>
</aside>
<main class="main-content">
<?php if ($selectedReport): ?>
<div class="report-header">
<h2><?= htmlspecialchars($selectedReport['ReportTitle'] ?? $selectedReport['SourceFileName']) ?></h2>
<div class="report-meta">
<span>📅 <?= formatDate($selectedReport['BeginningDate']) ?> - <?= formatDate($selectedReport['EndingDate']) ?></span>
<span>📊 <?= $selectedReport['CropDays'] ?? '-' ?> crop days</span>
<span>🏭
<select onchange="window.location.href='?file=<?= urlencode($selectedFile) ?>&report='+this.value" style="padding: 4px 8px; border-radius: 4px; border: 1px solid #ccc;">
<?php foreach ($millsForFile as $mill):
// Display "State Average" for STATE_AVG mill
$displayName = $mill['MillName'];
if ($displayName === 'Mill STATE_AVG') {
$displayName = 'State Average';
}
?>
<option value="<?= $mill['ReportId'] ?>" <?= $mill['ReportId'] == $selectedReportId ? 'selected' : '' ?>>
<?= htmlspecialchars($displayName) ?>
</option>
<?php endforeach; ?>
</select>
</span>
</div>
</div>
<form class="filters" method="get">
<input type="hidden" name="file" value="<?= htmlspecialchars($selectedFile) ?>">
<input type="hidden" name="report" value="<?= $selectedReportId ?>">
<input type="text" name="search" placeholder="Search metrics..."
value="<?= htmlspecialchars($searchTerm) ?>">
<select name="category">
<option value="">All Categories</option>
<?php foreach ($categories as $cat): ?>
<option value="<?= htmlspecialchars($cat) ?>"
<?= $cat === $selectedCategory ? 'selected' : '' ?>>
<?= htmlspecialchars($cat) ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit">Filter</button>
<?php if ($searchTerm || $selectedCategory): ?>
<a href="?report=<?= $selectedReportId ?>" style="padding: 8px; color: #666;">Clear</a>
<?php endif; ?>
</form>
<div class="results-count">
Showing <?= count($metrics) ?> metrics
<?php if ($searchTerm): ?>
matching "<?= htmlspecialchars($searchTerm) ?>"
<?php endif; ?>
</div>
<div class="table-container">
<table>
<thead>
<tr>
<th>Item</th>
<th>Metric</th>
<th>RUN</th>
<th>TO DATE</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<?php foreach ($metrics as $m): ?>
<tr>
<td class="item-num"><?= htmlspecialchars($m['ItemNumber'] ?? '') ?></td>
<td class="metric-name"><?= htmlspecialchars($m['MetricName'] ?? '') ?></td>
<td class="value-cell <?= ($m['RunValueNumeric'] ?? 0) < 0 ? 'negative' : '' ?>">
<?= htmlspecialchars($m['RunValue'] ?? '') ?>
</td>
<td class="value-cell <?= ($m['ToDateValueNumeric'] ?? 0) < 0 ? 'negative' : '' ?>">
<?= htmlspecialchars($m['ToDateValue'] ?? '') ?>
</td>
<td>
<?php if ($m['Category']): ?>
<span class="category-badge"><?= htmlspecialchars($m['Category']) ?></span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($metrics)): ?>
<tr>
<td colspan="5" class="no-data">No metrics found</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="no-data">
<p>Select a report from the sidebar to view metrics</p>
</div>
<?php endif; ?>
</main>
</div>
</div>
</body>
</html>