Files
firefly-iii/app/Models/Account.php

282 lines
8.5 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* Account.php
2020-02-16 13:55:32 +01:00
* 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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* 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.
2017-10-21 08:40:00 +02:00
*
* 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);
2016-05-20 08:57:45 +02:00
namespace FireflyIII\Models;
2015-02-27 16:48:33 +01:00
2018-07-22 16:35:46 +02:00
use Carbon\Carbon;
use Eloquent;
use FireflyIII\User;
2015-02-27 16:48:33 +01:00
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2020-11-02 06:20:49 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2015-02-27 16:48:33 +01:00
use Illuminate\Database\Eloquent\Model;
2016-04-06 09:27:45 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
2018-07-26 06:10:17 +02:00
use Illuminate\Database\Eloquent\Relations\MorphMany;
2015-02-27 16:48:33 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
2018-07-22 16:35:46 +02:00
use Illuminate\Support\Collection;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2016-11-18 20:06:08 +01:00
/**
2020-12-04 06:20:44 +01:00
* Class Account
*
2020-12-04 06:20:44 +01:00
* @property int $id
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property int $user_id
* @property int $account_type_id
* @property string $name
* @property string|null $virtual_balance
* @property string|null $iban
* @property bool $active
* @property bool $encrypted
* @property int $order
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\AccountMeta[] $accountMeta
* @property-read int|null $account_meta_count
* @property-read \FireflyIII\Models\AccountType $accountType
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Attachment[] $attachments
* @property-read int|null $attachments_count
* @property-read string $account_number
* @property-read string $edit_name
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Location[] $locations
* @property-read int|null $locations_count
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Note[] $notes
* @property-read int|null $notes_count
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\ObjectGroup[] $objectGroups
* @property-read int|null $object_groups_count
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBank[] $piggyBanks
* @property-read int|null $piggy_banks_count
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions
* @property-read int|null $transactions_count
* @property-read User $user
* @method static EloquentBuilder|Account accountTypeIn($types)
* @method static EloquentBuilder|Account newModelQuery()
* @method static EloquentBuilder|Account newQuery()
* @method static Builder|Account onlyTrashed()
* @method static EloquentBuilder|Account query()
* @method static EloquentBuilder|Account whereAccountTypeId($value)
* @method static EloquentBuilder|Account whereActive($value)
* @method static EloquentBuilder|Account whereCreatedAt($value)
* @method static EloquentBuilder|Account whereDeletedAt($value)
* @method static EloquentBuilder|Account whereEncrypted($value)
* @method static EloquentBuilder|Account whereIban($value)
* @method static EloquentBuilder|Account whereId($value)
* @method static EloquentBuilder|Account whereName($value)
2020-12-04 06:20:44 +01:00
* @method static EloquentBuilder|Account whereOrder($value)
* @method static EloquentBuilder|Account whereUpdatedAt($value)
* @method static EloquentBuilder|Account whereUserId($value)
* @method static EloquentBuilder|Account whereVirtualBalance($value)
* @method static Builder|Account withTrashed()
* @method static Builder|Account withoutTrashed()
* @mixin Eloquent
2016-11-18 20:06:08 +01:00
*/
2015-02-27 16:48:33 +01:00
class Account extends Model
{
2020-11-02 06:20:49 +01:00
use SoftDeletes, HasFactory;
2015-02-27 16:48:33 +01:00
2016-12-24 17:36:51 +01:00
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
2017-11-03 16:04:17 +01:00
'created_at' => 'datetime',
'updated_at' => 'datetime',
2019-03-18 16:52:49 +01:00
'user_id' => 'integer',
2017-11-03 16:04:17 +01:00
'deleted_at' => 'datetime',
2016-12-24 17:36:51 +01:00
'active' => 'boolean',
'encrypted' => 'boolean',
];
2018-08-04 17:30:06 +02:00
/** @var array Fields that can be filled */
2015-07-05 09:19:51 +02:00
protected $fillable = ['user_id', 'account_type_id', 'name', 'active', 'virtual_balance', 'iban'];
2018-08-04 17:30:06 +02:00
/** @var array Hidden from view */
2016-12-24 17:36:51 +01:00
protected $hidden = ['encrypted'];
2017-11-15 12:25:49 +01:00
/** @var bool */
private $joinedAccountTypes;
2015-02-27 16:48:33 +01:00
/**
2018-08-04 17:30:06 +02:00
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
* @param string $value
*
2018-08-04 17:30:06 +02:00
* @throws NotFoundHttpException
* @return Account
*/
2018-02-09 19:24:15 +01:00
public static function routeBinder(string $value): Account
{
2018-02-09 19:24:15 +01:00
if (auth()->check()) {
$accountId = (int) $value;
2018-07-22 16:35:46 +02:00
/** @var User $user */
$user = auth()->user();
/** @var Account $account */
$account = $user->accounts()->find($accountId);
2018-04-02 15:10:40 +02:00
if (null !== $account) {
return $account;
}
}
throw new NotFoundHttpException;
}
2020-07-24 16:40:42 +02:00
/**
* Get all of the tags for the post.
*/
public function objectGroups()
{
return $this->morphToMany(ObjectGroup::class, 'object_groupable');
}
/**
* @codeCoverageIgnore
* @return MorphMany
*/
public function attachments(): MorphMany
{
return $this->morphMany(Attachment::class, 'attachable');
}
2015-03-30 20:08:27 +02:00
/**
2016-04-06 09:27:45 +02:00
* @return HasMany
* @codeCoverageIgnore
2015-03-30 20:08:27 +02:00
*/
2016-04-06 09:27:45 +02:00
public function accountMeta(): HasMany
2015-03-30 20:08:27 +02:00
{
2018-04-28 06:23:13 +02:00
return $this->hasMany(AccountMeta::class);
2015-03-30 20:08:27 +02:00
}
/**
2016-04-06 09:27:45 +02:00
* @return BelongsTo
* @codeCoverageIgnore
2015-03-30 20:08:27 +02:00
*/
2016-04-06 09:27:45 +02:00
public function accountType(): BelongsTo
2015-03-30 20:08:27 +02:00
{
2018-04-28 06:23:13 +02:00
return $this->belongsTo(AccountType::class);
2015-03-30 20:08:27 +02:00
}
2019-12-20 21:01:27 +01:00
/**
* Get the account number.
*
* @return string
*/
2019-12-21 06:37:41 +01:00
public function getAccountNumberAttribute(): string
2019-12-20 21:01:27 +01:00
{
/** @var AccountMeta $metaValue */
$metaValue = $this->accountMeta()
->where('name', 'account_number')
->first();
2019-12-21 06:37:41 +01:00
return $metaValue ? $metaValue->data : '';
2019-12-20 21:01:27 +01:00
}
/**
* @return string
* @codeCoverageIgnore
*/
public function getEditNameAttribute(): string
{
$name = $this->name;
2017-11-15 12:25:49 +01:00
if (AccountType::CASH === $this->accountType->type) {
return '';
}
return $name;
}
/**
* @codeCoverageIgnore
* @return MorphMany
*/
public function locations(): MorphMany
{
return $this->morphMany(Location::class, 'locatable');
}
2018-02-07 11:15:36 +01:00
/**
* @codeCoverageIgnore
* Get all of the notes.
*/
2018-07-26 06:10:17 +02:00
public function notes(): MorphMany
2018-02-07 11:15:36 +01:00
{
return $this->morphMany(Note::class, 'noteable');
}
2015-02-27 16:48:33 +01:00
/**
2016-04-06 09:27:45 +02:00
* @return HasMany
* @codeCoverageIgnore
2015-02-27 16:48:33 +01:00
*/
2016-04-06 09:27:45 +02:00
public function piggyBanks(): HasMany
2015-02-27 16:48:33 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->hasMany(PiggyBank::class);
2015-02-27 16:48:33 +01:00
}
/**
* @codeCoverageIgnore
2017-12-29 09:05:35 +01:00
*
2015-03-30 20:08:27 +02:00
* @param EloquentBuilder $query
* @param array $types
2015-02-27 16:48:33 +01:00
*/
2018-07-22 16:35:46 +02:00
public function scopeAccountTypeIn(EloquentBuilder $query, array $types): void
2015-02-27 16:48:33 +01:00
{
2017-11-15 12:25:49 +01:00
if (null === $this->joinedAccountTypes) {
2015-03-30 20:08:27 +02:00
$query->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id');
$this->joinedAccountTypes = true;
}
$query->whereIn('account_types.type', $types);
2015-02-27 16:48:33 +01:00
}
2015-05-23 07:47:36 +02:00
/**
* @codeCoverageIgnore
2017-12-29 09:05:35 +01:00
*
2015-05-23 07:47:36 +02:00
* @param $value
2017-12-29 09:05:35 +01:00
*
* @codeCoverageIgnore
2015-05-23 07:47:36 +02:00
*/
2018-07-22 16:35:46 +02:00
public function setVirtualBalanceAttribute($value): void
2015-05-23 07:47:36 +02:00
{
2020-04-13 07:57:32 +02:00
$value = (string)$value;
if('' === $value) {
$value = null;
}
$this->attributes['virtual_balance'] = $value;
2015-05-23 07:47:36 +02:00
}
2015-02-27 16:48:33 +01:00
/**
2016-04-06 09:27:45 +02:00
* @return HasMany
* @codeCoverageIgnore
2015-02-27 16:48:33 +01:00
*/
2016-04-06 09:27:45 +02:00
public function transactions(): HasMany
2015-02-27 16:48:33 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->hasMany(Transaction::class);
2015-02-27 16:48:33 +01:00
}
/**
2016-04-06 09:27:45 +02:00
* @return BelongsTo
* @codeCoverageIgnore
2015-02-27 16:48:33 +01:00
*/
2016-04-06 09:27:45 +02:00
public function user(): BelongsTo
2015-02-27 16:48:33 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->belongsTo(User::class);
2015-02-27 16:48:33 +01:00
}
}