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

This commit is contained in:
James Cole
2023-02-18 06:37:05 +01:00
parent 38b88dce44
commit e0577bddc5
4 changed files with 66 additions and 13 deletions

View File

@@ -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']]);
}
}

View File

@@ -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),