From e209766ad02e442a148422a92113032a746cffc2 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 27 Aug 2019 05:57:58 +0200 Subject: [PATCH] Can also show and update notes. --- .../Update/RecurrenceUpdateService.php | 43 ++++++++++++++++++- app/Transformers/RecurrenceTransformer.php | 4 +- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/Services/Internal/Update/RecurrenceUpdateService.php b/app/Services/Internal/Update/RecurrenceUpdateService.php index 3bf2decfb5..063d5272e2 100644 --- a/app/Services/Internal/Update/RecurrenceUpdateService.php +++ b/app/Services/Internal/Update/RecurrenceUpdateService.php @@ -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())); + } + } + + } } diff --git a/app/Transformers/RecurrenceTransformer.php b/app/Transformers/RecurrenceTransformer.php index 94e14a0d49..f713963eca 100644 --- a/app/Transformers/RecurrenceTransformer.php +++ b/app/Transformers/RecurrenceTransformer.php @@ -91,7 +91,7 @@ class RecurrenceTransformer extends AbstractTransformer $this->budgetRepos->setUser($recurrence->user); $shortType = (string)config(sprintf('firefly.transactionTypesToShort.%s', $recurrence->transactionType->type)); - + $notes = $this->repository->getNoteText($recurrence); // basic data. $return = [ 'id' => (int)$recurrence->id, @@ -106,7 +106,7 @@ class RecurrenceTransformer extends AbstractTransformer 'apply_rules' => $recurrence->apply_rules, 'active' => $recurrence->active, 'nr_of_repetitions' => $recurrence->repetitions, - 'notes' => $this->repository->getNoteText($recurrence), + 'notes' => '' === $notes ? null : $notes, 'repetitions' => $this->getRepetitions($recurrence), 'transactions' => $this->getTransactions($recurrence), 'links' => [