'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 customer(): BelongsTo { return $this->belongsTo(Customer::class); } public function vehicle(): BelongsTo { return $this->belongsTo(Vehicle::class); } 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 workOrders(): HasMany { return $this->hasMany(WorkOrder::class); } public function getValidUntilAttribute() { return $this->created_at->addDays($this->validity_period_days); } public function getIsExpiredAttribute() { return $this->valid_until < now() && $this->status !== 'approved'; } 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(); } /** * Get the customer for this estimate (either direct or through job card) */ public function getEstimateCustomerAttribute() { return $this->customer_id ? $this->customer : $this->jobCard?->customer; } /** * Get the vehicle for this estimate (either direct or through job card) */ public function getEstimateVehicleAttribute() { return $this->vehicle_id ? $this->vehicle : $this->jobCard?->vehicle; } }