« Back to History
asset_assign.php
|
20260921_175631.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================= File: /erp/asset_assign.php Purpose: Assign an Available Asset to an Employee ============================================================================= */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('asset_master'); if (!function_exists('h')) { function h($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } } $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $fy = $ctx['fy'] ?? (date('m') >= 4 ? date('Y') . '-' . (date('Y') + 1) : (date('Y') - 1) . '-' . date('Y')); // User ID resolution $user_id = 0; if (isset($ctx['user']['id']) && (int)$ctx['user']['id'] > 0) { $user_id = (int)$ctx['user']['id']; } elseif (isset($_SESSION['user']['id']) && (int)$_SESSION['user']['id'] > 0) { $user_id = (int)$_SESSION['user']['id']; } $pre_asset_id = (int)($_GET['asset_id'] ?? 0); $error_msg = ''; $success_msg = ''; /* ===================== HANDLE FORM SUBMISSION ===================== */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $asset_id = (int)($_POST['asset_id'] ?? 0); $employee_id = (int)($_POST['employee_id'] ?? 0); $department_id = (int)($_POST['department_id'] ?? 0); $issue_date = trim($_POST['issue_date'] ?? ''); $issue_condition = trim($_POST['issue_condition'] ?? 'GOOD'); $issue_remark = trim($_POST['issue_remark'] ?? ''); if ($asset_id <= 0) { $error_msg = 'Please select a valid asset.'; } elseif ($employee_id <= 0) { $error_msg = 'Please select an employee.'; } elseif ($issue_date === '') { $error_msg = 'Issue date is required.'; } else { try { $pdo->beginTransaction(); // 1. Verify asset is currently AVAILABLE $chk = $pdo->prepare("SELECT asset_status FROM asset_master WHERE id = ? AND company_id = ? FOR UPDATE"); $chk->execute([$asset_id, $company_id]); $asset_row = $chk->fetch(PDO::FETCH_ASSOC); if (!$asset_row) { throw new Exception('Asset not found.'); } if ($asset_row['asset_status'] !== 'AVAILABLE') { throw new Exception('This asset is currently not available for assignment (Status: ' . $asset_row['asset_status'] . ').'); } // 2. Insert into asset_assignment_history $ins = $pdo->prepare(" INSERT INTO asset_assignment_history ( company_id, fy, asset_id, employee_id, department_id, issue_date, issue_condition, assignment_status, issue_remark, issued_by, created_at ) VALUES (?, ?, ?, ?, ?, ?, ?, 'ACTIVE', ?, ?, NOW()) "); $ins->execute([ $company_id, $fy, $asset_id, $employee_id, ($department_id > 0 ? $department_id : null), $issue_date, $issue_condition, $issue_remark, $user_id ]); // 3. Update asset_master status to ASSIGNED $upd = $pdo->prepare(" UPDATE asset_master SET asset_status = 'ASSIGNED', updated_by = ?, updated_at = NOW() WHERE id = ? AND company_id = ? "); $upd->execute([$user_id, $asset_id, $company_id]); $pdo->commit(); $success_msg = 'Asset assigned successfully.'; $pre_asset_id = 0; // Reset } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } $error_msg = 'Error: ' . $e->getMessage(); } } } // Fetch available assets $assets_stmt = $pdo->prepare("SELECT id, asset_code, brand, model FROM asset_master WHERE company_id = ? AND asset_status = 'AVAILABLE' ORDER BY asset_code ASC"); $assets_stmt->execute([$company_id]); $available_assets = $assets_stmt->fetchAll(PDO::FETCH_ASSOC); // Fetch employees (users table) $emp_stmt = $pdo->prepare("SELECT id, name FROM users WHERE company_id = ? ORDER BY name ASC"); $emp_stmt->execute([$company_id]); $employees = $emp_stmt->fetchAll(PDO::FETCH_ASSOC); // Fetch departments $dept_stmt = $pdo->prepare("SELECT id, department_name FROM department_master WHERE company_id = ? ORDER BY department_name ASC"); $dept_stmt->execute([$company_id]); $departments = $dept_stmt->fetchAll(PDO::FETCH_ASSOC); require_once __DIR__ . '/partials/header.php'; ?> <div class="container-fluid py-4"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card shadow-sm border-0"> <div class="card-header bg-dark text-white d-flex justify-content-between align-items-center py-2"> <h5 class="mb-0">📤 Assign Asset to Employee</h5> <a href="asset_master.php" class="btn btn-sm btn-outline-light">Back to Listing</a> </div> <div class="card-body p-4"> <?php if ($error_msg): ?> <div class="alert alert-danger alert-dismissible fade show" role="alert"> <?= h($error_msg) ?> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> <?php endif; ?> <?php if ($success_msg): ?> <div class="alert alert-success alert-dismissible fade show" role="alert"> <?= h($success_msg) ?> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> <?php endif; ?> <form method="POST" class="row g-3"> <div class="col-md-12"> <label class="form-label fw-bold small text-secondary">Select Available Asset <span class="text-danger">*</span></label> <select name="asset_id" class="form-select" required> <option value="">Choose Asset...</option> <?php foreach ($available_assets as $ast): ?> <option value="<?= $ast['id'] ?>" <?= ($pre_asset_id == $ast['id']) ? 'selected' : '' ?>> <?= h($ast['asset_code']) ?> — <?= h($ast['brand']) ?> <?= h($ast['model']) ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-6"> <label class="form-label fw-bold small text-secondary">Employee <span class="text-danger">*</span></label> <select name="employee_id" class="form-select" required> <option value="">Select Employee...</option> <?php foreach ($employees as $emp): ?> <option value="<?= $emp['id'] ?>"><?= h($emp['name']) ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-6"> <label class="form-label fw-bold small text-secondary">Department</label> <select name="department_id" class="form-select"> <option value="">Select Department...</option> <?php foreach ($departments as $dept): ?> <option value="<?= $dept['id'] ?>"><?= h($dept['department_name']) ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-6"> <label class="form-label fw-bold small text-secondary">Issue Date <span class="text-danger">*</span></label> <input type="date" name="issue_date" class="form-control" value="<?= date('Y-m-d') ?>" required> </div> <div class="col-md-6"> <label class="form-label fw-bold small text-secondary">Issue Condition</label> <select name="issue_condition" class="form-select"> <?php foreach (['NEW', 'GOOD', 'AVERAGE', 'DAMAGED'] as $cond): ?> <option value="<?= $cond ?>" <?= $cond === 'GOOD' ? 'selected' : '' ?>><?= $cond ?></option> <?php endforeach; ?> </select> </div> <div class="col-12"> <label class="form-label fw-bold small text-secondary">Issue Remark / Notes</label> <textarea name="issue_remark" class="form-control" rows="2" placeholder="Optional notes during hand-over..."></textarea> </div> <div class="col-12 d-flex justify-content-end gap-2 mt-4"> <a href="asset_master.php" class="btn btn-outline-secondary px-4">Cancel</a> <button type="submit" class="btn btn-primary px-4 fw-bold">Assign Asset</button> </div> </form> </div> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>