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

This commit is contained in:
James Cole 2014-08-19 20:38:49 +02:00
parent eacad20f51
commit 71504c76ac
6 changed files with 144 additions and 102 deletions

View File

@ -6,6 +6,13 @@
class BaseController extends Controller
{
/**
*
*/
public function __construct() {
parent::__construct();
Event::fire('limits.check');
}
/**
* Setup the layout used by the controller.
*

View File

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

View File

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

View File

@ -2,6 +2,7 @@
namespace Firefly\Trigger\Limits;
use Carbon\Carbon;
use Illuminate\Events\Dispatcher;
/**
@ -12,6 +13,84 @@ use Illuminate\Events\Dispatcher;
class EloquentLimitTrigger
{
/**
* This trigger checks if any budgets have limits that repeat every period.
* If there are, this method will create any missing repetitions.
*/
public function checkRepeatingLimits()
{
$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);
return true;
}
/**
* @param Dispatcher $events
*/
@ -21,98 +100,25 @@ class EloquentLimitTrigger
$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');
}
public function destroy(\Limit $limit)
{
return true;
}
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 \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;
// }
//
// }
// }
// }
//
// }
// }
}

View File

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

View File

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