James Cole 3 months ago
parent
commit
7bc2610c54
  1. 1
      app/Http/Controllers/Import/ConfigurationController.php
  2. 86
      app/Support/Internal/MergesAccountLists.php
  3. 10
      resources/views/v2/components/importer-account.blade.php

1
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)

86
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;
}
}
}

10
resources/views/v2/components/importer-account.blade.php

@ -13,8 +13,12 @@
<!-- Firefly III Account Content - Visibility Controlled -->
<div id="firefly-account-content-{{ $account['import_account']->id }}">
<!-- TODO this is one of those things to merge into one generic type -->
@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) )
<span class="text-danger">There are no Firefly III accounts to import into</span>
@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) )
)
<span class="text-danger">X There are no Firefly III accounts to import into</span>
@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) )
<x-firefly-iii-account-generic :flow="$flow" :account="$account" :configuration="$configuration"/>
@ -24,7 +28,7 @@
<!-- Not Imported Text - Hidden by Default -->
<div id="not-imported-text-{{ $account['import_account']->id }}" style="display: none;" class="text-muted py-2">
<small><i class="fas fa-info-circle fa-sm me-1"></i>Not Imported</small>
<small><i class="fas fa-info-circle fa-sm me-1"></i>Not imported</small>
</div>
</td>
</tr>
Loading…
Cancel
Save