1 | <?php |
||
2 | /** |
||
3 | * BulkController.php |
||
4 | * Copyright (c) 2017 [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\Http\Controllers\Transaction; |
||
25 | |||
26 | |||
27 | use FireflyIII\Http\Controllers\Controller; |
||
28 | use FireflyIII\Http\Requests\BulkEditJournalRequest; |
||
29 | use FireflyIII\Models\TransactionJournal; |
||
30 | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
||
31 | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
||
32 | use Illuminate\Support\Collection; |
||
33 | use Log; |
||
34 | |||
35 | /** |
||
36 | * Class BulkController |
||
37 | */ |
||
38 | class BulkController extends Controller |
||
39 | { |
||
40 | /** @var JournalRepositoryInterface Journals and transactions overview */ |
||
41 | private $repository; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * BulkController constructor. |
||
46 | * @codeCoverageIgnore |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | parent::__construct(); |
||
51 | |||
52 | $this->middleware( |
||
53 | function ($request, $next) { |
||
54 | $this->repository = app(JournalRepositoryInterface::class); |
||
55 | app('view')->share('title', (string)trans('firefly.transactions')); |
||
56 | app('view')->share('mainTitleIcon', 'fa-repeat'); |
||
57 | |||
58 | return $next($request); |
||
59 | } |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Edit a set of journals in bulk. |
||
65 | * |
||
66 | * TODO user wont be able to tell if journal is part of split. |
||
67 | * |
||
68 | * @param Collection $journals |
||
69 | * |
||
70 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
71 | */ |
||
72 | public function edit(array $journals) |
||
73 | { |
||
74 | $subTitle = (string)trans('firefly.mass_bulk_journals'); |
||
75 | |||
76 | $this->rememberPreviousUri('transactions.bulk-edit.uri'); |
||
77 | |||
78 | // make amounts positive. |
||
79 | |||
80 | // get list of budgets: |
||
81 | /** @var BudgetRepositoryInterface $repository */ |
||
82 | $repository = app(BudgetRepositoryInterface::class); |
||
83 | $budgetList = app('expandedform')->makeSelectListWithEmpty($repository->getActiveBudgets()); |
||
0 ignored issues
–
show
|
|||
84 | |||
85 | return view('transactions.bulk.edit', compact('journals', 'subTitle', 'budgetList')); |
||
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Update all journals. |
||
91 | * |
||
92 | * @param BulkEditJournalRequest $request |
||
93 | * |
||
94 | * @return mixed |
||
95 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
96 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
97 | */ |
||
98 | public function update(BulkEditJournalRequest $request) |
||
99 | { |
||
100 | $journalIds = $request->get('journals'); |
||
101 | $journalIds = is_array($journalIds) ? $journalIds : []; |
||
102 | $ignoreCategory = 1 === (int)$request->get('ignore_category'); |
||
103 | $ignoreBudget = 1 === (int)$request->get('ignore_budget'); |
||
104 | $ignoreTags = 1 === (int)$request->get('ignore_tags'); |
||
105 | $count = 0; |
||
106 | |||
107 | foreach ($journalIds as $journalId) { |
||
108 | $journalId = (int)$journalId; |
||
109 | $journal = $this->repository->findNull($journalId); |
||
110 | if (null !== $journal) { |
||
111 | $resultA = $this->updateJournalBudget($journal, $ignoreBudget, $request->integer('budget_id')); |
||
112 | $resultB = $this->updateJournalTags($journal, $ignoreTags, explode(',', $request->string('tags'))); |
||
113 | $resultC = $this->updateJournalCategory($journal, $ignoreCategory, $request->string('category')); |
||
114 | if ($resultA || $resultB || $resultC) { |
||
115 | $count++; |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | app('preferences')->mark(); |
||
120 | $request->session()->flash('success', (string)trans('firefly.mass_edited_transactions_success', ['amount' => $count])); |
||
121 | |||
122 | // redirect to previous URL: |
||
123 | return redirect($this->getPreviousUri('transactions.bulk-edit.uri')); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param TransactionJournal $journal |
||
128 | * @param bool $ignoreUpdate |
||
129 | * @param array $tags |
||
130 | * @return bool |
||
131 | */ |
||
132 | private function updateJournalTags(TransactionJournal $journal, bool $ignoreUpdate, array $tags): bool |
||
133 | { |
||
134 | |||
135 | if (true === $ignoreUpdate) { |
||
136 | return false; |
||
137 | } |
||
138 | Log::debug(sprintf('Set tags to %s', implode(',', $tags))); |
||
139 | $this->repository->updateTags($journal, $tags); |
||
140 | |||
141 | return true; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param TransactionJournal $journal |
||
146 | * @param bool $ignoreUpdate |
||
147 | * @param string $category |
||
148 | * @return bool |
||
149 | */ |
||
150 | private function updateJournalCategory(TransactionJournal $journal, bool $ignoreUpdate, string $category): bool |
||
151 | { |
||
152 | if (true === $ignoreUpdate) { |
||
153 | return false; |
||
154 | } |
||
155 | Log::debug(sprintf('Set budget to %s', $category)); |
||
156 | $this->repository->updateCategory($journal, $category); |
||
157 | |||
158 | return true; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param TransactionJournal $journal |
||
163 | * @param bool $ignoreUpdate |
||
164 | * @param int $budgetId |
||
165 | * @return bool |
||
166 | */ |
||
167 | private function updateJournalBudget(TransactionJournal $journal, bool $ignoreUpdate, int $budgetId): bool |
||
168 | { |
||
169 | if (true === $ignoreUpdate) { |
||
170 | return false; |
||
171 | } |
||
172 | Log::debug(sprintf('Set budget to %d', $budgetId)); |
||
173 | $this->repository->updateBudget($journal, $budgetId); |
||
174 | |||
175 | return true; |
||
176 | } |
||
177 | } |
||
178 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.