firefly-iii/app/Models/TransactionJournal.php

446 lines
12 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* TransactionJournal.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
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;
use FireflyIII\Support\Models\TransactionJournalTrait;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
2017-06-05 04:12:50 -05:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
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
/**
2017-11-15 05:25:49 -06:00
* Class TransactionJournal.
2016-11-18 13:06:08 -06:00
*/
class TransactionJournal extends Model
2015-02-05 22:04:06 -06:00
{
use SoftDeletes, ValidatingTrait, TransactionJournalTrait;
2015-02-09 00:56:24 -06:00
2016-12-24 10:36:51 -06:00
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
2017-11-03 10:04:17 -05:00
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
2016-12-24 10:36:51 -06:00
'date' => 'date',
'interest_date' => 'date',
'book_date' => 'date',
'process_date' => 'date',
'order' => 'int',
'tag_count' => 'int',
'encrypted' => 'boolean',
'completed' => 'boolean',
];
/** @var array */
2017-11-05 12:49:20 -06:00
protected $dates = ['date', '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',
2017-11-15 05:25:49 -06:00
'date', 'rent_date', 'encrypted', 'tag_count',];
/** @var array */
protected $hidden = ['encrypted'];
/** @var array */
protected $rules
= [
2017-06-05 04:12:50 -05:00
'user_id' => 'required|exists:users,id',
'transaction_type_id' => 'required|exists:transaction_types,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
2017-11-15 05:25:49 -06:00
*
* @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-24 10:36:51 -06:00
->with('transactionType')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('user_id', auth()->user()->id)->first(['transaction_journals.*']);
2017-11-15 05:25:49 -06:00
if (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
*/
2017-06-05 04:12:50 -05:00
public function budgets(): BelongsToMany
2015-02-05 22:14:27 -06:00
{
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
*/
2017-06-05 04:12:50 -05:00
public function categories(): BelongsToMany
2015-02-05 22:14:27 -06:00
{
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;
}
2017-09-08 23:41:45 -05:00
/**
* @return HasMany
*/
public function destinationJournalLinks(): HasMany
{
return $this->hasMany(TransactionJournalLink::class, 'destination_id');
}
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()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
}
Log::debug(sprintf('Looking for journal #%d meta field "%s".', $this->id, $name));
$entry = $this->transactionJournalMeta()->where('name', $name)->first();
2017-11-15 05:25:49 -06:00
if (null !== $entry) {
$value = $entry->data;
// cache:
$cache->store($value);
}
// convert to Carbon if name is _date
2017-11-15 05:25:49 -06:00
if (null !== $value && '_date' === substr($name, -5)) {
$value = new Carbon($value);
// cache:
$cache->store($value);
}
return $value;
}
/**
* @param string $name
*
* @return bool
*/
public function hasMeta(string $name): bool
{
2017-11-15 05:25:49 -06:00
return null !== $this->getMeta($name);
}
/**
* @return bool
*/
2017-06-05 04:12:50 -05:00
public function isDeposit(): bool
{
2017-11-15 05:25:49 -06:00
if (null !== $this->transaction_type_type) {
return TransactionType::DEPOSIT === $this->transaction_type_type;
}
return $this->transactionType->isDeposit();
}
/**
* @return bool
*/
2017-06-05 04:12:50 -05:00
public function isOpeningBalance(): bool
{
2017-11-15 05:25:49 -06:00
if (null !== $this->transaction_type_type) {
return TransactionType::OPENING_BALANCE === $this->transaction_type_type;
}
return $this->transactionType->isOpeningBalance();
}
/**
* @return bool
*/
2017-06-05 04:12:50 -05:00
public function isTransfer(): bool
{
2017-11-15 05:25:49 -06:00
if (null !== $this->transaction_type_type) {
return TransactionType::TRANSFER === $this->transaction_type_type;
}
return $this->transactionType->isTransfer();
}
/**
* @return bool
*/
2017-06-05 04:12:50 -05:00
public function isWithdrawal(): bool
{
2017-11-15 05:25:49 -06:00
if (null !== $this->transaction_type_type) {
return TransactionType::WITHDRAWAL === $this->transaction_type_type;
}
return $this->transactionType->isWithdrawal();
}
2017-10-03 03:30:56 -05:00
/**
* Get all of the notes.
*/
public function notes()
{
return $this->morphMany('FireflyIII\Models\Note', 'noteable');
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2017-06-05 04:12:50 -05:00
public function piggyBankEvents(): HasMany
2015-02-05 22:14:27 -06:00
{
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.
*
2017-11-15 05:25:49 -06:00
* @param array $options
*
* @return bool
*/
2017-06-05 04:12:50 -05:00
public function save(array $options = []): bool
{
$count = $this->tags()->count();
$this->tag_count = $count;
return parent::save($options);
}
/**
* @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'));
}
/**
* @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'));
}
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');
}
/**
* @param EloquentBuilder $query
* @param array $types
*/
public function scopeTransactionTypes(EloquentBuilder $query, array $types)
{
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
/**
* @param $value
*/
2015-02-05 22:14:27 -06:00
public function setDescriptionAttribute($value)
{
$encrypt = config('firefly.encryption');
$this->attributes['description'] = $encrypt ? Crypt::encrypt($value) : $value;
$this->attributes['encrypted'] = $encrypt;
2015-02-05 22:14:27 -06:00
}
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
{
2017-11-15 05:25:49 -06:00
if (null === $value) {
$this->deleteMeta($name);
return new TransactionJournalMeta();
}
2017-11-15 05:25:49 -06:00
if (is_string($value) && 0 === strlen($value)) {
$this->deleteMeta($name);
2017-11-30 12:43:02 -06:00
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();
2017-11-15 05:25:49 -06:00
if (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
}
/**
* @return HasMany
*/
public function sourceJournalLinks(): HasMany
{
return $this->hasMany(TransactionJournalLink::class, 'source_id');
}
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
/**
2017-06-05 04:12:50 -05:00
* @return HasMany
*/
2017-06-05 04:12:50 -05:00
public function transactions(): HasMany
{
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
}