Skip to content

Firefly III: a personal finances manager

Add notes to category #4002

James Coleca3d836c839a
diff --git a/app/Factory/CategoryFactory.php b/app/Factory/CategoryFactory.php
index ff95292f10..1be0a4c0c6 100644
--- a/app/Factory/CategoryFactory.php
+++ b/app/Factory/CategoryFactory.php
@@ -51,13 +51,13 @@ class CategoryFactory
* @param int|null $categoryId
* @param null|string $categoryName
*
- * @throws FireflyException
* @return Category|null
+ * @throws FireflyException
*/
public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
{
- $categoryId = (int) $categoryId;
- $categoryName = (string) $categoryName;
+ $categoryId = (int)$categoryId;
+ $categoryName = (string)$categoryName;
Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName));
diff --git a/app/Http/Requests/CategoryFormRequest.php b/app/Http/Requests/CategoryFormRequest.php
index de1b3d3f57..6b1a3bf7a5 100644
--- a/app/Http/Requests/CategoryFormRequest.php
+++ b/app/Http/Requests/CategoryFormRequest.php
@@ -42,7 +42,8 @@ class CategoryFormRequest extends FormRequest
public function getCategoryData(): array
{
return [
- 'name' => $this->string('name'),
+ 'name' => $this->string('name'),
+ 'notes' => $this->nlString('notes'),
];
}
diff --git a/app/Models/Category.php b/app/Models/Category.php
index 7d8ab4e35a..4fed25bc3d 100644
--- a/app/Models/Category.php
+++ b/app/Models/Category.php
@@ -70,7 +70,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read int|null $attachments_count
* @property-read int|null $transaction_journals_count
* @property-read int|null $transactions_count
- * @property bool $encrypted
*/
class Category extends Model
{
@@ -135,6 +134,15 @@ class Category extends Model
return $this->morphMany(Attachment::class, 'attachable');
}
+ /**
+ * @codeCoverageIgnore
+ * Get all of the category's notes.
+ */
+ public function notes(): MorphMany
+ {
+ return $this->morphMany(Note::class, 'noteable');
+ }
+
/**
* @codeCoverageIgnore
* @return BelongsToMany
diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php
index 3378e6cf10..14100abadc 100644
--- a/app/Repositories/Category/CategoryRepository.php
+++ b/app/Repositories/Category/CategoryRepository.php
@@ -28,6 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Category;
+use FireflyIII\Models\Note;
use FireflyIII\Models\RecurrenceTransactionMeta;
use FireflyIII\Models\RuleAction;
use FireflyIII\Services\Internal\Destroy\CategoryDestroyService;
@@ -202,7 +203,7 @@ class CategoryRepository implements CategoryRepositoryInterface
/**
* @param string $query
- * @param int $limit
+ * @param int $limit
*
* @return Collection
*/
@@ -241,10 +242,28 @@ class CategoryRepository implements CategoryRepositoryInterface
if (null === $category) {
throw new FireflyException(sprintf('400003: Could not store new category with name "%s"', $data['name']));
}
+
+ if (array_key_exists('notes', $data) && '' === $data['notes']) {
+ $this->removeNotes($category);
+ }
+ if (array_key_exists('notes', $data) && '' !== $data['notes']) {
+ $this->updateNotes($category, $data['notes']);
+ }
+
return $category;
}
+
+ /**
+ * @param Category $category
+ */
+ public function removeNotes(Category $category): void
+ {
+ $category->notes()->delete();
+ }
+
+
/**
* @param Category $category
* @param array $data
@@ -383,4 +402,31 @@ class CategoryRepository implements CategoryRepositoryInterface
}
);
}
+
+ /**
+ * @inheritDoc
+ */
+ public function updateNotes(Category $category, string $notes): void
+ {
+ $dbNote = $category->notes()->first();
+ if (null === $dbNote) {
+ $dbNote = new Note;
+ $dbNote->noteable()->associate($category);
+ }
+ $dbNote->text = trim($notes);
+ $dbNote->save();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getNoteText(Category $category): ?string
+ {
+ $dbNote = $category->notes()->first();
+ if (null === $dbNote) {
+ return null;
+ }
+
+ return $dbNote->text;
+ }
}
diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php
index 0b5e7fa9be..7feaa45baf 100644
--- a/app/Repositories/Category/CategoryRepositoryInterface.php
+++ b/app/Repositories/Category/CategoryRepositoryInterface.php
@@ -33,6 +33,27 @@ use Illuminate\Support\Collection;
*/
interface CategoryRepositoryInterface
{
+
+ /**
+ * Remove notes.
+ *
+ * @param Category $category
+ */
+ public function removeNotes(Category $category): void;
+
+ /**
+ * @param Category $category
+ * @param string $notes
+ */
+ public function updateNotes(Category $category, string $notes): void;
+
+ /**
+ * @param Category $category
+ *
+ * @return string|null
+ */
+ public function getNoteText(Category $category): ?string;
+
/**
* Delete all categories.
*/
diff --git a/app/Transformers/CategoryTransformer.php b/app/Transformers/CategoryTransformer.php
index 3c53267b03..381101856c 100644
--- a/app/Transformers/CategoryTransformer.php
+++ b/app/Transformers/CategoryTransformer.php
@@ -25,6 +25,7 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Category;
+use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use Illuminate\Support\Collection;
@@ -33,8 +34,8 @@ use Illuminate\Support\Collection;
*/
class CategoryTransformer extends AbstractTransformer
{
- /** @var OperationsRepositoryInterface */
- private $opsRepository;
+ private OperationsRepositoryInterface $opsRepository;
+ private CategoryRepositoryInterface $repository;
/**
* CategoryTransformer constructor.
@@ -44,6 +45,7 @@ class CategoryTransformer extends AbstractTransformer
public function __construct()
{
$this->opsRepository = app(OperationsRepositoryInterface::class);
+ $this->repository = app(CategoryRepositoryInterface::class);
}
/**
@@ -56,6 +58,7 @@ class CategoryTransformer extends AbstractTransformer
public function transform(Category $category): array
{
$this->opsRepository->setUser($category->user);
+ $this->repository->setUser($category->user);
$spent = [];
$earned = [];
@@ -65,11 +68,14 @@ class CategoryTransformer extends AbstractTransformer
$earned = $this->beautify($this->opsRepository->sumIncome($start, $end, null, new Collection([$category])));
$spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$category])));
}
+ $notes = $this->repository->getNoteText($category);
+
return [
'id' => (int)$category->id,
'created_at' => $category->created_at->toAtomString(),
'updated_at' => $category->updated_at->toAtomString(),
'name' => $category->name,
+ 'notes' => $notes,
'spent' => $spent,
'earned' => $earned,
'links' => [
@@ -90,7 +96,7 @@ class CategoryTransformer extends AbstractTransformer
{
$return = [];
foreach ($array as $data) {
- $data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', '');
+ $data['sum'] = number_format((float)$data['sum'], (int)$data['currency_decimal_places'], '.', '');
$return[] = $data;
}
diff --git a/resources/views/v1/categories/create.twig b/resources/views/v1/categories/create.twig
index c8b6fc8d49..a10ada5d18 100644
--- a/resources/views/v1/categories/create.twig
+++ b/resources/views/v1/categories/create.twig
@@ -26,6 +26,7 @@
<h3 class="box-title">{{ 'optionalFields'|_ }}</h3>
</div>
<div class="box-body">
+ {{ ExpandedForm.textarea('notes', null, {helpText: trans('firefly.field_supports_markdown')} ) }}
{{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }}
</div>
</div>