Add some forgotten code to the mobile piggy bank controller things.

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole 2016-08-24 20:06:45 +02:00
parent d4f8c41d80
commit 5aadb29905
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 157 additions and 69 deletions

View File

@ -48,26 +48,6 @@ class PiggyBankController extends Controller
View::share('mainTitleIcon', 'fa-sort-amount-asc');
}
/**
* Add money to piggy bank (for mobile devices)
*
* @param ARI $repository
* @param PiggyBank $piggyBank
*
* @return $this
*/
public function addMobile(ARI $repository, PiggyBank $piggyBank)
{
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
$leftOnAccount = $repository->leftOnAccount($piggyBank->account, $date);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount'));
}
/**
* Add money to piggy bank
*
@ -81,13 +61,33 @@ class PiggyBankController extends Controller
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
$leftOnAccount = $repository->leftOnAccount($piggyBank->account, $date);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount ?? '0';
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
return view('piggy-banks.add', compact('piggyBank', 'maxAmount'));
}
/**
* Add money to piggy bank (for mobile devices)
*
* @param ARI $repository
* @param PiggyBank $piggyBank
*
* @return $this
*/
public function addMobile(ARI $repository, PiggyBank $piggyBank)
{
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
$leftOnAccount = $repository->leftOnAccount($piggyBank->account, $date);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount?? '0';
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave);
return view('piggy-banks.add-mobile', compact('piggyBank', 'maxAmount'));
}
/**
* @param AccountCrudInterface $crud
*
@ -262,7 +262,8 @@ class PiggyBankController extends Controller
if ($amount <= $maxAmount) {
$repetition = $piggyBank->currentRelevantRep();
$repetition->currentamount = bcadd($repetition->currentamount, $amount);
$currentAmount = $repetition->currentamount ?? '0';
$repetition->currentamount = bcadd($currentAmount, $amount);
$repetition->save();
// create event
@ -326,6 +327,18 @@ class PiggyBankController extends Controller
return view('piggy-banks.remove', compact('piggyBank'));
}
/**
* Remove money from piggy bank (for mobile devices)
*
* @param PiggyBank $piggyBank
*
* @return $this
*/
public function removeMobile(PiggyBank $piggyBank)
{
return view('piggy-banks.remove-mobile', compact('piggyBank'));
}
/**
* @param PiggyBankRepositoryInterface $repository
* @param PiggyBank $piggyBank
@ -351,12 +364,12 @@ class PiggyBankController extends Controller
{
$piggyBankData = [
'name' => $request->get('name'),
'startdate' => new Carbon,
'account_id' => intval($request->get('account_id')),
'targetamount' => round($request->get('targetamount'), 2),
'order' => $repository->getMaxOrder() + 1,
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
'name' => $request->get('name'),
'startdate' => new Carbon,
'account_id' => intval($request->get('account_id')),
'targetamount' => round($request->get('targetamount'), 2),
'order' => $repository->getMaxOrder() + 1,
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
];
$piggyBank = $repository->store($piggyBankData);
@ -385,11 +398,11 @@ class PiggyBankController extends Controller
public function update(PiggyBankRepositoryInterface $repository, PiggyBankFormRequest $request, PiggyBank $piggyBank)
{
$piggyBankData = [
'name' => $request->get('name'),
'startdate' => is_null($piggyBank->startdate) ? $piggyBank->created_at : $piggyBank->startdate,
'account_id' => intval($request->get('account_id')),
'targetamount' => round($request->get('targetamount'), 2),
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
'name' => $request->get('name'),
'startdate' => is_null($piggyBank->startdate) ? $piggyBank->created_at : $piggyBank->startdate,
'account_id' => intval($request->get('account_id')),
'targetamount' => round($request->get('targetamount'), 2),
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
];
$piggyBank = $repository->update($piggyBank, $piggyBankData);

View File

@ -276,8 +276,8 @@ Route::group(
Route::get('/piggy-banks/remove/{piggyBank}', ['uses' => 'PiggyBankController@remove', 'as' => 'piggy-banks.removeMoney']);
Route::get('/piggy-banks/add-money/{piggyBank}', ['uses' => 'PiggyBankController@addMobile', 'as' => 'piggy-banks.add-money-mobile']);
Route::get('/piggy-banks/remove-money/{piggyBank}', ['uses' => 'PiggyBankController@removeMobile', 'as' => 'piggy-banks.remove-money-mobile']);
Route::post('/piggy-banks/add-money/{piggyBank}', ['uses' => 'PiggyBankController@postAddMobile', 'as' => 'piggy-banks.post-add-mobile']);
Route::post('/piggy-banks/remove-money/{piggyBank}', ['uses' => 'PiggyBankController@postRemoveMobile', 'as' => 'piggy-banks.post-remove-mobile']);
// Route::post('/piggy-banks/add-money/{piggyBank}', ['uses' => 'PiggyBankController@postAddMobile', 'as' => 'piggy-banks.post-add-mobile']);
// Route::post('/piggy-banks/remove-money/{piggyBank}', ['uses' => 'PiggyBankController@postRemoveMobile', 'as' => 'piggy-banks.post-remove-mobile']);
Route::get('/piggy-banks/create', ['uses' => 'PiggyBankController@create', 'as' => 'piggy-banks.create']);
Route::get('/piggy-banks/edit/{piggyBank}', ['uses' => 'PiggyBankController@edit', 'as' => 'piggy-banks.edit']);
Route::get('/piggy-banks/delete/{piggyBank}', ['uses' => 'PiggyBankController@delete', 'as' => 'piggy-banks.delete']);
@ -285,8 +285,10 @@ Route::group(
Route::post('/piggy-banks/store', ['uses' => 'PiggyBankController@store', 'as' => 'piggy-banks.store']);
Route::post('/piggy-banks/update/{piggyBank}', ['uses' => 'PiggyBankController@update', 'as' => 'piggy-banks.update']);
Route::post('/piggy-banks/destroy/{piggyBank}', ['uses' => 'PiggyBankController@destroy', 'as' => 'piggy-banks.destroy']);
Route::post('/piggy-banks/add/{piggyBank}', ['uses' => 'PiggyBankController@postAdd', 'as' => 'piggy-banks.add']);
Route::post('/piggy-banks/remove/{piggyBank}', ['uses' => 'PiggyBankController@postRemove', 'as' => 'piggy-banks.remove']);
Route::post('/piggy-banks/sort', ['uses' => 'PiggyBankController@order', 'as' => 'piggy-banks.order']);
/**

View File

@ -51,17 +51,32 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\PiggyBank whereDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\PiggyBank whereEncrypted($value)
* @mixin \Eloquent
* @property boolean $active
* @property boolean $active
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\PiggyBank whereActive($value)
*/
class PiggyBank extends Model
{
use SoftDeletes;
protected $dates = ['created_at', 'updated_at', 'deleted_at', 'startdate', 'targetdate'];
protected $fillable
= ['name', 'account_id', 'order', 'targetamount', 'startdate', 'targetdate'];
protected $hidden = ['targetamount_encrypted', 'encrypted'];
protected $dates = ['created_at', 'updated_at', 'deleted_at', 'startdate', 'targetdate'];
/**
* @param PiggyBank $value
*
* @return PiggyBank
*/
public static function routeBinder(PiggyBank $value)
{
if (Auth::check()) {
if ($value->account->user_id == Auth::user()->id) {
return $value;
}
}
throw new NotFoundHttpException;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
@ -76,13 +91,16 @@ class PiggyBank extends Model
*
* @returns PiggyBankRepetition
*/
public function currentRelevantRep()
public function currentRelevantRep(): PiggyBankRepetition
{
if (!is_null($this->currentRep)) {
return $this->currentRep;
}
// repeating piggy banks are no longer supported.
$rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']);
$rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']);
if (is_null($rep)) {
return new PiggyBankRepetition();
}
$this->currentRep = $rep;
return $rep;
@ -90,14 +108,6 @@ class PiggyBank extends Model
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function piggyBankRepetitions()
{
return $this->hasMany('FireflyIII\Models\PiggyBankRepetition');
}
/**
*
* @param $value
@ -122,6 +132,14 @@ class PiggyBank extends Model
return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function piggyBankRepetitions()
{
return $this->hasMany('FireflyIII\Models\PiggyBankRepetition');
}
/**
*
* @param $value
@ -139,19 +157,4 @@ class PiggyBank extends Model
{
$this->attributes['targetamount'] = strval(round($value, 2));
}
/**
* @param PiggyBank $value
*
* @return PiggyBank
*/
public static function routeBinder(PiggyBank $value)
{
if (Auth::check()) {
if ($value->account->user_id == Auth::user()->id) {
return $value;
}
}
throw new NotFoundHttpException;
}
}

View File

@ -438,7 +438,9 @@ class AccountRepository implements AccountRepositoryInterface
$balance = Steam::balanceIgnoreVirtual($account, $date);
/** @var PiggyBank $p */
foreach ($account->piggybanks()->get() as $p) {
$balance = bcsub($balance, $p->currentRelevantRep()->currentamount);
$currentAmount = $p->currentRelevantRep()->currentamount ?? '0';
$balance = bcsub($balance, $currentAmount);
}
return $balance;

View File

@ -118,7 +118,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
{
$set = $this->getPiggyBanks();
foreach ($set as $piggy) {
$piggy->name = $piggy->name . ' (' . Amount::format($piggy->currentRelevantRep()->currentamount, false) . ')';
$currentAmount = $piggy->currentRelevantRep()->currentamount ?? '0';
$piggy->name = $piggy->name . ' (' . Amount::format($currentAmount, false) . ')';
}
return $set;

View File

@ -304,8 +304,8 @@ class CreateMainTables extends Migration
$table->integer('account_id', false, true);
$table->string('name', 1024);
$table->decimal('targetamount', 10, 4);
$table->date('startdate');
$table->date('targetdate');
$table->date('startdate')->nullable();
$table->date('targetdate')->nullable();
$table->integer('order', false, true);
$table->boolean('active')->default(0);
$table->boolean('encrypted')->default(1);
@ -322,8 +322,8 @@ class CreateMainTables extends Migration
$table->increments('id');
$table->timestamps();
$table->integer('piggy_bank_id', false, true);
$table->date('startdate');
$table->date('targetdate');
$table->date('startdate')->nullable();
$table->date('targetdate')->nullable();
$table->decimal('currentamount', 10, 4);
$table->foreign('piggy_bank_id')->references('id')->on('piggy_banks')->onDelete('cascade');

View File

@ -5,7 +5,7 @@
{% endblock %}
{% block content %}
{{ Form.open({'class' : 'form-horizontal','id' : 'store','url' : route('piggy-banks.post-add-mobile', piggyBank.id)}) }}
{{ Form.open({'class' : 'form-horizontal','id' : 'store','url' : route('piggy-banks.add', piggyBank.id)}) }}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-primary">

View File

@ -0,0 +1,67 @@
{% extends "./layout/default.twig" %}
{% block breadcrumbs %}
{{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName) }}
{% endblock %}
{% block content %}
<!--
<div class="modal-dialog">
<div class="modal-content">
<form style="display: inline;" id="remove" action="{{ route('piggy-banks.remove', piggyBank.id) }}" method="POST">
<p>
</p>
<div class="input-group">
<div class="input-group-addon">{{ getCurrencySymbol()|raw }}</div>
<input step="any" class="form-control" id="amount" autocomplete="off" name="amount" max="{{ currentRelevantRepAmount(piggyBank)|round(2) }}"
type="number">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{{ 'close'|_ }}</button>
<button type="submit" class="btn btn-primary">{{ 'remove'|_ }}</button>
</div>
</form>
</div>
</div>
-->
<form id="remove" class="form-horizontal" action="{{ route('piggy-banks.remove', piggyBank.id) }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">{{ trans('firefly.remove_money_from_piggy_title', {name: piggyBank.name}) }}</h3>
</div>
<div class="box-body">
<p>
{{ 'max_amount_remove'|_ }}: {{ currentRelevantRepAmount(piggyBank)|formatAmount }}.
</p>
<div class="input-group">
<div class="input-group-addon">{{ getCurrencySymbol()|raw }}</div>
<input step="any" class="form-control" id="amount" autocomplete="off" name="amount" max="{{ currentRelevantRepAmount(piggyBank) }}" type="number"/>
</div>
<p>
&nbsp;
</p>
<button type="submit" class="btn btn-success pull-right">
{{ 'remove'|_ }}
</button>
</div>
</div>
</div>
</div>
{{ Form.close|raw }}
{% endblock %}