refactor: 견적 산출 서비스 DB 기반으로 재작성

- Quote 수식 모델 추가 (mng 패턴 적용)
  - QuoteFormula: 수식 정의 (input/calculation/range/mapping)
  - QuoteFormulaCategory: 카테고리 정의
  - QuoteFormulaItem: 품목 출력 정의
  - QuoteFormulaRange: 범위별 값 정의
  - QuoteFormulaMapping: 매핑 값 정의

- FormulaEvaluatorService 확장
  - executeAll(): 카테고리별 수식 실행
  - evaluateRangeFormula/evaluateMappingFormula: QuoteFormula 기반 평가
  - getItemPrice(): prices 테이블 연동

- QuoteCalculationService DB 기반으로 재작성
  - 하드코딩된 품목 코드/로직 제거
  - quote_formulas 테이블 기반 동적 계산
  - getInputSchema(): DB 기반 입력 스키마 생성

- Price 모델 수정
  - items 테이블 연동 (products/materials 대체)
  - ITEM_TYPE 상수 업데이트 (FG/PT/RM/SM/CS)
This commit is contained in:
2025-12-19 16:49:26 +09:00
parent 21d4d0d1b1
commit 0d49e4cc75
8 changed files with 838 additions and 361 deletions

View File

@@ -0,0 +1,219 @@
<?php
namespace App\Models\Quote;
use App\Models\User;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 견적 수식 모델
*
* @property int $id
* @property int $tenant_id
* @property int $category_id
* @property int|null $product_id
* @property string $name
* @property string $variable
* @property string $type
* @property string|null $formula
* @property string $output_type
* @property string|null $description
* @property int $sort_order
* @property bool $is_active
*/
class QuoteFormula extends Model
{
use BelongsToTenant, ModelTrait, SoftDeletes;
protected $table = 'quote_formulas';
protected $fillable = [
'tenant_id',
'category_id',
'product_id',
'name',
'variable',
'type',
'formula',
'output_type',
'description',
'sort_order',
'is_active',
'created_by',
'updated_by',
];
protected $casts = [
'is_active' => 'boolean',
'sort_order' => 'integer',
];
protected $attributes = [
'type' => 'calculation',
'output_type' => 'variable',
'sort_order' => 0,
'is_active' => true,
];
// 수식 유형 상수
public const TYPE_INPUT = 'input';
public const TYPE_CALCULATION = 'calculation';
public const TYPE_RANGE = 'range';
public const TYPE_MAPPING = 'mapping';
// 출력 유형 상수
public const OUTPUT_VARIABLE = 'variable';
public const OUTPUT_ITEM = 'item';
// =========================================================================
// Relationships
// =========================================================================
/**
* 카테고리 관계
*/
public function category(): BelongsTo
{
return $this->belongsTo(QuoteFormulaCategory::class, 'category_id');
}
/**
* 범위 규칙
*/
public function ranges(): HasMany
{
return $this->hasMany(QuoteFormulaRange::class, 'formula_id')
->orderBy('sort_order');
}
/**
* 매핑 규칙
*/
public function mappings(): HasMany
{
return $this->hasMany(QuoteFormulaMapping::class, 'formula_id')
->orderBy('sort_order');
}
/**
* 품목 출력
*/
public function items(): HasMany
{
return $this->hasMany(QuoteFormulaItem::class, 'formula_id')
->orderBy('sort_order');
}
/**
* 생성자
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* 수정자
*/
public function updater(): BelongsTo
{
return $this->belongsTo(User::class, 'updated_by');
}
// =========================================================================
// Scopes
// =========================================================================
/**
* 수식이 공통 수식인지 확인
*/
public function isCommon(): bool
{
return is_null($this->product_id);
}
/**
* Scope: 공통 수식
*/
public function scopeCommon(Builder $query): Builder
{
return $query->whereNull('product_id');
}
/**
* Scope: 특정 제품 수식 (공통 + 제품 전용)
*/
public function scopeForProduct(Builder $query, ?int $productId): Builder
{
return $query->where(function ($q) use ($productId) {
$q->whereNull('product_id');
if ($productId) {
$q->orWhere('product_id', $productId);
}
});
}
/**
* Scope: 활성화된 수식
*/
public function scopeActive(Builder $query): Builder
{
return $query->where('is_active', true);
}
/**
* Scope: 정렬 순서
*/
public function scopeOrdered(Builder $query): Builder
{
return $query->orderBy('sort_order');
}
/**
* Scope: 유형별 필터
*/
public function scopeOfType(Builder $query, string $type): Builder
{
return $query->where('type', $type);
}
// =========================================================================
// Helper Methods
// =========================================================================
/**
* 유형 레이블 조회
*/
public function getTypeLabelAttribute(): string
{
return match ($this->type) {
self::TYPE_INPUT => '입력값',
self::TYPE_CALCULATION => '계산식',
self::TYPE_RANGE => '범위별',
self::TYPE_MAPPING => '매핑',
default => $this->type,
};
}
/**
* 출력 유형 레이블 조회
*/
public function getOutputTypeLabelAttribute(): string
{
return match ($this->output_type) {
self::OUTPUT_VARIABLE => '변수',
self::OUTPUT_ITEM => '품목',
default => $this->output_type,
};
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace App\Models\Quote;
use App\Models\User;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 견적 수식 카테고리 모델
*
* @property int $id
* @property int $tenant_id
* @property string $code
* @property string $name
* @property string|null $description
* @property int $sort_order
* @property bool $is_active
* @property int|null $created_by
* @property int|null $updated_by
*/
class QuoteFormulaCategory extends Model
{
use BelongsToTenant, ModelTrait, SoftDeletes;
protected $table = 'quote_formula_categories';
protected $fillable = [
'tenant_id',
'code',
'name',
'description',
'sort_order',
'is_active',
'created_by',
'updated_by',
];
protected $casts = [
'is_active' => 'boolean',
'sort_order' => 'integer',
];
protected $attributes = [
'sort_order' => 0,
'is_active' => true,
];
// =========================================================================
// Relationships
// =========================================================================
/**
* 카테고리에 속한 수식들
*/
public function formulas(): HasMany
{
return $this->hasMany(QuoteFormula::class, 'category_id')
->orderBy('sort_order');
}
/**
* 활성화된 수식만
*/
public function activeFormulas(): HasMany
{
return $this->formulas()->where('is_active', true);
}
/**
* 생성자
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* 수정자
*/
public function updater(): BelongsTo
{
return $this->belongsTo(User::class, 'updated_by');
}
// =========================================================================
// Scopes
// =========================================================================
/**
* Scope: 활성화된 카테고리
*/
public function scopeActive(Builder $query): Builder
{
return $query->where('is_active', true);
}
/**
* Scope: 정렬 순서
*/
public function scopeOrdered(Builder $query): Builder
{
return $query->orderBy('sort_order');
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Models\Quote;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* 수식 품목 출력 모델
*
* @property int $id
* @property int $formula_id
* @property string $item_code
* @property string $item_name
* @property string|null $specification
* @property string $unit
* @property string $quantity_formula
* @property string|null $unit_price_formula
* @property int $sort_order
*/
class QuoteFormulaItem extends Model
{
protected $table = 'quote_formula_items';
protected $fillable = [
'formula_id',
'item_code',
'item_name',
'specification',
'unit',
'quantity_formula',
'unit_price_formula',
'sort_order',
];
protected $casts = [
'sort_order' => 'integer',
];
protected $attributes = [
'sort_order' => 0,
];
// =========================================================================
// Relationships
// =========================================================================
public function formula(): BelongsTo
{
return $this->belongsTo(QuoteFormula::class, 'formula_id');
}
// =========================================================================
// Helper Methods
// =========================================================================
/**
* 품목 표시 문자열
*/
public function getDisplayNameAttribute(): string
{
$name = "[{$this->item_code}] {$this->item_name}";
if ($this->specification) {
$name .= " ({$this->specification})";
}
return $name;
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Models\Quote;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* 수식 매핑 값 모델
*
* @property int $id
* @property int $formula_id
* @property string $source_variable
* @property string $source_value
* @property string $result_value
* @property string $result_type
* @property int $sort_order
*/
class QuoteFormulaMapping extends Model
{
protected $table = 'quote_formula_mappings';
protected $fillable = [
'formula_id',
'source_variable',
'source_value',
'result_value',
'result_type',
'sort_order',
];
protected $casts = [
'sort_order' => 'integer',
];
protected $attributes = [
'result_type' => 'fixed',
'sort_order' => 0,
];
public const RESULT_FIXED = 'fixed';
public const RESULT_FORMULA = 'formula';
// =========================================================================
// Relationships
// =========================================================================
public function formula(): BelongsTo
{
return $this->belongsTo(QuoteFormula::class, 'formula_id');
}
// =========================================================================
// Helper Methods
// =========================================================================
/**
* 매핑 조건 표시 문자열
*/
public function getConditionLabelAttribute(): string
{
return "{$this->source_variable} = '{$this->source_value}'";
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace App\Models\Quote;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* 수식 범위별 값 모델
*
* @property int $id
* @property int $formula_id
* @property float|null $min_value
* @property float|null $max_value
* @property string $condition_variable
* @property string $result_value
* @property string $result_type
* @property int $sort_order
*/
class QuoteFormulaRange extends Model
{
protected $table = 'quote_formula_ranges';
protected $fillable = [
'formula_id',
'min_value',
'max_value',
'condition_variable',
'result_value',
'result_type',
'sort_order',
];
protected $casts = [
'min_value' => 'decimal:4',
'max_value' => 'decimal:4',
'sort_order' => 'integer',
];
protected $attributes = [
'result_type' => 'fixed',
'sort_order' => 0,
];
public const RESULT_FIXED = 'fixed';
public const RESULT_FORMULA = 'formula';
// =========================================================================
// Relationships
// =========================================================================
public function formula(): BelongsTo
{
return $this->belongsTo(QuoteFormula::class, 'formula_id');
}
// =========================================================================
// Helper Methods
// =========================================================================
/**
* 값이 범위 내에 있는지 확인
*/
public function isInRange($value): bool
{
$min = $this->min_value;
$max = $this->max_value;
if (is_null($min) && is_null($max)) {
return true;
}
if (is_null($min)) {
return $value <= $max;
}
if (is_null($max)) {
return $value >= $min;
}
return $value >= $min && $value <= $max;
}
/**
* 범위 표시 문자열
*/
public function getRangeLabelAttribute(): string
{
$min = $this->min_value;
$max = $this->max_value;
if (is_null($min) && is_null($max)) {
return '전체';
}
if (is_null($min)) {
return "~ {$max}";
}
if (is_null($max)) {
return "{$min} ~";
}
return "{$min} ~ {$max}";
}
}