- Attendance 모델 (attendances 테이블, 상태/색상 매핑, check_in/check_out accessor) - AttendanceService (목록/월간통계/CRUD, 부서/사원 드롭다운) - API 컨트롤러 (HTMX+JSON 이중 응답, stats/index/store/update/destroy) - 페이지 컨트롤러 (index 페이지 렌더링) - 웹/API 라우트 등록 (hr/attendances, api/admin/hr/attendances) - index.blade.php (통계카드+필터+등록/수정 모달) - partials/table.blade.php (HTMX 부분 로드 테이블)
148 lines
3.8 KiB
PHP
148 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models\HR;
|
|
|
|
use App\Models\User;
|
|
use App\Traits\ModelTrait;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Attendance extends Model
|
|
{
|
|
use ModelTrait, SoftDeletes;
|
|
|
|
protected $table = 'attendances';
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'user_id',
|
|
'base_date',
|
|
'status',
|
|
'json_details',
|
|
'remarks',
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'json_details' => 'array',
|
|
'base_date' => 'date',
|
|
'tenant_id' => 'int',
|
|
'user_id' => 'int',
|
|
];
|
|
|
|
protected $attributes = [
|
|
'status' => 'onTime',
|
|
];
|
|
|
|
public const STATUS_MAP = [
|
|
'onTime' => '정시출근',
|
|
'late' => '지각',
|
|
'absent' => '결근',
|
|
'vacation' => '휴가',
|
|
'businessTrip' => '출장',
|
|
'fieldWork' => '외근',
|
|
'overtime' => '야근',
|
|
'remote' => '재택',
|
|
];
|
|
|
|
public const STATUS_COLORS = [
|
|
'onTime' => 'emerald',
|
|
'late' => 'amber',
|
|
'absent' => 'red',
|
|
'vacation' => 'blue',
|
|
'businessTrip' => 'purple',
|
|
'fieldWork' => 'indigo',
|
|
'overtime' => 'orange',
|
|
'remote' => 'teal',
|
|
];
|
|
|
|
// =========================================================================
|
|
// 관계 정의
|
|
// =========================================================================
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
// =========================================================================
|
|
// Accessor
|
|
// =========================================================================
|
|
|
|
public function getCheckInAttribute(): ?string
|
|
{
|
|
$checkIns = $this->json_details['check_ins'] ?? [];
|
|
|
|
if (! empty($checkIns)) {
|
|
$times = array_filter(array_map(fn ($entry) => $entry['time'] ?? null, $checkIns));
|
|
if (! empty($times)) {
|
|
sort($times);
|
|
|
|
return $times[0];
|
|
}
|
|
}
|
|
|
|
return $this->json_details['check_in'] ?? null;
|
|
}
|
|
|
|
public function getCheckOutAttribute(): ?string
|
|
{
|
|
$checkOuts = $this->json_details['check_outs'] ?? [];
|
|
|
|
if (! empty($checkOuts)) {
|
|
$times = array_filter(array_map(fn ($entry) => $entry['time'] ?? null, $checkOuts));
|
|
if (! empty($times)) {
|
|
rsort($times);
|
|
|
|
return $times[0];
|
|
}
|
|
}
|
|
|
|
return $this->json_details['check_out'] ?? null;
|
|
}
|
|
|
|
public function getWorkMinutesAttribute(): ?int
|
|
{
|
|
return isset($this->json_details['work_minutes'])
|
|
? (int) $this->json_details['work_minutes']
|
|
: null;
|
|
}
|
|
|
|
public function getStatusLabelAttribute(): string
|
|
{
|
|
return self::STATUS_MAP[$this->status] ?? $this->status;
|
|
}
|
|
|
|
public function getStatusColorAttribute(): string
|
|
{
|
|
return self::STATUS_COLORS[$this->status] ?? 'gray';
|
|
}
|
|
|
|
// =========================================================================
|
|
// 스코프
|
|
// =========================================================================
|
|
|
|
public function scopeForTenant($query, ?int $tenantId = null)
|
|
{
|
|
$tenantId = $tenantId ?? session('selected_tenant_id');
|
|
if ($tenantId) {
|
|
return $query->where($this->table.'.tenant_id', $tenantId);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function scopeOnDate($query, string $date)
|
|
{
|
|
return $query->whereDate('base_date', $date);
|
|
}
|
|
|
|
public function scopeBetweenDates($query, string $startDate, string $endDate)
|
|
{
|
|
return $query->whereBetween('base_date', [$startDate, $endDate]);
|
|
}
|
|
}
|