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

243 lines
9.4 KiB
PHP
Raw Normal View History

<?php
/**
* NetWorth.php
2020-01-28 01:46:01 -06: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 10:39:50 -05:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
2022-06-06 10:39:50 -05:00
use FireflyIII\Models\AccountType;
2023-08-06 04:22:36 -05:00
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface;
2023-10-28 08:03:33 -05:00
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2023-08-06 04:22:36 -05:00
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\User;
2023-02-22 12:54:19 -06:00
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
/**
* 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 04:22:36 -05:00
private AccountRepositoryInterface $accountRepository;
private AdminAccountRepositoryInterface $adminAccountRepository;
2022-06-25 07:23:52 -05:00
private CurrencyRepositoryInterface $currencyRepos;
2022-12-29 12:41:57 -06:00
private User $user;
private null | UserGroup $userGroup;
2023-08-06 04:22:36 -05:00
/**
2023-10-28 23:54:01 -05: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 04:22:36 -05:00
* @param Collection $accounts
* @param Carbon $date
*
* @return array
* @throws FireflyException
*/
public function byAccounts(Collection $accounts, Carbon $date): array
{
// start in the past, end in the future? use $date
$ids = implode(',', $accounts->pluck('id')->toArray());
$cache = new CacheProperties();
$cache->addProperty($date);
$cache->addProperty('net-worth-by-accounts');
$cache->addProperty($ids);
if ($cache->has()) {
return $cache->get();
2023-08-06 04:22:36 -05:00
}
app('log')->debug(sprintf('Now in byAccounts("%s", "%s")', $ids, $date->format('Y-m-d')));
$default = app('amount')->getDefaultCurrency();
$converter = new ExchangeRateConverter();
// default "native" currency has everything twice, for consistency.
$netWorth = [
'native' => [
'balance' => '0',
'native_balance' => '0',
2023-11-05 12:41:37 -06:00
'currency_id' => $default->id,
2023-08-06 04:22:36 -05:00
'currency_code' => $default->code,
'currency_name' => $default->name,
'currency_symbol' => $default->symbol,
2023-11-26 05:10:42 -06:00
'currency_decimal_places' => $default->decimal_places,
2023-11-05 12:41:37 -06:00
'native_id' => $default->id,
2023-08-06 04:22:36 -05:00
'native_code' => $default->code,
'native_name' => $default->name,
'native_symbol' => $default->symbol,
2023-11-26 05:10:42 -06:00
'native_decimal_places' => $default->decimal_places,
2023-08-06 04:22:36 -05:00
],
];
$balances = app('steam')->balancesByAccountsConverted($accounts, $date);
/** @var Account $account */
foreach ($accounts as $account) {
app('log')->debug(sprintf('Now at account #%d ("%s")', $account->id, $account->name));
$currency = $this->getRepository()->getAccountCurrency($account);
2023-12-09 13:12:34 -06:00
if(null === $currency) {
$currency = app('amount')->getDefaultCurrency();
}
2023-11-05 12:41:37 -06:00
$currencyId = $currency->id;
2023-08-06 04:22:36 -05:00
$balance = '0';
$nativeBalance = '0';
2023-11-05 12:41:37 -06:00
if (array_key_exists($account->id, $balances)) {
$balance = $balances[$account->id]['balance'] ?? '0';
$nativeBalance = $balances[$account->id]['native_balance'] ?? '0';
2023-08-06 04:22:36 -05:00
}
app('log')->debug(sprintf('Balance is %s, native balance is %s', $balance, $nativeBalance));
// always subtract virtual balance
2023-11-05 12:41:37 -06:00
$virtualBalance = $account->virtual_balance;
2023-08-06 04:22:36 -05:00
if ('' !== $virtualBalance) {
$balance = bcsub($balance, $virtualBalance);
$nativeVirtualBalance = $converter->convert($default, $currency, $account->created_at, $virtualBalance);
$nativeBalance = bcsub($nativeBalance, $nativeVirtualBalance);
}
2023-12-09 23:45:59 -06:00
$netWorth[$currencyId] ??= [
2023-08-06 04:22:36 -05:00
'balance' => '0',
'native_balance' => '0',
'currency_id' => $currencyId,
'currency_code' => $currency->code,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
2023-11-26 05:10:42 -06:00
'currency_decimal_places' => $currency->decimal_places,
2023-11-05 12:41:37 -06:00
'native_id' => $default->id,
2023-08-06 04:22:36 -05:00
'native_code' => $default->code,
'native_name' => $default->name,
'native_symbol' => $default->symbol,
2023-11-26 05:10:42 -06:00
'native_decimal_places' => $default->decimal_places,
2023-08-06 04:22:36 -05:00
];
$netWorth[$currencyId]['balance'] = bcadd($balance, $netWorth[$currencyId]['balance']);
$netWorth[$currencyId]['native_balance'] = bcadd($nativeBalance, $netWorth[$currencyId]['native_balance']);
$netWorth['native']['balance'] = bcadd($nativeBalance, $netWorth['native']['balance']);
$netWorth['native']['native_balance'] = bcadd($nativeBalance, $netWorth['native']['native_balance']);
}
$cache->store($netWorth);
return $netWorth;
}
/**
* @return AdminAccountRepositoryInterface|AccountRepositoryInterface
*/
private function getRepository(): AdminAccountRepositoryInterface | AccountRepositoryInterface
{
if (null === $this->userGroup) {
return $this->accountRepository;
}
return $this->adminAccountRepository;
}
/**
2023-06-21 05:34:58 -05:00
* @param User|Authenticatable|null $user
*/
2023-06-21 05:34:58 -05:00
public function setUser(User | Authenticatable | null $user): void
{
2023-10-30 13:49:40 -05:00
if (!($user instanceof User)) {
2023-02-22 12:54:19 -06: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 10:39:50 -05:00
2023-08-06 04:22:36 -05:00
/**
* @inheritDoc
*/
public function setUserGroup(UserGroup $userGroup): void
{
$this->userGroup = $userGroup;
$this->adminAccountRepository = app(AdminAccountRepositoryInterface::class);
2023-09-21 09:26:07 -05:00
$this->adminAccountRepository->setUserGroup($userGroup);
2023-08-06 04:22:36 -05:00
}
2022-06-06 10:39:50 -05:00
/**
* @inheritDoc
2023-10-29 00:09:21 -05:00
* @deprecated
2022-06-06 10:39:50 -05:00
*/
public function sumNetWorthByCurrency(Carbon $date): array
{
/**
* Collect accounts
*/
$accounts = $this->getAccounts();
$return = [];
$balances = app('steam')->balancesByAccounts($accounts, $date);
foreach ($accounts as $account) {
$currency = $this->getRepository()->getAccountCurrency($account);
2022-06-06 10:39:50 -05:00
$balance = $balances[$account->id] ?? '0';
// always subtract virtual balance.
2023-11-05 12:41:37 -06:00
$virtualBalance = $account->virtual_balance;
2022-06-06 10:39:50 -05:00
if ('' !== $virtualBalance) {
$balance = bcsub($balance, $virtualBalance);
}
2023-12-09 23:45:59 -06:00
$return[$currency->id] ??= [
2022-12-29 12:41:57 -06:00
'id' => (string)$currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
'decimal_places' => $currency->decimal_places,
'sum' => '0',
];
2022-06-06 10:39:50 -05:00
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $balance);
}
return $return;
}
/**
* @return Collection
*/
private function getAccounts(): Collection
{
$accounts = $this->getRepository()->getAccountsByType(
2023-02-22 11:14:14 -06:00
[AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]
);
2022-10-30 08:24:19 -05:00
$filtered = new Collection();
2022-06-06 10:39:50 -05:00
/** @var Account $account */
foreach ($accounts as $account) {
if (1 === (int)$this->getRepository()->getMetaValue($account, 'include_net_worth')) {
2022-06-06 10:39:50 -05:00
$filtered->push($account);
}
}
return $filtered;
}
2018-12-31 00:48:23 -06:00
}