« Back to History
pasaria_types.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ========================================================================= File: /erp/pasaria_types.php Purpose: Manage Pasaria Types (Add/Edit/View) for current company Scope : Page-only (no global edits) ========================================================================= */ error_reporting(E_ALL); ini_set('display_errors', 1); /* --- DB + Auth (page-local) --- */ if (!isset($pdo) || !($pdo instanceof PDO)) { require __DIR__ . '/core/db.php'; } require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $user_id = (int)$u['id']; /* --- Helpers --- */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function idx_exists(PDO $pdo,string $t,string $idx):bool{ try{ $db=$pdo->query("SELECT DATABASE()")->fetchColumn(); $st=$pdo->prepare("SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND INDEX_NAME=? LIMIT 1"); $st->execute([$db,$t,$idx]); return (bool)$st->fetchColumn(); }catch(Throwable $e){ return false; } } /* --- Ensure table (matches your screenshot columns) --- */ $pdo->exec("CREATE TABLE IF NOT EXISTS pasaria_types ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, company_id BIGINT UNSIGNED NOT NULL, pasaria_type VARCHAR(120) NOT NULL, description VARCHAR(255) NULL, is_active TINYINT(1) NOT NULL DEFAULT 1, sort_order INT NOT NULL DEFAULT 0, created_by BIGINT UNSIGNED NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY idx_company_active (company_id, is_active), KEY idx_company_sort (company_id, sort_order) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); if (!idx_exists($pdo,'pasaria_types','uniq_company_type')) { // Unique separately to keep old MySQL happy if table existed try{ $pdo->exec("CREATE UNIQUE INDEX uniq_company_type ON pasaria_types(company_id, pasaria_type)"); }catch(Throwable $e){} } /* --- Actions --- */ $msg=''; $err=''; $act = $_POST['_action'] ?? ''; if ($_SERVER['REQUEST_METHOD']==='POST' && $act==='add') { $type = trim($_POST['pasaria_type'] ?? ''); $desc = trim($_POST['description'] ?? ''); $active = isset($_POST['is_active']) ? 1 : 1; // default active if ($type==='') $err='Type is required.'; if (!$err) { try{ // default sort: max+10 $max = (int)$pdo->prepare("SELECT COALESCE(MAX(sort_order),0) FROM pasaria_types WHERE company_id=?") ->execute([$company_id]) ?: 0; $get = $pdo->prepare("SELECT COALESCE(MAX(sort_order),0) FROM pasaria_types WHERE company_id=?"); $get->execute([$company_id]); $max = (int)$get->fetchColumn(); $sort = $max + 10; $st=$pdo->prepare("INSERT INTO pasaria_types (company_id,pasaria_type,description,is_active,sort_order,created_by) VALUES (?,?,?,?,?,?)"); $st->execute([$company_id,$type,($desc!==''?$desc:null),$active,$sort,$user_id]); $msg='Added.'; }catch(Throwable $e){ if (strpos($e->getMessage(),'uniq_company_type')!==false) $err='This type already exists.'; else $err='Add failed: '.$e->getMessage(); } } } if ($_SERVER['REQUEST_METHOD']==='POST' && $act==='update') { $id = (int)($_POST['id'] ?? 0); $type = trim($_POST['pasaria_type'] ?? ''); $desc = trim($_POST['description'] ?? ''); $active = isset($_POST['is_active']) ? 1 : 0; $sort = (int)($_POST['sort_order'] ?? 0); if ($id<=0) $err='Invalid ID.'; elseif ($type==='') $err='Type is required.'; else { try{ $st=$pdo->prepare("UPDATE pasaria_types SET pasaria_type=?, description=?, is_active=?, sort_order=? WHERE id=? AND company_id=?"); $st->execute([$type, ($desc!==''?$desc:null), $active, $sort, $id, $company_id]); if ($st->rowCount()===0) $msg=$msg ?: 'No change.'; else $msg='Updated.'; }catch(Throwable $e){ if (strpos($e->getMessage(),'uniq_company_type')!==false) $err='Another row with this type already exists.'; else $err='Update failed: '.$e->getMessage(); } } } if ($_SERVER['REQUEST_METHOD']==='POST' && $act==='toggle') { $id = (int)($_POST['id'] ?? 0); $to = (int)($_POST['to'] ?? 1); if ($id>0){ $st=$pdo->prepare("UPDATE pasaria_types SET is_active=? WHERE id=? AND company_id=?"); $st->execute([$to, $id, $company_id]); if ($st->rowCount()>0) $msg = $to? 'Activated.' : 'Deactivated.'; } } if ($_SERVER['REQUEST_METHOD']==='POST' && $act==='delete') { $id = (int)($_POST['id'] ?? 0); if ($id>0){ try{ $st=$pdo->prepare("DELETE FROM pasaria_types WHERE id=? AND company_id=?"); $st->execute([$id,$company_id]); if ($st->rowCount()>0) $msg='Deleted.'; else $err='Not found.'; }catch(Throwable $e){ $err='Delete failed: '.$e->getMessage(); } } } /* --- Fetch list --- */ $q = trim($_GET['q'] ?? ''); $rows=[]; try{ $sql="SELECT id,pasaria_type,description,is_active,sort_order,created_at,updated_at FROM pasaria_types WHERE company_id=?"; $params=[$company_id]; if ($q!=='') { $sql .= " AND (pasaria_type LIKE ? OR description LIKE ?)"; $params[]="%$q%"; $params[]="%$q%"; } $sql .= " ORDER BY sort_order ASC, pasaria_type ASC, id DESC"; $st=$pdo->prepare($sql); $st->execute($params); $rows=$st->fetchAll(PDO::FETCH_ASSOC); }catch(Throwable $e){ $err = $err ?: ('Load failed: '.$e->getMessage()); } /* --- Optional header include --- */ $__header = __DIR__ . '/partials/header.php'; if (is_file($__header)) { $PAGE='pasaria_types'; include_once $__header; } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"> <title>Pasaria Types</title> <style> :root{ --primary:#34A853; --bg:#F5FFF7; --text:#202124; --muted:#5F6368; --card:#fff; --border:#e5e7eb; --shadow:0 8px 20px rgba(0,0,0,.06); } *{box-sizing:border-box} body{margin:0;background:var(--bg);color:var(--text);font:14px system-ui,-apple-system,Segoe UI,Roboto,Arial} .wrap{max-width:1000px;margin:18px auto;padding:12px} h1{margin:0 0 10px;font-size:20px} .card{background:var(--card);border:1px solid var(--border);border-radius:14px;box-shadow:var(--shadow);padding:16px} .nav{display:flex;gap:8px;flex-wrap:wrap;margin-bottom:12px} .chip{display:inline-flex;align-items:center;gap:8px;padding:6px 10px;border:1px solid var(--border);border-radius:999px;background:#fff} .chip a{text-decoration:none;color:var(--text)} a.btn{display:inline-block;padding:8px 12px;border-radius:10px;background:var(--primary);color:#fff;text-decoration:none;font-weight:700} .btn-ghost{background:#f1f3f4;color:#111} form.inline{display:flex;gap:8px;flex-wrap:wrap} input,select{padding:8px 10px;border:1px solid #d0d7de;border-radius:10px;background:#fff} table{width:100%;border-collapse:collapse;margin-top:12px;background:#fff;border-radius:12px;overflow:hidden} th,td{padding:10px;border-bottom:1px solid #eef2f7;text-align:left;font-size:13px} th{background:#f8fafc;color:#334155} .actions{display:flex;gap:6px} .msg{margin:10px 0;padding:10px;border-radius:10px} .ok{background:#e6f4ea;border:1px solid #cde7d7;color:#146c2e} .err{background:#fdecea;border:1px solid #f5c6cb;color:#7f1d1d} .small{color:#64748b;font-size:12px} @media(max-width:720px){ .row{display:block} } </style> </head> <body> <div class="wrap"> <div class="nav"> <div class="chip"><a href="/erp/pasaria_entry.php">← Go to Pasaria Entry</a></div> <div class="chip"><a href="/erp/pasaria_rates.php">Pasaria Rates</a></div> <div class="chip"><a href="/erp/pasaria_types.php"><b>Pasaria Types</b></a></div> </div> <div class="card"> <h1>Pasaria Types</h1> <?php if($msg): ?><div class="msg ok"><?=h($msg)?></div><?php endif; ?> <?php if($err): ?><div class="msg err"><?=h($err)?></div><?php endif; ?> <!-- Add form --> <form method="post" class="inline" autocomplete="off"> <input type="hidden" name="_action" value="add"> <input type="text" name="pasaria_type" placeholder="Type (e.g., With Jog)" required> <input type="text" name="description" placeholder="Description (optional)"> <label class="small" style="display:inline-flex;align-items:center;gap:6px"> <input type="checkbox" name="is_active" checked> Active </label> <button class="btn btn-ghost" type="submit" style="border:0;background:#34A853;color:#fff;border-radius:10px;padding:8px 14px;font-weight:700">Add</button> <span class="small">Unique per company</span> </form> <!-- Search --> <form method="get" class="inline" style="margin-top:10px"> <input type="text" name="q" value="<?= h($q) ?>" placeholder="Search type or description…"> <button class="btn-ghost" style="padding:8px 12px;border-radius:10px;border:1px solid #d0d7de" type="submit">Search</button> <?php if($q!==''): ?><a class="btn-ghost" style="padding:8px 12px;border-radius:10px;border:1px solid #d0d7de" href="/erp/pasaria_types.php">Reset</a><?php endif; ?> </form> <!-- List --> <table> <thead> <tr> <th style="width:60px">ID</th> <th>Pasaria Type</th> <th>Description</th> <th style="width:100px">Sort</th> <th style="width:110px">Active</th> <th style="width:220px">Actions</th> </tr> </thead> <tbody> <?php if(!$rows): ?> <tr><td colspan="6" class="small" style="text-align:center">No types yet. Add one above.</td></tr> <?php else: foreach($rows as $r): ?> <tr> <td><?= (int)$r['id'] ?></td> <td> <form method="post" class="inline" style="gap:6px"> <input type="hidden" name="_action" value="update"> <input type="hidden" name="id" value="<?= (int)$r['id'] ?>"> <input type="text" name="pasaria_type" value="<?= h($r['pasaria_type']) ?>" required> </td> <td> <input type="text" name="description" value="<?= h($r['description']) ?>"> </td> <td> <input type="number" name="sort_order" step="1" value="<?= (int)$r['sort_order'] ?>"> </td> <td> <label class="small" style="display:inline-flex;align-items:center;gap:6px"> <input type="checkbox" name="is_active" <?= ((int)$r['is_active']===1?'checked':'') ?>> Active </label> </td> <td class="actions"> <button class="btn-ghost" style="padding:8px 12px;border:1px solid #d0d7de;border-radius:10px" type="submit">Save</button> </form> <form method="post" style="display:inline" onsubmit="return confirm('Delete this type?')"> <input type="hidden" name="_action" value="delete"> <input type="hidden" name="id" value="<?= (int)$r['id'] ?>"> <button class="btn-ghost" style="padding:8px 12px;border:1px solid #d0d7de;border-radius:10px" type="submit">Delete</button> </form> <form method="post" style="display:inline"> <input type="hidden" name="_action" value="toggle"> <input type="hidden" name="id" value="<?= (int)$r['id'] ?>"> <input type="hidden" name="to" value="<?= (int)$r['is_active']?0:1 ?>"> <button class="btn-ghost" style="padding:8px 12px;border:1px solid #d0d7de;border-radius:10px" type="submit"> <?= (int)$r['is_active']? 'Deactivate' : 'Activate' ?> </button> </form> </td> </tr> <?php endforeach; endif; ?> </tbody> </table> <div class="small" style="margin-top:8px">Tip: Sorting left field se change karke “Save” dabao.</div> </div> </div> </body> </html>