« Back to History
mesr_data_location_manage.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_setup'); // Ya jo bhi aapki access key ho 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') { $loc_name = trim($_POST['location_name']); $is_active = isset($_POST['is_active']) ? 1 : 0; $id = isset($_POST['id']) ? (int)$_POST['id'] : 0; if ($id > 0) { // Update Existing $stmt = $pdo->prepare("UPDATE mesr_data_location SET location_name = ?, is_active = ? WHERE id = ? AND company_id = ?"); $stmt->execute([$loc_name, $is_active, $id, $company_id]); activity_log([ 'company_id' => $company_id, 'user_id' => $user_id, 'module' => 'mesr', 'action_name' => 'edit', 'entity_type' => 'mesr_data_location', 'entity_id' => $id, 'remarks' => 'Updated MESR location' ]); $message = "Location updated successfully!"; } else { // Add New $stmt = $pdo->prepare("INSERT INTO mesr_data_location (company_id, location_name, is_active, created_at) VALUES (?, ?, ?, NOW())"); $stmt->execute([$company_id, $loc_name, $is_active]); activity_log([ 'company_id' => $company_id, 'user_id' => $user_id, 'module' => 'mesr', 'action_name' => 'create', 'entity_type' => 'mesr_data_location', 'entity_id' => (int)$pdo->lastInsertId(), 'remarks' => 'Created MESR location' ]); $message = "New location added!"; } } // --- 2. DELETE LOGIC --- if (isset($_GET['delete'])) { $id = (int)$_GET['delete']; $stmt = $pdo->prepare("DELETE FROM mesr_data_location 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_data_location', 'entity_id' => $id, 'remarks' => 'Deleted MESR location' ]); header("Location: mesr_location_master.php?msg=Deleted"); exit; } // --- 3. FETCH DATA --- $stmt = $pdo->prepare("SELECT * FROM mesr_data_location WHERE company_id = ? ORDER BY id DESC"); $stmt->execute([$company_id]); $locations = $stmt->fetchAll(PDO::FETCH_ASSOC); // Edit mode check $edit_data = null; if (isset($_GET['edit'])) { foreach ($locations as $loc) { if ($loc['id'] == $_GET['edit']) { $edit_data = $loc; break; } } } require_once __DIR__ . '/../partials/header.php'; ?> <div class="app-container"> <div class="index-welcome"> <h2>Location Master <span class="welcome-user">| Manage Data</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: 350px 1fr; gap: 20px;"> <div class="form-card" style="padding: 20px; background:#fff; border:1px solid #ddd; height: fit-content;"> <h3 style="margin-top:0;"><?= $edit_data ? 'Edit Location' : 'Add New Location' ?></h3> <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>Location Name</label> <input type="text" name="location_name" class="form-control" value="<?= $edit_data ? htmlspecialchars($edit_data['location_name']) : '' ?>" required placeholder="e.g. Warping Jayesh"> </div> <div class="form-group" style="margin-bottom:15px;"> <label style="display:flex; align-items:center; cursor:pointer;"> <input type="checkbox" name="is_active" <?= (!$edit_data || $edit_data['is_active']) ? 'checked' : '' ?> style="margin-right:10px;"> Is Active? </label> </div> <div style="display:flex; gap:10px;"> <button type="submit" class="btn-primary" style="flex:1; padding:10px; border:none; cursor:pointer;"> <?= $edit_data ? 'Update Location' : 'Save Location' ?> </button> <?php if($edit_data): ?> <a href="mesr_location_master.php" class="btn-secondary" style="text-decoration:none; padding:10px; background:#eee; color:#333;">Cancel</a> <?php endif; ?> </div> </form> </div> <div class="form-card" style="padding: 20px; background:#fff; border:1px solid #ddd;"> <table class="table erp-table" style="width:100%; border-collapse: collapse;"> <thead> <tr style="background:#f4f4f4; text-align:left;"> <th style="padding:10px; border-bottom:2px solid #ddd;">ID</th> <th style="padding:10px; border-bottom:2px solid #ddd;">Location Name</th> <th style="padding:10px; border-bottom:2px solid #ddd;">Status</th> <th style="padding:10px; border-bottom:2px solid #ddd; text-align:center;">Action</th> </tr> </thead> <tbody> <?php foreach($locations as $row): ?> <tr style="border-bottom:1px solid #eee;"> <td style="padding:10px;"><?= $row['id'] ?></td> <td style="padding:10px; font-weight:bold;"><?= htmlspecialchars($row['location_name']) ?></td> <td style="padding:10px;"> <span style="padding:2px 8px; border-radius:10px; font-size:12px; background:<?= $row['is_active'] ? '#d4edda' : '#f8d7da' ?>; color:<?= $row['is_active'] ? '#155724' : '#721c24' ?>;"> <?= $row['is_active'] ? 'Active' : 'Inactive' ?> </span> </td> <td style="padding:10px; text-align:center;"> <a href="?edit=<?= $row['id'] ?>" style="color:#007bff; text-decoration:none; margin-right:15px;">Edit</a> <a href="?delete=<?= $row['id'] ?>" onclick="return confirm('Pakka delete karna hai?')" style="color:#dc3545; text-decoration:none;">Delete</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <?php require_once __DIR__ . '/../partials/footer.php'; ?>