Can also show and update notes.

This commit is contained in:
James Cole
2019-08-27 05:57:58 +02:00
parent f2c2d4e126
commit e209766ad0
2 changed files with 44 additions and 3 deletions

View File

@@ -23,11 +23,15 @@ declare(strict_types=1);
namespace FireflyIII\Services\Internal\Update;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Note;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Services\Internal\Support\RecurringTransactionTrait;
use FireflyIII\Services\Internal\Support\TransactionTypeTrait;
use FireflyIII\User;
use Log;
/**
* Class RecurrenceUpdateService
@@ -44,6 +48,8 @@ class RecurrenceUpdateService
/**
* Updates a recurrence.
*
* TODO if the user updates the type, accounts must be validated (again).
*
* @param Recurrence $recurrence
* @param array $data
*
@@ -63,10 +69,14 @@ class RecurrenceUpdateService
$recurrence->description = $data['recurrence']['description'] ?? $recurrence->description;
$recurrence->first_date = $data['recurrence']['first_date'] ?? $recurrence->first_date;
$recurrence->repeat_until = $data['recurrence']['repeat_until'] ?? $recurrence->repeat_until;
$recurrence->repetitions = $data['recurrence']['repetitions'] ?? $recurrence->repetitions;
$recurrence->repetitions = $data['recurrence']['nr_of_repetitions'] ?? $recurrence->repetitions;
$recurrence->apply_rules = $data['recurrence']['apply_rules'] ?? $recurrence->apply_rules;
$recurrence->active = $data['recurrence']['active'] ?? $recurrence->active;
// if nr_of_repetitions is set, then drop the "repeat_until" field.
if (0 !== $recurrence->repetitions) {
$recurrence->repeat_until = null;
}
if (isset($data['recurrence']['repetition_end'])) {
if (in_array($data['recurrence']['repetition_end'], ['forever', 'until_date'])) {
@@ -81,6 +91,10 @@ class RecurrenceUpdateService
// update all meta data:
//$this->updateMetaData($recurrence, $data);
if (null !== $data['recurrence']['notes']) {
$this->setNoteText($recurrence, $data['recurrence']['notes']);
}
// update all repetitions
if (null !== $data['repetitions']) {
$this->deleteRepetitions($recurrence);
@@ -95,4 +109,31 @@ class RecurrenceUpdateService
return $recurrence;
}
/**
* @param Recurrence $recurrence
* @param string $text
*/
private function setNoteText(Recurrence $recurrence, string $text): void
{
$dbNote = $recurrence->notes()->first();
if ('' !== $text) {
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($recurrence);
}
$dbNote->text = trim($text);
$dbNote->save();
return;
}
if (null !== $dbNote && '' === $text) {
try {
$dbNote->delete();
} catch (Exception $e) {
Log::debug(sprintf('Could not delete note: %s', $e->getMessage()));
}
}
}
}