2014-06-29 15:12:33 -05:00
|
|
|
<?php
|
2014-12-06 05:12:55 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletingTrait;
|
|
|
|
use Watson\Validating\ValidatingTrait;
|
2014-12-20 08:00:53 -06:00
|
|
|
use \Illuminate\Database\Eloquent\Model as Eloquent;
|
2014-06-29 15:12:33 -05:00
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* Class Account
|
|
|
|
*/
|
2014-12-06 05:12:55 -06:00
|
|
|
class Account extends Eloquent
|
|
|
|
{
|
|
|
|
use SoftDeletingTrait, ValidatingTrait;
|
2014-07-15 10:09:59 -05:00
|
|
|
/**
|
|
|
|
* Validation rules.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-06-30 08:46:12 -05:00
|
|
|
public static $rules
|
2014-12-13 14:59:02 -06:00
|
|
|
= [
|
2014-11-25 13:44:46 -06:00
|
|
|
'name' => ['required', 'between:1,100'],
|
2014-11-17 13:45:55 -06:00
|
|
|
'user_id' => 'required|exists:users,id',
|
|
|
|
'account_type_id' => 'required|exists:account_types,id',
|
|
|
|
'active' => 'required|boolean'
|
2014-07-09 06:19:30 -05:00
|
|
|
|
|
|
|
];
|
2014-12-13 14:59:02 -06:00
|
|
|
protected $dates = ['deleted_at', 'created_at', 'updated_at'];
|
2014-11-10 11:38:58 -06:00
|
|
|
protected $fillable = ['name', 'user_id', 'account_type_id', 'active'];
|
2014-06-29 15:12:33 -05:00
|
|
|
|
|
|
|
/**
|
2014-11-12 15:37:44 -06:00
|
|
|
* Account type.
|
2014-06-29 15:12:33 -05:00
|
|
|
*
|
2014-11-12 15:37:44 -06:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2014-06-29 15:12:33 -05:00
|
|
|
*/
|
2014-11-12 15:37:44 -06:00
|
|
|
public function accountType()
|
2014-06-29 15:12:33 -05:00
|
|
|
{
|
2014-11-12 15:37:44 -06:00
|
|
|
return $this->belongsTo('AccountType');
|
2014-07-06 08:18:11 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 00:33:18 -06:00
|
|
|
/**
|
2014-12-05 15:50:26 -06:00
|
|
|
*
|
2014-12-05 14:39:34 -06:00
|
|
|
* @param $fieldName
|
2014-11-17 00:33:18 -06:00
|
|
|
*
|
2014-12-05 14:39:34 -06:00
|
|
|
* @return mixed
|
2014-11-17 00:33:18 -06:00
|
|
|
*/
|
2014-12-05 14:39:34 -06:00
|
|
|
public function getMeta($fieldName)
|
2014-11-17 00:33:18 -06:00
|
|
|
{
|
2014-12-05 14:39:34 -06:00
|
|
|
foreach ($this->accountMeta as $meta) {
|
|
|
|
if ($meta->name == $fieldName) {
|
|
|
|
return $meta->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
2014-11-17 00:33:18 -06:00
|
|
|
}
|
2014-11-14 02:07:13 -06:00
|
|
|
|
2014-08-17 01:45:22 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function piggybanks()
|
|
|
|
{
|
|
|
|
return $this->hasMany('Piggybank');
|
|
|
|
}
|
|
|
|
|
2014-08-10 11:22:42 -05:00
|
|
|
/**
|
2014-11-21 04:11:52 -06:00
|
|
|
*
|
2014-11-10 11:38:58 -06:00
|
|
|
* @param Builder $query
|
|
|
|
* @param array $types
|
|
|
|
*/
|
|
|
|
public function scopeAccountTypeIn(Builder $query, array $types)
|
|
|
|
{
|
|
|
|
if (is_null($this->joinedAccountTypes)) {
|
|
|
|
$query->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id');
|
2014-08-30 07:26:33 -05:00
|
|
|
$this->joinedAccountTypes = true;
|
|
|
|
}
|
2014-11-10 11:38:58 -06:00
|
|
|
$query->whereIn('account_types.type', $types);
|
2014-08-30 07:26:33 -05:00
|
|
|
}
|
|
|
|
|
2014-12-05 14:39:34 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param Builder $query
|
|
|
|
*/
|
|
|
|
public function scopeWithMeta(Builder $query)
|
|
|
|
{
|
|
|
|
$query->with(['accountmeta']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transactions.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function transactions()
|
|
|
|
{
|
|
|
|
return $this->hasMany('Transaction');
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* @param $fieldName
|
|
|
|
* @param $fieldValue
|
|
|
|
*
|
|
|
|
* @return AccountMeta
|
|
|
|
*/
|
2014-12-05 14:39:34 -06:00
|
|
|
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);
|
2014-12-06 05:12:55 -06:00
|
|
|
$meta->name = $fieldName;
|
2014-12-05 14:39:34 -06:00
|
|
|
$meta->data = $fieldValue;
|
|
|
|
$meta->save();
|
|
|
|
|
|
|
|
return $meta;
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2014-12-05 14:39:34 -06:00
|
|
|
public function accountMeta()
|
|
|
|
{
|
|
|
|
return $this->hasMany('AccountMeta');
|
2014-11-21 04:11:52 -06:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:37:44 -06:00
|
|
|
/**
|
|
|
|
* User
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('User');
|
|
|
|
}
|
|
|
|
|
2014-08-30 07:26:33 -05:00
|
|
|
|
|
|
|
}
|