Files
firefly-iii/app/Console/Commands/System/ForceDecimalSize.php

569 lines
22 KiB
PHP
Raw Normal View History

2023-04-09 20:29:35 +02:00
<?php
2023-04-16 07:33:12 +02:00
/*
* ForceDecimalSize.php
* Copyright (c) 2023 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/>.
*/
2023-04-09 20:29:35 +02:00
2023-04-10 08:29:27 +02:00
declare(strict_types=1);
2023-04-16 07:33:12 +02:00
namespace FireflyIII\Console\Commands\System;
2023-04-09 20:29:35 +02:00
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2023-04-09 20:29:35 +02:00
use FireflyIII\Exceptions\FireflyException;
2023-05-07 20:17:29 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AutoBudget;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Bill;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\RecurrenceTransaction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
2023-04-09 20:29:35 +02:00
use Illuminate\Console\Command;
2023-05-07 20:17:29 +02:00
use Illuminate\Database\Eloquent\Builder;
2023-05-13 05:56:49 +02:00
use Illuminate\Database\Eloquent\Model;
2023-05-07 20:17:29 +02:00
use Illuminate\Support\Collection;
2023-04-09 20:29:35 +02:00
use Illuminate\Support\Facades\DB;
2023-05-07 20:17:29 +02:00
/**
* Class ForceDecimalSize
*
* This command was inspired by https://github.com/elliot-gh. It will check all amount fields
* and their values and correct them to the correct number of decimal places. This fixes issues where
* Firefly III would store 0.01 as 0.01000000000000000020816681711721685132943093776702880859375.
*/
2023-04-09 20:29:35 +02:00
class ForceDecimalSize extends Command
{
use ShowsFriendlyMessages;
protected $description = 'This command resizes DECIMAL columns in MySQL or PostgreSQL and correct amounts (only MySQL).';
protected $signature = 'firefly-iii:force-decimal-size';
2023-05-13 05:56:49 +02:00
private string $cast;
2023-05-29 13:56:55 +02:00
private array $classes
= [
'accounts' => Account::class,
'auto_budgets' => AutoBudget::class,
'available_budgets' => AvailableBudget::class,
'bills' => Bill::class,
'budget_limits' => BudgetLimit::class,
'piggy_bank_events' => PiggyBankEvent::class,
'piggy_bank_repetitions' => PiggyBankRepetition::class,
'piggy_banks' => PiggyBank::class,
'recurrences_transactions' => RecurrenceTransaction::class,
'transactions' => Transaction::class,
];
2023-05-13 05:56:49 +02:00
private string $operator;
private string $regularExpression;
2023-05-29 13:56:55 +02:00
private array $tables
= [
'accounts' => ['virtual_balance'],
'auto_budgets' => ['amount'],
'available_budgets' => ['amount'],
'bills' => ['amount_min', 'amount_max'],
'budget_limits' => ['amount'],
'currency_exchange_rates' => ['rate', 'user_rate'],
'limit_repetitions' => ['amount'],
'piggy_bank_events' => ['amount'],
2024-12-01 06:48:15 +01:00
'piggy_bank_repetitions' => ['current_amount'],
'piggy_banks' => ['target_amount'],
'recurrences_transactions' => ['amount', 'foreign_amount'],
'transactions' => ['amount', 'foreign_amount'],
];
2023-04-09 20:29:35 +02:00
/**
* Execute the console command.
*/
public function handle(): int
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in ForceDecimalSize::handle()');
2023-05-13 05:56:49 +02:00
$this->determineDatabaseType();
2023-04-09 20:29:35 +02:00
$this->friendlyError('Running this command is dangerous and can cause data loss.');
$this->friendlyError('Please do not continue.');
2023-04-09 20:29:35 +02:00
$question = $this->confirm('Do you want to continue?');
if (true === $question) {
2023-05-07 20:17:29 +02:00
$this->correctAmounts();
2023-04-09 20:29:35 +02:00
$this->updateDecimals();
}
2023-05-29 13:56:55 +02:00
2023-04-09 20:29:35 +02:00
return 0;
}
2023-06-21 12:34:58 +02:00
private function determineDatabaseType(): void
2023-05-07 20:17:29 +02:00
{
2023-06-21 12:34:58 +02:00
// switch stuff based on database connection:
$this->operator = 'REGEXP';
$this->regularExpression = '\'\\\.[\\\d]{%d}[1-9]+\'';
2023-06-21 12:34:58 +02:00
$this->cast = 'CHAR';
if ('pgsql' === config('database.default')) {
$this->operator = 'SIMILAR TO';
$this->regularExpression = '\'%%\.[\d]{%d}[1-9]+%%\'';
$this->cast = 'TEXT';
2023-05-07 20:17:29 +02:00
}
2023-06-21 12:34:58 +02:00
if ('sqlite' === config('database.default')) {
$this->regularExpression = '"\.[\d]{%d}[1-9]+"';
2023-05-07 20:17:29 +02:00
}
}
/**
* This method checks if a basic check can be done or if it needs to be complicated.
*/
private function correctAmounts(): void
{
2023-05-13 05:56:49 +02:00
// if sqlite, add function?
2024-12-22 08:43:12 +01:00
if ('sqlite' === (string) config('database.default')) {
2023-11-04 14:18:49 +01:00
DB::connection()->getPdo()->sqliteCreateFunction('REGEXP', static function ($pattern, $value) {
2023-05-13 05:56:49 +02:00
mb_regex_encoding('UTF-8');
$pattern = trim($pattern, '"');
2023-05-29 13:56:55 +02:00
2024-12-22 08:43:12 +01:00
return (false !== mb_ereg($pattern, (string) $value)) ? 1 : 0;
2023-05-13 05:56:49 +02:00
});
2023-05-07 20:17:29 +02:00
}
2024-12-22 08:43:12 +01:00
if (!in_array((string) config('database.default'), ['mysql', 'pgsql', 'sqlite'], true)) {
$this->friendlyWarning(sprintf('Skip correcting amounts, does not support "%s"...', (string) config('database.default')));
2023-05-29 13:56:55 +02:00
2023-05-07 20:17:29 +02:00
return;
}
2023-05-13 05:56:49 +02:00
$this->correctAmountsByCurrency();
2023-05-07 20:17:29 +02:00
}
/**
* This method loops all enabled currencies and then calls the method that will fix all objects in this currency.
*
2023-07-04 13:29:19 +02:00
* @throws FireflyException
2023-05-07 20:17:29 +02:00
*/
private function correctAmountsByCurrency(): void
{
/** @var Collection $enabled */
$enabled = TransactionCurrency::whereEnabled(1)->get();
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
/** @var TransactionCurrency $currency */
foreach ($enabled as $currency) {
$this->correctByCurrency($currency);
}
}
/**
* This method loops the available tables that may need fixing, and calls for the right method that can fix them.
*
* @throws FireflyException
*/
private function correctByCurrency(TransactionCurrency $currency): void
{
/**
* @var string $name
2023-06-21 12:34:58 +02:00
* @var array $fields
2023-05-07 20:17:29 +02:00
*/
foreach ($this->tables as $name => $fields) {
2023-10-29 12:10:03 +01:00
switch ($name) { // @phpstan-ignore-line
2023-05-07 20:17:29 +02:00
default:
$message = sprintf('Cannot handle table "%s"', $name);
$this->friendlyError($message);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
throw new FireflyException($message);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
case 'accounts':
$this->correctAccountAmounts($currency, $fields);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
break;
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
case 'auto_budgets':
case 'available_budgets':
case 'bills':
case 'budget_limits':
2023-05-13 05:56:49 +02:00
case 'recurrences_transactions':
$this->correctGeneric($currency, $name);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
break;
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
case 'currency_exchange_rates':
case 'limit_repetitions':
// do nothing
break;
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
case 'piggy_bank_events':
$this->correctPiggyEventAmounts($currency, $fields);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
break;
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
case 'piggy_bank_repetitions':
$this->correctPiggyRepetitionAmounts($currency, $fields);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
break;
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
case 'piggy_banks':
$this->correctPiggyAmounts($currency, $fields);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
break;
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
case 'transactions':
$this->correctTransactionAmounts($currency);
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
break;
}
}
}
2023-05-13 05:56:49 +02:00
/**
2023-06-21 12:34:58 +02:00
* This method loops over all accounts and validates the amounts.
2023-05-13 05:56:49 +02:00
*/
2023-06-21 12:34:58 +02:00
private function correctAccountAmounts(TransactionCurrency $currency, array $fields): void
2023-05-13 05:56:49 +02:00
{
$operator = $this->operator;
$cast = $this->cast;
$regularExpression = $this->regularExpression;
/** @var Builder $query */
$query = Account::leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
2023-12-20 19:35:52 +01:00
->where('account_meta.name', 'currency_id')
2024-12-22 08:43:12 +01:00
->where('account_meta.data', json_encode((string) $currency->id))
2023-12-20 19:35:52 +01:00
;
2023-12-21 05:07:26 +01:00
$query->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression): void {
2023-06-21 12:34:58 +02:00
foreach ($fields as $field) {
$q->orWhere(
2023-11-04 11:31:14 +01:00
DB::raw(sprintf('CAST(accounts.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
2023-06-21 12:34:58 +02:00
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
2023-05-13 05:56:49 +02:00
}
2023-06-21 12:34:58 +02:00
});
$result = $query->get(['accounts.*']);
2023-05-13 05:56:49 +02:00
if (0 === $result->count()) {
2023-06-21 12:34:58 +02:00
$this->friendlyPositive(sprintf('All accounts in %s are OK', $currency->code));
2023-05-29 13:56:55 +02:00
2023-05-13 05:56:49 +02:00
return;
}
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var Account $account */
foreach ($result as $account) {
2023-11-26 12:10:42 +01:00
/** @var string $field */
2023-05-13 05:56:49 +02:00
foreach ($fields as $field) {
$value = $account->{$field};
2023-05-13 05:56:49 +02:00
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
2023-12-10 06:45:59 +01:00
$pow = 10 ** $currency->decimal_places;
2024-12-22 08:43:12 +01:00
$correct = bcdiv((string) round($value * $pow), (string) $pow, 12);
2023-06-21 12:34:58 +02:00
$this->friendlyInfo(sprintf('Account #%d has %s with value "%s", this has been corrected to "%s".', $account->id, $field, $value, $correct));
Account::find($account->id)->update([$field => $correct]);
2023-05-13 05:56:49 +02:00
}
}
}
2023-05-07 20:17:29 +02:00
/**
2023-06-21 12:34:58 +02:00
* This method fixes all auto budgets in currency $currency.
2023-05-07 20:17:29 +02:00
*/
2023-06-21 12:34:58 +02:00
private function correctGeneric(TransactionCurrency $currency, string $table): void
2023-05-07 20:17:29 +02:00
{
2023-06-21 12:34:58 +02:00
$class = $this->classes[$table];
$fields = $this->tables[$table];
2023-05-13 05:56:49 +02:00
$operator = $this->operator;
$cast = $this->cast;
$regularExpression = $this->regularExpression;
2023-05-07 20:17:29 +02:00
/** @var Builder $query */
$query = $class::where('transaction_currency_id', $currency->id)->where(
2023-12-21 05:07:26 +01:00
static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression): void {
2023-11-26 12:10:42 +01:00
/** @var string $field */
2023-06-21 12:34:58 +02:00
foreach ($fields as $field) {
$q->orWhere(
2023-11-04 11:31:14 +01:00
DB::raw(sprintf('CAST(%s AS %s)', $field, $cast)), // @phpstan-ignore-line
2023-06-21 12:34:58 +02:00
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
}
);
2023-05-07 20:17:29 +02:00
$result = $query->get(['*']);
2023-05-07 20:17:29 +02:00
if (0 === $result->count()) {
2023-06-21 12:34:58 +02:00
$this->friendlyPositive(sprintf('All %s in %s are OK', $table, $currency->code));
2023-05-29 13:56:55 +02:00
2023-05-07 20:17:29 +02:00
return;
}
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var Model $item */
2023-05-07 20:17:29 +02:00
foreach ($result as $item) {
2023-11-26 12:10:42 +01:00
/** @var string $field */
2023-05-07 20:17:29 +02:00
foreach ($fields as $field) {
$value = $item->{$field};
2024-05-28 05:53:17 +02:00
if (null === $value || '' === $value) {
2023-05-07 20:17:29 +02:00
continue;
}
// fix $field by rounding it down correctly.
2023-12-10 06:45:59 +01:00
$pow = 10 ** $currency->decimal_places;
2024-12-22 08:43:12 +01:00
$correct = bcdiv((string) round($value * $pow), (string) $pow, 12);
2023-06-21 12:34:58 +02:00
$this->friendlyWarning(sprintf('%s #%d has %s with value "%s", this has been corrected to "%s".', $table, $item->id, $field, $value, $correct));
$class::find($item->id)->update([$field => $correct]);
2023-05-07 20:17:29 +02:00
}
}
}
/**
* This method fixes all piggy bank events in currency $currency.
*/
private function correctPiggyEventAmounts(TransactionCurrency $currency, array $fields): void
{
2023-05-13 05:56:49 +02:00
$operator = $this->operator;
$cast = $this->cast;
$regularExpression = $this->regularExpression;
2023-05-07 20:17:29 +02:00
/** @var Builder $query */
$query = PiggyBankEvent::leftJoin('piggy_banks', 'piggy_bank_events.piggy_bank_id', '=', 'piggy_banks.id')
2023-12-20 19:35:52 +01:00
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
2024-12-22 08:43:12 +01:00
->where('account_meta.data', json_encode((string) $currency->id))
2023-12-21 05:07:26 +01:00
->where(static function (Builder $q) use ($fields, $currency, $cast, $operator, $regularExpression): void {
2023-12-20 19:35:52 +01:00
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_bank_events.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
})
;
2023-05-07 20:17:29 +02:00
$result = $query->get(['piggy_bank_events.*']);
2023-05-07 20:17:29 +02:00
if (0 === $result->count()) {
$this->friendlyPositive(sprintf('All piggy bank events in %s are OK', $currency->code));
2023-05-29 13:56:55 +02:00
2023-05-07 20:17:29 +02:00
return;
}
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
/** @var PiggyBankEvent $item */
foreach ($result as $item) {
2023-11-26 12:10:42 +01:00
/** @var string $field */
2023-05-07 20:17:29 +02:00
foreach ($fields as $field) {
$value = $item->{$field};
2023-05-07 20:17:29 +02:00
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
2023-12-10 06:45:59 +01:00
$pow = 10 ** $currency->decimal_places;
2024-12-22 08:43:12 +01:00
$correct = bcdiv((string) round($value * $pow), (string) $pow, 12);
$this->friendlyWarning(
sprintf('Piggy bank event #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct)
);
2023-05-07 20:17:29 +02:00
PiggyBankEvent::find($item->id)->update([$field => $correct]);
}
}
}
/**
* This method fixes all piggy bank repetitions in currency $currency.
*/
2023-12-21 04:59:23 +01:00
private function correctPiggyRepetitionAmounts(TransactionCurrency $currency, array $fields): void
2023-05-07 20:17:29 +02:00
{
2023-05-13 05:56:49 +02:00
$operator = $this->operator;
$cast = $this->cast;
$regularExpression = $this->regularExpression;
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
// select all piggy bank repetitions with this currency and issue.
/** @var Builder $query */
$query = PiggyBankRepetition::leftJoin('piggy_banks', 'piggy_bank_repetitions.piggy_bank_id', '=', 'piggy_banks.id')
2023-12-20 19:35:52 +01:00
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
2024-12-22 08:43:12 +01:00
->where('account_meta.data', json_encode((string) $currency->id))
2023-12-21 05:07:26 +01:00
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression): void {
2023-12-20 19:35:52 +01:00
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_bank_repetitions.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
})
;
2023-05-07 20:17:29 +02:00
$result = $query->get(['piggy_bank_repetitions.*']);
2023-05-07 20:17:29 +02:00
if (0 === $result->count()) {
$this->friendlyPositive(sprintf('All piggy bank repetitions in %s', $currency->code));
2023-05-29 13:56:55 +02:00
2023-05-07 20:17:29 +02:00
return;
}
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
/** @var PiggyBankRepetition $item */
foreach ($result as $item) {
2023-11-26 12:10:42 +01:00
/** @var string $field */
2023-05-07 20:17:29 +02:00
foreach ($fields as $field) {
$value = $item->{$field};
2023-05-07 20:17:29 +02:00
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
2023-12-10 06:45:59 +01:00
$pow = 10 ** $currency->decimal_places;
2024-12-22 08:43:12 +01:00
$correct = bcdiv((string) round($value * $pow), (string) $pow, 12);
$this->friendlyWarning(
sprintf('Piggy bank repetition #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct)
);
2023-05-07 20:17:29 +02:00
PiggyBankRepetition::find($item->id)->update([$field => $correct]);
}
}
}
2023-06-21 12:34:58 +02:00
/**
* This method fixes all piggy banks in currency $currency.
*/
private function correctPiggyAmounts(TransactionCurrency $currency, array $fields): void
{
$operator = $this->operator;
$cast = $this->cast;
$regularExpression = $this->regularExpression;
/** @var Builder $query */
$query = PiggyBank::leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
2023-12-20 19:35:52 +01:00
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
2024-12-22 08:43:12 +01:00
->where('account_meta.data', json_encode((string) $currency->id))
2023-12-21 05:07:26 +01:00
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression): void {
2023-12-20 19:35:52 +01:00
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_banks.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
})
;
2023-06-21 12:34:58 +02:00
$result = $query->get(['piggy_banks.*']);
2023-06-21 12:34:58 +02:00
if (0 === $result->count()) {
$this->friendlyPositive(sprintf('All piggy banks in %s are OK', $currency->code));
return;
}
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var PiggyBank $item */
foreach ($result as $item) {
2023-11-26 12:10:42 +01:00
/** @var string $field */
2023-06-21 12:34:58 +02:00
foreach ($fields as $field) {
$value = $item->{$field};
2023-06-21 12:34:58 +02:00
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
2023-12-10 06:45:59 +01:00
$pow = 10 ** $currency->decimal_places;
2024-12-22 08:43:12 +01:00
$correct = bcdiv((string) round($value * $pow), (string) $pow, 12);
2023-06-21 12:34:58 +02:00
$this->friendlyWarning(sprintf('Piggy bank #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
PiggyBank::find($item->id)->update([$field => $correct]);
}
}
}
2023-05-07 20:17:29 +02:00
/**
* This method fixes all transactions in currency $currency.
*/
private function correctTransactionAmounts(TransactionCurrency $currency): void
{
// select all transactions with this currency and issue.
/** @var Builder $query */
$query = Transaction::where('transaction_currency_id', $currency->id)->where(
2023-11-04 11:31:14 +01:00
DB::raw(sprintf('CAST(amount as %s)', $this->cast)), // @phpstan-ignore-line
2023-05-13 05:56:49 +02:00
$this->operator,
DB::raw(sprintf($this->regularExpression, $currency->decimal_places))
2023-05-07 20:17:29 +02:00
);
2023-05-13 05:56:49 +02:00
$result = $query->get(['transactions.*']);
2023-05-07 20:17:29 +02:00
if (0 === $result->count()) {
$this->friendlyPositive(sprintf('All transactions in %s are OK', $currency->code));
2023-05-07 20:17:29 +02:00
}
2023-05-13 05:56:49 +02:00
2023-05-07 20:17:29 +02:00
/** @var Transaction $item */
foreach ($result as $item) {
$value = $item->amount;
2023-11-01 18:45:15 +01:00
if ('' === $value) {
2023-05-07 20:17:29 +02:00
continue;
}
// fix $field by rounding it down correctly.
2024-12-22 08:43:12 +01:00
$pow = (float) 10 ** $currency->decimal_places;
$correct = bcdiv((string) round((float) $value * $pow), (string) $pow, 12);
$this->friendlyWarning(sprintf('Transaction #%d has amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct));
2023-05-07 20:17:29 +02:00
Transaction::find($item->id)->update(['amount' => $correct]);
}
// select all transactions with this FOREIGN currency and issue.
/** @var Builder $query */
$query = Transaction::where('foreign_currency_id', $currency->id)->where(
2023-11-04 11:31:14 +01:00
DB::raw(sprintf('CAST(foreign_amount as %s)', $this->cast)), // @phpstan-ignore-line
2023-05-13 05:56:49 +02:00
$this->operator,
DB::raw(sprintf($this->regularExpression, $currency->decimal_places))
2023-05-07 20:17:29 +02:00
);
$result = $query->get(['*']);
if (0 === $result->count()) {
$this->friendlyPositive(sprintf('All transactions in foreign currency %s are OK', $currency->code));
2023-05-29 13:56:55 +02:00
2023-05-07 20:17:29 +02:00
return;
}
2023-12-20 19:35:52 +01:00
2023-05-07 20:17:29 +02:00
/** @var Transaction $item */
foreach ($result as $item) {
$value = $item->foreign_amount;
2023-05-07 20:17:29 +02:00
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
2024-12-22 08:43:12 +01:00
$pow = (float) 10 ** $currency->decimal_places;
$correct = bcdiv((string) round((float) $value * $pow), (string) $pow, 12);
$this->friendlyWarning(
sprintf('Transaction #%d has foreign amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct)
);
2023-05-07 20:17:29 +02:00
Transaction::find($item->id)->update(['foreign_amount' => $correct]);
}
}
2023-04-09 20:29:35 +02:00
private function updateDecimals(): void
{
$this->friendlyInfo('Going to force the size of DECIMAL columns. Please hold.');
2024-12-22 08:43:12 +01:00
$type = (string) config('database.default');
2023-05-07 20:17:29 +02:00
2023-04-09 20:29:35 +02:00
/**
* @var string $name
2023-06-21 12:34:58 +02:00
* @var array $fields
2023-04-09 20:29:35 +02:00
*/
2023-05-07 20:17:29 +02:00
foreach ($this->tables as $name => $fields) {
2023-04-09 20:29:35 +02:00
/** @var string $field */
2023-05-07 20:17:29 +02:00
foreach ($fields as $field) {
$this->friendlyLine(sprintf('Updating table "%s", field "%s"...', $name, $field));
2023-10-29 12:10:03 +01:00
if ('pgsql' === $type) {
DB::select(sprintf('ALTER TABLE %s ALTER COLUMN %s TYPE DECIMAL(32,12);', $name, $field));
sleep(1);
2023-12-20 19:35:52 +01:00
2023-10-29 12:10:03 +01:00
return;
}
if ('mysql' === $type) {
DB::select(sprintf('ALTER TABLE %s CHANGE COLUMN %s %s DECIMAL(32, 12);', $name, $field, $field));
sleep(1);
2023-12-20 19:35:52 +01:00
2023-10-29 12:10:03 +01:00
return;
2023-05-13 05:56:49 +02:00
}
2023-10-29 12:10:03 +01:00
$this->friendlyError(sprintf('Cannot handle database type "%s".', $type));
2023-04-09 20:29:35 +02:00
}
}
}
}