Files
sam-manage/app/Models/Items/Item.php
권혁성 f271f8bdc3 feat:품목관리 3-Panel 페이지 신규 구현 + FormulaEvaluatorService 연동
- 품목관리 3-Panel 레이아웃 (좌:목록, 중:BOM/수식산출, 우:상세)
- FormulaApiService로 API 견적수식 엔진 연동
- FG 품목 선택 시 기본값(W:1000, H:1000, QTY:1) 자동 산출
- 수식 산출 결과 트리 렌더링 (그룹별/소계/합계)
- 중앙 패널 클릭 시 우측 상세만 변경 (skipCenterUpdate)
- API 인증 버튼 전역 헤더로 이동 (모든 페이지에서 사용 가능)
- FormulaApiService에 Bearer 토큰 지원 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 10:50:24 +09:00

100 lines
2.5 KiB
PHP

<?php
namespace App\Models\Items;
use App\Models\Category;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Item extends Model
{
use BelongsToTenant, SoftDeletes;
protected $table = 'items';
protected $fillable = [
'tenant_id', 'item_type', 'item_category', 'code', 'name', 'unit',
'category_id', 'bom', 'attributes', 'attributes_archive', 'options',
'description', 'is_active', 'created_by', 'updated_by',
];
protected $casts = [
'bom' => 'array',
'attributes' => 'array',
'attributes_archive' => 'array',
'options' => 'array',
'is_active' => 'boolean',
];
// 유형 상수
const TYPE_FG = 'FG'; // 완제품
const TYPE_PT = 'PT'; // 부품
const TYPE_SM = 'SM'; // 부자재
const TYPE_RM = 'RM'; // 원자재
const TYPE_CS = 'CS'; // 소모품
const PRODUCT_TYPES = ['FG', 'PT'];
const MATERIAL_TYPES = ['SM', 'RM', 'CS'];
// ── 관계 ──
public function details()
{
return $this->hasOne(ItemDetail::class, 'item_id');
}
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
/**
* 파일 (document_id/document_type 기반)
* document_id = items.id, document_type = '1' (ITEM_GROUP_ID)
*/
public function files()
{
return $this->hasMany(\App\Models\Commons\File::class, 'document_id')
->where('document_type', '1');
}
// ── 스코프 ──
public function scopeType($query, string $type)
{
return $query->where('items.item_type', strtoupper($type));
}
public function scopeActive($query)
{
return $query->where('is_active', true);
}
public function scopeSearch($query, ?string $search)
{
if (!$search) return $query;
return $query->where(function ($q) use ($search) {
$q->where('code', 'like', "%{$search}%")
->orWhere('name', 'like', "%{$search}%");
});
}
// ── 헬퍼 ──
public function isProduct(): bool
{
return in_array($this->item_type, self::PRODUCT_TYPES);
}
public function isMaterial(): bool
{
return in_array($this->item_type, self::MATERIAL_TYPES);
}
public function getBomChildIds(): array
{
return collect($this->bom ?? [])->pluck('child_item_id')->filter()->toArray();
}
}