buckets/app/Models/Stream.php

109 lines
2.7 KiB
PHP
Raw Normal View History

2025-12-31 00:02:54 +01:00
<?php
namespace App\Models;
2025-12-31 01:48:13 +01:00
use App\Models\Traits\HasAmount;
2025-12-31 00:02:54 +01:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Stream extends Model
{
2025-12-31 01:48:13 +01:00
use HasAmount;
2025-12-31 00:02:54 +01:00
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 = [
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);
}
2025-12-31 01:48:13 +01:00
2025-12-31 00:02:54 +01:00
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);
}
}