buckets/app/Models/Stream.php
2025-12-31 00:02:54 +01:00

105 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Stream extends Model
{
const TYPE_INCOME = 'income';
const TYPE_EXPENSE = 'expense';
const FREQUENCY_ONCE = 'once';
const FREQUENCY_WEEKLY = 'weekly';
const FREQUENCY_BIWEEKLY = 'biweekly';
const FREQUENCY_MONTHLY = 'monthly';
const FREQUENCY_QUARTERLY = 'quarterly';
const FREQUENCY_YEARLY = 'yearly';
protected $fillable = [
'scenario_id',
'name',
'type',
'amount',
'frequency',
'start_date',
'end_date',
'bucket_id',
'description',
'is_active',
];
protected $casts = [
'amount' => 'decimal:2',
'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 [
self::TYPE_INCOME => 'Income',
self::TYPE_EXPENSE => 'Expense',
];
}
public static function getFrequencies(): array
{
return [
self::FREQUENCY_ONCE => 'One-time',
self::FREQUENCY_WEEKLY => 'Weekly',
self::FREQUENCY_BIWEEKLY => 'Bi-weekly',
self::FREQUENCY_MONTHLY => 'Monthly',
self::FREQUENCY_QUARTERLY => 'Quarterly',
self::FREQUENCY_YEARLY => 'Yearly',
];
}
public function getTypeLabel(): string
{
return self::getTypes()[$this->type] ?? $this->type;
}
public function getFrequencyLabel(): string
{
return self::getFrequencies()[$this->frequency] ?? $this->frequency;
}
public function getMonthlyEquivalent(): float
{
switch ($this->frequency) {
case self::FREQUENCY_WEEKLY:
return $this->amount * 4.33; // Average weeks per month
case self::FREQUENCY_BIWEEKLY:
return $this->amount * 2.17;
case self::FREQUENCY_MONTHLY:
return $this->amount;
case self::FREQUENCY_QUARTERLY:
return $this->amount / 3;
case self::FREQUENCY_YEARLY:
return $this->amount / 12;
case self::FREQUENCY_ONCE:
default:
return 0;
}
}
/* SCOPES */
public function scopeByType($query, string $type)
{
return $query->where('type', $type);
}
}