'192.168.0.16', 'database' => 'lasucaai', 'username' => 'lasucaai', 'password' => 'is413#dfslw', ]; // ============================================================================ // Database Connection // ============================================================================ function getCompareConnection($config) { $connectionOptions = [ "Database" => $config['database'], "Uid" => $config['username'], "PWD" => $config['password'], "TrustServerCertificate" => true, "Encrypt" => false, ]; $conn = sqlsrv_connect($config['server'], $connectionOptions); return $conn ?: null; } // ============================================================================ // Data Queries // ============================================================================ function getCompareSourceFiles($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 = []; if ($stmt) { while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $files[] = $row; } } return $files; } function getCompareCategories($conn, $sourceFileName = null) { // Get all categories - simpler query that matches working pattern $sql = "SELECT DISTINCT Category FROM dbo.MillDataMetrics WHERE Category IS NOT NULL ORDER BY Category"; $stmt = sqlsrv_query($conn, $sql); $categories = []; if ($stmt) { while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $categories[] = $row['Category']; } } return $categories; } function getMillsForCompare($conn, $sourceFileName) { $sql = "SELECT ReportId, MillName FROM dbo.MillDataReports WHERE SourceFileName = ? ORDER BY MillName"; $stmt = sqlsrv_query($conn, $sql, [$sourceFileName]); $mills = []; if ($stmt) { while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $mills[] = $row; } } return $mills; } function getComparisonData($conn, $sourceFileName, $category) { // Get all metrics for this category across all mills in this file $sql = "SELECT r.MillName, r.ReportId, m.ItemNumber, m.MetricName, m.RunValue, m.RunValueNumeric, m.ToDateValue, m.ToDateValueNumeric FROM dbo.MillDataMetrics m JOIN dbo.MillDataReports r ON m.ReportId = r.ReportId WHERE r.SourceFileName = ? AND m.Category = ? ORDER BY m.ItemNumber, m.MetricName, r.MillName"; $stmt = sqlsrv_query($conn, $sql, [$sourceFileName, $category]); $data = []; if ($stmt) { while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $data[] = $row; } } return $data; } function formatCompareDate($date) { if ($date instanceof DateTime) { return $date->format('m/d/Y'); } return $date ?? '-'; } // ============================================================================ // Main Logic // ============================================================================ $conn = getCompareConnection($config); $connectionError = $conn === null; $sourceFiles = []; $categories = []; $mills = []; $comparisonData = []; $pivotedData = []; $millNameLookup = []; $selectedFile = isset($_GET['file']) ? trim($_GET['file']) : ''; $selectedCategory = isset($_GET['category']) ? trim($_GET['category']) : ''; if (!$connectionError) { // Load mill name mappings $millNameLookup = getMillNames($conn); $sourceFiles = getCompareSourceFiles($conn); $categories = getCompareCategories($conn); // Default to first file if (empty($selectedFile) && !empty($sourceFiles)) { $selectedFile = $sourceFiles[0]['SourceFileName']; } if (!empty($selectedFile)) { $mills = getMillsForCompare($conn, $selectedFile); // Default to first category if (empty($selectedCategory) && !empty($categories)) { $selectedCategory = $categories[0]; } if (!empty($selectedCategory)) { $comparisonData = getComparisonData($conn, $selectedFile, $selectedCategory); // Pivot data: group by metric, columns are mills foreach ($comparisonData as $row) { $metricKey = $row['ItemNumber'] . '|' . $row['MetricName']; if (!isset($pivotedData[$metricKey])) { $pivotedData[$metricKey] = [ 'ItemNumber' => $row['ItemNumber'], 'MetricName' => $row['MetricName'], 'mills' => [] ]; } $pivotedData[$metricKey]['mills'][$row['MillName']] = [ 'RunValue' => $row['RunValue'], 'RunValueNumeric' => $row['RunValueNumeric'], 'ToDateValue' => $row['ToDateValue'], 'ToDateValueNumeric' => $row['ToDateValueNumeric'] ]; } } } sqlsrv_close($conn); } // Get unique mill codes for column headers (raw codes used as data keys) $millCodes = []; foreach ($mills as $m) { $millCodes[] = $m['MillName']; } // Build display names map for headers $millDisplayNames = []; foreach ($millCodes as $code) { $millDisplayNames[$code] = getMillDisplayName($millNameLookup, $code); } // ============================================================================ // Page Layout // ============================================================================ $pageTitle = 'Mill Comparison'; $pageSubtitle = 'Compare metrics across mills'; $pageDescription = 'Side-by-side comparison of mill production data.'; $layoutWithoutSidebar = true; $layoutReturnUrl = 'milldata.php'; $layoutReturnLabel = 'Back to Mill Data'; $assetBasePath = '../'; require __DIR__ . '/../includes/layout/header.php'; ?>
| Item | Metric | $millCode): ?>= htmlspecialchars($millDisplayNames[$millCode]) ?> | |
|---|---|---|---|
| RUN | TO DATE | ||
| = htmlspecialchars($metric['ItemNumber'] ?? '') ?> | = htmlspecialchars($metric['MetricName'] ?? '') ?> | $millCode): ?>= htmlspecialchars($runValue) ?> | = htmlspecialchars($toDateValue) ?> |
No comparison data available. Select a file and category above.