Remove the use of deprecated methods.

This commit is contained in:
James Cole 2023-10-29 05:54:01 +01:00
parent a28f561e0c
commit 4bb171128e
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
10 changed files with 43 additions and 137 deletions

View File

@ -30,6 +30,6 @@ SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Do static code analysis.
# ./vendor/bin/phpstan analyse -c .ci/phpstan.neon --no-progress
./vendor/bin/phpstan analyse -c .ci/phpstan.neon --xdebug --error-format=table > phpstan-report.txt
./vendor/bin/phpstan analyse -c .ci/phpstan.neon --error-format=table > phpstan-report.txt
echo 'The PHPstan report can be found in phpstan-report.txt'

View File

@ -79,7 +79,7 @@ class UpdateController extends Controller
$account = $this->repository->update($account, $data);
$manager = $this->getManager();
$account->refresh();
Preferences::mark();
app('preferences')->mark();
/** @var AccountTransformer $transformer */
$transformer = app(AccountTransformer::class);

View File

@ -107,8 +107,8 @@ class BasicController extends Controller
$balanceData = $this->getBalanceInformation($start, $end);
$billData = $this->getBillInformation($start, $end);
$spentData = $this->getLeftToSpendInfo($start, $end);
$networthData = $this->getNetWorthInfo($start, $end);
$total = array_merge($balanceData, $billData, $spentData, $networthData);
$netWorthData = $this->getNetWorthInfo($start, $end);
$total = array_merge($balanceData, $billData, $spentData, $netWorthData);
// give new keys
$return = [];
@ -368,30 +368,30 @@ class BasicController extends Controller
}
);
$netWorthSet = $netWorthHelper->getNetWorthByCurrency($filtered, $date);
$netWorthSet = $netWorthHelper->byAccounts($filtered, $date);
$return = [];
foreach ($netWorthSet as $data) {
/** @var TransactionCurrency $currency */
$currency = $data['currency'];
foreach ($netWorthSet as $key => $data) {
if('native' === $key) {
continue;
}
$amount = $data['balance'];
if (0 === bccomp($amount, '0')) {
continue;
}
// return stuff
$return[] = [
'key' => sprintf('net-worth-in-%s', $currency->code),
'title' => trans('firefly.box_net_worth_in_currency', ['currency' => $currency->symbol]),
'key' => sprintf('net-worth-in-%s', $data['currency_code']),
'title' => trans('firefly.box_net_worth_in_currency', ['currency' => $data['currency_symbol']]),
'monetary_value' => $amount,
'currency_id' => (string)$currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'value_parsed' => app('amount')->formatAnything($currency, $data['balance'], false),
'currency_id' => (string)$data['currency_id'],
'currency_code' => $data['currency_code'],
'currency_symbol' => $data['currency_symbol'],
'currency_decimal_places' => $data['currency_decimal_places'],
'value_parsed' => app('amount')->formatFlat($data['currency_symbol'], $data['currency_decimal_places'], $data['balance'], false),
'local_icon' => 'line-chart',
'sub_title' => '',
];
}
return $return;
}

View File

@ -54,6 +54,11 @@ class NetWorth implements NetWorthInterface
private null | UserGroup $userGroup;
/**
* 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.
*
* @param Collection $accounts
* @param Carbon $date
*
@ -150,88 +155,6 @@ class NetWorth implements NetWorthInterface
return $this->adminAccountRepository;
}
/**
* Returns the user's net worth in an array with the following layout:
*
* -
* - currency: TransactionCurrency object
* - date: the current date
* - amount: the user's net worth in that currency.
*
* This repeats for each currency the user has transactions in.
* Result of this method is cached.
*
* @param Collection $accounts
* @param Carbon $date
*
* @return array
* @throws JsonException
* @throws FireflyException
* @deprecated
*/
public function getNetWorthByCurrency(Collection $accounts, Carbon $date): array
{
// start in the past, end in the future? use $date
$cache = new CacheProperties();
$cache->addProperty($date);
$cache->addProperty('net-worth-by-currency');
$cache->addProperty(implode(',', $accounts->pluck('id')->toArray()));
if ($cache->has()) {
return $cache->get();
}
$netWorth = [];
$result = [];
// Log::debug(sprintf('Now in getNetWorthByCurrency(%s)', $date->format('Y-m-d')));
// get default currency
$default = app('amount')->getDefaultCurrencyByUser($this->user);
// get all balances:
$balances = app('steam')->balancesByAccounts($accounts, $date);
// get the preferred currency for this account
/** @var Account $account */
foreach ($accounts as $account) {
// Log::debug(sprintf('Now at account #%d: "%s"', $account->id, $account->name));
$currencyId = (int)$this->getRepository()->getMetaValue($account, 'currency_id');
$currencyId = 0 === $currencyId ? $default->id : $currencyId;
// Log::debug(sprintf('Currency ID is #%d', $currencyId));
// balance in array:
$balance = $balances[$account->id] ?? '0';
//Log::debug(sprintf('Balance for %s is %s', $date->format('Y-m-d'), $balance));
// always subtract virtual balance.
$virtualBalance = (string)$account->virtual_balance;
if ('' !== $virtualBalance) {
$balance = bcsub($balance, $virtualBalance);
}
// Log::debug(sprintf('Balance corrected to %s because of virtual balance (%s)', $balance, $virtualBalance));
if (!array_key_exists($currencyId, $netWorth)) {
$netWorth[$currencyId] = '0';
}
$netWorth[$currencyId] = bcadd($balance, $netWorth[$currencyId]);
// Log::debug(sprintf('Total net worth for currency #%d is %s', $currencyId, $netWorth[$currencyId]));
}
ksort($netWorth);
// loop results and add currency information:
foreach ($netWorth as $currencyId => $balance) {
$result[] = [
'currency' => $this->currencyRepos->find($currencyId),
'balance' => $balance,
];
}
$cache->store($result);
return $result;
}
/**
* @param User|Authenticatable|null $user
*/

