- 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>
113 lines
2.5 KiB
PHP
113 lines
2.5 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;
|
|
|
|
/**
|
|
* 휴가 잔여일수 모델
|
|
*
|
|
* @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 Auditable, 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;
|
|
}
|
|
}
|