firefly-iii/app/Models/Account.php

399 lines
11 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* Account.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Models;
2015-02-27 09:48:33 -06:00
use Carbon\Carbon;
2015-03-29 14:27:51 -05:00
use Crypt;
2016-07-02 13:40:23 -05:00
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Contracts\Encryption\DecryptException;
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;
2015-02-27 09:48:33 -06:00
use Illuminate\Database\Eloquent\SoftDeletes;
2015-04-03 15:54:21 -05:00
use Illuminate\Database\Query\JoinClause;
2016-01-09 08:39:34 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2015-02-27 09:48:33 -06:00
use Watson\Validating\ValidatingTrait;
2016-11-18 13:06:08 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class Account.
2016-11-18 13:06:08 -06:00
*/
2015-02-27 09:48:33 -06:00
class Account extends Model
{
use SoftDeletes, ValidatingTrait;
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',
];
/** @var array */
2015-07-05 02:19:51 -05:00
protected $fillable = ['user_id', 'account_type_id', 'name', 'active', 'virtual_balance', 'iban'];
/** @var array */
2016-12-24 10:36:51 -06:00
protected $hidden = ['encrypted'];
2017-12-17 07:30:53 -06:00
/**
* @var array
*/
2015-02-27 09:48:33 -06:00
protected $rules
2017-12-22 11:32:43 -06:00
= [
2015-02-27 09:48:33 -06:00
'user_id' => 'required|exists:users,id',
'account_type_id' => 'required|exists:account_types,id',
2016-08-11 11:44:11 -05:00
'name' => 'required|between:1,200',
2016-01-15 15:32:21 -06:00
'active' => 'required|boolean',
2016-08-11 11:44:11 -05:00
'iban' => 'between:1,50|iban',
2015-02-27 09:48:33 -06:00
];
2017-11-15 05:25:49 -06:00
/** @var bool */
private $joinedAccountTypes;
2015-02-27 09:48:33 -06:00
2015-04-03 15:54:21 -05:00
/**
* @param array $fields
*
2017-01-02 03:05:02 -06:00
* @return Account
2017-11-15 05:25:49 -06:00
*
2017-01-02 03:05:02 -06:00
* @throws FireflyException
2015-04-03 15:54:21 -05:00
*/
public static function firstOrCreateEncrypted(array $fields)
{
2017-01-02 03:05:02 -06:00
if (!isset($fields['user_id'])) {
throw new FireflyException('Missing required field "user_id".');
}
2015-04-03 15:54:21 -05:00
// everything but the name:
2016-12-15 14:35:33 -06:00
$query = self::orderBy('id');
2015-07-07 02:46:19 -05:00
$search = $fields;
unset($search['name'], $search['iban']);
foreach ($search as $name => $value) {
$query->where($name, $value);
2015-04-03 15:54:21 -05:00
}
$set = $query->get(['accounts.*']);
2017-01-02 03:05:02 -06:00
// account must have a name. If not set, use IBAN.
if (!isset($fields['name'])) {
$fields['name'] = $fields['iban'];
}
2015-04-03 15:54:21 -05:00
/** @var Account $account */
foreach ($set as $account) {
2017-07-15 09:41:07 -05:00
if ($account->name === $fields['name']) {
2015-04-03 15:54:21 -05:00
return $account;
}
}
2015-07-05 07:37:36 -05:00
2015-04-03 15:54:21 -05:00
// create it!
2016-12-15 14:35:33 -06:00
$account = self::create($fields);
2015-04-07 11:26:14 -05:00
return $account;
2015-04-03 15:54:21 -05:00
}
/**
* @param string $value
*
* @return Account
*/
2018-02-07 04:15:36 -06:00
public static function routeBinder($guard, string $value): Account
{
2018-02-07 04:15:36 -06:00
if ($guard->check()) {
$accountId = intval($value);
2018-02-07 04:15:36 -06:00
$account = $guard->user()->accounts()->find($accountId);
if (!is_null($account)) {
return $account;
}
}
throw new NotFoundHttpException;
}
2015-03-30 13:08:27 -05:00
/**
2016-04-06 02:27:45 -05:00
* @return HasMany
* @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
{
return $this->hasMany('FireflyIII\Models\AccountMeta');
}
/**
2016-04-06 02:27:45 -05:00
* @return BelongsTo
* @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
{
return $this->belongsTo('FireflyIII\Models\AccountType');
}
/**
* @return string
* @codeCoverageIgnore
*/
public function getEditNameAttribute(): string
{
$name = $this->name;
2017-11-15 05:25:49 -06:00
if (AccountType::CASH === $this->accountType->type) {
return '';
}
return $name;
}
2015-07-03 05:51:14 -05:00
/**
* @param $value
*
* @return string
2017-11-15 05:25:49 -06:00
*
* @throws FireflyException
2015-07-03 05:51:14 -05:00
*/
2016-04-06 02:27:45 -05:00
public function getIbanAttribute($value): string
2015-07-03 05:51:14 -05:00
{
2017-11-15 05:25:49 -06:00
if (null === $value || 0 === strlen(strval($value))) {
2016-04-06 02:27:45 -05:00
return '';
}
2016-07-02 13:40:23 -05:00
try {
$result = Crypt::decrypt($value);
} catch (DecryptException $e) {
throw new FireflyException('Cannot decrypt value "' . $value . '" for account #' . $this->id);
}
2017-11-15 05:25:49 -06:00
if (null === $result) {
2016-04-06 02:27:45 -05:00
return '';
2015-07-03 05:51:14 -05:00
}
2016-04-06 02:27:45 -05:00
return $result;
2015-07-03 05:51:14 -05:00
}
2015-02-27 09:48:33 -06:00
/**
* @codeCoverageIgnore
2017-12-29 02:05:35 -06:00
*
* @param string $fieldName
*
2016-02-10 23:30:09 -06:00
* @return string
2015-02-27 09:48:33 -06:00
*/
2016-04-06 02:27:45 -05:00
public function getMeta(string $fieldName): string
2015-02-27 09:48:33 -06:00
{
foreach ($this->accountMeta as $meta) {
2017-07-15 09:41:07 -05:00
if ($meta->name === $fieldName) {
return strval($meta->data);
2015-02-27 09:48:33 -06:00
}
}
2016-02-10 23:30:09 -06:00
return '';
2015-02-27 09:48:33 -06:00
}
2015-04-03 15:54:21 -05:00
/**
* @codeCoverageIgnore
2017-12-29 02:05:35 -06:00
*
2015-04-03 15:54:21 -05:00
* @param $value
*
* @return string
*/
public function getNameAttribute($value): ?string
2015-04-03 15:54:21 -05:00
{
2016-12-24 10:36:51 -06:00
if ($this->encrypted) {
2015-04-03 15:54:21 -05:00
return Crypt::decrypt($value);
}
2015-03-01 00:50:26 -06:00
2015-04-03 15:54:21 -05:00
return $value;
}
2015-03-01 00:50:26 -06:00
/**
2017-11-15 05:25:49 -06:00
* Returns the opening balance.
*
* @return TransactionJournal
*/
public function getOpeningBalance(): TransactionJournal
{
$journal = TransactionJournal::sortCorrectly()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $this->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
2017-11-15 05:25:49 -06:00
if (null === $journal) {
return new TransactionJournal;
}
return $journal;
}
2016-10-09 23:50:24 -05:00
/**
* Returns the amount of the opening balance for this account.
*
* @return string
2017-11-15 05:25:49 -06:00
*
* @throws FireflyException
*/
public function getOpeningBalanceAmount(): string
{
2016-11-28 13:38:03 -06:00
$journal = TransactionJournal::sortCorrectly()
2016-12-14 11:59:12 -06:00
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $this->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
2017-11-15 05:25:49 -06:00
if (null === $journal) {
return '0';
}
$count = $journal->transactions()->count();
2017-11-15 05:25:49 -06:00
if (2 !== $count) {
throw new FireflyException(sprintf('Cannot use getFirstTransaction on journal #%d', $journal->id));
}
$transaction = $journal->transactions()->where('account_id', $this->id)->first();
2017-11-15 05:25:49 -06:00
if (null === $transaction) {
return '0';
}
return strval($transaction->amount);
}
/**
2017-11-15 05:25:49 -06:00
* Returns the date of the opening balance for this account. If no date, will return 01-01-1900.
*
* @return Carbon
2016-10-09 23:50:24 -05:00
*/
public function getOpeningBalanceDate(): Carbon
2016-10-09 23:50:24 -05:00
{
$date = new Carbon('1900-01-01');
2016-11-28 13:38:03 -06:00
$journal = TransactionJournal::sortCorrectly()
2016-12-14 11:59:12 -06:00
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $this->id)
->transactionTypes([TransactionType::OPENING_BALANCE])
->first(['transaction_journals.*']);
2017-11-15 05:25:49 -06:00
if (null === $journal) {
return $date;
2016-10-09 23:50:24 -05:00
}
return $journal->date;
2016-10-09 23:50:24 -05:00
}
2018-02-07 04:15:36 -06:00
/**
* @codeCoverageIgnore
* Get all of the notes.
*/
public function notes()
{
return $this->morphMany(Note::class, 'noteable');
}
2015-02-27 09:48:33 -06:00
/**
2016-04-06 02:27:45 -05:00
* @return HasMany
* @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
{
2015-03-30 13:08:27 -05:00
return $this->hasMany('FireflyIII\Models\PiggyBank');
2015-02-27 09:48:33 -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
*/
2015-03-30 13:08:27 -05:00
public function scopeAccountTypeIn(EloquentBuilder $query, array $types)
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
}
/**
* @codeCoverageIgnore
2017-12-29 02:05:35 -06:00
*
2015-04-03 15:54:21 -05:00
* @param EloquentBuilder $query
* @param string $name
* @param string $value
2015-02-27 09:48:33 -06:00
*/
2015-04-03 15:54:21 -05:00
public function scopeHasMetaValue(EloquentBuilder $query, $name, $value)
2015-02-27 09:48:33 -06:00
{
2015-04-03 15:54:21 -05:00
$joinName = str_replace('.', '_', $name);
$query->leftJoin(
2017-11-15 03:52:29 -06:00
'account_meta as ' . $joinName,
function (JoinClause $join) use ($joinName, $name) {
$join->on($joinName . '.account_id', '=', 'accounts.id')->where($joinName . '.name', '=', $name);
}
2015-04-03 15:54:21 -05:00
);
$query->where($joinName . '.data', json_encode($value));
2015-02-27 09:48:33 -06:00
}
2015-07-03 05:51:14 -05:00
/**
* @codeCoverageIgnore
2017-12-29 02:05:35 -06:00
*
2015-07-03 05:51:14 -05:00
* @param $value
2017-12-29 02:05:35 -06:00
*
* @codeCoverageIgnore
2015-07-03 05:51:14 -05:00
*/
public function setIbanAttribute($value)
{
$this->attributes['iban'] = Crypt::encrypt($value);
}
/**
* @codeCoverageIgnore
2017-12-29 02:05:35 -06:00
*
2015-04-03 15:54:21 -05:00
* @param $value
*/
2015-04-03 15:54:21 -05:00
public function setNameAttribute($value)
{
$encrypt = config('firefly.encryption');
$this->attributes['name'] = $encrypt ? Crypt::encrypt($value) : $value;
$this->attributes['encrypted'] = $encrypt;
}
2015-05-23 00:47:36 -05: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
*
* @codeCoverageIgnore
2015-05-23 00:47:36 -05:00
*/
public function setVirtualBalanceAttribute($value)
{
$this->attributes['virtual_balance'] = strval($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
* @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
{
return $this->hasMany('FireflyIII\Models\Transaction');
}
/**
2016-04-06 02:27:45 -05:00
* @return BelongsTo
* @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
{
return $this->belongsTo('FireflyIII\User');
}
}