mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-05 21:53:08 -06:00
362 lines
9.1 KiB
PHP
362 lines
9.1 KiB
PHP
<?php namespace FireflyIII\Models;
|
|
|
|
use Auth;
|
|
use Carbon\Carbon;
|
|
use Crypt;
|
|
use FireflyIII\Support\CacheProperties;
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
use Watson\Validating\ValidatingTrait;
|
|
|
|
class TransactionJournal extends Model
|
|
{
|
|
use SoftDeletes, ValidatingTrait;
|
|
|
|
/** @var array */
|
|
protected $dates = ['created_at', 'updated_at', 'date', 'deleted_at', 'interest_date', 'book_date'];
|
|
|
|
/** @var array */
|
|
protected $fillable
|
|
= ['user_id', 'transaction_type_id', 'bill_id',
|
|
'transaction_currency_id', 'description', 'completed',
|
|
'date', 'rent_date', 'book_date', 'encrypted', 'tag_count'];
|
|
|
|
/** @var array */
|
|
protected $hidden = ['encrypted'];
|
|
|
|
/** @var array */
|
|
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',
|
|
];
|
|
|
|
/**
|
|
* @param $value
|
|
*
|
|
* @return mixed
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public static function routeBinder($value)
|
|
{
|
|
if (Auth::check()) {
|
|
$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.*']);
|
|
if ($object) {
|
|
return $object;
|
|
}
|
|
}
|
|
|
|
throw new NotFoundHttpException;
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
*/
|
|
public function attachments()
|
|
{
|
|
return $this->morphMany('FireflyIII\Models\Attachment', 'attachable');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function bill()
|
|
{
|
|
return $this->belongsTo('FireflyIII\Models\Bill');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function budgets()
|
|
{
|
|
return $this->belongsToMany('FireflyIII\Models\Budget');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function categories()
|
|
{
|
|
return $this->belongsToMany('FireflyIII\Models\Category');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*
|
|
* @param $value
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getDescriptionAttribute($value)
|
|
{
|
|
if ($this->encrypted) {
|
|
return Crypt::decrypt($value);
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* @return Account
|
|
*/
|
|
public function getDestinationAccountAttribute()
|
|
{
|
|
$account = $this->transactions()->where('amount', '>', 0)->first()->account;
|
|
|
|
return $account;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $fieldName
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getMeta($fieldName): string
|
|
{
|
|
foreach ($this->transactionjournalmeta as $meta) {
|
|
if ($meta->name == $fieldName) {
|
|
return $meta->data;
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* @return Account
|
|
*/
|
|
public function getSourceAccountAttribute()
|
|
{
|
|
$account = $this->transactions()->where('amount', '<', 0)->first()->account;
|
|
|
|
return $account;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getTransactionType()
|
|
{
|
|
return $this->transactionType->type;
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
public function isDeposit()
|
|
{
|
|
if (!is_null($this->type)) {
|
|
return $this->type == TransactionType::DEPOSIT;
|
|
}
|
|
|
|
return $this->transactionType->isDeposit();
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
public function isOpeningBalance()
|
|
{
|
|
if (!is_null($this->type)) {
|
|
return $this->type == TransactionType::OPENING_BALANCE;
|
|
}
|
|
|
|
return $this->transactionType->isOpeningBalance();
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
public function isTransfer()
|
|
{
|
|
if (!is_null($this->type)) {
|
|
return $this->type == TransactionType::TRANSFER;
|
|
}
|
|
|
|
return $this->transactionType->isTransfer();
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
public function isWithdrawal()
|
|
{
|
|
if (!is_null($this->type)) {
|
|
return $this->type == TransactionType::WITHDRAWAL;
|
|
}
|
|
|
|
return $this->transactionType->isWithdrawal();
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function piggyBankEvents()
|
|
{
|
|
return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
|
|
}
|
|
|
|
/**
|
|
* Save the model to the database.
|
|
*
|
|
* @param array $options
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function save(array $options = [])
|
|
{
|
|
$count = $this->tags()->count();
|
|
$this->tag_count = $count;
|
|
|
|
return parent::save($options);
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*
|
|
* @param EloquentBuilder $query
|
|
* @param Carbon $date
|
|
*
|
|
* @return EloquentBuilder
|
|
*/
|
|
public function scopeAfter(EloquentBuilder $query, Carbon $date)
|
|
{
|
|
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*
|
|
* @param EloquentBuilder $query
|
|
* @param Carbon $date
|
|
*
|
|
* @return EloquentBuilder
|
|
*/
|
|
public function scopeBefore(EloquentBuilder $query, Carbon $date)
|
|
{
|
|
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*
|
|
* @param EloquentBuilder $query
|
|
* @param array $types
|
|
*/
|
|
public function scopeTransactionTypes(EloquentBuilder $query, array $types)
|
|
{
|
|
$query->leftJoin(
|
|
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
|
|
);
|
|
$query->whereIn('transaction_types.type', $types);
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* 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']
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*
|
|
* @param $value
|
|
*/
|
|
public function setDescriptionAttribute($value)
|
|
{
|
|
$this->attributes['description'] = Crypt::encrypt($value);
|
|
$this->attributes['encrypted'] = true;
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function tags()
|
|
{
|
|
return $this->belongsToMany('FireflyIII\Models\Tag');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function transactionCurrency()
|
|
{
|
|
return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function transactionType()
|
|
{
|
|
return $this->belongsTo('FireflyIII\Models\TransactionType');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function transactiongroups()
|
|
{
|
|
return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany
|
|
*/
|
|
public function transactionjournalmeta(): HasMany
|
|
{
|
|
return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function transactions()
|
|
{
|
|
return $this->hasMany('FireflyIII\Models\Transaction');
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo('FireflyIII\User');
|
|
}
|
|
}
|