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.*)
This commit is contained in:
260
app/Models/Tenants/Leave.php
Normal file
260
app/Models/Tenants/Leave.php
Normal file
@@ -0,0 +1,260 @@
|
||||
<?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;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* 휴가 신청 모델
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $tenant_id
|
||||
* @property int $user_id
|
||||
* @property string $leave_type
|
||||
* @property string $start_date
|
||||
* @property string $end_date
|
||||
* @property float $days
|
||||
* @property string|null $reason
|
||||
* @property string $status
|
||||
* @property int|null $approved_by
|
||||
* @property string|null $approved_at
|
||||
* @property string|null $reject_reason
|
||||
* @property int|null $created_by
|
||||
* @property int|null $updated_by
|
||||
* @property int|null $deleted_by
|
||||
*/
|
||||
class Leave extends Model
|
||||
{
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
protected $table = 'leaves';
|
||||
|
||||
protected $casts = [
|
||||
'start_date' => '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');
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 스코프
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* 특정 상태 필터
|
||||
*/
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
111
app/Models/Tenants/LeaveBalance.php
Normal file
111
app/Models/Tenants/LeaveBalance.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user