initial commit
This commit is contained in:
+70
-27
@@ -9,59 +9,102 @@ use Illuminate\Support\Str;
|
||||
class Supply extends Model
|
||||
{
|
||||
protected $table = 'supplies';
|
||||
protected $guarded = ['id'];
|
||||
|
||||
// Menggunakan fillable untuk keamanan mass assignment
|
||||
protected $fillable = [
|
||||
'nama_perlengkapan',
|
||||
'foto_produk',
|
||||
'slug',
|
||||
'qty',
|
||||
'unit',
|
||||
'category_id',
|
||||
'kategori',
|
||||
'expired_at'
|
||||
];
|
||||
|
||||
/**
|
||||
* Modern Casting (Laravel 11/12)
|
||||
* Memastikan expired_at menjadi objek Carbon untuk mencegah error isPast()
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'expired_at' => 'date',
|
||||
'qty' => 'float',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Relasi ke model Category
|
||||
*/
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class, 'category_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Logic Otomatisasi & Integritas Data
|
||||
*/
|
||||
protected static function booted()
|
||||
{
|
||||
parent::booted();
|
||||
|
||||
static::saving(function ($supply) {
|
||||
// 1. Sinkronisasi Slug (Mencegah error SQL sebelumnya)
|
||||
// 1. OTOMATIS GENERATE SLUG
|
||||
if ($supply->isDirty('nama_perlengkapan') || empty($supply->slug)) {
|
||||
$supply->slug = Str::slug($supply->nama_perlengkapan) . '-' . Str::random(5);
|
||||
$supply->slug = Str::slug($supply->nama_perlengkapan) . '-' . Str::lower(Str::random(5));
|
||||
}
|
||||
|
||||
// 2. Auto-Categorization
|
||||
if (empty($supply->category_id)) {
|
||||
// 2. AUTO-CATEGORIZATION (SMART LOGIC)
|
||||
// Jalankan jika category_id kosong atau nama perlengkapan berubah
|
||||
if (empty($supply->category_id) || $supply->isDirty('nama_perlengkapan')) {
|
||||
$name = strtolower($supply->nama_perlengkapan);
|
||||
|
||||
// Ambil kategori yang punya keywords
|
||||
$categories = Category::whereNotNull('keywords')->get();
|
||||
$found = false;
|
||||
|
||||
foreach ($categories as $cat) {
|
||||
$keywords = explode(',', strtolower($cat->keywords));
|
||||
$keywordsArray = explode(',', strtolower($cat->keywords));
|
||||
|
||||
foreach ($keywords as $keyword) {
|
||||
$trimmed = trim($keyword);
|
||||
foreach ($keywordsArray as $keyword) {
|
||||
$cleanKeyword = trim($keyword);
|
||||
|
||||
// Cek apakah "pasir" atau keyword lain ada di nama produk
|
||||
if (!empty($trimmed) && str_contains($name, $trimmed)) {
|
||||
if (!empty($cleanKeyword) && str_contains($name, $cleanKeyword)) {
|
||||
$supply->category_id = $cat->id;
|
||||
// PENTING: Update kolom teks 'kategori' agar tampilan tidak 'Umum'
|
||||
$supply->kategori = $cat->name;
|
||||
return;
|
||||
$found = true;
|
||||
break 2; // Berhenti mencari jika sudah ketemu
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Fallback jika tidak ada yang cocok
|
||||
// Cari kategori yang mengandung kata 'Pasir' atau 'Umum'
|
||||
$default = Category::where('name', 'LIKE', '%Pasir%')
|
||||
->orWhere('name', 'Umum')
|
||||
->first();
|
||||
|
||||
if ($default) {
|
||||
$supply->category_id = $default->id;
|
||||
$supply->kategori = $default->name;
|
||||
} else {
|
||||
$supply->kategori = 'Umum';
|
||||
// 3. FALLBACK LOGIC
|
||||
if (!$found && empty($supply->category_id)) {
|
||||
$default = Category::where('name', 'LIKE', '%Pasir%')
|
||||
->orWhere('name', 'Umum')
|
||||
->first();
|
||||
|
||||
if ($default) {
|
||||
$supply->category_id = $default->id;
|
||||
$supply->kategori = $default->name;
|
||||
} else {
|
||||
$supply->kategori = $supply->kategori ?: 'Umum';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. SINKRONISASI TEKS KATEGORI
|
||||
// Jika ID kategori diubah manual di form, update teks kolom 'kategori'
|
||||
if ($supply->isDirty('category_id') && !empty($supply->category_id)) {
|
||||
$supply->kategori = $supply->category?->name ?? $supply->kategori;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Accessor: Membantu pengecekan expired di Blade secara rapi
|
||||
* Contoh: @if($supply->is_expired) ... @endif
|
||||
*/
|
||||
public function getIsExpiredAttribute(): bool
|
||||
{
|
||||
return $this->expired_at ? $this->expired_at->isPast() : false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user