« Back to History
company_salary_types.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('company_salary_types'); $u = $ctx['user']; $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $user_id = (int)$ctx['user']['id']; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ========================= POST HANDLER ========================= */ $flash = ''; $err = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $action = $_POST['action'] ?? ''; if ($action === 'add') { $name = trim($_POST['name'] ?? ''); $code = trim($_POST['code'] ?? ''); $is_active = isset($_POST['is_active']) ? 1 : 0; if ($name === '' || $code === '') { throw new Exception("Name and Code required"); } // Duplicate check per company $chk = $pdo->prepare(" SELECT id FROM company_salary_types WHERE company_id=? AND (name=? OR code=?) LIMIT 1 "); $chk->execute([$company_id, $name, $code]); if ($chk->fetch()) { throw new Exception("Name or Code already exists"); } $ins = $pdo->prepare(" INSERT INTO company_salary_types (company_id, name, code, is_active, created_by) VALUES (?,?,?,?,?) "); $ins->execute([ $company_id, $name, strtoupper($code), $is_active, $user_id ]); require_once __DIR__ . '/helpers/activity_helper.php'; log_activity([ 'activity_type' => 'action', 'module_name' => 'payroll', 'action_name' => 'CREATE', 'reference_id' => (int)$pdo->lastInsertId(), 'activity_note' => "Added new salary type: $name ($code)" ]); $flash = "Salary type added successfully."; } if ($action === 'delete') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) throw new Exception("Invalid ID"); $del = $pdo->prepare(" DELETE FROM company_salary_types WHERE id=? AND company_id=? "); $del->execute([$id, $company_id]); require_once __DIR__ . '/helpers/activity_helper.php'; log_activity([ 'activity_type' => 'action', 'module_name' => 'payroll', 'action_name' => 'DELETE', 'reference_id' => $id, 'activity_note' => "Deleted salary type ID: $id" ]); $flash = "Deleted successfully."; } } catch (Throwable $e) { $err = $e->getMessage(); } } /* ========================= LOAD LIST ========================= */ $st = $pdo->prepare(" SELECT id, name, code, is_active, created_at FROM company_salary_types WHERE company_id=? ORDER BY name ASC "); $st->execute([$company_id]); $rows = $st->fetchAll(PDO::FETCH_ASSOC); ?> <?php require_once __DIR__ . '/partials/header.php'; ?> <div class="container-fluid py-4"> <h4 class="fw-bold mb-4">Company Salary Types</h4> <?php if ($flash): ?> <div class="alert alert-success"><?= h($flash) ?></div> <?php endif; ?> <?php if ($err): ?> <div class="alert alert-danger"><?= h($err) ?></div> <?php endif; ?> <!-- ADD FORM --> <div class="card shadow-sm border-0 mb-4"> <div class="card-body"> <h5 class="fw-bold mb-3">Add Salary Type</h5> <form method="post" class="row g-3"> <input type="hidden" name="action" value="add"> <div class="col-md-4"> <label class="form-label">Name</label> <input type="text" name="name" class="form-control" required> </div> <div class="col-md-3"> <label class="form-label">Code</label> <input type="text" name="code" class="form-control" required> </div> <div class="col-md-2 d-flex align-items-end"> <div class="form-check"> <input type="checkbox" name="is_active" class="form-check-input" checked> <label class="form-check-label">Active</label> </div> </div> <div class="col-md-3 d-flex align-items-end"> <button class="btn btn-primary w-100"> Add Salary Type </button> </div> </form> </div> </div> <!-- LIST --> <div class="card shadow-sm border-0"> <div class="card-body"> <div class="table-responsive"> <table class="table table-hover align-middle"> <thead class="table-light"> <tr> <th>ID</th> <th>Name</th> <th>Code</th> <th>Status</th> <th>Created</th> <th>Action</th> </tr> </thead> <tbody> <?php if (!$rows): ?> <tr> <td colspan="6" class="text-center">No records found.</td> </tr> <?php else: foreach ($rows as $r): ?> <tr> <td><?= (int)$r['id'] ?></td> <td><?= h($r['name']) ?></td> <td><?= h($r['code']) ?></td> <td> <?php if ($r['is_active']): ?> <span class="badge bg-success">Active</span> <?php else: ?> <span class="badge bg-danger">Inactive</span> <?php endif; ?> </td> <td><?= h($r['created_at']) ?></td> <td> <form method="post" onsubmit="return confirm('Delete this salary type?');" class="d-inline"> <input type="hidden" name="action" value="delete"> <input type="hidden" name="id" value="<?= (int)$r['id'] ?>"> <button class="btn btn-sm btn-danger">Delete</button> </form> </td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>