|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/emp.outerorbittech.in/admin/ | |
|
Path: /home2/outerorb/emp.outerorbittech.in/admin/assets.php
Size: 24.87 KB
Permissions: 0666
<?php
require __DIR__ . '/../includes/helpers.php';
require_admin();
ensure_asset_tables();
ensure_employees_table();
$pdo = db();
$flash = flash();
$admin = current_admin();
$errors = [];
$tab = $_GET['tab'] ?? 'assets'; // assets | assign | employee
// ---- POST ----
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!verify_csrf($_POST['csrf_token'] ?? '')) {
redirect_with_message('assets.php', 'Session expired.', 'error');
}
$action = $_POST['action'] ?? '';
// Add/edit asset
if ($action === 'save_asset') {
$id = (int)($_POST['id'] ?? 0);
$tag = sanitize_text($_POST['asset_tag'] ?? '');
$name = sanitize_text($_POST['asset_name'] ?? '');
$category = sanitize_text($_POST['category'] ?? '');
$brand = sanitize_text($_POST['brand'] ?? '');
$model = sanitize_text($_POST['model'] ?? '');
$serial = sanitize_text($_POST['serial_number'] ?? '');
$pDate = sanitize_text($_POST['purchase_date'] ?? '') ?: null;
$pVal = (float)str_replace(',','',$_POST['purchase_value'] ?? '0');
$status = in_array($_POST['status'] ?? '', ['available','assigned','under_repair','retired']) ? $_POST['status'] : 'available';
$notes = sanitize_text($_POST['notes'] ?? '');
if (!$tag) $errors[] = 'Asset tag is required.';
if (!$name) $errors[] = 'Asset name is required.';
if (empty($errors)) {
if ($id) {
$pdo->prepare(
'UPDATE assets SET asset_tag=?,name=?,category=?,brand=?,model=?,serial_no=?,purchase_date=?,purchase_value=?,status=?,notes=?,updated_at=NOW() WHERE id=?'
)->execute([$tag, $name, $category, $brand, $model, $serial, $pDate, $pVal ?: null, $status, $notes ?: null, $id]);
} else {
$pdo->prepare(
'INSERT INTO assets (asset_tag,name,category,brand,model,serial_no,purchase_date,purchase_value,status,notes) VALUES (?,?,?,?,?,?,?,?,?,?)'
)->execute([$tag, $name, $category, $brand, $model, $serial, $pDate, $pVal ?: null, $status, $notes ?: null]);
}
redirect_with_message('assets.php?tab=assets', $id ? 'Asset updated.' : 'Asset added.');
}
}
// Assign asset to employee
if ($action === 'assign_asset') {
$assetId = (int)($_POST['asset_id'] ?? 0);
$empId = (int)($_POST['employee_id'] ?? 0);
$aDate = sanitize_text($_POST['assigned_date'] ?? date('Y-m-d'));
$condOut = sanitize_text($_POST['condition_out'] ?? 'good');
$notes = sanitize_text($_POST['notes'] ?? '');
if (!$assetId || !$empId) { $errors[] = 'Asset and employee are required.'; }
if (empty($errors)) {
$pdo->prepare(
'INSERT INTO asset_assignments (asset_id,employee_id,assigned_date,condition_out,notes) VALUES (?,?,?,?,?)'
)->execute([$assetId, $empId, $aDate, $condOut, $notes ?: null]);
$pdo->prepare('UPDATE assets SET status=\'assigned\' WHERE id=?')->execute([$assetId]);
redirect_with_message('assets.php?tab=assign', 'Asset assigned.');
}
}
// Return asset
if ($action === 'return_asset') {
$assignId = (int)($_POST['assignment_id'] ?? 0);
$assetId = (int)($_POST['asset_id'] ?? 0);
$returnDate = sanitize_text($_POST['return_date'] ?? date('Y-m-d'));
$condIn = sanitize_text($_POST['condition_in'] ?? 'good');
if ($assignId) {
$pdo->prepare('UPDATE asset_assignments SET returned_date=?,condition_in=? WHERE id=?')->execute([$returnDate, $condIn, $assignId]);
$pdo->prepare('UPDATE assets SET status=\'available\' WHERE id=?')->execute([$assetId]);
}
redirect_with_message('assets.php?tab=assign', 'Asset returned.');
}
// Delete asset
if ($action === 'delete_asset') {
$id = (int)($_POST['id'] ?? 0);
if ($id) { $pdo->prepare('UPDATE assets SET status=\'retired\' WHERE id=?')->execute([$id]); }
redirect_with_message('assets.php?tab=assets', 'Asset retired.');
}
}
// ---- Load ----
$assets = $pdo->query('SELECT *, name AS asset_name, serial_no AS serial_number FROM assets ORDER BY name')->fetchAll();
$availAssets = array_filter($assets, fn($a) => $a['status'] === 'available');
$where = ['e.deleted_at IS NULL', "e.employee_status='active'"];
$params = [];
apply_department_filter($where, $params);
$empStmt = $pdo->prepare('SELECT id, first_name, last_name, department FROM employees e WHERE '.implode(' AND ',$where).' ORDER BY first_name');
$empStmt->execute($params);
$employees = $empStmt->fetchAll();
// Current assignments (not returned)
$assignments = $pdo->query(
'SELECT aa.*, a.asset_tag, a.name AS asset_name, a.category, e.first_name, e.last_name, e.department
FROM asset_assignments aa
JOIN assets a ON a.id = aa.asset_id
JOIN employees e ON e.id = aa.employee_id
WHERE aa.returned_date IS NULL
ORDER BY aa.assigned_date DESC'
)->fetchAll();
// Employee filter for asset view
$filterEmp = isset($_GET['emp']) ? (int)$_GET['emp'] : 0;
$empAssets = [];
if ($filterEmp) {
$stmt = $pdo->prepare(
'SELECT aa.*, a.asset_tag, a.name AS asset_name, a.category, a.brand, a.model
FROM asset_assignments aa
JOIN assets a ON a.id = aa.asset_id
WHERE aa.employee_id = ?
ORDER BY aa.assigned_date DESC'
);
$stmt->execute([$filterEmp]);
$empAssets = $stmt->fetchAll();
}
$editAsset = null;
if (isset($_GET['edit'])) {
$ea = $pdo->prepare('SELECT *, name AS asset_name, serial_no AS serial_number FROM assets WHERE id=? LIMIT 1');
$ea->execute([(int)$_GET['edit']]);
$editAsset = $ea->fetch() ?: null;
if ($editAsset) $tab = 'assets';
}
function e(string $v): string { return htmlspecialchars($v, ENT_QUOTES, 'UTF-8'); }
$statusColors = ['available'=>'#d1fae5;color:#065f46','assigned'=>'#dbeafe;color:#1e40af','under_repair'=>'#fef3c7;color:#92400e','retired'=>'#f3f4f6;color:#374151'];
$conditions = ['good'=>'Good','fair'=>'Fair','poor'=>'Poor','damaged'=>'Damaged'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Asset Management — HRMS</title>
<link rel="stylesheet" href="../assets/css/style.css?v=<?php echo filemtime(__DIR__.'/../assets/css/style.css'); ?>" />
<link rel="stylesheet" href="../assets/css/polish.css">
<style>
.tabs { display:flex; gap:4px; margin-bottom:16px; }
.tab-link { padding:7px 18px; border-radius:6px; font-size:.85rem; cursor:pointer; text-decoration:none; color:#374151; background:#f3f4f6; }
.tab-link.active { background:#2563eb; color:#fff; font-weight:700; }
.status-badge { display:inline-block; padding:2px 10px; border-radius:999px; font-size:.72rem; font-weight:700; }
.table-std { width:100%; border-collapse:collapse; font-size:.82rem; }
.table-std th, .table-std td { padding:9px 12px; border-bottom:1px solid #f3f4f6; }
.table-std th { background:#f9fafb; border-bottom-color:#e5e7eb; text-align:left; font-weight:700; color:#374151; }
</style>
</head>
<body class="dashboard-layout">
<div class="app-wrapper">
<?php require '_sidebar.php'; ?>
<div class="main-content">
<div class="top-bar">
<div class="top-bar-left">
<button class="sidebar-toggle" id="sidebarToggle" aria-label="Toggle sidebar">โฐ</button>
<div>
<h1 class="page-title">Asset Management</h1>
<div class="breadcrumb-nav"><span>Track company assets and assignments.</span></div>
</div>
</div>
<div class="top-bar-right">
<a href="dashboard.php" class="badge">Dashboard</a>
<a href="logout.php" class="badge">Logout</a>
</div>
</div>
<div class="dashboard-container">
<?php if ($flash): ?>
<div class="alert <?php echo $flash['type']==='error' ? 'alert-error' : 'alert-success'; ?>">
<?php echo e($flash['message']); ?>
</div>
<?php endif; ?>
<?php foreach ($errors as $err): ?><div class="alert alert-error"><?php echo e($err); ?></div><?php endforeach; ?>
<div class="tabs">
<a href="assets.php?tab=assets" class="tab-link <?php echo $tab==='assets' ? 'active' : ''; ?>">Asset Register (<?php echo count($assets); ?>)</a>
<a href="assets.php?tab=assign" class="tab-link <?php echo $tab==='assign' ? 'active' : ''; ?>">Assignments (<?php echo count($assignments); ?>)</a>
<a href="assets.php?tab=employee" class="tab-link <?php echo $tab==='employee' ? 'active' : ''; ?>">By Employee</a>
</div>
<?php /* ========================= ASSETS TAB ========================= */ ?>
<?php if ($tab === 'assets'): ?>
<div class="card" style="margin-bottom:14px;">
<h4 style="margin:0 0 14px; font-size:.85rem; text-transform:uppercase; color:#6b7280;">
<?php echo $editAsset ? 'Edit Asset' : 'Add Asset'; ?>
</h4>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
<input type="hidden" name="action" value="save_asset" />
<?php if ($editAsset): ?><input type="hidden" name="id" value="<?php echo (int)$editAsset['id']; ?>" /><?php endif; ?>
<div style="display:grid; grid-template-columns:repeat(4,1fr); gap:12px;">
<div class="form-group" style="margin:0;">
<label>Asset Tag *</label>
<input type="text" name="asset_tag" class="form-control" required value="<?php echo e($editAsset ? $editAsset['asset_tag'] : ''); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Asset Name *</label>
<input type="text" name="asset_name" class="form-control" required value="<?php echo e($editAsset ? $editAsset['asset_name'] : ''); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Category</label>
<input type="text" name="category" class="form-control" value="<?php echo e($editAsset ? $editAsset['category'] : ''); ?>" list="cat-list" />
<datalist id="cat-list"><option>Laptop</option><option>Desktop</option><option>Monitor</option><option>Phone</option><option>Furniture</option><option>Vehicle</option></datalist>
</div>
<div class="form-group" style="margin:0;">
<label>Brand</label>
<input type="text" name="brand" class="form-control" value="<?php echo e($editAsset ? $editAsset['brand'] : ''); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Model</label>
<input type="text" name="model" class="form-control" value="<?php echo e($editAsset ? $editAsset['model'] : ''); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Serial Number</label>
<input type="text" name="serial_number" class="form-control" value="<?php echo e($editAsset ? $editAsset['serial_number'] : ''); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Purchase Date</label>
<input type="date" name="purchase_date" class="form-control" value="<?php echo e($editAsset ? $editAsset['purchase_date'] : ''); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Purchase Value (รขโยน)</label>
<input type="number" name="purchase_value" class="form-control" min="0" step="0.01" value="<?php echo $editAsset ? number_format((float)$editAsset['purchase_value'], 2, '.', '') : ''; ?>" />
</div>
<?php if ($editAsset): ?>
<div class="form-group" style="margin:0;">
<label>Status</label>
<select name="status" class="form-control">
<?php foreach (['available','under_repair','retired'] as $s): ?>
<option value="<?php echo $s; ?>" <?php echo $editAsset['status']===$s?'selected':''; ?>><?php echo ucfirst(str_replace('_',' ',$s)); ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
<div class="form-group" style="margin:0; grid-column:span 2;">
<label>Notes</label>
<input type="text" name="notes" class="form-control" value="<?php echo e($editAsset ? ($editAsset['notes'] ?? '') : ''); ?>" />
</div>
</div>
<button type="submit" class="btn btn-primary" style="margin-top:12px;"><?php echo $editAsset ? 'Update Asset' : 'Add Asset'; ?></button>
<?php if ($editAsset): ?><a href="assets.php?tab=assets" style="margin-left:10px; font-size:.85rem; color:#6b7280;">Cancel</a><?php endif; ?>
</form>
</div>
<div class="card" style="overflow-x:auto;">
<table class="table-std">
<thead><tr>
<th>Tag</th><th>Name</th><th>Category</th><th>Brand / Model</th><th>Serial</th><th>Purchase</th><th>Status</th><th></th>
</tr></thead>
<tbody>
<?php if (empty($assets)): ?>
<tr><td colspan="8" style="text-align:center; padding:30px; color:#9ca3af;">No assets added yet.</td></tr>
<?php else: ?>
<?php foreach ($assets as $a): ?>
<tr>
<td style="font-weight:700;"><?php echo e($a['asset_tag']); ?></td>
<td><?php echo e($a['asset_name']); ?></td>
<td style="color:#6b7280;"><?php echo e($a['category']); ?></td>
<td><?php echo e(trim($a['brand'].' '.$a['model'])); ?></td>
<td style="color:#6b7280;"><?php echo e($a['serial_number']); ?></td>
<td style="color:#6b7280;"><?php echo $a['purchase_date'] ? date('d M Y', strtotime($a['purchase_date'])) : 'โ'; ?></td>
<td><span class="status-badge" style="background:<?php echo $statusColors[$a['status']] ?? '#f3f4f6;color:#374151'; ?>;"><?php echo ucfirst(str_replace('_',' ',$a['status'])); ?></span></td>
<td>
<a href="assets.php?edit=<?php echo (int)$a['id']; ?>" style="font-size:.78rem; color:#2563eb;">Edit</a>
<?php if ($a['status'] !== 'assigned'): ?>
<form method="POST" style="display:inline;" onsubmit="return confirm('Retire this asset?');">
<input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
<input type="hidden" name="action" value="delete_asset" />
<input type="hidden" name="id" value="<?php echo (int)$a['id']; ?>" />
<button type="submit" style="background:none; border:none; color:#ef4444; cursor:pointer; font-size:.78rem;">Retire</button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
</div>
<?php /* ========================= ASSIGN TAB ========================= */ ?>
<?php elseif ($tab === 'assign'): ?>
<div class="card" style="margin-bottom:14px;">
<h4 style="margin:0 0 14px; font-size:.85rem; text-transform:uppercase; color:#6b7280;">Assign Asset to Employee</h4>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
<input type="hidden" name="action" value="assign_asset" />
<div style="display:grid; grid-template-columns:repeat(4,1fr); gap:12px;">
<div class="form-group" style="margin:0;">
<label>Asset *</label>
<select name="asset_id" class="form-control" required>
<option value="">Select asset</option>
<?php foreach ($availAssets as $a): ?>
<option value="<?php echo (int)$a['id']; ?>"><?php echo e($a['asset_tag']); ?> โ <?php echo e($a['asset_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group" style="margin:0;">
<label>Employee *</label>
<select name="employee_id" class="form-control" required>
<option value="">Select employee</option>
<?php foreach ($employees as $emp): ?>
<option value="<?php echo (int)$emp['id']; ?>"><?php echo e($emp['first_name'].' '.$emp['last_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group" style="margin:0;">
<label>Assign Date</label>
<input type="date" name="assigned_date" class="form-control" value="<?php echo date('Y-m-d'); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Condition Out</label>
<select name="condition_out" class="form-control">
<?php foreach ($conditions as $k=>$v): ?><option value="<?php echo $k; ?>"><?php echo $v; ?></option><?php endforeach; ?>
</select>
</div>
<div class="form-group" style="margin:0; grid-column:span 2;">
<label>Notes</label>
<input type="text" name="notes" class="form-control" />
</div>
</div>
<button type="submit" class="btn btn-primary" style="margin-top:12px;">Assign</button>
</form>
</div>
<div class="card" style="overflow-x:auto;">
<h4 style="margin:0 0 12px; font-size:.85rem; text-transform:uppercase; color:#6b7280;">Current Assignments</h4>
<table class="table-std">
<thead><tr><th>Asset</th><th>Employee</th><th>Dept</th><th>Assigned</th><th>Condition Out</th><th>Return</th></tr></thead>
<tbody>
<?php if (empty($assignments)): ?>
<tr><td colspan="6" style="text-align:center; padding:30px; color:#9ca3af;">No active assignments.</td></tr>
<?php else: ?>
<?php foreach ($assignments as $as): ?>
<tr>
<td><strong><?php echo e($as['asset_tag']); ?></strong><br/><span style="font-size:.78rem; color:#6b7280;"><?php echo e($as['asset_name']); ?></span></td>
<td><?php echo e($as['first_name'].' '.$as['last_name']); ?></td>
<td style="color:#6b7280;"><?php echo e($as['department']); ?></td>
<td><?php echo date('d M Y', strtotime($as['assigned_date'])); ?></td>
<td><?php echo $conditions[$as['condition_out']] ?? e($as['condition_out']); ?></td>
<td>
<button type="button" onclick="openReturnModal(<?php echo htmlspecialchars(json_encode($as), ENT_QUOTES); ?>)"
style="background:none; border:none; color:#2563eb; cursor:pointer; font-size:.78rem;">Return</button>
</td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
</div>
<?php /* ========================= EMPLOYEE TAB ========================= */ ?>
<?php elseif ($tab === 'employee'): ?>
<div class="card" style="margin-bottom:14px;">
<form method="GET" style="display:flex; gap:12px; align-items:flex-end;">
<input type="hidden" name="tab" value="employee" />
<div class="form-group" style="margin:0;">
<label>Select Employee</label>
<select name="emp" class="form-control" onchange="this.form.submit()">
<option value="">-- Select --</option>
<?php foreach ($employees as $emp): ?>
<option value="<?php echo (int)$emp['id']; ?>" <?php echo $filterEmp===(int)$emp['id'] ? 'selected' : ''; ?>>
<?php echo e($emp['first_name'].' '.$emp['last_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</form>
</div>
<?php if ($filterEmp): ?>
<div class="card" style="overflow-x:auto;">
<table class="table-std">
<thead><tr><th>Asset</th><th>Category</th><th>Serial</th><th>Assigned</th><th>Condition Out</th><th>Returned</th><th>Condition In</th></tr></thead>
<tbody>
<?php if (empty($empAssets)): ?>
<tr><td colspan="7" style="text-align:center; padding:30px; color:#9ca3af;">No assets assigned to this employee.</td></tr>
<?php else: ?>
<?php foreach ($empAssets as $ea): ?>
<tr>
<td><strong><?php echo e($ea['asset_tag']); ?></strong><br/><span style="font-size:.78rem; color:#6b7280;"><?php echo e($ea['asset_name']); ?></span></td>
<td style="color:#6b7280;"><?php echo e($ea['category']); ?></td>
<td style="color:#6b7280;"><?php echo e($ea['serial_number'] ?? ''); ?></td>
<td><?php echo date('d M Y', strtotime($ea['assigned_date'])); ?></td>
<td><?php echo $conditions[$ea['condition_out']] ?? e($ea['condition_out']); ?></td>
<td><?php echo $ea['returned_date'] ? date('d M Y', strtotime($ea['returned_date'])) : '<span style="color:#d97706;">Active</span>'; ?></td>
<td><?php echo $ea['condition_in'] ? ($conditions[$ea['condition_in']] ?? e($ea['condition_in'])) : 'โ'; ?></td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<!-- Return modal -->
<div id="return-modal" style="display:none; position:fixed; inset:0; background:rgba(0,0,0,.4); z-index:999; align-items:center; justify-content:center;">
<div style="background:#fff; border-radius:12px; padding:28px; width:380px; max-width:95vw;">
<h3 id="return-modal-title" style="margin:0 0 16px; font-size:1rem;"></h3>
<form method="POST" action="assets.php?tab=assign">
<input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
<input type="hidden" name="action" value="return_asset" />
<input type="hidden" name="assignment_id" id="return-assign-id" />
<input type="hidden" name="asset_id" id="return-asset-id" />
<div style="display:grid; grid-template-columns:1fr 1fr; gap:12px;">
<div class="form-group">
<label>Return Date</label>
<input type="date" name="return_date" id="return-date" class="form-control" value="<?php echo date('Y-m-d'); ?>" />
</div>
<div class="form-group">
<label>Condition In</label>
<select name="condition_in" class="form-control">
<?php foreach ($conditions as $k=>$v): ?><option value="<?php echo $k; ?>"><?php echo $v; ?></option><?php endforeach; ?>
</select>
</div>
</div>
<div style="display:flex; gap:10px; margin-top:4px;">
<button type="submit" class="btn btn-primary" style="flex:1;">Confirm Return</button>
<button type="button" onclick="document.getElementById('return-modal').style.display='none'"
style="flex:1; background:#f3f4f6; border:1px solid #e5e7eb; border-radius:6px; cursor:pointer;">Cancel</button>
</div>
</form>
</div>
</div>
<script>
function openReturnModal(as_) {
document.getElementById('return-modal-title').textContent = as_.asset_tag + ' โ ' + as_.asset_name + ' (returned by ' + as_.first_name + ')';
document.getElementById('return-assign-id').value = as_.id;
document.getElementById('return-asset-id').value = as_.asset_id;
document.getElementById('return-modal').style.display = 'flex';
}
document.getElementById('return-modal').addEventListener('click', function(e) {
if (e.target === this) this.style.display = 'none';
});
</script>
</div>
</div>
</div>
</body>
</html>