Skip to content

Firefly III: a personal finances manager

Fix #1564

James Cole566be8dc63c1
diff --git a/app/Import/Storage/ImportArrayStorage.php b/app/Import/Storage/ImportArrayStorage.php
index 0caabbaa1e..232f552b2f 100644
--- a/app/Import/Storage/ImportArrayStorage.php
+++ b/app/Import/Storage/ImportArrayStorage.php
@@ -160,7 +160,7 @@ class ImportArrayStorage
$array = $this->importJob->transactions;
$count = 0;
foreach ($array as $index => $transaction) {
- if (strtolower(TransactionType::TRANSFER) === $transaction['type']) {
+ if (strtolower(TransactionType::TRANSFER) === strtolower($transaction['type'])) {
$count++;
Log::debug(sprintf('Row #%d is a transfer, increase count to %d', $index + 1, $count));
}
@@ -479,7 +479,7 @@ class ImportArrayStorage
$collection->push($journal);
// add to collection of transfers, if necessary:
- if ('transfer' === $store['type']) {
+ if ('transfer' === strtolower($store['type'])) {
$transaction = $this->getTransactionFromJournal($journal);
Log::debug('We just stored a transfer, so add the journal to the list of transfers.');
$this->transfers->push($transaction);
@@ -505,7 +505,7 @@ class ImportArrayStorage
private function transferExists(array $transaction): bool
{
Log::debug('Check if is a double transfer.');
- if (strtolower(TransactionType::TRANSFER) !== $transaction['type']) {
+ if (strtolower(TransactionType::TRANSFER) !== strtolower($transaction['type'])) {
Log::debug(sprintf('Is a %s, not a transfer so no.', $transaction['type']));
return false;
diff --git a/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php b/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php
index 6a801deccc..4a357846e0 100644
--- a/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php
+++ b/app/Support/Import/JobConfiguration/Bunq/ChooseAccountsHandler.php
@@ -81,6 +81,17 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
$mapping = $data['account_mapping'] ?? [];
$applyRules = 1 === (int)$data['apply_rules'];
$final = [];
+
+ /*
+ * $ibanToAsset is used to map bunq IBAN's to Firefly III asset accounts. The array is structured like this:
+ * 12BUNQ123456.. => 1,
+ * 12BUNQ928811.. => 4,
+ *
+ * And contains the bunq asset account iban (left) and the FF3 asset ID (right).
+ *
+ * This is used to properly map transfers.
+ */
+ $ibanToAsset = [];
if (0 === \count($accounts)) {
throw new FireflyException('No bunq accounts found. Import cannot continue.'); // @codeCoverageIgnore
}
@@ -95,11 +106,16 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
$localId = (int)$localId;
// validate each
- $bunqId = $this->validBunqAccount($bunqId);
- $accountId = $this->validLocalAccount($localId);
+ $bunqId = $this->validBunqAccount($bunqId);
+ $accountId = $this->validLocalAccount($localId);
+ $bunqIban = $this->getBunqIban($bunqId);
+ if (null !== $bunqIban) {
+ $ibanToAsset[$bunqIban] = $accountId;
+ }
$final[$bunqId] = $accountId;
}
$config['mapping'] = $final;
+ $config['bunq-iban'] = $ibanToAsset;
$config['apply-rules'] = $applyRules;
$this->repository->setConfiguration($this->importJob, $config);
@@ -169,6 +185,25 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
$this->accountRepository->setUser($importJob->user);
}
+ /**
+ * @param int $bunqId
+ *
+ * @return null|string
+ */
+ private function getBunqIban(int $bunqId): ?string
+ {
+ $config = $this->repository->getConfiguration($this->importJob);
+ $accounts = $config['accounts'] ?? [];
+ /** @var array $bunqAccount */
+ foreach ($accounts as $bunqAccount) {
+ if ((int)$bunqAccount['id'] === $bunqId) {
+ return $bunqAccount['iban'] ?? null;
+ }
+ }
+
+ return null;
+ }
+
/**
* @param int $currencyId
*
diff --git a/app/Support/Import/Routine/Bunq/StageImportDataHandler.php b/app/Support/Import/Routine/Bunq/StageImportDataHandler.php
index cb3f2e2989..8581c9ddde 100644
--- a/app/Support/Import/Routine/Bunq/StageImportDataHandler.php
+++ b/app/Support/Import/Routine/Bunq/StageImportDataHandler.php
@@ -50,6 +50,8 @@ class StageImportDataHandler
private $accountRepository;
/** @var ImportJob */
private $importJob;
+ /** @var array */
+ private $jobConfiguration;
/** @var ImportJobRepositoryInterface */
private $repository;
/** @var array */
@@ -71,10 +73,11 @@ class StageImportDataHandler
public function run(): void
{
$this->getContext();
- $config = $this->repository->getConfiguration($this->importJob);
- $accounts = $config['accounts'] ?? [];
- $mapping = $config['mapping'] ?? [];
- $collection = [[]];
+ $config = $this->repository->getConfiguration($this->importJob);
+ $accounts = $config['accounts'] ?? [];
+ $mapping = $config['mapping'] ?? [];
+ $collection = [[]];
+ $this->jobConfiguration = $config;
/** @var array $bunqAccount */
foreach ($accounts as $bunqAccount) {
$bunqAccountId = $bunqAccount['id'] ?? 0;
@@ -192,9 +195,24 @@ class StageImportDataHandler
*/
private function convertToAccount(LabelMonetaryAccount $party, string $expectedType): LocalAccount
{
- Log::debug('in convertToAccount()');
+
+ Log::debug(sprintf('in convertToAccount() with LabelMonetaryAccount: %s', ''));
if (null !== $party->getIban()) {
- // find opposing party by IBAN first.
+ // find account in 'bunq-iban' array first.
+ $bunqIbans = $this->jobConfiguration['bunq-iban'] ?? [];
+ if (isset($bunqIbans[$party->getIban()])) {
+ $accountId = (int)$bunqIbans[$party->getIban()];
+ $result = $this->accountRepository->findNull($accountId);
+ if (null !== $result) {
+ Log::debug(
+ sprintf('Search for #%s (based on IBAN %s) resulted in account %s (#%d)', $accountId, $party->getIban(), $result->name, $result->id)
+ );
+
+ return $result;
+ }
+ }
+
+ // find opposing party by IBAN second.
$result = $this->accountRepository->findByIbanNull($party->getIban(), [$expectedType]);
if (null !== $result) {
Log::debug(sprintf('Search for %s resulted in account %s (#%d)', $party->getIban(), $result->name, $result->id));
diff --git a/app/Support/Import/Routine/Bunq/StageNewHandler.php b/app/Support/Import/Routine/Bunq/StageNewHandler.php
index b4e9a30743..08dc20dd4e 100644
--- a/app/Support/Import/Routine/Bunq/StageNewHandler.php
+++ b/app/Support/Import/Routine/Bunq/StageNewHandler.php
@@ -150,6 +150,7 @@ class StageNewHandler
'balance' => $mab->getBalance(),
'status' => $mab->getStatus(),
'type' => 'MonetaryAccountBank',
+ 'iban' => null,
'aliases' => [],
];
@@ -168,6 +169,11 @@ class StageNewHandler
'name' => $alias->getName(),
'value' => $alias->getValue(),
];
+
+ // store IBAN alias separately:
+ if ('IBAN' === $alias->getType()) {
+ $return['iban'] = $alias->getValue();
+ }
}
}