168 lines
4.3 KiB
PHP
168 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Paket extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $table = 'pakets';
|
|
|
|
protected $fillable = [
|
|
'nama_paket',
|
|
'slug',
|
|
'jenis_paket',
|
|
'harga',
|
|
'image',
|
|
'keterangan',
|
|
'detail_barang',
|
|
'stok',
|
|
'status',
|
|
'rating',
|
|
'total_rating',
|
|
'minimum_order',
|
|
'maximum_order',
|
|
'ongkir_km',
|
|
'min_jarak'
|
|
];
|
|
|
|
protected $casts = [
|
|
'detail_barang' => 'array',
|
|
'harga' => 'decimal:2',
|
|
'rating' => 'decimal:2',
|
|
'ongkir_km' => 'decimal:2',
|
|
'min_jarak' => 'integer',
|
|
];
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
protected $attributes = [
|
|
'status' => 'aktif',
|
|
'rating' => 0.00,
|
|
'total_rating' => 0,
|
|
'minimum_order' => 1,
|
|
'maximum_order' => null
|
|
];
|
|
|
|
// Relasi dengan StockModel (barang)
|
|
public function barangs()
|
|
{
|
|
return $this->belongsToMany(StockModel::class, 'paket_barang', 'paket_id', 'barang_id')
|
|
->withPivot('id', 'jumlah')
|
|
->withTimestamps();
|
|
}
|
|
|
|
// Accessor untuk mendapatkan URL gambar lengkap
|
|
public function getImageUrlAttribute()
|
|
{
|
|
return $this->image ? asset('storage/' . $this->image) : null;
|
|
}
|
|
|
|
// Accessor untuk mendapatkan status yang diformat
|
|
public function getStatusLabelAttribute()
|
|
{
|
|
return ucfirst($this->status);
|
|
}
|
|
|
|
// Accessor untuk mendapatkan detail barang yang terformat
|
|
public function getDetailBarangFormattedAttribute()
|
|
{
|
|
if (!$this->detail_barang) {
|
|
return [];
|
|
}
|
|
|
|
return collect($this->detail_barang)->map(function ($item) {
|
|
return [
|
|
'id' => $item['id'],
|
|
'nama_barang' => $item['nama_barang'],
|
|
'kode_barang' => $item['kode_barang'],
|
|
'kategori' => $item['kategori'],
|
|
'harga_sewa' => number_format($item['harga_sewa'], 0, ',', '.'),
|
|
'jumlah' => $item['jumlah'],
|
|
'deskripsi' => $item['deskripsi'] ?? '',
|
|
'subtotal' => number_format($item['harga_sewa'] * $item['jumlah'], 0, ',', '.')
|
|
];
|
|
})->all();
|
|
}
|
|
|
|
// Method untuk menghitung total harga barang dalam paket
|
|
public function getTotalHargaBarangAttribute()
|
|
{
|
|
if (!$this->detail_barang) {
|
|
return 0;
|
|
}
|
|
|
|
return collect($this->detail_barang)->sum(function ($item) {
|
|
return $item['harga_sewa'] * $item['jumlah'];
|
|
});
|
|
}
|
|
|
|
// Method untuk mendapatkan daftar barang dalam paket
|
|
public function getBarang()
|
|
{
|
|
return $this->barangs->map(function ($barang) {
|
|
return [
|
|
'id' => $barang->pivot->id,
|
|
'nama' => $barang->nama_barang,
|
|
'jumlah' => $barang->pivot->jumlah
|
|
];
|
|
})->all();
|
|
}
|
|
|
|
public function ongkirKota()
|
|
{
|
|
return $this->hasMany(PaketOngkirKota::class);
|
|
}
|
|
|
|
// Relasi dengan model Sewa
|
|
public function sewas()
|
|
{
|
|
return $this->hasMany(Sewa::class);
|
|
}
|
|
|
|
/**
|
|
* Menghitung jumlah sewa yang sedang aktif (confirmed/ongoing)
|
|
*/
|
|
public function getActiveSewaCountAttribute()
|
|
{
|
|
return $this->sewas()
|
|
->whereIn('status', ['confirmed', 'ongoing'])
|
|
->count();
|
|
}
|
|
|
|
/**
|
|
* Menghitung stok yang tersedia
|
|
*/
|
|
public function getStokTersediaAttribute()
|
|
{
|
|
return max(0, $this->stok - $this->active_sewa_count);
|
|
}
|
|
|
|
/**
|
|
* Cek apakah paket tersedia untuk disewa
|
|
*/
|
|
public function getIsAvailableAttribute()
|
|
{
|
|
return $this->status === 'aktif' && $this->stok_tersedia > 0;
|
|
}
|
|
|
|
/**
|
|
* Mendapatkan status ketersediaan yang diformat
|
|
*/
|
|
public function getStatusKetersediaanAttribute()
|
|
{
|
|
if ($this->status !== 'aktif') {
|
|
return 'Tidak Aktif';
|
|
}
|
|
|
|
if ($this->stok_tersedia <= 0) {
|
|
return 'Sedang Disewa Semua';
|
|
}
|
|
|
|
return "Tersedia ({$this->stok_tersedia} unit)";
|
|
}
|
|
}
|