Files
sam-api/app/Models/Products/Product.php
hskwon d5bfb24ef9 feat: BP-MES Phase 1 - products/product_components 테이블 확장
- products 테이블에 33개 필드 추가
  - 공통: is_active, margin_rate, costs, safety_stock, lead_time, is_variable_size
  - FG 전용: product_category, lot_abbreviation, note
  - PT 전용: part_type, part_usage, installation_type, assembly_type,
    side_spec_width, side_spec_height, assembly_length,
    guide_rail_model_type, guide_rail_model
  - 절곡품: bending_diagram, bending_details, material, length, bending_length
  - 인증: certification_number, start/end_date, specification/certification files
  - 동적 확장: options (JSON)

- product_components 테이블에 5개 필드 추가
  - 수식 계산: quantity_formula
  - 조건부 BOM: condition
  - 절곡품: is_bending, bending_diagram, bending_details

- Product/ProductComponent 모델 fillable/casts 업데이트
- 인덱스 추가: is_active, product_category, part_type, part_usage, is_bending
2025-11-14 09:07:33 +09:00

129 lines
3.7 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', 'is_active',
// BP-MES: 공통 필드
'margin_rate', 'processing_cost', 'labor_cost', 'install_cost',
'safety_stock', 'lead_time', 'is_variable_size',
// BP-MES: FG 전용
'product_category', 'lot_abbreviation', 'note',
// BP-MES: PT 전용
'part_type', 'part_usage', 'installation_type', 'assembly_type',
'side_spec_width', 'side_spec_height', 'assembly_length',
'guide_rail_model_type', 'guide_rail_model',
// BP-MES: 절곡품
'bending_diagram', 'bending_details', 'material', 'length', 'bending_length',
// BP-MES: 인증
'certification_number', 'certification_start_date', 'certification_end_date',
'specification_file', 'specification_file_name',
'certification_file', 'certification_file_name',
// BP-MES: 동적 확장
'options',
'created_by', 'updated_by',
];
protected $casts = [
'attributes' => 'array',
'is_sellable' => 'boolean',
'is_purchasable' => 'boolean',
'is_producible' => 'boolean',
'is_active' => 'boolean',
// BP-MES: 추가 boolean 필드
'is_variable_size' => 'boolean',
// BP-MES: JSON 필드
'bending_details' => 'array',
'options' => 'array',
// BP-MES: Date 필드
'certification_start_date' => 'date',
'certification_end_date' => 'date',
];
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);
}
}