2024-05-10 06:43:18 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2024-05-10 06:43:18 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
2025-02-15 16:51:13 +01:00
|
|
|
use FireflyIII\Enums\TransactionTypeEnum;
|
2025-02-15 15:44:21 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2025-02-15 16:51:13 +01:00
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2024-05-10 09:17:09 +02:00
|
|
|
use FireflyIII\Models\Account;
|
2025-02-15 15:44:21 +01:00
|
|
|
use FireflyIII\Models\AccountMeta;
|
2024-05-10 09:17:09 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
2025-02-15 16:51:13 +01:00
|
|
|
use FireflyIII\Models\Location;
|
|
|
|
use FireflyIII\Models\Note;
|
2024-07-31 20:19:17 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2025-02-15 15:44:21 +01:00
|
|
|
use FireflyIII\Models\UserGroup;
|
2024-07-31 20:19:17 +02:00
|
|
|
use FireflyIII\Support\Facades\Balance;
|
2025-02-15 16:51:13 +01:00
|
|
|
use FireflyIII\Support\Facades\Steam;
|
2025-02-15 15:44:21 +01:00
|
|
|
use FireflyIII\User;
|
2024-07-28 12:23:45 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-05-10 06:43:18 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
2024-07-26 18:50:41 +02:00
|
|
|
/**
|
|
|
|
* Class AccountEnrichment
|
|
|
|
*
|
|
|
|
* This class "enriches" accounts and adds data from other tables and models to each account model.
|
|
|
|
*/
|
2024-05-10 06:43:18 +02:00
|
|
|
class AccountEnrichment implements EnrichmentInterface
|
|
|
|
{
|
2025-02-17 04:11:50 +01:00
|
|
|
// private array $balances;
|
|
|
|
// private array $currencies;
|
|
|
|
// private CurrencyRepositoryInterface $currencyRepository;
|
|
|
|
// private TransactionCurrency $default;
|
|
|
|
// private ?Carbon $end;
|
|
|
|
// private array $grouped;
|
|
|
|
// private array $objectGroups;
|
|
|
|
// private AccountRepositoryInterface $repository;
|
|
|
|
// private ?Carbon $start;
|
2025-02-15 15:44:21 +01:00
|
|
|
|
|
|
|
private Collection $collection;
|
|
|
|
|
|
|
|
private bool $convertToNative;
|
|
|
|
private User $user;
|
|
|
|
private UserGroup $userGroup;
|
|
|
|
private TransactionCurrency $native;
|
|
|
|
private array $accountIds;
|
|
|
|
private array $accountTypeIds;
|
|
|
|
private array $accountTypes;
|
|
|
|
private array $currencies;
|
|
|
|
private array $meta;
|
2025-02-15 16:51:13 +01:00
|
|
|
private array $openingBalances;
|
|
|
|
private array $notes;
|
|
|
|
private array $locations;
|
2024-07-28 07:47:54 +02:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2025-02-15 15:44:21 +01:00
|
|
|
$this->convertToNative = false;
|
|
|
|
$this->accountIds = [];
|
2025-02-15 16:51:13 +01:00
|
|
|
$this->openingBalances = [];
|
2025-02-15 15:44:21 +01:00
|
|
|
$this->currencies = [];
|
|
|
|
$this->accountTypeIds = [];
|
|
|
|
$this->accountTypes = [];
|
|
|
|
$this->meta = [];
|
2025-02-15 16:51:13 +01:00
|
|
|
$this->notes = [];
|
|
|
|
$this->locations = [];
|
2025-02-17 04:11:50 +01:00
|
|
|
// $this->repository = app(AccountRepositoryInterface::class);
|
|
|
|
// $this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
|
|
|
// $this->start = null;
|
|
|
|
// $this->end = null;
|
2024-07-28 07:47:54 +02:00
|
|
|
}
|
|
|
|
|
2024-12-22 20:37:54 +01:00
|
|
|
#[\Override]
|
2025-02-17 04:11:50 +01:00
|
|
|
public function enrichSingle(array|Model $model): Account|array
|
2024-12-22 08:43:12 +01:00
|
|
|
{
|
|
|
|
Log::debug(__METHOD__);
|
|
|
|
$collection = new Collection([$model]);
|
|
|
|
$collection = $this->enrich($collection);
|
|
|
|
|
|
|
|
return $collection->first();
|
|
|
|
}
|
|
|
|
|
2024-12-22 20:37:54 +01:00
|
|
|
#[\Override]
|
2024-07-26 18:50:41 +02:00
|
|
|
/**
|
|
|
|
* Do the actual enrichment.
|
|
|
|
*/
|
2024-05-13 05:10:16 +02:00
|
|
|
public function enrich(Collection $collection): Collection
|
2024-05-10 06:43:18 +02:00
|
|
|
{
|
2024-07-26 18:50:41 +02:00
|
|
|
Log::debug(sprintf('Now doing account enrichment for %d account(s)', $collection->count()));
|
2025-02-15 15:44:21 +01:00
|
|
|
|
2024-07-26 18:50:41 +02:00
|
|
|
// prep local fields
|
2025-02-15 15:44:21 +01:00
|
|
|
$this->collection = $collection;
|
|
|
|
$this->collectAccountIds();
|
|
|
|
$this->getAccountTypes();
|
2024-05-10 09:17:09 +02:00
|
|
|
$this->collectMetaData();
|
2025-02-16 19:45:31 +01:00
|
|
|
$this->collectNotes();
|
2025-02-15 16:51:13 +01:00
|
|
|
$this->collectLocations();
|
|
|
|
$this->collectOpeningBalances();
|
2025-02-15 15:44:21 +01:00
|
|
|
$this->appendCollectedData();
|
|
|
|
|
2024-05-10 06:43:18 +02:00
|
|
|
return $this->collection;
|
|
|
|
}
|
|
|
|
|
2025-02-15 15:44:21 +01:00
|
|
|
private function getAccountTypes(): void
|
|
|
|
{
|
|
|
|
$types = AccountType::whereIn('id', $this->accountTypeIds)->get();
|
2025-02-17 04:11:50 +01:00
|
|
|
|
2025-02-15 15:44:21 +01:00
|
|
|
/** @var AccountType $type */
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$this->accountTypes[(int) $type->id] = $type->type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function collectAccountIds(): void
|
|
|
|
{
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($this->collection as $account) {
|
|
|
|
$this->accountIds[] = (int) $account->id;
|
|
|
|
$this->accountTypeIds[] = (int) $account->account_type_id;
|
|
|
|
}
|
|
|
|
$this->accountIds = array_unique($this->accountIds);
|
|
|
|
$this->accountTypeIds = array_unique($this->accountTypeIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function appendCollectedData(): void
|
|
|
|
{
|
|
|
|
$accountTypes = $this->accountTypes;
|
|
|
|
$meta = $this->meta;
|
2025-02-15 16:51:13 +01:00
|
|
|
$currencies = $this->currencies;
|
|
|
|
$notes = $this->notes;
|
|
|
|
$openingBalances = $this->openingBalances;
|
|
|
|
$locations = $this->locations;
|
|
|
|
$this->collection = $this->collection->map(function (Account $item) use ($accountTypes, $meta, $currencies, $notes, $openingBalances, $locations) {
|
2025-02-15 15:44:21 +01:00
|
|
|
$item->full_account_type = $accountTypes[(int) $item->account_type_id] ?? null;
|
2025-02-15 16:51:13 +01:00
|
|
|
$accountMeta = [
|
|
|
|
'currency' => null,
|
|
|
|
'location' => [
|
|
|
|
'latitude' => null,
|
|
|
|
'longitude' => null,
|
|
|
|
'zoom_level' => null,
|
|
|
|
],
|
|
|
|
];
|
2025-02-15 15:44:21 +01:00
|
|
|
if (array_key_exists((int) $item->id, $meta)) {
|
|
|
|
foreach ($meta[(int) $item->id] as $name => $value) {
|
2025-02-15 16:51:13 +01:00
|
|
|
$accountMeta[$name] = $value;
|
2025-02-15 15:44:21 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-15 16:51:13 +01:00
|
|
|
// also add currency, if present.
|
|
|
|
if (array_key_exists('currency_id', $accountMeta)) {
|
|
|
|
$currencyId = (int) $accountMeta['currency_id'];
|
|
|
|
$accountMeta['currency'] = $currencies[$currencyId];
|
|
|
|
}
|
|
|
|
|
|
|
|
// if notes, add notes.
|
|
|
|
if (array_key_exists($item->id, $notes)) {
|
|
|
|
$accountMeta['notes'] = $notes[$item->id];
|
|
|
|
}
|
|
|
|
// if opening balance, add opening balance
|
|
|
|
if (array_key_exists($item->id, $openingBalances)) {
|
|
|
|
$accountMeta['opening_balance_date'] = $openingBalances[$item->id]['date'];
|
|
|
|
$accountMeta['opening_balance_amount'] = $openingBalances[$item->id]['amount'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// if location, add location:
|
|
|
|
if (array_key_exists($item->id, $locations)) {
|
|
|
|
$accountMeta['location'] = $locations[$item->id];
|
|
|
|
}
|
2025-02-17 04:11:50 +01:00
|
|
|
$item->meta = $accountMeta;
|
2025-02-15 15:44:21 +01:00
|
|
|
|
|
|
|
return $item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-02-15 16:51:13 +01:00
|
|
|
private function collectOpeningBalances(): void
|
|
|
|
{
|
|
|
|
// use new group collector:
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setUser($this->user)->setAccounts($this->collection)
|
2025-02-17 04:11:50 +01:00
|
|
|
->withAccountInformation()
|
|
|
|
->setTypes([TransactionTypeEnum::OPENING_BALANCE->value])
|
|
|
|
;
|
|
|
|
$journals = $collector->getExtractedJournals();
|
2025-02-15 16:51:13 +01:00
|
|
|
foreach ($journals as $journal) {
|
|
|
|
$this->openingBalances[(int) $journal['source_account_id']]
|
|
|
|
= [
|
2025-02-17 04:11:50 +01:00
|
|
|
'amount' => Steam::negative($journal['amount']),
|
|
|
|
'date' => $journal['date'],
|
|
|
|
];
|
2025-02-15 16:51:13 +01:00
|
|
|
$this->openingBalances[(int) $journal['destination_account_id']]
|
|
|
|
= [
|
2025-02-17 04:11:50 +01:00
|
|
|
'amount' => Steam::positive($journal['amount']),
|
|
|
|
'date' => $journal['date'],
|
|
|
|
];
|
2025-02-15 16:51:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-17 04:11:50 +01:00
|
|
|
private function collectLocations(): void
|
|
|
|
{
|
2025-02-15 16:51:13 +01:00
|
|
|
$locations = Location::query()->whereIn('locatable_id', $this->accountIds)
|
2025-02-17 04:11:50 +01:00
|
|
|
->where('locatable_type', Account::class)->get(['locations.locatable_id', 'locations.latitude', 'locations.longitude', 'locations.zoom_level'])->toArray()
|
|
|
|
;
|
2025-02-15 16:51:13 +01:00
|
|
|
foreach ($locations as $location) {
|
|
|
|
$this->locations[(int) $location['locatable_id']]
|
|
|
|
= [
|
2025-02-17 04:11:50 +01:00
|
|
|
'latitude' => (float) $location['latitude'],
|
|
|
|
'longitude' => (float) $location['longitude'],
|
|
|
|
'zoom_level' => (int) $location['zoom_level'],
|
|
|
|
];
|
2025-02-15 16:51:13 +01:00
|
|
|
}
|
|
|
|
Log::debug(sprintf('Enrich with %d locations(s)', count($this->locations)));
|
|
|
|
}
|
|
|
|
|
2024-12-22 08:43:12 +01:00
|
|
|
private function collectMetaData(): void
|
|
|
|
{
|
2025-03-05 12:27:08 +01:00
|
|
|
$set = AccountMeta::whereIn('name', ['is_multi_currency', 'include_net_worth', 'currency_id', 'account_role', 'account_number', 'BIC', 'liability_direction', 'interest', 'interest_period', 'current_debt'])
|
2025-02-15 15:44:21 +01:00
|
|
|
->whereIn('account_id', $this->accountIds)
|
2025-02-17 04:11:50 +01:00
|
|
|
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])->toArray()
|
|
|
|
;
|
|
|
|
|
2025-02-15 15:44:21 +01:00
|
|
|
/** @var array $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
$this->meta[(int) $entry['account_id']][$entry['name']] = (string) $entry['data'];
|
|
|
|
if ('currency_id' === $entry['name']) {
|
|
|
|
$this->currencies[(int) $entry['data']] = true;
|
|
|
|
}
|
|
|
|
}
|
2025-02-20 08:11:36 +01:00
|
|
|
$currencies = TransactionCurrency::whereIn('id', array_keys($this->currencies))->get();
|
2025-02-15 15:44:21 +01:00
|
|
|
foreach ($currencies as $currency) {
|
|
|
|
$this->currencies[(int) $currency->id] = $currency;
|
|
|
|
}
|
2025-02-20 08:08:07 +01:00
|
|
|
$this->currencies[0] = $this->native;
|
2025-02-15 15:44:21 +01:00
|
|
|
foreach ($this->currencies as $id => $currency) {
|
2025-02-20 08:08:07 +01:00
|
|
|
if (true === $currency) {
|
2025-02-15 15:44:21 +01:00
|
|
|
throw new FireflyException(sprintf('Currency #%d not found.', $id));
|
|
|
|
}
|
|
|
|
}
|
2024-12-22 08:43:12 +01:00
|
|
|
}
|
2025-02-15 15:44:21 +01:00
|
|
|
|
|
|
|
public function setUserGroup(UserGroup $userGroup): void
|
|
|
|
{
|
|
|
|
$this->userGroup = $userGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->userGroup = $user->userGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setConvertToNative(bool $convertToNative): void
|
|
|
|
{
|
|
|
|
$this->convertToNative = $convertToNative;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setNative(TransactionCurrency $native): void
|
|
|
|
{
|
|
|
|
$this->native = $native;
|
|
|
|
}
|
|
|
|
|
2025-02-15 16:51:13 +01:00
|
|
|
private function collectNotes(): void
|
|
|
|
{
|
|
|
|
$notes = Note::query()->whereIn('noteable_id', $this->accountIds)
|
2025-02-17 04:11:50 +01:00
|
|
|
->whereNotNull('notes.text')
|
|
|
|
->where('notes.text', '!=', '')
|
|
|
|
->where('noteable_type', Account::class)->get(['notes.noteable_id', 'notes.text'])->toArray()
|
|
|
|
;
|
2025-02-15 16:51:13 +01:00
|
|
|
foreach ($notes as $note) {
|
|
|
|
$this->notes[(int) $note['noteable_id']] = (string) $note['text'];
|
|
|
|
}
|
|
|
|
Log::debug(sprintf('Enrich with %d note(s)', count($this->notes)));
|
|
|
|
}
|
2024-05-10 06:43:18 +02:00
|
|
|
}
|