2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Account.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://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
|
|
|
|
2018-07-22 09:35:46 -05:00
|
|
|
use Carbon\Carbon;
|
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;
|
|
|
|
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;
|
2018-07-22 09:35:46 -05:00
|
|
|
use Illuminate\Support\Collection;
|
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
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class Account.
|
2018-05-19 03:44:33 -05:00
|
|
|
*
|
2018-06-06 14:23:00 -05:00
|
|
|
* @property int $id
|
|
|
|
* @property string $name
|
|
|
|
* @property string $iban
|
2018-05-22 11:05:35 -05:00
|
|
|
* @property AccountType $accountType
|
2018-06-21 11:58:27 -05:00
|
|
|
* @property bool $active
|
|
|
|
* @property string $virtual_balance
|
2018-06-29 22:21:21 -05:00
|
|
|
* @property User $user
|
2018-07-22 09:35:46 -05:00
|
|
|
* @property string startBalance
|
|
|
|
* @property string endBalance
|
2018-07-09 12:24:08 -05:00
|
|
|
* @property string difference
|
2018-07-22 09:35:46 -05:00
|
|
|
* @property Carbon lastActivityDate
|
|
|
|
* @property Collection accountMeta
|
|
|
|
* @property bool encrypted
|
2018-08-05 11:59:15 -05:00
|
|
|
* @property int account_type_id
|
|
|
|
* @property Collection piggyBanks
|
|
|
|
* @property string $interest
|
|
|
|
* @property string $interestPeriod
|
|
|
|
* @property string accountTypeString
|
2018-12-15 00:59:02 -06:00
|
|
|
* @property Carbon created_at
|
|
|
|
* @property Carbon updated_at
|
2019-03-05 10:26:49 -06:00
|
|
|
* @SuppressWarnings (PHPMD.CouplingBetweenObjects)
|
|
|
|
* @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 bool $encrypted
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\AccountMeta[] $accountMeta
|
|
|
|
* @property-read string $edit_name
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Note[] $notes
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBank[] $piggyBanks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account accountTypeIn($types)
|
|
|
|
* @method static bool|null forceDelete()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account newQuery()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Account onlyTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account query()
|
|
|
|
* @method static bool|null restore()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereAccountTypeId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereActive($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereDeletedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereEncrypted($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereIban($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereName($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereUpdatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereUserId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\FireflyIII\Models\Account whereVirtualBalance($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Account withTrashed()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Account withoutTrashed()
|
|
|
|
* @mixin \Eloquent
|
2016-11-18 13:06:08 -06:00
|
|
|
*/
|
2015-02-27 09:48:33 -06:00
|
|
|
class Account extends Model
|
|
|
|
{
|
2018-04-02 08:10:40 -05:00
|
|
|
use SoftDeletes;
|
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',
|
|
|
|
'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 */
|
2016-12-24 10:36:51 -06:00
|
|
|
protected $hidden = ['encrypted'];
|
2017-11-15 05:25:49 -06:00
|
|
|
/** @var bool */
|
2016-01-27 12:35:00 -06:00
|
|
|
private $joinedAccountTypes;
|
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).
|
|
|
|
*
|
2017-12-25 01:45:23 -06:00
|
|
|
* @param string $value
|
2016-01-27 11:31:44 -06:00
|
|
|
*
|
|
|
|
* @return Account
|
2018-08-04 10:30:06 -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()) {
|
2018-04-02 08:10:40 -05:00
|
|
|
$accountId = (int)$value;
|
2018-07-22 09:35:46 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
/** @var Account $account */
|
|
|
|
$account = $user->accounts()->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
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
|
|
|
|
2015-03-30 13:08:27 -05:00
|
|
|
/**
|
2016-04-06 02:27:45 -05:00
|
|
|
* @return HasMany
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2015-03-30 13:08:27 -05:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function accountMeta(): HasMany
|
2015-03-30 13:08:27 -05:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->hasMany(AccountMeta::class);
|
2015-03-30 13:08:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-06 02:27:45 -05:00
|
|
|
* @return BelongsTo
|
2017-12-25 01:45:23 -06:00
|
|
|
* @codeCoverageIgnore
|
2015-03-30 13:08:27 -05:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function accountType(): BelongsTo
|
2015-03-30 13:08:27 -05:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->belongsTo(AccountType::class);
|
2015-03-30 13:08:27 -05:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-14 00:11:30 -05:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Returns the opening balance.
|
2017-04-14 00:11:30 -05:00
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
|
|
|
public function getOpeningBalance(): TransactionJournal
|
|
|
|
{
|
2018-03-11 07:34:16 -05:00
|
|
|
$journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
2017-04-14 00:11:30 -05:00
|
|
|
->where('transactions.account_id', $this->id)
|
|
|
|
->transactionTypes([TransactionType::OPENING_BALANCE])
|
|
|
|
->first(['transaction_journals.*']);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $journal) {
|
2017-04-14 00:11:30 -05:00
|
|
|
return new TransactionJournal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $journal;
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2015-03-30 13:08:27 -05: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
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $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
|
|
|
*
|
2015-05-23 00:47:36 -05:00
|
|
|
* @param $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
|
|
|
{
|
2018-04-02 08:10:40 -05:00
|
|
|
$this->attributes['virtual_balance'] = (string)$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-06 02:27:45 -05:00
|
|
|
* @return BelongsTo
|
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 user(): BelongsTo
|
2015-02-27 09:48:33 -06:00
|
|
|
{
|
2018-04-27 23:23:13 -05:00
|
|
|
return $this->belongsTo(User::class);
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
|
|
|
}
|