View File

@ -50,27 +50,6 @@ interface NetWorthInterface
*/
public function byAccounts(Collection $accounts, Carbon $date): array;
/**
* TODO unsure why this is deprecated.
*
* Returns the user's net worth in an array with the following layout:
*
* -
* - currency: TransactionCurrency object
* - date: the current date
* - amount: the user's net worth in that currency.
*
* This repeats for each currency the user has transactions in.
* Result of this method is cached.
*
* @param Collection $accounts
* @param Carbon $date
*
* @return array
* @deprecated
*/
public function getNetWorthByCurrency(Collection $accounts, Carbon $date): array;
/**
* @param User|Authenticatable|null $user
*/

View File

@ -109,19 +109,22 @@ class ReportController extends Controller
while ($current < $end) {
// get balances by date, grouped by currency.
$result = $helper->getNetWorthByCurrency($filtered, $current);
$result = $helper->byAccounts($filtered, $current);
// loop result, add to array.
/** @var array $netWorthItem */
foreach ($result as $netWorthItem) {
$currencyId = $netWorthItem['currency']->id;
foreach ($result as $key => $netWorthItem) {
if('native' === $key) {
continue;
}
$currencyId = $netWorthItem['currency_id'];
$label = $current->isoFormat((string)trans('config.month_and_day_js', [], $locale));
if (!array_key_exists($currencyId, $chartData)) {
$chartData[$currencyId] = [
'label' => 'Net worth in ' . $netWorthItem['currency']->name,
'label' => 'Net worth in ' . $netWorthItem['currency_name'],
'type' => 'line',
'currency_symbol' => $netWorthItem['currency']->symbol,
'currency_code' => $netWorthItem['currency']->code,
'currency_symbol' => $netWorthItem['currency_symbol'],
'currency_code' => $netWorthItem['currency_code'],
'entries' => [],
];
}

View File

@ -268,12 +268,13 @@ class BoxController extends Controller
}
);
$netWorthSet = $netWorthHelper->getNetWorthByCurrency($filtered, $date);
$netWorthSet = $netWorthHelper->byAccounts($filtered, $date);
$return = [];
foreach ($netWorthSet as $data) {
/** @var TransactionCurrency $currency */
$currency = $data['currency'];
$return[$currency->id] = app('amount')->formatAnything($currency, $data['balance'], false);
foreach ($netWorthSet as $key => $data) {
if('native' === $key) {
continue;
}
$return[$data['currency_id']] = app('amount')->formatFlat($data['currency_symbol'], $data['currency_decimal_places'], $data['balance'], false);
}
$return = [
'net_worths' => array_values($return),

View File

@ -158,7 +158,7 @@ class InstallController extends Controller
}
// clear cache as well.
Cache::clear();
Preferences::mark();
app('preferences')->mark();
return true;
}

View File

@ -54,23 +54,23 @@ class InterestingMessage
}
if ($this->groupMessage($request)) {
Preferences::mark();
app('preferences')->mark();
$this->handleGroupMessage($request);
}
if ($this->accountMessage($request)) {
Preferences::mark();
app('preferences')->mark();
$this->handleAccountMessage($request);
}
if ($this->billMessage($request)) {
Preferences::mark();
app('preferences')->mark();
$this->handleBillMessage($request);
}
if ($this->webhookMessage($request)) {
Preferences::mark();
app('preferences')->mark();
$this->handleWebhookMessage($request);
}
if ($this->currencyMessage($request)) {
Preferences::mark();
app('preferences')->mark();
$this->handleCurrencyMessage($request);
}

View File

@ -535,7 +535,7 @@ class Steam
$result = [];
/** @var Account $account */
foreach ($accounts as $account) {
$default = app('amount')->getDefaultCurrencyByUser($account->user);
$default = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
$result[(int)$account->id]
= [
'balance' => $this->balance($account, $date),