Files
sam-manage/app/Models/HR/LeaveGrant.php
김보곤 684bba105f feat: [leave] 휴가관리 Phase 1 구현
- Leave, LeavePolicy, LeaveGrant 모델 생성
- LeaveBalance 헬퍼 메서드 추가 (useLeave, restoreLeave, canUse)
- LeaveService 핵심 로직 (신청, 승인, 반려, 취소, 잔여연차, 통계)
- API 컨트롤러 (목록, 등록, 승인/반려/취소, 잔여연차, 통계, CSV 내보내기)
- 뷰 컨트롤러 + 라우트 등록 (web, api)
- Blade 뷰 (index + 3개 탭 partials: table, balance, stats)
2026-02-26 22:34:31 +09:00

89 lines
2.2 KiB
PHP

<?php
namespace App\Models\HR;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class LeaveGrant extends Model
{
use SoftDeletes;
protected $table = 'leave_grants';
protected $fillable = [
'tenant_id',
'user_id',
'grant_type',
'grant_date',
'grant_days',
'reason',
'created_by',
];
protected $casts = [
'tenant_id' => 'int',
'user_id' => 'int',
'created_by' => 'int',
'grant_date' => 'date',
'grant_days' => 'float',
];
public const TYPE_MAP = [
'annual' => '정기연차',
'monthly' => '월차',
'reward' => '포상',
'condolence' => '경조사',
'other' => '기타',
];
// =========================================================================
// 관계
// =========================================================================
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
// =========================================================================
// Accessor
// =========================================================================
public function getGrantTypeLabelAttribute(): string
{
return self::TYPE_MAP[$this->grant_type] ?? $this->grant_type;
}
// =========================================================================
// 스코프
// =========================================================================
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 scopeForUser($query, int $userId)
{
return $query->where('user_id', $userId);
}
public function scopeForYear($query, int $year)
{
return $query->whereYear('grant_date', $year);
}
}