firefly-iii/app/Models/TransactionJournal.php

245 lines
6.6 KiB
PHP
Raw Normal View History

2015-02-05 21:52:16 -06:00
<?php namespace FireflyIII\Models;
use Carbon\Carbon;
2015-02-06 23:49:24 -06:00
use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
2015-02-22 01:38:46 -06:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
2015-02-09 00:56:24 -06:00
use Watson\Validating\ValidatingTrait;
2015-02-09 00:56:24 -06:00
/**
* Class TransactionJournal
*
* @package FireflyIII\Models
*/
2015-02-05 22:04:06 -06:00
class TransactionJournal extends Model
{
2015-02-09 00:56:24 -06:00
use SoftDeletes, ValidatingTrait;
protected $fillable = ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted'];
protected $rules
= [
'user_id' => 'required|exists:users,id',
'transaction_type_id' => 'required|exists:transaction_types,id',
'bill_id' => 'exists:bills,id',
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
'description' => 'required|between:1,1024',
'completed' => 'required|boolean',
'date' => 'required|date',
'encrypted' => 'required|boolean'
];
2015-02-05 21:52:16 -06:00
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function bill()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\Models\Bill');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2015-02-05 22:14:27 -06:00
public function budgets()
{
2015-02-05 22:35:00 -06:00
return $this->belongsToMany('FireflyIII\Models\Budget');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2015-02-05 22:14:27 -06:00
public function categories()
{
2015-02-05 22:35:00 -06:00
return $this->belongsToMany('FireflyIII\Models\Category');
2015-02-05 22:14:27 -06:00
}
2015-04-07 03:14:10 -05:00
/**
* @return float
*/
public function getAmountAttribute()
{
/** @var Transaction $t */
foreach ($this->transactions as $t) {
if ($t->amount > 0) {
return floatval($t->amount);
}
}
}
2015-02-11 00:35:10 -06:00
/**
* @return array
*/
public function getDates()
{
2015-02-07 06:15:40 -06:00
return ['created_at', 'updated_at', 'date', 'deleted_at'];
}
2015-02-11 00:35:10 -06:00
/**
* @param $value
*
* @return string
*/
2015-02-05 22:14:27 -06:00
public function getDescriptionAttribute($value)
{
if ($this->encrypted) {
return Crypt::decrypt($value);
}
// @codeCoverageIgnoreStart
return $value;
// @codeCoverageIgnoreEnd
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-02-05 22:14:27 -06:00
public function piggyBankEvents()
{
2015-02-05 22:35:00 -06:00
return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
2015-02-05 22:14:27 -06:00
}
2015-02-22 08:40:13 -06:00
/**
* @param EloquentBuilder $query
* @param Account $account
*/
public function scopeAccountIs(EloquentBuilder $query, Account $account)
{
if (!isset($this->joinedTransactions)) {
$query->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id');
$this->joinedTransactions = true;
}
$query->where('transactions.account_id', $account->id);
}
/**
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return mixed
*/
public function scopeAfter(EloquentBuilder $query, Carbon $date)
{
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
}
/**
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return mixed
*/
public function scopeBefore(EloquentBuilder $query, Carbon $date)
{
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
}
/**
* @param EloquentBuilder $query
* @param $amount
*/
public function scopeLessThan(EloquentBuilder $query, $amount)
{
if (is_null($this->joinedTransactions)) {
$query->leftJoin(
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
);
$this->joinedTransactions = true;
}
$query->where('transactions.amount', '<=', $amount);
}
2015-02-22 08:40:13 -06:00
/**
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return mixed
*/
public function scopeOnDate(EloquentBuilder $query, Carbon $date)
{
return $query->where('date', '=', $date->format('Y-m-d'));
}
/**
* @param EloquentBuilder $query
* @param array $types
*/
public function scopeTransactionTypes(EloquentBuilder $query, array $types)
{
if (is_null($this->joinedTransactionTypes)) {
$query->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
);
$this->joinedTransactionTypes = true;
}
$query->whereIn('transaction_types.type', $types);
}
2015-02-22 08:40:13 -06:00
/**
* Automatically includes the 'with' parameters to get relevant related
* objects.
*
* @param EloquentBuilder $query
*/
public function scopeWithRelevantData(EloquentBuilder $query)
{
$query->with(
['transactions' => function (HasMany $q) {
$q->orderBy('amount', 'ASC');
}, 'transactiontype', 'transactioncurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill', 'budgets', 'categories']
);
}
2015-02-11 00:35:10 -06:00
/**
* @param $value
*/
2015-02-05 22:14:27 -06:00
public function setDescriptionAttribute($value)
{
2015-02-05 22:35:00 -06:00
$this->attributes['description'] = \Crypt::encrypt($value);
2015-02-05 22:14:27 -06:00
$this->attributes['encrypted'] = true;
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function transactionCurrency()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function transactionType()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\Models\TransactionType');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2015-02-05 22:14:27 -06:00
public function transactiongroups()
{
2015-02-05 22:35:00 -06:00
return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-02-05 22:14:27 -06:00
public function transactions()
{
2015-02-05 22:35:00 -06:00
return $this->hasMany('FireflyIII\Models\Transaction');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function user()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\User');
2015-02-05 22:14:27 -06:00
}
2015-02-05 21:52:16 -06:00
}