2015-02-27 09:48:33 -06:00
|
|
|
<?php namespace FireflyIII\Models;
|
|
|
|
|
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;
|
|
|
|
use Watson\Validating\ValidatingTrait;
|
2015-03-29 14:27:51 -05:00
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
|
|
|
* Class Account
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Models
|
|
|
|
*/
|
|
|
|
class Account extends Model
|
|
|
|
{
|
|
|
|
use SoftDeletes, ValidatingTrait;
|
|
|
|
|
2015-04-01 12:42:14 -05:00
|
|
|
protected $fillable = ['user_id', 'account_type_id', 'name', 'active','virtual_balance'];
|
2015-02-27 09:48:33 -06:00
|
|
|
protected $rules
|
|
|
|
= [
|
|
|
|
'user_id' => 'required|exists:users,id',
|
|
|
|
'account_type_id' => 'required|exists:account_types,id',
|
2015-03-26 12:05:23 -05:00
|
|
|
'name' => 'required|between:1,1024|uniqueAccountForUser',
|
2015-02-27 09:48:33 -06:00
|
|
|
'active' => 'required|boolean'
|
|
|
|
];
|
|
|
|
|
2015-03-30 13:08:27 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function accountMeta()
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\AccountMeta');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function accountType()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('FireflyIII\Models\AccountType');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return ['created_at', 'updated_at', 'deleted_at'];
|
|
|
|
}
|
2015-02-27 09:48:33 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $fieldName
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getMeta($fieldName)
|
|
|
|
{
|
|
|
|
foreach ($this->accountMeta as $meta) {
|
|
|
|
if ($meta->name == $fieldName) {
|
|
|
|
return $meta->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-01 00:50:26 -06:00
|
|
|
|
|
|
|
|
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-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-03-30 13:08:27 -05:00
|
|
|
* @param $value
|
2015-02-27 09:48:33 -06:00
|
|
|
*/
|
2015-03-30 13:08:27 -05:00
|
|
|
public function setNameAttribute($value)
|
2015-02-27 09:48:33 -06:00
|
|
|
{
|
2015-03-30 13:08:27 -05:00
|
|
|
$this->attributes['name'] = Crypt::encrypt($value);
|
|
|
|
$this->attributes['encrypted'] = true;
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
|
|
|
/**
|
2015-03-30 13:08:27 -05:00
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return string
|
2015-02-27 09:48:33 -06:00
|
|
|
*/
|
2015-03-30 13:08:27 -05:00
|
|
|
public function getNameAttribute($value)
|
2015-02-27 09:48:33 -06:00
|
|
|
{
|
2015-03-30 13:08:27 -05:00
|
|
|
|
|
|
|
if (intval($this->encrypted) == 1) {
|
|
|
|
return Crypt::decrypt($value);
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
2015-03-30 13:08:27 -05:00
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
return $value;
|
|
|
|
// @codeCoverageIgnoreEnd
|
2015-02-27 09:48:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-01 02:17:07 -05:00
|
|
|
/**
|
|
|
|
* @param array $fields
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public static function firstOrCreateEncrypted(array $fields) {
|
|
|
|
// everything but the name:
|
|
|
|
$query = Account::orderBy('id');
|
|
|
|
foreach($fields as $name => $value) {
|
|
|
|
if($name != 'name') {
|
|
|
|
$query->where($name,$value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$set = $query->get(['accounts.*']);
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach($set as $account) {
|
|
|
|
if($account->name == $fields['name']) {
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// create it!
|
|
|
|
return Account::create($fields);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-27 09:48:33 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function transactions()
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\Transaction');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('FireflyIII\User');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|