65 lines
1.4 KiB
PHP
65 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class FlashSale extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'nama',
|
|
'deskripsi',
|
|
'banner',
|
|
'waktu_mulai',
|
|
'waktu_selesai',
|
|
'aktif',
|
|
];
|
|
|
|
protected $casts = [
|
|
'waktu_mulai' => 'datetime',
|
|
'waktu_selesai' => 'datetime',
|
|
'aktif' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Relasi ke item flash sale
|
|
*/
|
|
public function items()
|
|
{
|
|
return $this->hasMany(FlashSaleItem::class);
|
|
}
|
|
|
|
/**
|
|
* Relasi ke barang-barang yang termasuk dalam flash sale
|
|
*/
|
|
public function barangs()
|
|
{
|
|
return $this->belongsToMany(Barang::class, 'flash_sale_items')
|
|
->withPivot('harga_flash_sale', 'persentase_diskon', 'stok_flash_sale', 'stok_terjual', 'aktif')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Scope untuk mendapatkan flash sale yang aktif dan berjalan saat ini
|
|
*/
|
|
public function scopeAktif($query)
|
|
{
|
|
return $query->where('aktif', true)
|
|
->where('waktu_mulai', '<=', now())
|
|
->where('waktu_selesai', '>=', now());
|
|
}
|
|
|
|
/**
|
|
* Cek apakah flash sale sedang berlangsung
|
|
*/
|
|
public function getSedangBerlangsungAttribute()
|
|
{
|
|
return $this->aktif &&
|
|
$this->waktu_mulai <= now() &&
|
|
$this->waktu_selesai >= now();
|
|
}
|
|
}
|