Use note object instead of field #888

This commit is contained in:
James Cole 2017-10-03 10:30:56 +02:00
parent 4af0c18020
commit d1a6b37eb3
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
7 changed files with 59 additions and 11 deletions

View File

@ -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();

View File

@ -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()
{

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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">

View File

@ -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 %}