Files
sam-api/app/Models/Orders/OrderNode.php
권혁성 874bf97b8f feat: [수주관리] order_nodes 테이블 및 모델 생성 (N-depth 트리 구조)
- order_nodes 마이그레이션: 자기참조 parent_id, 고정코어(통계용) + options JSON(하이브리드)
- order_items에 order_node_id nullable FK 추가
- OrderNode 모델: BelongsToTenant, Auditable, SoftDeletes, 트리 관계(parent/children)
- Order 모델: nodes(), rootNodes() HasMany 관계 추가
- OrderItem 모델: order_node_id fillable + node() BelongsTo 관계 추가

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

128 lines
2.9 KiB
PHP

<?php
namespace App\Models\Orders;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 수주 노드 (Order Nodes)
*
* N-depth 자기참조 트리 구조로 수주 하위 단위(개소/구역/층/실/공정 등)를 관리.
* 고정 코어 컬럼(통계/집계용) + options JSON(유형별 동적 속성) 하이브리드 패턴.
*/
class OrderNode extends Model
{
use Auditable, BelongsToTenant, SoftDeletes;
protected $table = 'order_nodes';
// 상태 코드 (Order와 동일 체계)
public const STATUS_PENDING = 'PENDING';
public const STATUS_CONFIRMED = 'CONFIRMED';
public const STATUS_IN_PRODUCTION = 'IN_PRODUCTION';
public const STATUS_PRODUCED = 'PRODUCED';
public const STATUS_SHIPPED = 'SHIPPED';
public const STATUS_COMPLETED = 'COMPLETED';
public const STATUS_CANCELLED = 'CANCELLED';
protected $fillable = [
'tenant_id',
'order_id',
'parent_id',
'node_type',
'code',
'name',
'status_code',
'quantity',
'unit_price',
'total_price',
'options',
'depth',
'sort_order',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'quantity' => 'integer',
'unit_price' => 'decimal:2',
'total_price' => 'decimal:2',
'options' => 'array',
'depth' => 'integer',
'sort_order' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
// ---- 트리 관계 ----
/**
* 상위 노드
*/
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
/**
* 하위 노드
*/
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id')->orderBy('sort_order');
}
// ---- 비즈니스 관계 ----
/**
* 수주 마스터
*/
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
/**
* 해당 노드에 속한 수주 품목
*/
public function items(): HasMany
{
return $this->hasMany(OrderItem::class, 'order_node_id');
}
// ---- 트리 헬퍼 ----
public function isRoot(): bool
{
return $this->parent_id === null;
}
public function isLeaf(): bool
{
return $this->children()->count() === 0;
}
/**
* 하위 노드 포함 전체 트리 재귀 로드
*/
public function scopeWithRecursiveChildren($query)
{
return $query->with(['children' => function ($q) {
$q->orderBy('sort_order')->with('children', 'items');
}, 'items']);
}
}