Files
firefly-iii/app/Console/Commands/Correction/TransferBudgets.php

75 lines
2.7 KiB
PHP
Raw Normal View History

2019-03-23 08:10:59 +01:00
<?php
/**
* TransferBudgets.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\Correction;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Console\Command;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2019-03-23 08:10:59 +01:00
/**
* Class TransferBudgets
*/
class TransferBudgets extends Command
{
use ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
protected $description = 'Removes budgets from transfers.';
protected $signature = 'firefly-iii:fix-transfer-budgets';
2019-03-23 08:10:59 +01:00
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$set = TransactionJournal::distinct()
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
->whereNotIn('transaction_types.type', [TransactionType::WITHDRAWAL])
->whereNotNull('budget_transaction_journal.budget_id')->get(['transaction_journals.*']);
$count = 0;
/** @var TransactionJournal $entry */
foreach ($set as $entry) {
2020-10-10 11:21:45 +02:00
$message = sprintf('Transaction journal #%d is a %s, so has no longer a budget.', $entry->id, $entry->transactionType->type);
$this->friendlyInfo($message);
2020-10-10 11:21:45 +02:00
Log::debug($message);
2019-03-23 08:10:59 +01:00
$entry->budgets()->sync([]);
$count++;
}
if (0 === $count) {
$message = 'No invalid budget/journal entries.';
$this->friendlyPositive($message);
2019-03-23 08:10:59 +01:00
}
2019-06-13 15:48:35 +02:00
if (0 !== $count) {
2020-10-10 11:21:45 +02:00
$message = sprintf('Corrected %d invalid budget/journal entries (entry).', $count);
Log::debug($message);
$this->friendlyInfo($message);
2019-06-10 20:14:00 +02:00
}
2019-03-23 08:10:59 +01:00
return 0;
}
}