firefly-iii/app/Models/TransactionJournal.php
2015-02-07 13:15:40 +01:00

132 lines
3.3 KiB
PHP

<?php namespace FireflyIII\Models;
use Carbon\Carbon;
use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
class TransactionJournal extends Model
{
public function bill()
{
return $this->belongsTo('FireflyIII\Models\Bill');
}
public function budgets()
{
return $this->belongsToMany('FireflyIII\Models\Budget');
}
public function categories()
{
return $this->belongsToMany('FireflyIII\Models\Category');
}
public function getDates()
{
return ['created_at', 'updated_at', 'date', 'deleted_at'];
}
public function getDescriptionAttribute($value)
{
if ($this->encrypted) {
return Crypt::decrypt($value);
}
// @codeCoverageIgnoreStart
return $value;
// @codeCoverageIgnoreEnd
}
public function piggyBankEvents()
{
return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
}
/**
* @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);
}
/**
* @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);
}
public function setDescriptionAttribute($value)
{
$this->attributes['description'] = \Crypt::encrypt($value);
$this->attributes['encrypted'] = true;
}
public function transactionCurrency()
{
return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
}
public function transactionType()
{
return $this->belongsTo('FireflyIII\Models\TransactionType');
}
public function transactiongroups()
{
return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
}
public function transactions()
{
return $this->hasMany('FireflyIII\Models\Transaction');
}
public function user()
{
return $this->belongsTo('FireflyIII\User');
}
}