. */ declare(strict_types=1); namespace App\Support\Internal; use App\Services\Session\Constants; use App\Services\Shared\Model\ImportServiceAccount; use GrumpyDictator\FFIIIApiSupport\Model\Account; use Illuminate\Support\Facades\Log; trait MergesAccountLists { protected function mergeNordigenAccountLists(array $nordigen, array $fireflyIII): array { Log::debug('Now merging GoCardless account lists.'); $generic = ImportServiceAccount::convertNordigenArray($nordigen); return $this->mergeGenericAccountList($generic, $fireflyIII); } private function mergeGenericAccountList(array $generic, array $fireflyIII): array { $return = []; /** @var ImportServiceAccount $account */ foreach ($generic as $account) { Log::debug(sprintf('Working on generic account name: "%s": id:"%s" (iban:"%s", number:"%s")', $account->name, $account->id, $account->iban, $account->bban)); $iban = $account->iban; $number = $account->bban; $currency = $account->currencyCode; $entry = [ 'import_account' => $account, ]; $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(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; continue; } 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. $entry['firefly_iii_accounts'] = $fireflyIII; $return[] = $entry; } return $return; } protected function filterByAccountNumber(array $firefly, 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 []; } $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; } } return $result; } protected function filterByCurrency(array $fireflyIII, string $currency): array { if ('' === $currency) { return []; } $result = []; $all = array_merge($fireflyIII[Constants::ASSET_ACCOUNTS] ?? [], $fireflyIII[Constants::LIABILITIES] ?? []); /** @var Account $account */ foreach ($all as $account) { if ($currency === $account->currencyCode) { $result[] = $account; } } return $result; } protected function mergeSpectreAccountLists(array $spectre, array $fireflyIII): array { Log::debug('Now merging Spectre account lists.'); $generic = ImportServiceAccount::convertSpectreArray($spectre); return $this->mergeGenericAccountList($generic, $fireflyIII); } }