Skip to content

Firefly III: a personal finances manager

Piggy bank supports notes (#350)

James Colee4d249e73cd9
diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php
index 59e79a74c7..ae9f639afb 100644
--- a/app/Http/Controllers/PiggyBankController.php
+++ b/app/Http/Controllers/PiggyBankController.php
@@ -168,6 +168,7 @@ class PiggyBankController extends Controller
'account_id' => $piggyBank->account_id,
'targetamount' => $piggyBank->targetamount,
'targetdate' => $targetDate,
+ 'note' => $piggyBank->notes()->first()->text,
];
Session::flash('preFilled', $preFilled);
Session::flash('gaEventCategory', 'piggy-banks');
@@ -346,10 +347,11 @@ class PiggyBankController extends Controller
*/
public function show(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
{
+ $note = $piggyBank->notes()->first();
$events = $repository->getEvents($piggyBank);
$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),
'order' => $repository->getMaxOrder() + 1,
'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null,
+ 'note' => $request->get('note'),
];
$piggyBank = $repository->store($piggyBankData);
@@ -402,6 +405,7 @@ class PiggyBankController extends Controller
'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,
+ 'note' => $request->get('note'),
];
$piggyBank = $repository->update($piggyBank, $piggyBankData);
diff --git a/app/Models/Note.php b/app/Models/Note.php
new file mode 100644
index 0000000000..db1d2b4603
--- /dev/null
+++ b/app/Models/Note.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Note.php
+ * Copyright (C) 2016 [email protected]
+ *
+ * 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();
+ }
+
+}
\ No newline at end of file
diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php
index f2d2ae4e50..3d5e5da150 100644
--- a/app/Models/PiggyBank.php
+++ b/app/Models/PiggyBank.php
@@ -56,6 +56,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @mixin \Eloquent
* @property boolean $active
* @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
{
@@ -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
*/
diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php
index 9eca01a95a..2560c440de 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepository.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepository.php
@@ -15,6 +15,7 @@ namespace FireflyIII\Repositories\PiggyBank;
use Amount;
use Carbon\Carbon;
+use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\User;
@@ -176,6 +177,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
{
$piggyBank = PiggyBank::create($data);
+ $this->updateNote($piggyBank, $data['note']);
+
return $piggyBank;
}
@@ -196,6 +199,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
$piggyBank->save();
+ $this->updateNote($piggyBank, $data['note']);
+
// if the piggy bank is now smaller than the current relevant rep,
// remove money from the rep.
$repetition = $piggyBank->currentRelevantRep();
@@ -210,4 +215,31 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
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;
+ }
}
diff --git a/database/migrations/2016_10_22_075804_changes_for_v410.php b/database/migrations/2016_10_22_075804_changes_for_v410.php
new file mode 100644
index 0000000000..bfd8c71085
--- /dev/null
+++ b/database/migrations/2016_10_22_075804_changes_for_v410.php
@@ -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();
+ }
+ );
+ }
+}
diff --git a/resources/views/piggy-banks/create.twig b/resources/views/piggy-banks/create.twig
index 492ff18ae8..f6d055b46e 100644
--- a/resources/views/piggy-banks/create.twig
+++ b/resources/views/piggy-banks/create.twig
@@ -31,6 +31,7 @@
</div>
<div class="box-body">
{{ ExpandedForm.date('targetdate') }}
+ {{ ExpandedForm.textarea('note') }}
</div>
</div>
diff --git a/resources/views/piggy-banks/edit.twig b/resources/views/piggy-banks/edit.twig
index 82bd5aeda7..c12e629504 100644
--- a/resources/views/piggy-banks/edit.twig
+++ b/resources/views/piggy-banks/edit.twig
@@ -33,6 +33,7 @@
</div>
<div class="box-body">
{{ ExpandedForm.date('targetdate') }}
+ {{ ExpandedForm.textarea('note') }}
</div>
</div>
diff --git a/resources/views/piggy-banks/show.twig b/resources/views/piggy-banks/show.twig
index a46f0920c2..0b625d7680 100644
--- a/resources/views/piggy-banks/show.twig
+++ b/resources/views/piggy-banks/show.twig
@@ -35,7 +35,7 @@
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
- <td>{{ 'account'|_ }}</td>
+ <td style="width:40%;">{{ 'account'|_ }}</td>
<td><a href="{{ route('accounts.show', piggyBank.account_id) }}">{{ piggyBank.account.name }}</a></td>
</tr>
<tr>
@@ -73,6 +73,20 @@
</table>
</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-header with-border">
<h3 class="box-title">{{ 'table'|_ }}</h3>