Files
sam-api/app/Models/Products/Product.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

79 lines
2.5 KiB
PHP

<?php
namespace App\Models\Products;
use App\Models\Commons\Category;
use App\Models\Commons\File;
use App\Models\Commons\Tag;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
use SoftDeletes, BelongsToTenant, ModelTrait;
protected $fillable = [
'tenant_id','code','name','unit','category_id',
'product_type', // 라벨/분류용
'attributes','description',
'is_sellable','is_purchasable','is_producible','is_active',
'created_by','updated_by'
];
protected $casts = [
'attributes' => 'array',
'is_sellable' => 'boolean',
'is_purchasable' => 'boolean',
'is_producible' => 'boolean',
'is_active' => 'boolean',
];
protected $hidden = [
'deleted_at',
];
// 분류
public function category() { return $this->belongsTo(Category::class, 'category_id'); }
// BOM (자기참조) — 라인 모델 경유
public function componentLines()
{
return $this->hasMany(ProductComponent::class, 'parent_product_id')->orderBy('sort_order');
}
// 라인들
public function parentLines()
{
return $this->hasMany(ProductComponent::class, 'child_product_id');
} // 나를 쓰는 상위 라인들
// 편의: 직접 children/parents 제품에 접근
public function children()
{
return $this->belongsToMany(
self::class, 'product_components', 'parent_product_id', 'child_product_id'
)->withPivot(['quantity','sort_order','is_default'])
->withTimestamps();
}
public function parents()
{
return $this->belongsToMany(
self::class, 'product_components', 'child_product_id', 'parent_product_id'
)->withPivot(['quantity','sort_order','is_default'])
->withTimestamps();
}
// 파일 / 태그 (폴리모픽)
public function files() { return $this->morphMany(File::class, 'fileable'); }
public function tags() { return $this->morphToMany(Tag::class, 'taggable'); }
// 스코프
public function scopeType($q, string $type) { return $q->where('product_type', $type); }
public function scopeSellable($q) { return $q->where('is_sellable', 1); }
public function scopePurchasable($q) { return $q->where('is_purchasable', 1); }
public function scopeProducible($q) { return $q->where('is_producible', 1); }
}