Basic testing, not full coverage.

This commit is contained in:
James Cole
2014-08-19 13:24:11 +02:00
parent f5330728d4
commit f00dfd2859
17 changed files with 325 additions and 162 deletions

View File

@@ -50,8 +50,8 @@ class BudgetController extends BaseController
*/ */
public function destroy(Budget $budget) public function destroy(Budget $budget)
{ {
Event::fire('budgets.destroy',[$budget]); // just before deletion.
$result = $this->_repository->destroy($budget); $result = $this->_repository->destroy($budget);
Event::fire('budgets.change');
if ($result === true) { if ($result === true) {
Session::flash('success', 'The budget was deleted.'); Session::flash('success', 'The budget was deleted.');
if (Input::get('from') == 'date') { if (Input::get('from') == 'date') {
@@ -86,8 +86,6 @@ class BudgetController extends BaseController
$budgets = $this->_repository->get(); $budgets = $this->_repository->get();
$today = new Carbon; $today = new Carbon;
Event::fire('budgets.change');
return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', $today); return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', $today);
} }
@@ -102,7 +100,6 @@ class BudgetController extends BaseController
$set = $this->_repository->get(); $set = $this->_repository->get();
$budgets = $this->_budgets->organizeByDate($set); $budgets = $this->_budgets->organizeByDate($set);
Event::fire('budgets.change');
return View::make('budgets.indexByDate')->with('budgets', $budgets); return View::make('budgets.indexByDate')->with('budgets', $budgets);
@@ -146,7 +143,7 @@ class BudgetController extends BaseController
$budget = $this->_repository->store(Input::all()); $budget = $this->_repository->store(Input::all());
if ($budget->validate()) { if ($budget->validate()) {
Event::fire('budgets.change'); Event::fire('budgets.store',[$budget]);
Session::flash('success', 'Budget created!'); Session::flash('success', 'Budget created!');
if (Input::get('create') == '1') { if (Input::get('create') == '1') {
@@ -175,7 +172,7 @@ class BudgetController extends BaseController
{ {
$budget = $this->_repository->update($budget, Input::all()); $budget = $this->_repository->update($budget, Input::all());
if ($budget->validate()) { if ($budget->validate()) {
Event::fire('budgets.change'); Event::fire('budgets.update',[$budget]);
Session::flash('success', 'Budget "' . $budget->name . '" updated.'); Session::flash('success', 'Budget "' . $budget->name . '" updated.');
if (Input::get('from') == 'date') { if (Input::get('from') == 'date') {

View File

@@ -31,13 +31,12 @@ class LimitController extends BaseController
{ {
$periods = \Config::get('firefly.periods_to_text'); $periods = \Config::get('firefly.periods_to_text');
$prefilled = [ $prefilled = [
'startdate' => Input::get('startdate') ? : date('Y-m-d'), 'startdate' => \Input::get('startdate') ? : date('Y-m-d'),
'repeat_freq' => Input::get('repeat_freq') ? : 'monthly', 'repeat_freq' => \Input::get('repeat_freq') ? : 'monthly',
'budget_id' => $budget ? $budget->id : null 'budget_id' => $budget ? $budget->id : null
]; ];
$budgets = $this->_budgets->getAsSelectList(); $budgets = $this->_budgets->getAsSelectList();
Event::fire('budgets.change');
return View::make('limits.create')->with('budgets', $budgets)->with( return View::make('limits.create')->with('budgets', $budgets)->with(
'periods', $periods 'periods', $periods
@@ -61,6 +60,7 @@ class LimitController extends BaseController
*/ */
public function destroy(\Limit $limit) public function destroy(\Limit $limit)
{ {
Event::fire('limits.destroy',[$limit]); // before
$success = $this->_limits->destroy($limit); $success = $this->_limits->destroy($limit);
if ($success) { if ($success) {
@@ -68,7 +68,6 @@ class LimitController extends BaseController
} else { } else {
Session::flash('error', 'Could not delete the envelope. Check the logs to be sure.'); Session::flash('error', 'Could not delete the envelope. Check the logs to be sure.');
} }
Event::fire('budgets.change');
if (Input::get('from') == 'date') { if (Input::get('from') == 'date') {
return Redirect::route('budgets.index'); return Redirect::route('budgets.index');
} else { } else {
@@ -103,7 +102,7 @@ class LimitController extends BaseController
$limit = $this->_limits->store(Input::all()); $limit = $this->_limits->store(Input::all());
if ($limit->validate()) { if ($limit->validate()) {
Session::flash('success', 'Envelope created!'); Session::flash('success', 'Envelope created!');
Event::fire('budgets.change'); Event::fire('limits.store',[$limit]);
if (Input::get('from') == 'date') { if (Input::get('from') == 'date') {
return Redirect::route('budgets.index'); return Redirect::route('budgets.index');
} else { } else {
@@ -126,18 +125,13 @@ class LimitController extends BaseController
*/ */
public function update(\Limit $limit) public function update(\Limit $limit)
{ {
// TODO move logic to repository.
/** @var \Limit $limit */
$limit->startdate = new \Carbon\Carbon(Input::get('date')); $limit = $this->_limits->update($limit,Input::all());
$limit->repeat_freq = Input::get('period');
$limit->repeats = !is_null(Input::get('repeats')) && Input::get('repeats') == '1' ? 1 : 0; if ($limit->validate()) {
$limit->amount = floatval(Input::get('amount')); Event::fire('limits.update',[$limit]);
Event::fire('budgets.change');
if ($limit->save()) {
Session::flash('success', 'Limit saved!'); Session::flash('success', 'Limit saved!');
foreach ($limit->limitrepetitions()->get() as $rep) {
$rep->delete();
}
if (Input::get('from') == 'date') { if (Input::get('from') == 'date') {
return Redirect::route('budgets.index'); return Redirect::route('budgets.index');
} else { } else {

View File

@@ -37,7 +37,7 @@ class TransactionController extends BaseController
$budgets = $budgetRepository->getAsSelectList(); $budgets = $budgetRepository->getAsSelectList();
$budgets[0] = '(no budget)'; $budgets[0] = '(no budget)';
// get the number of piggy banks. // get the piggy banks.
/** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggyRepository */ /** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggyRepository */
$piggyRepository = App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface'); $piggyRepository = App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
$piggies = $piggyRepository->get(); $piggies = $piggyRepository->get();
@@ -96,11 +96,22 @@ class TransactionController extends BaseController
$budgets = $budgetRepository->getAsSelectList(); $budgets = $budgetRepository->getAsSelectList();
$budgets[0] = '(no budget)'; $budgets[0] = '(no budget)';
// get the piggy banks.
/** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggyRepository */
$piggyRepository = App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
$piggies = $piggyRepository->get();
// piggy bank id?
$piggyBankId = null;
foreach($journal->transactions as $t) {
$piggyBankId = $t->piggybank_id;
}
// data to properly display form: // data to properly display form:
$data = [ $data = [
'date' => $journal->date->format('Y-m-d'), 'date' => $journal->date->format('Y-m-d'),
'category' => '', 'category' => '',
'budget_id' => 0 'budget_id' => 0,
'piggybank_id' => $piggyBankId
]; ];
$category = $journal->categories()->first(); $category = $journal->categories()->first();
if (!is_null($category)) { if (!is_null($category)) {
@@ -130,7 +141,7 @@ class TransactionController extends BaseController
return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with( return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with(
'what', $what 'what', $what
)->with('budgets', $budgets)->with('data', $data); )->with('budgets', $budgets)->with('data', $data)->with('piggies',$piggies);
} }
/** /**

View File

@@ -121,6 +121,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$limit->repeat_freq = $data['repeat_freq']; $limit->repeat_freq = $data['repeat_freq'];
if ($limit->validate()) { if ($limit->validate()) {
$limit->save(); $limit->save();
\Event::fire('limits.store',[$limit]);
} }
} }
if ($budget->validate()) { if ($budget->validate()) {

View File

@@ -26,6 +26,22 @@ class EloquentLimitRepository implements LimitRepositoryInterface
return true; return true;
} }
/**
* @param \Limit $limit
* @param $data
* @return mixed|void
*/
public function update(\Limit $limit, $data)
{
$limit->startdate = new Carbon($data['startdate']);
$limit->repeat_freq = $data['period'];
$limit->repeats = isset($data['repeats']) && $data['repeats'] == '1' ? 1 : 0;
$limit->amount = floatval($data['amount']);
$limit->save();
return $limit;
}
/** /**
* @param $limitId * @param $limitId
* *
@@ -41,8 +57,8 @@ class EloquentLimitRepository implements LimitRepositoryInterface
/** /**
* @param \Budget $budget * @param \Budget $budget
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return mixed * @return mixed
*/ */
@@ -97,11 +113,11 @@ class EloquentLimitRepository implements LimitRepositoryInterface
} }
// find existing: // find existing:
$count = \Limit:: $count = \Limit::
leftJoin('components', 'components.id', '=', 'limits.component_id')->where( leftJoin('components', 'components.id', '=', 'limits.component_id')->where(
'components.user_id', \Auth::user()->id 'components.user_id', \Auth::user()->id
)->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where( )->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where(
'repeat_freq', $data['period'] 'repeat_freq', $data['period']
)->count(); )->count();
if ($count > 0) { if ($count > 0) {
\Session::flash('error', 'There already is an entry for these parameters.'); \Session::flash('error', 'There already is an entry for these parameters.');

View File

@@ -19,6 +19,13 @@ interface LimitRepositoryInterface
*/ */
public function store($data); public function store($data);
/**
* @param \Limit $limit
* @param $data
* @return mixed
*/
public function update(\Limit $limit, $data);
/** /**
* @param \Budget $budget * @param \Budget $budget
* @param Carbon $start * @param Carbon $start

View File

@@ -121,8 +121,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$fromTransaction->description = null; $fromTransaction->description = null;
$fromTransaction->amount = $amountFrom; $fromTransaction->amount = $amountFrom;
if (!$fromTransaction->validate()) { if (!$fromTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()->first( throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()->first());
));
} }
$fromTransaction->save(); $fromTransaction->save();
@@ -151,9 +150,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
{ {
return \Auth::user()->transactionjournals()->with( return \Auth::user()->transactionjournals()->with(
['transactions' => function ($q) { ['transactions' => function ($q) {
return $q->orderBy('amount', 'ASC'); return $q->orderBy('amount', 'ASC');
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account', }, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
'transactions.account.accounttype'] 'transactions.account.accounttype']
) )
->where('id', $journalId)->first(); ->where('id', $journalId)->first();
} }
@@ -168,7 +167,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** /**
* @param \Account $account * @param \Account $account
* @param Carbon $date * @param Carbon $date
* *
* @return mixed * @return mixed
*/ */
@@ -197,9 +196,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** /**
* @param \Account $account * @param \Account $account
* @param int $count * @param int $count
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return mixed * @return mixed
*/ */
@@ -236,8 +235,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$query = \Auth::user()->transactionjournals()->with( $query = \Auth::user()->transactionjournals()->with(
[ [
'transactions' => function ($q) { 'transactions' => function ($q) {
return $q->orderBy('amount', 'ASC'); return $q->orderBy('amount', 'ASC');
}, },
'transactions.account', 'transactions.account',
'transactions.account.accounttype', 'transactions.account.accounttype',
'transactioncurrency', 'transactioncurrency',
@@ -300,8 +299,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
// find budget: // find budget:
$budget = isset($data['budget_id']) ? $budRepository->find(intval($data['budget_id'])) : null; $budget = isset($data['budget_id']) ? $budRepository->find(intval($data['budget_id'])) : null;
// // find amount & description:
// // find amount & description:
$description = trim($data['description']); $description = trim($data['description']);
$amount = floatval($data['amount']); $amount = floatval($data['amount']);
$date = new Carbon($data['date']); $date = new Carbon($data['date']);
@@ -323,25 +321,19 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$piggyBank = $piggyRepository->find(intval($data['piggybank_id'])); $piggyBank = $piggyRepository->find(intval($data['piggybank_id']));
if ($piggyBank) { if ($piggyBank) {
if ($toAccount->id == $piggyBank->account_id) { // one of the two transactions may be connected to this piggy bank.
$connected = false;
// find the transaction connected to the $toAccount: foreach ($transactionJournal->transactions()->get() as $transaction) {
/** @var \Transaction $transaction */ if ($transaction->account_id == $piggyBank->account_id) {
$transaction $connected = true;
= $transactionJournal->transactions()->where('account_id', $toAccount->id)->first(); $transaction->piggybank()->associate($piggyBank);
// connect the piggy to it: $transaction->save();
$transaction->piggybank()->associate($piggyBank); \Event::fire('piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]);
$transaction->save(); break;
\Event::fire( }
'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction] }
); if ($connected === false) {
} else { \Session::flash('warning', 'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from any of the accounts in this transfer');
\Session::flash(
'warning',
'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from account "' . e(
$toAccount->name
) . '", so the money isn\'t added to the piggy bank.'
);
} }
} }
} }
@@ -405,7 +397,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** @var \Transaction $transaction */ /** @var \Transaction $transaction */
foreach ($transactions as $transaction) { foreach ($transactions as $transaction) {
if (!is_null($transaction->piggybank()->first())) { if (!is_null($transaction->piggybank()->first())) {
$transaction->piggybank()->detach($transaction->piggybank()->first()->first()); $transaction->piggybank_id = null;
$transaction->save(); $transaction->save();
} }
} }
@@ -415,6 +407,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$transactions[1]->amount = $amount; $transactions[1]->amount = $amount;
// switch on type to properly change things: // switch on type to properly change things:
$fireEvent = false;
switch ($journal->transactiontype->type) { switch ($journal->transactiontype->type) {
case 'Withdrawal': case 'Withdrawal':
// means transaction[0] is the users account. // means transaction[0] is the users account.
@@ -454,24 +447,22 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** @var \Piggybank $piggyBank */ /** @var \Piggybank $piggyBank */
$piggyBank = $piggyRepository->find(intval($data['piggybank_id'])); $piggyBank = $piggyRepository->find(intval($data['piggybank_id']));
if ($piggyBank) { // loop transactions and re-attach the piggy bank:
if ($toAccount->id == $piggyBank->account_id) {
// find the transaction connected to the $toAccount: if ($piggyBank) {
/** @var \Transaction $transaction */
$transaction $connected = false;
= $journal->transactions()->where('account_id', $toAccount->id)->first(); foreach ($journal->transactions()->get() as $transaction) {
// connect the piggy to it: if ($transaction->account_id == $piggyBank->account_id) {
$transaction->piggybank()->associate($piggyBank); $connected = true;
$transaction->save(); $transaction->piggybank()->associate($piggyBank);
\Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank, $journal]); $transaction->save();
} else { $fireEvent = true;
\Session::flash( break;
'warning', }
'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from account "' . e( }
$toAccount->name if ($connected === false) {
) . '", so the money isn\'t added to the piggy bank.' \Session::flash('warning', 'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from any of the accounts in this transfer');
);
} }
} }
} }
@@ -488,6 +479,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
if ($journal->validate()) { if ($journal->validate()) {
$journal->save(); $journal->save();
} }
if($fireEvent) {
\Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank]);
}
return $journal; return $journal;

View File

@@ -0,0 +1,62 @@
<?php
namespace Firefly\Trigger\Budgets;
use Illuminate\Events\Dispatcher;
/**
* Class EloquentLimitTrigger
*
* @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)
{
return true;
}
/**
* A new budget is just there, there is nothing to trigger.
*
* @param \Budget $budget
* @return bool
*/
public function store(\Budget $budget)
{
return true;
}
/**
* Same. Doesn't do much.
*
* @param \Budget $budget
* @return bool
*/
public function update(\Budget $budget)
{
return true;
}
}

View File

@@ -2,7 +2,6 @@
namespace Firefly\Trigger\Limits; namespace Firefly\Trigger\Limits;
use Carbon\Carbon;
use Illuminate\Events\Dispatcher; use Illuminate\Events\Dispatcher;
/** /**
@@ -18,71 +17,102 @@ class EloquentLimitTrigger
*/ */
public function subscribe(Dispatcher $events) public function subscribe(Dispatcher $events)
{ {
$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions'); //$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');
} }
/** public function destroy(\Limit $limit)
*
*/
public function updateLimitRepetitions()
{ {
if (!\Auth::check() || is_null(\Auth::user())) { return true;
\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;
}
}
}
}
}
} }
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;
}
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) {
$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,12 +13,13 @@ use Illuminate\Events\Dispatcher;
class EloquentPiggybankTrigger class EloquentPiggybankTrigger
{ {
/** /**
* @param \Piggybank $piggyBank * @param \Piggybank $piggyBank
* @param \TransactionJournal $journal * @param \TransactionJournal $journal
*/ */
public function createRelatedTransfer( public function createRelatedTransfer(
\Piggybank $piggyBank, \TransactionJournal $journal, \Transaction $transaction \Piggybank $piggyBank, \TransactionJournal $journal, \Transaction $transaction
) { )
{
$repetition = $piggyBank->repetitionForDate($journal->date); $repetition = $piggyBank->repetitionForDate($journal->date);
if (!is_null($repetition)) { if (!is_null($repetition)) {
// get the amount transferred TO this // get the amount transferred TO this
@@ -226,15 +227,17 @@ class EloquentPiggybankTrigger
} }
} }
public function updateRelatedTransfer(\Piggybank $piggyBank, \TransactionJournal $journal) public function updateRelatedTransfer(\Piggybank $piggyBank)
{ {
die('no impl updateRelatedTransfer'); // fire the "update" trigger which should handle things just fine:
\Event::fire('piggybanks.update', [$piggyBank]);
} }
// //
// /** // /**
// * // *

View File

@@ -74,6 +74,9 @@ class Limit extends Ardent
} }
/** /**
* Create a new repetition for this limit, starting on
* the given date.
*
* @param Carbon $start * @param Carbon $start
*/ */
public function createRepetition(Carbon $start) public function createRepetition(Carbon $start)

View File

@@ -65,7 +65,7 @@ class BudgetControllerTest extends TestCase
Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
Event::shouldReceive('fire')->once()->with('budgets.change'); Event::shouldReceive('fire')->once()->with('budgets.destroy',[$budget]);
$this->_repository->shouldReceive('destroy')->once()->andReturn(true); $this->_repository->shouldReceive('destroy')->once()->andReturn(true);
$this->action('POST', 'BudgetController@destroy', $budget->id); $this->action('POST', 'BudgetController@destroy', $budget->id);
@@ -81,7 +81,7 @@ class BudgetControllerTest extends TestCase
Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
Event::shouldReceive('fire')->once()->with('budgets.change'); Event::shouldReceive('fire')->once()->with('budgets.destroy',[$budget]);
$this->_repository->shouldReceive('destroy')->once()->andReturn(true); $this->_repository->shouldReceive('destroy')->once()->andReturn(true);
$this->action('POST', 'BudgetController@destroy', [$budget->id, 'from' => 'date']); $this->action('POST', 'BudgetController@destroy', [$budget->id, 'from' => 'date']);
@@ -97,7 +97,7 @@ class BudgetControllerTest extends TestCase
Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
Event::shouldReceive('fire')->once()->with('budgets.change'); Event::shouldReceive('fire')->once()->with('budgets.destroy',[$budget]);
$this->_repository->shouldReceive('destroy')->once()->andReturn(false); $this->_repository->shouldReceive('destroy')->once()->andReturn(false);
@@ -233,7 +233,7 @@ class BudgetControllerTest extends TestCase
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_repository->shouldReceive('update')->andReturn($budget); $this->_repository->shouldReceive('update')->andReturn($budget);
Event::shouldReceive('fire')->with('budgets.change'); Event::shouldReceive('fire')->with('budgets.update',[$budget]);
$this->action('POST', 'BudgetController@update', $budget->id); $this->action('POST', 'BudgetController@update', $budget->id);
$this->assertRedirectedToRoute('budgets.index.budget'); $this->assertRedirectedToRoute('budgets.index.budget');
@@ -248,7 +248,7 @@ class BudgetControllerTest extends TestCase
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id);
$this->_repository->shouldReceive('update')->andReturn($budget); $this->_repository->shouldReceive('update')->andReturn($budget);
Event::shouldReceive('fire')->with('budgets.change'); Event::shouldReceive('fire')->with('budgets.update',[$budget]);
//$this->_user->shouldReceive('budgets')->andReturn([]); // trigger //$this->_user->shouldReceive('budgets')->andReturn([]); // trigger
$this->action('POST', 'BudgetController@update', [$budget->id, 'from' => 'date']); $this->action('POST', 'BudgetController@update', [$budget->id, 'from' => 'date']);

View File

@@ -175,6 +175,8 @@ class LimitControllerTest extends TestCase
$this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->_limits->shouldReceive('update')->once()->andReturn($limit);
$this->action( $this->action(
'POST', 'LimitController@update', 'POST', 'LimitController@update',
@@ -200,6 +202,9 @@ class LimitControllerTest extends TestCase
$this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
unset($limit->amount);
$this->_limits->shouldReceive('update')->once()->andReturn($limit);
$this->action( $this->action(
'POST', 'LimitController@update', 'POST', 'LimitController@update',
@@ -219,6 +224,8 @@ class LimitControllerTest extends TestCase
$this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($limit->budget()->first()->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->_limits->shouldReceive('update')->once()->andReturn($limit);
$this->action( $this->action(
'POST', 'LimitController@update', 'POST', 'LimitController@update',

View File

@@ -92,7 +92,7 @@ class PiggybankControllerTest extends TestCase
); );
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->_piggybanks->shouldReceive('destroy')->andReturn(true); $this->_piggybanks->shouldReceive('destroy')->andReturn(true);
Event::shouldReceive('fire')->with('piggybanks.change'); Event::shouldReceive('fire')->with('piggybanks.destroy', [$piggyBank]);
$this->action('POST', 'PiggybankController@destroy', $piggyBank->id); $this->action('POST', 'PiggybankController@destroy', $piggyBank->id);
@@ -114,6 +114,7 @@ class PiggybankControllerTest extends TestCase
); );
$this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email');
$this->action('GET', 'PiggybankController@edit', $piggyBank->id); $this->action('GET', 'PiggybankController@edit', $piggyBank->id);
$this->assertResponseOk(); $this->assertResponseOk();
} }
@@ -153,6 +154,7 @@ class PiggybankControllerTest extends TestCase
$three->account()->associate($aTwo); $three->account()->associate($aTwo);
$this->_piggybanks->shouldReceive('get')->andReturn([$one, $two, $three]); $this->_piggybanks->shouldReceive('get')->andReturn([$one, $two, $three]);
$this->_piggybanks->shouldReceive('countRepeating')->andReturn(0); $this->_piggybanks->shouldReceive('countRepeating')->andReturn(0);
$this->_piggybanks->shouldReceive('leftOnAccount')->andReturn(0);
$this->_piggybanks->shouldReceive('countNonrepeating')->andReturn(0); $this->_piggybanks->shouldReceive('countNonrepeating')->andReturn(0);
Event::shouldReceive('fire')->with('piggybanks.change'); Event::shouldReceive('fire')->with('piggybanks.change');
@@ -169,7 +171,7 @@ class PiggybankControllerTest extends TestCase
$input = [ $input = [
$piggyBank->id, $piggyBank->id,
'amount' => 10.0, 'amount' => 10.0,
'what' => 'add' 'what' => 'add'
]; ];
// for binding // for binding
@@ -179,7 +181,7 @@ class PiggybankControllerTest extends TestCase
$piggyBank->account()->first()->user_id $piggyBank->account()->first()->user_id
); );
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
Event::shouldReceive('fire')->with('piggybanks.change'); Event::shouldReceive('fire');//->with('piggybanks.modifyAmountAdd', [$piggyBank, 10.0]);
$this->_piggybanks->shouldReceive('modifyAmount')->once(); $this->_piggybanks->shouldReceive('modifyAmount')->once();
$this->_piggybanks->shouldReceive('leftOnAccount')->once()->andReturn(200); $this->_piggybanks->shouldReceive('leftOnAccount')->once()->andReturn(200);
@@ -199,7 +201,7 @@ class PiggybankControllerTest extends TestCase
$input = [ $input = [
$piggyBank->id, $piggyBank->id,
'amount' => 10.0, 'amount' => 10.0,
'what' => 'add' 'what' => 'add'
]; ];
// for binding // for binding
@@ -207,7 +209,7 @@ class PiggybankControllerTest extends TestCase
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
$this->_user->shouldReceive('getAttribute')->with('id')->andReturn($piggyBank->account()->first()->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($piggyBank->account()->first()->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
Event::shouldReceive('fire')->with('piggybanks.change'); Event::shouldReceive('fire')->with('piggybanks.modifyAmountAdd', [$piggyBank, -10.0]);
$this->_piggybanks->shouldReceive('leftOnAccount')->once()->andReturn(5); $this->_piggybanks->shouldReceive('leftOnAccount')->once()->andReturn(5);
@@ -228,7 +230,7 @@ class PiggybankControllerTest extends TestCase
$input = [ $input = [
$piggyBank->id, $piggyBank->id,
'amount' => 10.0, 'amount' => 10.0,
'what' => 'yomoma' 'what' => 'yomoma'
]; ];
// for binding // for binding
@@ -267,7 +269,7 @@ class PiggybankControllerTest extends TestCase
$input = [ $input = [
$rep->piggybank()->first()->id, $rep->piggybank()->first()->id,
'amount' => 10.0, 'amount' => 10.0,
'what' => 'remove' 'what' => 'remove'
]; ];
$this->action('POST', 'PiggybankController@modMoney', $input); $this->action('POST', 'PiggybankController@modMoney', $input);
@@ -297,7 +299,7 @@ class PiggybankControllerTest extends TestCase
$input = [ $input = [
$rep->piggybank()->first()->id, $rep->piggybank()->first()->id,
'amount' => 10.0, 'amount' => 10.0,
'what' => 'remove' 'what' => 'remove'
]; ];
$this->action('POST', 'PiggybankController@modMoney', $input); $this->action('POST', 'PiggybankController@modMoney', $input);
@@ -391,7 +393,7 @@ class PiggybankControllerTest extends TestCase
$piggyBank->account()->first()->user_id $piggyBank->account()->first()->user_id
); );
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
Event::shouldReceive('fire')->with('piggybanks.change'); Event::shouldReceive('fire')->with('piggybanks.update',[$piggyBank]);
$this->action('POST', 'PiggybankController@update', $piggyBank->id); $this->action('POST', 'PiggybankController@update', $piggyBank->id);
$this->assertResponseStatus(302); $this->assertResponseStatus(302);

View File

@@ -123,6 +123,8 @@ class TransactionControllerTest extends TestCase
$this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($journal->user_id); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($journal->user_id);
$this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
$this->_piggies->shouldReceive('get')->once()->andReturn([]);
$this->action('GET', 'TransactionController@edit', $journal->id); $this->action('GET', 'TransactionController@edit', $journal->id);
$this->assertResponseOk(); $this->assertResponseOk();
} }
@@ -152,6 +154,8 @@ class TransactionControllerTest extends TestCase
$this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]);
$this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]);
$this->_piggies->shouldReceive('get')->once()->andReturn([]);
// for binding // for binding
Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('user')->andReturn($this->_user);
Auth::shouldReceive('check')->andReturn(true); Auth::shouldReceive('check')->andReturn(true);
@@ -186,6 +190,7 @@ class TransactionControllerTest extends TestCase
$this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]);
$this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]);
$this->_piggies->shouldReceive('get')->once()->andReturn([]);
// for binding // for binding
Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('user')->andReturn($this->_user);

View File

@@ -138,6 +138,36 @@
</div> </div>
</div> </div>
<!-- RELATE THIS TRANSFER TO A PIGGY BANK -->
@if($what == 'transfer' && count($piggies) > 0)
<div class="form-group">
<label for="piggybank_id" class="col-sm-4 control-label">
Piggy bank
</label>
<div class="col-sm-8">
<select name="piggybank_id" class="form-control">
<option value="0" label="(no piggy bank)">(no piggy bank)</option>
@foreach($piggies as $piggy)
@if($piggy->id == Input::old('piggybank_id') || $piggy->id == $data['piggybank_id'])
<option value="{{$piggy->id}}" label="{{{$piggy->name}}}" selected="selected ">{{{$piggy->name}}}</option>
@else
<option value="{{$piggy->id}}" label="{{{$piggy->name}}}">{{{$piggy->name}}}</option>
@endif
@endforeach
</select>
@if($errors->has('piggybank_id'))
<p class="text-danger">{{$errors->first('piggybank_id')}}</p>
@else
<span class="help-block">
You can directly add the amount you're transferring
to one of your piggy banks, provided they are related to the account your
transferring <em>to</em>.
</span>
@endif
</div>
</div>
@endif
</div> </div>
</div> </div>

View File

@@ -93,6 +93,7 @@ Event::subscribe('Firefly\Helper\Form\FormTrigger');
// do something with events: // do something with events:
Event::subscribe('Firefly\Trigger\Limits\EloquentLimitTrigger'); Event::subscribe('Firefly\Trigger\Limits\EloquentLimitTrigger');
Event::subscribe('Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger'); Event::subscribe('Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger');
Event::subscribe('Firefly\Trigger\Budgets\EloquentBudgetTrigger');
//App::booted( //App::booted(
// function () { // function () {