mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-06 12:45:30 +00:00
Piggy bank supports notes (#350)
This commit is contained in:
@@ -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);
|
||||
|
54
app/Models/Note.php
Normal file
54
app/Models/Note.php
Normal 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();
|
||||
}
|
||||
|
||||
}
|
@@ -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
|
||||
*/
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
41
database/migrations/2016_10_22_075804_changes_for_v410.php
Normal file
41
database/migrations/2016_10_22_075804_changes_for_v410.php
Normal 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();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@@ -31,6 +31,7 @@
|
||||
</div>
|
||||
<div class="box-body">
|
||||
{{ ExpandedForm.date('targetdate') }}
|
||||
{{ ExpandedForm.textarea('note') }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@@ -33,6 +33,7 @@
|
||||
</div>
|
||||
<div class="box-body">
|
||||
{{ ExpandedForm.date('targetdate') }}
|
||||
{{ ExpandedForm.textarea('note') }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@@ -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>
|
||||
|
Reference in New Issue
Block a user