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

This commit is contained in:
James Cole 2014-11-17 21:50:11 +01:00
parent 15e99bd672
commit 4f4e6fac16
4 changed files with 70 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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