2015-02-27 09:48:33 -06:00
|
|
|
<?php namespace FireflyIII\Models;
|
|
|
|
|
2016-01-09 08:39:34 -06:00
|
|
|
use Auth;
|
2015-03-29 14:27:51 -05:00
|
|
|
use Crypt;
|
2015-02-27 09:48:33 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
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-01-15 10:38:09 -06:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2016-01-09 01:20:55 -06:00
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
2016-01-01 14:49:27 -06:00
|
|
|
* FireflyIII\Models\Account
|
2015-02-27 09:48:33 -06:00
|
|
|
*
|
2016-01-09 08:39:34 -06:00
|
|
|
* @property integer $id
|
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
|
|
|
* @property \Carbon\Carbon $deleted_at
|
|
|
|
* @property integer $user_id
|
|
|
|
* @property integer $account_type_id
|
|
|
|
* @property string $name
|
|
|
|
* @property boolean $active
|
|
|
|
* @property boolean $encrypted
|
|
|
|
* @property float $virtual_balance
|
|
|
|
* @property string $iban
|
2016-01-09 01:20:55 -06:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\AccountMeta[] $accountMeta
|
2016-01-09 08:39:34 -06:00
|
|
|
* @property-read \FireflyIII\Models\AccountType $accountType
|
|
|
|
* @property-read mixed $name_for_editform
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBank[] $piggyBanks
|
2016-01-09 01:20:55 -06:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions
|
2016-01-09 08:39:34 -06:00
|
|
|
* @property-read \FireflyIII\User $user
|
2016-01-15 10:38:09 -06:00
|
|
|
* @method static Builder|\FireflyIII\Models\Account accountTypeIn($types)
|
|
|
|
* @method static Builder|\FireflyIII\Models\Account hasMetaValue($name, $value)
|
2016-01-09 08:39:34 -06:00
|
|
|
* @property string $startBalance
|
|
|
|
* @property string $endBalance
|
2015-02-27 09:48:33 -06:00
|
|
|
*/
|
|
|
|
class Account extends Model
|
|
|
|
{
|
|
|
|
use SoftDeletes, ValidatingTrait;
|
|
|
|
|
2015-07-05 02:19:51 -05:00
|
|
|
protected $fillable = ['user_id', 'account_type_id', 'name', 'active', 'virtual_balance', 'iban'];
|
2015-05-23 10:11:16 -05:00
|
|
|
protected $hidden = ['virtual_balance_encrypted', 'encrypted'];
|
2015-02-27 09:48:33 -06:00
|
|
|
protected $rules
|
2015-04-03 15:54:21 -05:00
|
|
|
= [
|
2015-02-27 09:48:33 -06:00
|
|
|
'user_id' => 'required|exists:users,id',
|
|
|
|
'account_type_id' => 'required|exists:account_types,id',
|
2015-07-05 02:19:51 -05:00
|
|
|
'name' => 'required',
|
2015-02-27 09:48:33 -06:00
|
|
|
'active' => 'required|boolean'
|
|
|
|
];
|
|
|
|
|
2015-04-03 15:54:21 -05:00
|
|
|
/**
|
|
|
|
* @param array $fields
|
2015-05-10 06:22:00 -05:00
|
|
|
*
|
2015-04-03 15:54:21 -05:00
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public static function firstOrCreateEncrypted(array $fields)
|
|
|
|
{
|
|
|
|
// everything but the name:
|
2015-07-07 02:46:19 -05:00
|
|
|
$query = Account::orderBy('id');
|
|
|
|
$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.*']);
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($set as $account) {
|
|
|
|
if ($account->name == $fields['name']) {
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
}
|
2015-07-05 07:37:36 -05: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
|
|
|
// create it!
|
|
|
|
$account = Account::create($fields);
|
2015-04-07 11:26:14 -05:00
|
|
|
|
2015-04-03 18:36:55 -05:00
|
|
|
return $account;
|
2015-04-03 15:54:21 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-08 00:27:29 -05:00
|
|
|
/**
|
|
|
|
* @param array $fields
|
|
|
|
*
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public static function firstOrNullEncrypted(array $fields)
|
|
|
|
{
|
|
|
|
// everything but the name:
|
2015-07-07 02:46:19 -05:00
|
|
|
$query = Account::orderBy('id');
|
|
|
|
$search = $fields;
|
|
|
|
unset($search['name']);
|
|
|
|
foreach ($search as $name => $value) {
|
|
|
|
$query->where($name, $value);
|
2015-05-08 00:27:29 -05:00
|
|
|
}
|
|
|
|
$set = $query->get(['accounts.*']);
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($set as $account) {
|
|
|
|
if ($account->name == $fields['name']) {
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-03-30 13:08:27 -05:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-03-30 13:08:27 -05:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function accountMeta()
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\AccountMeta');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-03-30 13:08:27 -05:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function accountType()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('FireflyIII\Models\AccountType');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return string[]
|
2015-03-30 13:08:27 -05:00
|
|
|
*/
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return ['created_at', 'updated_at', 'deleted_at'];
|
|
|
|
}
|
2015-02-27 09:48:33 -06:00
|
|
|
|
2015-07-03 05:51:14 -05:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIbanAttribute($value)
|
|
|
|
{
|
|
|
|
if (is_null($value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Crypt::decrypt($value);
|
|
|
|
}
|
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @param string $fieldName
|
2015-05-23 10:33:04 -05:00
|
|
|
*
|
2015-05-23 10:11:16 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-02-27 09:48:33 -06:00
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getMeta($fieldName)
|
|
|
|
{
|
|
|
|
foreach ($this->accountMeta as $meta) {
|
|
|
|
if ($meta->name == $fieldName) {
|
|
|
|
return $meta->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-03 15:54:21 -05:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2015-04-03 15:54:21 -05:00
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNameAttribute($value)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (intval($this->encrypted) == 1) {
|
|
|
|
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
|
|
|
|
2015-06-25 21:57:30 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNameForEditformAttribute()
|
|
|
|
{
|
|
|
|
$name = $this->name;
|
|
|
|
if ($this->accountType->type == 'Cash account') {
|
|
|
|
$name = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-02-27 09:48:33 -06:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2015-03-30 13:08:27 -05:00
|
|
|
public function piggyBanks()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-10 06:40:29 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05: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
|
|
|
{
|
2015-03-30 13:08:27 -05:00
|
|
|
if (is_null($this->joinedAccountTypes)) {
|
|
|
|
$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-10 06:40:29 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05: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(
|
2015-06-06 16:09:12 -05:00
|
|
|
'account_meta as ' . $joinName, function (JoinClause $join) use ($joinName, $name) {
|
2015-04-03 15:54:21 -05:00
|
|
|
$join->on($joinName . '.account_id', '=', 'accounts.id')->where($joinName . '.name', '=', $name);
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setIbanAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['iban'] = Crypt::encrypt($value);
|
|
|
|
}
|
|
|
|
|
2015-04-01 02:17:07 -05:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2015-04-03 15:54:21 -05:00
|
|
|
* @param $value
|
2015-04-01 02:17:07 -05:00
|
|
|
*/
|
2015-04-03 15:54:21 -05:00
|
|
|
public function setNameAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['name'] = Crypt::encrypt($value);
|
|
|
|
$this->attributes['encrypted'] = true;
|
2015-04-01 02:17:07 -05:00
|
|
|
}
|
|
|
|
|
2015-05-23 00:47:36 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
2015-05-23 10:33:04 -05:00
|
|
|
*
|
2015-05-23 10:11:16 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-23 00:47:36 -05:00
|
|
|
*/
|
|
|
|
public function setVirtualBalanceAttribute($value)
|
|
|
|
{
|
2015-05-24 01:00:40 -05:00
|
|
|
$this->attributes['virtual_balance'] = strval(round($value, 2));
|
2015-05-23 00:47:36 -05:00
|
|
|
}
|
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-02-27 09:48:33 -06:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function transactions()
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\Transaction');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-02-27 09:48:33 -06:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('FireflyIII\User');
|
|
|
|
}
|
|
|
|
|
2016-01-15 10:38:09 -06:00
|
|
|
/**
|
|
|
|
* @param Account $value
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
2016-01-09 08:39:34 -06:00
|
|
|
public static function routeBinder(Account $value)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
if ($value->user_id == Auth::user()->id) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|