Files
firefly-iii/app/Helpers/Report/NetWorth.php

204 lines
8.0 KiB
PHP
Raw Normal View History

<?php
/**
* NetWorth.php
2020-01-28 08:46:01 +01:00
* Copyright (c) 2019 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\Helpers\Report;
use Carbon\Carbon;
2022-06-06 17:39:50 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
2022-06-06 17:39:50 +02:00
use FireflyIII\Models\AccountType;
2023-08-06 11:22:36 +02:00
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface;
2023-10-28 15:03:33 +02:00
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2024-12-24 06:34:12 +01:00
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Steam;
use FireflyIII\User;
2023-02-22 19:54:19 +01:00
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
2023-12-29 08:21:14 +01:00
use Illuminate\Support\Facades\Log;
/**
* This class can handle both request with and without a user group and will return the appropriate repository when
* necessary.
*
* Class NetWorth
*/
class NetWorth implements NetWorthInterface
{
2023-08-06 11:22:36 +02:00
private AccountRepositoryInterface $accountRepository;
private AdminAccountRepositoryInterface $adminAccountRepository;
2022-06-25 14:23:52 +02:00
private CurrencyRepositoryInterface $currencyRepos;
2022-12-29 19:41:57 +01:00
private User $user;
2024-03-06 07:16:01 +01:00
private ?UserGroup $userGroup;
2023-08-06 11:22:36 +02:00
/**
2023-10-29 05:54:01 +01:00
* This method collects the user's net worth in ALL the user's currencies
* (1, 4 and 8) and also in the 'native' currency for ease of use.
*
* The set of accounts has to be fed to it.
*
2023-08-06 11:22:36 +02:00
* @throws FireflyException
*/
public function byAccounts(Collection $accounts, Carbon $date): array
{
// start in the past, end in the future? use $date
2024-12-28 07:35:20 +01:00
$convertToNative = Amount::convertToNative();
2024-12-24 06:34:12 +01:00
$ids = implode(',', $accounts->pluck('id')->toArray());
$cache = new CacheProperties();
2023-08-06 11:22:36 +02:00
$cache->addProperty($date);
2024-12-24 06:34:12 +01:00
$cache->addProperty($convertToNative);
2023-08-06 11:22:36 +02:00
$cache->addProperty('net-worth-by-accounts');
$cache->addProperty($ids);
if ($cache->has()) {
return $cache->get();
2023-08-06 11:22:36 +02:00
}
2024-12-24 06:34:12 +01:00
Log::debug(sprintf('Now in byAccounts("%s", "%s")', $ids, $date->format('Y-m-d H:i:s')));
$default = Amount::getDefaultCurrency();
$netWorth = [];
$balances = Steam::finalAccountsBalance($accounts, $date);
2023-08-06 11:22:36 +02:00
/** @var Account $account */
foreach ($accounts as $account) {
2024-12-24 06:34:12 +01:00
Log::debug(sprintf('Now at account #%d ("%s")', $account->id, $account->name));
$currency = $this->getRepository()->getAccountCurrency($account) ?? $default;
$useNative = $convertToNative && $default->id !== $currency->id;
$currency = $useNative ? $default : $currency;
$currencyCode = $currency->code;
$balance = '0';
$nativeBalance = '0';
2023-11-05 19:41:37 +01:00
if (array_key_exists($account->id, $balances)) {
$balance = $balances[$account->id]['balance'] ?? '0';
$nativeBalance = $balances[$account->id]['native_balance'] ?? '0';
2023-08-06 11:22:36 +02:00
}
2024-12-24 06:34:12 +01:00
Log::debug(sprintf('Balance is %s, native balance is %s', $balance, $nativeBalance));
// always subtract virtual balance again.
$balance = '' !== (string) $account->virtual_balance ? bcsub($balance, $account->virtual_balance) : $balance;
$nativeBalance = '' !== (string) $account->native_virtual_balance ? bcsub($nativeBalance, $account->native_virtual_balance) : $nativeBalance;
$amountToUse = $useNative ? $nativeBalance : $balance;
2024-12-24 06:34:12 +01:00
Log::debug(sprintf('Will use %s %s', $currencyCode, $amountToUse));
2023-12-29 19:59:19 +01:00
$netWorth[$currencyCode] ??= [
2024-12-24 06:34:12 +01:00
'balance' => '0',
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
2023-08-06 11:22:36 +02:00
];
2024-12-24 06:34:12 +01:00
$netWorth[$currencyCode]['balance'] = bcadd($amountToUse, $netWorth[$currencyCode]['balance']);
2023-08-06 11:22:36 +02:00
}
$cache->store($netWorth);
return $netWorth;
}
private function getRepository(): AccountRepositoryInterface|AdminAccountRepositoryInterface
{
if (null === $this->userGroup) {
return $this->accountRepository;
}
return $this->adminAccountRepository;
}
public function setUser(null|Authenticatable|User $user): void
{
2023-12-20 19:35:52 +01:00
if (!$user instanceof User) {
2023-02-22 19:54:19 +01:00
return;
}
$this->user = $user;
$this->userGroup = null;
// make repository:
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->accountRepository->setUser($this->user);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->currencyRepos->setUser($this->user);
}
2022-06-06 17:39:50 +02:00
2023-08-06 11:22:36 +02:00
public function setUserGroup(UserGroup $userGroup): void
{
$this->userGroup = $userGroup;
$this->adminAccountRepository = app(AdminAccountRepositoryInterface::class);
2023-09-21 16:26:07 +02:00
$this->adminAccountRepository->setUserGroup($userGroup);
2023-08-06 11:22:36 +02:00
}
2022-06-06 17:39:50 +02:00
/**
2023-10-29 06:09:21 +01:00
* @deprecated
2022-06-06 17:39:50 +02:00
*/
public function sumNetWorthByCurrency(Carbon $date): array
{
/**
* Collect accounts
*/
$accounts = $this->getAccounts();
$return = [];
2024-12-24 06:34:12 +01:00
$balances = Steam::finalAccountsBalance($accounts, $date);
2022-06-06 17:39:50 +02:00
foreach ($accounts as $account) {
$currency = $this->getRepository()->getAccountCurrency($account);
$balance = $balances[$account->id]['balance'] ?? '0';
2022-06-06 17:39:50 +02:00
// always subtract virtual balance.
$virtualBalance = $account->virtual_balance;
2022-06-06 17:39:50 +02:00
if ('' !== $virtualBalance) {
$balance = bcsub($balance, $virtualBalance);
}
$return[$currency->id] ??= [
2024-12-22 08:43:12 +01:00
'id' => (string) $currency->id,
2022-12-29 19:41:57 +01:00
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
'decimal_places' => $currency->decimal_places,
'sum' => '0',
];
2022-06-06 17:39:50 +02:00
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $balance);
}
return $return;
}
private function getAccounts(): Collection
{
$accounts = $this->getRepository()->getAccountsByType(
2023-02-22 18:14:14 +01:00
[AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]
);
2022-10-30 14:24:19 +01:00
$filtered = new Collection();
2023-12-20 19:35:52 +01:00
2022-06-06 17:39:50 +02:00
/** @var Account $account */
foreach ($accounts as $account) {
2024-12-22 08:43:12 +01:00
if (1 === (int) $this->getRepository()->getMetaValue($account, 'include_net_worth')) {
2022-06-06 17:39:50 +02:00
$filtered->push($account);
}
}
2023-12-20 19:35:52 +01:00
2022-06-06 17:39:50 +02:00
return $filtered;
}
2018-12-31 07:48:23 +01:00
}