firefly-iii/app/Models/TransactionJournal.php

132 lines
3.3 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-05 22:04:06 -06:00
class TransactionJournal extends Model
{
2015-02-05 21:52:16 -06:00
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
}
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
}
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
}
public function getDates()
{
2015-02-07 06:15:40 -06:00
return ['created_at', 'updated_at', 'date', 'deleted_at'];
}
2015-02-05 22:14:27 -06:00
public function getDescriptionAttribute($value)
{
if ($this->encrypted) {
return Crypt::decrypt($value);
}
// @codeCoverageIgnoreStart
return $value;
// @codeCoverageIgnoreEnd
}
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
}
/**
* @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);
}
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;
}
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
}
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
}
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
}
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
}
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
}