mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-26 02:40:43 -06:00
149 lines
4.1 KiB
PHP
149 lines
4.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* PiggyBank.php
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
|
*
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace FireflyIII\Models;
|
|
|
|
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
/**
|
|
* @mixin IdeHelperPiggyBank
|
|
*/
|
|
class PiggyBank extends Model
|
|
{
|
|
use ReturnsIntegerIdTrait;
|
|
use SoftDeletes;
|
|
|
|
protected $casts
|
|
= [
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
'deleted_at' => 'datetime',
|
|
'startdate' => 'date',
|
|
'targetdate' => 'date',
|
|
'order' => 'int',
|
|
'active' => 'boolean',
|
|
'encrypted' => 'boolean',
|
|
];
|
|
|
|
protected $fillable = ['name', 'account_id', 'order', 'targetamount', 'startdate', 'targetdate', 'active'];
|
|
|
|
protected $hidden = ['targetamount_encrypted', 'encrypted'];
|
|
|
|
/**
|
|
* Route binder. Converts the key in the URL to the specified object (or throw 404).
|
|
*
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public static function routeBinder(string $value): self
|
|
{
|
|
if (auth()->check()) {
|
|
$piggyBankId = (int)$value;
|
|
$piggyBank = self::where('piggy_banks.id', $piggyBankId)
|
|
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
|
|
->where('accounts.user_id', auth()->user()->id)->first(['piggy_banks.*'])
|
|
;
|
|
if (null !== $piggyBank) {
|
|
return $piggyBank;
|
|
}
|
|
}
|
|
|
|
throw new NotFoundHttpException();
|
|
}
|
|
|
|
public function account(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Account::class);
|
|
}
|
|
|
|
public function attachments(): MorphMany
|
|
{
|
|
return $this->morphMany(Attachment::class, 'attachable');
|
|
}
|
|
|
|
/**
|
|
* Get all the piggy bank's notes.
|
|
*/
|
|
public function notes(): MorphMany
|
|
{
|
|
return $this->morphMany(Note::class, 'noteable');
|
|
}
|
|
|
|
/**
|
|
* Get all the tags for the post.
|
|
*/
|
|
public function objectGroups(): MorphToMany
|
|
{
|
|
return $this->morphToMany(ObjectGroup::class, 'object_groupable');
|
|
}
|
|
|
|
public function piggyBankEvents(): HasMany
|
|
{
|
|
return $this->hasMany(PiggyBankEvent::class);
|
|
}
|
|
|
|
public function piggyBankRepetitions(): HasMany
|
|
{
|
|
return $this->hasMany(PiggyBankRepetition::class);
|
|
}
|
|
|
|
/**
|
|
* @param mixed $value
|
|
*/
|
|
public function setTargetamountAttribute($value): void
|
|
{
|
|
$this->attributes['targetamount'] = (string)$value;
|
|
}
|
|
|
|
protected function accountId(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: static fn ($value) => (int)$value,
|
|
);
|
|
}
|
|
|
|
protected function order(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: static fn ($value) => (int)$value,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the max amount
|
|
*/
|
|
protected function targetamount(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: static fn ($value) => (string)$value,
|
|
);
|
|
}
|
|
}
|