Files
CKB/app/Models/Precheck.php

210 lines
5.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class Precheck extends Model
{
use HasFactory;
protected $fillable = [
'transaction_id',
'precheck_by',
'precheck_at',
'police_number',
'spk_number',
'front_image',
'front_image_metadata',
'kilometer',
'pressure_high',
'pressure_low',
'cabin_temperature',
'cabin_temperature_image',
'cabin_temperature_image_metadata',
'ac_condition',
'ac_image',
'ac_image_metadata',
'blower_condition',
'blower_image',
'blower_image_metadata',
'evaporator_condition',
'evaporator_image',
'evaporator_image_metadata',
'compressor_condition',
'precheck_notes'
];
protected $casts = [
'precheck_at' => 'datetime',
'kilometer' => 'decimal:2',
'pressure_high' => 'decimal:2',
'pressure_low' => 'decimal:2',
'cabin_temperature' => 'decimal:2',
'front_image_metadata' => 'array',
'cabin_temperature_image_metadata' => 'array',
'ac_image_metadata' => 'array',
'blower_image_metadata' => 'array',
'evaporator_image_metadata' => 'array',
];
/**
* Get the transaction associated with the Precheck
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function transaction()
{
return $this->belongsTo(Transaction::class);
}
/**
* Get the user who performed the precheck
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function precheckBy()
{
return $this->belongsTo(User::class, 'precheck_by');
}
/**
* Get front image URL
*/
public function getFrontImageUrlAttribute()
{
return $this->front_image ? Storage::disk('public')->url($this->front_image) : null;
}
/**
* Get cabin temperature image URL
*/
public function getCabinTemperatureImageUrlAttribute()
{
return $this->cabin_temperature_image ? Storage::disk('public')->url($this->cabin_temperature_image) : null;
}
/**
* Get AC image URL
*/
public function getAcImageUrlAttribute()
{
return $this->ac_image ? Storage::disk('public')->url($this->ac_image) : null;
}
/**
* Get blower image URL
*/
public function getBlowerImageUrlAttribute()
{
return $this->blower_image ? Storage::disk('public')->url($this->blower_image) : null;
}
/**
* Get evaporator image URL
*/
public function getEvaporatorImageUrlAttribute()
{
return $this->evaporator_image ? Storage::disk('public')->url($this->evaporator_image) : null;
}
/**
* Delete associated files when model is deleted
*/
protected static function boot()
{
parent::boot();
static::deleting(function ($precheck) {
$imageFields = [
'front_image', 'cabin_temperature_image', 'ac_image',
'blower_image', 'evaporator_image'
];
foreach ($imageFields as $field) {
if ($precheck->$field && Storage::disk('public')->exists($precheck->$field)) {
Storage::disk('public')->delete($precheck->$field);
}
}
});
}
/**
* Get the AC condition options
*
* @return array
*/
public static function getAcConditionOptions()
{
return ['kotor', 'rusak', 'baik', 'tidak ada'];
}
/**
* Get the blower condition options
*
* @return array
*/
public static function getBlowerConditionOptions()
{
return ['kotor', 'rusak', 'baik', 'tidak ada'];
}
/**
* Get the evaporator condition options
*
* @return array
*/
public static function getEvaporatorConditionOptions()
{
return ['kotor', 'berlendir', 'bocor', 'bersih'];
}
/**
* Get the compressor condition options
*
* @return array
*/
public static function getCompressorConditionOptions()
{
return ['kotor', 'rusak', 'baik', 'tidak ada'];
}
/**
* Scope to filter by transaction
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $transactionId
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeByTransaction($query, $transactionId)
{
return $query->where('transaction_id', $transactionId);
}
/**
* Scope to filter by user who performed precheck
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $userId
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeByUser($query, $userId)
{
return $query->where('precheck_by', $userId);
}
/**
* Scope to filter by date range
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $startDate
* @param string $endDate
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeByDateRange($query, $startDate, $endDate)
{
return $query->whereBetween('precheck_at', [$startDate, $endDate]);
}
}