Skip to content

Firefly III: a personal finances manager

Use note object instead of field #888

James Coled1a6b37eb328
diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php
index 09d7bfd05a..dabe765386 100644
--- a/app/Import/Storage/ImportStorage.php
+++ b/app/Import/Storage/ImportStorage.php
@@ -16,6 +16,7 @@ use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Object\ImportJournal;
use FireflyIII\Models\ImportJob;
+use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
use Log;
@@ -178,9 +179,14 @@ class ImportStorage
$this->storeBill($journal, $importJournal->bill->getBill());
$this->storeMeta($journal, $importJournal->metaDates);
- $journal->setMeta('notes', $importJournal->notes);
$this->storeTags($importJournal->tags, $journal);
+ // set notes for journal:
+ $dbNote = new Note();
+ $dbNote->noteable()->associate($journal);
+ $dbNote->text = trim($importJournal->notes);
+ $dbNote->save();
+
// set journal completed:
$journal->completed = true;
$journal->save();
diff --git a/app/Models/Note.php b/app/Models/Note.php
index 96423130a1..7bbd896145 100644
--- a/app/Models/Note.php
+++ b/app/Models/Note.php
@@ -48,7 +48,8 @@ class Note extends Model
}
/**
- * Get all of the owning noteable models. Currently only piggy bank
+ * Get all of the owning noteable models. Currently piggy bank and
+ * transaction journal
*/
public function noteable()
{
diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php
index 275ae35af2..45a9a03145 100644
--- a/app/Models/TransactionJournal.php
+++ b/app/Models/TransactionJournal.php
@@ -260,6 +260,14 @@ class TransactionJournal extends Model
return $this->transactionType->isWithdrawal();
}
+ /**
+ * Get all of the notes.
+ */
+ public function notes()
+ {
+ return $this->morphMany('FireflyIII\Models\Note', 'noteable');
+ }
+
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
diff --git a/app/Repositories/Journal/CreateJournalsTrait.php b/app/Repositories/Journal/CreateJournalsTrait.php
index 13edb07aa7..b63ee5d859 100644
--- a/app/Repositories/Journal/CreateJournalsTrait.php
+++ b/app/Repositories/Journal/CreateJournalsTrait.php
@@ -14,6 +14,7 @@ namespace FireflyIII\Repositories\Journal;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
+use FireflyIII\Models\Note;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
@@ -195,4 +196,31 @@ trait CreateJournalsTrait
}
+
+ /**
+ * @param TransactionJournal $journal
+ * @param string $note
+ *
+ * @return bool
+ */
+ protected function updateNote(TransactionJournal $journal, string $note): bool
+ {
+ if (strlen($note) === 0) {
+ $dbNote = $journal->notes()->first();
+ if (!is_null($dbNote)) {
+ $dbNote->delete();
+ }
+
+ return true;
+ }
+ $dbNote = $journal->notes()->first();
+ if (is_null($dbNote)) {
+ $dbNote = new Note();
+ $dbNote->noteable()->associate($journal);
+ }
+ $dbNote->text = trim($note);
+ $dbNote->save();
+
+ return true;
+ }
}
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 84e964735c..43bad26479 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -34,7 +34,7 @@ class JournalRepository implements JournalRepositoryInterface
/** @var User */
private $user;
/** @var array */
- private $validMetaFields = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes'];
+ private $validMetaFields = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference'];
/**
* @param TransactionJournal $journal
@@ -235,6 +235,9 @@ class JournalRepository implements JournalRepositoryInterface
$this->saveTags($journal, $data['tags']);
}
+ // update note:
+ $this->updateNote($journal, $data['notes']);
+
foreach ($data as $key => $value) {
if (in_array($key, $this->validMetaFields)) {
$journal->setMeta($key, $value);
@@ -287,6 +290,9 @@ class JournalRepository implements JournalRepositoryInterface
$this->updateTags($journal, $data['tags']);
}
+ // update note:
+ $this->updateNote($journal, $data['notes']);
+
// update meta fields:
$result = $journal->save();
if ($result) {
@@ -324,6 +330,9 @@ class JournalRepository implements JournalRepositoryInterface
$journal->categories()->detach();
$journal->budgets()->detach();
+ // update note:
+ $this->updateNote($journal, $data['notes']);
+
// update meta fields:
$result = $journal->save();
if ($result) {
diff --git a/resources/views/reports/partials/journals-audit.twig b/resources/views/reports/partials/journals-audit.twig
index d5da12c69f..b17b1b97af 100644
--- a/resources/views/reports/partials/journals-audit.twig
+++ b/resources/views/reports/partials/journals-audit.twig
@@ -134,11 +134,9 @@
</td>
<td class="hide-notes">
-
- {% if transaction.transactionJournal.hasMeta('notes') %}
- {{ transaction.transactionJournal.getMeta('notes')|nl2br }}
+ {% if transaction.transactionJournal.notes.count == 1 %}
+ {{ transaction.transactionJournal.notes.first.text }}
{% endif %}
-
</td>
<td class="hide-create_date">
diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig
index 3416cc4284..01f6e8e57f 100644
--- a/resources/views/transactions/show.twig
+++ b/resources/views/transactions/show.twig
@@ -235,13 +235,11 @@
<td>{{ journal.getMeta('internal_reference') }}</td>
</tr>
{% endif %}
-
- {% if journal.hasMeta('notes') and journal.getMeta('notes') != "" %}
+ {% if journal.notes.count == 1 %}
<tr>
<td>{{ trans('list.notes') }}</td>
- <td>{{ journal.getMeta('notes')|nl2br }}</td>
+ <td>{{ journal.notes.first.markdown|raw }}</td>
</tr>
-
{% endif %}
{% if journal.bill %}