Files
firefly-iii/app/Models/TransactionJournal.php

256 lines
7.2 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
2022-12-29 19:42:26 +01:00
/**
* TransactionJournal.php
2020-02-16 13:55:32 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
namespace FireflyIII\Models;
2015-02-06 04:52:16 +01:00
use Carbon\Carbon;
2024-11-09 12:19:01 +01:00
use FireflyIII\Casts\SeparateTimezoneCaster;
2025-01-03 09:05:56 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2023-12-10 06:51:59 +01:00
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
2023-11-05 19:41:37 +01:00
use FireflyIII\Support\Models\ReturnsIntegerUserIdTrait;
2018-03-02 16:31:02 +01:00
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2023-11-05 19:41:37 +01:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2020-11-02 06:20:49 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2018-08-04 17:30:06 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2017-06-05 11:12:50 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2015-02-22 08:38:46 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2018-07-26 06:10:17 +02:00
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
2016-01-09 15:53:11 +01:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2023-12-10 06:51:59 +01:00
2024-07-31 08:26:43 +02:00
/**
* @method EloquentBuilder|static before()
* @method EloquentBuilder|static after()
2025-01-03 18:16:27 +01:00
* @method static EloquentBuilder|static query()
2024-07-31 08:26:43 +02:00
*/
class TransactionJournal extends Model
2015-02-06 05:04:06 +01:00
{
2022-10-30 14:24:28 +01:00
use HasFactory;
2023-11-05 19:41:37 +01:00
use ReturnsIntegerIdTrait;
use ReturnsIntegerUserIdTrait;
2023-11-05 20:18:16 +01:00
use SoftDeletes;
2015-02-09 07:56:24 +01:00
2016-12-24 17:36:51 +01:00
protected $casts
2024-11-06 11:57:12 +01:00
= [
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
2024-11-09 12:19:01 +01:00
'date' => SeparateTimezoneCaster::class,
'interest_date' => 'date',
'book_date' => 'date',
'process_date' => 'date',
'order' => 'int',
'tag_count' => 'int',
'encrypted' => 'boolean',
'completed' => 'boolean',
];
protected $fillable
2024-11-06 11:57:12 +01:00
= [
'user_id',
'user_group_id',
'transaction_type_id',
'bill_id',
'tag_count',
'transaction_currency_id',
'description',
'completed',
'order',
'date',
2024-11-06 11:12:26 +01:00
'date_tz',
];
2023-11-05 09:54:53 +01:00
protected $hidden = ['encrypted'];
2015-02-06 04:52:16 +01:00
/**
2018-08-04 17:30:06 +02:00
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
2021-04-07 07:28:43 +02:00
* @throws NotFoundHttpException
*/
2023-11-04 14:18:49 +01:00
public static function routeBinder(string $value): self
{
if (auth()->check()) {
2024-11-06 11:12:26 +01:00
$journalId = (int) $value;
2023-12-20 19:35:52 +01:00
2018-07-22 16:35:46 +02:00
/** @var User $user */
2024-11-06 11:57:12 +01:00
$user = auth()->user();
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $journal */
2024-11-06 11:57:12 +01:00
$journal = $user->transactionJournals()->where('transaction_journals.id', $journalId)->first(['transaction_journals.*']);
2018-04-02 15:10:40 +02:00
if (null !== $journal) {
return $journal;
}
}
2022-10-30 14:24:28 +01:00
throw new NotFoundHttpException();
}
2023-06-21 12:34:58 +02:00
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
2018-08-06 19:14:30 +02:00
public function attachments(): MorphMany
{
2018-04-28 06:23:13 +02:00
return $this->morphMany(Attachment::class, 'attachable');
}
public function auditLogEntries(): MorphMany
{
return $this->morphMany(AuditLogEntry::class, 'auditable');
}
2018-08-04 17:30:06 +02:00
public function bill(): BelongsTo
2015-02-06 05:14:27 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->belongsTo(Bill::class);
2015-02-06 05:14:27 +01:00
}
2017-06-05 11:12:50 +02:00
public function budgets(): BelongsToMany
2015-02-06 05:14:27 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->belongsToMany(Budget::class);
2015-02-06 05:14:27 +01:00
}
2017-06-05 11:12:50 +02:00
public function categories(): BelongsToMany
2015-02-06 05:14:27 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->belongsToMany(Category::class);
2015-02-06 05:14:27 +01:00
}
public function destJournalLinks(): HasMany
{
return $this->hasMany(TransactionJournalLink::class, 'destination_id');
}
2017-06-05 11:12:50 +02:00
public function isTransfer(): bool
{
2017-11-15 12:25:49 +01:00
if (null !== $this->transaction_type_type) {
2025-01-03 09:05:56 +01:00
return TransactionTypeEnum::TRANSFER->value === $this->transaction_type_type;
}
return $this->transactionType->isTransfer();
}
2021-08-28 15:46:34 +02:00
public function locations(): MorphMany
{
return $this->morphMany(Location::class, 'locatable');
}
/**
* Get all the notes.
2017-10-03 10:30:56 +02:00
*/
2018-07-26 06:10:17 +02:00
public function notes(): MorphMany
2017-10-03 10:30:56 +02:00
{
2018-04-28 06:23:13 +02:00
return $this->morphMany(Note::class, 'noteable');
2017-10-03 10:30:56 +02:00
}
2017-06-05 11:12:50 +02:00
public function piggyBankEvents(): HasMany
2015-02-06 05:14:27 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->hasMany(PiggyBankEvent::class);
2015-02-06 05:14:27 +01:00
}
2018-07-22 16:35:46 +02:00
public function scopeAfter(EloquentBuilder $query, Carbon $date): EloquentBuilder
{
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d H:i:s'));
}
2018-07-22 16:35:46 +02:00
public function scopeBefore(EloquentBuilder $query, Carbon $date): EloquentBuilder
{
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'));
}
2018-07-22 16:35:46 +02:00
public function scopeTransactionTypes(EloquentBuilder $query, array $types): void
{
2016-03-02 12:52:36 +01:00
if (!self::isJoined($query, 'transaction_types')) {
2016-03-02 12:47:15 +01:00
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
}
2022-11-04 05:11:05 +01:00
if (0 !== count($types)) {
2016-11-01 18:40:35 +01:00
$query->whereIn('transaction_types.type', $types);
}
2016-05-15 18:36:40 +02:00
}
2023-06-21 12:34:58 +02:00
/**
* Checks if tables are joined.
*/
2025-01-03 19:07:29 +01:00
public static function isJoined(EloquentBuilder $query, string $table): bool
2023-06-21 12:34:58 +02:00
{
$joins = $query->getQuery()->joins;
foreach ($joins as $join) {
if ($join->table === $table) {
return true;
}
}
return false;
}
public function sourceJournalLinks(): HasMany
{
return $this->hasMany(TransactionJournalLink::class, 'source_id');
}
2018-07-22 16:35:46 +02:00
public function tags(): BelongsToMany
2015-02-06 05:14:27 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->belongsToMany(Tag::class);
2015-02-06 05:14:27 +01:00
}
2018-08-04 17:30:06 +02:00
public function transactionCurrency(): BelongsTo
2015-02-06 05:14:27 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->belongsTo(TransactionCurrency::class);
2015-02-06 05:14:27 +01:00
}
2019-04-16 16:20:46 +02:00
public function transactionGroup(): BelongsTo
2019-02-13 17:38:41 +01:00
{
2019-03-23 08:10:59 +01:00
return $this->belongsTo(TransactionGroup::class);
2019-02-13 17:38:41 +01:00
}
public function transactionJournalMeta(): HasMany
2015-02-06 05:14:27 +01:00
{
2018-04-28 06:23:13 +02:00
return $this->hasMany(TransactionJournalMeta::class);
2015-02-06 05:14:27 +01:00
}
2018-08-04 17:30:06 +02:00
public function transactionType(): BelongsTo
{
2018-04-28 06:23:13 +02:00
return $this->belongsTo(TransactionType::class);
}
2017-06-05 11:12:50 +02:00
public function transactions(): HasMany
{
2018-04-28 06:23:13 +02:00
return $this->hasMany(Transaction::class);
}
2023-11-05 19:41:37 +01:00
2023-12-10 06:51:59 +01:00
protected function order(): Attribute
2023-11-05 19:41:37 +01:00
{
return Attribute::make(
2024-11-06 11:57:12 +01:00
get: static fn ($value) => (int) $value,
2023-11-05 19:41:37 +01:00
);
}
2023-12-10 06:51:59 +01:00
protected function transactionTypeId(): Attribute
2023-11-05 19:55:39 +01:00
{
return Attribute::make(
2024-11-06 11:57:12 +01:00
get: static fn ($value) => (int) $value,
2023-11-05 19:55:39 +01:00
);
}
2015-02-06 04:52:16 +01:00
}