From f00dfd28594a7eb13d66b3ab9aaeef578a36b2d8 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 19 Aug 2014 13:24:11 +0200 Subject: [PATCH] Basic testing, not full coverage. --- app/controllers/BudgetController.php | 9 +- app/controllers/LimitController.php | 26 ++- app/controllers/TransactionController.php | 17 +- .../Budget/EloquentBudgetRepository.php | 1 + .../Storage/Limit/EloquentLimitRepository.php | 30 +++- .../Limit/LimitRepositoryInterface.php | 7 + .../EloquentTransactionJournalRepository.php | 94 +++++------ .../Trigger/Budgets/EloquentBudgetTrigger.php | 62 +++++++ .../Trigger/Limits/EloquentLimitTrigger.php | 154 +++++++++++------- .../Piggybanks/EloquentPiggybankTrigger.php | 11 +- app/models/Limit.php | 3 + .../controllers/BudgetControllerTest.php | 10 +- app/tests/controllers/LimitControllerTest.php | 7 + .../controllers/PiggybankControllerTest.php | 20 ++- .../controllers/TransactionControllerTest.php | 5 + app/views/transactions/edit.blade.php | 30 ++++ bootstrap/start.php | 1 + 17 files changed, 325 insertions(+), 162 deletions(-) create mode 100644 app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php index 5eb96b9ce6..644b849700 100644 --- a/app/controllers/BudgetController.php +++ b/app/controllers/BudgetController.php @@ -50,8 +50,8 @@ class BudgetController extends BaseController */ public function destroy(Budget $budget) { + Event::fire('budgets.destroy',[$budget]); // just before deletion. $result = $this->_repository->destroy($budget); - Event::fire('budgets.change'); if ($result === true) { Session::flash('success', 'The budget was deleted.'); if (Input::get('from') == 'date') { @@ -86,8 +86,6 @@ class BudgetController extends BaseController $budgets = $this->_repository->get(); $today = new Carbon; - Event::fire('budgets.change'); - return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', $today); } @@ -102,7 +100,6 @@ class BudgetController extends BaseController $set = $this->_repository->get(); $budgets = $this->_budgets->organizeByDate($set); - Event::fire('budgets.change'); return View::make('budgets.indexByDate')->with('budgets', $budgets); @@ -146,7 +143,7 @@ class BudgetController extends BaseController $budget = $this->_repository->store(Input::all()); if ($budget->validate()) { - Event::fire('budgets.change'); + Event::fire('budgets.store',[$budget]); Session::flash('success', 'Budget created!'); if (Input::get('create') == '1') { @@ -175,7 +172,7 @@ class BudgetController extends BaseController { $budget = $this->_repository->update($budget, Input::all()); if ($budget->validate()) { - Event::fire('budgets.change'); + Event::fire('budgets.update',[$budget]); Session::flash('success', 'Budget "' . $budget->name . '" updated.'); if (Input::get('from') == 'date') { diff --git a/app/controllers/LimitController.php b/app/controllers/LimitController.php index 4a9ae778b8..f8547b7bf9 100644 --- a/app/controllers/LimitController.php +++ b/app/controllers/LimitController.php @@ -31,13 +31,12 @@ class LimitController extends BaseController { $periods = \Config::get('firefly.periods_to_text'); $prefilled = [ - 'startdate' => Input::get('startdate') ? : date('Y-m-d'), - 'repeat_freq' => Input::get('repeat_freq') ? : 'monthly', + 'startdate' => \Input::get('startdate') ? : date('Y-m-d'), + 'repeat_freq' => \Input::get('repeat_freq') ? : 'monthly', 'budget_id' => $budget ? $budget->id : null ]; $budgets = $this->_budgets->getAsSelectList(); - Event::fire('budgets.change'); return View::make('limits.create')->with('budgets', $budgets)->with( 'periods', $periods @@ -61,6 +60,7 @@ class LimitController extends BaseController */ public function destroy(\Limit $limit) { + Event::fire('limits.destroy',[$limit]); // before $success = $this->_limits->destroy($limit); if ($success) { @@ -68,7 +68,6 @@ class LimitController extends BaseController } else { Session::flash('error', 'Could not delete the envelope. Check the logs to be sure.'); } - Event::fire('budgets.change'); if (Input::get('from') == 'date') { return Redirect::route('budgets.index'); } else { @@ -103,7 +102,7 @@ class LimitController extends BaseController $limit = $this->_limits->store(Input::all()); if ($limit->validate()) { Session::flash('success', 'Envelope created!'); - Event::fire('budgets.change'); + Event::fire('limits.store',[$limit]); if (Input::get('from') == 'date') { return Redirect::route('budgets.index'); } else { @@ -126,18 +125,13 @@ class LimitController extends BaseController */ public function update(\Limit $limit) { - // TODO move logic to repository. - /** @var \Limit $limit */ - $limit->startdate = new \Carbon\Carbon(Input::get('date')); - $limit->repeat_freq = Input::get('period'); - $limit->repeats = !is_null(Input::get('repeats')) && Input::get('repeats') == '1' ? 1 : 0; - $limit->amount = floatval(Input::get('amount')); - Event::fire('budgets.change'); - if ($limit->save()) { + + + $limit = $this->_limits->update($limit,Input::all()); + + if ($limit->validate()) { + Event::fire('limits.update',[$limit]); Session::flash('success', 'Limit saved!'); - foreach ($limit->limitrepetitions()->get() as $rep) { - $rep->delete(); - } if (Input::get('from') == 'date') { return Redirect::route('budgets.index'); } else { diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 010625c107..8ec4dd9ebe 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -37,7 +37,7 @@ class TransactionController extends BaseController $budgets = $budgetRepository->getAsSelectList(); $budgets[0] = '(no budget)'; - // get the number of piggy banks. + // get the piggy banks. /** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggyRepository */ $piggyRepository = App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface'); $piggies = $piggyRepository->get(); @@ -96,11 +96,22 @@ class TransactionController extends BaseController $budgets = $budgetRepository->getAsSelectList(); $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 = [ 'date' => $journal->date->format('Y-m-d'), 'category' => '', - 'budget_id' => 0 + 'budget_id' => 0, + 'piggybank_id' => $piggyBankId ]; $category = $journal->categories()->first(); if (!is_null($category)) { @@ -130,7 +141,7 @@ class TransactionController extends BaseController return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with( 'what', $what - )->with('budgets', $budgets)->with('data', $data); + )->with('budgets', $budgets)->with('data', $data)->with('piggies',$piggies); } /** diff --git a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php index dd4adaff14..e7f4c29242 100644 --- a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php +++ b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php @@ -121,6 +121,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface $limit->repeat_freq = $data['repeat_freq']; if ($limit->validate()) { $limit->save(); + \Event::fire('limits.store',[$limit]); } } if ($budget->validate()) { diff --git a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php index 67ea1e334c..fbdf99fdbf 100644 --- a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php +++ b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php @@ -26,6 +26,22 @@ class EloquentLimitRepository implements LimitRepositoryInterface 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 * @@ -41,8 +57,8 @@ class EloquentLimitRepository implements LimitRepositoryInterface /** * @param \Budget $budget - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return mixed */ @@ -97,11 +113,11 @@ class EloquentLimitRepository implements LimitRepositoryInterface } // find existing: $count = \Limit:: - leftJoin('components', 'components.id', '=', 'limits.component_id')->where( - 'components.user_id', \Auth::user()->id - )->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where( - 'repeat_freq', $data['period'] - )->count(); + leftJoin('components', 'components.id', '=', 'limits.component_id')->where( + 'components.user_id', \Auth::user()->id + )->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where( + 'repeat_freq', $data['period'] + )->count(); if ($count > 0) { \Session::flash('error', 'There already is an entry for these parameters.'); diff --git a/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php b/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php index 83b315b4bb..dc8b03647a 100644 --- a/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php @@ -19,6 +19,13 @@ interface LimitRepositoryInterface */ public function store($data); + /** + * @param \Limit $limit + * @param $data + * @return mixed + */ + public function update(\Limit $limit, $data); + /** * @param \Budget $budget * @param Carbon $start diff --git a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php index 65a180bbed..dbf11e8ed8 100644 --- a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php +++ b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php @@ -121,8 +121,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito $fromTransaction->description = null; $fromTransaction->amount = $amountFrom; 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(); @@ -151,9 +150,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito { return \Auth::user()->transactionjournals()->with( ['transactions' => function ($q) { - return $q->orderBy('amount', 'ASC'); - }, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account', - 'transactions.account.accounttype'] + return $q->orderBy('amount', 'ASC'); + }, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account', + 'transactions.account.accounttype'] ) ->where('id', $journalId)->first(); } @@ -168,7 +167,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito /** * @param \Account $account - * @param Carbon $date + * @param Carbon $date * * @return mixed */ @@ -197,9 +196,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito /** * @param \Account $account - * @param int $count - * @param Carbon $start - * @param Carbon $end + * @param int $count + * @param Carbon $start + * @param Carbon $end * * @return mixed */ @@ -236,8 +235,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito $query = \Auth::user()->transactionjournals()->with( [ 'transactions' => function ($q) { - return $q->orderBy('amount', 'ASC'); - }, + return $q->orderBy('amount', 'ASC'); + }, 'transactions.account', 'transactions.account.accounttype', 'transactioncurrency', @@ -300,8 +299,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito // find budget: $budget = isset($data['budget_id']) ? $budRepository->find(intval($data['budget_id'])) : null; -// -// // find amount & description: + // find amount & description: $description = trim($data['description']); $amount = floatval($data['amount']); $date = new Carbon($data['date']); @@ -323,25 +321,19 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito $piggyBank = $piggyRepository->find(intval($data['piggybank_id'])); if ($piggyBank) { - if ($toAccount->id == $piggyBank->account_id) { - - // find the transaction connected to the $toAccount: - /** @var \Transaction $transaction */ - $transaction - = $transactionJournal->transactions()->where('account_id', $toAccount->id)->first(); - // connect the piggy to it: - $transaction->piggybank()->associate($piggyBank); - $transaction->save(); - \Event::fire( - 'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction] - ); - } else { - \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.' - ); + // one of the two transactions may be connected to this piggy bank. + $connected = false; + foreach ($transactionJournal->transactions()->get() as $transaction) { + if ($transaction->account_id == $piggyBank->account_id) { + $connected = true; + $transaction->piggybank()->associate($piggyBank); + $transaction->save(); + \Event::fire('piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]); + break; + } + } + if ($connected === false) { + \Session::flash('warning', 'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from any of the accounts in this transfer'); } } } @@ -405,7 +397,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito /** @var \Transaction $transaction */ foreach ($transactions as $transaction) { if (!is_null($transaction->piggybank()->first())) { - $transaction->piggybank()->detach($transaction->piggybank()->first()->first()); + $transaction->piggybank_id = null; $transaction->save(); } } @@ -415,6 +407,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito $transactions[1]->amount = $amount; // switch on type to properly change things: + $fireEvent = false; switch ($journal->transactiontype->type) { case 'Withdrawal': // means transaction[0] is the users account. @@ -454,24 +447,22 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito /** @var \Piggybank $piggyBank */ $piggyBank = $piggyRepository->find(intval($data['piggybank_id'])); - if ($piggyBank) { - if ($toAccount->id == $piggyBank->account_id) { + // loop transactions and re-attach the piggy bank: - // find the transaction connected to the $toAccount: - /** @var \Transaction $transaction */ - $transaction - = $journal->transactions()->where('account_id', $toAccount->id)->first(); - // connect the piggy to it: - $transaction->piggybank()->associate($piggyBank); - $transaction->save(); - \Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank, $journal]); - } else { - \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.' - ); + if ($piggyBank) { + + $connected = false; + foreach ($journal->transactions()->get() as $transaction) { + if ($transaction->account_id == $piggyBank->account_id) { + $connected = true; + $transaction->piggybank()->associate($piggyBank); + $transaction->save(); + $fireEvent = true; + break; + } + } + if ($connected === false) { + \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()) { $journal->save(); } + if($fireEvent) { + \Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank]); + } return $journal; diff --git a/app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php b/app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php new file mode 100644 index 0000000000..57ec40eef9 --- /dev/null +++ b/app/lib/Firefly/Trigger/Budgets/EloquentBudgetTrigger.php @@ -0,0 +1,62 @@ +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; + + } +} diff --git a/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php b/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php index 3f9b771a4a..620b114ba1 100644 --- a/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php +++ b/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php @@ -2,7 +2,6 @@ namespace Firefly\Trigger\Limits; -use Carbon\Carbon; use Illuminate\Events\Dispatcher; /** @@ -18,71 +17,102 @@ class EloquentLimitTrigger */ 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 updateLimitRepetitions() + public function destroy(\Limit $limit) { - if (!\Auth::check() || is_null(\Auth::user())) { - \Log::debug('No user for updateLimitRepetitions.'); - return; - } + return true; - // 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; +// } +// +// } +// } +// } +// +// } +// } } diff --git a/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php index c4914f2e31..96108f9bdc 100644 --- a/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php +++ b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php @@ -13,12 +13,13 @@ use Illuminate\Events\Dispatcher; class EloquentPiggybankTrigger { /** - * @param \Piggybank $piggyBank + * @param \Piggybank $piggyBank * @param \TransactionJournal $journal */ public function createRelatedTransfer( \Piggybank $piggyBank, \TransactionJournal $journal, \Transaction $transaction - ) { + ) + { $repetition = $piggyBank->repetitionForDate($journal->date); if (!is_null($repetition)) { // 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]); } + // // /** // * diff --git a/app/models/Limit.php b/app/models/Limit.php index 6c6de35acf..856a736acb 100644 --- a/app/models/Limit.php +++ b/app/models/Limit.php @@ -74,6 +74,9 @@ class Limit extends Ardent } /** + * Create a new repetition for this limit, starting on + * the given date. + * * @param Carbon $start */ public function createRepetition(Carbon $start) diff --git a/app/tests/controllers/BudgetControllerTest.php b/app/tests/controllers/BudgetControllerTest.php index 217e464ab7..d3432943e3 100644 --- a/app/tests/controllers/BudgetControllerTest.php +++ b/app/tests/controllers/BudgetControllerTest.php @@ -65,7 +65,7 @@ class BudgetControllerTest extends TestCase Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('check')->andReturn(true); $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->action('POST', 'BudgetController@destroy', $budget->id); @@ -81,7 +81,7 @@ class BudgetControllerTest extends TestCase Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('check')->andReturn(true); $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->action('POST', 'BudgetController@destroy', [$budget->id, 'from' => 'date']); @@ -97,7 +97,7 @@ class BudgetControllerTest extends TestCase Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('check')->andReturn(true); $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); @@ -233,7 +233,7 @@ class BudgetControllerTest extends TestCase Auth::shouldReceive('check')->andReturn(true); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $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->assertRedirectedToRoute('budgets.index.budget'); @@ -248,7 +248,7 @@ class BudgetControllerTest extends TestCase Auth::shouldReceive('check')->andReturn(true); $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($budget->user_id); $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->action('POST', 'BudgetController@update', [$budget->id, 'from' => 'date']); diff --git a/app/tests/controllers/LimitControllerTest.php b/app/tests/controllers/LimitControllerTest.php index 93cdb5b239..8853a7ae06 100644 --- a/app/tests/controllers/LimitControllerTest.php +++ b/app/tests/controllers/LimitControllerTest.php @@ -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('email')->andReturn('some@email'); + $this->_limits->shouldReceive('update')->once()->andReturn($limit); + $this->action( '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('email')->andReturn('some@email'); + unset($limit->amount); + $this->_limits->shouldReceive('update')->once()->andReturn($limit); + $this->action( '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('email')->andReturn('some@email'); + $this->_limits->shouldReceive('update')->once()->andReturn($limit); + $this->action( 'POST', 'LimitController@update', diff --git a/app/tests/controllers/PiggybankControllerTest.php b/app/tests/controllers/PiggybankControllerTest.php index 8f31084b10..d220c48144 100644 --- a/app/tests/controllers/PiggybankControllerTest.php +++ b/app/tests/controllers/PiggybankControllerTest.php @@ -92,7 +92,7 @@ class PiggybankControllerTest extends TestCase ); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); $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); @@ -114,6 +114,7 @@ class PiggybankControllerTest extends TestCase ); $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); + $this->action('GET', 'PiggybankController@edit', $piggyBank->id); $this->assertResponseOk(); } @@ -153,6 +154,7 @@ class PiggybankControllerTest extends TestCase $three->account()->associate($aTwo); $this->_piggybanks->shouldReceive('get')->andReturn([$one, $two, $three]); $this->_piggybanks->shouldReceive('countRepeating')->andReturn(0); + $this->_piggybanks->shouldReceive('leftOnAccount')->andReturn(0); $this->_piggybanks->shouldReceive('countNonrepeating')->andReturn(0); Event::shouldReceive('fire')->with('piggybanks.change'); @@ -169,7 +171,7 @@ class PiggybankControllerTest extends TestCase $input = [ $piggyBank->id, 'amount' => 10.0, - 'what' => 'add' + 'what' => 'add' ]; // for binding @@ -179,7 +181,7 @@ class PiggybankControllerTest extends TestCase $piggyBank->account()->first()->user_id ); $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('leftOnAccount')->once()->andReturn(200); @@ -199,7 +201,7 @@ class PiggybankControllerTest extends TestCase $input = [ $piggyBank->id, 'amount' => 10.0, - 'what' => 'add' + 'what' => 'add' ]; // for binding @@ -207,7 +209,7 @@ class PiggybankControllerTest extends TestCase Auth::shouldReceive('check')->andReturn(true); $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($piggyBank->account()->first()->user_id); $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); @@ -228,7 +230,7 @@ class PiggybankControllerTest extends TestCase $input = [ $piggyBank->id, 'amount' => 10.0, - 'what' => 'yomoma' + 'what' => 'yomoma' ]; // for binding @@ -267,7 +269,7 @@ class PiggybankControllerTest extends TestCase $input = [ $rep->piggybank()->first()->id, 'amount' => 10.0, - 'what' => 'remove' + 'what' => 'remove' ]; $this->action('POST', 'PiggybankController@modMoney', $input); @@ -297,7 +299,7 @@ class PiggybankControllerTest extends TestCase $input = [ $rep->piggybank()->first()->id, 'amount' => 10.0, - 'what' => 'remove' + 'what' => 'remove' ]; $this->action('POST', 'PiggybankController@modMoney', $input); @@ -391,7 +393,7 @@ class PiggybankControllerTest extends TestCase $piggyBank->account()->first()->user_id ); $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->assertResponseStatus(302); diff --git a/app/tests/controllers/TransactionControllerTest.php b/app/tests/controllers/TransactionControllerTest.php index 40d7fcb138..6ac6777c8b 100644 --- a/app/tests/controllers/TransactionControllerTest.php +++ b/app/tests/controllers/TransactionControllerTest.php @@ -123,6 +123,8 @@ class TransactionControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn($journal->user_id); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + $this->_piggies->shouldReceive('get')->once()->andReturn([]); + $this->action('GET', 'TransactionController@edit', $journal->id); $this->assertResponseOk(); } @@ -152,6 +154,8 @@ class TransactionControllerTest extends TestCase $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); + $this->_piggies->shouldReceive('get')->once()->andReturn([]); + // for binding Auth::shouldReceive('user')->andReturn($this->_user); Auth::shouldReceive('check')->andReturn(true); @@ -186,6 +190,7 @@ class TransactionControllerTest extends TestCase $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); $this->_budgets->shouldReceive('getAsSelectList')->andReturn([]); + $this->_piggies->shouldReceive('get')->once()->andReturn([]); // for binding Auth::shouldReceive('user')->andReturn($this->_user); diff --git a/app/views/transactions/edit.blade.php b/app/views/transactions/edit.blade.php index 9fc90c54a4..a8a8058fc5 100644 --- a/app/views/transactions/edit.blade.php +++ b/app/views/transactions/edit.blade.php @@ -138,6 +138,36 @@ + + @if($what == 'transfer' && count($piggies) > 0) +
+ +
+ + @if($errors->has('piggybank_id')) +

{{$errors->first('piggybank_id')}}

+ @else + + You can directly add the amount you're transferring + to one of your piggy banks, provided they are related to the account your + transferring to. + + @endif +
+
+ @endif + diff --git a/bootstrap/start.php b/bootstrap/start.php index daa830666f..4a79de419b 100644 --- a/bootstrap/start.php +++ b/bootstrap/start.php @@ -93,6 +93,7 @@ Event::subscribe('Firefly\Helper\Form\FormTrigger'); // do something with events: Event::subscribe('Firefly\Trigger\Limits\EloquentLimitTrigger'); Event::subscribe('Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger'); +Event::subscribe('Firefly\Trigger\Budgets\EloquentBudgetTrigger'); //App::booted( // function () {