58 lines
1.1 KiB
PHP
58 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class FlashSaleItem extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'flash_sale_id',
|
|
'barang_id',
|
|
'harga_flash_sale',
|
|
'persentase_diskon',
|
|
'stok_flash_sale',
|
|
'stok_terjual',
|
|
'aktif',
|
|
];
|
|
|
|
protected $casts = [
|
|
'aktif' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Relasi ke flash sale
|
|
*/
|
|
public function flashSale()
|
|
{
|
|
return $this->belongsTo(FlashSale::class);
|
|
}
|
|
|
|
/**
|
|
* Relasi ke barang
|
|
*/
|
|
public function barang()
|
|
{
|
|
return $this->belongsTo(Barang::class);
|
|
}
|
|
|
|
/**
|
|
* Cek apakah masih tersedia stok
|
|
*/
|
|
public function getStokTersediaAttribute()
|
|
{
|
|
return $this->stok_flash_sale - $this->stok_terjual;
|
|
}
|
|
|
|
/**
|
|
* Cek apakah item flash sale masih tersedia
|
|
*/
|
|
public function getTersediaAttribute()
|
|
{
|
|
return $this->aktif && $this->getStokTersediaAttribute() > 0 && $this->flashSale && $this->flashSale->sedang_berlangsung;
|
|
}
|
|
}
|