153 lines
4.8 KiB
PHP
153 lines
4.8 KiB
PHP
<?php // phpcs:ignoreFile
|
|
/**
|
|
* Setup script to create mill_names lookup table
|
|
* Run this once via browser to create the table and populate with existing mill names
|
|
* Access via: http://server/reports/setup_mill_names.php
|
|
*/
|
|
|
|
require __DIR__ . '/../session.php';
|
|
require __DIR__ . '/../userAccess.php';
|
|
|
|
// Only allow admin or controls
|
|
if ($_SESSION['SESS_MEMBER_LEVEL'] < 4) {
|
|
header("Location: ../access-denied.php");
|
|
exit();
|
|
}
|
|
|
|
$config = [
|
|
'server' => '192.168.0.16',
|
|
'database' => 'lasucaai',
|
|
'username' => 'lasucaai',
|
|
'password' => 'is413#dfslw',
|
|
];
|
|
|
|
$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));
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Mill Names Setup</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; margin: 20px; background: #1a1a2e; color: #eee; }
|
|
table { border-collapse: collapse; margin: 10px 0; }
|
|
th, td { border: 1px solid #444; padding: 8px 12px; text-align: left; }
|
|
th { background: #333; }
|
|
.success { color: #4caf50; }
|
|
.error { color: #f44336; }
|
|
code { background: #333; padding: 2px 6px; border-radius: 3px; }
|
|
a { color: #64b5f6; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<?php
|
|
echo "<h2>Mill Names Setup</h2>";
|
|
|
|
// Step 1: Create the mill_names table
|
|
$createTableSql = "
|
|
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='mill_names' AND xtype='U')
|
|
CREATE TABLE mill_names (
|
|
mill_id INT IDENTITY(1,1) PRIMARY KEY,
|
|
mill_code NVARCHAR(100) NOT NULL UNIQUE,
|
|
display_name NVARCHAR(200) NOT NULL,
|
|
sort_order INT DEFAULT 0,
|
|
is_active BIT DEFAULT 1,
|
|
created_at DATETIME DEFAULT GETDATE()
|
|
)
|
|
";
|
|
|
|
$result = sqlsrv_query($conn, $createTableSql);
|
|
if ($result === false) {
|
|
echo "<p style='color:red'>Error creating table: " . print_r(sqlsrv_errors(), true) . "</p>";
|
|
} else {
|
|
echo "<p style='color:green'>✓ mill_names table created (or already exists)</p>";
|
|
}
|
|
|
|
// Step 2: Get existing mill names from reports
|
|
$existingMillsSql = "SELECT DISTINCT MillName FROM dbo.MillDataReports ORDER BY MillName";
|
|
$existingResult = sqlsrv_query($conn, $existingMillsSql);
|
|
|
|
$existingMills = [];
|
|
if ($existingResult) {
|
|
while ($row = sqlsrv_fetch_array($existingResult, SQLSRV_FETCH_ASSOC)) {
|
|
$existingMills[] = $row['MillName'];
|
|
}
|
|
}
|
|
|
|
echo "<h3>Existing Mill Codes in Reports:</h3>";
|
|
echo "<ul>";
|
|
foreach ($existingMills as $mill) {
|
|
echo "<li>" . htmlspecialchars($mill) . "</li>";
|
|
}
|
|
echo "</ul>";
|
|
|
|
// Step 3: Insert existing mills into lookup table (if not already there)
|
|
$insertCount = 0;
|
|
foreach ($existingMills as $index => $millCode) {
|
|
$checkSql = "SELECT mill_id FROM mill_names WHERE mill_code = ?";
|
|
$checkResult = sqlsrv_query($conn, $checkSql, [$millCode]);
|
|
|
|
if ($checkResult && !sqlsrv_fetch_array($checkResult)) {
|
|
// Not in table yet, insert with mill_code as default display_name
|
|
$insertSql = "INSERT INTO mill_names (mill_code, display_name, sort_order) VALUES (?, ?, ?)";
|
|
$insertResult = sqlsrv_query($conn, $insertSql, [$millCode, $millCode, $index + 1]);
|
|
if ($insertResult) {
|
|
$insertCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($insertCount > 0) {
|
|
echo "<p class='success'>✓ Inserted $insertCount new mill codes</p>";
|
|
} else {
|
|
echo "<p>No new mill codes to insert</p>";
|
|
}
|
|
|
|
// Step 4: Show current table contents
|
|
echo "<h3>Current mill_names Table:</h3>";
|
|
$selectSql = "SELECT * FROM mill_names ORDER BY sort_order";
|
|
$selectResult = sqlsrv_query($conn, $selectSql);
|
|
|
|
if ($selectResult) {
|
|
echo "<table>";
|
|
echo "<tr><th>ID</th><th>Mill Code</th><th>Display Name</th><th>Sort Order</th><th>Active</th></tr>";
|
|
|
|
while ($row = sqlsrv_fetch_array($selectResult, SQLSRV_FETCH_ASSOC)) {
|
|
echo "<tr>";
|
|
echo "<td>" . $row['mill_id'] . "</td>";
|
|
echo "<td>" . htmlspecialchars($row['mill_code']) . "</td>";
|
|
echo "<td>" . htmlspecialchars($row['display_name']) . "</td>";
|
|
echo "<td>" . $row['sort_order'] . "</td>";
|
|
echo "<td>" . ($row['is_active'] ? 'Yes' : 'No') . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
echo "</table>";
|
|
}
|
|
|
|
sqlsrv_close($conn);
|
|
|
|
echo "<hr>";
|
|
echo "<p><strong>Next steps:</strong></p>";
|
|
echo "<ol>";
|
|
echo "<li>Go to <a href='mill-names-admin.php'>Mill Names Admin</a> to update display names</li>";
|
|
echo "<li>Or run SQL like: <code>UPDATE mill_names SET display_name = 'East Mill' WHERE mill_code = 'EastMill'</code></li>";
|
|
echo "<li>Delete this setup script when done</li>";
|
|
echo "</ol>";
|
|
echo "<p><a href='mill-names-admin.php'>→ Open Mill Names Admin</a></p>";
|
|
?>
|
|
</body>
|
|
</html>";
|
|
echo "</ol>";
|
|
?>
|