Files
sam-api/app/Models/Tenants/Tenant.php
hskwon d94ab59fd1 refactor: 모델 개선 및 관계 정의 수정
- Material 모델
  - fillable 속성 추가 (모든 필드 명시)

- User 모델
  - BelongsToMany 관계 타입 힌트 추가

- Product 모델
  - fillable에 unit 필드 추가
  - casts 순서 정리 (boolean 그룹화)

- ProductComponent 모델
  - quantity 캐스트 정밀도 변경 (decimal:4 → decimal:6)
  - referencedItem() 메서드 추가 (동적 관계 로드)
  - product(), material() 관계 메서드 수정 (where 조건 추가)
  - is_default 캐스트 제거 (컬럼 없음)

- Tenant 모델
  - options 캐스트 추가 (array)
  - scopeActive() 추가 (trial, active 상태 필터링)
  - isActive(), isTrial() 헬퍼 메서드 추가

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-30 14:35:26 +09:00

123 lines
2.5 KiB
PHP

<?php
namespace App\Models\Tenants;
use App\Models\Commons\File;
use App\Models\Members\User;
use App\Models\Members\UserRole;
use App\Models\Members\UserTenant;
use App\Models\Permissions\Role;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @mixin IdeHelperTenant
*/
class Tenant extends Model
{
use SoftDeletes, ModelTrait;
protected $fillable = [
'company_name',
'code',
'email',
'phone',
'address',
'business_num',
'corp_reg_no',
'ceo_name',
'homepage',
'fax',
'logo',
'admin_memo',
'options',
'tenant_st_code',
'billing_tp_code',
];
protected $guarded = [
'id',
'created_at',
'updated_at',
'deleted_at',
'plan_id',
'subscription_id',
];
protected $casts = [
'trial_ends_at' => 'datetime',
'expires_at' => 'datetime',
'last_paid_at' => 'datetime',
'max_users' => 'integer',
'options' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
protected $hidden = [
'deleted_at',
];
/**
* 활성화된 테넌트만 조회하는 스코프
*/
public function scopeActive($query)
{
return $query->whereIn('tenant_st_code', ['trial', 'active']);
}
/**
* 테넌트가 활성 상태인지 확인
*/
public function isActive(): bool
{
return in_array($this->tenant_st_code, ['trial', 'active']);
}
/**
* 테넌트가 트라이얼 상태인지 확인
*/
public function isTrial(): bool
{
return $this->tenant_st_code === 'trial';
}
// 관계 정의 (예시)
public function plan()
{
return $this->belongsTo(Plan::class, 'plan_id');
}
public function subscription()
{
return $this->belongsTo(Subscription::class, 'subscription_id');
}
public function userTenants()
{
return $this->hasMany(UserTenant::class);
}
public function users()
{
return $this->belongsToMany(User::class, 'user_tenants');
}
public function roles()
{
return $this->hasMany(Role::class);
}
public function userRoles()
{
return $this->hasMany(UserRole::class);
}
public function files()
{
return $this->morphMany(File::class, 'fileable');
}
}