Skip to content

Firefly III: a personal finances manager

Created some triggers, added some fixes. Will probably fail.

James Cole71504c76aceb
diff --git a/app/controllers/BaseController.php b/app/controllers/BaseController.php
index bef3da1fe4..69243c88a4 100644
--- a/app/controllers/BaseController.php
+++ b/app/controllers/BaseController.php
@@ -6,6 +6,13 @@
class BaseController extends Controller
{
+ /**
+ *
+ */
+ public function __construct() {
+ parent::__construct();
+ Event::fire('limits.check');
+ }
/**
* Setup the layout used by the controller.
*
diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php
index 644b849700..a7ff8f3936 100644
--- a/app/controllers/BudgetController.php
+++ b/app/controllers/BudgetController.php
@@ -86,6 +86,7 @@ class BudgetController extends BaseController
$budgets = $this->_repository->get();
$today = new Carbon;
+
return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', $today);
}
diff --git a/app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php b/app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php
index 57ec40eef9..54a389c2df 100644
--- a/app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php
+++ b/app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php
@@ -5,29 +5,21 @@ namespace Firefly\Trigger\Budgets;
use Illuminate\Events\Dispatcher;
/**
- * Class EloquentLimitTrigger
+ * Class EloquentBudgetTrigger
+ *
+ * These triggers don't actually DO anything but are here in case it should be necessary to trigger to something
+ * anyway. I may have forgotten.
*
* @package Firefly\Trigger\Budgets
*/
class EloquentBudgetTrigger
{
- /**
- * @param Dispatcher $events
- */
- public function subscribe(Dispatcher $events)
- {
- //$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
- $events->listen('budgets.destroy', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@destroy');
- $events->listen('budgets.store', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@store');
- $events->listen('budgets.update', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@update');
-
- }
-
/**
* Destroying a budget doesn't do much either.
*
* @param \Budget $budget
+ *
* @return bool
*/
public function destroy(\Budget $budget)
@@ -40,6 +32,7 @@ class EloquentBudgetTrigger
* A new budget is just there, there is nothing to trigger.
*
* @param \Budget $budget
+ *
* @return bool
*/
public function store(\Budget $budget)
@@ -48,10 +41,22 @@ class EloquentBudgetTrigger
}
+ /**
+ * @param Dispatcher $events
+ */
+ public function subscribe(Dispatcher $events)
+ {
+ $events->listen('budgets.destroy', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@destroy');
+ $events->listen('budgets.store', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@store');
+ $events->listen('budgets.update', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@update');
+
+ }
+
/**
* Same. Doesn't do much.
*
* @param \Budget $budget
+ *
* @return bool
*/
public function update(\Budget $budget)
diff --git a/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php b/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php
index 620b114ba1..b16c472480 100644
--- a/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php
+++ b/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php
@@ -2,6 +2,7 @@
namespace Firefly\Trigger\Limits;
+use Carbon\Carbon;
use Illuminate\Events\Dispatcher;
/**
@@ -13,106 +14,111 @@ class EloquentLimitTrigger
{
/**
- * @param Dispatcher $events
+ * This trigger checks if any budgets have limits that repeat every period.
+ * If there are, this method will create any missing repetitions.
*/
- public function subscribe(Dispatcher $events)
+ public function checkRepeatingLimits()
{
- //$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
- $events->listen('limits.destroy', 'Firefly\Trigger\Limits\EloquentLimitTrigger@destroy');
- $events->listen('limits.store', 'Firefly\Trigger\Limits\EloquentLimitTrigger@store');
- $events->listen('limits.update', 'Firefly\Trigger\Limits\EloquentLimitTrigger@update');
+ $limits = \Limit::leftJoin('components', 'components.id', '=', 'limits.component_id')
+ ->where('components.user_id', \Auth::user()->id)
+ ->where('limits.repeats', 1)
+ ->get(['limits.*']);
+
+ /** @var \Limit $limit */
+ foreach ($limits as $limit) {
+ /** @var \Budget $budget */
+ $budget = $limit->budget()->first();
+ // the limit repeats, and there should be at least one repetition already.
+ /** @var \LimitRepetition $primer */
+ $primer = $limit->limitrepetitions()->orderBy('startdate', 'DESC')->first();
+ $today = new Carbon;
+ $start = clone $primer->enddate;
+ // from the primer onwards.
+ while ($start <= $today) {
+ $start->addDay();
+ $end = clone $start;
+ // add period to determin end of limitrepetition:
+ switch ($limit->repeat_freq) {
+ case 'daily':
+ $end->addDay();
+ break;
+ case 'weekly':
+ $end->addWeek();
+ break;
+ case 'monthly':
+ $end->addMonth();
+ break;
+ case 'quarterly':
+ $end->addMonths(3);
+ break;
+ case 'half-year':
+ $end->addMonths(6);
+ break;
+ case 'yearly':
+ $end->addYear();
+ break;
+ }
+ $end->subDay();
+ // create repetition:
+ $limit->createRepetition($start);
+ $start = clone $end;
+ }
+ }
}
+ /**
+ * @param \Limit $limit
+ *
+ * @return bool
+ */
public function destroy(\Limit $limit)
{
return true;
}
+ /**
+ * @param \Limit $limit
+ *
+ * @return bool
+ */
public function store(\Limit $limit)
{
// create a repetition (repetitions) for this limit (we ignore "repeats"):
$limit->createRepetition($limit->startdate);
- // we may want to build a routine that does this for repeating limits.
- // TODO.
return true;
}
+ /**
+ * @param Dispatcher $events
+ */
+ public function subscribe(Dispatcher $events)
+ {
+ //$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
+ $events->listen('limits.destroy', 'Firefly\Trigger\Limits\EloquentLimitTrigger@destroy');
+ $events->listen('limits.store', 'Firefly\Trigger\Limits\EloquentLimitTrigger@store');
+ $events->listen('limits.update', 'Firefly\Trigger\Limits\EloquentLimitTrigger@update');
+ $events->listen('limits.check', 'Firefly\Trigger\Limits\EloquentLimitTrigger@checkRepeatingLimits');
+
+ }
+
+ /**
+ * @param \Limit $limit
+ *
+ * @return bool
+ */
public function update(\Limit $limit)
{
// remove and recreate limit repetitions.
// if limit is not repeating, simply update the repetition to match the limit,
// even though deleting everything is easier.
- foreach($limit->limitrepetitions()->get() as $l) {
+ foreach ($limit->limitrepetitions()->get() as $l) {
$l->delete();
}
$limit->createRepetition($limit->startdate);
+
return true;
}
-
-// /**
-// *
-// */
-// public function updateLimitRepetitions()
-// {
-// if (!\Auth::check() || is_null(\Auth::user())) {
-// \Log::debug('No user for updateLimitRepetitions.');
-// return;
-// }
-//
-// // get budgets with limits:
-// $budgets = \Auth::user()->budgets()
-// ->with(
-// ['limits', 'limits.limitrepetitions']
-// )
-// ->where('components.class', 'Budget')
-// ->get(['components.*']);
-//
-// // double check the non-repeating budgetlimits first.
-// foreach ($budgets as $budget) {
-// \Log::debug('Budgetstart: ' . $budget->name);
-// foreach ($budget->limits as $limit) {
-// if ($limit->repeats == 0) {
-// $limit->createRepetition($limit->startdate);
-// }
-// if ($limit->repeats == 1) {
-// $start = $limit->startdate;
-// $end = new Carbon;
-//
-// // repeat for period:
-// $current = clone $start;
-// \Log::debug('Create repeating limit for #' . $limit->id . ' starting on ' . $current);
-// while ($current <= $end) {
-// \Log::debug('Current is now: ' . $current);
-// $limit->createRepetition(clone $current);
-// // switch period, add time:
-// switch ($limit->repeat_freq) {
-// case 'daily':
-// $current->addDay();
-// break;
-// case 'weekly':
-// $current->addWeek();
-// break;
-// case 'monthly':
-// $current->addMonth();
-// break;
-// case 'quarterly':
-// $current->addMonths(3);
-// break;
-// case 'half-year':
-// $current->addMonths(6);
-// break;
-// case 'yearly':
-// $current->addYear();
-// break;
-// }
-//
-// }
-// }
-// }
-//
-// }
-// }
}
diff --git a/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php
index 96108f9bdc..4b4475e324 100644
--- a/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php
+++ b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php
@@ -13,13 +13,15 @@ use Illuminate\Events\Dispatcher;
class EloquentPiggybankTrigger
{
/**
- * @param \Piggybank $piggyBank
+ * @param \Piggybank $piggyBank
* @param \TransactionJournal $journal
+ * @param \Transaction $transaction
+ *
+ * @return bool
*/
public function createRelatedTransfer(
\Piggybank $piggyBank, \TransactionJournal $journal, \Transaction $transaction
- )
- {
+ ) {
$repetition = $piggyBank->repetitionForDate($journal->date);
if (!is_null($repetition)) {
// get the amount transferred TO this
diff --git a/app/tests/controllers/AccountControllerTest.php b/app/tests/controllers/AccountControllerTest.php
index dfe9578f61..0aa9e62d2c 100644
--- a/app/tests/controllers/AccountControllerTest.php
+++ b/app/tests/controllers/AccountControllerTest.php
@@ -10,6 +10,7 @@ use Zizaco\FactoryMuff\Facade\FactoryMuff as f;
*
* @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
+ * @coversDefaultClass \AccountController
*/
class AccountControllerTest extends TestCase
{
@@ -33,13 +34,22 @@ class AccountControllerTest extends TestCase
Mockery::close();
}
+ /**
+ * @covers ::create
+ */
public function testCreate()
{
+ View::shouldReceive('make')->with('accounts.create')->once();
+
$this->action('GET', 'AccountController@create');
$this->assertResponseOk();
+
}
+ /**
+ * @covers ::delete
+ */
public function testDelete()
{
@@ -49,19 +59,27 @@ class AccountControllerTest extends TestCase
Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
- $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
+
+ // view
+ View::shouldReceive('make')->once()->with('accounts.delete')->andReturn(m::self())->shouldReceive('with')->with(
+ 'account', m::any()
+ );
$this->action('GET', 'AccountController@delete', $account->id);
$this->assertResponseOk();
}
+ /**
+ * @covers ::destroy
+ */
public function testDestroy()
{
$account = f::create('Account');
// for successful binding:
- Auth::shouldReceive('user')->andReturn($this->_user);
- Auth::shouldReceive('check')->andReturn(true);
+ Auth::shouldReceive('user')->once()->andReturn($this->_user);
+ Auth::shouldReceive('check')->once()->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
$this->_repository->shouldReceive('destroy')->once()->andReturn(true);
@@ -70,13 +88,16 @@ class AccountControllerTest extends TestCase
$this->assertSessionHas('success');
}
+ /**
+ * @covers ::destroy
+ */
public function testDestroyFails()
{
$account = f::create('Account');
// for successful binding:
- Auth::shouldReceive('user')->andReturn($this->_user);
- Auth::shouldReceive('check')->andReturn(true);
+ Auth::shouldReceive('user')->once()->andReturn($this->_user);
+ Auth::shouldReceive('check')->once()->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($account->user_id);
$this->_repository->shouldReceive('destroy')->once()->andReturn(false);