Updates to budgets.

This commit is contained in:
James Cole
2016-04-28 10:59:36 +02:00
parent 19d7e27fa9
commit b47a140c2f
20 changed files with 510 additions and 182 deletions

View File

@@ -1,39 +0,0 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangesFor385 extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// remove an index.
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->dropUnique('unique_limit');
}
);
// create it again, correctly.
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->unique(['budget_id', 'startdate','repeat_freq'], 'unique_limit');
}
);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View File

@@ -0,0 +1,148 @@
<?php
use FireflyIII\Models\BudgetLimit;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class ChangesForV385
*/
class ChangesForV385 extends Migration
{
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$backup = $this->backupRepeatFreqsFromString();
// drop string and create enum field
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->dropColumn('repeat_freq');
}
);
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->enum('repeat_freq', ['daily', 'weekly', 'monthly', 'quarterly', 'half-year', 'yearly']);
}
);
// restore backup. Change unknowns to "monthly".
$this->restoreRepeatFreqsToEnum($backup);
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// remove an index.
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->dropUnique('unique_limit');
$table->dropForeign('bid_foreign');
$table->dropUnique('unique_bl_combi');
}
);
// recreate foreign key:
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->foreign('budget_id', 'bid_foreign')->references('id')->on('budgets')->onDelete('cascade');
}
);
// backup values
$backup = $this->backupRepeatFreqsFromEnum();
// drop enum and create varchar field
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->dropColumn('repeat_freq');
}
);
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->string('repeat_freq', 20)->default('monthly');
}
);
// put data back:
$this->restoreRepeatFreqsToVarchar($backup);
// create it again, correctly.
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->unique(['budget_id', 'startdate', 'repeat_freq'], 'unique_limit');
}
);
}
/**
* @return array
*/
private function backupRepeatFreqsFromEnum(): array
{
$backup = [];
$set = BudgetLimit::get();
/** @var BudgetLimit $entry */
foreach ($set as $entry) {
$backup[$entry->id] = $entry->repeat_freq;
}
return $backup;
}
/**
* Same routine.
*
* @return array
*/
private function backupRepeatFreqsFromString()
{
return $this->backupRepeatFreqsFromEnum();
}
/**
* @param array $backup
*
* @return bool
*/
private function restoreRepeatFreqsToEnum(array $backup): bool
{
foreach ($backup as $id => $repeatFreq) {
$budgetLimit = BudgetLimit::find($id);
if (!in_array($repeatFreq, ['daily', 'weekly', 'monthly', 'quarterly', 'half-year', 'yearly'])) {
$repeatFreq = 'monthly';
}
$budgetLimit->repeat_freq = $repeatFreq;
$budgetLimit->save();
}
return true;
}
/**
* @param array $backup
*
* @return bool
*/
private function restoreRepeatFreqsToVarchar(array $backup): bool
{
foreach ($backup as $id => $repeatFreq) {
$budgetLimit = BudgetLimit::find($id);
$budgetLimit->repeat_freq = $repeatFreq;
$budgetLimit->save();
}
return true;
}
}

View File

@@ -9,6 +9,7 @@ declare(strict_types = 1);
*/
use Carbon\Carbon;
use FireflyIII\Events\BudgetLimitStored;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Support\Migration\TestData;
use Illuminate\Database\Seeder;
@@ -94,19 +95,24 @@ class TestDataSeeder extends Seeder
// create some special budget limits to test stuff with multiple budget limits
// for a range of dates:
$this->end->startOfMonth();
$this->end->startOfMonth()->addDay();
$budget = TestData::findBudget($user, 'Bills');
$ranges = ['daily','weekly','monthly','quarterly','half-year','yearly'];
foreach($ranges as $range) {
BudgetLimit::create(
$ranges = ['daily', 'weekly', 'monthly', 'quarterly', 'half-year', 'yearly'];
foreach ($ranges as $range) {
$limit = BudgetLimit::create(
[
'budget_id' => $budget->id,
'startdate' => $this->end->format('Y-m-d'),
'amount' => rand(100,200),
'amount' => rand(100, 200),
'repeats' => 0,
'repeat_freq' => $range,
]
);
// also trigger event.
$thisEnd = Navigation::addPeriod($this->end, $range, 0);
$thisEnd->subDay();
event(new BudgetLimitStored($limit, $thisEnd));
$this->end->addDay();
}