- Auditable 트레이트 신규 생성 (bootAuditable 패턴) - creating: created_by/updated_by 자동 채우기 - updating: updated_by 자동 채우기 - deleting: deleted_by 채우기 + saveQuietly() - created/updated/deleted: audit_logs 자동 기록 - 기존 AuditLogger 패턴과 동일한 try/catch 조용한 실패 - 변경된 필드만 before/after 기록 (updated 이벤트) - auditExclude 프로퍼티로 모델별 제외 필드 설정 가능 - 제외 대상: Attendance, StockTransaction, TodayIssue 등 고빈도/시스템 모델 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
120 lines
3.3 KiB
PHP
120 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Products;
|
|
|
|
use App\Models\Commons\Category;
|
|
use App\Models\Commons\File;
|
|
use App\Models\Commons\Tag;
|
|
use App\Traits\Auditable;
|
|
use App\Traits\BelongsToTenant;
|
|
use App\Traits\ModelTrait;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Product extends Model
|
|
{
|
|
use Auditable, BelongsToTenant, ModelTrait, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'tenant_id', 'code', 'name', 'unit', 'category_id',
|
|
'product_type', // 라벨/분류용
|
|
'attributes', 'attributes_archive', 'options', 'bom', 'description',
|
|
'is_sellable', 'is_purchasable', 'is_producible',
|
|
// 하이브리드 구조: 최소 고정 필드
|
|
'safety_stock', 'lead_time', 'is_variable_size',
|
|
'product_category', 'part_type',
|
|
// 파일 필드
|
|
'bending_diagram', 'bending_details',
|
|
'specification_file', 'specification_file_name',
|
|
'certification_file', 'certification_file_name',
|
|
'certification_number', 'certification_start_date', 'certification_end_date',
|
|
'created_by', 'updated_by', 'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'attributes' => 'array',
|
|
'attributes_archive' => 'array',
|
|
'options' => 'array',
|
|
'bom' => 'array',
|
|
'bending_details' => 'array',
|
|
'certification_start_date' => 'date',
|
|
'certification_end_date' => 'date',
|
|
'is_sellable' => 'boolean',
|
|
'is_purchasable' => 'boolean',
|
|
'is_producible' => 'boolean',
|
|
'is_variable_size' => 'boolean',
|
|
'is_active' => 'boolean',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'deleted_at',
|
|
];
|
|
|
|
// 분류
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(Category::class, 'category_id');
|
|
}
|
|
|
|
// BOM (자기참조) — 라인 모델 경유
|
|
public function componentLines()
|
|
{
|
|
return $this->hasMany(ProductComponent::class, 'parent_product_id')->orderBy('sort_order');
|
|
}
|
|
|
|
// 라인들
|
|
public function parentLines()
|
|
{
|
|
return $this->hasMany(ProductComponent::class, 'child_product_id');
|
|
} // 나를 쓰는 상위 라인들
|
|
|
|
// 편의: 직접 children/parents 제품에 접근
|
|
public function children()
|
|
{
|
|
return $this->belongsToMany(
|
|
self::class, 'product_components', 'parent_product_id', 'child_product_id'
|
|
)->withPivot(['quantity', 'sort_order', 'is_default'])
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function parents()
|
|
{
|
|
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);
|
|
}
|
|
}
|