mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 04:03:26 +00:00
Trying to fix piggies [skip ci]
This commit is contained in:
@@ -1,26 +1,82 @@
|
|||||||
$(function () {
|
$(function () {
|
||||||
|
|
||||||
$('input[type="range"]').on('input',inputAmount);
|
$('input[type="range"]').on('input', inputAmount);
|
||||||
$('input[type="range"]').on('change',changeAmount);
|
$('input[type="range"]').on('change', changeAmount);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update some fields to reflect drag changes.
|
||||||
|
* @param e
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
function inputAmount(e) {
|
function inputAmount(e) {
|
||||||
var target = $(e.target);
|
// var target = $(e.target);
|
||||||
var piggyBankId = target.attr('name').substring(6);
|
// var piggyBankId = target.attr('name').substring(6);
|
||||||
var value = target.val();
|
// var accountId = target.data('account');
|
||||||
|
// var value = target.val();
|
||||||
valueFormatted = '€ ' + (Math.round(value * 100) / 100).toFixed(2);;
|
//
|
||||||
|
// // update all accounts and return false if we're going overboard.
|
||||||
console.log(piggyBankId + ': ' + value);
|
// var updateResult = updateAccounts(accountId);
|
||||||
|
// if(!updateResult) {
|
||||||
var valueId = 'piggy_'+piggyBankId+'_amount';
|
// return false;
|
||||||
$('#' + valueId).text(valueFormatted);
|
// }
|
||||||
|
//
|
||||||
|
// // new value for amount in piggy bank, formatted:
|
||||||
|
// valueFormatted = '€ ' + (Math.round(value * 100) / 100).toFixed(2);
|
||||||
|
// var valueId = 'piggy_' + piggyBankId + '_amount';
|
||||||
|
// $('#' + valueId).text(valueFormatted);
|
||||||
|
//
|
||||||
|
// // new percentage for amount in piggy bank, formatted.
|
||||||
|
// var pctId = 'piggy_' + piggyBankId + '_pct';
|
||||||
|
// percentage = Math.round((value / parseFloat(target.attr('max'))) * 100) + '%'; //Math.round((value / parseFloat(target.attr('total'))) * 100) + '%';
|
||||||
|
// $('#' + pctId).text(percentage);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeAmount(e) {
|
function changeAmount(e) {
|
||||||
console.log('Change!');
|
var target = $(e.target);
|
||||||
|
var piggyBankId = target.attr('name').substring(6);
|
||||||
|
var accountId = target.data('account');
|
||||||
|
var value = target.val();
|
||||||
|
|
||||||
|
$.post('piggybanks/updateAmount/' + piggyBankId, {amount: value});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateAccounts(id) {
|
||||||
|
//
|
||||||
|
// var spent = 0;
|
||||||
|
// $.each($('input[type="range"]'), function (i, v) {
|
||||||
|
// var current = $(v);
|
||||||
|
// var accountId = parseInt(current.data('account'));
|
||||||
|
// if (accountId == id) {
|
||||||
|
// spent += parseFloat(current.val());
|
||||||
|
// }
|
||||||
|
//// var value = parseFloat(current.val());
|
||||||
|
//// var accountId = parseInt(current.data('account'));
|
||||||
|
////
|
||||||
|
//// // only when we're working on this account we update "spent"
|
||||||
|
//// if(accountId == id) {
|
||||||
|
//// spent = spent[accountId] == undefined ? value : spent[accountId] + value;
|
||||||
|
//// //var leftNow = accountLeft[accountId] - value;
|
||||||
|
//// }
|
||||||
|
// });
|
||||||
|
// console.log('Spent for account ' + id + ': ' + spent);
|
||||||
|
// var left = accountLeft[id] - spent;
|
||||||
|
// var leftFormatted = '€ ' + (Math.round((left) * 100) / 100).toFixed(2);
|
||||||
|
// var entryId = 'account_' + id + '_left';
|
||||||
|
// $('#' + entryId).text(leftFormatted);
|
||||||
|
// if(left < 0) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// return true;
|
||||||
|
////
|
||||||
|
//// // now we update the amount in the list of accounts:
|
||||||
|
//// var left = accountLeft[id] - spent;
|
||||||
|
//// var leftFormatted =
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -38,6 +38,9 @@ class PiggybankController extends BaseController
|
|||||||
public function edit()
|
public function edit()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
public function updateAmount(Piggybank $piggybank) {
|
||||||
|
$this->_repository->updateAmount($piggybank,Input::get('amount'));
|
||||||
|
}
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
@@ -45,21 +48,27 @@ class PiggybankController extends BaseController
|
|||||||
$piggybanks = $this->_repository->get();
|
$piggybanks = $this->_repository->get();
|
||||||
$accounts = [];
|
$accounts = [];
|
||||||
// get accounts:
|
// get accounts:
|
||||||
foreach($piggybanks as $piggyBank) {
|
foreach ($piggybanks as $piggyBank) {
|
||||||
$account = $piggyBank->account;
|
$account = $piggyBank->account;
|
||||||
|
$piggyBank->pct = round(($piggyBank->amount / $piggyBank->target) * 100, 2) . '%';
|
||||||
$id = $account->id;
|
$id = $account->id;
|
||||||
if(!isset($accounts[$id])) {
|
if (!isset($accounts[$id])) {
|
||||||
$account->balance = $account->balance();
|
$account->balance = $account->balance();
|
||||||
$account->left = $account->balance;
|
$account->left = $account->balance - $piggyBank->amount;
|
||||||
} else {
|
} else {
|
||||||
|
echo $account->left.'-';
|
||||||
|
echo '('.$piggyBank->amount.')';
|
||||||
$account->left -= $piggyBank->amount;
|
$account->left -= $piggyBank->amount;
|
||||||
|
echo $account->left;
|
||||||
|
|
||||||
}
|
}
|
||||||
$accounts[$id] = $account;
|
$accounts[$id] = $account;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return View::make('piggybanks.index')->with('count', $count)->with('accounts', $accounts)->with(
|
||||||
return View::make('piggybanks.index')->with('count', $count)->with('accounts',$accounts)->with('piggybanks',$piggybanks);
|
'piggybanks', $piggybanks
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show()
|
public function show()
|
||||||
@@ -69,11 +78,13 @@ class PiggybankController extends BaseController
|
|||||||
public function store()
|
public function store()
|
||||||
{
|
{
|
||||||
$piggyBank = $this->_repository->store(Input::all());
|
$piggyBank = $this->_repository->store(Input::all());
|
||||||
if(!$piggyBank->id) {
|
if (!$piggyBank->id) {
|
||||||
Session::flash('error','Could not save piggy bank: ' . $piggyBank->errors()->first());
|
Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first());
|
||||||
|
|
||||||
return Redirect::route('piggybanks.create')->withInput();
|
return Redirect::route('piggybanks.create')->withInput();
|
||||||
} else {
|
} else {
|
||||||
Session::flash('success','New piggy bank created!');
|
Session::flash('success', 'New piggy bank created!');
|
||||||
|
|
||||||
return Redirect::route('piggybanks.index');
|
return Redirect::route('piggybanks.index');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,6 +32,13 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
|||||||
'accounts.user_id', \Auth::user()->id
|
'accounts.user_id', \Auth::user()->id
|
||||||
)->get(['piggybanks.*']);
|
)->get(['piggybanks.*']);
|
||||||
}
|
}
|
||||||
|
public function updateAmount(\Piggybank $piggyBank, $amount) {
|
||||||
|
$piggyBank->amount = floatval($amount);
|
||||||
|
if($piggyBank->validate()) {
|
||||||
|
$piggyBank->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function store($data)
|
public function store($data)
|
||||||
{
|
{
|
||||||
|
@@ -15,4 +15,6 @@ interface PiggybankRepositoryInterface
|
|||||||
public function count();
|
public function count();
|
||||||
public function store($data);
|
public function store($data);
|
||||||
public function get();
|
public function get();
|
||||||
|
|
||||||
|
public function updateAmount(\Piggybank $piggyBank, $amount);
|
||||||
}
|
}
|
@@ -42,6 +42,17 @@ Route::bind('limit', function($value, $route)
|
|||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::bind('piggybank', function($value, $route)
|
||||||
|
{
|
||||||
|
if(Auth::check()) {
|
||||||
|
return Piggybank::
|
||||||
|
where('piggybanks.id', $value)->
|
||||||
|
leftJoin('accounts','accounts.id','=','piggybanks.account_id')->
|
||||||
|
where('accounts.user_id',Auth::user()->id)->first(['piggybanks.*']);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// protected routes:
|
// protected routes:
|
||||||
Route::group(['before' => 'auth'], function () {
|
Route::group(['before' => 'auth'], function () {
|
||||||
@@ -67,6 +78,7 @@ Route::group(['before' => 'auth'], function () {
|
|||||||
// piggy bank controller
|
// piggy bank controller
|
||||||
Route::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']);
|
Route::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']);
|
||||||
Route::get('/piggybanks/create', ['uses' => 'PiggybankController@create','as' => 'piggybanks.create']);
|
Route::get('/piggybanks/create', ['uses' => 'PiggybankController@create','as' => 'piggybanks.create']);
|
||||||
|
Route::post('/piggybanks/updateAmount/{piggybank}',['uses' => 'PiggybankController@updateAmount','as' => 'piggybanks.updateAmount']);
|
||||||
|
|
||||||
|
|
||||||
// preferences controller
|
// preferences controller
|
||||||
@@ -140,6 +152,7 @@ Route::group(['before' => 'csrf|auth'], function () {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// preferences controller
|
// preferences controller
|
||||||
Route::post('/preferences', ['uses' => 'PreferencesController@postIndex']);
|
Route::post('/preferences', ['uses' => 'PreferencesController@postIndex']);
|
||||||
|
|
||||||
|
@@ -32,8 +32,8 @@
|
|||||||
@foreach($accounts as $account)
|
@foreach($accounts as $account)
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{{$account->name}}}</td>
|
<td>{{{$account->name}}}</td>
|
||||||
<td>{{mf($account->balance())}}</td>
|
<td>{{mf($account->balance)}}</td>
|
||||||
<td>{{mf($account->left)}}</td>
|
<td id="account_{{$account->id}}_left">{{mf($account->left)}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
@endforeach
|
@endforeach
|
||||||
</table>
|
</table>
|
||||||
@@ -49,8 +49,8 @@
|
|||||||
<table class="table table-bordered">
|
<table class="table table-bordered">
|
||||||
<tr>
|
<tr>
|
||||||
<td style="width:10%;"><span id="piggy_{{$piggybank->id}}_amount">{{mf($piggybank->amount,false)}}</span></td>
|
<td style="width:10%;"><span id="piggy_{{$piggybank->id}}_amount">{{mf($piggybank->amount,false)}}</span></td>
|
||||||
<td><input type="range" name="piggy_{{$piggybank->id}}" min="1" max="{{$piggybank->target}}" step="any" value="{{$piggybank->amount}}" /></td>
|
<td><input type="range" data-account="{{$piggybank->account_id}}" name="piggy_{{$piggybank->id}}" min="0" max="{{$piggybank->target}}" step="any" value="{{$piggybank->amount}}" /></td>
|
||||||
<td>Y</td>
|
<td style="width: 10%;"><span id="piggy_{{$piggybank->id}}_pct">{{$piggybank->pct}}</span></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
@endforeach
|
@endforeach
|
||||||
@@ -66,5 +66,14 @@
|
|||||||
@endif
|
@endif
|
||||||
@stop
|
@stop
|
||||||
@section('scripts')
|
@section('scripts')
|
||||||
|
<script type="text/javascript">
|
||||||
|
var accountBalances = [];
|
||||||
|
var accountLeft = [];
|
||||||
|
@foreach($accounts as $account)
|
||||||
|
accountBalances[{{$account->id}}] = {{$account->balance()}};
|
||||||
|
accountLeft[{{$account->id}}] = {{$account->left}};
|
||||||
|
@endforeach
|
||||||
|
</script>
|
||||||
|
|
||||||
<?php echo javascript_include_tag('piggybanks'); ?>
|
<?php echo javascript_include_tag('piggybanks'); ?>
|
||||||
@stop
|
@stop
|
Reference in New Issue
Block a user