mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-01 11:15:55 +00:00
Fix #11388
This commit is contained in:
@@ -100,6 +100,10 @@ class CorrectsAmounts extends Command
|
||||
if (null === $source->foreign_currency_id || null === $destination->foreign_currency_id) {
|
||||
continue;
|
||||
}
|
||||
if(null === $source->foreign_amount || null === $destination->foreign_amount) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sourceAccount = $source->account;
|
||||
$destAccount = $destination->account;
|
||||
if (null === $sourceAccount || null === $destAccount) {
|
||||
|
||||
@@ -132,11 +132,7 @@ class CorrectsUnevenAmount extends Command
|
||||
private function fixUnevenAmounts(): void
|
||||
{
|
||||
Log::debug('fixUnevenAmounts()');
|
||||
$journals = DB::table('transactions')
|
||||
->groupBy('transaction_journal_id')
|
||||
->whereNull('deleted_at')
|
||||
->get(['transaction_journal_id', DB::raw('SUM(amount) AS the_sum')])
|
||||
;
|
||||
$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) {
|
||||
@@ -146,11 +142,7 @@ class CorrectsUnevenAmount extends Command
|
||||
|| '' === $sum // @phpstan-ignore-line
|
||||
|| 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
|
||||
);
|
||||
$message = sprintf('Journal #%d has an invalid sum ("%s"). No sure what to do.', $entry->transaction_journal_id, $entry->the_sum);
|
||||
$this->friendlyWarning($message);
|
||||
Log::warning($message);
|
||||
++$this->count;
|
||||
@@ -184,13 +176,7 @@ class CorrectsUnevenAmount extends Command
|
||||
$source = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
if (null === $source) {
|
||||
$this->friendlyError(
|
||||
sprintf(
|
||||
'Journal #%d ("%s") has no source transaction. It will be deleted to maintain database consistency.',
|
||||
$journal->id ?? 0,
|
||||
$journal->description ?? ''
|
||||
)
|
||||
);
|
||||
$this->friendlyError(sprintf('Journal #%d ("%s") has no source transaction. It will be deleted to maintain database consistency.', $journal->id ?? 0, $journal->description ?? ''));
|
||||
Transaction::where('transaction_journal_id', $journal->id ?? 0)->forceDelete();
|
||||
TransactionJournal::where('id', $journal->id ?? 0)->forceDelete();
|
||||
++$this->count;
|
||||
@@ -205,13 +191,7 @@ class CorrectsUnevenAmount extends Command
|
||||
$destination = $journal->transactions()->where('amount', '>', 0)->first();
|
||||
|
||||
if (null === $destination) {
|
||||
$this->friendlyError(
|
||||
sprintf(
|
||||
'Journal #%d ("%s") has no destination transaction. It will be deleted to maintain database consistency.',
|
||||
$journal->id ?? 0,
|
||||
$journal->description ?? ''
|
||||
)
|
||||
);
|
||||
$this->friendlyError(sprintf('Journal #%d ("%s") has no destination transaction. It will be deleted to maintain database consistency.', $journal->id ?? 0, $journal->description ?? ''));
|
||||
|
||||
Transaction::where('transaction_journal_id', $journal->id ?? 0)->forceDelete();
|
||||
TransactionJournal::where('id', $journal->id ?? 0)->forceDelete();
|
||||
|
||||
@@ -34,7 +34,6 @@ use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use JsonException;
|
||||
use stdClass;
|
||||
|
||||
use function Safe\json_decode;
|
||||
|
||||
class RemovesDatabaseDecryption extends Command
|
||||
@@ -120,8 +119,8 @@ class RemovesDatabaseDecryption extends Command
|
||||
if (null === $original) {
|
||||
return;
|
||||
}
|
||||
$id = (int) $row->id;
|
||||
$value = '';
|
||||
$id = (int)$row->id;
|
||||
$value = '';
|
||||
|
||||
try {
|
||||
$value = $this->tryDecrypt($original);
|
||||
@@ -133,7 +132,7 @@ class RemovesDatabaseDecryption extends Command
|
||||
}
|
||||
|
||||
// A separate routine for preferences table:
|
||||
if ('preferences' === $table) {
|
||||
if ('preferences' === $table && is_string($value)) {
|
||||
$this->decryptPreferencesRow($id, $value);
|
||||
|
||||
return;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* CorrectAccountBalance.php
|
||||
* RepairsAccountBalances.php
|
||||
* Copyright (c) 2024 james@firefly-iii.org.
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
|
||||
@@ -42,8 +42,8 @@ class UpgradesLiabilitiesEight extends Command
|
||||
use ShowsFriendlyMessages;
|
||||
|
||||
public const string CONFIG_NAME = '600_upgrade_liabilities';
|
||||
protected $description = 'Upgrade liabilities to new 6.0.0 structure.';
|
||||
protected $signature = 'upgrade:600-liabilities {--F|force : Force the execution of this command.}';
|
||||
protected $description = 'Upgrade liabilities to new 6.0.0 structure.';
|
||||
protected $signature = 'upgrade:600-liabilities {--F|force : Force the execution of this command.}';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
@@ -82,10 +82,9 @@ class UpgradesLiabilitiesEight extends Command
|
||||
private function upgradeForUser(User $user): void
|
||||
{
|
||||
$accounts = $user->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->whereIn('account_types.type', config('firefly.valid_liabilities'))
|
||||
->get(['accounts.*'])
|
||||
;
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->whereIn('account_types.type', config('firefly.valid_liabilities'))
|
||||
->get(['accounts.*']);
|
||||
|
||||
/** @var Account $account */
|
||||
foreach ($accounts as $account) {
|
||||
@@ -102,7 +101,7 @@ class UpgradesLiabilitiesEight extends Command
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($account->user);
|
||||
|
||||
$direction = $repository->getMetaValue($account, 'liability_direction');
|
||||
$direction = $repository->getMetaValue($account, 'liability_direction');
|
||||
if ('credit' === $direction && $this->hasBadOpening($account)) {
|
||||
$this->deleteCreditTransaction($account);
|
||||
$this->reverseOpeningBalance($account);
|
||||
@@ -121,34 +120,31 @@ class UpgradesLiabilitiesEight extends Command
|
||||
$openingBalanceType = TransactionType::whereType(TransactionTypeEnum::OPENING_BALANCE->value)->first();
|
||||
$liabilityType = TransactionType::whereType(TransactionTypeEnum::LIABILITY_CREDIT->value)->first();
|
||||
$openingJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $openingBalanceType->id)
|
||||
->first(['transaction_journals.*'])
|
||||
;
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $openingBalanceType->id)
|
||||
->first(['transaction_journals.*']);
|
||||
if (null === $openingJournal) {
|
||||
return false;
|
||||
}
|
||||
$liabilityJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $liabilityType->id)
|
||||
->first(['transaction_journals.*'])
|
||||
;
|
||||
$liabilityJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $liabilityType->id)
|
||||
->first(['transaction_journals.*']);
|
||||
if (null === $liabilityJournal) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $openingJournal->date->isSameDay($liabilityJournal->date);
|
||||
return (bool)$openingJournal->date->isSameDay($liabilityJournal->date);
|
||||
}
|
||||
|
||||
private function deleteCreditTransaction(Account $account): void
|
||||
{
|
||||
$liabilityType = TransactionType::whereType(TransactionTypeEnum::LIABILITY_CREDIT->value)->first();
|
||||
$liabilityJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $liabilityType->id)
|
||||
->first(['transaction_journals.*'])
|
||||
;
|
||||
if (null !== $liabilityJournal) {
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $liabilityType->id)
|
||||
->first(['transaction_journals.*']);
|
||||
if (null !== $liabilityJournal && null !== $liabilityJournal->transactionGroup) {
|
||||
$group = $liabilityJournal->transactionGroup;
|
||||
$service = new TransactionGroupDestroyService();
|
||||
$service->destroy($group);
|
||||
@@ -161,17 +157,16 @@ class UpgradesLiabilitiesEight extends Command
|
||||
$openingBalanceType = TransactionType::whereType(TransactionTypeEnum::OPENING_BALANCE->value)->first();
|
||||
|
||||
/** @var TransactionJournal $openingJournal */
|
||||
$openingJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $openingBalanceType->id)
|
||||
->first(['transaction_journals.*'])
|
||||
;
|
||||
$openingJournal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.account_id', $account->id)
|
||||
->where('transaction_journals.transaction_type_id', $openingBalanceType->id)
|
||||
->first(['transaction_journals.*']);
|
||||
|
||||
/** @var null|Transaction $source */
|
||||
$source = $openingJournal->transactions()->where('amount', '<', 0)->first();
|
||||
$source = $openingJournal->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
/** @var null|Transaction $dest */
|
||||
$dest = $openingJournal->transactions()->where('amount', '>', 0)->first();
|
||||
$dest = $openingJournal->transactions()->where('amount', '>', 0)->first();
|
||||
if (null !== $source && null !== $dest) {
|
||||
$sourceId = $source->account_id;
|
||||
$destId = $dest->account_id;
|
||||
@@ -189,14 +184,16 @@ class UpgradesLiabilitiesEight extends Command
|
||||
{
|
||||
$count = 0;
|
||||
$journals = TransactionJournal::leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->where('transactions.account_id', $account->id)->get(['transaction_journals.*'])
|
||||
;
|
||||
->where('transactions.account_id', $account->id)->get(['transaction_journals.*']);
|
||||
|
||||
$service = app(TransactionGroupDestroyService::class);
|
||||
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$service = app(TransactionGroupDestroyService::class);
|
||||
$service->destroy($journal->transactionGroup);
|
||||
++$count;
|
||||
if (null !== $journal->transactionGroup) {
|
||||
$service->destroy($journal->transactionGroup);
|
||||
++$count;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
|
||||
@@ -167,11 +167,6 @@ class AccountRepository implements AccountRepositoryInterface, UserGroupInterfac
|
||||
return $account;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getAccountBalances(Account $account): Collection
|
||||
{
|
||||
return $account->accountBalances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return account type or null if not found.
|
||||
|
||||
@@ -71,7 +71,6 @@ interface AccountRepositoryInterface
|
||||
|
||||
public function findByName(string $name, array $types): ?Account;
|
||||
|
||||
public function getAccountBalances(Account $account): Collection;
|
||||
|
||||
public function getAccountCurrency(Account $account): ?TransactionCurrency;
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ class Balance
|
||||
{
|
||||
/**
|
||||
* Returns the accounts balances as an array, on the account ID.
|
||||
* @deprecated
|
||||
*/
|
||||
public function getAccountBalances(Collection $accounts, Carbon $date): array
|
||||
{
|
||||
|
||||
@@ -161,7 +161,7 @@ class AccountBalanceCalculator
|
||||
// then update all transactions.
|
||||
|
||||
// save all collected balances in their respective account objects.
|
||||
$this->storeAccountBalances($balances);
|
||||
// $this->storeAccountBalances($balances);
|
||||
}
|
||||
|
||||
private function storeAccountBalances(array $balances): void
|
||||
|
||||
Reference in New Issue
Block a user