Rename fields in piggy bank.

This commit is contained in:
James Cole
2024-11-30 15:57:11 +01:00
parent 9ad005e31f
commit 92190bbc54
8 changed files with 127 additions and 18 deletions

View File

@@ -105,7 +105,7 @@ class PiggyBankController extends Controller
/** @var PiggyBank $piggy */
foreach ($piggies as $piggy) {
$currency = $this->accountRepository->getAccountCurrency($piggy->account) ?? $defaultCurrency;
$currentAmount = $this->piggyRepository->getRepetition($piggy)->currentamount ?? '0';
$currentAmount = $this->piggyRepository->getRepetition($piggy)->current_amount ?? '0';
$objectGroup = $piggy->objectGroups()->first();
$response[] = [
'id' => (string)$piggy->id,

View File

@@ -173,7 +173,7 @@ class CorrectAmounts extends Command
/** @var PiggyBankRepetition $item */
foreach ($set as $item) {
$item->currentamount = app('steam')->positive($item->currentamount);
$item->currentamount = app('steam')->positive($item->current_amount);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d piggy bank repetition amount(s).', $count));
@@ -191,7 +191,7 @@ class CorrectAmounts extends Command
/** @var PiggyBank $item */
foreach ($set as $item) {
$item->targetamount = app('steam')->positive($item->targetamount);
$item->targetamount = app('steam')->positive($item->target_amount);
$item->save();
}
$this->friendlyInfo(sprintf('Corrected %d piggy bank amount(s).', $count));

View File

@@ -37,11 +37,11 @@ class PiggyBankObserver
app('log')->debug('Observe "created" of a piggy bank.');
$repetition = new PiggyBankRepetition();
$repetition->piggyBank()->associate($piggyBank);
$repetition->startdate = $piggyBank->startdate;
$repetition->startdate_tz = $piggyBank->startdate->format('e');
$repetition->targetdate = $piggyBank->targetdate;
$repetition->targetdate_tz = $piggyBank->targetdate?->format('e');
$repetition->currentamount = '0';
$repetition->start_date = $piggyBank->startdate;
$repetition->start_date_tz = $piggyBank->startdate->format('e');
$repetition->target_date = $piggyBank->targetdate;
$repetition->target_date_tz = $piggyBank->targetdate?->format('e');
$repetition->current_amount = '0';
$repetition->save();
}

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Http\Controllers\PiggyBank;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\ObjectGroup\OrganisesObjectGroups;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;

View File

@@ -27,6 +27,7 @@ use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
@@ -46,17 +47,15 @@ class PiggyBank extends Model
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
'startdate' => 'date',
'targetdate' => 'date',
'start_date' => 'date',
'target_date' => 'date',
'order' => 'int',
'active' => 'boolean',
'encrypted' => 'boolean',
'targetamount' => 'string',
'target_amount' => 'string',
];
protected $fillable = ['name', 'account_id', 'order', 'targetamount', 'startdate', 'startdate_tz', 'targetdate', 'targetdate_tz', 'active'];
protected $hidden = ['targetamount_encrypted', 'encrypted'];
protected $fillable = ['name', 'account_id', 'order', 'target_amount', 'start_date', 'start_date_tz', 'target_date', 'target_date_tz', 'active'];
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
@@ -110,6 +109,11 @@ class PiggyBank extends Model
return $this->hasMany(PiggyBankEvent::class);
}
public function accounts(): BelongsToMany
{
return $this->belongsToMany(Account::class);
}
public function piggyBankRepetitions(): HasMany
{
return $this->hasMany(PiggyBankRepetition::class);
@@ -118,9 +122,9 @@ class PiggyBank extends Model
/**
* @param mixed $value
*/
public function setTargetamountAttribute($value): void
public function setTargetAmountAttribute($value): void
{
$this->attributes['targetamount'] = (string)$value;
$this->attributes['target_amount'] = (string)$value;
}
protected function accountId(): Attribute
@@ -140,7 +144,7 @@ class PiggyBank extends Model
/**
* Get the max amount
*/
protected function targetamount(): Attribute
protected function targetAmount(): Attribute
{
return Attribute::make(
get: static fn ($value) => (string)$value,

View File

@@ -47,7 +47,7 @@ class PiggyBankRepetition extends Model
'virtual_balance' => 'string',
];
protected $fillable = ['piggy_bank_id', 'startdate', 'startdate_tz', 'targetdate', 'targetdate_tz', 'currentamount'];
protected $fillable = ['piggy_bank_id', 'start_date', 'start_date_tz', 'target_date', 'target_date_tz', 'current_amount'];
public function piggyBank(): BelongsTo
{

View File

@@ -42,6 +42,7 @@ class UserGroups extends Migration
'categories',
'recurrences',
'object_groups',
'preferences',
'rule_groups',
'rules',
'tags',

View File

@@ -0,0 +1,103 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// make account_id nullable and the relation also nullable.
Schema::table('piggy_banks', static function (Blueprint $table) {
// 1. drop index
$table->dropForeign('piggy_banks_account_id_foreign');
});
Schema::table('piggy_banks', static function (Blueprint $table) {
// 2. make column nullable.
$table->unsignedInteger('account_id')->nullable()->change();
});
Schema::table('piggy_banks', static function (Blueprint $table) {
// 3. add currency
$table->integer('transaction_currency_id', false, true)->after('account_id');
$table->foreign('transaction_currency_id','unique_currency')->references('id')->on('transaction_currencies')->onDelete('cascade');
});
Schema::table('piggy_banks', static function (Blueprint $table) {
// 4. rename columns
$table->renameColumn('targetamount', 'target_amount');
$table->renameColumn('startdate', 'start_date');
$table->renameColumn('targetdate', 'target_date');
$table->renameColumn('startdate_tz', 'start_date_tz');
$table->renameColumn('targetdate_tz', 'target_date_tz');
});
Schema::table('piggy_banks', static function (Blueprint $table) {
// 5. add new index
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('set null');
});
// rename some fields in piggy bank reps.
Schema::table('piggy_bank_repetitions', static function (Blueprint $table) {
// 6. rename columns
$table->renameColumn('currentamount', 'current_amount');
$table->renameColumn('startdate', 'start_date');
$table->renameColumn('targetdate', 'target_date');
$table->renameColumn('startdate_tz', 'start_date_tz');
$table->renameColumn('targetdate_tz', 'target_date_tz');
});
// create table account_piggy_bank
Schema::create('account_piggy_bank', static function (Blueprint $table) {
$table->id();
$table->integer('account_id', false, true);
$table->integer('piggy_bank_id',false, true);
$table->decimal('current_amount', 32, 12)->default('0');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
$table->foreign('piggy_bank_id')->references('id')->on('piggy_banks')->onDelete('cascade');
$table->unique(['account_id', 'piggy_bank_id'],'unique_piggy_save');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('piggy_banks', static function (Blueprint $table) {
// 1. drop account index again.
$table->dropForeign('piggy_banks_account_id_foreign');
// rename columns again.
$table->renameColumn('target_amount', 'targetamount');
$table->renameColumn('start_date', 'startdate');
$table->renameColumn('target_date', 'targetdate');
$table->renameColumn('start_date_tz', 'startdate_tz');
$table->renameColumn('target_date_tz', 'targetdate_tz');
// 3. drop currency again + index
$table->dropForeign('unique_currency');
$table->dropColumn('transaction_currency_id');
// 2. make column non-nullable.
$table->unsignedInteger('account_id')->change();
// 5. add new index
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
});
// rename some fields in piggy bank reps.
Schema::table('piggy_bank_repetitions', static function (Blueprint $table) {
// 6. rename columns
$table->renameColumn('current_amount', 'currentamount');
$table->renameColumn('start_date', 'startdate');
$table->renameColumn('target_date', 'targetdate');
$table->renameColumn('start_date_tz', 'startdate_tz');
$table->renameColumn('target_date_tz', 'targetdate_tz');
});
Schema::dropIfExists('account_piggy_bank');
}
};