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>
This commit is contained in:
2026-02-06 20:06:14 +09:00
parent 4ae7b438f1
commit 874bf97b8f
5 changed files with 235 additions and 0 deletions

View File

@@ -161,6 +161,22 @@ public function items(): HasMany
return $this->hasMany(OrderItem::class)->orderBy('sort_order'); return $this->hasMany(OrderItem::class)->orderBy('sort_order');
} }
/**
* 수주 노드 (전체, depth/sort_order 순)
*/
public function nodes(): HasMany
{
return $this->hasMany(OrderNode::class)->orderBy('depth')->orderBy('sort_order');
}
/**
* 수주 루트 노드 (parent_id가 NULL인 최상위)
*/
public function rootNodes(): HasMany
{
return $this->hasMany(OrderNode::class)->whereNull('parent_id')->orderBy('sort_order');
}
/** /**
* 수주 이력 * 수주 이력
*/ */

View File

@@ -26,6 +26,7 @@ class OrderItem extends Model
protected $fillable = [ protected $fillable = [
'tenant_id', 'tenant_id',
'order_id', 'order_id',
'order_node_id',
'quote_id', 'quote_id',
'quote_item_id', 'quote_item_id',
'serial_no', 'serial_no',
@@ -82,6 +83,14 @@ public function order(): BelongsTo
return $this->belongsTo(Order::class); return $this->belongsTo(Order::class);
} }
/**
* 수주 노드 (개소/구역/층 등)
*/
public function node(): BelongsTo
{
return $this->belongsTo(OrderNode::class, 'order_node_id');
}
/** /**
* 품목 마스터 * 품목 마스터
*/ */

View File

@@ -0,0 +1,127 @@
<?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']);
}
}

View File

@@ -0,0 +1,56 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('order_nodes', function (Blueprint $table) {
$table->id()->comment('ID');
$table->foreignId('tenant_id')->comment('테넌트 ID');
$table->foreignId('order_id')->comment('수주 ID');
// ---- 트리 구조 ----
$table->foreignId('parent_id')->nullable()->comment('상위 노드 ID (NULL=루트)');
// ---- 고정 코어 (통계/집계용) ----
$table->string('node_type', 50)->comment('노드 유형 (location, zone, floor, room, process...)');
$table->string('code', 100)->comment('식별 코드');
$table->string('name', 200)->comment('표시명');
$table->string('status_code', 30)->default('PENDING')
->comment('상태 (PENDING/CONFIRMED/IN_PRODUCTION/PRODUCED/SHIPPED/COMPLETED/CANCELLED)');
$table->integer('quantity')->default(1)->comment('수량');
$table->decimal('unit_price', 15, 2)->default(0)->comment('단가');
$table->decimal('total_price', 15, 2)->default(0)->comment('합계');
// ---- 유연 확장 (유형별 상세) ----
$table->json('options')->nullable()->comment('유형별 동적 속성 JSON');
// ---- 정렬 ----
$table->integer('depth')->default(0)->comment('트리 깊이 (0=루트)');
$table->integer('sort_order')->default(0)->comment('정렬 순서');
// ---- 감사 ----
$table->foreignId('created_by')->nullable()->comment('생성자 ID');
$table->foreignId('updated_by')->nullable()->comment('수정자 ID');
$table->foreignId('deleted_by')->nullable()->comment('삭제자 ID');
$table->timestamps();
$table->softDeletes();
// ---- 인덱스 ----
$table->index('tenant_id');
$table->index('parent_id');
$table->index(['order_id', 'depth', 'sort_order']);
$table->index(['order_id', 'node_type']);
$table->index(['tenant_id', 'node_type', 'status_code']); // 통계용
});
}
public function down(): void
{
Schema::dropIfExists('order_nodes');
}
};

View File

@@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('order_items', function (Blueprint $table) {
$table->foreignId('order_node_id')
->nullable()
->after('order_id')
->comment('수주 노드 ID (order_nodes)');
$table->index('order_node_id');
});
}
public function down(): void
{
Schema::table('order_items', function (Blueprint $table) {
$table->dropIndex(['order_node_id']);
$table->dropColumn('order_node_id');
});
}
};