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

143 lines
4.7 KiB
PHP
Raw Normal View History

2019-03-23 08:10:59 +01:00
<?php
/**
* FixUnevenAmount.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 DB;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Console\Command;
use stdClass;
/**
* Class FixUnevenAmount
*/
class FixUnevenAmount extends Command
{
use ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
protected $description = 'Fix journals with uneven amounts.';
protected $signature = 'firefly-iii:fix-uneven-amount';
2019-03-23 08:10:59 +01:00
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
2023-06-21 12:34:58 +02:00
$count = 0;
2019-03-23 08:10:59 +01:00
$journals = DB::table('transactions')
->groupBy('transaction_journal_id')
->whereNull('deleted_at')
->get(['transaction_journal_id', DB::raw('SUM(amount) AS the_sum')]);
/** @var stdClass $entry */
foreach ($journals as $entry) {
2023-06-12 06:24:30 +02:00
$sum = (string)$entry->the_sum;
2023-06-21 06:07:35 +02:00
if (!is_numeric($sum) || '' === $sum || str_contains($sum, 'e') || str_contains($sum, ',')) {
$message = sprintf(
'Journal #%d has an invalid sum ("%s"). No sure what to do.',
$entry->transaction_journal_id,
$entry->the_sum
);
$this->friendlyWarning($message);
2023-06-12 06:24:30 +02:00
app('log')->warning($message);
$count++;
continue;
}
2022-10-30 12:24:51 +01:00
if (0 !== bccomp((string)$entry->the_sum, '0')) {
2023-06-21 06:07:35 +02:00
$message = sprintf(
'Sum of journal #%d is %s instead of zero.',
$entry->transaction_journal_id,
$entry->the_sum
);
$this->friendlyWarning($message);
2022-10-30 14:44:49 +01:00
app('log')->warning($message);
2022-10-30 12:24:51 +01:00
$this->fixJournal((int)$entry->transaction_journal_id);
2019-03-23 08:10:59 +01:00
$count++;
}
}
if (0 === $count) {
$this->friendlyPositive('Database amount integrity is OK');
2019-03-23 08:10:59 +01:00
}
return 0;
}
/**
2023-06-21 06:07:35 +02:00
* @param int $param
2019-03-23 08:10:59 +01:00
*/
private function fixJournal(int $param): void
{
// one of the transactions is bad.
$journal = TransactionJournal::find($param);
if (!$journal) {
return;
2019-03-23 08:10:59 +01:00
}
2023-01-03 06:48:53 +01:00
/** @var Transaction|null $source */
2019-03-23 08:10:59 +01:00
$source = $journal->transactions()->where('amount', '<', 0)->first();
2020-02-14 05:39:21 +01:00
if (null === $source) {
$this->friendlyError(
2020-02-14 05:39:21 +01:00
sprintf(
2020-03-15 08:16:16 +01:00
'Journal #%d ("%s") has no source transaction. It will be deleted to maintain database consistency.',
$journal->id ?? 0,
2020-02-14 05:39:21 +01:00
$journal->description ?? ''
)
);
Transaction::where('transaction_journal_id', $journal->id ?? 0)->forceDelete();
2020-02-14 05:40:46 +01:00
TransactionJournal::where('id', $journal->description ?? 0)->forceDelete();
2020-02-14 05:39:21 +01:00
return;
}
2022-10-30 12:24:51 +01:00
$amount = bcmul('-1', (string)$source->amount);
2019-03-23 08:10:59 +01:00
// fix amount of destination:
2023-01-03 06:48:53 +01:00
/** @var Transaction|null $destination */
2020-02-14 05:39:21 +01:00
$destination = $journal->transactions()->where('amount', '>', 0)->first();
if (null === $destination) {
$this->friendlyError(
2020-02-14 05:39:21 +01:00
sprintf(
2020-03-15 08:16:16 +01:00
'Journal #%d ("%s") has no destination transaction. It will be deleted to maintain database consistency.',
$journal->id ?? 0,
2020-02-14 05:39:21 +01:00
$journal->description ?? ''
)
);
Transaction::where('transaction_journal_id', $journal->id ?? 0)->forceDelete();
2020-02-14 05:40:46 +01:00
TransactionJournal::where('id', $journal->description ?? 0)->forceDelete();
2020-02-14 05:39:21 +01:00
return;
}
2019-03-23 08:10:59 +01:00
$destination->amount = $amount;
$destination->save();
2019-06-10 20:14:00 +02:00
$message = sprintf('Corrected amount in transaction journal #%d', $param);
$this->friendlyInfo($message);
2019-03-23 08:10:59 +01:00
}
}