1 | <?php |
||
2 | /** |
||
3 | * BudgetController.php |
||
4 | * Copyright (c) 2019 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License as |
||
10 | * published by the Free Software Foundation, either version 3 of the |
||
11 | * License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU Affero General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Affero General Public License |
||
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\Http\Controllers\Report; |
||
24 | |||
25 | use Carbon\Carbon; |
||
26 | use FireflyIII\Http\Controllers\Controller; |
||
27 | use FireflyIII\Models\Account; |
||
28 | use FireflyIII\Models\Budget; |
||
29 | use FireflyIII\Models\BudgetLimit; |
||
30 | use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; |
||
31 | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
||
32 | use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface; |
||
33 | use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; |
||
34 | use FireflyIII\Support\CacheProperties; |
||
35 | use FireflyIII\Support\Http\Controllers\BasicDataSupport; |
||
36 | use Illuminate\Contracts\View\Factory; |
||
37 | use Illuminate\Support\Collection; |
||
38 | use Illuminate\View\View; |
||
39 | use Log; |
||
40 | use Throwable; |
||
41 | |||
42 | /** |
||
43 | * Class BudgetController. |
||
44 | */ |
||
45 | class BudgetController extends Controller |
||
46 | { |
||
47 | use BasicDataSupport; |
||
48 | |||
49 | /** @var BudgetLimitRepositoryInterface */ |
||
50 | private $blRepository; |
||
51 | /** @var NoBudgetRepositoryInterface */ |
||
52 | private $nbRepository; |
||
53 | /** @var OperationsRepositoryInterface */ |
||
54 | private $opsRepository; |
||
55 | /** @var BudgetRepositoryInterface */ |
||
56 | private $repository; |
||
57 | |||
58 | /** |
||
59 | * ExpenseReportController constructor. |
||
60 | * |
||
61 | * @codeCoverageIgnore |
||
62 | */ |
||
63 | public function __construct() |
||
64 | { |
||
65 | parent::__construct(); |
||
66 | $this->middleware( |
||
67 | function ($request, $next) { |
||
68 | $this->opsRepository = app(OperationsRepositoryInterface::class); |
||
69 | $this->repository = app(BudgetRepositoryInterface::class); |
||
70 | $this->blRepository = app(BudgetLimitRepositoryInterface::class); |
||
71 | $this->nbRepository = app(NoBudgetRepositoryInterface::class); |
||
72 | |||
73 | return $next($request); |
||
74 | } |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param Collection $accounts |
||
80 | * @param Collection $budgets |
||
81 | * @param Carbon $start |
||
82 | * @param Carbon $end |
||
83 | * |
||
84 | * @return Factory|View |
||
85 | */ |
||
86 | public function accountPerBudget(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) |
||
87 | { |
||
88 | $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); |
||
89 | $report = []; |
||
90 | /** @var Account $account */ |
||
91 | foreach ($accounts as $account) { |
||
92 | $accountId = $account->id; |
||
93 | $report[$accountId] = $report[$accountId] ?? [ |
||
94 | 'name' => $account->name, |
||
95 | 'id' => $account->id, |
||
96 | 'iban' => $account->iban, |
||
97 | 'currencies' => [], |
||
98 | ]; |
||
99 | } |
||
100 | |||
101 | // loop expenses. |
||
102 | foreach ($spent as $currency) { |
||
103 | $currencyId = $currency['currency_id']; |
||
104 | |||
105 | foreach ($currency['budgets'] as $budget) { |
||
106 | foreach ($budget['transaction_journals'] as $journal) { |
||
107 | $sourceAccountId = $journal['source_account_id']; |
||
108 | |||
109 | |||
110 | $report[$sourceAccountId]['currencies'][$currencyId] = $report[$sourceAccountId]['currencies'][$currencyId] ?? [ |
||
111 | 'currency_id' => $currency['currency_id'], |
||
112 | 'currency_symbol' => $currency['currency_symbol'], |
||
113 | 'currency_name' => $currency['currency_name'], |
||
114 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
115 | 'budgets' => [], |
||
116 | ]; |
||
117 | $report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budget['id']] |
||
118 | = $report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budget['id']] |
||
119 | ?? '0'; |
||
120 | $report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budget['id']] = bcadd( |
||
121 | $report[$sourceAccountId]['currencies'][$currencyId]['budgets'][$budget['id']], $journal['amount'] |
||
122 | ); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | return view('reports.budget.partials.account-per-budget', compact('report', 'budgets')); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param Collection $accounts |
||
132 | * @param Collection $budgets |
||
133 | * @param Carbon $start |
||
134 | * @param Carbon $end |
||
135 | * |
||
136 | * @return Factory|View |
||
137 | */ |
||
138 | public function accounts(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) |
||
139 | { |
||
140 | $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); |
||
141 | $report = []; |
||
142 | $sums = []; |
||
143 | /** @var Account $account */ |
||
144 | foreach ($accounts as $account) { |
||
145 | $accountId = $account->id; |
||
146 | $report[$accountId] = $report[$accountId] ?? [ |
||
147 | 'name' => $account->name, |
||
148 | 'id' => $account->id, |
||
149 | 'iban' => $account->iban, |
||
150 | 'currencies' => [], |
||
151 | ]; |
||
152 | } |
||
153 | |||
154 | // loop expenses. |
||
155 | foreach ($spent as $currency) { |
||
156 | $currencyId = $currency['currency_id']; |
||
157 | $sums[$currencyId] = $sums[$currencyId] ?? [ |
||
158 | 'currency_id' => $currency['currency_id'], |
||
159 | 'currency_symbol' => $currency['currency_symbol'], |
||
160 | 'currency_name' => $currency['currency_name'], |
||
161 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
162 | 'sum' => '0', |
||
163 | ]; |
||
164 | foreach ($currency['budgets'] as $budget) { |
||
165 | foreach ($budget['transaction_journals'] as $journal) { |
||
166 | $sourceAccountId = $journal['source_account_id']; |
||
167 | $report[$sourceAccountId]['currencies'][$currencyId] = $report[$sourceAccountId]['currencies'][$currencyId] ?? [ |
||
168 | 'currency_id' => $currency['currency_id'], |
||
169 | 'currency_symbol' => $currency['currency_symbol'], |
||
170 | 'currency_name' => $currency['currency_name'], |
||
171 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
172 | 'sum' => '0', |
||
173 | ]; |
||
174 | $report[$sourceAccountId]['currencies'][$currencyId]['sum'] = bcadd( |
||
175 | $report[$sourceAccountId]['currencies'][$currencyId]['sum'], $journal['amount'] |
||
176 | ); |
||
177 | $sums[$currencyId]['sum'] = bcadd($sums[$currencyId]['sum'], $journal['amount']); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | return view('reports.budget.partials.accounts', compact('sums', 'report')); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param Collection $accounts |
||
187 | * @param Collection $budgets |
||
188 | * @param Carbon $start |
||
189 | * @param Carbon $end |
||
190 | * |
||
191 | * @return array|string |
||
192 | */ |
||
193 | public function avgExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) |
||
194 | { |
||
195 | $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); |
||
196 | $result = []; |
||
197 | foreach ($spent as $currency) { |
||
198 | $currencyId = $currency['currency_id']; |
||
199 | foreach ($currency['budgets'] as $budget) { |
||
200 | foreach ($budget['transaction_journals'] as $journal) { |
||
201 | $destinationId = $journal['destination_account_id']; |
||
202 | $key = sprintf('%d-%d', $destinationId, $currency['currency_id']); |
||
203 | $result[$key] = $result[$key] ?? [ |
||
204 | 'transactions' => 0, |
||
205 | 'sum' => '0', |
||
206 | 'avg' => '0', |
||
207 | 'avg_float' => 0, |
||
208 | 'destination_account_name' => $journal['destination_account_name'], |
||
209 | 'destination_account_id' => $journal['destination_account_id'], |
||
210 | 'currency_id' => $currency['currency_id'], |
||
211 | 'currency_name' => $currency['currency_name'], |
||
212 | 'currency_symbol' => $currency['currency_symbol'], |
||
213 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
214 | ]; |
||
215 | $result[$key]['transactions']++; |
||
216 | $result[$key]['sum'] = bcadd($journal['amount'], $result[$key]['sum']); |
||
217 | $result[$key]['avg'] = bcdiv($result[$key]['sum'], (string)$result[$key]['transactions']); |
||
218 | $result[$key]['avg_float'] = (float)$result[$key]['avg']; |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | // sort by amount_float |
||
223 | // sort temp array by amount. |
||
224 | $amounts = array_column($result, 'avg_float'); |
||
225 | array_multisort($amounts, SORT_ASC, $result); |
||
226 | |||
227 | try { |
||
228 | $result = view('reports.budget.partials.avg-expenses', compact('result'))->render(); |
||
229 | // @codeCoverageIgnoreStart |
||
230 | } catch (Throwable $e) { |
||
231 | Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage())); |
||
232 | $result = sprintf('Could not render view: %s', $e->getMessage()); |
||
233 | } |
||
234 | |||
235 | return $result; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param Collection $accounts |
||
240 | * @param Collection $budgets |
||
241 | * @param Carbon $start |
||
242 | * @param Carbon $end |
||
243 | * |
||
244 | * @return Factory|View |
||
245 | */ |
||
246 | public function budgets(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) |
||
247 | { |
||
248 | $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); |
||
249 | $sums = []; |
||
250 | $report = []; |
||
251 | /** @var Budget $budget */ |
||
252 | foreach ($budgets as $budget) { |
||
253 | $budgetId = $budget->id; |
||
254 | $report[$budgetId] = $report[$budgetId] ?? [ |
||
255 | 'name' => $budget->name, |
||
256 | 'id' => $budget->id, |
||
257 | 'currencies' => [], |
||
258 | ]; |
||
259 | } |
||
260 | foreach ($spent as $currency) { |
||
261 | $currencyId = $currency['currency_id']; |
||
262 | $sums[$currencyId] = $sums[$currencyId] ?? [ |
||
263 | 'currency_id' => $currency['currency_id'], |
||
264 | 'currency_symbol' => $currency['currency_symbol'], |
||
265 | 'currency_name' => $currency['currency_name'], |
||
266 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
267 | 'sum' => '0', |
||
268 | ]; |
||
269 | /** @var array $budget */ |
||
270 | foreach ($currency['budgets'] as $budget) { |
||
271 | $budgetId = $budget['id']; |
||
272 | |||
273 | foreach ($budget['transaction_journals'] as $journal) { |
||
274 | // add currency info to report array: |
||
275 | $report[$budgetId]['currencies'][$currencyId] = $report[$budgetId]['currencies'][$currencyId] ?? [ |
||
276 | 'sum' => '0', |
||
277 | 'sum_pct' => '0', |
||
278 | 'currency_id' => $currency['currency_id'], |
||
279 | 'currency_symbol' => $currency['currency_symbol'], |
||
280 | 'currency_name' => $currency['currency_name'], |
||
281 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
282 | |||
283 | ]; |
||
284 | $report[$budgetId]['currencies'][$currencyId]['sum'] = bcadd($report[$budgetId]['currencies'][$currencyId]['sum'], $journal['amount']); |
||
285 | $sums[$currencyId]['sum'] = bcadd($sums[$currencyId]['sum'], $journal['amount']); |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | // loop again to get percentages. |
||
291 | foreach ($report as $budgetId => $data) { |
||
292 | foreach ($data['currencies'] as $currencyId => $data) { |
||
0 ignored issues
–
show
Comprehensibility
Bug
introduced
by
Loading history...
|
|||
293 | $sum = $data['sum'] ?? '0'; |
||
294 | $total = $sums[$currencyId]['sum'] ?? '0'; |
||
295 | $pct = '0'; |
||
296 | if (0 !== bccomp($sum, '0') && 0 !== bccomp($total, '9')) { |
||
297 | $pct = round(bcmul(bcdiv($sum, $total), '100')); |
||
298 | |||
299 | } |
||
300 | $report[$budgetId]['currencies'][$currencyId]['sum_pct'] = $pct; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | return view('reports.budget.partials.budgets', compact('sums', 'report')); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Show partial overview of budgets. |
||
309 | * TODO can be replaced I think. |
||
310 | * |
||
311 | * @param Collection $accounts |
||
312 | * @param Carbon $start |
||
313 | * @param Carbon $end |
||
314 | * |
||
315 | * @return mixed|string |
||
316 | */ |
||
317 | public function general(Collection $accounts, Carbon $start, Carbon $end) |
||
318 | { |
||
319 | $report = [ |
||
320 | 'budgets' => [], |
||
321 | 'sums' => [], |
||
322 | ]; |
||
323 | $budgets = $this->repository->getBudgets(); |
||
324 | $defaultCurrency = app('amount')->getDefaultCurrency(); |
||
325 | /** @var Budget $budget */ |
||
326 | foreach ($budgets as $budget) { |
||
327 | $budgetId = $budget->id; |
||
328 | $report['budgets'][$budgetId] = $report['budgets'][$budgetId] ?? [ |
||
329 | 'budget_id' => $budgetId, |
||
330 | 'budget_name' => $budget->name, |
||
331 | 'no_budget' => false, |
||
332 | 'budget_limits' => [], |
||
333 | ]; |
||
334 | |||
335 | // get all budget limits for budget in period: |
||
336 | $limits = $this->blRepository->getBudgetLimits($budget, $start, $end); |
||
337 | /** @var BudgetLimit $limit */ |
||
338 | foreach ($limits as $limit) { |
||
339 | $limitId = $limit->id; |
||
340 | $currency = $limit->transactionCurrency ?? $defaultCurrency; |
||
341 | $expenses = $this->opsRepository->sumExpenses($limit->start_date, $limit->end_date, $accounts, new Collection([$budget])); |
||
342 | $spent = $expenses[$currency->id]['sum'] ?? '0'; |
||
343 | $left = -1 === bccomp(bcadd($limit->amount, $spent), '0') ? '0' : bcadd($limit->amount, $spent); |
||
344 | $overspent = 1 === bccomp(bcmul($spent, '-1'), $limit->amount) ? bcadd($spent, $limit->amount) : '0'; |
||
345 | |||
346 | $report['budgets'][$budgetId]['budget_limits'][$limitId] = $report['budgets'][$budgetId]['budget_limits'][$limitId] ?? [ |
||
347 | 'budget_limit_id' => $limitId, |
||
348 | 'start_date' => $limit->start_date, |
||
349 | 'end_date' => $limit->end_date, |
||
350 | 'budgeted' => $limit->amount, |
||
351 | 'budgeted_pct' => '0', |
||
352 | 'spent' => $spent, |
||
353 | 'spent_pct' => '0', |
||
354 | 'left' => $left, |
||
355 | 'overspent' => $overspent, |
||
356 | 'currency_id' => $currency->id, |
||
357 | 'currency_code' => $currency->code, |
||
358 | 'currency_name' => $currency->name, |
||
359 | 'currency_symbol' => $currency->symbol, |
||
360 | 'currency_decimal_places' => $currency->decimal_places, |
||
361 | ]; |
||
362 | |||
363 | // make sum information: |
||
364 | $report['sums'][$currency->id] = $report['sums'][$currency->id] ?? [ |
||
365 | 'budgeted' => '0', |
||
366 | 'spent' => '0', |
||
367 | 'left' => '0', |
||
368 | 'overspent' => '0', |
||
369 | 'currency_id' => $currency->id, |
||
370 | 'currency_code' => $currency->code, |
||
371 | 'currency_name' => $currency->name, |
||
372 | 'currency_symbol' => $currency->symbol, |
||
373 | 'currency_decimal_places' => $currency->decimal_places, |
||
374 | ]; |
||
375 | $report['sums'][$currency->id]['budgeted'] = bcadd($report['sums'][$currency->id]['budgeted'], $limit->amount); |
||
376 | $report['sums'][$currency->id]['spent'] = bcadd($report['sums'][$currency->id]['spent'], $spent); |
||
377 | $report['sums'][$currency->id]['left'] = bcadd($report['sums'][$currency->id]['left'], bcadd($limit->amount, $spent)); |
||
378 | $report['sums'][$currency->id]['overspent'] = bcadd($report['sums'][$currency->id]['overspent'], $overspent); |
||
379 | } |
||
380 | } |
||
381 | |||
382 | // add no budget info. |
||
383 | $report['budgets'][0] = $report['budgets'][0] ?? [ |
||
384 | 'budget_id' => null, |
||
385 | 'budget_name' => null, |
||
386 | 'no_budget' => true, |
||
387 | 'budget_limits' => [], |
||
388 | ]; |
||
389 | $noBudget = $this->nbRepository->sumExpenses($start, $end); |
||
390 | foreach ($noBudget as $noBudgetEntry) { |
||
391 | $report['budgets'][0]['budget_limits'][] = [ |
||
392 | 'budget_limit_id' => null, |
||
393 | 'start_date' => $start, |
||
394 | 'end_date' => $end, |
||
395 | 'budgeted' => '0', |
||
396 | 'budgeted_pct' => '0', |
||
397 | 'spent' => $noBudgetEntry['sum'], |
||
398 | 'spent_pct' => '0', |
||
399 | 'left' => '0', |
||
400 | 'overspent' => '0', |
||
401 | 'currency_id' => $noBudgetEntry['currency_id'], |
||
402 | 'currency_code' => $noBudgetEntry['currency_code'], |
||
403 | 'currency_name' => $noBudgetEntry['currency_name'], |
||
404 | 'currency_symbol' => $noBudgetEntry['currency_symbol'], |
||
405 | 'currency_decimal_places' => $noBudgetEntry['currency_decimal_places'], |
||
406 | ]; |
||
407 | $report['sums'][$noBudgetEntry['currency_id']]['spent'] |
||
408 | = bcadd($report['sums'][$noBudgetEntry['currency_id']]['spent'] ?? '0', $noBudgetEntry['sum']); |
||
409 | } |
||
410 | |||
411 | // make percentages based on total amount. |
||
412 | foreach ($report['budgets'] as $budgetId => $data) { |
||
413 | foreach ($data['budget_limits'] as $limitId => $entry) { |
||
414 | $currencyId = $entry['currency_id']; |
||
415 | |||
416 | $spent = $entry['spent']; |
||
417 | $totalSpent = $report['sums'][$currencyId]['spent'] ?? '0'; |
||
418 | $spentPct = '0'; |
||
419 | |||
420 | $budgeted = $entry['budgeted']; |
||
421 | $totalBudgeted = $report['sums'][$currencyId]['budgeted'] ?? '0';; |
||
422 | $budgetedPct = '0'; |
||
423 | |||
424 | if (0 !== bccomp($spent, '0') && 0 !== bccomp($totalSpent, '0')) { |
||
425 | $spentPct = round(bcmul(bcdiv($spent, $totalSpent), '100')); |
||
426 | } |
||
427 | if (0 !== bccomp($budgeted, '0') && 0 !== bccomp($totalBudgeted, '0')) { |
||
428 | $budgetedPct = round(bcmul(bcdiv($budgeted, $totalBudgeted), '100')); |
||
429 | } |
||
430 | $report['budgets'][$budgetId]['budget_limits'][$limitId]['spent_pct'] = $spentPct; |
||
431 | $report['budgets'][$budgetId]['budget_limits'][$limitId]['budgeted_pct'] = $budgetedPct; |
||
432 | } |
||
433 | } |
||
434 | |||
435 | // var_dump($noBudget); |
||
436 | // |
||
437 | // |
||
438 | // echo '<pre>'; |
||
439 | // print_r($report); |
||
440 | // exit; |
||
441 | // try { |
||
442 | $result = view('reports.partials.budgets', compact('report'))->render(); |
||
443 | // @codeCoverageIgnoreStart |
||
444 | // } catch (Throwable $e) { |
||
445 | // Log::debug(sprintf('Could not render reports.partials.budgets: %s', $e->getMessage())); |
||
446 | // $result = 'Could not render view.'; |
||
447 | // } |
||
448 | |||
449 | // @codeCoverageIgnoreEnd |
||
450 | |||
451 | return $result; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Show budget overview for a period. |
||
456 | * |
||
457 | * @param Collection $accounts |
||
458 | * @param Carbon $start |
||
459 | * @param Carbon $end |
||
460 | * |
||
461 | * @return mixed|string |
||
462 | */ |
||
463 | public function period(Collection $accounts, Carbon $start, Carbon $end) |
||
464 | { |
||
465 | $cache = new CacheProperties; |
||
466 | $cache->addProperty($start); |
||
467 | $cache->addProperty($end); |
||
468 | $cache->addProperty('budget-period-report'); |
||
469 | $cache->addProperty($accounts->pluck('id')->toArray()); |
||
470 | if ($cache->has()) { |
||
471 | return $cache->get(); // @codeCoverageIgnore |
||
472 | } |
||
473 | |||
474 | $periods = app('navigation')->listOfPeriods($start, $end); |
||
475 | $keyFormat = app('navigation')->preferredCarbonFormat($start, $end); |
||
476 | |||
477 | |||
478 | // list expenses for budgets in account(s) |
||
479 | $expenses = $this->opsRepository->listExpenses($start, $end, $accounts); |
||
480 | |||
481 | $report = []; |
||
482 | foreach ($expenses as $currency) { |
||
483 | foreach ($currency['budgets'] as $budget) { |
||
484 | $count = 0; |
||
485 | foreach ($budget['transaction_journals'] as $journal) { |
||
486 | $count++; |
||
487 | $key = sprintf('%d-%d', $budget['id'], $currency['currency_id']); |
||
488 | $dateKey = $journal['date']->format($keyFormat); |
||
489 | $report[$key] = $report[$key] ?? [ |
||
490 | 'id' => $budget['id'], |
||
491 | 'name' => sprintf('%s (%s)', $budget['name'], $currency['currency_name']), |
||
492 | 'sum' => '0', |
||
493 | 'currency_id' => $currency['currency_id'], |
||
494 | 'currency_name' => $currency['currency_name'], |
||
495 | 'currency_symbol' => $currency['currency_symbol'], |
||
496 | 'currency_code' => $currency['currency_code'], |
||
497 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
498 | 'entries' => [], |
||
499 | ]; |
||
500 | $report[$key]['entries'][$dateKey] = $report[$key] ['entries'][$dateKey] ?? '0'; |
||
501 | $report[$key]['entries'][$dateKey] = bcadd($journal['amount'], $report[$key] ['entries'][$dateKey]); |
||
502 | $report[$key]['sum'] = bcadd($report[$key] ['sum'], $journal['amount']); |
||
503 | $report[$key]['avg'] = bcdiv($report[$key]['sum'], (string)$count); |
||
504 | } |
||
505 | } |
||
506 | } |
||
507 | try { |
||
508 | $result = view('reports.partials.budget-period', compact('report', 'periods'))->render(); |
||
509 | // @codeCoverageIgnoreStart |
||
510 | } catch (Throwable $e) { |
||
511 | Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage())); |
||
512 | $result = 'Could not render view.'; |
||
513 | } |
||
514 | // @codeCoverageIgnoreEnd |
||
515 | $cache->store($result); |
||
516 | |||
517 | return $result; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * @param Collection $accounts |
||
522 | * @param Collection $budgets |
||
523 | * @param Carbon $start |
||
524 | * @param Carbon $end |
||
525 | * |
||
526 | * @return array|string |
||
527 | */ |
||
528 | public function topExpenses(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end) |
||
529 | { |
||
530 | $spent = $this->opsRepository->listExpenses($start, $end, $accounts, $budgets); |
||
531 | $result = []; |
||
532 | foreach ($spent as $currency) { |
||
533 | $currencyId = $currency['currency_id']; |
||
534 | foreach ($currency['budgets'] as $budget) { |
||
535 | foreach ($budget['transaction_journals'] as $journal) { |
||
536 | $result[] = [ |
||
537 | 'description' => $journal['description'], |
||
538 | 'transaction_group_id' => $journal['transaction_group_id'], |
||
539 | 'amount_float' => (float)$journal['amount'], |
||
540 | 'amount' => $journal['amount'], |
||
541 | 'date' => $journal['date']->formatLocalized($this->monthAndDayFormat), |
||
542 | 'destination_account_name' => $journal['destination_account_name'], |
||
543 | 'destination_account_id' => $journal['destination_account_id'], |
||
544 | 'currency_id' => $currency['currency_id'], |
||
545 | 'currency_name' => $currency['currency_name'], |
||
546 | 'currency_symbol' => $currency['currency_symbol'], |
||
547 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
548 | 'budget_id' => $budget['id'], |
||
549 | 'budget_name' => $budget['name'], |
||
550 | ]; |
||
551 | } |
||
552 | } |
||
553 | } |
||
554 | // sort by amount_float |
||
555 | // sort temp array by amount. |
||
556 | $amounts = array_column($result, 'amount_float'); |
||
557 | array_multisort($amounts, SORT_ASC, $result); |
||
558 | |||
559 | try { |
||
560 | $result = view('reports.budget.partials.top-expenses', compact('result'))->render(); |
||
561 | // @codeCoverageIgnoreStart |
||
562 | } catch (Throwable $e) { |
||
563 | Log::debug(sprintf('Could not render reports.partials.budget-period: %s', $e->getMessage())); |
||
564 | $result = sprintf('Could not render view: %s', $e->getMessage()); |
||
565 | } |
||
566 | |||
567 | return $result; |
||
568 | } |
||
569 | |||
570 | } |
||
571 |