Files
sam-api/app/Models/Estimate/Estimate.php
권혁성 189b38c936 feat: Auditable 트레이트 구현 및 97개 모델 적용
- 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>
2026-01-29 15:33:54 +09:00

109 lines
2.5 KiB
PHP

<?php
namespace App\Models\Estimate;
use App\Models\Commons\Category;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Estimate extends Model
{
use Auditable, BelongsToTenant, HasFactory, SoftDeletes;
protected $fillable = [
'tenant_id',
'model_set_id',
'estimate_no',
'estimate_name',
'customer_name',
'project_name',
'parameters',
'calculated_results',
'bom_data',
'total_amount',
'status',
'notes',
'valid_until',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'parameters' => 'array',
'calculated_results' => 'array',
'bom_data' => 'array',
'total_amount' => 'decimal:2',
'valid_until' => 'date',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
/**
* 모델셋 관계 (카테고리)
*/
public function modelSet(): BelongsTo
{
return $this->belongsTo(Category::class, 'model_set_id');
}
/**
* 견적 항목들
*/
public function items(): HasMany
{
return $this->hasMany(EstimateItem::class);
}
/**
* 견적 번호 자동 생성
*/
public static function generateEstimateNo(int $tenantId): string
{
$prefix = 'EST';
$date = now()->format('Ymd');
$lastEstimate = self::where('tenant_id', $tenantId)
->whereDate('created_at', today())
->orderBy('id', 'desc')
->first();
$sequence = $lastEstimate ? (int) substr($lastEstimate->estimate_no, -3) + 1 : 1;
return $prefix.$date.str_pad($sequence, 3, '0', STR_PAD_LEFT);
}
/**
* 견적 상태별 스코프
*/
public function scopeDraft($query)
{
return $query->where('status', 'DRAFT');
}
public function scopeSent($query)
{
return $query->where('status', 'SENT');
}
public function scopeApproved($query)
{
return $query->where('status', 'APPROVED');
}
/**
* 만료된 견적 스코프
*/
public function scopeExpired($query)
{
return $query->whereNotNull('valid_until')
->where('valid_until', '<', now());
}
}