repository = app(AccountRepositoryInterface::class); $this->currencyRepository = app(CurrencyRepositoryInterface::class); } #[\Override] /** * Do the actual enrichment. */ public function enrich(Collection $collection): Collection { Log::debug(sprintf('Now doing account enrichment for %d account(s)', $collection->count())); // prep local fields $this->collection = $collection; $this->currencies = []; // do everything here: $this->getLastActivity(); $this->collectAccountTypes(); $this->collectMetaData(); // $this->getMetaBalances(); // $this->collection->transform(function (Account $account) { // $account->user_array = ['id' => 1, 'bla bla' => 'bla']; // $account->balances = collect([ // ['balance_id' => 1, 'balance' => 5], // ['balance_id' => 2, 'balance' => 5], // ['balance_id' => 3, 'balance' => 5], // ]); // // return $account; // }); return $this->collection; } /** * TODO this method refers to a single-use method inside Steam that could be moved here. */ private function getLastActivity(): void { $lastActivity = $this->repository->getLastActivity($this->collection); foreach ($lastActivity as $row) { $this->collection->where('id', $row['account_id'])->first()->last_activity = Carbon::parse($row['date_max'], config('app.timezone')); } } /** * TODO this method refers to a single-use method inside Steam that could be moved here. */ private function getMetaBalances(): void { try { $array = app('steam')->balancesByAccountsConverted($this->collection, today()); } catch (FireflyException $e) { Log::error(sprintf('Could not load balances: %s', $e->getMessage())); return; } foreach ($array as $accountId => $row) { $this->collection->where('id', $accountId)->first()->balance = $row['balance']; $this->collection->where('id', $accountId)->first()->native_balance = $row['native_balance']; } } /** * TODO this method refers to a single-use method inside Steam that could be moved here. */ private function collectAccountTypes(): void { $accountTypes = $this->repository->getAccountTypes($this->collection); $types = []; /** @var AccountType $row */ foreach ($accountTypes as $row) { $types[$row->id] = $row->type; } $this->collection->transform(function (Account $account) use ($types) { $account->account_type_string = $types[$account->id]; return $account; }); } private function collectMetaData(): void { $metaFields = $this->repository->getMetaValues($this->collection, ['currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']); $currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray(); $currencies = []; foreach ($this->currencyRepository->getByIds($currencyIds) as $currency) { $id = $currency->id; $currencies[$id] = $currency; } $this->collection->transform(function (Account $account) use ($metaFields, $currencies) { $set = $metaFields->where('account_id', $account->id); foreach ($set as $entry) { $account->{$entry->name} = $entry->data; if ('currency_id' === $entry->name) { $id = (int) $entry->data; $account->currency_name = $currencies[$id]?->name; $account->currency_code = $currencies[$id]?->code; $account->currency_symbol = $currencies[$id]?->symbol; $account->currency_decimal_places = $currencies[$id]?->decimal_places; } } return $account; }); } }