James Cole 2024-11-19 06:31:49 +01:00
parent 8d2d3d4002
commit 07b55bd71f
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 41 additions and 11 deletions

View File

@ -40,6 +40,7 @@ use FireflyIII\Support\Http\Controllers\ChartGeneration;
use FireflyIII\Support\Http\Controllers\DateCalculation; use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/** /**
* Class AccountController. * Class AccountController.
@ -300,13 +301,13 @@ class AccountController extends Controller
$start = clone session('start', today(config('app.timezone'))->startOfMonth()); $start = clone session('start', today(config('app.timezone'))->startOfMonth());
$end = clone session('end', today(config('app.timezone'))->endOfMonth()); $end = clone session('end', today(config('app.timezone'))->endOfMonth());
$defaultSet = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray(); $defaultSet = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray();
app('log')->debug('Default set is ', $defaultSet); Log::debug('Default set is ', $defaultSet);
$frontpage = app('preferences')->get('frontpageAccounts', $defaultSet); $frontpage = app('preferences')->get('frontpageAccounts', $defaultSet);
$frontpageArray = !is_array($frontpage->data) ? [] : $frontpage->data; $frontpageArray = !is_array($frontpage->data) ? [] : $frontpage->data;
app('log')->debug('Frontpage preference set is ', $frontpageArray); Log::debug('Frontpage preference set is ', $frontpageArray);
if (0 === count($frontpageArray)) { if (0 === count($frontpageArray)) {
app('preferences')->set('frontpageAccounts', $defaultSet); app('preferences')->set('frontpageAccounts', $defaultSet);
app('log')->debug('frontpage set is empty!'); Log::debug('frontpage set is empty!');
} }
$accounts = $repository->getAccountsById($frontpageArray); $accounts = $repository->getAccountsById($frontpageArray);
@ -414,7 +415,7 @@ class AccountController extends Controller
*/ */
private function periodByCurrency(Carbon $start, Carbon $end, Account $account, TransactionCurrency $currency): array private function periodByCurrency(Carbon $start, Carbon $end, Account $account, TransactionCurrency $currency): array
{ {
app('log')->debug(sprintf('Now in periodByCurrency("%s", "%s", %s, "%s")', $start->format('Y-m-d'), $end->format('Y-m-d'), $account->id, $currency->code)); Log::debug(sprintf('Now in periodByCurrency("%s", "%s", %s, "%s")', $start->format('Y-m-d'), $end->format('Y-m-d'), $account->id, $currency->code));
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
$step = $this->calculateStep($start, $end); $step = $this->calculateStep($start, $end);
$result = [ $result = [
@ -424,13 +425,13 @@ class AccountController extends Controller
]; ];
$entries = []; $entries = [];
$current = clone $start; $current = clone $start;
app('log')->debug(sprintf('Step is %s', $step)); Log::debug(sprintf('Step is %s', $step));
// fix for issue https://github.com/firefly-iii/firefly-iii/issues/8041 // fix for issue https://github.com/firefly-iii/firefly-iii/issues/8041
// have to make sure this chart is always based on the balance at the END of the period. // have to make sure this chart is always based on the balance at the END of the period.
// This period depends on the size of the chart // This period depends on the size of the chart
$current = app('navigation')->endOfX($current, $step, null); $current = app('navigation')->endOfX($current, $step, null);
app('log')->debug(sprintf('$current date is %s', $current->format('Y-m-d'))); Log::debug(sprintf('$current date is %s', $current->format('Y-m-d')));
if ('1D' === $step) { if ('1D' === $step) {
// per day the entire period, balance for every day. // per day the entire period, balance for every day.
$format = (string)trans('config.month_and_day_js', [], $locale); $format = (string)trans('config.month_and_day_js', [], $locale);
@ -447,7 +448,7 @@ class AccountController extends Controller
} }
if ('1W' === $step || '1M' === $step || '1Y' === $step) { if ('1W' === $step || '1M' === $step || '1Y' === $step) {
while ($end >= $current) { while ($end >= $current) {
app('log')->debug(sprintf('Current is: %s', $current->format('Y-m-d'))); Log::debug(sprintf('Current is: %s', $current->format('Y-m-d')));
$balance = (float)app('steam')->balance($account, $current, $currency); $balance = (float)app('steam')->balance($account, $current, $currency);
$label = app('navigation')->periodShow($current, $step); $label = app('navigation')->periodShow($current, $step);
$entries[$label] = $balance; $entries[$label] = $balance;

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Chart; namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface; use FireflyIII\Helpers\Report\NetWorthInterface;
@ -36,6 +37,7 @@ 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;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/** /**
* Class ReportController. * Class ReportController.
@ -88,7 +90,7 @@ class ReportController extends Controller
$includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth'); $includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth');
$result = null === $includeNetWorth ? true : '1' === $includeNetWorth; $result = null === $includeNetWorth ? true : '1' === $includeNetWorth;
if (false === $result) { if (false === $result) {
app('log')->debug(sprintf('Will not include "%s" in net worth charts.', $account->name)); Log::debug(sprintf('Will not include "%s" in net worth charts.', $account->name));
} }
return $result; return $result;
@ -136,6 +138,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
{ {
$end->endOfDay();
// chart properties for cache: // chart properties for cache:
$cache = new CacheProperties(); $cache = new CacheProperties();
$cache->addProperty('chart.report.operations'); $cache->addProperty('chart.report.operations');
@ -146,7 +149,8 @@ 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()); Log::debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray());
Log::debug(sprintf('Period: %s to %s', $start->toW3cString(), $end->toW3cString()));
$format = app('navigation')->preferredCarbonFormat($start, $end); $format = app('navigation')->preferredCarbonFormat($start, $end);
$titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end); $titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end);
$preferredRange = app('navigation')->preferredRangeFormat($start, $end); $preferredRange = app('navigation')->preferredRangeFormat($start, $end);
@ -158,7 +162,13 @@ class ReportController extends Controller
$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(
[
TransactionTypeEnum::WITHDRAWAL,
TransactionTypeEnum::DEPOSIT,
TransactionTypeEnum::RECONCILIATION,
TransactionTypeEnum::TRANSFER
]);
$journals = $collector->getExtractedJournals(); $journals = $collector->getExtractedJournals();
// loop. group by currency and by period. // loop. group by currency and by period.
@ -184,15 +194,23 @@ class ReportController extends Controller
// 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.
if (TransactionType::DEPOSIT === $journal['transaction_type_type'] || ((TransactionType::TRANSFER === $journal['transaction_type_type'] || TransactionType::RECONCILIATION === $journal['transaction_type_type'] || TransactionType::OPENING_BALANCE === $journal['transaction_type_type']) && in_array($journal['destination_account_id'], $ids, true))) { if (
TransactionTypeEnum::DEPOSIT->value === $journal['transaction_type_type'] ||
((
TransactionTypeEnum::TRANSFER->value === $journal['transaction_type_type'] ||
TransactionTypeEnum::RECONCILIATION->value === $journal['transaction_type_type'] ||
TransactionTypeEnum::OPENING_BALANCE->value === $journal['transaction_type_type']) &&
in_array($journal['destination_account_id'], $ids, true))) {
$key = 'earned'; $key = 'earned';
} }
$data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount); $data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount);
} }
// loop this data, make chart bars for each currency: // loop this data, make chart bars for each currency:
Log::debug('Looping data');
/** @var array $currency */ /** @var array $currency */
foreach ($data as $currency) { foreach ($data as $currency) {
Log::debug(sprintf('Now processing currency "%s"', $currency['currency_name']));
$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',
@ -214,12 +232,15 @@ class ReportController extends Controller
// loop all possible periods between $start and $end // loop all possible periods between $start and $end
$currentStart = clone $start; $currentStart = clone $start;
$currentEnd = clone $end; $currentEnd = clone $end;
Log::debug(sprintf('START current start and end: %s and %s', $currentStart->toW3cString(), $currentEnd->toW3cString()));
// #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 = app('navigation')->endOfPeriod($currentEnd, $preferredRange); $currentEnd = app('navigation')->endOfPeriod($currentEnd, $preferredRange);
} }
Log::debug('Start of sub-loop');
while ($currentStart <= $currentEnd) { while ($currentStart <= $currentEnd) {
Log::debug(sprintf('Current start: %s', $currentStart->toW3cString()));
$key = $currentStart->format($format); $key = $currentStart->format($format);
$title = $currentStart->isoFormat($titleFormat); $title = $currentStart->isoFormat($titleFormat);
// #8663 make sure the period exists in the data previously collected. // #8663 make sure the period exists in the data previously collected.
@ -227,12 +248,20 @@ 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']);
} }
// #9477 if the period is not in the data, add it with zero values.
if (!array_key_exists($key, $currency)) {
$income['entries'][$title] = '0';
$expense['entries'][$title] = '0';
}
$currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0); $currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0);
} }
Log::debug('End of sub-loop');
$chartData[] = $income; $chartData[] = $income;
$chartData[] = $expense; $chartData[] = $expense;
} }
Log::debug('End of loop');
$data = $this->generator->multiSet($chartData); $data = $this->generator->multiSet($chartData);
$cache->store($data); $cache->store($data);