'date', 'end_date' => 'date', 'days' => 'decimal:1', 'approved_at' => 'datetime', ]; protected $fillable = [ 'tenant_id', 'user_id', 'leave_type', 'start_date', 'end_date', 'days', 'reason', 'status', 'approved_by', 'approved_at', 'reject_reason', 'created_by', 'updated_by', 'deleted_by', ]; protected $attributes = [ 'status' => 'pending', ]; // ========================================================================= // 상수 정의 // ========================================================================= public const TYPE_ANNUAL = 'annual'; // 연차 public const TYPE_HALF_AM = 'half_am'; // 오전반차 public const TYPE_HALF_PM = 'half_pm'; // 오후반차 public const TYPE_SICK = 'sick'; // 병가 public const TYPE_FAMILY = 'family'; // 경조사 public const TYPE_MATERNITY = 'maternity'; // 출산 public const TYPE_PARENTAL = 'parental'; // 육아 public const STATUS_PENDING = 'pending'; public const STATUS_APPROVED = 'approved'; public const STATUS_REJECTED = 'rejected'; public const STATUS_CANCELLED = 'cancelled'; public const LEAVE_TYPES = [ self::TYPE_ANNUAL, self::TYPE_HALF_AM, self::TYPE_HALF_PM, self::TYPE_SICK, self::TYPE_FAMILY, self::TYPE_MATERNITY, self::TYPE_PARENTAL, ]; public const STATUSES = [ self::STATUS_PENDING, self::STATUS_APPROVED, self::STATUS_REJECTED, self::STATUS_CANCELLED, ]; // ========================================================================= // 관계 정의 // ========================================================================= /** * 신청자 */ public function user(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } /** * 승인자 */ public function approver(): BelongsTo { return $this->belongsTo(User::class, 'approved_by'); } /** * 생성자 */ public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); } /** * 수정자 */ public function updater(): BelongsTo { return $this->belongsTo(User::class, 'updated_by'); } /** * 신청자 프로필 (테넌트별) * 주의: eager loading 시 constrain 필요 * ->with(['userProfile' => fn($q) => $q->where('tenant_id', $tenantId)]) */ public function userProfile(): HasOne { return $this->hasOne(TenantUserProfile::class, 'user_id', 'user_id'); } // ========================================================================= // 스코프 // ========================================================================= /** * 특정 상태 필터 */ public function scopeWithStatus($query, string $status) { return $query->where('status', $status); } /** * 승인 대기 중 */ public function scopePending($query) { return $query->where('status', self::STATUS_PENDING); } /** * 승인됨 */ public function scopeApproved($query) { return $query->where('status', self::STATUS_APPROVED); } /** * 특정 기간 내 */ public function scopeBetweenDates($query, string $startDate, string $endDate) { return $query->where(function ($q) use ($startDate, $endDate) { $q->whereBetween('start_date', [$startDate, $endDate]) ->orWhereBetween('end_date', [$startDate, $endDate]) ->orWhere(function ($q2) use ($startDate, $endDate) { $q2->where('start_date', '<=', $startDate) ->where('end_date', '>=', $endDate); }); }); } /** * 특정 사용자 */ public function scopeForUser($query, int $userId) { return $query->where('user_id', $userId); } /** * 특정 연도 */ public function scopeForYear($query, int $year) { return $query->whereYear('start_date', $year); } // ========================================================================= // 헬퍼 메서드 // ========================================================================= /** * 수정 가능 여부 */ public function isEditable(): bool { return $this->status === self::STATUS_PENDING; } /** * 승인 가능 여부 */ public function isApprovable(): bool { return $this->status === self::STATUS_PENDING; } /** * 취소 가능 여부 */ public function isCancellable(): bool { return in_array($this->status, [self::STATUS_PENDING, self::STATUS_APPROVED]); } /** * 휴가 유형 라벨 */ public function getLeaveTypeLabelAttribute(): string { return match ($this->leave_type) { self::TYPE_ANNUAL => '연차', self::TYPE_HALF_AM => '오전반차', self::TYPE_HALF_PM => '오후반차', self::TYPE_SICK => '병가', self::TYPE_FAMILY => '경조사', self::TYPE_MATERNITY => '출산휴가', self::TYPE_PARENTAL => '육아휴직', default => $this->leave_type, }; } /** * 상태 라벨 */ public function getStatusLabelAttribute(): string { return match ($this->status) { self::STATUS_PENDING => '승인대기', self::STATUS_APPROVED => '승인', self::STATUS_REJECTED => '반려', self::STATUS_CANCELLED => '취소', default => $this->status, }; } }