firefly-iii/app/Models/TransactionJournal.php

443 lines
12 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
* Creative Commons Attribution-ShareAlike 4.0 International 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
use Carbon\Carbon;
2015-02-06 23:49:24 -06:00
use Crypt;
use FireflyIII\Support\CacheProperties;
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;
use Log;
use Preferences;
2016-01-09 08:53:11 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Watson\Validating\ValidatingTrait;
2016-11-18 13:06:08 -06:00
/**
* Class TransactionJournal
*
* @package FireflyIII\Models
*/
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',
'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)
{
2016-09-16 05:07:45 -05:00
if (auth()->check()) {
2016-12-15 14:35:33 -06:00
$object = self::where('transaction_journals.id', $value)
2016-12-14 11:59:12 -06:00
->with('transactionType')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('user_id', auth()->user()->id)->first(['transaction_journals.*']);
2016-11-22 12:10:17 -06:00
if (!is_null($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
}
/**
* @param string $name
*
* @return bool
*/
2016-12-14 11:59:12 -06:00
public function deleteMeta(string $name): bool
{
$this->transactionJournalMeta()->where('name', $name)->delete();
return true;
}
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 $name
*
* @return string
*/
public function getMeta(string $name)
{
$value = null;
$cache = new CacheProperties;
$cache->addProperty('journal-meta');
$cache->addProperty($this->id);
$cache->addProperty($name);
if ($cache->has()) {
return $cache->get();
}
Log::debug(sprintf('Looking for journal #%d meta field "%s".', $this->id, $name));
$entry = $this->transactionJournalMeta()->where('name', $name)->first();
if (!is_null($entry)) {
$value = $entry->data;
// cache:
$cache->store($value);
}
// convert to Carbon if name is _date
if (!is_null($value) && substr($name, -5) === '_date') {
$value = new Carbon($value);
// cache:
$cache->store($value);
}
return $value;
}
/**
* @param string $name
*
* @return bool
*/
public function hasMeta(string $name): bool
{
return !is_null($this->getMeta($name));
}
/**
* @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');
2016-09-16 02:09:54 -05:00
// extend group by:
$query->groupBy(
[
'transaction_journals.id',
'transaction_journals.created_at',
'transaction_journals.updated_at',
'transaction_journals.deleted_at',
'transaction_journals.user_id',
'transaction_journals.transaction_type_id',
'transaction_journals.bill_id',
'transaction_journals.transaction_currency_id',
'transaction_journals.description',
'transaction_journals.date',
'transaction_journals.interest_date',
'transaction_journals.book_date',
'transaction_journals.process_date',
'transaction_journals.order',
'transaction_journals.tag_count',
'transaction_journals.encrypted',
'transaction_journals.completed',
'transaction_types.type',
'transaction_currencies.code',
2016-09-16 05:07:45 -05:00
]
);
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');
}
2016-11-01 12:40:35 -05:00
if (count($types) > 0) {
$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
/**
* @param string $name
* @param $value
*
* @return TransactionJournalMeta
2015-07-18 02:49:19 -05:00
*/
public function setMeta(string $name, $value): TransactionJournalMeta
2015-07-18 02:49:19 -05:00
{
if (is_null($value)) {
$this->deleteMeta($name);
return new TransactionJournalMeta();
}
if (is_string($value) && strlen($value) === 0) {
return new TransactionJournalMeta();
}
if ($value instanceof Carbon) {
$value = $value->toW3cString();
}
Log::debug(sprintf('Going to set "%s" with value "%s"', $name, json_encode($value)));
$entry = $this->transactionJournalMeta()->where('name', $name)->first();
if (is_null($entry)) {
$entry = new TransactionJournalMeta();
$entry->transactionJournal()->associate($this);
$entry->name = $name;
}
$entry->data = $value;
$entry->save();
Preferences::mark();
return $entry;
2015-07-18 02:49:19 -05:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
2015-02-11 00:35:10 -06:00
*/
public function tags()
2015-02-05 22:14:27 -06:00
{
return $this->belongsToMany('FireflyIII\Models\Tag');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function transactionCurrency()
2015-02-05 22:14:27 -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 HasMany
2015-02-11 00:35:10 -06:00
*/
public function transactionJournalMeta(): HasMany
2015-02-05 22:14:27 -06:00
{
return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
2015-02-05 22:14:27 -06:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function transactionType()
{
return $this->belongsTo('FireflyIII\Models\TransactionType');
}
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
}