diff --git a/api.php b/api.php index b2e7bd4..343de55 100644 --- a/api.php +++ b/api.php @@ -60,34 +60,56 @@ try { case 'add_user': $data = json_decode(file_get_contents('php://input'), true); + $status = $data['status'] ?? 'active'; + $department_id = $data['department_id']; + + // Auto-relocate based on status + if ($status === 'on_leave' || $status === 'retired') { + $dept_name = ($status === 'on_leave') ? '휴직' : '퇴사'; + $target_dept = $db->query("SELECT id FROM departments WHERE name = '$dept_name'")->fetchColumn(); + if ($target_dept) + $department_id = $target_dept; + } + $stmt = $db->prepare("INSERT INTO users (emp_id, name, department_id, position, email, phone, mobile, accounting_type, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([ $data['emp_id'], $data['name'], - $data['department_id'], + $department_id, $data['position'], $data['email'], $data['phone'] ?? '', $data['mobile'], $data['accounting_type'], - $data['status'] ?? 'active' + $status ]); echo json_encode(['success' => true]); break; case 'update_user': $data = json_decode(file_get_contents('php://input'), true); + $status = $data['status']; + $department_id = $data['department_id']; + + // Auto-relocate based on status + if ($status === 'on_leave' || $status === 'retired') { + $dept_name = ($status === 'on_leave') ? '휴직' : '퇴사'; + $target_dept = $db->query("SELECT id FROM departments WHERE name = '$dept_name'")->fetchColumn(); + if ($target_dept) + $department_id = $target_dept; + } + $stmt = $db->prepare("UPDATE users SET emp_id = ?, name = ?, department_id = ?, position = ?, email = ?, phone = ?, mobile = ?, accounting_type = ?, status = ? WHERE id = ?"); $stmt->execute([ $data['emp_id'], $data['name'], - $data['department_id'], + $department_id, $data['position'], $data['email'], $data['phone'] ?? '', $data['mobile'], $data['accounting_type'], - $data['status'], + $status, $data['id'] ]); echo json_encode(['success' => true]); @@ -352,8 +374,49 @@ try { case 'update_dept': $data = json_decode(file_get_contents('php://input'), true); - $stmt = $db->prepare("UPDATE departments SET name = ?, parent_id = ? WHERE id = ?"); - $stmt->execute([$data['name'], $data['parent_id'] ?: null, $data['id']]); + $new_name = $data['name']; + $parent_id = $data['parent_id'] ?: null; + $dept_id = $data['id']; + $merge_target_id = $data['merge_target_id'] ?? null; + + if ($merge_target_id) { + // Scenario: Merge members to an existing department with the same name + $db->prepare("UPDATE users SET department_id = ? WHERE department_id = ?")->execute([$merge_target_id, $dept_id]); + // Recursively move children? For now, we delete the source dept after merging members + $db->prepare("DELETE FROM departments WHERE id = ?")->execute([$dept_id]); + echo json_encode(['success' => true, 'merged' => true]); + } else { + // Check if a department with the same name exists under the same parent (excluding itself) + $check_stmt = $db->prepare("SELECT id FROM departments WHERE name = ? AND (parent_id <=> ?) AND id != ?"); + $check_stmt->execute([$new_name, $parent_id, $dept_id]); + $existing = $check_stmt->fetch(); + + if ($existing) { + echo json_encode(['success' => false, 'conflict' => true, 'existing_id' => $existing['id']]); + } else { + $stmt = $db->prepare("UPDATE departments SET name = ?, parent_id = ? WHERE id = ?"); + $stmt->execute([$new_name, $parent_id, $dept_id]); + echo json_encode(['success' => true]); + } + } + break; + + case 'delete_dept': + $dept_id = $_GET['id']; + // 1. Find the '미소속' department ID + $miso_id = $db->query("SELECT id FROM departments WHERE name = '미소속'")->fetchColumn(); + if (!$miso_id) { + // Fallback creation if not exists + $db->exec("INSERT INTO departments (name, level) VALUES ('미소속', 0)"); + $miso_id = $db->lastInsertId(); + } + + // 2. Move members to '미소속' + $db->prepare("UPDATE users SET department_id = ? WHERE department_id = ?")->execute([$miso_id, $dept_id]); + + // 3. Delete the department + $db->prepare("DELETE FROM departments WHERE id = ?")->execute([$dept_id]); + echo json_encode(['success' => true]); break; @@ -500,6 +563,14 @@ try { break; } + // Enhanced logic: if status is changed to leave/retired, override dept + if ($newStatus === 'on_leave' || $newStatus === 'retired') { + $dept_name = ($newStatus === 'on_leave') ? '휴직' : '퇴사'; + $target_dept = $db->query("SELECT id FROM departments WHERE name = '$dept_name'")->fetchColumn(); + if ($target_dept) + $newDeptId = $target_dept; + } + $sql = "UPDATE users SET "; $params = []; $updates = []; diff --git a/cards.php b/cards.php index ced6352..25f2348 100644 --- a/cards.php +++ b/cards.php @@ -37,34 +37,38 @@ 카드 등록 - +