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

191 lines
6.5 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;
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;
/**
* Class ReportEmptyObjects
*/
class ReportEmptyObjects extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Reports on empty database objects.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:report-empty-objects';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
2019-03-23 18:58:06 +01:00
$start = microtime(true);
2019-03-23 08:10:59 +01:00
$this->reportEmptyBudgets();
$this->reportEmptyCategories();
$this->reportEmptyTags();
$this->reportAccounts();
$this->reportBudgetLimits();
2019-03-23 18:58:06 +01:00
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Report on empty objects finished in %s seconds', $end));
2019-03-23 08:10:59 +01:00
return 0;
}
2019-03-23 18:58:06 +01:00
/**
* Reports on accounts with no transactions.
*/
private function reportAccounts(): void
{
$set = Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')
->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']
);
2019-03-23 08:10:59 +01:00
2019-03-23 18:58:06 +01:00
/** @var stdClass $entry */
foreach ($set as $entry) {
$line = 'User #%d (%s) has account #%d ("%s") which has no transactions.';
2019-06-10 20:14:00 +02:00
$line = sprintf($line, $entry->user_id, $entry->email, $entry->id, $entry->name);
2019-03-23 18:58:06 +01:00
$this->line($line);
}
}
2019-03-23 08:10:59 +01:00
/**
* 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')
->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']);
/** @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->line($line);
}
}
/**
* 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')
->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 */
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->line($line);
}
}
/**
* 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')
->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 */
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->line($line);
}
}
/**
*
*/
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')
->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']);
2019-03-23 08:10:59 +01:00
/** @var stdClass $entry */
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->line($line);
}
}
}