buckets/app/Models/Stream.php

91 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\StreamFrequencyEnum;
use App\Enums\StreamTypeEnum;
use App\Models\Traits\HasAmount;
use App\Models\Traits\HasUuid;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $amount
* @property string $uuid
* @property StreamFrequencyEnum $frequency
* @property Carbon $start_date
*/
class Stream extends Model
{
use HasAmount, HasFactory, HasUuid;
protected $fillable = [
'scenario_id',
'name',
'type',
'amount',
'frequency',
'start_date',
'end_date',
'bucket_id',
'description',
'is_active',
];
protected $casts = [
'type' => StreamTypeEnum::class,
'frequency' => StreamFrequencyEnum::class,
'amount' => 'integer',
'start_date' => 'date',
'end_date' => 'date',
'is_active' => 'boolean',
];
public function scenario(): BelongsTo
{
return $this->belongsTo(Scenario::class);
}
public function bucket(): BelongsTo
{
return $this->belongsTo(Bucket::class);
}
public static function getTypes(): array
{
return StreamTypeEnum::labels();
}
public static function getFrequencies(): array
{
return StreamFrequencyEnum::labels();
}
public function getTypeLabel(): string
{
return $this->type?->label() ?? '';
}
public function getFrequencyLabel(): string
{
return $this->frequency?->label() ?? '';
}
public function getMonthlyEquivalent(): float
{
if (! $this->frequency) {
return 0;
}
return $this->amount * $this->frequency->getMonthlyEquivalentMultiplier();
}
/* SCOPES */
public function scopeByType($query, string $type)
{
return $query->where('type', $type);
}
}