firefly-iii/app/Models/TransactionJournal.php

413 lines
17 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* TransactionJournal.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Models;
2015-02-05 21:52:16 -06:00
2016-01-09 08:53:11 -06:00
use Auth;
use Carbon\Carbon;
2015-02-06 23:49:24 -06:00
use Crypt;
2016-03-02 12:41:22 -06:00
use FireflyIII\Support\Models\TransactionJournalSupport;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2015-02-22 01:38:46 -06:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
2016-01-09 08:53:11 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Watson\Validating\ValidatingTrait;
2016-03-02 04:59:39 -06:00
/**
* FireflyIII\Models\TransactionJournal
*
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Carbon\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\Carbon $date
* @property \Carbon\Carbon $interest_date
* @property \Carbon\Carbon $book_date
* @property boolean $encrypted
* @property integer $order
* @property integer $tag_count
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Attachment[] $attachments
* @property-read \FireflyIII\Models\Bill $bill
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Category[] $categories
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBankEvent[] $piggyBankEvents
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Tag[] $tags
* @property-read \FireflyIII\Models\TransactionCurrency $transactionCurrency
* @property-read \FireflyIII\Models\TransactionType $transactionType
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\TransactionGroup[] $transactiongroups
2016-03-02 04:59:39 -06:00
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\TransactionJournalMeta[] $transactionjournalmeta
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Transaction[] $transactions
* @property-read \FireflyIII\User $user
2016-03-02 04:59:39 -06:00
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal after($date)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal before($date)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal transactionTypes($types)
2016-05-20 04:27:41 -05:00
* @property string $transaction_type_type
2016-03-12 00:17:00 -06:00
* @property-read string $transaction_currency_code
* @property-read string $destination_amount
2016-05-20 04:27:41 -05:00
* @property string $destination_account_id
* @property string $destination_account_name
2016-03-12 00:17:00 -06:00
* @property-read string $destination_account_type
* @property-read string $source_amount
2016-05-20 04:27:41 -05:00
* @property string $source_account_id
* @property string $source_account_name
2016-03-12 00:17:00 -06:00
* @property-read string $source_account_type
* @property \Carbon\Carbon $process_date
2016-03-14 14:47:13 -05:00
* @property int $account_id
* @property float $journalAmount
* @property string $account_name
* @property int $budget_id
2016-03-12 00:36:23 -06:00
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTransactionTypeId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereBillId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTransactionCurrencyId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereDescription($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereCompleted($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereDate($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereInterestDate($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereBookDate($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereProcessDate($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereEncrypted($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereOrder($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTagCount($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal expanded()
* @mixin \Eloquent
2016-05-20 01:00:35 -05:00
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal sortCorrectly()
2016-03-02 04:59:39 -06:00
*/
2016-03-02 12:41:22 -06:00
class TransactionJournal extends TransactionJournalSupport
2015-02-05 22:04:06 -06:00
{
use SoftDeletes, ValidatingTrait;
2015-02-09 00:56:24 -06:00
/** @var array */
protected $dates = ['created_at', 'updated_at', 'date', 'deleted_at', 'interest_date', 'book_date', 'process_date'];
/** @var array */
protected $fillable
= ['user_id', 'transaction_type_id', 'bill_id', 'interest_date', 'book_date', 'process_date',
'transaction_currency_id', 'description', 'completed',
'date', 'rent_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',
];
2015-02-05 21:52:16 -06:00
/**
* @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');
}
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
/**
* @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
/**
* @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-02-11 00:35:10 -06:00
/**
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;
}
2016-03-02 04:56:47 -06:00
/**
*
* @param string $fieldName
*
* @return string
*/
2016-04-08 13:36:11 -05:00
public function getMeta($fieldName)
{
foreach ($this->transactionjournalmeta as $meta) {
if ($meta->name == $fieldName) {
return $meta->data;
}
}
return '';
}
/**
* @return bool
*/
public function isDeposit()
{
if (!is_null($this->transaction_type_type)) {
return $this->transaction_type_type == TransactionType::DEPOSIT;
}
return $this->transactionType->isDeposit();
}
/**
*
* @return bool
*/
public function isOpeningBalance()
{
if (!is_null($this->transaction_type_type)) {
return $this->transaction_type_type == TransactionType::OPENING_BALANCE;
}
return $this->transactionType->isOpeningBalance();
}
/**
*
* @return bool
*/
public function isTransfer()
{
if (!is_null($this->transaction_type_type)) {
return $this->transaction_type_type == TransactionType::TRANSFER;
}
return $this->transactionType->isTransfer();
}
/**
*
* @return bool
*/
public function isWithdrawal()
{
if (!is_null($this->transaction_type_type)) {
return $this->transaction_type_type == TransactionType::WITHDRAWAL;
}
return $this->transactionType->isWithdrawal();
}
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
}
/**
* 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);
}
/**
2015-05-14 02:51:54 -05:00
*
* @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'));
}
/**
2015-05-14 02:51:54 -05:00
*
* @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'));
}
/**
* @param EloquentBuilder $query
*/
public function scopeExpanded(EloquentBuilder $query)
{
// left join transaction type:
2016-03-02 05:52:36 -06:00
if (!self::isJoined($query, 'transaction_types')) {
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
}
// left join transaction currency:
$query->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id');
// left join destination (for amount and account info).
$query->groupBy('transaction_journals.id');
2016-05-11 00:57:16 -05:00
$query->with(['categories', 'budgets', 'attachments', 'bill', 'transactions']);
}
2016-05-20 04:27:41 -05:00
/**
* @param EloquentBuilder $query
*/
public function scopeSortCorrectly(EloquentBuilder $query)
{
$query->orderBy('transaction_journals.date', 'DESC');
$query->orderBy('transaction_journals.order', 'ASC');
$query->orderBy('transaction_journals.id', 'DESC');
}
/**
2015-05-14 02:51:54 -05:00
*
* @param EloquentBuilder $query
* @param array $types
*/
public function scopeTransactionTypes(EloquentBuilder $query, array $types)
{
2016-03-02 05:47:15 -06:00
2016-03-02 05:52:36 -06:00
if (!self::isJoined($query, 'transaction_types')) {
2016-03-02 05:47:15 -06:00
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
}
$query->whereIn('transaction_types.type', $types);
2016-05-15 11:36:40 -05:00
}
2015-02-11 00:35:10 -06:00
/**
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\BelongsToMany
2015-07-18 02:49:19 -05:00
*/
public function tags()
2015-07-18 02:49:19 -05:00
{
return $this->belongsToMany('FireflyIII\Models\Tag');
2015-07-18 02:49:19 -05:00
}
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
/**
* @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
/**
* @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
}
/**
* @return HasMany
*/
public function transactionjournalmeta(): HasMany
{
return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactions()
{
return $this->hasMany('FireflyIII\Models\Transaction');
}
2016-01-09 08:53:11 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2016-01-09 08:53:11 -06:00
*/
public function user()
2016-01-09 08:53:11 -06:00
{
return $this->belongsTo('FireflyIII\User');
2016-01-09 08:53:11 -06:00
}
2015-02-05 21:52:16 -06:00
}