Skip to content

Firefly III: a personal finances manager

Fix #8405

James Cole6ecbddb6b689
diff --git a/app/Api/V1/Controllers/Models/Category/StoreController.php b/app/Api/V1/Controllers/Models/Category/StoreController.php
index b9639902a3..fa1fd5fbb4 100644
--- a/app/Api/V1/Controllers/Models/Category/StoreController.php
+++ b/app/Api/V1/Controllers/Models/Category/StoreController.php
@@ -70,15 +70,12 @@ final class StoreController extends Controller
/** @var CategoryTransformer $transformer */
$transformer = app(CategoryTransformer::class);
- $transformer->setParameters($this->parameters);
// enrich
/** @var User $admin */
$admin = auth()->user();
$enrichment = new CategoryEnrichment();
$enrichment->setUser($admin);
- $enrichment->setStart($this->parameters->get('start'));
- $enrichment->setEnd($this->parameters->get('end'));
$category = $enrichment->enrichSingle($category);
$resource = new Item($category, $transformer, 'categories');
diff --git a/app/Api/V1/Controllers/Models/Category/UpdateController.php b/app/Api/V1/Controllers/Models/Category/UpdateController.php
index 3f482b35ad..b09adba3d9 100644
--- a/app/Api/V1/Controllers/Models/Category/UpdateController.php
+++ b/app/Api/V1/Controllers/Models/Category/UpdateController.php
@@ -69,15 +69,12 @@ final class UpdateController extends Controller
/** @var CategoryTransformer $transformer */
$transformer = app(CategoryTransformer::class);
- $transformer->setParameters($this->parameters);
// enrich
/** @var User $admin */
$admin = auth()->user();
$enrichment = new CategoryEnrichment();
$enrichment->setUser($admin);
- $enrichment->setStart($this->parameters->get('start'));
- $enrichment->setEnd($this->parameters->get('end'));
$category = $enrichment->enrichSingle($category);
$resource = new Item($category, $transformer, 'categories');
diff --git a/app/Api/V1/Requests/Models/Category/StoreRequest.php b/app/Api/V1/Requests/Models/Category/StoreRequest.php
index 47d1991540..aad2c13fa6 100644
--- a/app/Api/V1/Requests/Models/Category/StoreRequest.php
+++ b/app/Api/V1/Requests/Models/Category/StoreRequest.php
@@ -43,7 +43,11 @@ class StoreRequest extends FormRequest
*/
public function getAll(): array
{
- return ['name' => $this->convertString('name'), 'notes' => $this->stringWithNewlines('notes')];
+ return [
+ 'name' => $this->convertString('name'),
+ 'notes' => $this->stringWithNewlines('notes'),
+ 'color' => $this->convertString('color'),
+ ];
}
/**
@@ -51,6 +55,10 @@ class StoreRequest extends FormRequest
*/
public function rules(): array
{
- return ['name' => ['required', 'min:1', 'max:100', 'uniqueObjectForUser:categories,name']];
+ return [
+ 'name' => ['required', 'min:1', 'max:100', 'uniqueObjectForUser:categories,name'],
+ 'notes' => ['nullable', 'string', 'max:65535'],
+ 'color' => ['nullable', 'string', 'max:6', 'regex:/^[0-9a-fA-F]{6}$/'],
+ ];
}
}
diff --git a/app/Api/V1/Requests/Models/Category/UpdateRequest.php b/app/Api/V1/Requests/Models/Category/UpdateRequest.php
index ed56278f5a..06c041c340 100644
--- a/app/Api/V1/Requests/Models/Category/UpdateRequest.php
+++ b/app/Api/V1/Requests/Models/Category/UpdateRequest.php
@@ -44,7 +44,11 @@ class UpdateRequest extends FormRequest
*/
public function getAll(): array
{
- $fields = ['name' => ['name', 'convertString'], 'notes' => ['notes', 'stringWithNewlines']];
+ $fields = [
+ 'name' => ['name', 'convertString'],
+ 'color' => ['color', 'convertString'],
+ 'notes' => ['notes', 'stringWithNewlines']
+ ];
return $this->getAllData($fields);
}
@@ -57,6 +61,10 @@ class UpdateRequest extends FormRequest
/** @var Category $category */
$category = $this->route()->parameter('category');
- return ['name' => sprintf('min:1|max:100|uniqueObjectForUser:categories,name,%d', $category->id)];
+ return [
+ 'name' => sprintf('min:1|max:100|uniqueObjectForUser:categories,name,%d', $category->id),
+ 'notes' => ['nullable', 'string', 'max:65535'],
+ 'color' => ['nullable', 'string', 'max:6', 'regex:/^[0-9a-fA-F]{6}$/'],
+ ];
}
}
diff --git a/app/Factory/CategoryFactory.php b/app/Factory/CategoryFactory.php
index 8ea94a3915..9c1b3414ed 100644
--- a/app/Factory/CategoryFactory.php
+++ b/app/Factory/CategoryFactory.php
@@ -45,7 +45,7 @@ class CategoryFactory
/**
* @throws FireflyException
*/
- public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
+ public function findOrCreate(?int $categoryId, ?string $categoryName, ?string $color): ?Category
{
$categoryId = (int) $categoryId;
$categoryName = (string) $categoryName;
@@ -71,7 +71,7 @@ class CategoryFactory
}
try {
- return Category::create(['user_id' => $this->user->id, 'user_group_id' => $this->user->user_group_id, 'name' => $categoryName]);
+ return Category::create(['user_id' => $this->user->id, 'user_group_id' => $this->user->user_group_id, 'name' => $categoryName, 'color' => $color]);
} catch (QueryException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php
index cc92999c07..44c6480e3f 100644
--- a/app/Repositories/Category/CategoryRepository.php
+++ b/app/Repositories/Category/CategoryRepository.php
@@ -321,8 +321,8 @@ class CategoryRepository implements CategoryRepositoryInterface, UserGroupInterf
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user);
-
- $category = $factory->findOrCreate(null, $data['name']);
+ $color= array_key_exists('color', $data) ? $data['color'] : null;
+ $category = $factory->findOrCreate(null, $data['name'], $color);
if (null === $category) {
throw new FireflyException(sprintf('400003: Could not store new category with name "%s"', $data['name']));
diff --git a/app/Services/Internal/Update/CategoryUpdateService.php b/app/Services/Internal/Update/CategoryUpdateService.php
index bad91d4d44..c5f11fff30 100644
--- a/app/Services/Internal/Update/CategoryUpdateService.php
+++ b/app/Services/Internal/Update/CategoryUpdateService.php
@@ -63,8 +63,17 @@ class CategoryUpdateService
public function update(Category $category, array $data): Category
{
$oldName = $category->name;
+ $changed = false;
if (array_key_exists('name', $data)) {
$category->name = $data['name'];
+ $changed = true;
+ }
+ if (array_key_exists('color', $data)) {
+ $data['color'] = '' === $data['color'] ? null : $data['color'];
+ $category->color = $data['color'];
+ $changed = true;
+ }
+ if ($changed) {
$category->save();
// update triggers and actions
$this->updateRuleTriggers($oldName, $data['name']);