diff --git a/app/Http/Controllers/Budget/ShowController.php b/app/Http/Controllers/Budget/ShowController.php index 0fa9bd416fd..45a3bdc1784 100644 --- a/app/Http/Controllers/Budget/ShowController.php +++ b/app/Http/Controllers/Budget/ShowController.php @@ -123,7 +123,7 @@ public function noBudgetAll(Request $request) $subTitle = (string) trans('firefly.all_journals_without_budget'); $first = $this->journalRepos->firstNull(); $start = null === $first ? new Carbon : $first->date; - $end = new Carbon; + $end = today(config('app.timezone')); $page = (int) $request->get('page'); $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; @@ -150,11 +150,7 @@ public function show(Request $request, Budget $budget) { /** @var Carbon $start */ $allStart = session('first', Carbon::now()->startOfYear()); - $allEnd = new Carbon; - - // use session range: - $start = session('start'); - $end = session('end'); + $allEnd = today(); $page = (int) $request->get('page'); @@ -166,7 +162,7 @@ public function show(Request $request, Budget $budget) // collector: /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); - $collector->setRange($start, $end)->setBudget($budget) + $collector->setRange($allStart, $allEnd)->setBudget($budget) ->withAccountInformation() ->setLimit($pageSize)->setPage($page)->withBudgetInformation()->withCategoryInformation(); $groups = $collector->getPaginatedGroups(); @@ -215,7 +211,7 @@ public function showByBudgetLimit(Request $request, Budget $budget, BudgetLimit $groups->setPath(route('budgets.show', [$budget->id, $budgetLimit->id])); /** @var Carbon $start */ $start = session('first', Carbon::now()->startOfYear()); - $end = new Carbon; + $end = today(config('app.timezone')); $attachments = $this->repository->getAttachments($budget); $limits = $this->getLimits($budget, $start, $end); diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 12a898f5a53..ba44248e2b0 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -48,6 +48,7 @@ class BudgetController extends Controller { use DateCalculation, AugumentData; + /** @var GeneratorInterface Chart generation methods. */ protected $generator; /** @var OperationsRepositoryInterface */ @@ -92,9 +93,9 @@ function ($request, $next) { public function budget(Budget $budget): JsonResponse { /** @var Carbon $start */ - $start = $this->repository->firstUseDate($budget) ?? session('start', new Carbon); + $start = $this->repository->firstUseDate($budget) ?? session('start', today(config('app.timezone'))); /** @var Carbon $end */ - $end = session('end', new Carbon); + $end = session('end', today(config('app.timezone'))); $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); @@ -102,7 +103,7 @@ public function budget(Budget $budget): JsonResponse $cache->addProperty($budget->id); if ($cache->has()) { - return response()->json($cache->get()); // @codeCoverageIgnore + //return response()->json($cache->get()); // @codeCoverageIgnore } $step = $this->calculateStep($start, $end); // depending on diff, do something with range of chart. $collection = new Collection([$budget]); @@ -114,9 +115,6 @@ public function budget(Budget $budget): JsonResponse while ($end >= $loopStart) { /** @var Carbon $currentEnd */ $loopEnd = app('navigation')->endOfPeriod($loopStart, $step); - if ('1Y' === $step) { - $loopEnd->subDay(); // @codeCoverageIgnore - } $spent = $this->opsRepository->sumExpenses($loopStart, $loopEnd, null, $collection); $label = trim(app('navigation')->periodShow($loopStart, $step)); @@ -141,7 +139,7 @@ public function budget(Budget $budget): JsonResponse 'entries' => $defaultEntries, ]; foreach ($currency['spent'] as $label => $spent) { - $chartData[$currencyId]['entries'][$label] = round(bcmul($spent, '-1'), $currency['currency_decimal_places']); + $chartData[$currencyId]['entries'][$label] = bcmul($spent, '-1'); } } $data = $this->generator->multiSet(array_values($chartData)); @@ -179,19 +177,19 @@ public function budgetLimit(Budget $budget, BudgetLimit $budgetLimit): JsonRespo if ($cache->has()) { return response()->json($cache->get()); // @codeCoverageIgnore } - $locale = app('steam')->getLocale(); + $locale = app('steam')->getLocale(); $entries = []; $amount = $budgetLimit->amount; $budgetCollection = new Collection([$budget]); while ($start <= $end) { $spent = $this->opsRepository->spentInPeriod($budgetCollection, new Collection, $start, $start); $amount = bcadd($amount, $spent); - $format = $start->formatLocalized((string)trans('config.month_and_day', [], $locale)); + $format = $start->formatLocalized((string) trans('config.month_and_day', [], $locale)); $entries[$format] = $amount; $start->addDay(); } - $data = $this->generator->singleSet((string)trans('firefly.left'), $entries); + $data = $this->generator->singleSet((string) trans('firefly.left'), $entries); // add currency symbol from budget limit: $data['datasets'][0]['currency_symbol'] = $budgetLimit->transactionCurrency->symbol; $data['datasets'][0]['currency_code'] = $budgetLimit->transactionCurrency->code; @@ -218,8 +216,9 @@ public function expenseAsset(Budget $budget, ?BudgetLimit $budgetLimit = null): $cache->addProperty($budget->id); $cache->addProperty($budgetLimitId); $cache->addProperty('chart.budget.expense-asset'); - $start = session()->get('start'); - $end = session()->get('end'); + $start = session('first', Carbon::now()->startOfYear()); + $end = today(); + if (null !== $budgetLimit) { $start = $budgetLimit->start_date; $end = $budgetLimit->end_date; @@ -239,7 +238,7 @@ public function expenseAsset(Budget $budget, ?BudgetLimit $budgetLimit = null): // group by asset account ID: foreach ($journals as $journal) { - $key = sprintf('%d-%d', (int)$journal['source_account_id'], $journal['currency_id']); + $key = sprintf('%d-%d', (int) $journal['source_account_id'], $journal['currency_id']); $result[$key] = $result[$key] ?? [ 'amount' => '0', 'currency_symbol' => $journal['currency_symbol'], @@ -252,13 +251,13 @@ public function expenseAsset(Budget $budget, ?BudgetLimit $budgetLimit = null): $names = $this->getAccountNames(array_keys($result)); foreach ($result as $combinedId => $info) { $parts = explode('-', $combinedId); - $assetId = (int)$parts[0]; + $assetId = (int) $parts[0]; $title = sprintf('%s (%s)', $names[$assetId] ?? '(empty)', $info['currency_name']); $chartData[$title] = [ 'amount' => $info['amount'], 'currency_symbol' => $info['currency_symbol'], - 'currency_code' => $info['currency_code'], + 'currency_code' => $info['currency_code'], ]; } @@ -286,8 +285,8 @@ public function expenseCategory(Budget $budget, ?BudgetLimit $budgetLimit = null $cache->addProperty($budget->id); $cache->addProperty($budgetLimitId); $cache->addProperty('chart.budget.expense-category'); - $start = session()->get('start'); - $end = session()->get('end'); + $start = session('first', Carbon::now()->startOfYear()); + $end = today(); if (null !== $budgetLimit) { $start = $budgetLimit->start_date; $end = $budgetLimit->end_date; @@ -319,12 +318,12 @@ public function expenseCategory(Budget $budget, ?BudgetLimit $budgetLimit = null $names = $this->getCategoryNames(array_keys($result)); foreach ($result as $combinedId => $info) { $parts = explode('-', $combinedId); - $categoryId = (int)$parts[0]; + $categoryId = (int) $parts[0]; $title = sprintf('%s (%s)', $names[$categoryId] ?? '(empty)', $info['currency_name']); $chartData[$title] = [ 'amount' => $info['amount'], 'currency_symbol' => $info['currency_symbol'], - 'currency_code' => $info['currency_code'], + 'currency_code' => $info['currency_code'], ]; } $data = $this->generator->multiCurrencyPieChart($chartData); @@ -352,8 +351,8 @@ public function expenseExpense(Budget $budget, ?BudgetLimit $budgetLimit = null) $cache->addProperty($budget->id); $cache->addProperty($budgetLimitId); $cache->addProperty('chart.budget.expense-expense'); - $start = session()->get('start'); - $end = session()->get('end'); + $start = session('first', Carbon::now()->startOfYear()); + $end = today(); if (null !== $budgetLimit) { $start = $budgetLimit->start_date; $end = $budgetLimit->end_date; @@ -385,13 +384,13 @@ public function expenseExpense(Budget $budget, ?BudgetLimit $budgetLimit = null) $names = $this->getAccountNames(array_keys($result)); foreach ($result as $combinedId => $info) { $parts = explode('-', $combinedId); - $opposingId = (int)$parts[0]; + $opposingId = (int) $parts[0]; $name = $names[$opposingId] ?? 'no name'; $title = sprintf('%s (%s)', $name, $info['currency_name']); $chartData[$title] = [ 'amount' => $info['amount'], 'currency_symbol' => $info['currency_symbol'], - 'currency_code' => $info['currency_code'], + 'currency_code' => $info['currency_code'], ]; } @@ -421,13 +420,13 @@ public function frontpage(): JsonResponse $cache->addProperty($end); $cache->addProperty('chart.budget.frontpage'); if ($cache->has()) { - return response()->json($cache->get()); // @codeCoverageIgnore + return response()->json($cache->get()); // @codeCoverageIgnore } $budgets = $this->repository->getActiveBudgets(); $chartData = [ - ['label' => (string)trans('firefly.spent_in_budget'), 'entries' => [], 'type' => 'bar'], - ['label' => (string)trans('firefly.left_to_spend'), 'entries' => [], 'type' => 'bar'], - ['label' => (string)trans('firefly.overspent'), 'entries' => [], 'type' => 'bar'], + ['label' => (string) trans('firefly.spent_in_budget'), 'entries' => [], 'type' => 'bar'], + ['label' => (string) trans('firefly.left_to_spend'), 'entries' => [], 'type' => 'bar'], + ['label' => (string) trans('firefly.overspent'), 'entries' => [], 'type' => 'bar'], ]; /** @var Budget $budget */ @@ -439,8 +438,8 @@ public function frontpage(): JsonResponse foreach ($spent as $entry) { $title = sprintf('%s (%s)', $budget->name, $entry['currency_name']); $chartData[0]['entries'][$title] = bcmul($entry['sum'], '-1'); // spent - $chartData[1]['entries'][$title] = 0; // left to spend - $chartData[2]['entries'][$title] = 0; // overspent + $chartData[1]['entries'][$title] = 0; // left to spend + $chartData[2]['entries'][$title] = 0; // overspent } } if (0 !== $limits->count()) { @@ -596,7 +595,7 @@ public function periodNoBudget(TransactionCurrency $currency, Collection $accoun $currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0); } - $data = $this->generator->singleSet((string)trans('firefly.spent'), $chartData); + $data = $this->generator->singleSet((string) trans('firefly.spent'), $chartData); $cache->store($data); return response()->json($data); diff --git a/resources/views/v1/budgets/show.twig b/resources/views/v1/budgets/show.twig index b114f732c9c..d4cfbdb76a8 100644 --- a/resources/views/v1/budgets/show.twig +++ b/resources/views/v1/budgets/show.twig @@ -112,26 +112,23 @@ - + {% if limits|length > 0 %}

{{ 'showEverything'|_ }}

+ {% endif %}
-
+

{{ 'transactions'|_ }}

- {% if budgetLimit %} - {% include 'list.groups' %} - {% else %} - {% include 'list.groups' %} - {% endif %} + {% include 'list.groups' %} {% if budgetLimit %}

@@ -143,6 +140,7 @@

+ {% if limits|length > 0 %}
{% for limit in limits %}
@@ -199,6 +197,7 @@ {% endfor %}

{{ 'showEverything'|_ }}

+ {% endif %}
{% endblock %}