feat: [attendance] 근태관리 2차 고도화 8개 기능 구현
- 월간 캘린더 뷰 (사원별 필터, 날짜 클릭 등록, HTMX 월 이동) - 일괄 등록 (다수 사원 체크박스 선택 후 일괄 등록, upsert 처리) - 사원별 월간 요약 (상태별 카운트 + 총 근무시간 집계 테이블) - 초과근무 알림 (주 48h 경고 / 52h 위험 배너) - 근태 승인 워크플로우 (신청→승인→근태 레코드 자동 생성) - 자동 결근 처리 (매일 23:50 스케줄러, 주말 제외) - 연차 관리 연동 (휴가 등록 시 leave_balances 자동 차감) - GPS 출퇴근 UI (테이블 GPS 아이콘 + 상세 모달) - 탭 네비게이션 (목록/캘린더/요약/승인) HTMX 기반 전환
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
|
||||
use App\Models\HR\Attendance;
|
||||
use App\Models\HR\Employee;
|
||||
use App\Models\HR\LeaveBalance;
|
||||
use App\Models\Tenants\Department;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -116,6 +118,15 @@ public function storeAttendance(array $data): Attendance
|
||||
$jsonDetails['check_out'] = $data['check_out'];
|
||||
}
|
||||
|
||||
// 근무 시간 자동 계산
|
||||
if (! empty($data['check_in']) && ! empty($data['check_out'])) {
|
||||
$in = Carbon::createFromFormat('H:i', $data['check_in']);
|
||||
$out = Carbon::createFromFormat('H:i', $data['check_out']);
|
||||
if ($out->gt($in)) {
|
||||
$jsonDetails['work_minutes'] = $out->diffInMinutes($in);
|
||||
}
|
||||
}
|
||||
|
||||
$attendance = Attendance::updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $tenantId,
|
||||
@@ -134,10 +145,72 @@ public function storeAttendance(array $data): Attendance
|
||||
$attendance->update(['created_by' => auth()->id()]);
|
||||
}
|
||||
|
||||
// 휴가 상태이면 연차 차감
|
||||
if (($data['status'] ?? '') === 'vacation') {
|
||||
$this->deductLeaveBalance($tenantId, $data['user_id']);
|
||||
}
|
||||
|
||||
return $attendance->load('user');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 일괄 등록
|
||||
*/
|
||||
public function bulkStore(array $data): array
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
$created = 0;
|
||||
$updated = 0;
|
||||
|
||||
DB::transaction(function () use ($data, $tenantId, &$created, &$updated) {
|
||||
$jsonDetails = [];
|
||||
if (! empty($data['check_in'])) {
|
||||
$jsonDetails['check_in'] = $data['check_in'];
|
||||
}
|
||||
if (! empty($data['check_out'])) {
|
||||
$jsonDetails['check_out'] = $data['check_out'];
|
||||
}
|
||||
|
||||
if (! empty($data['check_in']) && ! empty($data['check_out'])) {
|
||||
$in = Carbon::createFromFormat('H:i', $data['check_in']);
|
||||
$out = Carbon::createFromFormat('H:i', $data['check_out']);
|
||||
if ($out->gt($in)) {
|
||||
$jsonDetails['work_minutes'] = $out->diffInMinutes($in);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data['user_ids'] as $userId) {
|
||||
$attendance = Attendance::updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $tenantId,
|
||||
'user_id' => $userId,
|
||||
'base_date' => $data['base_date'],
|
||||
],
|
||||
[
|
||||
'status' => $data['status'] ?? 'onTime',
|
||||
'json_details' => ! empty($jsonDetails) ? $jsonDetails : null,
|
||||
'remarks' => $data['remarks'] ?? null,
|
||||
'updated_by' => auth()->id(),
|
||||
]
|
||||
);
|
||||
|
||||
if ($attendance->wasRecentlyCreated) {
|
||||
$attendance->update(['created_by' => auth()->id()]);
|
||||
$created++;
|
||||
} else {
|
||||
$updated++;
|
||||
}
|
||||
|
||||
if (($data['status'] ?? '') === 'vacation') {
|
||||
$this->deductLeaveBalance($tenantId, $userId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return ['created' => $created, 'updated' => $updated];
|
||||
}
|
||||
|
||||
/**
|
||||
* 근태 수정
|
||||
*/
|
||||
@@ -178,6 +251,20 @@ public function updateAttendance(int $id, array $data): ?Attendance
|
||||
unset($jsonDetails['check_out']);
|
||||
}
|
||||
}
|
||||
|
||||
// 근무 시간 재계산
|
||||
$checkIn = $jsonDetails['check_in'] ?? null;
|
||||
$checkOut = $jsonDetails['check_out'] ?? null;
|
||||
if ($checkIn && $checkOut) {
|
||||
$in = Carbon::createFromFormat('H:i', $checkIn);
|
||||
$out = Carbon::createFromFormat('H:i', $checkOut);
|
||||
if ($out->gt($in)) {
|
||||
$jsonDetails['work_minutes'] = $out->diffInMinutes($in);
|
||||
}
|
||||
} else {
|
||||
unset($jsonDetails['work_minutes']);
|
||||
}
|
||||
|
||||
$updateData['json_details'] = ! empty($jsonDetails) ? $jsonDetails : null;
|
||||
$updateData['updated_by'] = auth()->id();
|
||||
|
||||
@@ -229,6 +316,218 @@ public function bulkDelete(array $ids): int
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 월간 캘린더 데이터 (base_date 기준 그룹화)
|
||||
*/
|
||||
public function getMonthlyCalendarData(int $year, int $month, ?int $userId = null): Collection
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
|
||||
$startDate = sprintf('%04d-%02d-01', $year, $month);
|
||||
$endDate = sprintf('%04d-%02d-%02d', $year, $month, cal_days_in_month(CAL_GREGORIAN, $month, $year));
|
||||
|
||||
$query = Attendance::query()
|
||||
->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId)])
|
||||
->forTenant($tenantId)
|
||||
->betweenDates($startDate, $endDate)
|
||||
->orderBy('base_date')
|
||||
->orderBy('user_id');
|
||||
|
||||
if ($userId) {
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 사원별 월간 요약
|
||||
*/
|
||||
public function getEmployeeMonthlySummary(int $year, int $month): array
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
|
||||
$startDate = sprintf('%04d-%02d-01', $year, $month);
|
||||
$endDate = sprintf('%04d-%02d-%02d', $year, $month, cal_days_in_month(CAL_GREGORIAN, $month, $year));
|
||||
|
||||
$raw = Attendance::query()
|
||||
->forTenant($tenantId)
|
||||
->betweenDates($startDate, $endDate)
|
||||
->select(
|
||||
'user_id',
|
||||
'status',
|
||||
DB::raw('COUNT(*) as cnt'),
|
||||
DB::raw("SUM(CAST(JSON_UNQUOTE(JSON_EXTRACT(json_details, '$.work_minutes')) AS UNSIGNED)) as total_minutes")
|
||||
)
|
||||
->groupBy('user_id', 'status')
|
||||
->get();
|
||||
|
||||
$summary = [];
|
||||
foreach ($raw as $row) {
|
||||
if (! isset($summary[$row->user_id])) {
|
||||
$summary[$row->user_id] = [
|
||||
'user_id' => $row->user_id,
|
||||
'total_days' => 0,
|
||||
'total_minutes' => 0,
|
||||
'statuses' => [],
|
||||
];
|
||||
}
|
||||
$summary[$row->user_id]['total_days'] += $row->cnt;
|
||||
$summary[$row->user_id]['total_minutes'] += (int) $row->total_minutes;
|
||||
$summary[$row->user_id]['statuses'][$row->status] = $row->cnt;
|
||||
}
|
||||
|
||||
// 사원 정보 가져오기
|
||||
$employees = Employee::query()
|
||||
->with(['user:id,name', 'department:id,name'])
|
||||
->forTenant($tenantId)
|
||||
->activeEmployees()
|
||||
->get()
|
||||
->keyBy('user_id');
|
||||
|
||||
foreach ($summary as &$item) {
|
||||
$emp = $employees[$item['user_id']] ?? null;
|
||||
$item['name'] = $emp?->display_name ?? $emp?->user?->name ?? '-';
|
||||
$item['department'] = $emp?->department?->name ?? '-';
|
||||
}
|
||||
|
||||
return array_values($summary);
|
||||
}
|
||||
|
||||
/**
|
||||
* 초과근무 알림 (이번 주 기준)
|
||||
*/
|
||||
public function getOvertimeAlerts(): array
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
|
||||
$weekStart = now()->startOfWeek(Carbon::MONDAY)->toDateString();
|
||||
$weekEnd = now()->endOfWeek(Carbon::SUNDAY)->toDateString();
|
||||
|
||||
$results = Attendance::query()
|
||||
->forTenant($tenantId)
|
||||
->betweenDates($weekStart, $weekEnd)
|
||||
->select(
|
||||
'user_id',
|
||||
DB::raw("SUM(CAST(JSON_UNQUOTE(JSON_EXTRACT(json_details, '$.work_minutes')) AS UNSIGNED)) as week_minutes")
|
||||
)
|
||||
->groupBy('user_id')
|
||||
->having('week_minutes', '>=', 2880) // 48시간 = 2880분
|
||||
->get();
|
||||
|
||||
$alerts = [];
|
||||
if ($results->isNotEmpty()) {
|
||||
$employees = Employee::query()
|
||||
->with(['user:id,name'])
|
||||
->forTenant($tenantId)
|
||||
->activeEmployees()
|
||||
->get()
|
||||
->keyBy('user_id');
|
||||
|
||||
foreach ($results as $row) {
|
||||
$emp = $employees[$row->user_id] ?? null;
|
||||
$hours = round($row->week_minutes / 60, 1);
|
||||
$alerts[] = [
|
||||
'user_id' => $row->user_id,
|
||||
'name' => $emp?->display_name ?? $emp?->user?->name ?? '-',
|
||||
'hours' => $hours,
|
||||
'level' => $row->week_minutes >= 3120 ? 'danger' : 'warning', // 52h = 3120분
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $alerts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 자동 결근 처리 (영업일에 출근 기록 없는 사원)
|
||||
*/
|
||||
public function markAbsentees(?string $date = null): int
|
||||
{
|
||||
$date = $date ?? now()->toDateString();
|
||||
$carbonDate = Carbon::parse($date);
|
||||
|
||||
// 주말이면 스킵
|
||||
if ($carbonDate->isWeekend()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
|
||||
// 모든 테넌트의 활성 사원 조회
|
||||
$tenantIds = DB::table('tenants')->pluck('id');
|
||||
|
||||
foreach ($tenantIds as $tenantId) {
|
||||
$activeUserIds = Employee::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->activeEmployees()
|
||||
->pluck('user_id')
|
||||
->toArray();
|
||||
|
||||
if (empty($activeUserIds)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 이미 기록이 있는 사원 제외
|
||||
$existingUserIds = Attendance::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereDate('base_date', $date)
|
||||
->pluck('user_id')
|
||||
->toArray();
|
||||
|
||||
$absentUserIds = array_diff($activeUserIds, $existingUserIds);
|
||||
|
||||
foreach ($absentUserIds as $userId) {
|
||||
Attendance::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'user_id' => $userId,
|
||||
'base_date' => $date,
|
||||
'status' => 'absent',
|
||||
'remarks' => '자동 결근 처리',
|
||||
'created_by' => null,
|
||||
]);
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 연차 잔여 조회
|
||||
*/
|
||||
public function getLeaveBalance(int $userId): ?LeaveBalance
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
$year = now()->year;
|
||||
|
||||
return LeaveBalance::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('user_id', $userId)
|
||||
->where('year', $year)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* 연차 차감 (remaining_days는 stored generated이므로 used_days만 업데이트)
|
||||
*/
|
||||
private function deductLeaveBalance(int $tenantId, int $userId): void
|
||||
{
|
||||
$year = now()->year;
|
||||
|
||||
$balance = LeaveBalance::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('user_id', $userId)
|
||||
->where('year', $year)
|
||||
->first();
|
||||
|
||||
if ($balance && $balance->remaining_days > 0) {
|
||||
$balance->update([
|
||||
'used_days' => $balance->used_days + 1,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 부서 목록 (드롭다운용)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user