mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-28 09:51:21 -06:00
183 lines
4.5 KiB
PHP
183 lines
4.5 KiB
PHP
<?php namespace FireflyIII\Models;
|
|
|
|
use App;
|
|
use Crypt;
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Database\Query\JoinClause;
|
|
use Watson\Validating\ValidatingTrait;
|
|
|
|
/**
|
|
* Class Account
|
|
*
|
|
* @package FireflyIII\Models
|
|
*/
|
|
class Account extends Model
|
|
{
|
|
use SoftDeletes, ValidatingTrait;
|
|
|
|
protected $fillable = ['user_id', 'account_type_id', 'name', 'active', 'virtual_balance'];
|
|
protected $rules
|
|
= [
|
|
'user_id' => 'required|exists:users,id',
|
|
'account_type_id' => 'required|exists:account_types,id',
|
|
'name' => 'required|between:1,1024|uniqueAccountForUser',
|
|
'active' => 'required|boolean'
|
|
];
|
|
|
|
/**
|
|
* @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!
|
|
$account = Account::create($fields);
|
|
if (is_null($account->id)) {
|
|
// could not create account:
|
|
App::abort(500, 'Could not create new account with data: ' . json_encode($fields));
|
|
|
|
}
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
/**
|
|
* @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'];
|
|
}
|
|
|
|
/**
|
|
* @param $fieldName
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function getMeta($fieldName)
|
|
{
|
|
foreach ($this->accountMeta as $meta) {
|
|
if ($meta->name == $fieldName) {
|
|
return $meta->data;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
* @param $value
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getNameAttribute($value)
|
|
{
|
|
|
|
if (intval($this->encrypted) == 1) {
|
|
return Crypt::decrypt($value);
|
|
}
|
|
|
|
// @codeCoverageIgnoreStart
|
|
return $value;
|
|
// @codeCoverageIgnoreEnd
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function piggyBanks()
|
|
{
|
|
return $this->hasMany('FireflyIII\Models\PiggyBank');
|
|
}
|
|
|
|
/**
|
|
* @param EloquentBuilder $query
|
|
* @param array $types
|
|
*/
|
|
public function scopeAccountTypeIn(EloquentBuilder $query, array $types)
|
|
{
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* @param EloquentBuilder $query
|
|
* @param string $name
|
|
* @param string $value
|
|
*/
|
|
public function scopeHasMetaValue(EloquentBuilder $query, $name, $value)
|
|
{
|
|
$joinName = str_replace('.', '_', $name);
|
|
$query->leftJoin(
|
|
'account_meta as ' . $joinName, function (JoinClause $join) use ($joinName, $name) {
|
|
$join->on($joinName . '.account_id', '=', 'accounts.id')->where($joinName . '.name', '=', $name);
|
|
}
|
|
);
|
|
$query->where($joinName . '.data', json_encode($value));
|
|
}
|
|
|
|
/**
|
|
* @param $value
|
|
*/
|
|
public function setNameAttribute($value)
|
|
{
|
|
$this->attributes['name'] = Crypt::encrypt($value);
|
|
$this->attributes['encrypted'] = true;
|
|
}
|
|
|
|
/**
|
|
* @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');
|
|
}
|
|
|
|
}
|