'decimal:2', 'parts_cost' => 'decimal:2', 'miscellaneous_cost' => 'decimal:2', 'subtotal' => 'decimal:2', 'tax_rate' => 'decimal:4', 'tax_amount' => 'decimal:2', 'discount_amount' => 'decimal:2', 'total_amount' => 'decimal:2', 'customer_approved_at' => 'datetime', 'sent_to_customer_at' => 'datetime', 'sms_sent_at' => 'datetime', 'email_sent_at' => 'datetime', ]; protected static function boot() { parent::boot(); static::creating(function ($estimate) { if (empty($estimate->estimate_number)) { $estimate->estimate_number = 'EST-' . date('Y') . '-' . str_pad( static::whereYear('created_at', now()->year)->count() + 1, 6, '0', STR_PAD_LEFT ); } }); } public function jobCard(): BelongsTo { return $this->belongsTo(JobCard::class); } public function diagnosis(): BelongsTo { return $this->belongsTo(Diagnosis::class); } public function preparedBy(): BelongsTo { return $this->belongsTo(User::class, 'prepared_by_id'); } public function lineItems(): HasMany { return $this->hasMany(EstimateLineItem::class); } public function originalEstimate(): BelongsTo { return $this->belongsTo(Estimate::class, 'original_estimate_id'); } public function revisions(): HasMany { return $this->hasMany(Estimate::class, 'original_estimate_id'); } public function calculateTotals(): void { $this->labor_cost = $this->lineItems()->where('type', 'labor')->sum('total_amount'); $this->parts_cost = $this->lineItems()->where('type', 'parts')->sum('total_amount'); $this->miscellaneous_cost = $this->lineItems()->where('type', 'miscellaneous')->sum('total_amount'); $this->subtotal = $this->labor_cost + $this->parts_cost + $this->miscellaneous_cost - $this->discount_amount; $this->tax_amount = $this->subtotal * ($this->tax_rate / 100); $this->total_amount = $this->subtotal + $this->tax_amount; $this->save(); } }