Skip to content

Firefly III: a personal finances manager

Introduce new model for future remodeling of split transactions.

James Cole894b48df8e89
diff --git a/app/Models/TransactionGroup.php b/app/Models/TransactionGroup.php
new file mode 100644
index 0000000000..9d25ba44db
--- /dev/null
+++ b/app/Models/TransactionGroup.php
@@ -0,0 +1,100 @@
+<?php
+/**
+ * TransactionGroup.php
+ * Copyright (c) 2017 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * 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
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace FireflyIII\Models;
+
+use FireflyIII\User;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Database\Eloquent\SoftDeletes;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+
+
+/**
+ * Class TransactionGroup.
+ */
+class TransactionGroup extends Model
+{
+ use SoftDeletes;
+
+ /**
+ * The attributes that should be casted to native types.
+ *
+ * @var array
+ */
+ protected $casts
+ = [
+ 'created_at' => 'datetime',
+ 'updated_at' => 'datetime',
+ 'deleted_at' => 'datetime',
+ 'title' => 'string',
+ ];
+
+ /** @var array Fields that can be filled */
+ protected $fillable
+ = ['user_id', 'title'];
+
+ /**
+ * Route binder. Converts the key in the URL to the specified object (or throw 404).
+ *
+ * @param string $value
+ *
+ * @return TransactionGroup
+ * @throws NotFoundHttpException
+ */
+ public static function routeBinder(string $value): TransactionGroup
+ {
+ if (auth()->check()) {
+ $groupId = (int)$value;
+ /** @var User $user */
+ $user = auth()->user();
+ /** @var TransactionGroup $group */
+ $group = $user->transactionGroups()->where('transaction_groups.id', $groupId)
+ ->first(['transaction_groups.*']);
+ if (null !== $group) {
+ return $group;
+ }
+ }
+
+ throw new NotFoundHttpException;
+ }
+
+ /**
+ * @codeCoverageIgnore
+ * @return BelongsToMany
+ */
+ public function transactionJournals(): BelongsToMany
+ {
+ return $this->belongsToMany(TransactionJournal::class);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ * @return BelongsTo
+ */
+ public function user(): BelongsTo
+ {
+ return $this->belongsTo(User::class);
+ }
+
+}
diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php
index 9220b3a867..b43ebfdff6 100644
--- a/app/Models/TransactionJournal.php
+++ b/app/Models/TransactionJournal.php
@@ -186,6 +186,15 @@ class TransactionJournal extends Model
return $this->belongsToMany(Category::class);
}
+ /**
+ * @codeCoverageIgnore
+ * @return BelongsToMany
+ */
+ public function transactionGroups(): BelongsToMany
+ {
+ return $this->belongsToMany(Category::class);
+ }
+
/**
* @codeCoverageIgnore
*
diff --git a/app/User.php b/app/User.php
index 17a64c29c9..6d4a7c848e 100644
--- a/app/User.php
+++ b/app/User.php
@@ -42,6 +42,7 @@ use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
+use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -320,6 +321,17 @@ class User extends Authenticatable
return $this->hasMany(Tag::class);
}
+ /**
+ * @codeCoverageIgnore
+ * Link to transaction groups.
+ *
+ * @return HasMany
+ */
+ public function transactionGroups(): HasMany
+ {
+ return $this->hasMany(TransactionGroup::class);
+ }
+
/**
* @codeCoverageIgnore
* Link to transaction journals.
diff --git a/database/migrations/2018_06_08_200526_changes_for_v475.php b/database/migrations/2018_06_08_200526_changes_for_v475.php
index bca2ab325c..003c556ea4 100644
--- a/database/migrations/2018_06_08_200526_changes_for_v475.php
+++ b/database/migrations/2018_06_08_200526_changes_for_v475.php
@@ -71,10 +71,6 @@ class ChangesForV475 extends Migration
$table->boolean('apply_rules')->default(true);
$table->boolean('active')->default(true);
- // also separate:
- // category, budget, tags, notes, bill, piggy bank
-
-
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('transaction_type_id')->references('id')->on('transaction_types')->onDelete('cascade');
}
diff --git a/database/migrations/2019_01_28_193833_changes_for_v4710.php b/database/migrations/2019_01_28_193833_changes_for_v4710.php
new file mode 100644
index 0000000000..011305f270
--- /dev/null
+++ b/database/migrations/2019_01_28_193833_changes_for_v4710.php
@@ -0,0 +1,60 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class ChangesForV4710 extends Migration
+{
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('group_journals');
+ Schema::dropIfExists('transaction_groups');
+ }
+
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ if (!Schema::hasTable('transaction_groups')) {
+ Schema::create(
+ 'transaction_groups', function (Blueprint $table) {
+ $table->increments('id');
+ $table->timestamps();
+ $table->softDeletes();
+ $table->integer('user_id', false, true);
+ $table->string('title', 1024)->nullable();
+
+
+ $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
+ }
+ );
+ }
+
+ if (!Schema::hasTable('group_journals')) {
+ Schema::create(
+ 'group_journals',
+ function (Blueprint $table) {
+ $table->increments('id');
+ $table->integer('transaction_group_id', false, true);
+ $table->integer('transaction_journal_id', false, true);
+
+ $table->foreign('transaction_group_id')->references('id')->on('transaction_groups')->onDelete('cascade');
+ $table->foreign('transaction_journal_id')->references('id')->on('transaction_journals')->onDelete('cascade');
+
+ // unique combi:
+ $table->unique(['transaction_group_id', 'transaction_journal_id'],'unique_in_group');
+ }
+ );
+ }
+
+ }
+}