« Back to History
mesr_data_machine_manage.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_setup'); require_once __DIR__ . '/../modules/activity/activity_logger.php'; $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; $user_id = (int)($ctx['user']['id'] ?? 0); $message = ""; // --- 1. SAVE / UPDATE LOGIC --- if ($_SERVER['REQUEST_METHOD'] === 'POST') { $m_code = trim($_POST['machine_code']); $m_name = trim($_POST['machine_name']); $is_active = isset($_POST['is_active']) ? 1 : 0; $id = isset($_POST['id']) ? (int)$_POST['id'] : 0; if ($id > 0) { // Update Existing Machine $stmt = $pdo->prepare("UPDATE mesr_machine SET machine_code = ?, machine_name = ?, is_active = ? WHERE id = ? AND company_id = ?"); $stmt->execute([$m_code, $m_name, $is_active, $id, $company_id]); activity_log([ 'company_id' => $company_id, 'user_id' => $user_id, 'module' => 'mesr', 'action_name' => 'edit', 'entity_type' => 'mesr_machine', 'entity_id' => $id, 'remarks' => 'Updated MESR machine' ]); $message = "Machine updated successfully!"; } else { // Add New Machine $stmt = $pdo->prepare("INSERT INTO mesr_machine (company_id, machine_code, machine_name, is_active, created_at) VALUES (?, ?, ?, ?, NOW())"); $stmt->execute([$company_id, $m_code, $m_name, $is_active]); activity_log([ 'company_id' => $company_id, 'user_id' => $user_id, 'module' => 'mesr', 'action_name' => 'create', 'entity_type' => 'mesr_machine', 'entity_id' => (int)$pdo->lastInsertId(), 'remarks' => 'Created MESR machine' ]); $message = "New machine added!"; } } // --- 2. DELETE LOGIC --- if (isset($_GET['delete'])) { $id = (int)$_GET['delete']; $stmt = $pdo->prepare("DELETE FROM mesr_machine WHERE id = ? AND company_id = ?"); $stmt->execute([$id, $company_id]); activity_log([ 'company_id' => $company_id, 'user_id' => $user_id, 'module' => 'mesr', 'action_name' => 'delete', 'entity_type' => 'mesr_machine', 'entity_id' => $id, 'remarks' => 'Deleted MESR machine' ]); header("Location: mesr_machine_master.php?msg=Deleted"); exit; } // --- 3. FETCH DATA --- $stmt = $pdo->prepare("SELECT * FROM mesr_machine WHERE company_id = ? ORDER BY id DESC"); $stmt->execute([$company_id]); $machines = $stmt->fetchAll(PDO::FETCH_ASSOC); // Edit mode data fetch $edit_data = null; if (isset($_GET['edit'])) { foreach ($machines as $m) { if ($m['id'] == $_GET['edit']) { $edit_data = $m; break; } } } require_once __DIR__ . '/../partials/header.php'; ?> <div class="app-container"> <div class="index-welcome"> <h2>Machine Master <span class="welcome-user">| Manage Equipment</span></h2> </div> <?php if($message): ?> <div style="padding:10px; background:#d4edda; color:#155724; margin-bottom:15px; border-radius:4px;"><?= $message ?></div> <?php endif; ?> <div style="display: grid; grid-template-columns: 380px 1fr; gap: 20px;"> <div class="form-card" style="padding: 20px; background:#fff; border:1px solid #ddd; height: fit-content; border-radius: 8px;"> <h3 style="margin-top:0; color: #333;"><?= $edit_data ? 'Edit Machine Details' : 'Add New Machine' ?></h3> <hr style="border: 0; border-top: 1px solid #eee; margin-bottom: 20px;"> <form method="POST"> <?php if($edit_data): ?> <input type="hidden" name="id" value="<?= $edit_data['id'] ?>"> <?php endif; ?> <div class="form-group" style="margin-bottom:15px;"> <label style="font-weight: 600;">Machine Code (Internal)</label> <input type="text" name="machine_code" class="form-control" value="<?= $edit_data ? htmlspecialchars($edit_data['machine_code']) : '' ?>" required placeholder="e.g. TFO, ZARI"> </div> <div class="form-group" style="margin-bottom:15px;"> <label style="font-weight: 600;">Display Name</label> <input type="text" name="machine_name" class="form-control" value="<?= $edit_data ? htmlspecialchars($edit_data['machine_name']) : '' ?>" required placeholder="e.g. Zari Machine"> </div> <div class="form-group" style="margin-bottom:20px;"> <label style="display:flex; align-items:center; cursor:pointer; font-weight: 600;"> <input type="checkbox" name="is_active" <?= (!$edit_data || $edit_data['is_active']) ? 'checked' : '' ?> style="margin-right:10px; transform: scale(1.2);"> Machine is Active </label> </div> <div style="display:flex; gap:10px;"> <button type="submit" class="btn-primary" style="flex:1; padding:12px; border:none; cursor:pointer; font-weight: bold; border-radius: 4px;"> <?= $edit_data ? 'Update Machine' : 'Save Machine' ?> </button> <?php if($edit_data): ?> <a href="mesr_machine_master.php" class="btn-secondary" style="text-decoration:none; padding:12px; background:#6c757d; color:#fff; border-radius: 4px;">Cancel</a> <?php endif; ?> </div> </form> </div> <div class="form-card" style="padding: 20px; background:#fff; border:1px solid #ddd; border-radius: 8px;"> <table class="table erp-table" style="width:100%; border-collapse: collapse;"> <thead> <tr style="background:#f8f9fa; text-align:left;"> <th style="padding:12px; border-bottom:2px solid #dee2e6;">Code</th> <th style="padding:12px; border-bottom:2px solid #dee2e6;">Machine Name</th> <th style="padding:12px; border-bottom:2px solid #dee2e6;">Status</th> <th style="padding:12px; border-bottom:2px solid #dee2e6; text-align:center;">Action</th> </tr> </thead> <tbody> <?php if(empty($machines)): ?> <tr><td colspan="4" style="text-align:center; padding:20px; color:#999;">No machines found.</td></tr> <?php endif; ?> <?php foreach($machines as $row): ?> <tr style="border-bottom:1px solid #eee;"> <td style="padding:12px; font-family: monospace; color: #555;"><?= htmlspecialchars($row['machine_code']) ?></td> <td style="padding:12px; font-weight:bold;"><?= htmlspecialchars($row['machine_name']) ?></td> <td style="padding:12px;"> <span style="padding:4px 10px; border-radius:20px; font-size:11px; font-weight: bold; background:<?= $row['is_active'] ? '#e2f9e1' : '#ffe9e9' ?>; color:<?= $row['is_active'] ? '#1e7e34' : '#c82333' ?>; border: 1px solid currentColor;"> <?= $row['is_active'] ? 'ACTIVE' : 'INACTIVE' ?> </span> </td> <td style="padding:12px; text-align:center;"> <a href="?edit=<?= $row['id'] ?>" style="color:#007bff; text-decoration:none; margin-right:15px; font-weight: 600;">Edit</a> <a href="?delete=<?= $row['id'] ?>" onclick="return confirm('Machine delete karein?')" style="color:#dc3545; text-decoration:none; font-weight: 600;">Delete</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <?php require_once __DIR__ . '/../partials/footer.php'; ?>