Add notes to category #4002

This commit is contained in:
James Cole
2020-10-28 06:32:37 +01:00
parent 3aa835a985
commit ca3d836c83
7 changed files with 92 additions and 9 deletions

View File

@@ -51,13 +51,13 @@ class CategoryFactory
* @param int|null $categoryId * @param int|null $categoryId
* @param null|string $categoryName * @param null|string $categoryName
* *
* @throws FireflyException
* @return Category|null * @return Category|null
* @throws FireflyException
*/ */
public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
{ {
$categoryId = (int) $categoryId; $categoryId = (int)$categoryId;
$categoryName = (string) $categoryName; $categoryName = (string)$categoryName;
Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName)); Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName));

View File

@@ -42,7 +42,8 @@ class CategoryFormRequest extends FormRequest
public function getCategoryData(): array public function getCategoryData(): array
{ {
return [ return [
'name' => $this->string('name'), 'name' => $this->string('name'),
'notes' => $this->nlString('notes'),
]; ];
} }

View File

@@ -70,7 +70,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read int|null $attachments_count * @property-read int|null $attachments_count
* @property-read int|null $transaction_journals_count * @property-read int|null $transaction_journals_count
* @property-read int|null $transactions_count * @property-read int|null $transactions_count
* @property bool $encrypted
*/ */
class Category extends Model class Category extends Model
{ {
@@ -135,6 +134,15 @@ class Category extends Model
return $this->morphMany(Attachment::class, 'attachable'); 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 * @codeCoverageIgnore
* @return BelongsToMany * @return BelongsToMany

View File

@@ -28,6 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\CategoryFactory; use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use FireflyIII\Models\Category; use FireflyIII\Models\Category;
use FireflyIII\Models\Note;
use FireflyIII\Models\RecurrenceTransactionMeta; use FireflyIII\Models\RecurrenceTransactionMeta;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
use FireflyIII\Services\Internal\Destroy\CategoryDestroyService; use FireflyIII\Services\Internal\Destroy\CategoryDestroyService;
@@ -202,7 +203,7 @@ class CategoryRepository implements CategoryRepositoryInterface
/** /**
* @param string $query * @param string $query
* @param int $limit * @param int $limit
* *
* @return Collection * @return Collection
*/ */
@@ -241,10 +242,28 @@ class CategoryRepository implements CategoryRepositoryInterface
if (null === $category) { if (null === $category) {
throw new FireflyException(sprintf('400003: Could not store new category with name "%s"', $data['name'])); 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; return $category;
} }
/**
* @param Category $category
*/
public function removeNotes(Category $category): void
{
$category->notes()->delete();
}
/** /**
* @param Category $category * @param Category $category
* @param array $data * @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;
}
} }

View File

@@ -33,6 +33,27 @@ use Illuminate\Support\Collection;
*/ */
interface CategoryRepositoryInterface 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. * Delete all categories.
*/ */

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Category; use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Category\OperationsRepositoryInterface; use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -33,8 +34,8 @@ use Illuminate\Support\Collection;
*/ */
class CategoryTransformer extends AbstractTransformer class CategoryTransformer extends AbstractTransformer
{ {
/** @var OperationsRepositoryInterface */ private OperationsRepositoryInterface $opsRepository;
private $opsRepository; private CategoryRepositoryInterface $repository;
/** /**
* CategoryTransformer constructor. * CategoryTransformer constructor.
@@ -44,6 +45,7 @@ class CategoryTransformer extends AbstractTransformer
public function __construct() public function __construct()
{ {
$this->opsRepository = app(OperationsRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class);
$this->repository = app(CategoryRepositoryInterface::class);
} }
/** /**
@@ -56,6 +58,7 @@ class CategoryTransformer extends AbstractTransformer
public function transform(Category $category): array public function transform(Category $category): array
{ {
$this->opsRepository->setUser($category->user); $this->opsRepository->setUser($category->user);
$this->repository->setUser($category->user);
$spent = []; $spent = [];
$earned = []; $earned = [];
@@ -65,11 +68,14 @@ class CategoryTransformer extends AbstractTransformer
$earned = $this->beautify($this->opsRepository->sumIncome($start, $end, null, new Collection([$category]))); $earned = $this->beautify($this->opsRepository->sumIncome($start, $end, null, new Collection([$category])));
$spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$category]))); $spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$category])));
} }
$notes = $this->repository->getNoteText($category);
return [ return [
'id' => (int)$category->id, 'id' => (int)$category->id,
'created_at' => $category->created_at->toAtomString(), 'created_at' => $category->created_at->toAtomString(),
'updated_at' => $category->updated_at->toAtomString(), 'updated_at' => $category->updated_at->toAtomString(),
'name' => $category->name, 'name' => $category->name,
'notes' => $notes,
'spent' => $spent, 'spent' => $spent,
'earned' => $earned, 'earned' => $earned,
'links' => [ 'links' => [
@@ -90,7 +96,7 @@ class CategoryTransformer extends AbstractTransformer
{ {
$return = []; $return = [];
foreach ($array as $data) { 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; $return[] = $data;
} }

View File

@@ -26,6 +26,7 @@
<h3 class="box-title">{{ 'optionalFields'|_ }}</h3> <h3 class="box-title">{{ 'optionalFields'|_ }}</h3>
</div> </div>
<div class="box-body"> <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}) }) }} {{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }}
</div> </div>
</div> </div>