Car-Repairs-Shop/app/Models/Estimate.php.backup
sackey 5403c3591d
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
feat: Enhance job card workflow with diagnosis actions and technician assignment modal
- Added buttons for assigning diagnosis and starting diagnosis based on job card status in the job card view.
- Implemented a modal for assigning technicians for diagnosis, including form validation and technician selection.
- Updated routes to include a test route for job cards.
- Created a new Blade view for testing inspection inputs.
- Developed comprehensive feature tests for the estimate module, including creation, viewing, editing, and validation of estimates.
- Added tests for estimate model relationships and statistics calculations.
- Introduced a basic feature test for job cards index.
2025-08-15 08:37:45 +00:00

159 lines
4.3 KiB
Plaintext

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Estimate extends Model
{
use HasFactory;
protected $fillable = [
'job_card_id',
'diagnosis_id',
'customer_id',
'vehicle_id',
'estimate_number',
'prepared_by_id',
'labor_cost',
'parts_cost',
'miscellaneous_cost',
'subtotal',
'tax_rate',
'tax_amount',
'discount_amount',
'total_amount',
'validity_period_days',
'terms_and_conditions',
'status',
'customer_approval_status',
'customer_approved_at',
'customer_approval_method',
'sent_to_customer_at',
'sms_sent_at',
'email_sent_at',
'notes',
'internal_notes',
'revision_number',
'original_estimate_id',
];
protected $casts = [
'labor_cost' => '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;
}
}