'대기중', self::STATUS_APPROVED => '승인', self::STATUS_REJECTED => '반려', ]; // ========================================================================= // 모델 설정 // ========================================================================= protected $fillable = [ 'user_id', 'business_number', 'company_name', 'ceo_name', 'address', 'phone', 'email', 'status', 'message', 'reject_reason', 'barobill_response', 'approved_by', 'created_tenant_id', 'processed_at', ]; protected $casts = [ 'barobill_response' => 'array', 'processed_at' => 'datetime', ]; protected $attributes = [ 'status' => self::STATUS_PENDING, ]; // ========================================================================= // 관계 // ========================================================================= /** * 신청자 */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * 처리자 (승인/반려) */ public function approver(): BelongsTo { return $this->belongsTo(User::class, 'approved_by'); } /** * 생성된 테넌트 */ public function createdTenant(): BelongsTo { return $this->belongsTo(Tenant::class, 'created_tenant_id'); } // ========================================================================= // 스코프 // ========================================================================= /** * 대기중인 신청만 */ 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 scopeOfStatus($query, string $status) { return $query->where('status', $status); } // ========================================================================= // 접근자 // ========================================================================= /** * 상태 라벨 */ public function getStatusLabelAttribute(): string { return self::STATUS_LABELS[$this->status] ?? $this->status; } /** * 대기중 여부 */ public function getIsPendingAttribute(): bool { return $this->status === self::STATUS_PENDING; } /** * 승인됨 여부 */ public function getIsApprovedAttribute(): bool { return $this->status === self::STATUS_APPROVED; } /** * 반려됨 여부 */ public function getIsRejectedAttribute(): bool { return $this->status === self::STATUS_REJECTED; } /** * 사업자등록번호 포맷 (하이픈 포함) */ public function getFormattedBusinessNumberAttribute(): string { $num = preg_replace('/[^0-9]/', '', $this->business_number); if (strlen($num) === 10) { return substr($num, 0, 3).'-'.substr($num, 3, 2).'-'.substr($num, 5, 5); } return $this->business_number; } // ========================================================================= // 헬퍼 메서드 // ========================================================================= /** * 승인 처리 */ public function approve(int $approverId, int $tenantId): bool { if (! $this->is_pending) { return false; } $this->status = self::STATUS_APPROVED; $this->approved_by = $approverId; $this->created_tenant_id = $tenantId; $this->processed_at = now(); return $this->save(); } /** * 반려 처리 */ public function reject(int $approverId, ?string $reason = null): bool { if (! $this->is_pending) { return false; } $this->status = self::STATUS_REJECTED; $this->approved_by = $approverId; $this->reject_reason = $reason; $this->processed_at = now(); return $this->save(); } }