From 4ad666d9640d5c07d4b58db5692cb15dea2ae947 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 19 Oct 2022 19:57:22 +0200 Subject: [PATCH] Add button to remove unused accounts. --- .../V1/Controllers/Data/DestroyController.php | 19 +++++++++++++++--- app/Api/V1/Requests/Data/DestroyRequest.php | 1 + app/Helpers/Report/NetWorth.php | 1 + resources/lang/en_US/firefly.php | 3 +++ resources/views/profile/index.twig | 20 ++++++++++++++++++- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/Api/V1/Controllers/Data/DestroyController.php b/app/Api/V1/Controllers/Data/DestroyController.php index adba823dbc..ad7b41e66b 100644 --- a/app/Api/V1/Controllers/Data/DestroyController.php +++ b/app/Api/V1/Controllers/Data/DestroyController.php @@ -45,12 +45,15 @@ use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Services\Internal\Destroy\AccountDestroyService; use FireflyIII\Services\Internal\Destroy\JournalDestroyService; use Illuminate\Http\JsonResponse; +use Illuminate\Support\Facades\Log; /** * Class DestroyController */ class DestroyController extends Controller { + private bool $unused; + /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/#/data/destroyData @@ -62,8 +65,8 @@ class DestroyController extends Controller */ public function destroy(DestroyRequest $request): JsonResponse { - $objects = $request->getObjects(); - + $objects = $request->getObjects(); + $this->unused = $request->boolean('unused', false); switch ($objects) { default: throw new FireflyException(sprintf('This endpoint can\'t handle object "%s"', $objects)); @@ -260,9 +263,19 @@ class DestroyController extends Controller $repository = app(AccountRepositoryInterface::class); $collection = $repository->getAccountsByType($types); $service = app(AccountDestroyService::class); + /** @var Account $account */ foreach ($collection as $account) { - $service->destroy($account, null); + $count = $account->transactions()->count(); + if (true === $this->unused && 0 === $count) { + Log::info(sprintf('Deleted unused account #%d "%s"', $account->id, $account->name)); + $service->destroy($account, null); + continue; + } + if (false === $this->unused) { + Log::info(sprintf('Deleting account #%d "%s"', $account->id, $account->name)); + $service->destroy($account, null); + } } } diff --git a/app/Api/V1/Requests/Data/DestroyRequest.php b/app/Api/V1/Requests/Data/DestroyRequest.php index 2bc93dbb56..2b77fc5614 100644 --- a/app/Api/V1/Requests/Data/DestroyRequest.php +++ b/app/Api/V1/Requests/Data/DestroyRequest.php @@ -56,6 +56,7 @@ class DestroyRequest extends FormRequest return [ 'objects' => sprintf('required|min:1|string|in:%s', $valid), + 'unused' => 'in:true,false', ]; } } diff --git a/app/Helpers/Report/NetWorth.php b/app/Helpers/Report/NetWorth.php index 6f816018e7..fa686b3af4 100644 --- a/app/Helpers/Report/NetWorth.php +++ b/app/Helpers/Report/NetWorth.php @@ -33,6 +33,7 @@ use FireflyIII\Support\CacheProperties; use FireflyIII\User; use Illuminate\Support\Collection; use JsonException; +use Illuminate\Support\Facades\Log; /** * diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 7d6d2435e4..8b03bd5821 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -1344,6 +1344,9 @@ return [ 'delete_data_title' => 'Delete data from Firefly III', 'permanent_delete_stuff' => 'You can delete stuff from Firefly III. Using the buttons below means that your items will be removed from view and hidden. There is no undo-button for this, but the items may remain in the database where you can salvage them if necessary.', 'other_sessions_logged_out' => 'All your other sessions have been logged out.', + 'delete_unused_accounts' => 'Deleting unused accounts will clean your auto-complete lists.', + 'delete_all_unused_accounts' => 'Delete unused accounts', + 'deleted_all_unused_accounts' => 'All unused accounts are deleted', 'delete_all_budgets' => 'Delete ALL your budgets', 'delete_all_categories' => 'Delete ALL your categories', 'delete_all_tags' => 'Delete ALL your tags', diff --git a/resources/views/profile/index.twig b/resources/views/profile/index.twig index 11a56516cd..3698ecb27e 100644 --- a/resources/views/profile/index.twig +++ b/resources/views/profile/index.twig @@ -257,6 +257,21 @@ data-type="liabilities" class="confirm btn btn-warning btn-sm"> {{ 'delete_all_liabilities'|_ }} +

{{ 'accounts'|_ }}

+

+ + {{ 'delete_unused_accounts'|_ }} + +

+
+ +
+ +

{{ 'transactions'|_ }}