From fb3402acd4f61efdc468a17056552ed8cac0e3a1 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 11 May 2025 08:00:38 +0200 Subject: [PATCH] Fix storage issue with transactions. --- app/Factory/TransactionJournalFactory.php | 200 +++++++++++------- app/Repositories/Bill/BillRepository.php | 2 +- .../Currency/CurrencyRepository.php | 54 ++--- .../PiggyBank/PiggyBankRepository.php | 2 +- .../Currency/CurrencyRepository.php | 46 ++-- app/Validation/AccountValidator.php | 2 +- app/Validation/TransactionValidation.php | 2 +- 7 files changed, 175 insertions(+), 133 deletions(-) diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index fe17fa054d..ec74a52d0c 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -25,6 +25,7 @@ declare(strict_types=1); namespace FireflyIII\Factory; use Carbon\Carbon; +use FireflyIII\Enums\AccountTypeEnum; use FireflyIII\Enums\TransactionTypeEnum; use FireflyIII\Exceptions\DuplicateTransactionException; use FireflyIII\Exceptions\FireflyException; @@ -101,15 +102,15 @@ class TransactionJournalFactory */ public function create(array $data): Collection { - app('log')->debug('Now in TransactionJournalFactory::create()'); + Log::debug('Now in TransactionJournalFactory::create()'); // convert to special object. - $dataObject = new NullArrayObject($data); + $dataObject = new NullArrayObject($data); - app('log')->debug('Start of TransactionJournalFactory::create()'); + Log::debug('Start of TransactionJournalFactory::create()'); $collection = new Collection(); $transactions = $dataObject['transactions'] ?? []; if (0 === count($transactions)) { - app('log')->error('There are no transactions in the array, the TransactionJournalFactory cannot continue.'); + Log::error('There are no transactions in the array, the TransactionJournalFactory cannot continue.'); return new Collection(); } @@ -117,26 +118,26 @@ class TransactionJournalFactory try { /** @var array $row */ foreach ($transactions as $index => $row) { - app('log')->debug(sprintf('Now creating journal %d/%d', $index + 1, count($transactions))); + Log::debug(sprintf('Now creating journal %d/%d', $index + 1, count($transactions))); $journal = $this->createJournal(new NullArrayObject($row)); if (null !== $journal) { $collection->push($journal); } if (null === $journal) { - app('log')->error('The createJournal() method returned NULL. This may indicate an error.'); + Log::error('The createJournal() method returned NULL. This may indicate an error.'); } } } catch (DuplicateTransactionException $e) { - app('log')->warning('TransactionJournalFactory::create() caught a duplicate journal in createJournal()'); - app('log')->error($e->getMessage()); - app('log')->error($e->getTraceAsString()); + Log::warning('TransactionJournalFactory::create() caught a duplicate journal in createJournal()'); + Log::error($e->getMessage()); + Log::error($e->getTraceAsString()); $this->forceDeleteOnError($collection); throw new DuplicateTransactionException($e->getMessage(), 0, $e); } catch (FireflyException $e) { - app('log')->warning('TransactionJournalFactory::create() caught an exception.'); - app('log')->error($e->getMessage()); - app('log')->error($e->getTraceAsString()); + Log::warning('TransactionJournalFactory::create() caught an exception.'); + Log::error($e->getMessage()); + Log::error($e->getTraceAsString()); $this->forceDeleteOnError($collection); throw new FireflyException($e->getMessage(), 0, $e); @@ -156,19 +157,23 @@ class TransactionJournalFactory */ private function createJournal(NullArrayObject $row): ?TransactionJournal { + Log::debug('Now in TransactionJournalFactory::createJournal()'); $row['import_hash_v2'] = $this->hashArray($row); $this->errorIfDuplicate($row['import_hash_v2']); // Some basic fields - $type = $this->typeRepository->findTransactionType(null, $row['type']); - $carbon = $row['date'] ?? today(config('app.timezone')); - $order = $row['order'] ?? 0; - $currency = $this->currencyRepository->findCurrency((int) $row['currency_id'], $row['currency_code']); - $foreignCurrency = $this->currencyRepository->findCurrencyNull($row['foreign_currency_id'], $row['foreign_currency_code']); - $bill = $this->billRepository->findBill((int) $row['bill_id'], $row['bill_name']); - $billId = TransactionTypeEnum::WITHDRAWAL->value === $type->type && null !== $bill ? $bill->id : null; - $description = (string) $row['description']; + $type = $this->typeRepository->findTransactionType(null, $row['type']); + $carbon = $row['date'] ?? today(config('app.timezone')); + $order = $row['order'] ?? 0; + + Log::debug('Find currency or return default.'); + $currency = $this->currencyRepository->findCurrency((int) $row['currency_id'], $row['currency_code']); + Log::debug('Find foreign currency or return NULL.'); + $foreignCurrency = $this->currencyRepository->findCurrencyNull($row['foreign_currency_id'], $row['foreign_currency_code']); + $bill = $this->billRepository->findBill((int) $row['bill_id'], $row['bill_name']); + $billId = TransactionTypeEnum::WITHDRAWAL->value === $type->type && null !== $bill ? $bill->id : null; + $description = (string) $row['description']; // Manipulate basic fields $carbon->setTimezone(config('app.timezone')); @@ -183,14 +188,14 @@ class TransactionJournalFactory // validate source and destination using a new Validator. $this->validateAccounts($row); } catch (FireflyException $e) { - app('log')->error('Could not validate source or destination.'); - app('log')->error($e->getMessage()); + Log::error('Could not validate source or destination.'); + Log::error($e->getMessage()); return null; } /** create or get source and destination accounts */ - $sourceInfo = [ + $sourceInfo = [ 'id' => $row['source_id'], 'name' => $row['source_name'], 'iban' => $row['source_iban'], @@ -199,7 +204,7 @@ class TransactionJournalFactory 'currency_id' => $currency->id, ]; - $destInfo = [ + $destInfo = [ 'id' => $row['destination_id'], 'name' => $row['destination_name'], 'iban' => $row['destination_iban'], @@ -207,26 +212,27 @@ class TransactionJournalFactory 'bic' => $row['destination_bic'], 'currency_id' => $currency->id, ]; - app('log')->debug('Source info:', $sourceInfo); - app('log')->debug('Destination info:', $destInfo); - $sourceAccount = $this->getAccount($type->type, 'source', $sourceInfo); - $destinationAccount = $this->getAccount($type->type, 'destination', $destInfo); - app('log')->debug('Done with getAccount(2x)'); + Log::debug('Source info:', $sourceInfo); + Log::debug('Destination info:', $destInfo); + $sourceAccount = $this->getAccount($type->type, 'source', $sourceInfo); + $destinationAccount = $this->getAccount($type->type, 'destination', $destInfo); + Log::debug('Done with getAccount(2x)'); // this is the moment for a reconciliation sanity check (again). if (TransactionTypeEnum::RECONCILIATION->value === $type->type) { [$sourceAccount, $destinationAccount] = $this->reconciliationSanityCheck($sourceAccount, $destinationAccount); } - $currency = $this->getCurrencyByAccount($type->type, $currency, $sourceAccount, $destinationAccount); - $foreignCurrency = $this->compareCurrencies($currency, $foreignCurrency); - $foreignCurrency = $this->getForeignByAccount($type->type, $foreignCurrency, $destinationAccount); - $description = $this->getDescription($description); + $currency = $this->getCurrencyByAccount($type->type, $currency, $sourceAccount, $destinationAccount); + $foreignCurrency = $this->compareCurrencies($currency, $foreignCurrency); + $foreignCurrency = $this->getForeignByAccount($type->type, $foreignCurrency, $destinationAccount); + $description = $this->getDescription($description); - app('log')->debug(sprintf('Date: %s (%s)', $carbon->toW3cString(), $carbon->getTimezone()->getName())); + Log::debug(sprintf('Currency is #%d "%s", foreign currency is #%d "%s"', $currency->id, $currency->code, $foreignCurrency?->id, $foreignCurrency)); + Log::debug(sprintf('Date: %s (%s)', $carbon->toW3cString(), $carbon->getTimezone()->getName())); /** Create a basic journal. */ - $journal = TransactionJournal::create( + $journal = TransactionJournal::create( [ 'user_id' => $this->user->id, 'user_group_id' => $this->userGroup->id, @@ -241,10 +247,10 @@ class TransactionJournalFactory 'completed' => 0, ] ); - app('log')->debug(sprintf('Created new journal #%d: "%s"', $journal->id, $journal->description)); + Log::debug(sprintf('Created new journal #%d: "%s"', $journal->id, $journal->description)); /** Create two transactions. */ - $transactionFactory = app(TransactionFactory::class); + $transactionFactory = app(TransactionFactory::class); $transactionFactory->setJournal($journal); $transactionFactory->setAccount($sourceAccount); $transactionFactory->setCurrency($currency); @@ -255,14 +261,14 @@ class TransactionJournalFactory try { $negative = $transactionFactory->createNegative((string) $row['amount'], (string) $row['foreign_amount']); } catch (FireflyException $e) { - app('log')->error(sprintf('Exception creating negative transaction: %s', $e->getMessage())); + Log::error(sprintf('Exception creating negative transaction: %s', $e->getMessage())); $this->forceDeleteOnError(new Collection([$journal])); throw new FireflyException($e->getMessage(), 0, $e); } /** @var TransactionFactory $transactionFactory */ - $transactionFactory = app(TransactionFactory::class); + $transactionFactory = app(TransactionFactory::class); $transactionFactory->setJournal($journal); $transactionFactory->setAccount($destinationAccount); $transactionFactory->setAccountInformation($destInfo); @@ -274,8 +280,8 @@ class TransactionJournalFactory // Firefly III will save the foreign currency information in such a way that both // asset accounts can look at the "amount" and "transaction_currency_id" column and // see the currency they expect to see. - $amount = (string) $row['amount']; - $foreignAmount = (string) $row['foreign_amount']; + $amount = (string) $row['amount']; + $foreignAmount = (string) $row['foreign_amount']; if (null !== $foreignCurrency && $foreignCurrency->id !== $currency->id && TransactionTypeEnum::TRANSFER->value === $type->type ) { @@ -289,13 +295,13 @@ class TransactionJournalFactory try { $transactionFactory->createPositive($amount, $foreignAmount); } catch (FireflyException $e) { - app('log')->error(sprintf('Exception creating positive transaction: %s', $e->getMessage())); + Log::error(sprintf('Exception creating positive transaction: %s', $e->getMessage())); $this->forceTrDelete($negative); $this->forceDeleteOnError(new Collection([$journal])); throw new FireflyException($e->getMessage(), 0, $e); } - $journal->completed = true; + $journal->completed = true; $journal->save(); $this->storeBudget($journal, $row); $this->storeCategory($journal, $row); @@ -317,11 +323,11 @@ class TransactionJournalFactory try { $json = \Safe\json_encode($dataRow, JSON_THROW_ON_ERROR); } catch (\JsonException $e) { - app('log')->error(sprintf('Could not encode dataRow: %s', $e->getMessage())); + Log::error(sprintf('Could not encode dataRow: %s', $e->getMessage())); $json = microtime(); } - $hash = hash('sha256', $json); - app('log')->debug(sprintf('The hash is: %s', $hash), $dataRow); + $hash = hash('sha256', $json); + Log::debug(sprintf('The hash is: %s', $hash), $dataRow); return $hash; } @@ -333,23 +339,22 @@ class TransactionJournalFactory */ private function errorIfDuplicate(string $hash): void { - app('log')->debug(sprintf('In errorIfDuplicate(%s)', $hash)); + Log::debug(sprintf('In errorIfDuplicate(%s)', $hash)); if (false === $this->errorOnHash) { return; } - app('log')->debug('Will verify duplicate!'); + Log::debug('Will verify duplicate!'); /** @var null|TransactionJournalMeta $result */ $result = TransactionJournalMeta::withTrashed() - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id') - ->whereNotNull('transaction_journals.id') - ->where('transaction_journals.user_id', $this->user->id) - ->where('data', \Safe\json_encode($hash, JSON_THROW_ON_ERROR)) - ->with(['transactionJournal', 'transactionJournal.transactionGroup']) - ->first(['journal_meta.*']) - ; + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id') + ->whereNotNull('transaction_journals.id') + ->where('transaction_journals.user_id', $this->user->id) + ->where('data', \Safe\json_encode($hash, JSON_THROW_ON_ERROR)) + ->with(['transactionJournal', 'transactionJournal.transactionGroup']) + ->first(['journal_meta.*']); if (null !== $result) { - app('log')->warning(sprintf('Found a duplicate in errorIfDuplicate because hash %s is not unique!', $hash)); + Log::warning(sprintf('Found a duplicate in errorIfDuplicate because hash %s is not unique!', $hash)); $journal = $result->transactionJournal()->withTrashed()->first(); $group = $journal?->transactionGroup()->withTrashed()->first(); $groupId = (int) $group?->id; @@ -363,28 +368,28 @@ class TransactionJournalFactory */ private function validateAccounts(NullArrayObject $data): void { - app('log')->debug(sprintf('Now in %s', __METHOD__)); - $transactionType = $data['type'] ?? 'invalid'; + Log::debug(sprintf('Now in %s', __METHOD__)); + $transactionType = $data['type'] ?? 'invalid'; $this->accountValidator->setUser($this->user); $this->accountValidator->setTransactionType($transactionType); // validate source account. - $array = [ + $array = [ 'id' => null !== $data['source_id'] ? (int) $data['source_id'] : null, 'name' => null !== $data['source_name'] ? (string) $data['source_name'] : null, 'iban' => null !== $data['source_iban'] ? (string) $data['source_iban'] : null, 'number' => null !== $data['source_number'] ? (string) $data['source_number'] : null, ]; - $validSource = $this->accountValidator->validateSource($array); + $validSource = $this->accountValidator->validateSource($array); // do something with result: if (false === $validSource) { throw new FireflyException(sprintf('Source: %s', $this->accountValidator->sourceError)); } - app('log')->debug('Source seems valid.'); + Log::debug('Source seems valid.'); // validate destination account - $array = [ + $array = [ 'id' => null !== $data['destination_id'] ? (int) $data['destination_id'] : null, 'name' => null !== $data['destination_name'] ? (string) $data['destination_name'] : null, 'iban' => null !== $data['destination_iban'] ? (string) $data['destination_iban'] : null, @@ -416,28 +421,28 @@ class TransactionJournalFactory private function reconciliationSanityCheck(?Account $sourceAccount, ?Account $destinationAccount): array { - app('log')->debug(sprintf('Now in %s', __METHOD__)); + Log::debug(sprintf('Now in %s', __METHOD__)); if (null !== $sourceAccount && null !== $destinationAccount) { - app('log')->debug('Both accounts exist, simply return them.'); + Log::debug('Both accounts exist, simply return them.'); return [$sourceAccount, $destinationAccount]; } if (null === $destinationAccount) { - app('log')->debug('Destination account is NULL, source account is not.'); + Log::debug('Destination account is NULL, source account is not.'); $account = $this->accountRepository->getReconciliation($sourceAccount); - app('log')->debug(sprintf('Will return account #%d ("%s") of type "%s"', $account->id, $account->name, $account->accountType->type)); + Log::debug(sprintf('Will return account #%d ("%s") of type "%s"', $account->id, $account->name, $account->accountType->type)); return [$sourceAccount, $account]; } if (null === $sourceAccount) { // @phpstan-ignore-line - app('log')->debug('Source account is NULL, destination account is not.'); + Log::debug('Source account is NULL, destination account is not.'); $account = $this->accountRepository->getReconciliation($destinationAccount); - app('log')->debug(sprintf('Will return account #%d ("%s") of type "%s"', $account->id, $account->name, $account->accountType->type)); + Log::debug(sprintf('Will return account #%d ("%s") of type "%s"', $account->id, $account->name, $account->accountType->type)); return [$account, $destinationAccount]; } - app('log')->debug('Unused fallback'); // @phpstan-ignore-line + Log::debug('Unused fallback'); // @phpstan-ignore-line return [$sourceAccount, $destinationAccount]; } @@ -447,7 +452,15 @@ class TransactionJournalFactory */ private function getCurrencyByAccount(string $type, ?TransactionCurrency $currency, Account $source, Account $destination): TransactionCurrency { - app('log')->debug('Now in getCurrencyByAccount()'); + Log::debug('Now in getCurrencyByAccount()'); + + /* + * Deze functie moet bij een transactie van liability naar asset wel degelijk de currency + * van de liability teruggeven en niet die van de destination. Fix voor #10265 + */ + if ($this->isBetweenAssetAndLiability($source, $destination) && TransactionTypeEnum::DEPOSIT->value === $type) { + return $this->getCurrency($currency, $source); + } return match ($type) { default => $this->getCurrency($currency, $source), @@ -460,7 +473,7 @@ class TransactionJournalFactory */ private function getCurrency(?TransactionCurrency $currency, Account $account): TransactionCurrency { - app('log')->debug('Now in getCurrency()'); + Log::debug(sprintf('Now in getCurrency(#%d, "%s")', $currency->id, $account->name)); /** @var null|TransactionCurrency $preference */ $preference = $this->accountRepository->getAccountCurrency($account); @@ -468,8 +481,8 @@ class TransactionJournalFactory // return user's default: return app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup); } - $result = $preference ?? $currency; - app('log')->debug(sprintf('Currency is now #%d (%s) because of account #%d (%s)', $result->id, $result->code, $account->id, $account->name)); + $result = $preference ?? $currency; + Log::debug(sprintf('Currency is now #%d (%s) because of account #%d (%s)', $result->id, $result->code, $account->id, $account->name)); return $result; } @@ -479,6 +492,7 @@ class TransactionJournalFactory */ private function compareCurrencies(?TransactionCurrency $currency, ?TransactionCurrency $foreignCurrency): ?TransactionCurrency { + Log::debug(sprintf('Now in compareCurrencies("%s", "%s")', $currency?->code, $foreignCurrency?->code)); if (null === $currency) { return null; } @@ -494,6 +508,7 @@ class TransactionJournalFactory */ private function getForeignByAccount(string $type, ?TransactionCurrency $foreignCurrency, Account $destination): ?TransactionCurrency { + Log::debug(sprintf('Now in getForeignByAccount("%s", #%d, "%s")', $type, $foreignCurrency?->id, $destination->name)); if (TransactionTypeEnum::TRANSFER->value === $type) { return $this->getCurrency($foreignCurrency, $destination); } @@ -514,12 +529,12 @@ class TransactionJournalFactory */ private function forceDeleteOnError(Collection $collection): void { - app('log')->debug(sprintf('forceDeleteOnError on collection size %d item(s)', $collection->count())); + Log::debug(sprintf('forceDeleteOnError on collection size %d item(s)', $collection->count())); $service = app(JournalDestroyService::class); /** @var TransactionJournal $journal */ foreach ($collection as $journal) { - app('log')->debug(sprintf('forceDeleteOnError on journal #%d', $journal->id)); + Log::debug(sprintf('forceDeleteOnError on journal #%d', $journal->id)); $service->destroy($journal); } } @@ -534,17 +549,17 @@ class TransactionJournalFactory */ private function storePiggyEvent(TransactionJournal $journal, NullArrayObject $data): void { - app('log')->debug('Will now store piggy event.'); + Log::debug('Will now store piggy event.'); $piggyBank = $this->piggyRepository->findPiggyBank((int) $data['piggy_bank_id'], $data['piggy_bank_name']); if (null !== $piggyBank) { $this->piggyEventFactory->create($journal, $piggyBank); - app('log')->debug('Create piggy event.'); + Log::debug('Create piggy event.'); return; } - app('log')->debug('Create no piggy event'); + Log::debug('Create no piggy event'); } private function storeMetaFields(TransactionJournal $journal, NullArrayObject $transaction): void @@ -556,18 +571,18 @@ class TransactionJournalFactory protected function storeMeta(TransactionJournal $journal, NullArrayObject $data, string $field): void { - $set = [ + $set = [ 'journal' => $journal, 'name' => $field, 'data' => (string) ($data[$field] ?? ''), ]; if ($data[$field] instanceof Carbon) { $data[$field]->setTimezone(config('app.timezone')); - app('log')->debug(sprintf('%s Date: %s (%s)', $field, $data[$field], $data[$field]->timezone->getName())); + Log::debug(sprintf('%s Date: %s (%s)', $field, $data[$field], $data[$field]->timezone->getName())); $set['data'] = $data[$field]->format('Y-m-d H:i:s'); } - app('log')->debug(sprintf('Going to store meta-field "%s", with value "%s".', $set['name'], $set['data'])); + Log::debug(sprintf('Going to store meta-field "%s", with value "%s".', $set['name'], $set['data'])); /** @var TransactionJournalMetaFactory $factory */ $factory = app(TransactionJournalMetaFactory::class); @@ -590,7 +605,7 @@ class TransactionJournalFactory { $this->errorOnHash = $errorOnHash; if (true === $errorOnHash) { - app('log')->info('Will trigger duplication alert for this journal.'); + Log::info('Will trigger duplication alert for this journal.'); } } @@ -605,4 +620,25 @@ class TransactionJournalFactory $this->piggyRepository->setUserGroup($userGroup); $this->accountRepository->setUserGroup($userGroup); } + + private function isBetweenAssetAndLiability(Account $source, Account $destination): bool + { + if (null === $source || null === $destination) { + Log::warning('Either is false, stop.'); + return false; + } + $sourceTypes = [AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value]; + + // source is liability, destination is asset + if (in_array($source->accountType->type, $sourceTypes, true) && AccountTypeEnum::ASSET->value === $destination->accountType->type) { + Log::debug('Source is a liability account, destination is an asset account, return TRUE.'); + return true; + } + // source is asset, destination is liability + if (in_array($destination->accountType->type, $sourceTypes, true) && AccountTypeEnum::ASSET->value === $source->accountType->type) { + Log::debug('Destination is a liability account, source is an asset account, return TRUE.'); + return true; + } + return false; + } } diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 56094d1199..c08acbad8e 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -133,7 +133,7 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface return $searchResult; } } - app('log')->debug('Found nothing'); + app('log')->debug('Found no bill in findBill()'); return null; } diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php index c6262f2b04..9676766b6c 100644 --- a/app/Repositories/Currency/CurrencyRepository.php +++ b/app/Repositories/Currency/CurrencyRepository.php @@ -65,17 +65,17 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf */ public function currencyInUseAt(TransactionCurrency $currency): ?string { - app('log')->debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code)); + Log::debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code)); $countJournals = $this->countJournals($currency); if ($countJournals > 0) { - app('log')->info(sprintf('Count journals is %d, return true.', $countJournals)); + Log::info(sprintf('Count journals is %d, return true.', $countJournals)); return 'journals'; } // is the only currency left if (1 === $this->getAll()->count()) { - app('log')->info('Is the last currency in the system, return true. '); + Log::info('Is the last currency in the system, return true. '); return 'last_left'; } @@ -83,7 +83,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // is being used in accounts: $meta = AccountMeta::where('name', 'currency_id')->where('data', \Safe\json_encode((string) $currency->id))->count(); if ($meta > 0) { - app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); + Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); return 'account_meta'; } @@ -91,7 +91,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // second search using integer check. $meta = AccountMeta::where('name', 'currency_id')->where('data', \Safe\json_encode((int) $currency->id))->count(); if ($meta > 0) { - app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); + Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); return 'account_meta'; } @@ -99,7 +99,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // is being used in bills: $bills = Bill::where('transaction_currency_id', $currency->id)->count(); if ($bills > 0) { - app('log')->info(sprintf('Used in %d bills as currency, return true. ', $bills)); + Log::info(sprintf('Used in %d bills as currency, return true. ', $bills)); return 'bills'; } @@ -109,7 +109,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf $recurringForeign = RecurrenceTransaction::where('foreign_currency_id', $currency->id)->count(); if ($recurringAmount > 0 || $recurringForeign > 0) { - app('log')->info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign)); + Log::info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign)); return 'recurring'; } @@ -120,7 +120,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf ->where('account_meta.name', 'currency_id')->where('account_meta.data', \Safe\json_encode($currency->id))->count() ; if ($meta > 0) { - app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); + Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); return 'account_meta'; } @@ -128,7 +128,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // is being used in available budgets $availableBudgets = AvailableBudget::where('transaction_currency_id', $currency->id)->count(); if ($availableBudgets > 0) { - app('log')->info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets)); + Log::info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets)); return 'available_budgets'; } @@ -136,7 +136,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // is being used in budget limits $budgetLimit = BudgetLimit::where('transaction_currency_id', $currency->id)->count(); if ($budgetLimit > 0) { - app('log')->info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit)); + Log::info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit)); return 'budget_limits'; } @@ -144,7 +144,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // is the default currency for the user or the system $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); if ($count > 0) { - app('log')->info('Is the default currency of the user, return true.'); + Log::info('Is the default currency of the user, return true.'); return 'current_default'; } @@ -152,12 +152,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // is the default currency for the user or the system $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); if ($count > 0) { - app('log')->info('Is the default currency of the user group, return true.'); + Log::info('Is the default currency of the user group, return true.'); return 'current_default'; } - app('log')->debug('Currency is not used, return false.'); + Log::debug('Currency is not used, return false.'); return null; } @@ -237,15 +237,15 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf $result = $this->findCurrencyNull($currencyId, $currencyCode); if (null === $result) { - app('log')->debug('Grabbing default currency for this user...'); + Log::debug('Grabbing default currency for this user...'); /** @var null|TransactionCurrency $result */ $result = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup); } - app('log')->debug(sprintf('Final result: %s', $result->code)); + Log::debug(sprintf('Final result: %s', $result->code)); if (false === $result->enabled) { - app('log')->debug(sprintf('Also enabled currency %s', $result->code)); + Log::debug(sprintf('Also enabled currency %s', $result->code)); $this->enable($result); } @@ -257,16 +257,22 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf */ public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency { - app('log')->debug('Now in findCurrencyNull()'); + Log::debug(sprintf('Now in findCurrencyNull(%s, "%s")', var_export($currencyId, true), $currencyCode)); $result = $this->find((int) $currencyId); + if (null !== $result) { + Log::debug(sprintf('Found currency by ID: %s', $result->code)); + + return $result; + } if (null === $result) { - app('log')->debug(sprintf('Searching for currency with code %s...', $currencyCode)); + Log::debug(sprintf('Searching for currency with code "%s"...', $currencyCode)); $result = $this->findByCode((string) $currencyCode); } if (null !== $result && false === $result->enabled) { - app('log')->debug(sprintf('Also enabled currency %s', $result->code)); + Log::debug(sprintf('Also enabled currency %s', $result->code)); $this->enable($result); } + Log::debug('Found no currency, returning NULL.'); return $result; } @@ -321,7 +327,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf ->where('date', $date->format('Y-m-d'))->first() ; if (null !== $rate) { - app('log')->debug(sprintf('Found cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d'))); + Log::debug(sprintf('Found cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d'))); return $rate; } @@ -380,7 +386,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf public function update(TransactionCurrency $currency, array $data): TransactionCurrency { - app('log')->debug('Now in update()'); + Log::debug('Now in update()'); // can be true, false, null $enabled = array_key_exists('enabled', $data) ? $data['enabled'] : null; // can be true, false, but method only responds to "true". @@ -396,12 +402,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf // currency is enabled, must be disabled. if (false === $enabled) { - app('log')->debug(sprintf('Disabled currency %s for user #%d', $currency->code, $this->userGroup->id)); + Log::debug(sprintf('Disabled currency %s for user #%d', $currency->code, $this->userGroup->id)); $this->userGroup->currencies()->detach($currency->id); } // currency must be enabled if (true === $enabled) { - app('log')->debug(sprintf('Enabled currency %s for user #%d', $currency->code, $this->userGroup->id)); + Log::debug(sprintf('Enabled currency %s for user #%d', $currency->code, $this->userGroup->id)); $this->userGroup->currencies()->detach($currency->id); $this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => false]]); } @@ -420,7 +426,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf public function makeDefault(TransactionCurrency $currency): void { $current = app('amount')->getNativeCurrencyByUserGroup($this->userGroup); - app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id)); + Log::debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id)); $this->userGroup->currencies()->detach($currency->id); foreach ($this->userGroup->currencies()->get() as $item) { $this->userGroup->currencies()->updateExistingPivot($item->id, ['group_default' => false]); diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php index 4118bee399..9296d3c977 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepository.php +++ b/app/Repositories/PiggyBank/PiggyBankRepository.php @@ -82,7 +82,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte return $searchResult; } } - app('log')->debug('Found nothing'); + app('log')->debug('Found no piggy bank.'); return null; } diff --git a/app/Repositories/UserGroups/Currency/CurrencyRepository.php b/app/Repositories/UserGroups/Currency/CurrencyRepository.php index 813bdad772..092ef4b50c 100644 --- a/app/Repositories/UserGroups/Currency/CurrencyRepository.php +++ b/app/Repositories/UserGroups/Currency/CurrencyRepository.php @@ -65,17 +65,17 @@ class CurrencyRepository implements CurrencyRepositoryInterface */ public function currencyInUseAt(TransactionCurrency $currency): ?string { - app('log')->debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code)); + Log::debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code)); $countJournals = $this->countJournals($currency); if ($countJournals > 0) { - app('log')->info(sprintf('Count journals is %d, return true.', $countJournals)); + Log::info(sprintf('Count journals is %d, return true.', $countJournals)); return 'journals'; } // is the only currency left if (1 === $this->getAll()->count()) { - app('log')->info('Is the last currency in the system, return true. '); + Log::info('Is the last currency in the system, return true. '); return 'last_left'; } @@ -83,7 +83,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface // is being used in accounts: $meta = AccountMeta::where('name', 'currency_id')->where('data', \Safe\json_encode((string) $currency->id))->count(); if ($meta > 0) { - app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); + Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); return 'account_meta'; } @@ -91,7 +91,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface // second search using integer check. $meta = AccountMeta::where('name', 'currency_id')->where('data', \Safe\json_encode((int) $currency->id))->count(); if ($meta > 0) { - app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); + Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); return 'account_meta'; } @@ -99,7 +99,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface // is being used in bills: $bills = Bill::where('transaction_currency_id', $currency->id)->count(); if ($bills > 0) { - app('log')->info(sprintf('Used in %d bills as currency, return true. ', $bills)); + Log::info(sprintf('Used in %d bills as currency, return true. ', $bills)); return 'bills'; } @@ -109,7 +109,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface $recurringForeign = RecurrenceTransaction::where('foreign_currency_id', $currency->id)->count(); if ($recurringAmount > 0 || $recurringForeign > 0) { - app('log')->info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign)); + Log::info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign)); return 'recurring'; } @@ -120,7 +120,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface ->where('account_meta.name', 'currency_id')->where('account_meta.data', \Safe\json_encode($currency->id))->count() ; if ($meta > 0) { - app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); + Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); return 'account_meta'; } @@ -128,7 +128,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface // is being used in available budgets $availableBudgets = AvailableBudget::where('transaction_currency_id', $currency->id)->count(); if ($availableBudgets > 0) { - app('log')->info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets)); + Log::info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets)); return 'available_budgets'; } @@ -136,7 +136,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface // is being used in budget limits $budgetLimit = BudgetLimit::where('transaction_currency_id', $currency->id)->count(); if ($budgetLimit > 0) { - app('log')->info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit)); + Log::info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit)); return 'budget_limits'; } @@ -144,7 +144,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface // is the default currency for the user or the system $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); if ($count > 0) { - app('log')->info('Is the default currency of the user, return true.'); + Log::info('Is the default currency of the user, return true.'); return 'current_default'; } @@ -152,12 +152,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface // is the default currency for the user or the system $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); if ($count > 0) { - app('log')->info('Is the default currency of the user group, return true.'); + Log::info('Is the default currency of the user group, return true.'); return 'current_default'; } - app('log')->debug('Currency is not used, return false.'); + Log::debug('Currency is not used, return false.'); return null; } @@ -240,15 +240,15 @@ class CurrencyRepository implements CurrencyRepositoryInterface $result = $this->findCurrencyNull($currencyId, $currencyCode); if (null === $result) { - app('log')->debug('Grabbing default currency for this user...'); + Log::debug('Grabbing default currency for this user...'); /** @var null|TransactionCurrency $result */ $result = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup); } - app('log')->debug(sprintf('Final result: %s', $result->code)); + Log::debug(sprintf('Final result: %s', $result->code)); if (false === $result->enabled) { - app('log')->debug(sprintf('Also enabled currency %s', $result->code)); + Log::debug(sprintf('Also enabled currency %s', $result->code)); $this->enable($result); } @@ -260,14 +260,14 @@ class CurrencyRepository implements CurrencyRepositoryInterface */ public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency { - app('log')->debug('Now in findCurrencyNull()'); + Log::debug(sprintf('Now in findCurrencyNull("%s", "%s")', $currencyId, $currencyCode)); $result = $this->find((int) $currencyId); if (null === $result) { - app('log')->debug(sprintf('Searching for currency with code %s...', $currencyCode)); + Log::debug(sprintf('Searching for currency with code "%s"...', $currencyCode)); $result = $this->findByCode((string) $currencyCode); } if (null !== $result && false === $result->enabled) { - app('log')->debug(sprintf('Also enabled currency %s', $result->code)); + Log::debug(sprintf('Also enabled currency %s', $result->code)); $this->enable($result); } @@ -335,7 +335,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface public function update(TransactionCurrency $currency, array $data): TransactionCurrency { - app('log')->debug('Now in update()'); + Log::debug('Now in update()'); // can be true, false, null $enabled = array_key_exists('enabled', $data) ? $data['enabled'] : null; // can be true, false, but method only responds to "true". @@ -351,12 +351,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface // currency is enabled, must be disabled. if (false === $enabled) { - app('log')->debug(sprintf('Disabled currency %s for user #%d', $currency->code, $this->userGroup->id)); + Log::debug(sprintf('Disabled currency %s for user #%d', $currency->code, $this->userGroup->id)); $this->userGroup->currencies()->detach($currency->id); } // currency must be enabled if (true === $enabled) { - app('log')->debug(sprintf('Enabled currency %s for user #%d', $currency->code, $this->userGroup->id)); + Log::debug(sprintf('Enabled currency %s for user #%d', $currency->code, $this->userGroup->id)); $this->userGroup->currencies()->detach($currency->id); $this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => false]]); } @@ -375,7 +375,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface public function makeDefault(TransactionCurrency $currency): void { $current = app('amount')->getNativeCurrencyByUserGroup($this->userGroup); - app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id)); + Log::debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id)); $this->userGroup->currencies()->detach($currency->id); foreach ($this->userGroup->currencies()->get() as $item) { $this->userGroup->currencies()->updateExistingPivot($item->id, ['group_default' => false]); diff --git a/app/Validation/AccountValidator.php b/app/Validation/AccountValidator.php index 456d0df50b..d6de97ed25 100644 --- a/app/Validation/AccountValidator.php +++ b/app/Validation/AccountValidator.php @@ -305,7 +305,7 @@ class AccountValidator return $first; } } - app('log')->debug('Found nothing!'); + app('log')->debug('Found nothing in findExistingAccount()'); return null; } diff --git a/app/Validation/TransactionValidation.php b/app/Validation/TransactionValidation.php index c6e4d09496..e1b7c573af 100644 --- a/app/Validation/TransactionValidation.php +++ b/app/Validation/TransactionValidation.php @@ -214,7 +214,7 @@ trait TransactionValidation $destination = $accountValidator->destination; Log::debug(sprintf('Source: #%d "%s (%s)"', $source->id, $source->name, $source->accountType->type)); - Log::debug(sprintf('Destination: #%d "%s" (%s)', $destination->id, $destination->name, $source->accountType->type)); + Log::debug(sprintf('Destination: #%d "%s" (%s)', $destination->id, $destination->name, $destination->accountType->type)); if (!$this->isLiabilityOrAsset($source) || !$this->isLiabilityOrAsset($destination)) { Log::debug('Any account must be liability or asset account to continue.');