2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Account.php
|
2020-02-16 06:55:32 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 01:57:45 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
2015-02-27 09:48:33 -06:00
|
|
|
|
2021-04-26 23:23:16 -05:00
|
|
|
use Carbon\Carbon;
|
2020-03-17 10:46:00 -05:00
|
|
|
use Eloquent;
|
2018-05-19 03:44:33 -05:00
|
|
|
use FireflyIII\User;
|
2015-02-27 09:48:33 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2021-05-24 01:50:17 -05:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-11-01 23:20:49 -06:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2015-02-27 09:48:33 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-04-06 02:27:45 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2018-07-25 23:10:17 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
2015-02-27 09:48:33 -06:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2020-03-17 10:46:00 -05:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2018-05-19 03:44:33 -05:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2016-01-09 01:20:55 -06:00
|
|
|
|
2016-11-18 13:06:08 -06:00
|
|
|
/**
|
2020-12-03 23:20:44 -06:00
|
|
|
* Class Account
|
2018-05-19 03:44:33 -05:00
|
|
|
*
|
2022-12-29 12:42:26 -06:00
|
|
|
* @property int $id
|
2021-08-28 08:47:09 -05:00
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
2021-05-24 01:50:17 -05:00
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
* @property \Illuminate\Support\Carbon|null $deleted_at
|
2022-12-29 12:42:26 -06:00
|
|
|
* @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 Collection|AccountMeta[] $accountMeta
|
|
|
|
* @property-read int|null $account_meta_count
|
|
|
|
* @property AccountType $accountType
|
|
|
|
* @property-read Collection|Attachment[] $attachments
|
|
|
|
* @property-read int|null $attachments_count
|
|
|
|
* @property-read string $account_number
|
|
|
|
* @property-read string $edit_name
|
|
|
|
* @property-read Collection|Location[] $locations
|
|
|
|
* @property-read int|null $locations_count
|
|
|
|
* @property-read Collection|Note[] $notes
|
|
|
|
* @property-read int|null $notes_count
|
|
|
|
* @property-read Collection|ObjectGroup[] $objectGroups
|
|
|
|
* @property-read int|null $object_groups_count
|
|
|
|
* @property-read Collection|PiggyBank[] $piggyBanks
|
|
|
|
* @property-read int|null $piggy_banks_count
|
|
|
|
* @property-read Collection|Transaction[] $transactions
|
|
|
|
* @property-read int|null $transactions_count
|
|
|
|
* @property-read User $user
|
2020-03-17 10:46:00 -05:00
|
|
|
* @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-03 23:20:44 -06:00
|
|
|
* @method static EloquentBuilder|Account whereOrder($value)
|
2020-03-17 10:46:00 -05:00
|
|
|
* @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
|
2022-12-29 12:42:26 -06:00
|
|
|
* @property Carbon $lastActivityDate
|
|
|
|
* @property string $startBalance
|
|
|
|
* @property string $endBalance
|
|
|
|
* @property string $difference
|
|
|
|
* @property string $interest
|
|
|
|
* @property string $interestPeriod
|
|
|
|
* @property string $accountTypeString
|
|
|
|
* @property string $location
|
|
|
|
* @property string $liability_direction
|
|
|
|
* @property string $current_debt
|
|
|
|
* @property int|null $user_group_id
|
2021-09-18 03:08:10 -05:00
|
|
|
* @method static EloquentBuilder|Account whereUserGroupId($value)
|
2016-11-18 13:06:08 -06:00
|
|
|
*/
|
2015-02-27 09:48:33 -06:00
|
|
|
class Account extends Model
|
|
|
|
{
|
2022-10-30 08:24:28 -05:00
|
|
|
use SoftDeletes;
|
|
|
|
use HasFactory;
|
2015-02-27 09:48:33 -06:00
|
|
|
|
2016-12-24 10:36:51 -06:00
|
|
|
/**
|
|
|
|
* The attributes that should be casted to native types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts
|
|
|
|
= [
|
2017-11-03 10:04:17 -05:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
2019-03-18 10:52:49 -05:00
|
|
|
'user_id' => 'integer',
|
2017-11-03 10:04:17 -05:00
|
|
|
'deleted_at' => 'datetime',
|
2016-12-24 10:36:51 -06:00
|
|
|
'active' => 'boolean',
|
|
|
|
'encrypted' => 'boolean',
|
|
|
|
];
|
2018-08-04 10:30:06 -05:00
|
|
|
/** @var array Fields that can be filled */
|
2015-07-05 02:19:51 -05:00
|
|
|
protected $fillable = ['user_id', 'account_type_id', 'name', 'active', 'virtual_balance', 'iban'];
|
2018-08-04 10:30:06 -05:00
|
|
|
/** @var array Hidden from view */
|
2022-12-29 12:42:26 -06:00
|
|
|
protected $hidden = ['encrypted'];
|
2022-06-25 07:23:52 -05:00
|
|
|
private bool $joinedAccountTypes = false;
|
2015-02-27 09:48:33 -06:00
|
|
|
|
2016-01-27 11:31:44 -06:00
|
|
|
/**
|
2018-08-04 10:30:06 -05:00
|
|
|
* Route binder. Converts the key in the URL to the specified object (or throw 404).
|
|
|
|
*
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $value
|
2016-01-27 11:31:44 -06:00
|
|
|
*
|
2020-03-17 10:46:00 -05:00
|
|
|
* @return Account
|
2021-03-28 04:46:23 -05:00
|
|
|
* @throws NotFoundHttpException
|
2016-01-27 11:31:44 -06:00
|
|
|
*/
|
2018-02-09 12:24:15 -06:00
|
|
|
public static function routeBinder(string $value): Account
|
2016-01-27 11:31:44 -06:00
|
|
|
{
|
2018-02-09 12:24:15 -06:00
|
|
|
if (auth()->check()) {
|
2022-12-29 12:42:26 -06:00
|
|
|
$accountId = (int)$value;
|
2018-07-22 09:35:46 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
/** @var Account $account */
|
2022-06-25 07:23:52 -05:00
|
|
|
$account = $user->accounts()->with(['accountType'])->find($accountId);
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null !== $account) {
|
2017-12-25 01:45:23 -06:00
|
|
|
return $account;
|
2016-01-27 11:31:44 -06:00
|
|
|
}
|
|
|
|
}
|
2022-10-30 08:24:28 -05:00
|
|
|
throw new NotFoundHttpException();
|
2016-01-27 11:31:44 -06:00
|
|
|
}
|
|
|
|
|
2022-12-29 12:42:26 -06:00
|
|
|
/**
|
|
|
|
* @return BelongsTo
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:40:42 -05:00
|
|
|
/**
|
2021-08-28 08:47:09 -05:00
|
|
|
* @return BelongsTo
|
|
|
|
* @codeCoverageIgnore
|
2020-07-24 09:40:42 -05:00
|
|
|
*/
|
2021-08-28 08:47:09 -05:00
|
|
|
public function accountType(): BelongsTo
|
2020-07-24 09:40:42 -05:00
|
|
|
{
|
2021-08-28 08:47:09 -05:00
|
|
|
return $this->belongsTo(AccountType::class);
|
2020-07-24 09:40:42 -05:00
|
|
|
}
|
2020-03-19 02:32:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @return MorphMany
|
|
|
|
*/
|
|
|
|
public function attachments(): MorphMany
|
|
|
|
{
|
|
|
|
return $this->morphMany(Attachment::class, 'attachable');
|
|
|
|
}
|
|
|
|
|
2019-12-20 14:01:27 -06:00
|
|
|
/**
|
|
|
|
* Get the account number.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-20 23:37:41 -06:00
|
|
|
public function getAccountNumberAttribute(): string
|
2019-12-20 14:01:27 -06:00
|
|
|
{
|
|
|
|
/** @var AccountMeta $metaValue */
|
|
|
|
$metaValue = $this->accountMeta()
|
|
|
|
->where('name', 'account_number')
|
|
|
|
->first();
|
|
|
|
|
2019-12-20 23:37:41 -06:00
|
|
|
return $metaValue ? $metaValue->data : '';
|
2019-12-20 14:01:27 -06:00
|
|
|
}
|
|
|
|
|
2021-08-28 08:47:09 -05:00
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function accountMeta(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(AccountMeta::class);
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:57:35 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2016-11-19 05:57:35 -06:00
|
|
|
*/
|
|
|
|
public function getEditNameAttribute(): string
|
|
|
|
{
|
|
|
|
$name = $this->name;
|
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (AccountType::CASH === $this->accountType->type) {
|
2016-11-19 05:57:35 -06:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2020-03-17 10:46:00 -05:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @return MorphMany
|
|
|
|
*/
|
|
|
|
public function locations(): MorphMany
|
|
|
|
{
|
|
|
|
return $this->morphMany(Location::class, 'locatable');
|
|
|
|
}
|
|
|
|
|
2018-02-07 04:15:36 -06:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* Get all of the notes.
|
|
|
|
*/
|
2018-07-25 23:10:17 -05:00
|
|
|
public function notes(): MorphMany
|
2018-02-07 04:15:36 -06:00
|
|
|
{
|
|
|
|
return $this->morphMany(Note::class, 'noteable');
|
|
|
|
}
|
|
|
|
|
2021-08-28 08:47:09 -05:00
|
|
|
/**
|
|
|
|
* Get all of the tags for the post.
|
|
|
|
*/
|
|
|
|
public function objectGroups()
|
|
|
|
{
|
|
|
|
return $this->morphToMany(ObjectGroup::class, 'object_groupable');
|
|
|
|
}
|
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
2016-04-06 02:27:45 -05:00
|
|
|
* @return HasMany
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2015-02-27 09:48:33 -06:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function piggyBanks(): HasMany
|
2015-02-27 09:48:33 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(PiggyBank::class);
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-12-29 02:05:35 -06:00
|
|
|
*
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param EloquentBuilder $query
|
|
|
|
* @param array $types
|
2015-02-27 09:48:33 -06:00
|
|
|
*/
|
2018-07-22 09:35:46 -05:00
|
|
|
public function scopeAccountTypeIn(EloquentBuilder $query, array $types): void
|
2015-02-27 09:48:33 -06:00
|
|
|
{
|
2022-06-25 07:23:52 -05:00
|
|
|
if (false === $this->joinedAccountTypes) {
|
2015-03-30 13:08:27 -05:00
|
|
|
$query->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id');
|
|
|
|
$this->joinedAccountTypes = true;
|
|
|
|
}
|
|
|
|
$query->whereIn('account_types.type', $types);
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
|
|
|
|
2015-05-23 00:47:36 -05:00
|
|
|
/**
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2017-12-29 02:05:35 -06:00
|
|
|
*
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param mixed $value
|
2017-12-29 02:05:35 -06:00
|
|
|
*
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2015-05-23 00:47:36 -05:00
|
|
|
*/
|
2018-07-22 09:35:46 -05:00
|
|
|
public function setVirtualBalanceAttribute($value): void
|
2015-05-23 00:47:36 -05:00
|
|
|
{
|
2022-12-29 12:42:26 -06:00
|
|
|
$value = (string)$value;
|
2021-03-28 04:46:23 -05:00
|
|
|
if ('' === $value) {
|
2020-04-13 00:57:32 -05:00
|
|
|
$value = null;
|
|
|
|
}
|
|
|
|
$this->attributes['virtual_balance'] = $value;
|
2015-05-23 00:47:36 -05:00
|
|
|
}
|
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
2016-04-06 02:27:45 -05:00
|
|
|
* @return HasMany
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2015-02-27 09:48:33 -06:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function transactions(): HasMany
|
2015-02-27 09:48:33 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(Transaction::class);
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
|
|
|
}
|