From 706d853b04c7602572de4e7d971fe29b86662529 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 31 Jul 2014 22:01:52 +0200 Subject: [PATCH] Work on piggy banks [skip ci] --- app/assets/javascripts/firefly/piggybanks.js | 26 ++++ app/assets/javascripts/piggybanks.js | 13 ++ app/controllers/PiggybankController.php | 53 +++++++- .../Piggybank/EloquentPiggybankRepository.php | 58 +++++++++ .../PiggybankRepositoryInterface.php | 18 +++ .../Storage/StorageServiceProvider.php | 5 + app/models/Piggybank.php | 21 ++++ app/models/User.php | 4 + app/routes.php | 10 ++ app/views/partials/menu/home.blade.php | 2 +- app/views/piggybanks/create.blade.php | 116 ++++++++++++++++++ app/views/piggybanks/index.blade.php | 70 +++++++++++ 12 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/firefly/piggybanks.js create mode 100644 app/assets/javascripts/piggybanks.js create mode 100644 app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php create mode 100644 app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php create mode 100644 app/models/Piggybank.php create mode 100644 app/views/piggybanks/create.blade.php create mode 100644 app/views/piggybanks/index.blade.php diff --git a/app/assets/javascripts/firefly/piggybanks.js b/app/assets/javascripts/firefly/piggybanks.js new file mode 100644 index 0000000000..1c818e314e --- /dev/null +++ b/app/assets/javascripts/firefly/piggybanks.js @@ -0,0 +1,26 @@ +$(function () { + + $('input[type="range"]').on('input',inputAmount); + $('input[type="range"]').on('change',changeAmount); + +}); + +function inputAmount(e) { + var target = $(e.target); + var piggyBankId = target.attr('name').substring(6); + var value = target.val(); + + valueFormatted = '€ ' + (Math.round(value * 100) / 100).toFixed(2);; + + console.log(piggyBankId + ': ' + value); + + var valueId = 'piggy_'+piggyBankId+'_amount'; + $('#' + valueId).text(valueFormatted); + + + return true; +} + +function changeAmount(e) { + console.log('Change!'); +} \ No newline at end of file diff --git a/app/assets/javascripts/piggybanks.js b/app/assets/javascripts/piggybanks.js new file mode 100644 index 0000000000..da4638fd51 --- /dev/null +++ b/app/assets/javascripts/piggybanks.js @@ -0,0 +1,13 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear in whatever order it +// gets included (e.g. say you have require_tree . then the code will appear after all the directories +// but before any files alphabetically greater than 'application.js' +// +// The available directives right now are require, require_directory, and require_tree +// +//= require firefly/piggybanks diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index 9b8a8632e7..cecd1ba0dd 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -1,7 +1,30 @@ _repository = $repository; + $this->_accounts = $accounts; + View::share('menu', 'home'); + + } + public function create() { + $accounts = $this->_accounts->getActiveDefaultAsSelectList(); + + return View::make('piggybanks.create')->with('accounts', $accounts); } public function delete() @@ -18,6 +41,25 @@ class PiggybankController extends BaseController { public function index() { + $count = $this->_repository->count(); + $piggybanks = $this->_repository->get(); + $accounts = []; + // get accounts: + foreach($piggybanks as $piggyBank) { + $account = $piggyBank->account; + $id = $account->id; + if(!isset($accounts[$id])) { + $account->balance = $account->balance(); + $account->left = $account->balance; + } else { + $account->left -= $piggyBank->amount; + } + $accounts[$id] = $account; + } + + + + return View::make('piggybanks.index')->with('count', $count)->with('accounts',$accounts)->with('piggybanks',$piggybanks); } public function show() @@ -26,6 +68,15 @@ class PiggybankController extends BaseController { public function store() { + $piggyBank = $this->_repository->store(Input::all()); + if(!$piggyBank->id) { + Session::flash('error','Could not save piggy bank: ' . $piggyBank->errors()->first()); + return Redirect::route('piggybanks.create')->withInput(); + } else { + Session::flash('success','New piggy bank created!'); + return Redirect::route('piggybanks.index'); + } + } public function update() diff --git a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php new file mode 100644 index 0000000000..e6afbb9899 --- /dev/null +++ b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php @@ -0,0 +1,58 @@ +where( + 'accounts.user_id', \Auth::user()->id + )->count(); + } + + public function find($piggyBankId) + { + return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where( + 'accounts.user_id', \Auth::user()->id + )->where('piggybanks.id', $piggyBankId)->first('piggybanks.*'); + } + + public function get() + { + return \Piggybank::with('account')->leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where( + 'accounts.user_id', \Auth::user()->id + )->get(['piggybanks.*']); + } + + public function store($data) + { + var_dump($data); + /** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */ + $accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface'); + $account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null; + + + $piggyBank = new \Piggybank; + $piggyBank->account()->associate($account); + $piggyBank->targetdate + = isset($data['targetdate']) && strlen($data['targetdate']) > 0 ? $data['targetdate'] : null; + $piggyBank->name = isset($data['name']) ? $data['name'] : null; + $piggyBank->amount = 0; + $piggyBank->target = floatval($data['target']); + $piggyBank->order = 1; + if ($piggyBank->validate()) { + $piggyBank->save(); + } + + return $piggyBank; + } +} \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php new file mode 100644 index 0000000000..97d21f0dc5 --- /dev/null +++ b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php @@ -0,0 +1,18 @@ +app->bind( + 'Firefly\Storage\Piggybank\PiggybankRepositoryInterface', + 'Firefly\Storage\Piggybank\EloquentPiggybankRepository' + ); + diff --git a/app/models/Piggybank.php b/app/models/Piggybank.php new file mode 100644 index 0000000000..f1b4291d7c --- /dev/null +++ b/app/models/Piggybank.php @@ -0,0 +1,21 @@ + 'required|between:1,255', + 'account_id' => 'required|exists:accounts,id', + 'targetdate' => 'date', + 'amount' => 'required|min:0', + 'target' => 'required|min:1', + 'order' => 'required:min:1', + ]; + + public function account() + { + return $this->belongsTo('Account'); + } + +} \ No newline at end of file diff --git a/app/models/User.php b/app/models/User.php index 51b6854ddf..72ea913144 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -72,6 +72,10 @@ class User extends Ardent implements UserInterface, RemindableInterface return $this->hasMany('Account'); } + public function piggybanks() { + return $this->hasMany('Piggybank'); + } + public function preferences() { return $this->hasMany('Preference'); diff --git a/app/routes.php b/app/routes.php index 103c63eaca..6280d9bff5 100644 --- a/app/routes.php +++ b/app/routes.php @@ -64,6 +64,10 @@ Route::group(['before' => 'auth'], function () { Route::get('/categories/edit/{category}',['uses' => 'CategoryController@edit','as' => 'categories.edit']); Route::get('/categories/delete/{category}',['uses' => 'CategoryController@delete','as' => 'categories.delete']); + // piggy bank controller + Route::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']); + Route::get('/piggybanks/create', ['uses' => 'PiggybankController@create','as' => 'piggybanks.create']); + // preferences controller Route::get('/preferences', ['uses' => 'PreferencesController@index', 'as' => 'preferences']); @@ -90,6 +94,7 @@ Route::group(['before' => 'auth'], function () { Route::get('/budgets/edit/{budget}',['uses' => 'BudgetController@edit', 'as' => 'budgets.edit']); Route::get('/budgets/delete/{budget}',['uses' => 'BudgetController@delete', 'as' => 'budgets.delete']); + // limit controller: Route::get('/budgets/limits/create/{budget?}',['uses' => 'LimitController@create','as' => 'budgets.limits.create']); Route::get('/budgets/limits/delete/{limit}',['uses' => 'LimitController@delete','as' => 'budgets.limits.delete']); @@ -130,6 +135,11 @@ Route::group(['before' => 'csrf|auth'], function () { // migration controller Route::post('/migrate', ['uses' => 'MigrationController@postIndex']); + // piggy bank controller + Route::post('/piggybanks/store',['uses' => 'PiggybankController@store','as' => 'piggybanks.store']); + + + // preferences controller Route::post('/preferences', ['uses' => 'PreferencesController@postIndex']); diff --git a/app/views/partials/menu/home.blade.php b/app/views/partials/menu/home.blade.php index 3adf9d0a93..20c320b05b 100644 --- a/app/views/partials/menu/home.blade.php +++ b/app/views/partials/menu/home.blade.php @@ -29,7 +29,7 @@ $r = Route::current()->getName();
  • Transactions
  • -
  • Piggy banks
  • +
  • Piggy banks
  • diff --git a/app/views/piggybanks/create.blade.php b/app/views/piggybanks/create.blade.php new file mode 100644 index 0000000000..01c22ae740 --- /dev/null +++ b/app/views/piggybanks/create.blade.php @@ -0,0 +1,116 @@ +@extends('layouts.default') +@section('content') +
    +
    +

    Firefly + Create a new piggy bank +

    +

    Create piggy banks to make saving money easier

    +

    + Saving money is hard. Piggy banks allow you to group money + from an account together. If you also set a target (and a target date) you + can save towards your goals. +

    +
    +
    + +{{Form::open(['class' => 'form-horizontal','url' => route('piggybanks.store')])}} + +
    +
    +

    Mandatory fields

    + +
    + +
    + + @if($errors->has('name')) +

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

    + @else + For example: new bike, new camera + @endif +
    +
    + +
    + {{ Form::label('target', 'Target amount', ['class' => 'col-sm-4 control-label'])}} +
    +
    + + {{Form::input('number','target', Input::old('target'), ['step' => 'any', 'min' => '1', 'class' => 'form-control'])}} +
    + + @if($errors->has('target')) +

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

    + @else + How much money do you need to save? + @endif +
    +
    + +
    + +
    + {{Form::select('account_id',$accounts,Input::old('account_id') ?: Input::get('account'),['class' => 'form-control'])}} + @if($errors->has('account_id')) +

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

    + @else + Indicate on which account you've got your savings. + @endif +
    +
    + +
    +
    +

    Optional fields

    + +
    + {{ Form::label('targetdate', 'Target date', ['class' => 'col-sm-4 control-label'])}} +
    + {{ Form::input('date','targetdate', Input::old('targetdate') ?: '', ['class' + => 'form-control']) }} + @if($errors->has('targetdate')) +

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

    + @else + + If you want to, set a target date. This will inform you how much money you should save to + get to the target amount. + + @endif +
    +
    + + +
    +
    + +
    +
    + + +
    + +
    +
    + +
    +
    +
    + +
    +
    + +
    +
    +
    +
    + +{{Form::close()}} + + +@stop diff --git a/app/views/piggybanks/index.blade.php b/app/views/piggybanks/index.blade.php new file mode 100644 index 0000000000..cb6fef49ff --- /dev/null +++ b/app/views/piggybanks/index.blade.php @@ -0,0 +1,70 @@ +@extends('layouts.default') +@section('content') +
    +
    +

    Firefly + Piggy banks +

    +

    Set targets and save money

    +

    + Saving money is hard. Piggy banks allow you to group money + from an account together. If you also set a target (and a target date) you + can save towards your goals. +

    + @if($count == 0) +

    + Create new piggy bank +

    + @endif +
    +
    + +@if($count > 0) +
    +
    +

    Accounts used for piggy banks

    + + + + + + + @foreach($accounts as $account) + + + + + + @endforeach +
    AccountCurrent balanceLeft for (other) piggy banks
    {{{$account->name}}}{{mf($account->balance())}}{{mf($account->left)}}
    +
    +
    + +
    +
    +

    Piggy banks

    + + @foreach($piggybanks as $piggybank) +

    {{{$piggybank->name}}} {{mf($piggybank->target)}}

    + + + + + + +
    {{mf($piggybank->amount,false)}}Y
    + @endforeach + + +

    + Create new piggy bank +

    + +
    +
    + +@endif +@stop +@section('scripts') + +@stop \ No newline at end of file