Piggy bank supports notes (#350)

This commit is contained in:
James Cole
2016-10-22 10:13:49 +02:00
parent 091f6e918b
commit e4d249e73c
8 changed files with 158 additions and 2 deletions

View File

@@ -168,6 +168,7 @@ class PiggyBankController extends Controller
'account_id' => $piggyBank->account_id, 'account_id' => $piggyBank->account_id,
'targetamount' => $piggyBank->targetamount, 'targetamount' => $piggyBank->targetamount,
'targetdate' => $targetDate, 'targetdate' => $targetDate,
'note' => $piggyBank->notes()->first()->text,
]; ];
Session::flash('preFilled', $preFilled); Session::flash('preFilled', $preFilled);
Session::flash('gaEventCategory', 'piggy-banks'); Session::flash('gaEventCategory', 'piggy-banks');
@@ -346,10 +347,11 @@ class PiggyBankController extends Controller
*/ */
public function show(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) public function show(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
{ {
$note = $piggyBank->notes()->first();
$events = $repository->getEvents($piggyBank); $events = $repository->getEvents($piggyBank);
$subTitle = e($piggyBank->name); $subTitle = e($piggyBank->name);
return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle')); return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle', 'note'));
} }
@@ -369,6 +371,7 @@ class PiggyBankController extends Controller
'targetamount' => round($request->get('targetamount'), 2), 'targetamount' => round($request->get('targetamount'), 2),
'order' => $repository->getMaxOrder() + 1, 'order' => $repository->getMaxOrder() + 1,
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null, 'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
'note' => $request->get('note'),
]; ];
$piggyBank = $repository->store($piggyBankData); $piggyBank = $repository->store($piggyBankData);
@@ -402,6 +405,7 @@ class PiggyBankController extends Controller
'account_id' => intval($request->get('account_id')), 'account_id' => intval($request->get('account_id')),
'targetamount' => round($request->get('targetamount'), 2), 'targetamount' => round($request->get('targetamount'), 2),
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null, 'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
'note' => $request->get('note'),
]; ];
$piggyBank = $repository->update($piggyBank, $piggyBankData); $piggyBank = $repository->update($piggyBank, $piggyBankData);

54
app/Models/Note.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
/**
* Note.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Models;
use Illuminate\Database\Eloquent\Model;
/**
* FireflyIII\Models\Note
*
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $deleted_at
* @property integer $noteable_id
* @property string $noteable_type
* @property string $title
* @property string $text
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $noteable
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereNoteableId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereNoteableType($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereTitle($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Note whereText($value)
* @mixin \Eloquent
*/
class Note extends Model
{
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
protected $fillable = ['title', 'text'];
/**
* Get all of the owning noteable models. Currently only piggy bank
*/
public function noteable()
{
return $this->morphTo();
}
}

View File

@@ -56,6 +56,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @mixin \Eloquent * @mixin \Eloquent
* @property boolean $active * @property boolean $active
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\PiggyBank whereActive($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\PiggyBank whereActive($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Note[] $notes
*/ */
class PiggyBank extends Model class PiggyBank extends Model
{ {
@@ -146,6 +147,14 @@ class PiggyBank extends Model
} }
/**
* Get all of the piggy bank's notes.
*/
public function notes()
{
return $this->morphMany('FireflyIII\Models\Note', 'noteable');
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
*/ */

View File

@@ -15,6 +15,7 @@ namespace FireflyIII\Repositories\PiggyBank;
use Amount; use Amount;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\User; use FireflyIII\User;
@@ -176,6 +177,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
{ {
$piggyBank = PiggyBank::create($data); $piggyBank = PiggyBank::create($data);
$this->updateNote($piggyBank, $data['note']);
return $piggyBank; return $piggyBank;
} }
@@ -196,6 +199,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
$piggyBank->save(); $piggyBank->save();
$this->updateNote($piggyBank, $data['note']);
// if the piggy bank is now smaller than the current relevant rep, // if the piggy bank is now smaller than the current relevant rep,
// remove money from the rep. // remove money from the rep.
$repetition = $piggyBank->currentRelevantRep(); $repetition = $piggyBank->currentRelevantRep();
@@ -210,4 +215,31 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $piggyBank; return $piggyBank;
} }
/**
* @param PiggyBank $piggyBank
* @param string $note
*
* @return bool
*/
private function updateNote(PiggyBank $piggyBank, string $note): bool
{
if (strlen($note) === 0) {
$dbNote = $piggyBank->notes()->first();
if (!is_null($dbNote)) {
$dbNote->delete();
}
return true;
}
$dbNote= $piggyBank->notes()->first();
if (is_null($dbNote)) {
$dbNote= new Note();
$dbNote->noteable()->associate($piggyBank);
}
$dbNote->text = trim($note);
$dbNote->save();
return true;
}
} }

View File

@@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Class ChangesForV410
*/
class ChangesForV410 extends Migration
{
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notes');
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(
'notes', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->integer('noteable_id', false, true);
$table->string('noteable_type');
$table->string('title')->nullable();
$table->text('text')->nullable();
}
);
}
}

View File

@@ -31,6 +31,7 @@
</div> </div>
<div class="box-body"> <div class="box-body">
{{ ExpandedForm.date('targetdate') }} {{ ExpandedForm.date('targetdate') }}
{{ ExpandedForm.textarea('note') }}
</div> </div>
</div> </div>

View File

@@ -33,6 +33,7 @@
</div> </div>
<div class="box-body"> <div class="box-body">
{{ ExpandedForm.date('targetdate') }} {{ ExpandedForm.date('targetdate') }}
{{ ExpandedForm.textarea('note') }}
</div> </div>
</div> </div>

View File

@@ -35,7 +35,7 @@
<div class="box-body table-responsive no-padding"> <div class="box-body table-responsive no-padding">
<table class="table table-hover"> <table class="table table-hover">
<tr> <tr>
<td>{{ 'account'|_ }}</td> <td style="width:40%;">{{ 'account'|_ }}</td>
<td><a href="{{ route('accounts.show', piggyBank.account_id) }}">{{ piggyBank.account.name }}</a></td> <td><a href="{{ route('accounts.show', piggyBank.account_id) }}">{{ piggyBank.account.name }}</a></td>
</tr> </tr>
<tr> <tr>
@@ -73,6 +73,20 @@
</table> </table>
</div> </div>
</div> </div>
{% if note %}
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ trans('form.notes') }}</h3>
</div>
<div class="box-body">
<p>
{{ note.text|nl2br }}
</p>
</div>
</div>
{% endif %}
<div class="box"> <div class="box">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ 'table'|_ }}</h3> <h3 class="box-title">{{ 'table'|_ }}</h3>