Files
firefly-iii/app/Console/Commands/Integrity/ReportsEmptyObjects.php

179 lines
6.1 KiB
PHP
Raw Normal View History

2019-03-23 08:10:59 +01:00
<?php
/**
* ReportEmptyObjects.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-08-17 12:09:03 +02:00
declare(strict_types=1);
2019-03-23 08:10:59 +01:00
namespace FireflyIII\Console\Commands\Integrity;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2019-03-23 18:58:06 +01:00
use FireflyIII\Models\Account;
2019-03-23 08:10:59 +01:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use Illuminate\Console\Command;
use stdClass;
2019-03-23 08:10:59 +01:00
2024-12-27 06:48:58 +01:00
class ReportsEmptyObjects extends Command
2019-03-23 08:10:59 +01:00
{
use ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
protected $description = 'Reports on empty database objects.';
2023-11-05 09:54:53 +01:00
2024-12-27 06:48:58 +01:00
protected $signature = 'integrity:empty-objects';
2019-03-23 08:10:59 +01:00
/**
* Execute the console command.
*/
public function handle(): int
{
$this->reportEmptyBudgets();
$this->reportEmptyCategories();
$this->reportEmptyTags();
$this->reportAccounts();
$this->reportBudgetLimits();
2020-03-21 15:43:41 +01:00
2019-03-23 08:10:59 +01:00
return 0;
}
/**
* Report on budgets with no transactions or journals.
*/
private function reportEmptyBudgets(): void
{
$set = Budget::leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
2023-12-20 19:35:52 +01:00
->leftJoin('users', 'budgets.user_id', '=', 'users.id')
->distinct()
->whereNull('budget_transaction_journal.budget_id')
->whereNull('budgets.deleted_at')
->get(['budgets.id', 'budgets.name', 'budgets.user_id', 'users.email'])
;
/** @var stdClass $entry */
2019-03-23 08:10:59 +01:00
foreach ($set as $entry) {
2019-06-13 15:48:35 +02:00
$line = sprintf(
2019-03-23 18:58:06 +01:00
'User #%d (%s) has budget #%d ("%s") which has no transaction journals.',
2019-03-23 08:10:59 +01:00
$entry->user_id,
$entry->email,
$entry->id,
2019-06-10 20:14:00 +02:00
$entry->name
2019-03-23 08:10:59 +01:00
);
$this->friendlyWarning($line);
2019-03-23 08:10:59 +01:00
}
}
/**
* Report on categories with no transactions or journals.
*/
private function reportEmptyCategories(): void
{
$set = Category::leftJoin('category_transaction_journal', 'categories.id', '=', 'category_transaction_journal.category_id')
2023-12-20 19:35:52 +01:00
->leftJoin('users', 'categories.user_id', '=', 'users.id')
->distinct()
->whereNull('category_transaction_journal.category_id')
->whereNull('categories.deleted_at')
->get(['categories.id', 'categories.name', 'categories.user_id', 'users.email'])
;
/** @var stdClass $entry */
2019-03-23 08:10:59 +01:00
foreach ($set as $entry) {
$line = sprintf(
2019-03-23 18:58:06 +01:00
'User #%d (%s) has category #%d ("%s") which has no transaction journals.',
2019-03-23 08:10:59 +01:00
$entry->user_id,
$entry->email,
$entry->id,
2019-06-10 20:14:00 +02:00
$entry->name
2019-03-23 08:10:59 +01:00
);
$this->friendlyWarning($line);
2019-03-23 08:10:59 +01:00
}
}
private function reportEmptyTags(): void
{
2019-03-23 18:58:06 +01:00
$set = Tag::leftJoin('tag_transaction_journal', 'tags.id', '=', 'tag_transaction_journal.tag_id')
2023-12-20 19:35:52 +01:00
->leftJoin('users', 'tags.user_id', '=', 'users.id')
->distinct()
->whereNull('tag_transaction_journal.tag_id')
->whereNull('tags.deleted_at')
->get(['tags.id', 'tags.tag', 'tags.user_id', 'users.email'])
;
/** @var stdClass $entry */
2019-03-23 08:10:59 +01:00
foreach ($set as $entry) {
$line = sprintf(
2019-03-23 18:58:06 +01:00
'User #%d (%s) has tag #%d ("%s") which has no transaction journals.',
2019-03-23 08:10:59 +01:00
$entry->user_id,
$entry->email,
$entry->id,
2019-06-10 20:14:00 +02:00
$entry->tag
2019-03-23 08:10:59 +01:00
);
$this->friendlyWarning($line);
2019-03-23 08:10:59 +01:00
}
}
2023-06-21 12:34:58 +02:00
/**
* Reports on accounts with no transactions.
*/
private function reportAccounts(): void
{
$set = Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')
2023-12-20 19:35:52 +01:00
->leftJoin('users', 'accounts.user_id', '=', 'users.id')
->groupBy(['accounts.id', 'accounts.encrypted', 'accounts.name', 'accounts.user_id', 'users.email'])
->whereNull('transactions.account_id')
->get(
['accounts.id', 'accounts.encrypted', 'accounts.name', 'accounts.user_id', 'users.email']
)
;
/** @var stdClass $entry */
2023-06-21 12:34:58 +02:00
foreach ($set as $entry) {
$line = 'User #%d (%s) has account #%d ("%s") which has no transactions.';
$line = sprintf($line, $entry->user_id, $entry->email, $entry->id, $entry->name);
$this->friendlyWarning($line);
}
}
/**
* Reports on budgets with no budget limits (which makes them pointless).
*/
private function reportBudgetLimits(): void
{
$set = Budget::leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id')
2023-12-20 19:35:52 +01:00
->leftJoin('users', 'budgets.user_id', '=', 'users.id')
->groupBy(['budgets.id', 'budgets.name', 'budgets.encrypted', 'budgets.user_id', 'users.email'])
->whereNull('budget_limits.id')
->get(['budgets.id', 'budgets.name', 'budgets.user_id', 'budgets.encrypted', 'users.email'])
;
2023-06-21 12:34:58 +02:00
/** @var Budget $entry */
foreach ($set as $entry) {
$line = sprintf(
'User #%d (%s) has budget #%d ("%s") which has no budget limits.',
$entry->user_id,
$entry->email,
$entry->id,
$entry->name
);
$this->friendlyWarning($line);
}
}
2019-03-23 08:10:59 +01:00
}