diff --git a/app/models/Account.php b/app/models/Account.php index bffa32415f..eda2c9d90c 100644 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -1,22 +1,21 @@ hasMany('Transaction'); + foreach ($this->accountMeta as $meta) { + if ($meta->name == $fieldName) { + return $meta->data; + } + } + + return null; + } /** @@ -87,12 +93,57 @@ class Account extends Ardent $query->whereIn('account_types.type', $types); } + /** + * + * @param Builder $query + */ + public function scopeWithMeta(Builder $query) + { + $query->with(['accountmeta']); + } + /** * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough */ public function transactionjournals() { - return $this->hasManyThrough('TransactionJournal', 'Transaction','transaction_journal_id','id'); + return $this->hasManyThrough('TransactionJournal', 'Transaction', 'transaction_journal_id', 'id'); + } + + /** + * Transactions. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function transactions() + { + return $this->hasMany('Transaction'); + } + + public function updateMeta($fieldName, $fieldValue) + { + $meta = $this->accountMeta()->get(); + /** @var AccountMeta $entry */ + foreach ($meta as $entry) { + if ($entry->name == $fieldName) { + $entry->data = $fieldValue; + $entry->save(); + + return $entry; + } + } + $meta = new AccountMeta; + $meta->account()->associate($this); + $meta->name = $fieldName; + $meta->data = $fieldValue; + $meta->save(); + + return $meta; + } + + public function accountMeta() + { + return $this->hasMany('AccountMeta'); } /** diff --git a/app/models/AccountMeta.php b/app/models/AccountMeta.php index ada62b614a..a5d47aada7 100644 --- a/app/models/AccountMeta.php +++ b/app/models/AccountMeta.php @@ -16,13 +16,13 @@ class AccountMeta extends Ardent = [ 'account_id' => 'numeric|required|exists:accounts,id', 'name' => 'required|between:1,250', - 'data' => 'required']; - + 'data' => 'required' + ]; /** * @var array */ protected $fillable = ['account_id', 'name', 'date']; - + protected $table = 'account_meta'; /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo @@ -32,4 +32,22 @@ class AccountMeta extends Ardent return $this->belongsTo('Account'); } + /** + * @param $value + * + * @return mixed + */ + public function getDataAttribute($value) + { + return json_decode($value); + } + + /** + * @param $value + */ + public function setDataAttribute($value) + { + $this->attributes['data'] = json_encode($value); + } + } \ No newline at end of file