buckets/app/Models/Stream.php

90 lines
1.9 KiB
PHP
Raw Normal View History

2025-12-31 00:02:54 +01:00
<?php
namespace App\Models;
2025-12-31 02:34:30 +01:00
use App\Enums\StreamFrequencyEnum;
use App\Enums\StreamTypeEnum;
2025-12-31 01:48:13 +01:00
use App\Models\Traits\HasAmount;
2025-12-31 02:34:30 +01:00
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2025-12-31 00:02:54 +01:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2025-12-31 02:34:30 +01:00
/**
* @property int $amount
* @property StreamFrequencyEnum $frequency
* @property Carbon $start_date
*/
2025-12-31 00:02:54 +01:00
class Stream extends Model
{
use HasAmount, HasFactory;
2025-12-31 00:02:54 +01:00
protected $fillable = [
'scenario_id',
'name',
'type',
'amount',
'frequency',
'start_date',
'end_date',
'bucket_id',
'description',
'is_active',
];
protected $casts = [
2025-12-31 02:34:30 +01:00
'type' => StreamTypeEnum::class,
'frequency' => StreamFrequencyEnum::class,
2025-12-31 01:48:13 +01:00
'amount' => 'integer',
2025-12-31 00:02:54 +01:00
'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
{
2025-12-31 02:34:30 +01:00
return StreamTypeEnum::labels();
2025-12-31 00:02:54 +01:00
}
public static function getFrequencies(): array
{
2025-12-31 02:34:30 +01:00
return StreamFrequencyEnum::labels();
2025-12-31 00:02:54 +01:00
}
public function getTypeLabel(): string
{
2025-12-31 02:34:30 +01:00
return $this->type?->label() ?? '';
2025-12-31 00:02:54 +01:00
}
public function getFrequencyLabel(): string
{
2025-12-31 02:34:30 +01:00
return $this->frequency?->label() ?? '';
2025-12-31 00:02:54 +01:00
}
public function getMonthlyEquivalent(): float
{
if (! $this->frequency) {
2025-12-31 02:34:30 +01:00
return 0;
2025-12-31 00:02:54 +01:00
}
2025-12-31 02:34:30 +01:00
return $this->amount * $this->frequency->getMonthlyEquivalentMultiplier();
2025-12-31 00:02:54 +01:00
}
/* SCOPES */
public function scopeByType($query, string $type)
{
return $query->where('type', $type);
}
}