Files
firefly-iii/app/Support/JsonApi/Enrichments/AccountEnrichment.php

237 lines
8.8 KiB
PHP
Raw Normal View History

<?php
/*
* AccountEnricher.php
* Copyright (c) 2024 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
declare(strict_types=1);
namespace FireflyIII\Support\JsonApi\Enrichments;
use Carbon\Carbon;
2024-05-10 09:17:09 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2024-07-31 20:19:17 +02:00
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
2024-05-10 09:17:09 +02:00
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
2024-07-31 20:19:17 +02:00
use FireflyIII\Support\Facades\Balance;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
2024-07-28 12:23:45 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/**
* Class AccountEnrichment
*
* This class "enriches" accounts and adds data from other tables and models to each account model.
*/
class AccountEnrichment implements EnrichmentInterface
{
2024-07-31 20:19:17 +02:00
private Collection $collection;
private array $currencies;
private array $balances;
private TransactionCurrency $default;
private ?Carbon $start;
private ?Carbon $end;
2024-05-10 09:17:09 +02:00
2024-07-28 07:47:54 +02:00
private AccountRepositoryInterface $repository;
private CurrencyRepositoryInterface $currencyRepository;
public function __construct()
{
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
2024-07-31 20:19:17 +02:00
$this->start = null;
$this->end = null;
2024-07-28 07:47:54 +02:00
}
#[\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
2024-05-10 09:17:09 +02:00
$this->collection = $collection;
2024-07-31 20:19:17 +02:00
$this->default = app('amount')->getDefaultCurrency();
2024-05-10 09:17:09 +02:00
$this->currencies = [];
2024-07-31 20:19:17 +02:00
$this->balances = [];
// do everything here:
$this->getLastActivity();
2024-05-10 09:17:09 +02:00
$this->collectAccountTypes();
$this->collectMetaData();
2024-07-31 20:19:17 +02:00
$this->getMetaBalances();
2024-05-10 09:17:09 +02:00
// $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
{
2024-07-28 07:47:54 +02:00
$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'));
}
}
private function getMetaBalances(): void
{
2024-07-31 20:19:17 +02:00
$this->balances = Balance::getAccountBalances($this->collection, today());
$balances = $this->balances;
$default = $this->default;
// get start and end, so the balance difference can be generated.
$start = null;
$end = null;
if(null !== $this->start) {
$start = Balance::getAccountBalances($this->collection, $this->start);
}
2024-07-31 20:19:17 +02:00
if(null !== $this->end) {
$end = Balance::getAccountBalances($this->collection, $this->end);
}
2024-07-31 20:19:17 +02:00
$this->collection->transform(function (Account $account) use ($balances, $default) {
$converter = new ExchangeRateConverter();
$native = [
'currency_id' => $this->default->id,
'currency_name' => $this->default->name,
'currency_code' => $this->default->code,
'currency_symbol' => $this->default->symbol,
'currency_decimal_places' => $this->default->decimal_places,
'balance' => '0',
];
if (array_key_exists($account->id, $balances)) {
$set = [];
foreach ($balances[$account->id] as $entry) {
$set[] = [
'currency_id' => $entry['currency']->id,
'currency_name' => $entry['currency']->name,
'currency_code' => $entry['currency']->code,
'currency_symbol' => $entry['currency']->symbol,
'currency_decimal_places' => $entry['currency']->decimal_places,
'balance' => $entry['balance'],
];
$native['balance'] = bcadd($native['balance'], $converter->convert($entry['currency'], $default, today(), $entry['balance']));
}
$account->balance = $set;
$account->native_balance = $native;
}
return $account;
});
// 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'];
// }
}
2024-05-10 09:17:09 +02:00
/**
* TODO this method refers to a single-use method inside Steam that could be moved here.
*/
private function collectAccountTypes(): void
{
2024-07-28 07:47:54 +02:00
$accountTypes = $this->repository->getAccountTypes($this->collection);
$types = [];
2024-05-10 09:17:09 +02:00
/** @var AccountType $row */
foreach ($accountTypes as $row) {
$types[$row->id] = $row->type;
}
$this->collection->transform(function (Account $account) use ($types) {
2024-07-28 07:47:54 +02:00
$account->account_type_string = $types[$account->id];
2024-05-10 09:17:09 +02:00
return $account;
});
}
private function collectMetaData(): void
{
2024-07-31 20:19:17 +02:00
$metaFields = $this->repository->getMetaValues($this->collection, ['is_multi_currency', 'currency_id', 'account_role', 'account_number', 'liability_direction', 'interest', 'interest_period', 'current_debt']);
2024-07-28 07:47:54 +02:00
$currencyIds = $metaFields->where('name', 'currency_id')->pluck('data')->toArray();
2024-05-10 09:17:09 +02:00
2024-07-31 20:19:17 +02:00
$currencies = [];
2024-07-28 07:47:54 +02:00
foreach ($this->currencyRepository->getByIds($currencyIds) as $currency) {
2024-05-10 09:17:09 +02:00
$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;
2024-07-28 07:47:54 +02:00
$account->currency_name = $currencies[$id]?->name;
2024-05-10 09:17:09 +02:00
$account->currency_code = $currencies[$id]?->code;
$account->currency_symbol = $currencies[$id]?->symbol;
$account->currency_decimal_places = $currencies[$id]?->decimal_places;
}
}
2024-05-10 09:17:09 +02:00
return $account;
});
}
2024-07-28 12:23:45 +02:00
#[\Override]
public function enrichSingle(Model $model): Model
2024-07-28 12:23:45 +02:00
{
Log::debug(__METHOD__);
$collection = new Collection([$model]);
$collection = $this->enrich($collection);
2024-07-28 12:23:45 +02:00
return $collection->first();
}
2024-07-31 20:19:17 +02:00
public function setStart(?Carbon $start): void
{
$this->start = $start;
}
public function setEnd(?Carbon $end): void
{
$this->end = $end;
}
}