From fdba0addb64e7190c2ec0b53dc80409782de560e Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 21 Aug 2014 15:15:39 +0200 Subject: [PATCH] Build some extra features for the recurring transaction controller. --- app/controllers/RecurringController.php | 15 +++++++-- ...EloquentRecurringTransactionRepository.php | 31 +++++++++++++++++++ ...ecurringTransactionRepositoryInterface.php | 7 +++++ app/models/RecurringTransaction.php | 22 +------------ .../controllers/RecurringControllerTest.php | 2 ++ app/views/recurring/create.blade.php | 2 +- app/views/recurring/index.blade.php | 2 +- 7 files changed, 56 insertions(+), 25 deletions(-) diff --git a/app/controllers/RecurringController.php b/app/controllers/RecurringController.php index 46be9a5b6a..bf85aaf2bb 100644 --- a/app/controllers/RecurringController.php +++ b/app/controllers/RecurringController.php @@ -82,8 +82,10 @@ class RecurringController extends BaseController /** * */ - public function show() + public function show(RecurringTransaction $recurringTransaction) { + return View::make('recurring.show')->with('recurring',$recurringTransaction); + } /** @@ -113,5 +115,14 @@ class RecurringController extends BaseController */ public function update(RecurringTransaction $recurringTransaction) { + /** @var \RecurringTransaction $recurringTransaction */ + $recurringTransaction = $this->_repository->update($recurringTransaction, Input::all()); + if($recurringTransaction->errors()->count() == 0) { + Session::flash('success', 'The recurring transaction has been updated.'); + return Redirect::route('recurring.index'); + } else { + Session::flash('error', 'Could not update the recurring transaction: ' . $recurringTransaction->errors()->first()); + return Redirect::route('recurring.edit',$recurringTransaction->id)->withInput()->withErrors($recurringTransaction->errors()); + } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php index 123ff2bc75..b8d840c501 100644 --- a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php +++ b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php @@ -66,4 +66,35 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo return $recurringTransaction; } + /** + * @param \RecurringTransaction $recurringTransaction + * @param $data + * @return mixed|void + */ + public function update(\RecurringTransaction $recurringTransaction, $data) { + $recurringTransaction->name = $data['name']; + $recurringTransaction->match = join(' ', explode(',', $data['match'])); + $recurringTransaction->amount_max = floatval($data['amount_max']); + $recurringTransaction->amount_min = floatval($data['amount_min']); + + // both amounts zero: + if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) { + $recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.'); + + return $recurringTransaction; + } + $recurringTransaction->date = new Carbon($data['date']); + $recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0; + $recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0; + $recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0; + $recurringTransaction->repeat_freq = $data['repeat_freq']; + + if ($recurringTransaction->validate()) { + $recurringTransaction->save(); + } + + return $recurringTransaction; + + } + } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php b/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php index a4f33014b6..dc296f28c4 100644 --- a/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php +++ b/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php @@ -30,5 +30,12 @@ interface RecurringTransactionRepositoryInterface */ public function destroy(\RecurringTransaction $recurringTransaction); + /** + * @param \RecurringTransaction $recurringTransaction + * @param $data + * @return mixed + */ + public function update(\RecurringTransaction $recurringTransaction, $data); + } \ No newline at end of file diff --git a/app/models/RecurringTransaction.php b/app/models/RecurringTransaction.php index 7b0ccde6ed..365cc797b7 100644 --- a/app/models/RecurringTransaction.php +++ b/app/models/RecurringTransaction.php @@ -50,26 +50,6 @@ class RecurringTransaction extends Ardent 'skip' => 'required|between:0,31', ]; - /** - * @return array - */ - public static function factory() - { - $date = new Carbon; - - return [ - 'user_id' => 'factory|User', - 'name' => 'string', - 'match' => 'string', - 'amount_max' => 100, - 'amount_min' => 50, - 'date' => $date, - 'active' => 1, - 'automatch' => 1, - 'repeat_freq' => 'monthly', - 'skip' => 0, - ]; - } /** * @return array @@ -99,7 +79,7 @@ class RecurringTransaction extends Ardent $start->addMonths($skip); break; case 'quarterly': - $start->addMonths($skip); + $start->addMonths($skip * 3); break; case 'half-year': $start->addMonths($skip * 6); diff --git a/app/tests/controllers/RecurringControllerTest.php b/app/tests/controllers/RecurringControllerTest.php index f35d918c7b..2c210e6672 100644 --- a/app/tests/controllers/RecurringControllerTest.php +++ b/app/tests/controllers/RecurringControllerTest.php @@ -158,6 +158,8 @@ class RecurringControllerTest extends TestCase $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($recurringTransaction->user_id); $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + $this->_repository->shouldReceive('update')->andReturn($recurringTransaction); + $this->action('POST', 'RecurringController@update', $recurringTransaction->id); $this->assertResponseOk(); diff --git a/app/views/recurring/create.blade.php b/app/views/recurring/create.blade.php index 4b143f4ec8..4cfd860cda 100644 --- a/app/views/recurring/create.blade.php +++ b/app/views/recurring/create.blade.php @@ -38,7 +38,7 @@

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

@else For example: rent, [company name]. All matches need to - be present for the recurring transaction to be recognized. This field is not case-sensitive. + be present for the recurring transaction to be recognized. This field is not case-sensitive. Press enter after every match @endif diff --git a/app/views/recurring/index.blade.php b/app/views/recurring/index.blade.php index 179d44f1e8..2cf5be03b5 100644 --- a/app/views/recurring/index.blade.php +++ b/app/views/recurring/index.blade.php @@ -5,7 +5,7 @@

Firefly Recurring transactions

-

Use recurring transactions to track repeated expenses

+

Use recurring transactions to track repeated withdrawals

We all have bills to pay. Firefly can help you organize those bills into recurring transactions, which are exactly what the name suggests. Firefly can match new (and existing) transactions to such a recurring transaction and help you organize these expenses into manageable groups. The front page of Firefly will show you which recurring