first
This commit is contained in:
91
app/Models/Prize.php
Normal file
91
app/Models/Prize.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Prize extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const STATUS_BELUM = 'belum_dicabut';
|
||||
public const STATUS_SEDANG = 'sedang_dicabut';
|
||||
public const STATUS_DISAHKAN = 'disahkan';
|
||||
public const STATUS_REDRAW = 'redraw_required';
|
||||
|
||||
public const STATUS_LABELS = [
|
||||
self::STATUS_BELUM => 'Belum Dicabut',
|
||||
self::STATUS_SEDANG => 'Sedang Dicabut',
|
||||
self::STATUS_DISAHKAN => 'Disahkan',
|
||||
self::STATUS_REDRAW => 'Perlu Cabut Semula',
|
||||
];
|
||||
|
||||
protected $attributes = [
|
||||
'status' => self::STATUS_BELUM,
|
||||
'item_index' => 1,
|
||||
'draw_order' => 0,
|
||||
'redraw_count' => 0,
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'kod_hadiah',
|
||||
'nama_hadiah',
|
||||
'kategori',
|
||||
'nilai_anggaran',
|
||||
'draw_order',
|
||||
'item_index',
|
||||
'batch_kod',
|
||||
'status',
|
||||
'redraw_count',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'nilai_anggaran' => 'decimal:2',
|
||||
'draw_order' => 'integer',
|
||||
'item_index' => 'integer',
|
||||
'redraw_count' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
public function drawResults(): HasMany
|
||||
{
|
||||
return $this->hasMany(DrawResult::class);
|
||||
}
|
||||
|
||||
public function confirmedResult(): HasMany
|
||||
{
|
||||
return $this->hasMany(DrawResult::class)->where('status', DrawResult::STATUS_CONFIRMED);
|
||||
}
|
||||
|
||||
public function statusLabel(): string
|
||||
{
|
||||
return self::STATUS_LABELS[$this->status] ?? $this->status;
|
||||
}
|
||||
|
||||
/** Nama paparan termasuk nombor item bila kuantiti > 1 cth "Hamper #3" */
|
||||
public function displayName(): string
|
||||
{
|
||||
return $this->nama_hadiah;
|
||||
}
|
||||
|
||||
/** Hadiah yang masih boleh dicabut (belum ada pemenang disahkan) */
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return in_array($this->status, [self::STATUS_BELUM, self::STATUS_REDRAW], true);
|
||||
}
|
||||
|
||||
public function scopeAvailable(Builder $query): Builder
|
||||
{
|
||||
return $query->whereIn('status', [self::STATUS_BELUM, self::STATUS_REDRAW]);
|
||||
}
|
||||
|
||||
public function scopeOrdered(Builder $query): Builder
|
||||
{
|
||||
return $query->orderBy('draw_order')->orderBy('item_index')->orderBy('id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user