« Back to History
edit_beam_status.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================ edit_beam_status.php (WITH FINISH DATE OVERRIDE) - Edit/override beam status and finish date - Detects available columns in beam_card_overrides and acts accordingly - Immediately updates beam_card.status and beam_card.finish_date so report reflects change ============================================================================ */ require_once __DIR__ . '/modules/auth/auth.php'; require_login(); $user = auth_user(); $company_id = (int)($user['company_id'] ?? 0); $user_id = (int)($user['id'] ?? 0); $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) require_once __DIR__ . '/core/db.php'; /* Safe session start */ if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } /* CSRF setup */ if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(16)); } $csrf = $_SESSION['csrf_token']; /* Helpers */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function q1($pdo,$sql,$p=[]){ $st=$pdo->prepare($sql); $st->execute($p); return $st->fetch(PDO::FETCH_ASSOC); } function qcol($pdo,$sql,$p=[]){ $st=$pdo->prepare($sql); $st->execute($p); return $st->fetchColumn(); } /* Input */ $beam = trim($_GET['beam'] ?? $_POST['beam'] ?? ''); if ($beam === '') { header("Location: beam_card_report.php?msg=" . urlencode("Invalid beam selected.")); exit; } $allowed_statuses = ['RUNNING','FINISHED','CUT','INSTALLED_ADVANCE']; $message = null; /* POST handling */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (empty($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) { $message = "Invalid CSRF token."; } else { $status = strtoupper(trim($_POST['status'] ?? '')); $notes = trim($_POST['notes'] ?? ''); $finish_input = trim($_POST['finish_date'] ?? ''); // expected format YYYY-MM-DD or blank // validate status if (!in_array($status, $allowed_statuses, true)) { $message = "Invalid status selected."; } else { // validate finish_date if provided $finish_date = null; if ($finish_input !== '') { // simple YYYY-MM-DD check $d = DateTime::createFromFormat('Y-m-d', $finish_input); if (!$d || $d->format('Y-m-d') !== $finish_input) { $message = "Invalid finish date format. Use YYYY-MM-DD."; } else { $finish_date = $finish_input; } } if ($message === null) { try { // Detect columns in beam_card_overrides $colsPresent = []; $st = $pdo->query("SHOW COLUMNS FROM beam_card_overrides"); $cols = $st->fetchAll(PDO::FETCH_COLUMN, 0); $colsPresent = array_flip($cols); // Build insert/update for overrides $insertCols = ['company_id','beam_no','status_override']; $placeholders = [':cid', ':beam', ':st']; $updateAssignments = ['status_override=VALUES(status_override)']; $params = [ ':cid' => $company_id, ':beam' => $beam, ':st' => $status ]; if (isset($colsPresent['notes'])) { $insertCols[] = 'notes'; $placeholders[] = ':notes'; $updateAssignments[] = 'notes=VALUES(notes)'; $params[':notes'] = $notes; } if (isset($colsPresent['finish_override'])) { $insertCols[] = 'finish_override'; $placeholders[] = ':finish_override'; $updateAssignments[] = 'finish_override=VALUES(finish_override)'; $params[':finish_override'] = $finish_date; // may be null } if (isset($colsPresent['override_by'])) { $insertCols[] = 'override_by'; $placeholders[] = ':override_by'; $updateAssignments[] = 'override_by=VALUES(override_by)'; $params[':override_by'] = $user_id; } $useOverrideAt = isset($colsPresent['override_at']); if ($useOverrideAt) { $insertCols[] = 'override_at'; $placeholders[] = 'NOW()'; $updateAssignments[] = 'override_at=VALUES(override_at)'; } $colsSql = implode(',', $insertCols); $valsSql = implode(',', $placeholders); $updateSql = implode(',', $updateAssignments); $sql = "INSERT INTO beam_card_overrides ($colsSql) VALUES ($valsSql) ON DUPLICATE KEY UPDATE $updateSql"; $stmt = $pdo->prepare($sql); $stmt->execute($params); // Immediately reflect in beam_card: update status and finish_date (if provided) try { $updParams = [':st' => $status, ':cid' => $company_id, ':beam' => $beam]; $updSql = "UPDATE beam_card SET status = :st, updated_at = NOW()"; if ($finish_date !== null) { $updSql .= ", finish_date = :finish"; $updParams[':finish'] = $finish_date; } $updSql .= " WHERE company_id = :cid AND beam_no = :beam"; $upd = $pdo->prepare($updSql); $upd->execute($updParams); if ($upd->rowCount() === 0) { // No existing beam_card row. Insert minimal row with status and optional finish. if ($finish_date !== null) { $ins = $pdo->prepare("INSERT INTO beam_card (company_id, beam_no, status, finish_date, created_at, updated_at) VALUES (:cid, :beam, :st, :finish, NOW(), NOW())"); $ins->execute([':cid' => $company_id, ':beam' => $beam, ':st' => $status, ':finish' => $finish_date]); } else { $ins = $pdo->prepare("INSERT INTO beam_card (company_id, beam_no, status, created_at, updated_at) VALUES (:cid, :beam, :st, NOW(), NOW())"); $ins->execute([':cid' => $company_id, ':beam' => $beam, ':st' => $status]); } } } catch (Exception $innerEx) { // Non-fatal: ignore to avoid breaking user flow // error_log("beam_card immediate update failed: " . $innerEx->getMessage()); } header("Location: beam_card_report.php?msg=" . urlencode("Override saved for beam {$beam}.")); exit; } catch (Exception $ex) { $message = "Database error while saving override: " . h($ex->getMessage()); } } } } } /* Fetch current beam_card data */ $row = q1($pdo, "SELECT * FROM beam_card WHERE company_id=:c AND beam_no=:b LIMIT 1", [ ':c'=>$company_id, ':b'=>$beam ]); /* Include header if exists */ if (file_exists(__DIR__ . '/erp/partials/header.php')) require_once __DIR__ . '/erp/partials/header.php'; ?> <div class="card" style="max-width:720px;margin:20px auto;padding:18px;"> <h2>Edit Beam — <?= h($beam) ?></h2> <?php if ($message): ?> <div class="notice" style="padding:8px;margin-bottom:10px;background:#fee;border-radius:6px"><?= h($message) ?></div> <?php endif; ?> <div style="margin-bottom:12px"> <strong>Calculated status:</strong> <div><?= isset($row['status']) ? h($row['status']) : '-' ?></div> <small>Machine: <?= h($row['machine_no'] ?? '-') ?> | Install: <?= h($row['install_date'] ?? '-') ?> | Finish: <?= h($row['finish_date'] ?? '-') ?></small> </div> <form method="post"> <input type="hidden" name="csrf_token" value="<?= h($csrf) ?>"> <input type="hidden" name="beam" value="<?= h($beam) ?>"> <div style="margin-bottom:8px"> <label for="status"><strong>Override status</strong></label><br> <select id="status" name="status" required> <?php foreach ($allowed_statuses as $s): ?> <option value="<?= h($s) ?>"><?= h($s) ?></option> <?php endforeach; ?> </select> </div> <div style="margin-bottom:8px"> <label for="finish_date"><strong>Finish date (optional)</strong></label><br> <input type="date" id="finish_date" name="finish_date" value="<?= isset($row['finish_date']) ? h($row['finish_date']) : '' ?>"> <div style="font-size:90%;color:#666">यदि आप finish date सेट करेंगे तो उसे override कर देगा।</div> </div> <div style="margin-bottom:8px"> <label for="notes">Notes / Reason (optional)</label><br> <textarea id="notes" name="notes" rows="4" cols="60"></textarea> </div> <div> <button type="submit" class="btn">Save Override</button> <a class="btn" href="beam_card_report.php">Back to report</a> </div> </form> </div> <?php /* Include footer if exists */ if (file_exists(__DIR__ . '/erp/partials/footer.php')) require_once __DIR__ . '/erp/partials/footer.php'; ?>