Code cleanup.

This commit is contained in:
James Cole 2024-01-14 05:10:05 +01:00
parent 5c81e98218
commit 458402aaff
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80

View File

@ -32,7 +32,6 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\CacheProperties; use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Facades\Navigation;
use FireflyIII\Support\Http\Controllers\BasicDataSupport; use FireflyIII\Support\Http\Controllers\BasicDataSupport;
use FireflyIII\Support\Http\Controllers\ChartGeneration; use FireflyIII\Support\Http\Controllers\ChartGeneration;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
@ -65,7 +64,7 @@ class ReportController extends Controller
public function netWorth(Collection $accounts, Carbon $start, Carbon $end): JsonResponse public function netWorth(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
{ {
// chart properties for cache: // chart properties for cache:
$cache = new CacheProperties(); $cache = new CacheProperties();
$cache->addProperty('chart.report.net-worth'); $cache->addProperty('chart.report.net-worth');
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty(implode(',', $accounts->pluck('id')->toArray())); $cache->addProperty(implode(',', $accounts->pluck('id')->toArray()));
@ -73,12 +72,12 @@ class ReportController extends Controller
if ($cache->has()) { if ($cache->has()) {
return response()->json($cache->get()); return response()->json($cache->get());
} }
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
$current = clone $start; $current = clone $start;
$chartData = []; $chartData = [];
/** @var NetWorthInterface $helper */ /** @var NetWorthInterface $helper */
$helper = app(NetWorthInterface::class); $helper = app(NetWorthInterface::class);
$helper->setUser(auth()->user()); $helper->setUser(auth()->user());
// filter accounts on having the preference for being included. // filter accounts on having the preference for being included.
@ -108,11 +107,11 @@ class ReportController extends Controller
if ('native' === $key) { if ('native' === $key) {
continue; continue;
} }
$currencyId = $netWorthItem['currency_id']; $currencyId = $netWorthItem['currency_id'];
$label = $current->isoFormat((string)trans('config.month_and_day_js', [], $locale)); $label = $current->isoFormat((string)trans('config.month_and_day_js', [], $locale));
if (!array_key_exists($currencyId, $chartData)) { if (!array_key_exists($currencyId, $chartData)) {
$chartData[$currencyId] = [ $chartData[$currencyId] = [
'label' => 'Net worth in ' . $netWorthItem['currency_name'], 'label' => 'Net worth in '.$netWorthItem['currency_name'],
'type' => 'line', 'type' => 'line',
'currency_symbol' => $netWorthItem['currency_symbol'], 'currency_symbol' => $netWorthItem['currency_symbol'],
'currency_code' => $netWorthItem['currency_code'], 'currency_code' => $netWorthItem['currency_code'],
@ -124,7 +123,7 @@ class ReportController extends Controller
$current->addDays(7); $current->addDays(7);
} }
$data = $this->generator->multiSet($chartData); $data = $this->generator->multiSet($chartData);
$cache->store($data); $cache->store($data);
return response()->json($data); return response()->json($data);
@ -138,7 +137,7 @@ class ReportController extends Controller
public function operations(Collection $accounts, Carbon $start, Carbon $end): JsonResponse public function operations(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
{ {
// chart properties for cache: // chart properties for cache:
$cache = new CacheProperties(); $cache = new CacheProperties();
$cache->addProperty('chart.report.operations'); $cache->addProperty('chart.report.operations');
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($accounts); $cache->addProperty($accounts);
@ -147,7 +146,6 @@ class ReportController extends Controller
return response()->json($cache->get()); return response()->json($cache->get());
} }
app('log')->debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray()); app('log')->debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray());
$format = app('navigation')->preferredCarbonFormat($start, $end); $format = app('navigation')->preferredCarbonFormat($start, $end);
$titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end); $titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
@ -157,17 +155,17 @@ class ReportController extends Controller
$chartData = []; $chartData = [];
/** @var GroupCollectorInterface $collector */ /** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class); $collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->withAccountInformation(); $collector->setRange($start, $end)->withAccountInformation();
$collector->setXorAccounts($accounts); $collector->setXorAccounts($accounts);
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION, TransactionType::TRANSFER]); $collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION, TransactionType::TRANSFER]);
$journals = $collector->getExtractedJournals(); $journals = $collector->getExtractedJournals();
// loop. group by currency and by period. // loop. group by currency and by period.
/** @var array $journal */ /** @var array $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
$period = $journal['date']->format($format); $period = $journal['date']->format($format);
$currencyId = (int)$journal['currency_id']; $currencyId = (int)$journal['currency_id'];
$data[$currencyId] ??= [ $data[$currencyId] ??= [
'currency_id' => $currencyId, 'currency_id' => $currencyId,
'currency_symbol' => $journal['currency_symbol'], 'currency_symbol' => $journal['currency_symbol'],
@ -181,8 +179,8 @@ class ReportController extends Controller
'earned' => '0', 'earned' => '0',
]; ];
// in our outgoing? // in our outgoing?
$key = 'spent'; $key = 'spent';
$amount = app('steam')->positive($journal['amount']); $amount = app('steam')->positive($journal['amount']);
// deposit = incoming // deposit = incoming
// transfer or reconcile or opening balance, and these accounts are the destination. // transfer or reconcile or opening balance, and these accounts are the destination.
@ -195,7 +193,7 @@ class ReportController extends Controller
// loop this data, make chart bars for each currency: // loop this data, make chart bars for each currency:
/** @var array $currency */ /** @var array $currency */
foreach ($data as $currency) { foreach ($data as $currency) {
$income = [ $income = [
'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]), 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]),
'type' => 'bar', 'type' => 'bar',
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green 'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
@ -204,7 +202,7 @@ class ReportController extends Controller
'currency_code' => $currency['currency_code'], 'currency_code' => $currency['currency_code'],
'entries' => [], 'entries' => [],
]; ];
$expense = [ $expense = [
'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]), 'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]),
'type' => 'bar', 'type' => 'bar',
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red 'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
@ -219,7 +217,7 @@ class ReportController extends Controller
// #8374. Sloppy fix for yearly charts. Not really interested in a better fix with v2 layout and all. // #8374. Sloppy fix for yearly charts. Not really interested in a better fix with v2 layout and all.
if ('1Y' === $preferredRange) { if ('1Y' === $preferredRange) {
$currentEnd = Navigation::endOfPeriod($currentEnd, $preferredRange); $currentEnd = app('navigation')->endOfPeriod($currentEnd, $preferredRange);
} }
while ($currentStart <= $currentEnd) { while ($currentStart <= $currentEnd) {
$key = $currentStart->format($format); $key = $currentStart->format($format);
@ -227,13 +225,13 @@ class ReportController extends Controller
$income['entries'][$title] = app('steam')->bcround($currency[$key]['earned'] ?? '0', $currency['currency_decimal_places']); $income['entries'][$title] = app('steam')->bcround($currency[$key]['earned'] ?? '0', $currency['currency_decimal_places']);
$expense['entries'][$title] = app('steam')->bcround($currency[$key]['spent'] ?? '0', $currency['currency_decimal_places']); $expense['entries'][$title] = app('steam')->bcround($currency[$key]['spent'] ?? '0', $currency['currency_decimal_places']);
$currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0); $currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0);
}; }
$chartData[] = $income; $chartData[] = $income;
$chartData[] = $expense; $chartData[] = $expense;
} }
$data = $this->generator->multiSet($chartData); $data = $this->generator->multiSet($chartData);
$cache->store($data); $cache->store($data);
return response()->json($data); return response()->json($data);