Files
sam-manage/app/Models/Barobill/BarobillSubscription.php
2026-02-25 11:45:01 +09:00

122 lines
2.7 KiB
PHP

<?php
namespace App\Models\Barobill;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 바로빌 월정액 구독 모델
*/
class BarobillSubscription extends Model
{
use SoftDeletes;
protected $table = 'barobill_subscriptions';
protected $fillable = [
'member_id',
'service_type',
'monthly_fee',
'started_at',
'ended_at',
'is_active',
'memo',
];
protected $casts = [
'monthly_fee' => 'integer',
'started_at' => 'date',
'ended_at' => 'date',
'is_active' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
/**
* 서비스 유형 라벨
*/
public const SERVICE_TYPES = [
'bank_account' => '계좌조회',
'card' => '카드내역',
'hometax' => '홈텍스 매입/매출',
];
/**
* 기본 월정액 (원)
*/
public const DEFAULT_MONTHLY_FEES = [
'bank_account' => 10000,
'card' => 10000,
'hometax' => 0, // 본사 부담 (무료 제공)
];
/**
* 바로빌 회원사 관계
*/
public function member(): BelongsTo
{
return $this->belongsTo(BarobillMember::class, 'member_id');
}
/**
* 서비스 유형 라벨
*/
public function getServiceTypeLabelAttribute(): string
{
return self::SERVICE_TYPES[$this->service_type] ?? $this->service_type;
}
/**
* 상태 라벨
*/
public function getStatusLabelAttribute(): string
{
if (! $this->is_active) {
return '비활성';
}
if ($this->ended_at && $this->ended_at->isPast()) {
return '종료';
}
return '구독중';
}
/**
* 상태 색상 클래스
*/
public function getStatusColorAttribute(): string
{
if (! $this->is_active) {
return 'bg-gray-100 text-gray-800';
}
if ($this->ended_at && $this->ended_at->isPast()) {
return 'bg-red-100 text-red-800';
}
return 'bg-green-100 text-green-800';
}
/**
* 활성 구독만 조회
*/
public function scopeActive($query)
{
return $query->where('is_active', true)
->where(function ($q) {
$q->whereNull('ended_at')
->orWhere('ended_at', '>=', now()->toDateString());
});
}
/**
* 특정 서비스 유형 조회
*/
public function scopeOfService($query, string $serviceType)
{
return $query->where('service_type', $serviceType);
}
}