Files
sam-manage/app/Models/Approvals/Approval.php
김보곤 12c9ad620a feat: [approval] 결재관리 Phase 1 MVP 구현
- 모델 4개: Approval, ApprovalStep, ApprovalForm, ApprovalLine
- ApprovalService: 목록/CRUD/워크플로우(상신/승인/반려/회수) 비즈니스 로직
- ApprovalApiController: JSON API 엔드포인트 (기안함/결재함/완료함/참조함)
- ApprovalController: Blade 뷰 컨트롤러 (HX-Redirect 처리)
- 뷰 8개: drafts, pending, completed, references, create, edit, show
- partials: _status-badge, _step-progress, _approval-line-editor
- api.php/web.php 라우트 등록
2026-02-27 23:17:41 +09:00

243 lines
6.6 KiB
PHP

<?php
namespace App\Models\Approvals;
use App\Models\User;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Approval extends Model
{
use BelongsToTenant, SoftDeletes;
protected $table = 'approvals';
protected $casts = [
'content' => 'array',
'attachments' => 'array',
'drafted_at' => 'datetime',
'completed_at' => 'datetime',
'current_step' => 'integer',
'is_urgent' => 'boolean',
];
protected $fillable = [
'tenant_id',
'document_number',
'form_id',
'line_id',
'title',
'content',
'body',
'status',
'is_urgent',
'drafter_id',
'department_id',
'drafted_at',
'completed_at',
'current_step',
'attachments',
'created_by',
'updated_by',
'deleted_by',
];
protected $attributes = [
'status' => 'draft',
'current_step' => 0,
'is_urgent' => false,
];
// =========================================================================
// 상태 상수
// =========================================================================
public const STATUS_DRAFT = 'draft';
public const STATUS_PENDING = 'pending';
public const STATUS_APPROVED = 'approved';
public const STATUS_REJECTED = 'rejected';
public const STATUS_CANCELLED = 'cancelled';
public const STATUSES = [
self::STATUS_DRAFT,
self::STATUS_PENDING,
self::STATUS_APPROVED,
self::STATUS_REJECTED,
self::STATUS_CANCELLED,
];
// =========================================================================
// 관계 정의
// =========================================================================
public function form(): BelongsTo
{
return $this->belongsTo(ApprovalForm::class, 'form_id');
}
public function line(): BelongsTo
{
return $this->belongsTo(ApprovalLine::class, 'line_id');
}
public function drafter(): BelongsTo
{
return $this->belongsTo(User::class, 'drafter_id');
}
public function steps(): HasMany
{
return $this->hasMany(ApprovalStep::class, 'approval_id')->orderBy('step_order');
}
public function approverSteps(): HasMany
{
return $this->hasMany(ApprovalStep::class, 'approval_id')
->whereIn('step_type', [ApprovalLine::STEP_TYPE_APPROVAL, ApprovalLine::STEP_TYPE_AGREEMENT])
->orderBy('step_order');
}
public function referenceSteps(): HasMany
{
return $this->hasMany(ApprovalStep::class, 'approval_id')
->where('step_type', ApprovalLine::STEP_TYPE_REFERENCE)
->orderBy('step_order');
}
// =========================================================================
// 스코프
// =========================================================================
public function scopeWithStatus($query, string $status)
{
return $query->where('status', $status);
}
public function scopeDraft($query)
{
return $query->where('status', self::STATUS_DRAFT);
}
public function scopePending($query)
{
return $query->where('status', self::STATUS_PENDING);
}
public function scopeApproved($query)
{
return $query->where('status', self::STATUS_APPROVED);
}
public function scopeRejected($query)
{
return $query->where('status', self::STATUS_REJECTED);
}
public function scopeCompleted($query)
{
return $query->whereIn('status', [self::STATUS_APPROVED, self::STATUS_REJECTED, self::STATUS_CANCELLED]);
}
public function scopeByDrafter($query, int $userId)
{
return $query->where('drafter_id', $userId);
}
// =========================================================================
// 헬퍼 메서드
// =========================================================================
public function isEditable(): bool
{
return in_array($this->status, [self::STATUS_DRAFT, self::STATUS_REJECTED]);
}
public function isSubmittable(): bool
{
return in_array($this->status, [self::STATUS_DRAFT, self::STATUS_REJECTED]);
}
public function isActionable(): bool
{
return $this->status === self::STATUS_PENDING;
}
public function isCancellable(): bool
{
return $this->status === self::STATUS_PENDING;
}
public function isDeletable(): bool
{
return $this->status === self::STATUS_DRAFT;
}
public function getStatusLabelAttribute(): string
{
return match ($this->status) {
self::STATUS_DRAFT => '임시저장',
self::STATUS_PENDING => '진행',
self::STATUS_APPROVED => '완료',
self::STATUS_REJECTED => '반려',
self::STATUS_CANCELLED => '회수',
default => $this->status,
};
}
public function getStatusColorAttribute(): string
{
return match ($this->status) {
self::STATUS_DRAFT => 'gray',
self::STATUS_PENDING => 'blue',
self::STATUS_APPROVED => 'green',
self::STATUS_REJECTED => 'red',
self::STATUS_CANCELLED => 'yellow',
default => 'gray',
};
}
public function getCurrentApproverStep(): ?ApprovalStep
{
return $this->steps()
->whereIn('step_type', [ApprovalLine::STEP_TYPE_APPROVAL, ApprovalLine::STEP_TYPE_AGREEMENT])
->where('status', ApprovalStep::STATUS_PENDING)
->orderBy('step_order')
->first();
}
public function isCurrentApprover(int $userId): bool
{
$currentStep = $this->getCurrentApproverStep();
return $currentStep && $currentStep->approver_id === $userId;
}
public function isReferee(int $userId): bool
{
return $this->referenceSteps()
->where('approver_id', $userId)
->exists();
}
public function getProgressAttribute(): array
{
$totalSteps = $this->approverSteps()->count();
$completedSteps = $this->approverSteps()
->whereIn('status', [ApprovalStep::STATUS_APPROVED, ApprovalStep::STATUS_REJECTED])
->count();
return [
'total' => $totalSteps,
'completed' => $completedSteps,
'percentage' => $totalSteps > 0 ? round(($completedSteps / $totalSteps) * 100) : 0,
];
}
}