mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 16:57:09 +00:00
Will update IBAN in existing account if necessary and/or possible.
This commit is contained in:
@@ -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']]);
|
||||
}
|
||||
}
|
||||
|
@@ -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),
|
||||
|
Reference in New Issue
Block a user