[모델 업데이트] - Product 모델: 하이브리드 구조 필드로 fillable/casts 간소화 - 고정 필드: safety_stock, lead_time, is_variable_size, product_category, part_type, attributes_archive - 동적 필드: attributes JSON (category_fields로 관리) - 제거: BP-MES 전용 33개 필드 (이제 attributes에 저장) - ProductComponent 모델: BOM 계산 필드 + 동적 필드 - 고정 필드: quantity_formula, condition - 동적 필드: attributes JSON - 제거: is_bending, bending_diagram, bending_details (이제 attributes에 저장) [Seeder 실행] - BpMesCategoryFieldsSeeder: FG/PT/절곡품 카테고리 및 필드 생성 - BpMesTenantStatFieldsSeeder: 통계 필드 설정 (마진율, 가공비, 인건비, 설치비 등) [검증 완료] - Tinker 모델 테스트 통과 - Pint 코드 포맷팅 검사 통과
109 lines
2.8 KiB
PHP
109 lines
2.8 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 BelongsToTenant, ModelTrait, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'tenant_id', 'code', 'name', 'unit', 'category_id',
|
|
'product_type', // 라벨/분류용
|
|
'attributes', 'description',
|
|
'is_sellable', 'is_purchasable', 'is_producible',
|
|
// 하이브리드 구조: 최소 고정 필드
|
|
'safety_stock', 'lead_time', 'is_variable_size',
|
|
'product_category', 'part_type',
|
|
'attributes_archive',
|
|
'created_by', 'updated_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'attributes' => 'array',
|
|
'attributes_archive' => 'array',
|
|
'is_sellable' => 'boolean',
|
|
'is_purchasable' => 'boolean',
|
|
'is_producible' => 'boolean',
|
|
'is_variable_size' => '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);
|
|
}
|
|
}
|