'작성중', self::STATUS_PENDING => '결재중', self::STATUS_APPROVED => '승인', self::STATUS_REJECTED => '반려', self::STATUS_CANCELLED => '취소', ]; protected $fillable = [ 'tenant_id', 'template_id', 'document_no', 'title', 'status', 'linkable_type', 'linkable_id', 'submitted_at', 'completed_at', 'created_by', 'updated_by', 'deleted_by', ]; protected $casts = [ 'submitted_at' => 'datetime', 'completed_at' => 'datetime', ]; protected $attributes = [ 'status' => self::STATUS_DRAFT, ]; // ========================================================================= // Relationships // ========================================================================= public function template(): BelongsTo { return $this->belongsTo(DocumentTemplate::class, 'template_id'); } public function approvals(): HasMany { return $this->hasMany(DocumentApproval::class)->orderBy('step'); } public function data(): HasMany { return $this->hasMany(DocumentData::class); } public function attachments(): HasMany { return $this->hasMany(DocumentAttachment::class); } public function linkable(): MorphTo { return $this->morphTo(); } public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); } public function updater(): BelongsTo { return $this->belongsTo(User::class, 'updated_by'); } // ========================================================================= // Accessors // ========================================================================= public function getStatusLabelAttribute(): string { return self::STATUS_LABELS[$this->status] ?? $this->status; } public function getStatusColorAttribute(): string { return match ($this->status) { self::STATUS_DRAFT => 'gray', self::STATUS_PENDING => 'yellow', self::STATUS_APPROVED => 'green', self::STATUS_REJECTED => 'red', self::STATUS_CANCELLED => 'gray', default => 'gray', }; } // ========================================================================= // Scopes // ========================================================================= public function scopeStatus($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); } // ========================================================================= // Helper Methods // ========================================================================= public function isDraft(): bool { return $this->status === self::STATUS_DRAFT; } public function isPending(): bool { return $this->status === self::STATUS_PENDING; } public function isApproved(): bool { return $this->status === self::STATUS_APPROVED; } public function isRejected(): bool { return $this->status === self::STATUS_REJECTED; } public function canEdit(): bool { return in_array($this->status, [self::STATUS_DRAFT, self::STATUS_REJECTED]); } }