1 | <?php |
||||
2 | /** |
||||
3 | * RenderPartialViews.php |
||||
4 | * Copyright (c) 2018 [email protected] |
||||
5 | * |
||||
6 | * This file is part of Firefly III. |
||||
7 | * |
||||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||||
9 | * it under the terms of the GNU General Public License as published by |
||||
10 | * the Free Software Foundation, either version 3 of the License, or |
||||
11 | * (at your option) any later version. |
||||
12 | * |
||||
13 | * Firefly III 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 General Public License for more details. |
||||
17 | * |
||||
18 | * You should have received a copy of the GNU General Public License |
||||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||||
20 | */ |
||||
21 | |||||
22 | declare(strict_types=1); |
||||
23 | |||||
24 | namespace FireflyIII\Support\Http\Controllers; |
||||
25 | |||||
26 | use FireflyIII\Helpers\Collection\BalanceLine; |
||||
27 | use FireflyIII\Helpers\Report\PopupReportInterface; |
||||
28 | use FireflyIII\Models\AccountType; |
||||
29 | use FireflyIII\Models\Budget; |
||||
30 | use FireflyIII\Models\Rule; |
||||
31 | use FireflyIII\Models\RuleAction; |
||||
32 | use FireflyIII\Models\RuleTrigger; |
||||
33 | use FireflyIII\Models\Tag; |
||||
34 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||||
35 | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
||||
36 | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; |
||||
37 | use FireflyIII\Repositories\Tag\TagRepositoryInterface; |
||||
38 | use Illuminate\Support\Collection; |
||||
39 | use Log; |
||||
40 | use Throwable; |
||||
41 | |||||
42 | /** |
||||
43 | * Trait RenderPartialViews |
||||
44 | * |
||||
45 | */ |
||||
46 | trait RenderPartialViews |
||||
47 | { |
||||
48 | |||||
49 | /** |
||||
50 | * Get options for account report. |
||||
51 | * |
||||
52 | * @return string |
||||
53 | */ |
||||
54 | protected function accountReportOptions(): string // render a view |
||||
55 | { |
||||
56 | /** @var AccountRepositoryInterface $repository */ |
||||
57 | $repository = app(AccountRepositoryInterface::class); |
||||
58 | $expense = $repository->getActiveAccountsByType([AccountType::EXPENSE]); |
||||
59 | $revenue = $repository->getActiveAccountsByType([AccountType::REVENUE]); |
||||
60 | $set = new Collection; |
||||
61 | $names = $revenue->pluck('name')->toArray(); |
||||
62 | foreach ($expense as $exp) { |
||||
63 | if (\in_array($exp->name, $names, true)) { |
||||
64 | $set->push($exp); |
||||
65 | } |
||||
66 | } |
||||
67 | try { |
||||
68 | $result = view('reports.options.account', compact('set'))->render(); |
||||
69 | } catch (Throwable $e) { |
||||
70 | Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); |
||||
71 | $result = 'Could not render view.'; |
||||
72 | } |
||||
73 | |||||
74 | return $result; |
||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * View for balance row. |
||||
79 | * |
||||
80 | * @param array $attributes |
||||
81 | * |
||||
82 | * @return string |
||||
83 | * |
||||
84 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||||
85 | */ |
||||
86 | protected function balanceAmount(array $attributes): string // generate view for report. |
||||
87 | { |
||||
88 | $role = (int)$attributes['role']; |
||||
89 | /** @var BudgetRepositoryInterface $budgetRepository */ |
||||
90 | $budgetRepository = app(BudgetRepositoryInterface::class); |
||||
91 | |||||
92 | /** @var AccountRepositoryInterface $accountRepository */ |
||||
93 | $accountRepository = app(AccountRepositoryInterface::class); |
||||
94 | |||||
95 | /** @var PopupReportInterface $popupHelper */ |
||||
96 | $popupHelper = app(PopupReportInterface::class); |
||||
97 | |||||
98 | $budget = $budgetRepository->findNull((int)$attributes['budgetId']); |
||||
99 | $account = $accountRepository->findNull((int)$attributes['accountId']); |
||||
100 | |||||
101 | |||||
102 | switch (true) { |
||||
103 | case BalanceLine::ROLE_DEFAULTROLE === $role && null !== $budget && null !== $account: |
||||
104 | // normal row with a budget: |
||||
105 | $journals = $popupHelper->balanceForBudget($budget, $account, $attributes); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
106 | break; |
||||
107 | case BalanceLine::ROLE_DEFAULTROLE === $role && null === $budget && null !== $account: |
||||
108 | // normal row without a budget: |
||||
109 | $budget = new Budget; |
||||
110 | $journals = $popupHelper->balanceForNoBudget($account, $attributes); |
||||
0 ignored issues
–
show
It seems like
$account can also be of type null ; however, parameter $account of FireflyIII\Helpers\Repor...e::balanceForNoBudget() does only seem to accept FireflyIII\Models\Account , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
111 | $budget->name = (string)trans('firefly.no_budget'); |
||||
112 | break; |
||||
113 | case BalanceLine::ROLE_TAGROLE === $role: |
||||
114 | // row with tag info. |
||||
115 | return 'Firefly cannot handle this type of info-button (BalanceLine::TagRole)'; |
||||
116 | } |
||||
117 | try { |
||||
118 | $view = view('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render(); |
||||
119 | } catch (Throwable $e) { |
||||
120 | Log::error(sprintf('Could not render: %s', $e->getMessage())); |
||||
121 | $view = 'Firefly III could not render the view. Please see the log files.'; |
||||
122 | } |
||||
123 | |||||
124 | return $view; |
||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * Get options for budget report. |
||||
129 | * |
||||
130 | * @return string |
||||
131 | */ |
||||
132 | protected function budgetReportOptions(): string // render a view |
||||
133 | { |
||||
134 | /** @var BudgetRepositoryInterface $repository */ |
||||
135 | $repository = app(BudgetRepositoryInterface::class); |
||||
136 | $budgets = $repository->getBudgets(); |
||||
137 | try { |
||||
138 | $result = view('reports.options.budget', compact('budgets'))->render(); |
||||
139 | } catch (Throwable $e) { |
||||
140 | Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); |
||||
141 | $result = 'Could not render view.'; |
||||
142 | } |
||||
143 | |||||
144 | return $result; |
||||
145 | } |
||||
146 | |||||
147 | /** |
||||
148 | * View for spent in a single budget. |
||||
149 | * |
||||
150 | * @param array $attributes |
||||
151 | * |
||||
152 | * @return string |
||||
153 | */ |
||||
154 | protected function budgetSpentAmount(array $attributes): string // generate view for report. |
||||
155 | { |
||||
156 | /** @var BudgetRepositoryInterface $budgetRepository */ |
||||
157 | $budgetRepository = app(BudgetRepositoryInterface::class); |
||||
158 | |||||
159 | /** @var PopupReportInterface $popupHelper */ |
||||
160 | $popupHelper = app(PopupReportInterface::class); |
||||
161 | |||||
162 | $budget = $budgetRepository->findNull((int)$attributes['budgetId']); |
||||
163 | if (null === $budget) { |
||||
164 | $budget = new Budget; |
||||
165 | } |
||||
166 | $journals = $popupHelper->byBudget($budget, $attributes); |
||||
167 | try { |
||||
168 | $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render(); |
||||
169 | } catch (Throwable $e) { |
||||
170 | Log::error(sprintf('Could not render: %s', $e->getMessage())); |
||||
171 | $view = 'Firefly III could not render the view. Please see the log files.'; |
||||
172 | } |
||||
173 | |||||
174 | return $view; |
||||
175 | } |
||||
176 | |||||
177 | /** |
||||
178 | * View for transactions in a category. |
||||
179 | * |
||||
180 | * @param array $attributes |
||||
181 | * |
||||
182 | * @return string |
||||
183 | */ |
||||
184 | protected function categoryEntry(array $attributes): string // generate view for report. |
||||
185 | { |
||||
186 | /** @var PopupReportInterface $popupHelper */ |
||||
187 | $popupHelper = app(PopupReportInterface::class); |
||||
188 | |||||
189 | /** @var CategoryRepositoryInterface $categoryRepository */ |
||||
190 | $categoryRepository = app(CategoryRepositoryInterface::class); |
||||
191 | $category = $categoryRepository->findNull((int)$attributes['categoryId']); |
||||
192 | |||||
193 | if (null === $category) { |
||||
194 | return 'This is an unknown category. Apologies.'; |
||||
195 | } |
||||
196 | |||||
197 | $journals = $popupHelper->byCategory($category, $attributes); |
||||
198 | try { |
||||
199 | $view = view('popup.report.category-entry', compact('journals', 'category'))->render(); |
||||
200 | } catch (Throwable $e) { |
||||
201 | Log::error(sprintf('Could not render: %s', $e->getMessage())); |
||||
202 | $view = 'Firefly III could not render the view. Please see the log files.'; |
||||
203 | } |
||||
204 | |||||
205 | return $view; |
||||
206 | } |
||||
207 | |||||
208 | /** |
||||
209 | * Get options for category report. |
||||
210 | * |
||||
211 | * @return string |
||||
212 | */ |
||||
213 | protected function categoryReportOptions(): string // render a view |
||||
214 | { |
||||
215 | /** @var CategoryRepositoryInterface $repository */ |
||||
216 | $repository = app(CategoryRepositoryInterface::class); |
||||
217 | $categories = $repository->getCategories(); |
||||
218 | try { |
||||
219 | $result = view('reports.options.category', compact('categories'))->render(); |
||||
220 | } catch (Throwable $e) { |
||||
221 | Log::error(sprintf('Cannot render reports.options.category: %s', $e->getMessage())); |
||||
222 | $result = 'Could not render view.'; |
||||
223 | } |
||||
224 | |||||
225 | return $result; |
||||
226 | } |
||||
227 | |||||
228 | /** |
||||
229 | * Returns all the expenses that went to the given expense account. |
||||
230 | * |
||||
231 | * @param array $attributes |
||||
232 | * |
||||
233 | * @return string |
||||
234 | */ |
||||
235 | protected function expenseEntry(array $attributes): string // generate view for report. |
||||
236 | { |
||||
237 | /** @var AccountRepositoryInterface $accountRepository */ |
||||
238 | $accountRepository = app(AccountRepositoryInterface::class); |
||||
239 | |||||
240 | /** @var PopupReportInterface $popupHelper */ |
||||
241 | $popupHelper = app(PopupReportInterface::class); |
||||
242 | |||||
243 | $account = $accountRepository->findNull((int)$attributes['accountId']); |
||||
244 | |||||
245 | if (null === $account) { |
||||
246 | return 'This is an unknown account. Apologies.'; |
||||
247 | } |
||||
248 | |||||
249 | $journals = $popupHelper->byExpenses($account, $attributes); |
||||
250 | try { |
||||
251 | $view = view('popup.report.expense-entry', compact('journals', 'account'))->render(); |
||||
252 | } catch (Throwable $e) { |
||||
253 | Log::error(sprintf('Could not render: %s', $e->getMessage())); |
||||
254 | $view = 'Firefly III could not render the view. Please see the log files.'; |
||||
255 | } |
||||
256 | |||||
257 | return $view; |
||||
258 | } |
||||
259 | |||||
260 | /** |
||||
261 | * Get current (from system) rule actions. |
||||
262 | * |
||||
263 | * @param Rule $rule |
||||
264 | * |
||||
265 | * @return array |
||||
266 | */ |
||||
267 | protected function getCurrentActions(Rule $rule): array // get info from object and present. |
||||
268 | { |
||||
269 | $index = 0; |
||||
270 | $actions = []; |
||||
271 | // todo must be repos |
||||
272 | $currentActions = $rule->ruleActions()->orderBy('order', 'ASC')->get(); |
||||
273 | /** @var RuleAction $entry */ |
||||
274 | foreach ($currentActions as $entry) { |
||||
275 | $count = ($index + 1); |
||||
276 | try { |
||||
277 | $actions[] = view( |
||||
278 | 'rules.partials.action', |
||||
279 | [ |
||||
280 | 'oldAction' => $entry->action_type, |
||||
281 | 'oldValue' => $entry->action_value, |
||||
282 | 'oldChecked' => $entry->stop_processing, |
||||
283 | 'count' => $count, |
||||
284 | ] |
||||
285 | )->render(); |
||||
286 | // @codeCoverageIgnoreStart |
||||
287 | } catch (Throwable $e) { |
||||
288 | Log::debug(sprintf('Throwable was thrown in getCurrentActions(): %s', $e->getMessage())); |
||||
289 | Log::error($e->getTraceAsString()); |
||||
290 | } |
||||
291 | // @codeCoverageIgnoreEnd |
||||
292 | ++$index; |
||||
293 | } |
||||
294 | |||||
295 | return $actions; |
||||
296 | } |
||||
297 | |||||
298 | /** |
||||
299 | * Get current (from DB) rule triggers. |
||||
300 | * |
||||
301 | * @param Rule $rule |
||||
302 | * |
||||
303 | * @return array |
||||
304 | * |
||||
305 | */ |
||||
306 | protected function getCurrentTriggers(Rule $rule): array // get info from object and present. |
||||
307 | { |
||||
308 | $index = 0; |
||||
309 | $triggers = []; |
||||
310 | // todo must be repos |
||||
311 | $currentTriggers = $rule->ruleTriggers()->orderBy('order', 'ASC')->get(); |
||||
312 | /** @var RuleTrigger $entry */ |
||||
313 | foreach ($currentTriggers as $entry) { |
||||
314 | if ('user_action' !== $entry->trigger_type) { |
||||
315 | $count = ($index + 1); |
||||
316 | try { |
||||
317 | $triggers[] = view( |
||||
318 | 'rules.partials.trigger', |
||||
319 | [ |
||||
320 | 'oldTrigger' => $entry->trigger_type, |
||||
321 | 'oldValue' => $entry->trigger_value, |
||||
322 | 'oldChecked' => $entry->stop_processing, |
||||
323 | 'count' => $count, |
||||
324 | ] |
||||
325 | )->render(); |
||||
326 | // @codeCoverageIgnoreStart |
||||
327 | } catch (Throwable $e) { |
||||
328 | Log::debug(sprintf('Throwable was thrown in getCurrentTriggers(): %s', $e->getMessage())); |
||||
329 | Log::error($e->getTraceAsString()); |
||||
330 | } |
||||
331 | // @codeCoverageIgnoreEnd |
||||
332 | ++$index; |
||||
333 | } |
||||
334 | } |
||||
335 | |||||
336 | return $triggers; |
||||
337 | } |
||||
338 | |||||
339 | /** |
||||
340 | * Returns all the incomes that went to the given asset account. |
||||
341 | * |
||||
342 | * @param array $attributes |
||||
343 | * |
||||
344 | * @return string |
||||
345 | */ |
||||
346 | protected function incomeEntry(array $attributes): string // generate view for report. |
||||
347 | { |
||||
348 | /** @var AccountRepositoryInterface $accountRepository */ |
||||
349 | $accountRepository = app(AccountRepositoryInterface::class); |
||||
350 | |||||
351 | /** @var PopupReportInterface $popupHelper */ |
||||
352 | $popupHelper = app(PopupReportInterface::class); |
||||
353 | |||||
354 | $account = $accountRepository->findNull((int)$attributes['accountId']); |
||||
355 | |||||
356 | if (null === $account) { |
||||
357 | return 'This is an unknown category. Apologies.'; |
||||
358 | } |
||||
359 | |||||
360 | $journals = $popupHelper->byIncome($account, $attributes); |
||||
361 | try { |
||||
362 | $view = view('popup.report.income-entry', compact('journals', 'account'))->render(); |
||||
363 | } catch (Throwable $e) { |
||||
364 | Log::error(sprintf('Could not render: %s', $e->getMessage())); |
||||
365 | $view = 'Firefly III could not render the view. Please see the log files.'; |
||||
366 | } |
||||
367 | |||||
368 | return $view; |
||||
369 | } |
||||
370 | |||||
371 | /** |
||||
372 | * Get options for default report. |
||||
373 | * |
||||
374 | * @return string |
||||
375 | */ |
||||
376 | protected function noReportOptions(): string // render a view |
||||
377 | { |
||||
378 | try { |
||||
379 | $result = view('reports.options.no-options')->render(); |
||||
380 | } catch (Throwable $e) { |
||||
381 | Log::error(sprintf('Cannot render reports.options.no-options: %s', $e->getMessage())); |
||||
382 | $result = 'Could not render view.'; |
||||
383 | } |
||||
384 | |||||
385 | return $result; |
||||
386 | } |
||||
387 | |||||
388 | /** |
||||
389 | * Get options for tag report. |
||||
390 | * |
||||
391 | * @return string |
||||
392 | */ |
||||
393 | protected function tagReportOptions(): string // render a view |
||||
394 | { |
||||
395 | /** @var TagRepositoryInterface $repository */ |
||||
396 | $repository = app(TagRepositoryInterface::class); |
||||
397 | $tags = $repository->get()->sortBy( |
||||
398 | function (Tag $tag) { |
||||
399 | return $tag->tag; |
||||
400 | } |
||||
401 | ); |
||||
402 | try { |
||||
403 | $result = view('reports.options.tag', compact('tags'))->render(); |
||||
404 | } catch (Throwable $e) { |
||||
405 | Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage())); |
||||
406 | $result = 'Could not render view.'; |
||||
407 | } |
||||
408 | |||||
409 | return $result; |
||||
410 | } |
||||
411 | } |
||||
412 |