Various code fixes.

This commit is contained in:
James Cole
2022-11-04 05:11:05 +01:00
parent 8d8f81c27d
commit f16b679049
48 changed files with 226 additions and 246 deletions

View File

@@ -1,4 +1,5 @@
<?php
/**
* AccountController.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -114,14 +115,14 @@ class AccountController extends Controller
// loop the end balances. This is an array for each account ($expenses)
foreach ($endBalances as $accountId => $expenses) {
$accountId = (int) $accountId;
$accountId = (int)$accountId;
// loop each expense entry (each entry can be a different currency).
foreach ($expenses as $currencyId => $endAmount) {
$currencyId = (int) $currencyId;
$currencyId = (int)$currencyId;
// see if there is an accompanying start amount.
// grab the difference and find the currency.
$startAmount = (string) ($startBalances[$accountId][$currencyId] ?? '0');
$startAmount = (string)($startBalances[$accountId][$currencyId] ?? '0');
$diff = bcsub((string)$endAmount, $startAmount);
$currencies[$currencyId] = $currencies[$currencyId] ?? $this->currencyRepository->find($currencyId);
if (0 !== bccomp($diff, '0')) {
@@ -129,7 +130,7 @@ class AccountController extends Controller
$tempData[] = [
'name' => $accountNames[$accountId],
'difference' => $diff,
'diff_float' => (float) $diff,
'diff_float' => (float)$diff,
'currency_id' => $currencyId,
];
}
@@ -142,13 +143,13 @@ class AccountController extends Controller
// loop all found currencies and build the data array for the chart.
/**
* @var int $currencyId
* @var int $currencyId
* @var TransactionCurrency $currency
*/
foreach ($currencies as $currencyId => $currency) {
$dataSet
= [
'label' => (string) trans('firefly.spent'),
'label' => (string)trans('firefly.spent'),
'type' => 'bar',
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
@@ -173,8 +174,8 @@ class AccountController extends Controller
/**
* Expenses per budget for all time, as shown on account overview.
*
* @param AccountRepositoryInterface $repository
* @param Account $account
* @param AccountRepositoryInterface $repository
* @param Account $account
*
* @return JsonResponse
* @throws JsonException
@@ -190,9 +191,9 @@ class AccountController extends Controller
/**
* Expenses per budget, as shown on account overview.
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return JsonResponse
*/
@@ -215,7 +216,7 @@ class AccountController extends Controller
$budgetIds = [];
/** @var array $journal */
foreach ($journals as $journal) {
$budgetId = (int) $journal['budget_id'];
$budgetId = (int)$journal['budget_id'];
$key = sprintf('%d-%d', $budgetId, $journal['currency_id']);
$budgetIds[] = $budgetId;
if (!array_key_exists($key, $result)) {
@@ -235,7 +236,7 @@ class AccountController extends Controller
foreach ($result as $row) {
$budgetId = $row['budget_id'];
$name = $names[$budgetId];
$label = (string) trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
$chartData[$label] = ['amount' => $row['total'], 'currency_symbol' => $row['currency_symbol'], 'currency_code' => $row['currency_code']];
}
@@ -248,8 +249,8 @@ class AccountController extends Controller
/**
* Expenses grouped by category for account.
*
* @param AccountRepositoryInterface $repository
* @param Account $account
* @param AccountRepositoryInterface $repository
* @param Account $account
*
* @return JsonResponse
* @throws JsonException
@@ -265,9 +266,9 @@ class AccountController extends Controller
/**
* Expenses per category for one single account.
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return JsonResponse
*/
@@ -295,7 +296,7 @@ class AccountController extends Controller
if (!array_key_exists($key, $result)) {
$result[$key] = [
'total' => '0',
'category_id' => (int) $journal['category_id'],
'category_id' => (int)$journal['category_id'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $journal['currency_code'],
@@ -308,7 +309,7 @@ class AccountController extends Controller
foreach ($result as $row) {
$categoryId = $row['category_id'];
$name = $names[$categoryId] ?? '(unknown)';
$label = (string) trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
$chartData[$label] = ['amount' => $row['total'], 'currency_symbol' => $row['currency_symbol'], 'currency_code' => $row['currency_code']];
}
@@ -321,7 +322,7 @@ class AccountController extends Controller
/**
* Shows the balances for all the user's frontpage accounts.
*
* @param AccountRepositoryInterface $repository
* @param AccountRepositoryInterface $repository
*
* @return JsonResponse
* @throws FireflyException
@@ -337,7 +338,7 @@ class AccountController extends Controller
Log::debug('Default set is ', $defaultSet);
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
Log::debug('Frontpage preference set is ', $frontPage->data);
if (empty($frontPage->data)) {
if (0 === count($frontPage->data)) {
app('preferences')->set('frontPageAccounts', $defaultSet);
Log::debug('frontpage set is empty!');
}
@@ -349,8 +350,8 @@ class AccountController extends Controller
/**
* Shows the income grouped by category for an account, in all time.
*
* @param AccountRepositoryInterface $repository
* @param Account $account
* @param AccountRepositoryInterface $repository
* @param Account $account
*
* @return JsonResponse
* @throws JsonException
@@ -366,9 +367,9 @@ class AccountController extends Controller
/**
* Shows all income per account for each category.
*
* @param Account $account
* @param Carbon $start
* @param Carbon $end
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return JsonResponse
*/
@@ -410,7 +411,7 @@ class AccountController extends Controller
foreach ($result as $row) {
$categoryId = $row['category_id'];
$name = $names[$categoryId] ?? '(unknown)';
$label = (string) trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency_name']]);
$chartData[$label] = ['amount' => $row['total'], 'currency_symbol' => $row['currency_symbol'], 'currency_code' => $row['currency_code']];
}
$data = $this->generator->multiCurrencyPieChart($chartData);
@@ -422,10 +423,10 @@ class AccountController extends Controller
/**
* Shows overview of account during a single period.
*
* @param Account $account
* @param Carbon $start
* @param Account $account
* @param Carbon $start
*
* @param Carbon $end
* @param Carbon $end
*
* @return JsonResponse
* @throws FireflyException
@@ -461,10 +462,10 @@ class AccountController extends Controller
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Account $account
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param Account $account
* @param TransactionCurrency $currency
*
* @return array
* @throws FireflyException
@@ -478,38 +479,31 @@ class AccountController extends Controller
'label' => sprintf('%s (%s)', $account->name, $currency->symbol),
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'entries' => [],
];
$entries = [];
$current = clone $start;
switch ($step) {
default:
break;
case '1D':
// per day the entire period, balance for every day.
$format = (string) trans('config.month_and_day_js', [], $locale);
$range = app('steam')->balanceInRange($account, $start, $end, $currency);
$previous = array_values($range)[0];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$label = $current->isoFormat($format);
$entries[$label] = (float) $balance;
$previous = $balance;
$current->addDay();
}
break;
case '1W':
case '1M':
case '1Y':
while ($end >= $current) {
$balance = (float) app('steam')->balance($account, $current, $currency);
$label = app('navigation')->periodShow($current, $step);
$entries[$label] = $balance;
$current = app('navigation')->addPeriod($current, $step, 0);
}
break;
if ('1D' === $step) {
// per day the entire period, balance for every day.
$format = (string)trans('config.month_and_day_js', [], $locale);
$range = app('steam')->balanceInRange($account, $start, $end, $currency);
$previous = array_values($range)[0];
while ($end >= $current) {
$theDate = $current->format('Y-m-d');
$balance = $range[$theDate] ?? $previous;
$label = $current->isoFormat($format);
$entries[$label] = (float)$balance;
$previous = $balance;
$current->addDay();
}
}
if ('1W' === $step || '1M' === $step || '1Y' === $step) {
while ($end >= $current) {
$balance = (float)app('steam')->balance($account, $current, $currency);
$label = app('navigation')->periodShow($current, $step);
$entries[$label] = $balance;
$current = app('navigation')->addPeriod($current, $step, 0);
}
}
$result['entries'] = $entries;
@@ -521,10 +515,10 @@ class AccountController extends Controller
*
* TODO this chart is not multi currency aware.
*
* @param Collection $accounts
* @param Collection $accounts
*
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @return JsonResponse
* @throws FireflyException
* @throws JsonException
@@ -572,22 +566,22 @@ class AccountController extends Controller
// loop the end balances. This is an array for each account ($expenses)
foreach ($endBalances as $accountId => $expenses) {
$accountId = (int) $accountId;
$accountId = (int)$accountId;
// loop each expense entry (each entry can be a different currency).
foreach ($expenses as $currencyId => $endAmount) {
$currencyId = (int) $currencyId;
$currencyId = (int)$currencyId;
// see if there is an accompanying start amount.
// grab the difference and find the currency.
$startAmount = (string)($startBalances[$accountId][$currencyId] ?? '0');
$diff = bcsub((string) $endAmount, $startAmount);
$diff = bcsub((string)$endAmount, $startAmount);
$currencies[$currencyId] = $currencies[$currencyId] ?? $this->currencyRepository->find($currencyId);
if (0 !== bccomp($diff, '0')) {
// store the values in a temporary array.
$tempData[] = [
'name' => $accountNames[$accountId],
'difference' => $diff,
'diff_float' => (float) $diff,
'diff_float' => (float)$diff,
'currency_id' => $currencyId,
];
}
@@ -600,13 +594,13 @@ class AccountController extends Controller
// loop all found currencies and build the data array for the chart.
/**
* @var int $currencyId
* @var int $currencyId
* @var TransactionCurrency $currency
*/
foreach ($currencies as $currencyId => $currency) {
$dataSet
= [
'label' => (string) trans('firefly.earned'),
'label' => (string)trans('firefly.earned'),
'type' => 'bar',
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,