« Back to History
payroll_periods.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ============================================================================= File: /erp/payroll_periods.php Purpose: View + Add + Edit/Update company_payroll_periods (company-scoped) Scope : Page-local only. No global/base changes. ============================================================================= */ error_reporting(E_ALL); ini_set('display_errors', 1); /* -------- Auth & PDO -------- */ require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $user_id = (int)$u['id']; $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/core/db.php'; } /* ---------- Page header (global include) ---------- */ require_once __DIR__ . '/partials/header.php'; /* -------- Helpers -------- */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function b2i($v){ return (!empty($v) && $v!=='0') ? 1 : 0; } /* -------- POST handlers -------- */ $flash = ''; $err = ''; if ($_SERVER['REQUEST_METHOD']==='POST') { $action = $_POST['action'] ?? ''; try { if ($action === 'add') { $payroll_type = trim($_POST['payroll_type'] ?? ''); $is_active = b2i($_POST['is_active'] ?? 1); $is_default = b2i($_POST['is_default'] ?? 0); $notes = trim($_POST['notes'] ?? ''); if ($payroll_type==='') throw new Exception('Payroll Type आवश्यक है।'); $pdo->beginTransaction(); if ($is_default === 1) { // Make sure only one default per company $pdo->prepare("UPDATE company_payroll_periods SET is_default=0 WHERE company_id=?") ->execute([$company_id]); } $stmt = $pdo->prepare("INSERT INTO company_payroll_periods (company_id, payroll_type, is_active, is_default, notes, created_by) VALUES (?,?,?,?,?,?)"); $stmt->execute([$company_id, $payroll_type, $is_active, $is_default, $notes!=='' ? $notes : null, $user_id]); $pdo->commit(); $flash = "Payroll period added."; } if ($action === 'update') { $id = (int)($_POST['id'] ?? 0); $payroll_type = trim($_POST['payroll_type'] ?? ''); $is_active = b2i($_POST['is_active'] ?? 0); $is_default = b2i($_POST['is_default'] ?? 0); $notes = trim($_POST['notes'] ?? ''); if ($id<=0) throw new Exception('Invalid ID'); if ($payroll_type==='') throw new Exception('Payroll Type आवश्यक है।'); $pdo->beginTransaction(); if ($is_default === 1) { // unset default for others in same company $pdo->prepare("UPDATE company_payroll_periods SET is_default=0 WHERE company_id=? AND id<>?") ->execute([$company_id, $id]); } $stmt = $pdo->prepare("UPDATE company_payroll_periods SET payroll_type=?, is_active=?, is_default=?, notes=?, updated_at=NOW() WHERE id=? AND company_id=?"); $stmt->execute([ $payroll_type, $is_active, $is_default, ($notes!=='' ? $notes : null), $id, $company_id ]); $pdo->commit(); $flash = "Row #{$id} updated."; } } catch (Throwable $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $err = $e->getMessage(); } } /* -------- Filters & data load -------- */ $q = trim($_GET['q'] ?? ''); $params = [$company_id]; $where = "WHERE company_id=?"; if ($q!=='') { $where .= " AND (payroll_type LIKE ? OR notes LIKE ?)"; $params[] = "%{$q}%"; $params[] = "%{$q}%"; } $sql = "SELECT id, company_id, payroll_type, is_active, is_default, notes, created_by, created_at, updated_at FROM company_payroll_periods {$where} ORDER BY is_default DESC, payroll_type ASC, id DESC"; $st = $pdo->prepare($sql); $st->execute($params); $rows = $st->fetchAll(PDO::FETCH_ASSOC); ?><!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Payroll Periods</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div class="container-fluid py-4"> <div class="mb-4"> <h4 class="fw-bold">Payroll Periods</h4> </div> <?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; ?> <!-- FILTER --> <div class="card shadow-sm border-0 mb-4"> <div class="card-body"> <form method="get" class="row g-3 align-items-end"> <div class="col-md-6"> <label class="form-label">Search (Type / Notes)</label> <input type="text" name="q" value="<?= h($q) ?>" class="form-control" placeholder="e.g., Monthly"> </div> <div class="col-md-2"> <button class="btn btn-primary w-100">Filter</button> </div> </form> </div> </div> <!-- ADD NEW --> <div class="card shadow-sm border-0 mb-4"> <div class="card-body"> <h5 class="fw-bold mb-3">Add Payroll Period</h5> <form method="post" class="row g-3 align-items-end" autocomplete="off"> <input type="hidden" name="action" value="add"> <div class="col-md-4"> <label class="form-label">Payroll Type</label> <input name="payroll_type" class="form-control" required placeholder="e.g., Monthly"> </div> <div class="col-md-2"> <label class="form-label">Active</label> <select name="is_active" class="form-select"> <option value="1" selected>Yes</option> <option value="0">No</option> </select> </div> <div class="col-md-2"> <label class="form-label">Default</label> <select name="is_default" class="form-select"> <option value="0" selected>No</option> <option value="1">Yes</option> </select> </div> <div class="col-md-3"> <label class="form-label">Notes</label> <input name="notes" class="form-control" placeholder="Optional"> </div> <div class="col-md-1"> <button class="btn btn-primary w-100">Add</button> </div> </form> </div> </div> <!-- TABLE --> <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>Payroll Type</th> <th>Active</th> <th>Default</th> <th>Notes</th> <th>Created</th> <th>Save</th> </tr> </thead> <tbody> <?php foreach($rows as $r): ?> <tr> <form method="post"> <input type="hidden" name="action" value="update"> <input type="hidden" name="id" value="<?= (int)$r['id'] ?>"> <td><?= (int)$r['id'] ?></td> <td> <input name="payroll_type" value="<?= h($r['payroll_type']) ?>" class="form-control" required> </td> <td> <select name="is_active" class="form-select"> <option value="1" <?= ($r['is_active']?'selected':'') ?>>Yes</option> <option value="0" <?= (!$r['is_active']?'selected':'') ?>>No</option> </select> </td> <td> <select name="is_default" class="form-select"> <option value="0" <?= (!$r['is_default']?'selected':'') ?>>No</option> <option value="1" <?= ($r['is_default']?'selected':'') ?>>Yes</option> </select> </td> <td> <input name="notes" value="<?= h($r['notes']) ?>" class="form-control"> </td> <td><?= h($r['created_at']) ?></td> <td> <button class="btn btn-sm btn-primary">Save</button> </td> </form> </tr> <?php endforeach; ?> <?php if(empty($rows)): ?> <tr> <td colspan="7" class="text-center text-muted"> No rows for your company. </td> </tr> <?php endif; ?> </tbody> </table> </div> </div> </div> </div> <?php /* ---------- Page footer (global include) ---------- */ require_once __DIR__ . '/partials/footer.php'; ?> </body> </html>