dstpabuaran.com/app/Services/Admin/HR/LeaveRequestService.php
Yoga Pangestu da30f12fc2 feat: add employee management features including create, edit, and list functionalities
- Implemented employee columns for data table with sorting and action buttons.
- Created employee creation form with validation and default password information.
- Developed employee editing form pre-filled with existing data.
- Added employee listing page with delete and reset password functionalities.
- Introduced leave request management with columns for status and actions.
- Created leave request form for adding and editing requests with date pickers.
- Implemented confirmation dialogs for delete, approve, and reject actions.
2026-07-29 22:33:55 +07:00

87 lines
2.4 KiB
PHP

<?php
namespace App\Services\Admin\HR;
use App\Enums\LeaveRequestStatus;
use App\Models\LeaveRequest;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class LeaveRequestService
{
public function getAll(): Collection
{
return LeaveRequest::with(['employee.user.userProfile', 'verifiedBy'])
->latest()
->get();
}
public function create(array $data): LeaveRequest
{
return DB::transaction(function () use ($data) {
$employee = auth()->user()->employee;
if (! $employee) {
throw new \Exception('Anda tidak terdaftar sebagai karyawan.');
}
$startDate = new Carbon($data['start_date']);
$endDate = new Carbon($data['end_date']);
$totalDays = $startDate->diffInDays($endDate) + 1;
return LeaveRequest::create([
'employee_id' => $employee->id,
'start_date' => $data['start_date'],
'end_date' => $data['end_date'],
'total_days' => $totalDays,
'status' => LeaveRequestStatus::PENDING,
]);
});
}
public function update(LeaveRequest $leaveRequest, array $data): LeaveRequest
{
return DB::transaction(function () use ($leaveRequest, $data) {
$startDate = new Carbon($data['start_date']);
$endDate = new Carbon($data['end_date']);
$totalDays = $startDate->diffInDays($endDate) + 1;
$leaveRequest->update([
'start_date' => $data['start_date'],
'end_date' => $data['end_date'],
'total_days' => $totalDays,
]);
return $leaveRequest;
});
}
public function delete(LeaveRequest $leaveRequest): bool
{
return $leaveRequest->delete();
}
public function approve(LeaveRequest $leaveRequest): LeaveRequest
{
$leaveRequest->update([
'status' => LeaveRequestStatus::APPROVED,
'verified_by_id' => auth()->id(),
'verified_at' => now(),
]);
return $leaveRequest;
}
public function reject(LeaveRequest $leaveRequest): LeaveRequest
{
$leaveRequest->update([
'status' => LeaveRequestStatus::REJECTED,
'verified_by_id' => auth()->id(),
'verified_at' => now(),
]);
return $leaveRequest;
}
}