feat: add Role dropdown to employee create and edit forms
- EmployeeController::create(): pass company roles + defaultRole (employee) to Inertia - EmployeeController::store(): assign role from role_id request field (fallback to 'employee') - EmployeeController::edit(): pass company roles + currentRoleId to Inertia - EmployeeController::update(): syncRoles() with selected role_id - create.tsx: Role <Select> dropdown pre-selected to default employee role - edit.tsx: Role <Select> dropdown pre-selected to employee's current role
This commit is contained in:
@@ -190,14 +190,18 @@ class EmployeeController extends Controller
|
||||
->where('status', 'active')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$roles = Role::whereIn('created_by', getCompanyAndUsersId())->get(['id', 'name']);
|
||||
|
||||
return Inertia::render('hr/employees/create', [
|
||||
'branches' => $branches,
|
||||
'departments' => $departments,
|
||||
'designations' => $designations,
|
||||
'documentTypes' => $documentTypes,
|
||||
'shifts' => $shifts,
|
||||
'branches' => $branches,
|
||||
'departments' => $departments,
|
||||
'designations' => $designations,
|
||||
'documentTypes' => $documentTypes,
|
||||
'shifts' => $shifts,
|
||||
'attendancePolicies' => $attendancePolicies,
|
||||
'generatedEmployeeId' => Employee::generateEmployeeId(),
|
||||
'generatedEmployeeId'=> Employee::generateEmployeeId(),
|
||||
'roles' => $roles,
|
||||
'defaultRole' => $roles->firstWhere('name', 'employee')?->id,
|
||||
]);
|
||||
} else {
|
||||
return redirect()->back()->with('error', __('Permission Denied.'));
|
||||
@@ -324,17 +328,17 @@ class EmployeeController extends Controller
|
||||
}
|
||||
$user->save();
|
||||
|
||||
// Assign Employee role
|
||||
if (isSaaS()) {
|
||||
$employeeRole = Role::where('created_by', createdBy())->where('name', 'employee')->first();
|
||||
if ($employeeRole) {
|
||||
$user->assignRole($employeeRole);
|
||||
}
|
||||
} else {
|
||||
$employeeRole = Role::where('name', 'employee')->first();
|
||||
if ($employeeRole) {
|
||||
$user->assignRole($employeeRole);
|
||||
}
|
||||
// Assign role from request, with fallback to 'employee' role
|
||||
$roleId = $request->input('role_id');
|
||||
$roleToAssign = $roleId ? Role::find($roleId) : null;
|
||||
if (!$roleToAssign) {
|
||||
// Fallback: find the default 'employee' role
|
||||
$roleToAssign = isSaaS()
|
||||
? Role::where('created_by', createdBy())->where('name', 'employee')->first()
|
||||
: Role::where('name', 'employee')->first();
|
||||
}
|
||||
if ($roleToAssign) {
|
||||
$user->assignRole($roleToAssign);
|
||||
}
|
||||
|
||||
// Create Employee model object
|
||||
@@ -508,14 +512,19 @@ class EmployeeController extends Controller
|
||||
->where('status', 'active')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$roles = Role::whereIn('created_by', getCompanyAndUsersId())->get(['id', 'name']);
|
||||
$currentRole = $user->roles->first();
|
||||
|
||||
return Inertia::render('hr/employees/edit', [
|
||||
'employee' => $user,
|
||||
'branches' => $branches,
|
||||
'departments' => $departments,
|
||||
'designations' => $designations,
|
||||
'documentTypes' => $documentTypes,
|
||||
'shifts' => $shifts,
|
||||
'employee' => $user,
|
||||
'branches' => $branches,
|
||||
'departments' => $departments,
|
||||
'designations' => $designations,
|
||||
'documentTypes' => $documentTypes,
|
||||
'shifts' => $shifts,
|
||||
'attendancePolicies' => $attendancePolicies,
|
||||
'roles' => $roles,
|
||||
'currentRoleId' => $currentRole?->id,
|
||||
]);
|
||||
} else {
|
||||
return redirect()->back()->with('error', __('Permission Denied.'));
|
||||
@@ -689,6 +698,15 @@ class EmployeeController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// Sync role if provided
|
||||
$roleId = $request->input('role_id');
|
||||
if ($roleId) {
|
||||
$role = Role::find($roleId);
|
||||
if ($role) {
|
||||
$user->syncRoles([$role]);
|
||||
}
|
||||
}
|
||||
|
||||
\DB::commit();
|
||||
return redirect()->route('hr.employees.index')->with('success', __('Employee updated successfully'));
|
||||
} catch (\Exception $e) {
|
||||
|
||||
Reference in New Issue
Block a user