'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()); } }