- LeaveService: 휴가 신청 시 결재 자동 생성+상신 - LeaveService: approveByApproval/rejectByApproval 메서드 추가 - LeaveService: deletePendingLeave 시 연결된 결재 자동 취소 - ApprovalService: 승인/반려/회수/전결 시 휴가 상태 자동 동기화 - Leave 모델: approval_id, approval() 관계 추가 - UI: pending 휴가에 결재 상세 링크 추가, 승인/반려 버튼 제거
164 lines
4.2 KiB
PHP
164 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models\HR;
|
|
|
|
use App\Models\Approvals\Approval;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Leave extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'leaves';
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'user_id',
|
|
'leave_type',
|
|
'start_date',
|
|
'end_date',
|
|
'days',
|
|
'reason',
|
|
'status',
|
|
'approved_by',
|
|
'approved_at',
|
|
'approval_id',
|
|
'reject_reason',
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'tenant_id' => 'int',
|
|
'user_id' => 'int',
|
|
'approval_id' => 'int',
|
|
'approved_by' => 'int',
|
|
'created_by' => 'int',
|
|
'updated_by' => 'int',
|
|
'deleted_by' => 'int',
|
|
'start_date' => 'date',
|
|
'end_date' => 'date',
|
|
'days' => 'float',
|
|
'approved_at' => 'datetime',
|
|
];
|
|
|
|
// =========================================================================
|
|
// 상수
|
|
// =========================================================================
|
|
|
|
public const TYPE_MAP = [
|
|
'annual' => '연차',
|
|
'half_am' => '오전반차',
|
|
'half_pm' => '오후반차',
|
|
'sick' => '병가',
|
|
'family' => '경조사',
|
|
'maternity' => '출산',
|
|
'parental' => '육아',
|
|
];
|
|
|
|
public const STATUS_MAP = [
|
|
'pending' => '대기',
|
|
'approved' => '승인',
|
|
'rejected' => '반려',
|
|
'cancelled' => '취소',
|
|
];
|
|
|
|
public const STATUS_COLORS = [
|
|
'pending' => 'amber',
|
|
'approved' => 'emerald',
|
|
'rejected' => 'red',
|
|
'cancelled' => 'gray',
|
|
];
|
|
|
|
public const DEDUCTIBLE_TYPES = ['annual', 'half_am', 'half_pm'];
|
|
|
|
// =========================================================================
|
|
// 관계
|
|
// =========================================================================
|
|
|
|
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 approval(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Approval::class, 'approval_id');
|
|
}
|
|
|
|
// =========================================================================
|
|
// Accessor
|
|
// =========================================================================
|
|
|
|
public function getTypeLabelAttribute(): string
|
|
{
|
|
return self::TYPE_MAP[$this->leave_type] ?? $this->leave_type;
|
|
}
|
|
|
|
public function getStatusLabelAttribute(): string
|
|
{
|
|
return self::STATUS_MAP[$this->status] ?? $this->status;
|
|
}
|
|
|
|
public function getStatusColorAttribute(): string
|
|
{
|
|
return self::STATUS_COLORS[$this->status] ?? 'gray';
|
|
}
|
|
|
|
public function getIsDeductibleAttribute(): bool
|
|
{
|
|
return in_array($this->leave_type, self::DEDUCTIBLE_TYPES);
|
|
}
|
|
|
|
// =========================================================================
|
|
// 스코프
|
|
// =========================================================================
|
|
|
|
public function scopeForTenant($query, ?int $tenantId = null)
|
|
{
|
|
$tenantId = $tenantId ?? session('selected_tenant_id');
|
|
if ($tenantId) {
|
|
return $query->where($this->table.'.tenant_id', $tenantId);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function scopeBetweenDates($query, string $startDate, string $endDate)
|
|
{
|
|
return $query->where(function ($q) use ($startDate, $endDate) {
|
|
$q->where('start_date', '<=', $endDate)
|
|
->where('end_date', '>=', $startDate);
|
|
});
|
|
}
|
|
|
|
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 scopeWithStatus($query, string $status)
|
|
{
|
|
return $query->where('status', $status);
|
|
}
|
|
}
|