Skip to content

Commit

Permalink
Fix #3791
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Sep 11, 2020
1 parent 22dc03f commit 0b308cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
12 changes: 4 additions & 8 deletions app/Http/Controllers/Budget/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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');
Expand All @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
59 changes: 29 additions & 30 deletions app/Http/Controllers/Chart/BudgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
class BudgetController extends Controller
{
use DateCalculation, AugumentData;

/** @var GeneratorInterface Chart generation methods. */
protected $generator;
/** @var OperationsRepositoryInterface */
Expand Down Expand Up @@ -92,17 +93,17 @@ 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);
$cache->addProperty('chart.budget.budget');
$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]);
Expand All @@ -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));

Expand All @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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'],
Expand All @@ -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'],
];
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
];
}

Expand Down Expand Up @@ -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 */
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions resources/views/v1/budgets/show.twig
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,23 @@
</div>
</div>
</div>

{% if limits|length > 0 %}
<div class="row">
<div class="col-lg-offset-9 col-lg-3 col-md-offset-9 col-md-3 col-sm-12 col-xs-12">
<p class="small text-center"><a href="{{ route('budgets.show',budget.id) }}">{{ 'showEverything'|_ }}</a></p>
</div>
</div>
{% endif %}

<div class="row">
<div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
<div class="{% if limits|length > 0 %}col-lg-9 col-md-9 col-sm-12 col-xs-12{% else %}col-lg-12 col-md-12 col-sm-12 col-xs-12{% endif %}">

<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'transactions'|_ }}</h3>
</div>
<div class="box-body">
{% if budgetLimit %}
{% include 'list.groups' %}
{% else %}
{% include 'list.groups' %}
{% endif %}
{% include 'list.groups' %}
{% if budgetLimit %}
<p>
<i class="fa fa-calendar"></i>
Expand All @@ -143,6 +140,7 @@
</div>
</div>
</div>
{% if limits|length > 0 %}
<div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
{% for limit in limits %}
<div class="box {% if limit.start_date == budgetLimit.start_date %}box-primary box-solid{% endif %}">
Expand Down Expand Up @@ -199,6 +197,7 @@
{% endfor %}
<p class="small text-center"><a href="{{ route('budgets.show',budget.id) }}">{{ 'showEverything'|_ }}</a></p>
</div>
{% endif %}
</div>

{% endblock %}
Expand Down

0 comments on commit 0b308cb

Please sign in to comment.