2015-02-05 21:52:16 -06:00
|
|
|
<?php namespace FireflyIII\Models;
|
|
|
|
|
2016-01-09 08:53:11 -06:00
|
|
|
use Auth;
|
2015-02-07 01:23:44 -06:00
|
|
|
use Carbon\Carbon;
|
2015-02-06 23:49:24 -06:00
|
|
|
use Crypt;
|
2015-06-03 11:22:47 -05:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2016-01-01 14:49:27 -06:00
|
|
|
use FireflyIII\User;
|
2015-02-07 01:23:44 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2016-01-01 14:49:27 -06:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2015-02-07 01:23:44 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-02-22 01:38:46 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2015-02-07 16:19:28 -06:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2016-01-01 14:49:27 -06:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2016-01-09 08:53:11 -06:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2015-02-07 01:23:44 -06:00
|
|
|
|
2015-02-09 00:56:24 -06:00
|
|
|
/**
|
2016-01-01 14:49:27 -06:00
|
|
|
* FireflyIII\Models\TransactionJournal
|
2015-02-09 00:56:24 -06:00
|
|
|
*
|
2016-01-01 14:49:27 -06:00
|
|
|
* @property integer $id
|
|
|
|
* @property Carbon $created_at
|
|
|
|
* @property Carbon $updated_at
|
|
|
|
* @property Carbon $deleted_at
|
|
|
|
* @property integer $user_id
|
|
|
|
* @property integer $transaction_type_id
|
|
|
|
* @property integer $bill_id
|
|
|
|
* @property integer $transaction_currency_id
|
|
|
|
* @property string $description
|
|
|
|
* @property boolean $completed
|
|
|
|
* @property Carbon $date
|
|
|
|
* @property boolean $encrypted
|
|
|
|
* @property integer $order
|
|
|
|
* @property integer $tag_count
|
|
|
|
* @property-read Bill $bill
|
|
|
|
* @property-read Collection|Budget[] $budgets
|
|
|
|
* @property-read Collection|Category[] $categories
|
|
|
|
* @property-read mixed $amount_positive
|
|
|
|
* @property-read mixed $amount
|
|
|
|
* @property-read Collection|Tag[] $tags
|
|
|
|
* @property-read Collection|Transaction[] $transactions
|
|
|
|
* @property-read mixed $destination_account
|
|
|
|
* @property-read mixed $source_account
|
|
|
|
* @property-read Collection|PiggyBankEvent[] $piggyBankEvents
|
|
|
|
* @property-read Collection|Attachment[] $attachments
|
|
|
|
* @property-read TransactionCurrency $transactionCurrency
|
|
|
|
* @property-read TransactionType $transactionType
|
|
|
|
* @property-read Collection|TransactionGroup[] $transactiongroups
|
|
|
|
* @property-read User $user
|
|
|
|
* @method static Builder|TransactionJournal accountIs($account)
|
|
|
|
* @method static Builder|TransactionJournal after($date)
|
|
|
|
* @method static Builder|TransactionJournal before($date)
|
|
|
|
* @method static Builder|TransactionJournal onDate($date)
|
|
|
|
* @method static Builder|TransactionJournal transactionTypes($types)
|
|
|
|
* @method static Builder|TransactionJournal withRelevantData()
|
2015-02-09 00:56:24 -06:00
|
|
|
*/
|
2015-02-05 22:04:06 -06:00
|
|
|
class TransactionJournal extends Model
|
|
|
|
{
|
2016-01-15 15:20:38 -06:00
|
|
|
use SoftDeletes;
|
2015-02-09 00:56:24 -06:00
|
|
|
|
2015-06-14 04:52:07 -05:00
|
|
|
|
|
|
|
protected $fillable
|
|
|
|
= ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted', 'tag_count'];
|
|
|
|
protected $hidden = ['encrypted'];
|
2016-01-15 15:23:01 -06:00
|
|
|
protected $dates = ['created_at', 'updated_at', 'date', 'deleted_at'];
|
2015-02-05 21:52:16 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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-05-18 10:02:18 -05:00
|
|
|
/**
|
2015-05-24 04:13:46 -05:00
|
|
|
* @return string
|
2015-05-18 10:02:18 -05:00
|
|
|
*/
|
2015-09-25 13:40:24 -05:00
|
|
|
public function getAmountPositiveAttribute()
|
2015-05-18 10:02:18 -05:00
|
|
|
{
|
2015-05-24 04:13:46 -05:00
|
|
|
$amount = '0';
|
2015-05-18 10:02:18 -05:00
|
|
|
/** @var Transaction $t */
|
|
|
|
foreach ($this->transactions as $t) {
|
|
|
|
if ($t->amount > 0) {
|
2015-05-24 04:13:46 -05:00
|
|
|
$amount = $t->amount;
|
2015-05-18 10:02:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
2015-04-07 03:14:10 -05:00
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getAmountAttribute()
|
|
|
|
{
|
2015-06-03 14:25:11 -05:00
|
|
|
$cache = new CacheProperties();
|
|
|
|
$cache->addProperty($this->id);
|
|
|
|
$cache->addProperty('amount');
|
|
|
|
if ($cache->has()) {
|
2015-06-04 14:35:36 -05:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2015-06-03 11:22:47 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 04:13:46 -05:00
|
|
|
bcscale(2);
|
2015-09-25 13:40:24 -05:00
|
|
|
$transaction = $this->transactions->sortByDesc('amount')->first();
|
|
|
|
$amount = $transaction->amount;
|
2015-12-09 18:39:50 -06:00
|
|
|
if ($this->isWithdrawal()) {
|
2015-09-25 13:40:24 -05:00
|
|
|
$amount = $amount * -1;
|
2015-05-17 09:12:00 -05:00
|
|
|
}
|
2015-06-07 02:09:27 -05:00
|
|
|
$cache->store($amount);
|
2015-05-17 09:12:00 -05:00
|
|
|
|
2015-06-07 02:09:27 -05:00
|
|
|
return $amount;
|
2015-06-03 11:22:47 -05:00
|
|
|
|
2015-06-07 02:09:27 -05:00
|
|
|
}
|
2015-05-19 23:50:15 -05:00
|
|
|
|
2015-06-15 12:25:54 -05:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function tags()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany('FireflyIII\Models\Tag');
|
|
|
|
}
|
|
|
|
|
2015-06-03 11:22:47 -05:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function transactions()
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\Transaction');
|
2015-02-07 01:23:44 -06:00
|
|
|
}
|
|
|
|
|
2015-06-14 04:52:07 -05:00
|
|
|
/**
|
|
|
|
* Save the model to the database.
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
|
|
|
$count = $this->tags()->count();
|
|
|
|
$this->tag_count = $count;
|
|
|
|
|
2015-06-15 12:25:54 -05:00
|
|
|
return parent::save($options);
|
2015-06-14 04:52:07 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-05-23 08:42:19 -05:00
|
|
|
/**
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public function getDestinationAccountAttribute()
|
|
|
|
{
|
2015-06-05 12:02:23 -05:00
|
|
|
$account = $this->transactions()->where('amount', '>', 0)->first()->account;
|
2016-01-13 07:02:22 -06:00
|
|
|
|
2015-06-05 12:02:23 -05:00
|
|
|
return $account;
|
2015-05-23 08:42:19 -05:00
|
|
|
}
|
|
|
|
|
2015-06-05 12:02:23 -05:00
|
|
|
/**
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public function getSourceAccountAttribute()
|
|
|
|
{
|
|
|
|
$account = $this->transactions()->where('amount', '<', 0)->first()->account;
|
2015-06-06 08:36:12 -05:00
|
|
|
|
2015-06-05 12:02:23 -05:00
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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-07 01:23:44 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2015-02-07 01:23:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return EloquentBuilder
|
2015-02-07 01:23:44 -06:00
|
|
|
*/
|
|
|
|
public function scopeAfter(EloquentBuilder $query, Carbon $date)
|
|
|
|
{
|
|
|
|
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2015-02-07 01:23:44 -06:00
|
|
|
* @param EloquentBuilder $query
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return EloquentBuilder
|
2015-02-07 01:23:44 -06:00
|
|
|
*/
|
|
|
|
public function scopeBefore(EloquentBuilder $query, Carbon $date)
|
|
|
|
{
|
|
|
|
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2015-02-07 01:23:44 -06:00
|
|
|
* @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
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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(
|
2015-06-05 12:02:23 -05:00
|
|
|
['transactions' => function (HasMany $q) {
|
2015-02-22 08:40:13 -06:00
|
|
|
$q->orderBy('amount', 'ASC');
|
2015-06-14 04:52:07 -05:00
|
|
|
}, 'transactionType', 'transactionCurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill']
|
2015-02-22 08:40:13 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2015-02-11 00:35:10 -06:00
|
|
|
* @param $value
|
|
|
|
*/
|
2015-02-05 22:14:27 -06:00
|
|
|
public function setDescriptionAttribute($value)
|
|
|
|
{
|
2015-05-08 07:00:49 -05:00
|
|
|
$this->attributes['description'] = Crypt::encrypt($value);
|
2015-02-05 22:14:27 -06:00
|
|
|
$this->attributes['encrypted'] = true;
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:49:19 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
*/
|
|
|
|
public function attachments()
|
|
|
|
{
|
2015-07-18 14:12:34 -05:00
|
|
|
return $this->morphMany('FireflyIII\Models\Attachment', 'attachable');
|
2015-07-18 02:49:19 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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
|
|
|
/**
|
2015-05-10 06:22:00 -05:00
|
|
|
* @codeCoverageIgnore
|
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-12-09 18:39:50 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTransactionType()
|
|
|
|
{
|
|
|
|
return $this->transactionType->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isWithdrawal()
|
|
|
|
{
|
2015-12-25 02:34:23 -06:00
|
|
|
if (!is_null($this->type)) {
|
|
|
|
return $this->type == TransactionType::WITHDRAWAL;
|
|
|
|
}
|
|
|
|
|
2015-12-09 18:39:50 -06:00
|
|
|
return $this->transactionType->isWithdrawal();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isDeposit()
|
|
|
|
{
|
2015-12-25 02:34:23 -06:00
|
|
|
if (!is_null($this->type)) {
|
|
|
|
return $this->type == TransactionType::DEPOSIT;
|
|
|
|
}
|
|
|
|
|
2015-12-09 18:39:50 -06:00
|
|
|
return $this->transactionType->isDeposit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isTransfer()
|
|
|
|
{
|
2015-12-25 02:34:23 -06:00
|
|
|
if (!is_null($this->type)) {
|
|
|
|
return $this->type == TransactionType::TRANSFER;
|
|
|
|
}
|
|
|
|
|
2015-12-09 18:39:50 -06:00
|
|
|
return $this->transactionType->isTransfer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isOpeningBalance()
|
|
|
|
{
|
2015-12-25 02:34:23 -06:00
|
|
|
if (!is_null($this->type)) {
|
|
|
|
return $this->type == TransactionType::OPENING_BALANCE;
|
|
|
|
}
|
|
|
|
|
2015-12-09 18:39:50 -06:00
|
|
|
return $this->transactionType->isOpeningBalance();
|
|
|
|
}
|
2016-01-09 08:53:11 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
2016-01-15 13:57:26 -06:00
|
|
|
public static function routeBinder($value)
|
2016-01-09 08:53:11 -06:00
|
|
|
{
|
|
|
|
if (Auth::check()) {
|
2016-01-15 12:48:09 -06:00
|
|
|
$validTypes = [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER];
|
|
|
|
$object = TransactionJournal::where('transaction_journals.id', $value)
|
|
|
|
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
|
|
|
->whereIn('transaction_types.type', $validTypes)
|
|
|
|
->where('user_id', Auth::user()->id)->first(['transaction_journals.*']);
|
2016-01-09 08:53:11 -06:00
|
|
|
if ($object) {
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
2015-02-05 21:52:16 -06:00
|
|
|
}
|