diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 8840131812..a4862219a6 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use FireflyIII\Http\Requests; use FireflyIII\Http\Requests\BudgetFormRequest; use FireflyIII\Models\Budget; +use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\LimitRepetition; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use Input; @@ -120,6 +121,13 @@ class BudgetController extends Controller $budgets = Auth::user()->budgets()->where('active', 1)->get(); $inactive = Auth::user()->budgets()->where('active', 0)->get(); + /** + * Do some cleanup: + */ + $repository->cleanupBudgets(); + + + // loop the budgets: $budgets->each( function (Budget $budget) use ($repository) { diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 00403df610..bfd2655e46 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -16,6 +16,31 @@ use Illuminate\Pagination\LengthAwarePaginator; class BudgetRepository implements BudgetRepositoryInterface { + /** + * @return void + */ + public function cleanupBudgets() + { + $limits = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->get(['budget_limits.*']); + + // loop budget limits: + $found = []; + /** @var BudgetLimit $limit */ + foreach ($limits as $limit) { + $key = $limit->budget_id . '-' . $limit->startdate; + if (isset($found[$key])) { + $limit->delete(); + } else { + $found[$key] = true; + } + unset($key); + } + + // delete limits with amount 0: + BudgetLimit::where('amount',0)->delete(); + + } + /** * @param Budget $budget * diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index dce5f21ae4..9b5eef9229 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -20,6 +20,11 @@ interface BudgetRepositoryInterface */ public function destroy(Budget $budget); + /** + * @return void + */ + public function cleanupBudgets(); + /** * @param Budget $budget * @param Carbon $date diff --git a/database/migrations/2015_03_29_174140_changes_for_v336.php b/database/migrations/2015_03_29_174140_changes_for_v336.php index bd15dfcc89..304fd78ac9 100644 --- a/database/migrations/2015_03_29_174140_changes_for_v336.php +++ b/database/migrations/2015_03_29_174140_changes_for_v336.php @@ -68,6 +68,13 @@ class ChangesForV336 extends Migration } ); + // remove a long forgotten index: + Schema::table( + 'budget_limits', function (Blueprint $table) { + $table->dropUnique('unique_limit'); + } + ); + } /** @@ -95,7 +102,7 @@ class ChangesForV336 extends Migration Schema::table( 'accounts', function (Blueprint $table) { $table->text('name')->change(); - $table->decimal('virtual_balance',10,2)->default(0); + $table->decimal('virtual_balance', 10, 2)->default(0); $table->foreign('user_id', 'account_user_id')->references('id')->on('users')->onDelete('cascade'); } ); @@ -124,6 +131,13 @@ class ChangesForV336 extends Migration } ); + // reinstate a long forgotten index: + Schema::table( + 'budget_limits', function (Blueprint $table) { + $table->unique(['budget_id', 'startdate'],'unique_limit'); + } + ); + /** * BILLS