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,

View File

@@ -205,7 +205,7 @@ class BudgetController extends Controller
*
* @return JsonResponse
*/
public function expenseAsset(Budget $budget, ?BudgetLimit $budgetLimit = null): JsonResponse
public function expenseAsset(Budget $budget, ?BudgetLimit $budgetLimit = null): JsonResponse // @phpstan-ignore-line
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
@@ -273,7 +273,7 @@ class BudgetController extends Controller
*
* @return JsonResponse
*/
public function expenseCategory(Budget $budget, ?BudgetLimit $budgetLimit = null): JsonResponse
public function expenseCategory(Budget $budget, ?BudgetLimit $budgetLimit = null): JsonResponse // @phpstan-ignore-line
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
@@ -337,7 +337,7 @@ class BudgetController extends Controller
*
* @return JsonResponse
*/
public function expenseExpense(Budget $budget, ?BudgetLimit $budgetLimit = null): JsonResponse
public function expenseExpense(Budget $budget, ?BudgetLimit $budgetLimit = null): JsonResponse // @phpstan-ignore-line
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);

View File

@@ -1,4 +1,5 @@
<?php
/**
* CategoryController.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -23,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Http\Controllers\Controller;
@@ -39,7 +39,6 @@ use FireflyIII\Support\Http\Controllers\ChartGeneration;
use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use JsonException;
/**
* Class CategoryController.
@@ -69,7 +68,7 @@ class CategoryController extends Controller
* Show an overview for a category for all time, per month/week/year.
* TODO test method, for category refactor.
*
* @param Category $category
* @param Category $category
*
* @return JsonResponse
* @throws FireflyException
@@ -106,14 +105,7 @@ class CategoryController extends Controller
*/
private function getDate(): Carbon
{
$carbon = null;
try {
$carbon = today(config('app.timezone'));
} catch (Exception $e) {
// @ignoreException
}
return $carbon;
return today(config('app.timezone'));
}
/**
@@ -147,10 +139,10 @@ class CategoryController extends Controller
* Chart report.
* TODO test method for category refactor.
*
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return JsonResponse
*/
@@ -175,10 +167,10 @@ class CategoryController extends Controller
/**
* Generate report chart for either with or without category.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Category|null $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Category|null $category
*
* @return array
*/
@@ -199,7 +191,7 @@ class CategoryController extends Controller
if (null !== $category) {
/** @var OperationsRepositoryInterface $opsRepository */
$opsRepository = app(OperationsRepositoryInterface::class);
$categoryId = (int) $category->id;
$categoryId = (int)$category->id;
// this gives us all currencies
$collection = new Collection([$category]);
$expenses = $opsRepository->listExpenses($start, $end, null, $collection);
@@ -217,7 +209,7 @@ class CategoryController extends Controller
$inKey = sprintf('%d-in', $currencyId);
$chartData[$outKey]
= [
'label' => sprintf('%s (%s)', (string) trans('firefly.spent'), $currencyInfo['currency_name']),
'label' => sprintf('%s (%s)', (string)trans('firefly.spent'), $currencyInfo['currency_name']),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
@@ -225,7 +217,7 @@ class CategoryController extends Controller
$chartData[$inKey]
= [
'label' => sprintf('%s (%s)', (string) trans('firefly.earned'), $currencyInfo['currency_name']),
'label' => sprintf('%s (%s)', (string)trans('firefly.earned'), $currencyInfo['currency_name']),
'entries' => [],
'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
@@ -262,9 +254,9 @@ class CategoryController extends Controller
* Chart for period for transactions without a category.
* TODO test me.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return JsonResponse
*/
@@ -289,8 +281,8 @@ class CategoryController extends Controller
* Chart for a specific period.
* TODO test me, for category refactor.
*
* @param Category $category
* @param Carbon $date
* @param Category $category
* @param Carbon $date
*
* @return JsonResponse
* @throws FireflyException

View File

@@ -318,7 +318,7 @@ class DoubleReportController extends Controller
$journalId = $journal['transaction_journal_id'];
// no tags? also deserves a sport
if (empty($journal['tags'])) {
if (0 === count($journal['tags'])) {
$includedJournals[] = $journalId;
// do something
$tagName = trans('firefly.no_tags');
@@ -378,7 +378,7 @@ class DoubleReportController extends Controller
$journalId = $journal['transaction_journal_id'];
// no tags? also deserves a sport
if (empty($journal['tags'])) {
if (0 === count($journal['tags'])) {
$includedJournals[] = $journalId;
// do something
$tagName = trans('firefly.no_tags');

View File

@@ -189,7 +189,7 @@ class ExpenseReportController extends Controller
$newSet[$key] = $entry;
}
}
if (empty($newSet)) {
if (0 === count($newSet)) {
$newSet = $chartData;
}
$data = $this->generator->multiSet($newSet);

View File

@@ -115,20 +115,17 @@ class TransactionController extends Controller
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end);
$collector->withCategoryInformation();
switch ($objectType) {
default:
throw new FireflyException(sprintf('Cant handle "%s"', $objectType));
case 'withdrawal':
$collector->setTypes([TransactionType::WITHDRAWAL]);
break;
case 'deposit':
$collector->setTypes([TransactionType::DEPOSIT]);
break;
case 'transfers':
case 'transfer':
$collector->setTypes([TransactionType::TRANSFER]);
break;
if ('withdrawal' === $objectType) {
$collector->setTypes([TransactionType::WITHDRAWAL]);
}
if ('deposit' === $objectType) {
$collector->setTypes([TransactionType::DEPOSIT]);
}
if ('transfer' === $objectType || 'transfers' === $objectType) {
$collector->setTypes([TransactionType::TRANSFER]);
}
$result = $collector->getExtractedJournals();
$data = [];
@@ -172,20 +169,17 @@ class TransactionController extends Controller
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end);
$collector->withAccountInformation();
switch ($objectType) {
default:
throw new FireflyException(sprintf('Cant handle "%s"', $objectType));
case 'withdrawal':
$collector->setTypes([TransactionType::WITHDRAWAL]);
break;
case 'deposit':
$collector->setTypes([TransactionType::DEPOSIT]);
break;
case 'transfers':
case 'transfer':
$collector->setTypes([TransactionType::TRANSFER]);
break;
if ('withdrawal' === $objectType) {
$collector->setTypes([TransactionType::WITHDRAWAL]);
}
if ('deposit' === $objectType) {
$collector->setTypes([TransactionType::DEPOSIT]);
}
if ('transfer' === $objectType || 'transfers' === $objectType) {
$collector->setTypes([TransactionType::TRANSFER]);
}
$result = $collector->getExtractedJournals();
$data = [];
@@ -229,20 +223,17 @@ class TransactionController extends Controller
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end);
$collector->withAccountInformation();
switch ($objectType) {
default:
throw new FireflyException(sprintf('Cant handle "%s"', $objectType));
case 'withdrawal':
$collector->setTypes([TransactionType::WITHDRAWAL]);
break;
case 'deposit':
$collector->setTypes([TransactionType::DEPOSIT]);
break;
case 'transfers':
case 'transfer':
$collector->setTypes([TransactionType::TRANSFER]);
break;
if ('withdrawal' === $objectType) {
$collector->setTypes([TransactionType::WITHDRAWAL]);
}
if ('deposit' === $objectType) {
$collector->setTypes([TransactionType::DEPOSIT]);
}
if ('transfer' === $objectType || 'transfers' === $objectType) {
$collector->setTypes([TransactionType::TRANSFER]);
}
$result = $collector->getExtractedJournals();
$data = [];