1 | <?php |
||
2 | /** |
||
3 | * TransactionController.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 | /** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
||
22 | /** @noinspection MoreThanThreeArgumentsInspection */ |
||
23 | declare(strict_types=1); |
||
24 | |||
25 | namespace FireflyIII\Http\Controllers; |
||
26 | |||
27 | use Carbon\Carbon; |
||
28 | use FireflyIII\Exceptions\FireflyException; |
||
29 | use FireflyIII\Helpers\Attachments\AttachmentHelper; |
||
30 | use FireflyIII\Helpers\Collector\TransactionCollectorInterface; |
||
31 | use FireflyIII\Helpers\Filter\CountAttachmentsFilter; |
||
32 | use FireflyIII\Helpers\Filter\InternalTransferFilter; |
||
33 | use FireflyIII\Helpers\Filter\SplitIndicatorFilter; |
||
34 | use FireflyIII\Models\Attachment; |
||
35 | use FireflyIII\Models\Transaction; |
||
36 | use FireflyIII\Models\TransactionJournal; |
||
37 | use FireflyIII\Models\TransactionType; |
||
38 | use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; |
||
39 | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
||
40 | use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; |
||
41 | use FireflyIII\Support\Http\Controllers\ModelInformation; |
||
42 | use FireflyIII\Support\Http\Controllers\PeriodOverview; |
||
43 | use FireflyIII\Transformers\TransactionTransformer; |
||
44 | use Illuminate\Http\JsonResponse; |
||
45 | use Illuminate\Http\Request; |
||
46 | use Illuminate\Support\Collection; |
||
47 | use Log; |
||
48 | use Symfony\Component\HttpFoundation\ParameterBag; |
||
49 | use View; |
||
50 | |||
51 | /** |
||
52 | * Class TransactionController. |
||
53 | * |
||
54 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
||
55 | */ |
||
56 | class TransactionController extends Controller |
||
57 | { |
||
58 | use ModelInformation, PeriodOverview; |
||
59 | /** @var AttachmentRepositoryInterface */ |
||
60 | private $attachmentRepository; |
||
61 | /** @var JournalRepositoryInterface Journals and transactions overview */ |
||
62 | private $repository; |
||
63 | |||
64 | /** |
||
65 | * TransactionController constructor. |
||
66 | */ |
||
67 | public function __construct() |
||
68 | { |
||
69 | parent::__construct(); |
||
70 | |||
71 | $this->middleware( |
||
72 | function ($request, $next) { |
||
73 | app('view')->share('title', (string)trans('firefly.transactions')); |
||
74 | app('view')->share('mainTitleIcon', 'fa-repeat'); |
||
75 | $this->repository = app(JournalRepositoryInterface::class); |
||
76 | $this->attachmentRepository = app(AttachmentRepositoryInterface::class); |
||
77 | |||
78 | return $next($request); |
||
79 | } |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Index for a range of transactions. |
||
85 | * |
||
86 | * @param Request $request |
||
87 | * @param string $what |
||
88 | * @param Carbon|null $start |
||
89 | * @param Carbon|null $end |
||
90 | * |
||
91 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
92 | */ |
||
93 | public function index(Request $request, string $what, Carbon $start = null, Carbon $end = null) |
||
94 | { |
||
95 | $subTitleIcon = config('firefly.transactionIconsByWhat.' . $what); |
||
96 | $types = config('firefly.transactionTypesByWhat.' . $what); |
||
97 | $page = (int)$request->get('page'); |
||
98 | $pageSize = (int)app('preferences')->get('listPageSize', 50)->data; |
||
99 | if (null === $start) { |
||
100 | $start = session('start'); |
||
101 | $end = session('end'); |
||
102 | } |
||
103 | if (null === $end) { |
||
104 | $end = session('end'); |
||
105 | } |
||
106 | |||
107 | if ($end < $start) { |
||
108 | [$start, $end] = [$end, $start]; |
||
109 | } |
||
110 | |||
111 | $path = route('transactions.index', [$what, $start->format('Y-m-d'), $end->format('Y-m-d')]); |
||
112 | |||
113 | $startStr = $start->formatLocalized($this->monthAndDayFormat); |
||
114 | $endStr = $end->formatLocalized($this->monthAndDayFormat); |
||
115 | $subTitle = (string)trans('firefly.title_' . $what . '_between', ['start' => $startStr, 'end' => $endStr]); |
||
116 | $periods = $this->getTransactionPeriodOverview($what, $end); |
||
117 | |||
118 | /** @var TransactionCollectorInterface $collector */ |
||
119 | $collector = app(TransactionCollectorInterface::class); |
||
120 | $collector->setAllAssetAccounts()->setRange($start, $end) |
||
121 | ->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount() |
||
122 | ->withBudgetInformation()->withCategoryInformation(); |
||
123 | $collector->removeFilter(InternalTransferFilter::class); |
||
124 | $collector->addFilter(SplitIndicatorFilter::class); |
||
125 | $collector->addFilter(CountAttachmentsFilter::class); |
||
126 | $transactions = $collector->getPaginatedTransactions(); |
||
127 | $transactions->setPath($path); |
||
128 | |||
129 | return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'transactions', 'periods', 'start', 'end')); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Index for ALL transactions. |
||
134 | * |
||
135 | * @param Request $request |
||
136 | * @param string $what |
||
137 | * |
||
138 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
139 | */ |
||
140 | public function indexAll(Request $request, string $what) |
||
141 | { |
||
142 | $subTitleIcon = config('firefly.transactionIconsByWhat.' . $what); |
||
143 | $types = config('firefly.transactionTypesByWhat.' . $what); |
||
144 | $page = (int)$request->get('page'); |
||
145 | $pageSize = (int)app('preferences')->get('listPageSize', 50)->data; |
||
146 | $path = route('transactions.index.all', [$what]); |
||
147 | $first = $this->repository->firstNull(); |
||
148 | $start = null === $first ? new Carbon : $first->date; |
||
149 | $end = new Carbon; |
||
150 | $subTitle = (string)trans('firefly.all_' . $what); |
||
151 | |||
152 | /** @var TransactionCollectorInterface $collector */ |
||
153 | $collector = app(TransactionCollectorInterface::class); |
||
154 | $collector->setAllAssetAccounts()->setRange($start, $end) |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
155 | ->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount() |
||
156 | ->withBudgetInformation()->withCategoryInformation(); |
||
157 | $collector->removeFilter(InternalTransferFilter::class); |
||
158 | $collector->addFilter(SplitIndicatorFilter::class); |
||
159 | $collector->addFilter(CountAttachmentsFilter::class); |
||
160 | $transactions = $collector->getPaginatedTransactions(); |
||
161 | $transactions->setPath($path); |
||
162 | |||
163 | return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'transactions', 'start', 'end')); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Do a reconciliation. |
||
168 | * |
||
169 | * @param Request $request |
||
170 | * |
||
171 | * @return JsonResponse |
||
172 | */ |
||
173 | public function reconcile(Request $request): JsonResponse |
||
174 | { |
||
175 | $transactionIds = $request->get('transactions'); |
||
176 | foreach ($transactionIds as $transactionId) { |
||
177 | $transactionId = (int)$transactionId; |
||
178 | $transaction = $this->repository->findTransaction($transactionId); |
||
179 | if (null !== $transaction) { |
||
180 | Log::debug(sprintf('Transaction ID is %d', $transaction->id)); |
||
181 | $this->repository->reconcile($transaction); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return response()->json(['ok' => 'reconciled']); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Reorder transactions. |
||
190 | * |
||
191 | * @param Request $request |
||
192 | * |
||
193 | * @return \Illuminate\Http\JsonResponse |
||
194 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
195 | */ |
||
196 | public function reorder(Request $request): JsonResponse |
||
197 | { |
||
198 | $ids = $request->get('items'); |
||
199 | $date = new Carbon($request->get('date')); |
||
200 | if (\count($ids) > 0) { |
||
201 | $order = 0; |
||
202 | $ids = array_unique($ids); |
||
203 | foreach ($ids as $id) { |
||
204 | $journal = $this->repository->findNull((int)$id); |
||
205 | if (null !== $journal && $journal->date->isSameDay($date)) { |
||
206 | $this->repository->setOrder($journal, $order); |
||
207 | ++$order; |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | app('preferences')->mark(); |
||
212 | |||
213 | return response()->json([true]); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Show a transaction. |
||
218 | * |
||
219 | * @param TransactionJournal $journal |
||
220 | * @param LinkTypeRepositoryInterface $linkTypeRepository |
||
221 | * @param AttachmentHelper $attHelper |
||
222 | * |
||
223 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View |
||
224 | * @throws FireflyException |
||
225 | */ |
||
226 | public function show(TransactionJournal $journal, LinkTypeRepositoryInterface $linkTypeRepository) |
||
227 | { |
||
228 | if ($this->isOpeningBalance($journal)) { |
||
229 | return $this->redirectToAccount($journal); |
||
230 | } |
||
231 | $transactionType = $journal->transactionType->type; |
||
232 | if (TransactionType::RECONCILIATION === $transactionType) { |
||
233 | return redirect(route('accounts.reconcile.show', [$journal->id])); // @codeCoverageIgnore |
||
234 | } |
||
235 | $linkTypes = $linkTypeRepository->get(); |
||
236 | $links = $linkTypeRepository->getLinks($journal); |
||
237 | |||
238 | // get attachments: |
||
239 | $attachments = $this->repository->getAttachments($journal); |
||
240 | $attachments = $attachments->each( |
||
241 | function (Attachment $attachment) { |
||
242 | $attachment->file_exists = $this->attachmentRepository->exists($attachment); |
||
243 | |||
244 | return $attachment; |
||
245 | } |
||
246 | ); |
||
247 | |||
248 | // get transactions using the collector: |
||
249 | $collector = app(TransactionCollectorInterface::class); |
||
250 | $collector->setUser(auth()->user()); |
||
251 | $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); |
||
252 | // filter on specific journals. |
||
253 | $collector->setJournals(new Collection([$journal])); |
||
254 | $set = $collector->getTransactions(); |
||
255 | $transactions = []; |
||
256 | |||
257 | /** @var TransactionTransformer $transformer */ |
||
258 | $transformer = app(TransactionTransformer::class); |
||
259 | $transformer->setParameters(new ParameterBag); |
||
260 | |||
261 | /** @var Transaction $transaction */ |
||
262 | foreach ($set as $transaction) { |
||
263 | $transactions[] = $transformer->transform($transaction); |
||
264 | } |
||
265 | |||
266 | $events = $this->repository->getPiggyBankEvents($journal); |
||
267 | $what = strtolower($transactionType); |
||
268 | $subTitle = trans('firefly.' . $what) . ' "' . $journal->description . '"'; |
||
269 | |||
270 | return view('transactions.show', compact('journal','attachments', 'events', 'subTitle', 'what', 'transactions', 'linkTypes', 'links')); |
||
271 | } |
||
272 | |||
273 | |||
274 | } |
||
275 |