« Back to History
running_beam_snapshot.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('production_entry'); // adjust permission if needed $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $u = $ctx['user']; require_once __DIR__ . '/partials/header.php'; $message = ''; /* ========================= SAVE SNAPSHOT (DELETE-THEN-INSERT) ========================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_snapshot'])) { $pdo->beginTransaction(); try { // 1. First delete existing snapshot data for this company $deleteStmt = $pdo->prepare("DELETE FROM mesr_running_beam_snapshot WHERE company_id = ?"); $deleteStmt->execute([$company_id]); // 2. Fetch current running beams $stmt = $pdo->prepare(" SELECT machine_no, primary_running_beam, secondary_running_beam FROM machine_beam_status WHERE company_id = ? "); $stmt->execute([$company_id]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // 3. Insert fresh snapshot data if ($rows) { $insert = $pdo->prepare(" INSERT INTO mesr_running_beam_snapshot (company_id, machine_no, primary_running_beam, secondary_running_beam, snapshot_at) VALUES (?, ?, ?, ?, NOW()) "); foreach ($rows as $r) { $insert->execute([ $company_id, $r['machine_no'], $r['primary_running_beam'], $r['secondary_running_beam'] ]); } } $pdo->commit(); $message = "Snapshot updated successfully (Old records cleared)."; } catch (Exception $e) { $pdo->rollBack(); $message = "Error: " . $e->getMessage(); } } /* ========================= CURRENT RUNNING LIST ========================= */ $stmt = $pdo->prepare(" SELECT machine_no, primary_running_beam, secondary_running_beam FROM machine_beam_status WHERE company_id = ? ORDER BY machine_no "); $stmt->execute([$company_id]); $running = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <div class="container-fluid mt-4"> <div class="card shadow-sm"> <div class="card-header d-flex justify-content-between align-items-center"> <h5 class="mb-0">Running Beam Snapshot</h5> <form method="post" class="mb-0"> <button type="submit" name="save_snapshot" class="btn btn-primary btn-sm"> Save Snapshot </button> </form> </div> <div class="card-body"> <?php if ($message): ?> <div class="alert alert-info"> <?= htmlspecialchars($message) ?> </div> <?php endif; ?> <div class="table-responsive"> <table class="table table-bordered table-striped"> <thead class="table-light"> <tr> <th>Machine No</th> <th>Primary Running Beam</th> <th>Secondary Running Beam</th> </tr> </thead> <tbody> <?php if ($running): ?> <?php foreach ($running as $r): ?> <tr> <td><?= htmlspecialchars($r['machine_no']) ?></td> <td><?= htmlspecialchars($r['primary_running_beam']) ?></td> <td><?= htmlspecialchars($r['secondary_running_beam']) ?></td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="3" class="text-center">No running beams found</td> </tr> <?php endif; ?> </tbody> </table> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>