diff --git a/app/Http/Controllers/Import/ConfigurationController.php b/app/Http/Controllers/Import/ConfigurationController.php index 4107e22b..2e2cf276 100644 --- a/app/Http/Controllers/Import/ConfigurationController.php +++ b/app/Http/Controllers/Import/ConfigurationController.php @@ -98,6 +98,7 @@ class ConfigurationController extends Controller } // collect Firefly III accounts + // this function returns an array with keys 'assets' and 'liabilities', each containing an array of Firefly III accounts. $fireflyIIIaccounts = $this->getFireflyIIIAccounts(); // possibilities for duplicate detection (unique columns) diff --git a/app/Support/Internal/MergesAccountLists.php b/app/Support/Internal/MergesAccountLists.php index 5bd241af..ff057690 100644 --- a/app/Support/Internal/MergesAccountLists.php +++ b/app/Support/Internal/MergesAccountLists.php @@ -53,26 +53,39 @@ trait MergesAccountLists $currency = $account->currencyCode; $entry = [ 'import_account' => $account, + 'firefly_iii_accounts' => [ + Constants::ASSET_ACCOUNTS => [], + Constants::LIABILITIES => [], + ], ]; $filteredByNumber = $this->filterByAccountNumber($fireflyIII, $iban, $number); $filteredByCurrency = $this->filterByCurrency($fireflyIII, $currency); - - if (1 === count($filteredByNumber)) { - Log::debug(sprintf('Generic account ("%s", "%s") has a single FF3 counter part (#%d, "%s")', $iban, $number, $filteredByNumber[0]->id, $filteredByNumber[0]->name)); - $entry['firefly_iii_accounts'] = array_unique(array_merge($filteredByNumber, $filteredByCurrency), SORT_REGULAR); - $return[] = $entry; - - continue; + Log::debug('Filtered by number', $filteredByNumber); + Log::debug('Filtered by currency', $filteredByCurrency); + $count = 0; + foreach([Constants::ASSET_ACCOUNTS, Constants::LIABILITIES] as $key) { + if (1 === count($filteredByNumber[$key])) { + Log::debug(sprintf('Generic account ("%s", "%s") has a single FF3 %s counter part (#%d, "%s")', $iban, $number, $key, $filteredByNumber[$key][0]->id, $filteredByNumber[$key][0]->name)); + $entry['firefly_iii_accounts'][$key] = array_unique(array_merge( + $filteredByNumber[$key], + $filteredByCurrency[$key]), SORT_REGULAR); + $return[] = $entry; + $count++; + continue 2; + } } - Log::debug(sprintf('Found %d FF3 accounts with the same IBAN or number ("%s")', count($filteredByNumber), $iban)); - if (count($filteredByCurrency) > 0) { - Log::debug(sprintf('Generic account ("%s") has %d Firefly III counter part(s) with the same currency %s.', $account->name, count($filteredByCurrency), $currency)); - $entry['firefly_iii_accounts'] = $filteredByCurrency; - $return[] = $entry; + Log::debug(sprintf('Found %d FF3 accounts with the same IBAN or number ("%s")', $count, $iban)); + unset($count); - continue; + foreach([Constants::ASSET_ACCOUNTS, Constants::LIABILITIES] as $key) { + if (count($filteredByCurrency[$key]) > 0) { + Log::debug(sprintf('Generic account ("%s") has %d Firefly III %s counter part(s) with the same currency %s.', $account->name, $key, count($filteredByCurrency), $currency)); + $entry['firefly_iii_accounts'][$key] = $filteredByCurrency[$key]; + $return[] = $entry; + continue 2; + } } Log::debug('No special filtering on the Firefly III account list.'); // remove array_merge because SimpleFIN does not do this so it broke all the other importer routines. @@ -83,21 +96,26 @@ trait MergesAccountLists return $return; } - protected function filterByAccountNumber(array $firefly, string $iban, string $number): array + protected function filterByAccountNumber(array $fireflyIII, string $iban, string $number): array { Log::debug(sprintf('Now filtering Firefly III accounts by IBAN "%s" or number "%s".', $iban, $number)); // FIXME this check should also check the number of the account. if ('' === $iban) { - return []; + return [ + Constants::ASSET_ACCOUNTS => [], + Constants::LIABILITIES => [], + ]; } - $result = []; - // TODO check if this the correct merge type. - $all = array_merge($firefly[Constants::ASSET_ACCOUNTS] ?? [], $firefly[Constants::LIABILITIES] ?? []); - - /** @var Account $account */ - foreach ($all as $account) { - if ($iban === $account->iban || $number === $account->number || $iban === $account->number || $number === $account->iban) { - $result[] = $account; + $result = [ + Constants::ASSET_ACCOUNTS => [], + Constants::LIABILITIES => [], + ]; + + foreach($fireflyIII as $key => $accounts) { + foreach ($accounts as $account) { + if ($iban === $account->iban || $number === $account->number || $iban === $account->number || $number === $account->iban) { + $result[$key][] = $account; + } } } @@ -107,15 +125,21 @@ trait MergesAccountLists protected function filterByCurrency(array $fireflyIII, string $currency): array { if ('' === $currency) { - return []; + return [ + Constants::ASSET_ACCOUNTS => [], + Constants::LIABILITIES => [], + ]; } - $result = []; - $all = array_merge($fireflyIII[Constants::ASSET_ACCOUNTS] ?? [], $fireflyIII[Constants::LIABILITIES] ?? []); - - /** @var Account $account */ - foreach ($all as $account) { - if ($currency === $account->currencyCode) { - $result[] = $account; + $result = [ + Constants::ASSET_ACCOUNTS => [], + Constants::LIABILITIES => [], + ]; + + foreach($fireflyIII as $key => $accounts) { + foreach ($accounts as $account) { + if ($currency === $account->currencyCode) { + $result[$key][] = $account; + } } } diff --git a/resources/views/v2/components/importer-account.blade.php b/resources/views/v2/components/importer-account.blade.php index 5c702f0f..9bbbddd2 100644 --- a/resources/views/v2/components/importer-account.blade.php +++ b/resources/views/v2/components/importer-account.blade.php @@ -13,8 +13,12 @@
- @if( $flow !== 'simplefin' && (!isset($account['firefly_iii_accounts']['assets']) || count($account['firefly_iii_accounts']['assets']) === 0) && (!isset($account['firefly_iii_accounts']['liabilities']) || count($account['firefly_iii_accounts']['liabilities']) === 0) ) - There are no Firefly III accounts to import into + @if( + // flow is not simplefin. + $flow !== 'simplefin' && + ((!isset($account['firefly_iii_accounts']['assets']) || count($account['firefly_iii_accounts']['assets']) === 0) && (!isset($account['firefly_iii_accounts']['liabilities']) || count($account['firefly_iii_accounts']['liabilities']) === 0) ) + ) + X There are no Firefly III accounts to import into @endif @if( $flow === 'simplefin' || (isset($account['firefly_iii_accounts']['assets']) && count($account['firefly_iii_accounts']['assets']) > 0) || (isset($account['firefly_iii_accounts']['liabilities']) && count($account['firefly_iii_accounts']['liabilities']) > 0) ) @@ -24,7 +28,7 @@