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:
@@ -161,6 +161,22 @@ public function items(): HasMany
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 수주 이력
|
||||
*/
|
||||
|
||||
@@ -26,6 +26,7 @@ class OrderItem extends Model
|
||||
protected $fillable = [
|
||||
'tenant_id',
|
||||
'order_id',
|
||||
'order_node_id',
|
||||
'quote_id',
|
||||
'quote_item_id',
|
||||
'serial_no',
|
||||
@@ -82,6 +83,14 @@ public function order(): BelongsTo
|
||||
return $this->belongsTo(Order::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 수주 노드 (개소/구역/층 등)
|
||||
*/
|
||||
public function node(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(OrderNode::class, 'order_node_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 품목 마스터
|
||||
*/
|
||||
|
||||
127
app/Models/Orders/OrderNode.php
Normal file
127
app/Models/Orders/OrderNode.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user