Introduce new model for future remodeling of split transactions.

This commit is contained in:
James Cole 2019-01-28 20:00:40 +01:00
parent 195fba5931
commit 894b48df8e
5 changed files with 181 additions and 4 deletions

View File

@ -0,0 +1,100 @@
<?php
/**
* TransactionGroup.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* 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);
}
}

View File

@ -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
*

View File

@ -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.

View File

@ -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');
}

View File

@ -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');
}
);
}
}
}