firefly-iii/app/models/Account.php

139 lines
4.1 KiB
PHP
Raw Normal View History

<?php
use LaravelBook\Ardent\Ardent as Ardent;
2014-08-30 07:26:33 -05:00
use LaravelBook\Ardent\Builder;
2014-07-15 15:16:29 -05:00
/**
* Account
*
2014-08-31 00:32:48 -05:00
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $user_id
* @property integer $account_type_id
* @property string $name
* @property boolean $active
* @property-read \AccountType $accountType
2014-07-15 15:16:29 -05:00
* @property-read \Illuminate\Database\Eloquent\Collection|\Transaction[] $transactions
2014-08-31 00:32:48 -05:00
* @property-read \Illuminate\Database\Eloquent\Collection|\Piggybank[] $piggybanks
* @property-read \User $user
2014-08-28 00:53:54 -05:00
* @method static \Illuminate\Database\Query\Builder|\Account whereId($value)
* @method static \Illuminate\Database\Query\Builder|\Account whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Account whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Account whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\Account whereAccountTypeId($value)
* @method static \Illuminate\Database\Query\Builder|\Account whereName($value)
* @method static \Illuminate\Database\Query\Builder|\Account whereActive($value)
* @method static \Account accountTypeIn($types)
2014-07-15 15:16:29 -05:00
*/
class Account extends Ardent
{
/**
* Validation rules.
*
* @var array
*/
public static $rules
= [
2014-08-30 07:26:33 -05:00
'name' => ['required', 'between:1,100', 'alphabasic'],
2014-07-09 06:19:30 -05: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
];
/**
* Account type.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function accountType()
{
return $this->belongsTo('AccountType');
}
/**
* Get an accounts current balance.
*
* @param \Carbon\Carbon $date
*
* @return float
*/
public function balance(\Carbon\Carbon $date = null)
{
$date = is_null($date) ? new \Carbon\Carbon : $date;
return floatval(
$this->transactions()
2014-08-30 07:26:33 -05:00
->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)
2014-08-30 07:26:33 -05:00
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
);
}
public function balanceBeforeJournal(TransactionJournal $journal)
{
return floatval(
$this->transactions()
->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)
->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d'))
->where('transaction_journals.created_at', '<=', $journal->created_at->format('Y-m-d H:i:s'))
->where('transaction_journals.id','!=',$journal->id)
->sum('transactions.amount')
);
}
2014-08-10 12:02:24 -05:00
/**
* Transactions.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany('Transaction');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function piggybanks()
{
return $this->hasMany('Piggybank');
}
2014-08-10 11:22:42 -05:00
/**
* @param \Carbon\Carbon $date
*
* @return null
*/
2014-08-10 12:02:24 -05:00
public function predict(
/** @noinspection PhpUnusedParameterInspection */
\Carbon\Carbon $date
2014-08-30 07:26:33 -05:00
)
{
return null;
}
/**
2014-08-10 12:02:24 -05:00
* User
*
2014-08-10 12:02:24 -05:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2014-08-10 12:02:24 -05:00
public function user()
{
2014-08-10 12:02:24 -05:00
return $this->belongsTo('User');
}
public function scopeAccountTypeIn(Builder $query, array $types) {
2014-08-30 07:26:33 -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);
}
}