dstpabuaran.com/app/Http/Controllers/Admin/HR/EmployeeController.php
Yoga Pangestu 0023309a8f Refactor services to improve role checks and streamline data retrieval
- Updated CustomerService to simplify getAll method.
- Refactored ProductService to utilize HasRoleChecks trait and improved role verification logic.
- Enhanced ProductVariantService with new methods for fetching data for restocking and transactions.
- Cleaned up RawMaterialService by removing unused methods and improving data retrieval.
- Adjusted SupplierService to streamline getAll method.
- Refactored RoleService to use Spatie's Role model and improved role filtering logic.
- Updated NotificationService to handle role labels more effectively.
- Improved StockMutationService by removing redundant paginated method.
- Cleaned up various frontend components to directly accept necessary props instead of nested data objects.
- Updated tests to reflect changes in service method names and ensure proper notification handling.
2026-08-09 11:33:25 +07:00

99 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\HR;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\HR\EmployeeRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\User;
use App\Services\Admin\HR\EmployeeService;
use App\Services\Admin\Settings\RoleService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
class EmployeeController extends Controller
{
public function __construct(
private EmployeeService $service,
private RoleService $roleService,
) {}
public function index(PaginatedRequest $request): Response
{
return Inertia::render('admin/hr/employee/index', [
'employees' => $this->service->paginated(
...$request->validatedWithDefaults(),
filters: $request->only(['employment_status', 'is_active', 'gender']),
),
'filters' => $request->only(['employment_status', 'is_active', 'gender']),
'canViewAll' => $this->service->canViewAll(),
]);
}
public function create(): Response
{
return Inertia::render('admin/hr/employee/create', [
'roles' => $this->roleService->getForEmployee(),
'canViewAll' => $this->service->canViewAll(),
]);
}
public function store(EmployeeRequest $request): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->store($request->validated()),
'Pegawai berhasil ditambahkan.',
'admin.hr.employees.index'
);
}
public function edit(User $user): Response
{
$user->load(['userProfile', 'employee', 'roles']);
return Inertia::render('admin/hr/employee/edit', [
'employee' => $user,
'roles' => $this->roleService->getForEmployee(),
'canViewAll' => $this->service->canViewAll(),
]);
}
public function update(EmployeeRequest $request, User $user): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->update($user, $request->validated()),
'Pegawai berhasil diperbarui.',
'admin.hr.employees.index'
);
}
public function destroy(User $user): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->destroy($user),
'Pegawai berhasil dihapus.',
'admin.hr.employees.index'
);
}
public function toggleActive(User $user): RedirectResponse
{
$employee = $this->service->toggleActive($user);
$status = $employee->is_active ? 'diaktifkan' : 'dinonaktifkan';
Inertia::flash('toast', ['type' => 'success', 'message' => "Pegawai berhasil {$status}."]);
return back();
}
public function resetPassword(User $user): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->resetPassword($user),
'Kata sandi pegawai berhasil direset ke kata sandi default.',
'admin.hr.employees.index'
);
}
}