Skip to content

Firefly III: a personal finances manager

Fixed a bug where limits and limit repetitions would not be updated and/or created because of double post calls and missing events.

James Cole4f4e6fac167e
diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php
index c4617ffde5..9900f994b2 100644
--- a/app/controllers/BudgetController.php
+++ b/app/controllers/BudgetController.php
@@ -40,7 +40,7 @@ class BudgetController extends BaseController
/*
* A newly stored limit also created a limit repetition.
*/
- Event::fire('limits.store', [$limit]); // Still in use.
+ Event::fire('limits.store', [$limit]); // TODO Nothing responds here!
} else {
if ($amount > 0) {
@@ -49,7 +49,7 @@ class BudgetController extends BaseController
/*
* An updated limit also updates the associated limit repetitions.
*/
- Event::fire('limits.update', [$limit]); // Still in use.
+ Event::fire('limits.update', [$limit]); // TODO Nothing responds here!
} else {
$limit->delete();
}
diff --git a/app/lib/FireflyIII/Event/Budget.php b/app/lib/FireflyIII/Event/Budget.php
new file mode 100644
index 0000000000..063838fe27
--- /dev/null
+++ b/app/lib/FireflyIII/Event/Budget.php
@@ -0,0 +1,64 @@
+<?php
+namespace FireflyIII\Event;
+
+
+use Illuminate\Database\QueryException;
+use Illuminate\Events\Dispatcher;
+
+class Budget
+{
+
+ /**
+ * @param \Limit $limit
+ */
+ public function storeOrUpdateLimit(\Limit $limit)
+ {
+ /** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
+ $dateKit = \App::make('FireflyIII\Shared\Toolkit\Date');
+
+
+ $end = $dateKit->addPeriod(clone $limit->startdate, $limit->repeat_freq, 0);
+ $end->subDay();
+
+ $set = $limit->limitrepetitions()->where('startdate', $limit->startdate->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'))->get();
+ /*
+ * Create new LimitRepetition:
+ */
+ if ($set->count() == 0) {
+
+ $repetition = new \LimitRepetition();
+ $repetition->startdate = $limit->startdate;
+ $repetition->enddate = $end;
+ $repetition->amount = $limit->amount;
+ $repetition->limit()->associate($limit);
+
+ try {
+ $repetition->save();
+ } catch (QueryException $e) {
+ \Log::error('Trying to save new Limitrepetition failed!');
+ \Log::error($e->getMessage());
+ }
+ } else {
+ if ($set->count() == 1) {
+ /*
+ * Update existing one.
+ */
+ $repetition = $set->first();
+ $repetition->amount = $limit->amount;
+ $repetition->save();
+
+ }
+ }
+
+ }
+
+ /**
+ * @param Dispatcher $events
+ */
+ public function subscribe(Dispatcher $events)
+ {
+ $events->listen('limits.store', 'FireflyIII\Event\Budget@storeOrUpdateLimit');
+ $events->listen('limits.update', 'FireflyIII\Event\Budget@storeOrUpdateLimit');
+
+ }
+}
\ No newline at end of file
diff --git a/bootstrap/start.php b/bootstrap/start.php
index 5a7761f3ab..45ff16c5da 100644
--- a/bootstrap/start.php
+++ b/bootstrap/start.php
@@ -89,6 +89,7 @@ require $framework . '/Illuminate/Foundation/start.php';
//Event::subscribe('Firefly\Trigger\Recurring\EloquentRecurringTrigger');
//Event::subscribe('Firefly\Trigger\Journals\EloquentJournalTrigger');
Event::subscribe('FireflyIII\Event\Piggybank');
+Event::subscribe('FireflyIII\Event\Budget');
// TODO event that creates a relationship between transaction journals and recurring events when created.
// TODO event that updates the relationship between transaction journals and recurring events when edited.
diff --git a/public/assets/javascript/firefly/budgets.js b/public/assets/javascript/firefly/budgets.js
index f31d78b637..5f25805989 100644
--- a/public/assets/javascript/firefly/budgets.js
+++ b/public/assets/javascript/firefly/budgets.js
@@ -1,8 +1,8 @@
$(function () {
updateRanges();
- $('input[type="range"]').change(updateSingleRange);
+ //$('input[type="range"]').change(updateSingleRange);
$('input[type="range"]').on('input', updateSingleRange);
- $('input[type="number"]').on('change', updateSingleRange);
+ //$('input[type="number"]').on('change', updateSingleRange);
$('input[type="number"]').on('input', updateSingleRange);
$('.updateIncome').on('click', updateIncome);
@@ -56,6 +56,7 @@ function updateSingleRange(e) {
// send a post to Firefly to update the amount:
console.log('Value is: ' + value);
+ console.log('POST! with ID ' + id + ' AND value ' + value);
$.post('budgets/amount/' + id, {amount: value}).success(function (data) {
console.log('Budget ' + data.name + ' updated!');
// update the link if relevant: