112 lines
2.4 KiB
PHP
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;
|
||
|
|
}
|
||
|
|
}
|