fix : Category, Product 관련 테이블 정리 (Models, DB, seeder)

This commit is contained in:
2025-08-22 15:57:14 +09:00
parent 5f62179473
commit 189bdbfd80
10 changed files with 569 additions and 45 deletions

View File

@@ -3,44 +3,37 @@
namespace App\Models\Commons;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @mixin IdeHelperCategory
*/
class Category extends Model
{
use SoftDeletes, BelongsToTenant;
protected $table = 'common_codes';
use SoftDeletes, BelongsToTenant, ModelTrait;
protected $fillable = [
'tenant_id',
'code_group',
'code',
'name',
'parent_id',
'attributes',
'description',
'is_active',
'sort_order'
'tenant_id','parent_id','code_group','code','name',
'profile_code', // capability_profile 연결
'is_active','sort_order','description',
'created_by','updated_by','deleted_by'
];
protected $casts = [
'attributes' => 'array',
'is_active' => 'boolean',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
// 관계: 상위 코드
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}
// 계층
public function parent() { return $this->belongsTo(self::class, 'parent_id'); }
public function children() { return $this->hasMany(self::class, 'parent_id'); }
// 관계: 하위 코드
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
// 카테고리의 제품
public function products() { return $this->hasMany(\App\Models\Products\Product::class, 'category_id'); }
// 태그(폴리모픽) — 이미 taggables 존재
public function tags() { return $this->morphToMany(\App\Models\Commons\Tag::class, 'taggable'); }
// 스코프
public function scopeGroup($q, string $group) { return $q->where('code_group', $group); }
public function scopeCode($q, string $code) { return $q->where('code', $code); }
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Models\Commons;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class CategoryField extends Model
{
use SoftDeletes, BelongsToTenant, ModelTrait;
protected $table = 'category_fields';
protected $fillable = [
'tenant_id','category_id',
'field_key','field_name','field_type',
'is_required','sort_order','default_value','options','description',
'created_by','updated_by','deleted_by',
];
protected $casts = [
'is_required' => 'boolean',
'sort_order' => 'integer',
'options' => 'array',
];
public function category()
{
return $this->belongsTo(Category::class);
}
// 편의 스코프
public function scopeRequired($q) { return $q->where('is_required', 1); }
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Models\Commons;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
class CategoryLog extends Model
{
use BelongsToTenant, ModelTrait;
protected $table = 'category_logs';
public $timestamps = false; // changed_at 컬럼 단일 사용
protected $fillable = [
'category_id','tenant_id','action','changed_by','changed_at',
'before_json','after_json','remarks',
];
protected $casts = [
'changed_at' => 'datetime',
'before_json' => 'array',
'after_json' => 'array',
];
public function category()
{
return $this->belongsTo(Category::class);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Commons;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
class CategoryTemplate extends Model
{
use BelongsToTenant, ModelTrait;
protected $table = 'category_templates';
protected $fillable = [
'tenant_id','category_id','version_no','template_json','applied_at',
'created_by','updated_by','deleted_by','remarks',
];
protected $casts = [
'version_no' => 'integer',
'template_json' => 'array',
'applied_at' => 'datetime',
];
public function category()
{
return $this->belongsTo(Category::class);
}
}

View File

@@ -5,12 +5,13 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
/**
* @mixin IdeHelperCommonCode
*/
class CommonCode extends Model
{
use SoftDeletes, BelongsToTenant;
use SoftDeletes, BelongsToTenant, ModelTrait;
protected $table = 'common_codes';

View File

@@ -2,35 +2,65 @@
namespace App\Models\Products;
use App\Models\Commons\Category;
use App\Models\Commons\File;
use App\Models\Commons\Tag;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @mixin IdeHelperProduct
*/
class Product extends Model
{
use SoftDeletes;
protected $fillable = ['tenant_id','code','name','category_id','attributes','description','is_active'];
use SoftDeletes, BelongsToTenant, ModelTrait;
public function category() {
return $this->belongsTo(CommonCode::class, 'category_id');
}
public function boms() {
return $this->hasMany(Bom::class);
}
protected $fillable = [
'tenant_id','code','name','category_id',
'product_type', // 라벨/분류용
'attributes','description','is_active',
'is_sellable','is_purchasable','is_producible',
'created_by','updated_by'
];
// 파일 목록 (N:M, 폴리모픽)
public function files()
protected $casts = [
'attributes' => 'array',
'is_active' => 'boolean',
'is_sellable' => 'boolean',
'is_purchasable' => 'boolean',
'is_producible' => 'boolean',
];
// 분류
public function category() { return $this->belongsTo(Category::class, 'category_id'); }
// BOM (자기참조) — 라인 모델 경유
public function componentLines() { return $this->hasMany(ProductComponent::class, 'parent_product_id'); } // 라인들
public function parentLines() { return $this->hasMany(ProductComponent::class, 'child_product_id'); } // 나를 쓰는 상위 라인들
// 편의: 직접 children/parents 제품에 접근
public function children()
{
return $this->morphMany(File::class, 'fileable');
return $this->belongsToMany(
self::class, 'product_components', 'parent_product_id', 'child_product_id'
)->withPivot(['quantity','sort_order','is_default'])
->withTimestamps();
}
// 태그 목록 (N:M, 폴리모픽)
public function tags()
public function parents()
{
return $this->morphToMany(Tag::class, 'taggable');
return $this->belongsToMany(
self::class, 'product_components', 'child_product_id', 'parent_product_id'
)->withPivot(['quantity','sort_order','is_default'])
->withTimestamps();
}
// 파일 / 태그 (폴리모픽)
public function files() { return $this->morphMany(File::class, 'fileable'); }
public function tags() { return $this->morphToMany(Tag::class, 'taggable'); }
// 스코프
public function scopeType($q, string $type) { return $q->where('product_type', $type); }
public function scopeSellable($q) { return $q->where('is_sellable', 1); }
public function scopePurchasable($q) { return $q->where('is_purchasable', 1); }
public function scopeProducible($q) { return $q->where('is_producible', 1); }
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models\Products;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class ProductComponent extends Model
{
use SoftDeletes, BelongsToTenant, ModelTrait;
protected $table = 'product_components';
protected $fillable = [
'tenant_id','parent_product_id','child_product_id',
'quantity','sort_order','is_default',
'created_by','updated_by'
];
protected $casts = [
'quantity' => 'decimal:4',
'sort_order' => 'integer',
'is_default' => 'boolean',
];
public function parent() { return $this->belongsTo(Product::class, 'parent_product_id'); }
public function child() { return $this->belongsTo(Product::class, 'child_product_id'); }
}