Files
sam-api/app/Models/Tenants/LeaveGrant.php
권혁성 189b38c936 feat: Auditable 트레이트 구현 및 97개 모델 적용
- Auditable 트레이트 신규 생성 (bootAuditable 패턴)
  - creating: created_by/updated_by 자동 채우기
  - updating: updated_by 자동 채우기
  - deleting: deleted_by 채우기 + saveQuietly()
  - created/updated/deleted: audit_logs 자동 기록
- 기존 AuditLogger 패턴과 동일한 try/catch 조용한 실패
- 변경된 필드만 before/after 기록 (updated 이벤트)
- auditExclude 프로퍼티로 모델별 제외 필드 설정 가능
- 제외 대상: Attendance, StockTransaction, TodayIssue 등 고빈도/시스템 모델

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 15:33:54 +09:00

146 lines
3.6 KiB
PHP

<?php
namespace App\Models\Tenants;
use App\Models\Members\User;
use App\Traits\Auditable;
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 $grant_type
* @property string $grant_date
* @property float $grant_days
* @property string|null $reason
* @property int|null $created_by
* @property int|null $updated_by
* @property int|null $deleted_by
*/
class LeaveGrant extends Model
{
use Auditable, BelongsToTenant, SoftDeletes;
protected $table = 'leave_grants';
protected $casts = [
'grant_date' => 'date',
'grant_days' => 'decimal:1',
];
protected $fillable = [
'tenant_id',
'user_id',
'grant_type',
'grant_date',
'grant_days',
'reason',
'created_by',
'updated_by',
'deleted_by',
];
// =========================================================================
// 상수 정의
// =========================================================================
public const TYPE_ANNUAL = 'annual'; // 연차
public const TYPE_MONTHLY = 'monthly'; // 월차
public const TYPE_REWARD = 'reward'; // 포상휴가
public const TYPE_CONDOLENCE = 'condolence'; // 경조사
public const TYPE_OTHER = 'other'; // 기타
public const GRANT_TYPES = [
self::TYPE_ANNUAL,
self::TYPE_MONTHLY,
self::TYPE_REWARD,
self::TYPE_CONDOLENCE,
self::TYPE_OTHER,
];
// =========================================================================
// 관계 정의
// =========================================================================
/**
* 부여 대상자
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
/**
* 부여자
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
// =========================================================================
// 스코프
// =========================================================================
/**
* 특정 유형
*/
public function scopeOfType($query, string $type)
{
return $query->where('grant_type', $type);
}
/**
* 특정 사용자
*/
public function scopeForUser($query, int $userId)
{
return $query->where('user_id', $userId);
}
/**
* 특정 연도
*/
public function scopeForYear($query, int $year)
{
return $query->whereYear('grant_date', $year);
}
/**
* 날짜 범위
*/
public function scopeBetweenDates($query, string $startDate, string $endDate)
{
return $query->whereBetween('grant_date', [$startDate, $endDate]);
}
// =========================================================================
// 헬퍼 메서드
// =========================================================================
/**
* 부여 유형 라벨
*/
public function getGrantTypeLabelAttribute(): string
{
return match ($this->grant_type) {
self::TYPE_ANNUAL => '연차',
self::TYPE_MONTHLY => '월차',
self::TYPE_REWARD => '포상휴가',
self::TYPE_CONDOLENCE => '경조사',
self::TYPE_OTHER => '기타',
default => $this->grant_type,
};
}
}