Files
sam-api/app/Models/Tenants/LeaveBalance.php
hskwon e81e5d7084 feat: 휴가 관리 API 구현 (Phase 1)
- leaves, leave_balances 테이블 마이그레이션 추가
- Leave, LeaveBalance 모델 구현 (BelongsToTenant, SoftDeletes)
- LeaveService 서비스 구현 (CRUD, 승인/반려/취소, 잔여일수 관리)
- LeaveController 및 FormRequest 5개 생성
- API 엔드포인트 11개 등록 (/v1/leaves/*)
- Swagger 문서 (LeaveApi.php) 작성
- i18n 메시지 키 추가 (message.leave.*, error.leave.*)
2025-12-17 20:13:48 +09:00

112 lines
2.4 KiB
PHP

<?php
namespace App\Models\Tenants;
use App\Models\Members\User;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* 휴가 잔여일수 모델
*
* @property int $id
* @property int $tenant_id
* @property int $user_id
* @property int $year
* @property float $total_days
* @property float $used_days
* @property float $remaining_days
*/
class LeaveBalance extends Model
{
use BelongsToTenant;
protected $table = 'leave_balances';
protected $casts = [
'year' => 'integer',
'total_days' => 'decimal:1',
'used_days' => 'decimal:1',
'remaining_days' => 'decimal:1',
];
protected $fillable = [
'tenant_id',
'user_id',
'year',
'total_days',
'used_days',
];
// =========================================================================
// 관계 정의
// =========================================================================
/**
* 사용자
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
// =========================================================================
// 스코프
// =========================================================================
/**
* 특정 연도
*/
public function scopeForYear($query, int $year)
{
return $query->where('year', $year);
}
/**
* 특정 사용자
*/
public function scopeForUser($query, int $userId)
{
return $query->where('user_id', $userId);
}
/**
* 현재 연도
*/
public function scopeCurrentYear($query)
{
return $query->where('year', now()->year);
}
// =========================================================================
// 헬퍼 메서드
// =========================================================================
/**
* 휴가 사용
*/
public function useLeave(float $days): void
{
$this->used_days += $days;
$this->save();
}
/**
* 휴가 복원 (취소 시)
*/
public function restoreLeave(float $days): void
{
$this->used_days = max(0, $this->used_days - $days);
$this->save();
}
/**
* 사용 가능 여부
*/
public function canUse(float $days): bool
{
return $this->remaining_days >= $days;
}
}