diff --git a/api.php b/api.php index 6a89418..c2063f2 100644 --- a/api.php +++ b/api.php @@ -488,6 +488,15 @@ last_confirmed_date $old_stmt->execute([$data['id']]); $old_asset = $old_stmt->fetch(); + // Auto-relocate based on status if needed (optional) + + // If assigned_user_name is missing but current_user_id exists, try to fill it for logging + if (empty($data['assigned_user_name']) && !empty($data['current_user_id'])) { + $u_stmt = $db->prepare("SELECT name FROM users WHERE id = ?"); + $u_stmt->execute([$data['current_user_id']]); + $data['assigned_user_name'] = $u_stmt->fetchColumn() ?: ''; + } + $stmt = $db->prepare("UPDATE laptop_assets SET asset_tag = ?, model_id = ?, current_user_id = ?, status = ?, serial_number = ?, purchase_date = ?, ip_address = ?, cpu = ?, npu = ?, hdd0_model = ?, hdd0_capacity = ?, hdd1_model = ?, hdd1_capacity = ?, ram = ?, @@ -523,10 +532,11 @@ WHERE id = ?"); ]); // Auto-log assignment change - if ($data['assigned_user_name'] && $data['assigned_user_name'] !== ($old_asset['assigned_user_name'] ?? '')) { - $log_stmt = $db->prepare("INSERT INTO asset_history (asset_id, log_type, user_name, action_date) VALUES (?, -'assignment', ?, ?)"); - $log_stmt->execute([$data['id'], $data['assigned_user_name'], date('Y-m-d')]); + $new_name = $data['assigned_user_name'] ?: ''; + $old_name = $old_asset['assigned_user_name'] ?? ''; + if ($new_name !== $old_name) { + $log_stmt = $db->prepare("INSERT INTO asset_history (asset_id, log_type, user_name, action_date) VALUES (?, 'assignment', ?, ?)"); + $log_stmt->execute([$data['id'], $new_name ?: '재고(반납)', date('Y-m-d')]); } echo json_encode(['success' => true]); @@ -632,6 +642,11 @@ LIMIT 1")->fetchColumn(); if ($newStatus) { $updates[] = "status = ?"; $params[] = $newStatus; + // 만약 상태를 'stock'(재고)으로 변경하는 경우 배정 정보도 초기화 + if ($newStatus === 'stock') { + $updates[] = "current_user_id = NULL"; + $updates[] = "assigned_user_name = NULL"; + } } if ($newModelId) { $updates[] = "model_id = ?"; @@ -799,6 +814,83 @@ LIMIT 1")->fetchColumn(); echo json_encode(['success' => true]); break; + case 'archive_users': + $data = json_decode(file_get_contents('php://input'), true); + $userIds = $data['user_ids']; + if (empty($userIds)) { + echo json_encode(['success' => false, 'error' => 'No users selected']); + break; + } + + $db->beginTransaction(); + try { + $placeholders = implode(',', array_fill(0, count($userIds), '?')); + // Copy to archive + $db->prepare("INSERT INTO users_archive (id, emp_id, name, department_id, position, email, phone, mobile, accounting_type, status) + SELECT id, emp_id, name, department_id, position, email, phone, mobile, accounting_type, status FROM users WHERE id IN ($placeholders)") + ->execute($userIds); + // Delete from original + $db->prepare("DELETE FROM users WHERE id IN ($placeholders)")->execute($userIds); + $db->commit(); + echo json_encode(['success' => true]); + } catch (Exception $e) { + $db->rollBack(); + echo json_encode(['success' => false, 'error' => $e->getMessage()]); + } + break; + + case 'get_archived_users': + $query = "SELECT u.*, dp.path as dept_name + FROM users_archive u + LEFT JOIN ( + WITH RECURSIVE dept_path(id, path) AS ( + SELECT id, name FROM departments WHERE parent_id IS NULL + UNION ALL + SELECT d.id, dp.path || ' > ' || d.name + FROM departments d JOIN dept_path dp ON d.parent_id = dp.id + ) SELECT * FROM dept_path + ) dp ON u.department_id = dp.id + ORDER BY u.archived_at DESC"; + echo json_encode($db->query($query)->fetchAll()); + break; + + case 'restore_users': + $data = json_decode(file_get_contents('php://input'), true); + $userIds = $data['user_ids']; + if (empty($userIds)) { + echo json_encode(['success' => false, 'error' => 'No users selected']); + break; + } + + $db->beginTransaction(); + try { + $placeholders = implode(',', array_fill(0, count($userIds), '?')); + // Restore to users + $db->prepare("INSERT INTO users (id, emp_id, name, department_id, position, email, phone, mobile, accounting_type, status) + SELECT id, emp_id, name, department_id, position, email, phone, mobile, accounting_type, status FROM users_archive WHERE id IN ($placeholders)") + ->execute($userIds); + // Delete from archive + $db->prepare("DELETE FROM users_archive WHERE id IN ($placeholders)")->execute($userIds); + $db->commit(); + echo json_encode(['success' => true]); + } catch (Exception $e) { + $db->rollBack(); + echo json_encode(['success' => false, 'error' => $e->getMessage()]); + } + break; + + case 'bulk_delete_users': + $data = json_decode(file_get_contents('php://input'), true); + $userIds = $data['user_ids']; + if (empty($userIds)) { + echo json_encode(['success' => false, 'error' => 'No users selected']); + break; + } + $placeholders = implode(',', array_fill(0, count($userIds), '?')); + $db->prepare("DELETE FROM users WHERE id IN ($placeholders)")->execute($userIds); + echo json_encode(['success' => true]); + break; + case 'get_general_rentals': $query = "SELECT r.*, GROUP_CONCAT(i.item_name, ', ') as items FROM general_rentals r diff --git a/assets.db b/assets.db index 5a61581..cbf3bc4 100644 Binary files a/assets.db and b/assets.db differ diff --git a/fix_missing_column.php b/fix_missing_column.php new file mode 100644 index 0000000..a790fc5 --- /dev/null +++ b/fix_missing_column.php @@ -0,0 +1,9 @@ +exec("ALTER TABLE laptop_assets ADD COLUMN last_confirmed_date TEXT"); + echo "Column 'last_confirmed_date' added successfully to laptop_assets table.\n"; +} catch (Exception $e) { + echo "Error or column already exists: " . $e->getMessage() . "\n"; +} +?> \ No newline at end of file diff --git a/laptops.php b/laptops.php index 28704f8..f76a915 100644 --- a/laptops.php +++ b/laptops.php @@ -38,6 +38,18 @@