Skip to content

Firefly III: a personal finances manager

Will update IBAN in existing account if necessary and/or possible.

James Colee0577bddc5f6
diff --git a/app/Factory/TransactionFactory.php b/app/Factory/TransactionFactory.php
index d2781b3e18..ded41e19fb 100644
--- a/app/Factory/TransactionFactory.php
+++ b/app/Factory/TransactionFactory.php
@@ -29,9 +29,12 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Rules\UniqueIban;
+use FireflyIII\Services\Internal\Update\AccountUpdateService;
use FireflyIII\User;
use Illuminate\Database\QueryException;
use Log;
+use Validator;
/**
* Class TransactionFactory
@@ -43,6 +46,7 @@ class TransactionFactory
private ?TransactionCurrency $foreignCurrency;
private TransactionJournal $journal;
private bool $reconciled;
+ private array $accountInformation;
/**
* Constructor.
@@ -51,7 +55,8 @@ class TransactionFactory
*/
public function __construct()
{
- $this->reconciled = false;
+ $this->reconciled = false;
+ $this->accountInformation = [];
}
/**
@@ -129,6 +134,9 @@ class TransactionFactory
}
$result->save();
+ // if present, update account with relevant account information from the array
+ $this->updateAccountInformation();
+
return $result;
}
@@ -212,4 +220,45 @@ class TransactionFactory
{
// empty function.
}
+
+ /**
+ * @param array $accountInformation
+ */
+ public function setAccountInformation(array $accountInformation): void
+ {
+ $this->accountInformation = $accountInformation;
+ }
+
+
+ /**
+ * @return void
+ * @throws FireflyException
+ */
+ private function updateAccountInformation(): void
+ {
+ if (!array_key_exists('iban', $this->accountInformation)) {
+ Log::debug('No IBAN information in array, will not update.');
+ return;
+ }
+ if ('' !== (string)$this->account->iban) {
+ Log::debug('Account already has IBAN information, will not update.');
+ return;
+ }
+ if ($this->account->iban === $this->accountInformation['iban']) {
+ Log::debug('Account already has this IBAN, will not update.');
+ return;
+ }
+ // validate info:
+ $validator = Validator::make(['iban' => $this->accountInformation['iban']], [
+ 'iban' => ['required', new UniqueIban($this->account, $this->account->accountType->type)],
+ ]);
+ if ($validator->fails()) {
+ Log::debug('Invalid or non-unique IBAN, will not update.');
+ return;
+ }
+
+ Log::debug('Will update account with IBAN information.');
+ $service = app(AccountUpdateService::class);
+ $service->update($this->account, ['iban' => $this->accountInformation['iban']]);
+ }
}
diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php
index 9044d79525..4a324362f5 100644
--- a/app/Factory/TransactionJournalFactory.php
+++ b/app/Factory/TransactionJournalFactory.php
@@ -244,6 +244,7 @@ class TransactionJournalFactory
$transactionFactory->setJournal($journal);
$transactionFactory->setAccount($sourceAccount);
$transactionFactory->setCurrency($currency);
+ $transactionFactory->setAccountInformation($sourceInfo);
$transactionFactory->setForeignCurrency($foreignCurrency);
$transactionFactory->setReconciled($row['reconciled'] ?? false);
try {
@@ -262,6 +263,7 @@ class TransactionJournalFactory
$transactionFactory->setUser($this->user);
$transactionFactory->setJournal($journal);
$transactionFactory->setAccount($destinationAccount);
+ $transactionFactory->setAccountInformation($destInfo);
$transactionFactory->setCurrency($currency);
$transactionFactory->setForeignCurrency($foreignCurrency);
$transactionFactory->setReconciled($row['reconciled'] ?? false);
@@ -451,7 +453,7 @@ class TransactionJournalFactory
*/
private function getCurrencyByAccount(string $type, ?TransactionCurrency $currency, Account $source, Account $destination): TransactionCurrency
{
- Log::debug('Now ingetCurrencyByAccount()');
+ Log::debug('Now in getCurrencyByAccount()');
return match ($type) {
default => $this->getCurrency($currency, $source),
diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php
index 9807f7fdc0..3f103dd549 100644
--- a/app/Services/Internal/Support/AccountServiceTrait.php
+++ b/app/Services/Internal/Support/AccountServiceTrait.php
@@ -125,7 +125,7 @@ trait AccountServiceTrait
}
}
- // the account role may not be set in the data but we may have it already:
+ // the account role may not be set in the data, but we may have it already:
if (!array_key_exists('account_role', $data)) {
$data['account_role'] = null;
}
diff --git a/app/Services/Internal/Support/JournalServiceTrait.php b/app/Services/Internal/Support/JournalServiceTrait.php
index 6ac8f3b8c8..5f2c0bd247 100644
--- a/app/Services/Internal/Support/JournalServiceTrait.php
+++ b/app/Services/Internal/Support/JournalServiceTrait.php
@@ -54,7 +54,6 @@ trait JournalServiceTrait
* @param array $data
*
* @return Account|null
-
* @throws FireflyException
*/
protected function getAccount(string $transactionType, string $direction, array $data): ?Account
@@ -78,10 +77,11 @@ trait JournalServiceTrait
$result = $this->findAccountByNumber($result, $data, $expectedTypes[$transactionType]);
$numberResult = $result;
$result = $this->findAccountByName($result, $data, $expectedTypes[$transactionType]);
+ $nameResult =$result;
- // if result is NULL but IBAN is set, any result of the search by NAME can't overrule
+ // if $result (find by name) is NULL, but IBAN is set, any result of the search by NAME can't overrule
// this account. In such a case, the name search must be retried with a new name.
- if (null !== $result && null === $numberResult && null === $ibanResult && '' !== (string) $data['iban']) {
+ if (null !== $nameResult && null === $numberResult && null === $ibanResult && '' !== (string)$data['iban'] && '' !== (string) $nameResult->iban) {
$data['name'] = sprintf('%s (%s)', $data['name'], $data['iban']);
Log::debug(sprintf('Search again using the new name, "%s".', $data['name']));
$result = $this->findAccountByName(null, $data, $expectedTypes[$transactionType]);
@@ -101,11 +101,15 @@ trait JournalServiceTrait
$result = $this->createAccount(null, $tempData, $expectedTypes[$transactionType][0]);
}
}
-
- Log::debug('If nothing is found, create it.');
- $result = $this->createAccount($result, $data, $expectedTypes[$transactionType][0]);
- Log::debug('If cant be created, return cash account.');
- return $this->getCashAccount($result, $data, $expectedTypes[$transactionType]);
+ if (null === $result) {
+ Log::debug('If nothing is found, create it.');
+ $result = $this->createAccount($result, $data, $expectedTypes[$transactionType][0]);
+ }
+ if (null === $result) {
+ Log::debug('If cant be created, return cash account.');
+ $result = $this->getCashAccount($result, $data, $expectedTypes[$transactionType]);
+ }
+ return $result;
}
/**
@@ -329,7 +333,6 @@ trait JournalServiceTrait
*
* @return string
* @throws FireflyException
-
*/
protected function getAmount(string $amount): string
{
@@ -348,7 +351,6 @@ trait JournalServiceTrait
* @param string|null $amount
*
* @return string|null
-
*/
protected function getForeignAmount(?string $amount): ?string
{