firefly-iii/app/Models/TransactionJournal.php

408 lines
14 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
2022-12-29 12:42:26 -06:00
/**
* TransactionJournal.php
2020-02-16 06:55:32 -06: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 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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 01:40:00 -05: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 01:57:45 -05:00
namespace FireflyIII\Models;
2015-02-05 21:52:16 -06:00
use Carbon\Carbon;
use Eloquent;
2023-11-05 12:41:37 -06:00
use FireflyIII\Support\Models\ReturnsIntegerUserIdTrait;
2018-03-02 09:31:02 -06:00
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2023-11-05 12:41:37 -06:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2021-08-28 08:46:34 -05:00
use Illuminate\Database\Eloquent\Collection;
2020-11-01 23:20:49 -06:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2018-08-04 10:30:06 -05:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
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;
2018-07-25 23:10:17 -05:00
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
2016-01-09 08:53:11 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2023-11-05 12:41:37 -06:00
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
2016-11-18 13:06:08 -06:00
/**
2020-12-03 23:20:44 -06:00
* FireflyIII\Models\TransactionJournal
2018-03-10 15:34:02 -06:00
*
2023-11-05 12:41:37 -06:00
* @property int $id
2023-10-29 00:36:37 -05:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
2023-11-05 12:41:37 -06:00
* @property int $user_id
* @property int $transaction_type_id
* @property int|string|null $transaction_group_id
* @property int|string|null $bill_id
* @property int|string|null $transaction_currency_id
* @property string|null $description
2023-10-29 00:36:37 -05:00
* @property Carbon $date
* @property Carbon|null $interest_date
* @property Carbon|null $book_date
* @property Carbon|null $process_date
* @property int $order
* @property int $tag_count
* @property string $transaction_type_type
* @property bool $encrypted
* @property bool $completed
* @property-read Collection|Attachment[] $attachments
* @property-read int|null $attachments_count
* @property-read Bill|null $bill
* @property-read Collection|Budget[] $budgets
* @property-read int|null $budgets_count
* @property-read Collection|Category[] $categories
* @property-read int|null $categories_count
* @property-read Collection|TransactionJournalLink[] $destJournalLinks
* @property-read int|null $dest_journal_links_count
* @property-read Collection|Note[] $notes
* @property-read int|null $notes_count
* @property-read Collection|PiggyBankEvent[] $piggyBankEvents
* @property-read int|null $piggy_bank_events_count
* @property-read Collection|TransactionJournalLink[] $sourceJournalLinks
* @property-read int|null $source_journal_links_count
* @property-read Collection|Tag[] $tags
* @property-read int|null $tags_count
* @property-read TransactionCurrency|null $transactionCurrency
* @property-read TransactionGroup|null $transactionGroup
* @property-read Collection|TransactionJournalMeta[] $transactionJournalMeta
* @property-read int|null $transaction_journal_meta_count
* @property-read TransactionType $transactionType
* @property-read Collection|Transaction[] $transactions
* @property-read int|null $transactions_count
* @property-read User $user
2021-08-28 08:46:34 -05:00
* @method static EloquentBuilder|TransactionJournal after(Carbon $date)
* @method static EloquentBuilder|TransactionJournal before(Carbon $date)
* @method static EloquentBuilder|TransactionJournal newModelQuery()
* @method static EloquentBuilder|TransactionJournal newQuery()
* @method static \Illuminate\Database\Query\Builder|TransactionJournal onlyTrashed()
* @method static EloquentBuilder|TransactionJournal query()
* @method static EloquentBuilder|TransactionJournal transactionTypes($types)
* @method static EloquentBuilder|TransactionJournal whereBillId($value)
* @method static EloquentBuilder|TransactionJournal whereBookDate($value)
* @method static EloquentBuilder|TransactionJournal whereCompleted($value)
* @method static EloquentBuilder|TransactionJournal whereCreatedAt($value)
* @method static EloquentBuilder|TransactionJournal whereDate($value)
* @method static EloquentBuilder|TransactionJournal whereDeletedAt($value)
* @method static EloquentBuilder|TransactionJournal whereDescription($value)
* @method static EloquentBuilder|TransactionJournal whereEncrypted($value)
* @method static EloquentBuilder|TransactionJournal whereId($value)
* @method static EloquentBuilder|TransactionJournal whereInterestDate($value)
* @method static EloquentBuilder|TransactionJournal whereOrder($value)
* @method static EloquentBuilder|TransactionJournal whereProcessDate($value)
* @method static EloquentBuilder|TransactionJournal whereTagCount($value)
* @method static EloquentBuilder|TransactionJournal whereTransactionCurrencyId($value)
2020-12-03 23:20:44 -06:00
* @method static EloquentBuilder|TransactionJournal whereTransactionGroupId($value)
* @method static EloquentBuilder|TransactionJournal whereTransactionTypeId($value)
* @method static EloquentBuilder|TransactionJournal whereUpdatedAt($value)
* @method static EloquentBuilder|TransactionJournal whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|TransactionJournal withTrashed()
* @method static \Illuminate\Database\Query\Builder|TransactionJournal withoutTrashed()
2023-10-29 00:36:37 -05:00
* @property-read Collection|Location[] $locations
* @property-read int|null $locations_count
2023-11-05 12:41:37 -06:00
* @property int|string $the_count
* @property int $user_group_id
2021-09-18 03:08:10 -05:00
* @method static EloquentBuilder|TransactionJournal whereUserGroupId($value)
2023-10-29 00:36:37 -05:00
* @property-read Collection<int, AuditLogEntry> $auditLogEntries
* @property-read int|null $audit_log_entries_count
2023-03-08 23:33:23 -06:00
* @mixin Eloquent
2016-11-18 13:06:08 -06:00
*/
class TransactionJournal extends Model
2015-02-05 22:04:06 -06:00
{
2022-10-30 08:24:28 -05:00
use HasFactory;
2023-11-05 12:41:37 -06:00
use ReturnsIntegerIdTrait;
2023-11-04 08:18:49 -05:00
use SoftDeletes;
2023-11-05 12:41:37 -06:00
use ReturnsIntegerUserIdTrait;
2015-02-09 00:56:24 -06:00
2023-11-05 02:54:53 -06:00
2016-12-24 10:36:51 -06:00
protected $casts
= [
2017-11-03 10:04:17 -05:00
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
2019-02-08 00:14:45 -06:00
'date' => 'datetime',
2016-12-24 10:36:51 -06:00
'interest_date' => 'date',
'book_date' => 'date',
'process_date' => 'date',
'order' => 'int',
'tag_count' => 'int',
'encrypted' => 'boolean',
'completed' => 'boolean',
];
2023-11-05 02:54:53 -06:00
protected $fillable
2022-12-29 12:42:26 -06:00
= [
'user_id',
'user_group_id',
2022-12-29 12:42:26 -06:00
'transaction_type_id',
'bill_id',
'tag_count',
'transaction_currency_id',
'description',
'completed',
'order',
'date',
];
2023-11-05 02:54:53 -06:00
protected $hidden = ['encrypted'];
2015-02-05 21:52:16 -06:00
/**
2018-08-04 10:30:06 -05:00
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
2023-06-21 05:34:58 -05:00
* @param string $value
2017-11-15 05:25:49 -06:00
*
* @return TransactionJournal
2021-04-07 00:28:43 -05:00
* @throws NotFoundHttpException
*/
2023-11-04 08:18:49 -05:00
public static function routeBinder(string $value): self
{
if (auth()->check()) {
2022-12-29 12:42:26 -06:00
$journalId = (int)$value;
2018-07-22 09:35:46 -05:00
/** @var User $user */
$user = auth()->user();
2023-11-05 09:55:16 -06:00
/** @var TransactionJournal|null $journal */
2019-07-19 23:47:34 -05:00
$journal = $user->transactionJournals()->where('transaction_journals.id', $journalId)->first(['transaction_journals.*']);
2018-04-02 08:10:40 -05:00
if (null !== $journal) {
return $journal;
}
}
2022-10-30 08:24:28 -05:00
throw new NotFoundHttpException();
}
2023-06-21 05:34:58 -05:00
/**
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
2018-08-06 12:14:30 -05:00
* @return MorphMany
*/
2018-08-06 12:14:30 -05:00
public function attachments(): MorphMany
{
2018-04-27 23:23:13 -05:00
return $this->morphMany(Attachment::class, 'attachable');
}
/**
* @return MorphMany
*/
public function auditLogEntries(): MorphMany
{
return $this->morphMany(AuditLogEntry::class, 'auditable');
}
2015-02-11 00:35:10 -06:00
/**
2018-08-04 10:30:06 -05:00
* @return BelongsTo
2015-02-11 00:35:10 -06:00
*/
2018-08-04 10:30:06 -05:00
public function bill(): BelongsTo
2015-02-05 22:14:27 -06:00
{
2018-04-27 23:23:13 -05:00
return $this->belongsTo(Bill::class);
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
2018-08-04 10:30:06 -05:00
* @return BelongsToMany
2015-02-11 00:35:10 -06:00
*/
2017-06-05 04:12:50 -05:00
public function budgets(): BelongsToMany
2015-02-05 22:14:27 -06:00
{
2018-04-27 23:23:13 -05:00
return $this->belongsToMany(Budget::class);
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
2018-08-04 10:30:06 -05:00
* @return BelongsToMany
2015-02-11 00:35:10 -06:00
*/
2017-06-05 04:12:50 -05:00
public function categories(): BelongsToMany
2015-02-05 22:14:27 -06:00
{
2018-04-27 23:23:13 -05:00
return $this->belongsToMany(Category::class);
2015-02-05 22:14:27 -06:00
}
/**
* @return HasMany
*/
public function destJournalLinks(): HasMany
{
return $this->hasMany(TransactionJournalLink::class, 'destination_id');
}
/**
* @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();
}
2021-08-28 08:46:34 -05:00
/**
* @return MorphMany
*/
public function locations(): MorphMany
{
return $this->morphMany(Location::class, 'locatable');
}
/**
* Get all the notes.
2017-10-03 03:30:56 -05:00
*/
2018-07-25 23:10:17 -05:00
public function notes(): MorphMany
2017-10-03 03:30:56 -05:00
{
2018-04-27 23:23:13 -05:00
return $this->morphMany(Note::class, 'noteable');
2017-10-03 03:30:56 -05:00
}
2015-02-11 00:35:10 -06:00
/**
2018-08-04 10:30:06 -05:00
* @return HasMany
2015-02-11 00:35:10 -06:00
*/
2017-06-05 04:12:50 -05:00
public function piggyBankEvents(): HasMany
2015-02-05 22:14:27 -06:00
{
2018-04-27 23:23:13 -05:00
return $this->hasMany(PiggyBankEvent::class);
2015-02-05 22:14:27 -06:00
}
/**
2017-12-29 02:05:35 -06:00
*
2023-06-21 05:34:58 -05:00
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return EloquentBuilder
*/
2018-07-22 09:35:46 -05:00
public function scopeAfter(EloquentBuilder $query, Carbon $date): EloquentBuilder
{
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
}
/**
2017-12-29 02:05:35 -06:00
*
2023-06-21 05:34:58 -05:00
* @param EloquentBuilder $query
* @param Carbon $date
*
* @return EloquentBuilder
*/
2018-07-22 09:35:46 -05:00
public function scopeBefore(EloquentBuilder $query, Carbon $date): EloquentBuilder
{
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
}
/**
2017-12-29 02:05:35 -06:00
*
2023-06-21 05:34:58 -05:00
* @param EloquentBuilder $query
* @param array $types
*/
2018-07-22 09:35:46 -05:00
public function scopeTransactionTypes(EloquentBuilder $query, array $types): void
{
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');
}
2022-11-03 23:11:05 -05:00
if (0 !== count($types)) {
2016-11-01 12:40:35 -05:00
$query->whereIn('transaction_types.type', $types);
}
2016-05-15 11:36:40 -05:00
}
2023-06-21 05:34:58 -05:00
/**
* Checks if tables are joined.
*
*
* @param Builder $query
* @param string $table
*
* @return bool
*/
public static function isJoined(Builder $query, string $table): bool
{
$joins = $query->getQuery()->joins;
foreach ($joins as $join) {
if ($join->table === $table) {
return true;
}
}
return false;
}
/**
* @return HasMany
*/
public function sourceJournalLinks(): HasMany
{
return $this->hasMany(TransactionJournalLink::class, 'source_id');
}
2015-02-11 00:35:10 -06:00
/**
2018-08-04 10:30:06 -05:00
* @return BelongsToMany
2015-02-11 00:35:10 -06:00
*/
2018-07-22 09:35:46 -05:00
public function tags(): BelongsToMany
2015-02-05 22:14:27 -06:00
{
2018-04-27 23:23:13 -05:00
return $this->belongsToMany(Tag::class);
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
2018-08-04 10:30:06 -05:00
* @return BelongsTo
2015-02-11 00:35:10 -06:00
*/
2018-08-04 10:30:06 -05:00
public function transactionCurrency(): BelongsTo
2015-02-05 22:14:27 -06:00
{
2018-04-27 23:23:13 -05:00
return $this->belongsTo(TransactionCurrency::class);
2015-02-05 22:14:27 -06:00
}
2019-02-13 10:38:41 -06:00
/**
2019-03-23 02:10:59 -05:00
* @return BelongsTo
2019-02-13 10:38:41 -06:00
*/
2019-04-16 09:20:46 -05:00
public function transactionGroup(): BelongsTo
2019-02-13 10:38:41 -06:00
{
2019-03-23 02:10:59 -05:00
return $this->belongsTo(TransactionGroup::class);
2019-02-13 10:38:41 -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
{
2018-04-27 23:23:13 -05:00
return $this->hasMany(TransactionJournalMeta::class);
2015-02-05 22:14:27 -06:00
}
/**
2018-08-04 10:30:06 -05:00
* @return BelongsTo
*/
2018-08-04 10:30:06 -05:00
public function transactionType(): BelongsTo
{
2018-04-27 23:23:13 -05:00
return $this->belongsTo(TransactionType::class);
}
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
{
2018-04-27 23:23:13 -05:00
return $this->hasMany(Transaction::class);
}
2023-11-05 12:41:37 -06:00
/**
* @return Attribute
*/
protected function transactionTypeId(): Attribute
{
return Attribute::make(
get: static fn($value) => (int)$value,
);
}
2015-02-05 21:52:16 -06:00
}