firefly-iii/app/Models/TransactionJournal.php

343 lines
9.4 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-05-17 05:49:09 -05:00
use Illuminate\Database\Query\JoinClause;
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
/**
* @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
/**
* @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
/**
* @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-04-07 03:14:10 -05:00
/**
* @return float
*/
public function getAmountAttribute()
{
2015-05-17 09:12:00 -05:00
$amount = 0;
2015-04-07 03:14:10 -05:00
/** @var Transaction $t */
foreach ($this->transactions as $t) {
if ($t->amount > 0) {
2015-05-17 09:12:00 -05:00
$amount = floatval($t->amount);
2015-04-07 03:14:10 -05:00
}
}
2015-05-03 05:58:55 -05:00
2015-05-17 09:12:00 -05:00
/*
* If the journal has tags, it gets complicated.
*/
if ($this->tags->count() == 0) {
return $amount;
}
// if journal is part of advancePayment AND journal is a withdrawal,
// then journal is being repaid by other journals, so the actual amount will lower:
/** @var Tag $tag */
$tag = $this->tags()->where('tagMode', 'advancePayment')->first();
if ($tag && $this->transactionType->type == 'Withdrawal') {
// loop other deposits, remove from our amount.
$others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get();
foreach ($others as $other) {
$amount -= $other->amount;
}
return $amount;
}
return $amount;
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany('FireflyIII\Models\Tag');
2015-04-07 03:14:10 -05:00
}
2015-05-03 05:58:55 -05:00
/**
2015-05-17 05:49:09 -05:00
* @return Account
2015-05-03 05:58:55 -05:00
*/
2015-04-28 10:10:52 -05:00
public function getAssetAccountAttribute()
{
$positive = true; // the asset account is in the transaction with the positive amount.
if ($this->transactionType->type === 'Withdrawal') {
$positive = false;
}
/** @var Transaction $transaction */
foreach ($this->transactions()->get() as $transaction) {
if (floatval($transaction->amount) > 0 && $positive === true) {
2015-04-28 14:00:26 -05:00
return $transaction->account;
2015-04-28 10:10:52 -05:00
}
if (floatval($transaction->amount) < 0 && $positive === false) {
2015-04-28 14:00:26 -05:00
return $transaction->account;
2015-04-28 10:10:52 -05:00
}
}
2015-05-17 05:49:09 -05:00
return $this->transactions()->first()->account;
2015-04-28 10:10:52 -05:00
}
/**
* @codeCoverageIgnore
2015-04-28 10:10:52 -05:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany('FireflyIII\Models\Transaction');
}
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
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
/**
* @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-02-11 00:35:10 -06: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-22 08:40:13 -06:00
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05: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);
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05: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'));
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05: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'));
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05: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
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
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'));
}
2015-05-17 05:49:09 -05:00
/**
* Returns the account to which the money was moved.
*
* @codeCoverageIgnore
*
* @param EloquentBuilder $query
* @param Account $account
*/
public function scopeToAccountIs(EloquentBuilder $query, Account $account)
{
$query->leftJoin(
'transactions', function (JoinClause $join) {
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '>', 0);
}
);
$query->where('transactions.account_id', $account->id);
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05: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
/**
* @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(
['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
/**
* @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-02-11 00:35:10 -06: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
/**
* @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
/**
* @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
/**
* @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-02-05 21:52:16 -06:00
}