Files
sam-api/app/Models/Products/ProductComponent.php

94 lines
2.0 KiB
PHP

<?php
namespace App\Models\Products;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\ModelTrait;
use App\Traits\BelongsToTenant;
class ProductComponent extends Model
{
use SoftDeletes, ModelTrait, BelongsToTenant;
protected $table = 'product_components';
protected $fillable = [
'tenant_id',
'parent_product_id',
'ref_type',
'child_product_id',
'material_id',
'quantity',
'sort_order',
'is_default',
'created_by',
'updated_by',
];
protected $casts = [
'quantity' => 'decimal:4',
'is_default' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
protected $hidden = [
'deleted_at',
];
/**
* 상위 제품 (모델/제품)
*/
public function parentProduct()
{
return $this->belongsTo(Product::class, 'parent_product_id');
}
/**
* 하위 제품 (ref_type = PRODUCT일 때만 의미 있음)
*/
public function childProduct()
{
return $this->belongsTo(Product::class, 'child_product_id');
}
/**
* 하위 자재 (ref_type = MATERIAL일 때만 의미 있음)
*/
public function material()
{
return $this->belongsTo(\App\Models\Materials\Material::class, 'material_id');
}
// ---------------------------------------------------
// 🔎 Query Scopes
// ---------------------------------------------------
/**
* 제품 BOM 아이템만
*/
public function scopeProducts($query)
{
return $query->where('ref_type', 'PRODUCT');
}
/**
* 자재 BOM 아이템만
*/
public function scopeMaterials($query)
{
return $query->where('ref_type', 'MATERIAL');
}
/**
* 특정 상위 제품의 BOM
*/
public function scopeForParent($query, int $parentProductId)
{
return $query->where('parent_product_id', $parentProductId);
}
}