GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

app/Http/Controllers/TransactionController.php (1 issue)

Severity
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
declare(strict_types=1);
22
23
namespace FireflyIII\Http\Controllers;
24
25
use Carbon\Carbon;
26
use FireflyIII\Exceptions\FireflyException;
27
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
28
use FireflyIII\Helpers\Filter\CountAttachmentsFilter;
29
use FireflyIII\Helpers\Filter\InternalTransferFilter;
30
use FireflyIII\Helpers\Filter\SplitIndicatorFilter;
31
use FireflyIII\Models\Transaction;
32
use FireflyIII\Models\TransactionJournal;
33
use FireflyIII\Models\TransactionType;
34
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
35
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
36
use FireflyIII\Transformers\TransactionTransformer;
37
use Illuminate\Http\Request;
38
use Illuminate\Support\Collection;
39
use Log;
40
use Preferences;
41
use Symfony\Component\HttpFoundation\ParameterBag;
42
use View;
43
44
/**
45
 * Class TransactionController.
46
 */
47
class TransactionController extends Controller
48
{
49
    /** @var JournalRepositoryInterface */
50
    private $repository;
51
52
    /**
53
     * TransactionController constructor.
54
     */
55
    public function __construct()
56
    {
57
        parent::__construct();
58
59
        $this->middleware(
60
            function ($request, $next) {
61
                app('view')->share('title', trans('firefly.transactions'));
62
                app('view')->share('mainTitleIcon', 'fa-repeat');
63
                $this->repository = app(JournalRepositoryInterface::class);
64
65
                return $next($request);
66
            }
67
        );
68
    }
69
70
    /**
71
     * Index for a range of transactions.
72
     *
73
     * @param Request $request
74
     * @param string  $what
75
     * @param Carbon  $start
76
     * @param Carbon  $end
77
     *
78
     * @return View
79
     *
80
     */
81
    public function index(Request $request, string $what, Carbon $start = null, Carbon $end = null)
82
    {
83
        $subTitleIcon = config('firefly.transactionIconsByWhat.' . $what);
84
        $types        = config('firefly.transactionTypesByWhat.' . $what);
85
        $page         = (int)$request->get('page');
86
        $pageSize     = (int)Preferences::get('listPageSize', 50)->data;
87
        $path         = route('transactions.index', [$what]);
88
        if (null === $start) {
89
            $start = session('start');
90
            $end   = session('end');
91
        }
92
        if (null === $end) {
93
            $end = session('end');
94
        }
95
96
        if ($end < $start) {
97
            [$start, $end] = [$end, $start];
98
        }
99
        $startStr = $start->formatLocalized($this->monthAndDayFormat);
100
        $endStr   = $end->formatLocalized($this->monthAndDayFormat);
101
        $subTitle = trans('firefly.title_' . $what . '_between', ['start' => $startStr, 'end' => $endStr]);
102
        $periods  = $this->getPeriodOverview($what, $end);
103
104
        /** @var JournalCollectorInterface $collector */
105
        $collector = app(JournalCollectorInterface::class);
106
        $collector->setAllAssetAccounts()->setRange($start, $end)
107
                  ->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount()
108
                  ->withBudgetInformation()->withCategoryInformation();
109
        $collector->removeFilter(InternalTransferFilter::class);
110
        $collector->addFilter(SplitIndicatorFilter::class);
111
        $collector->addFilter(CountAttachmentsFilter::class);
112
        $transactions = $collector->getPaginatedJournals();
113
        $transactions->setPath($path);
114
115
        return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'transactions', 'periods', 'start', 'end'));
116
    }
117
118
    /**
119
     * @param Request $request
120
     * @param string  $what
121
     *
122
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
123
     */
124
    public function indexAll(Request $request, string $what)
125
    {
126
        $subTitleIcon = config('firefly.transactionIconsByWhat.' . $what);
127
        $types        = config('firefly.transactionTypesByWhat.' . $what);
128
        $page         = (int)$request->get('page');
129
        $pageSize     = (int)Preferences::get('listPageSize', 50)->data;
130
        $path         = route('transactions.index.all', [$what]);
131
        $first        = $this->repository->first();
0 ignored issues
show
Deprecated Code introduced by
The function FireflyIII\Repositories\...itoryInterface::first() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

131
        $first        = /** @scrutinizer ignore-deprecated */ $this->repository->first();
Loading history...
132
        $start        = $first->date ?? new Carbon;
133
        $end          = new Carbon;
134
        $subTitle     = trans('firefly.all_' . $what);
135
136
        /** @var JournalCollectorInterface $collector */
137
        $collector = app(JournalCollectorInterface::class);
138
        $collector->setAllAssetAccounts()->setRange($start, $end)
139
                  ->setTypes($types)->setLimit($pageSize)->setPage($page)->withOpposingAccount()
140
                  ->withBudgetInformation()->withCategoryInformation();
141
        $collector->removeFilter(InternalTransferFilter::class);
142
        $collector->addFilter(SplitIndicatorFilter::class);
143
        $collector->addFilter(CountAttachmentsFilter::class);
144
        $transactions = $collector->getPaginatedJournals();
145
        $transactions->setPath($path);
146
147
        return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'transactions', 'start', 'end'));
148
    }
149
150
    /**
151
     * @param Request $request
152
     *
153
     * @return \Illuminate\Http\JsonResponse
154
     */
155
    public function reconcile(Request $request)
156
    {
157
        $transactionIds = $request->get('transactions');
158
        foreach ($transactionIds as $transactionId) {
159
            $transactionId = (int)$transactionId;
160
            $transaction   = $this->repository->findTransaction($transactionId);
161
            Log::debug(sprintf('Transaction ID is %d', $transaction->id));
162
163
            $this->repository->reconcile($transaction);
164
        }
165
166
        return response()->json(['ok' => 'reconciled']);
167
    }
168
169
    /**
170
     * @param Request $request
171
     *
172
     * @return \Illuminate\Http\JsonResponse
173
     */
174
    public function reorder(Request $request)
175
    {
176
        $ids  = $request->get('items');
177
        $date = new Carbon($request->get('date'));
178
        if (count($ids) > 0) {
179
            $order = 0;
180
            $ids   = array_unique($ids);
181
            foreach ($ids as $id) {
182
                $journal = $this->repository->find((int)$id);
183
                if ($journal && $journal->date->isSameDay($date)) {
184
                    $this->repository->setOrder($journal, $order);
185
                    ++$order;
186
                }
187
            }
188
        }
189
        Preferences::mark();
190
191
        return response()->json([true]);
192
    }
193
194
    /**
195
     * @param TransactionJournal          $journal
196
     * @param LinkTypeRepositoryInterface $linkTypeRepository
197
     *
198
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
199
     * @throws FireflyException
200
     */
201
    public function show(TransactionJournal $journal, LinkTypeRepositoryInterface $linkTypeRepository)
202
    {
203
        if ($this->isOpeningBalance($journal)) {
204
            return $this->redirectToAccount($journal);
205
        }
206
        $transactionType = $journal->transactionType->type;
207
        if (TransactionType::RECONCILIATION === $transactionType) {
208
            return redirect(route('accounts.reconcile.show', [$journal->id])); // @codeCoverageIgnore
209
        }
210
        $linkTypes = $linkTypeRepository->get();
211
        $links     = $linkTypeRepository->getLinks($journal);
212
213
        // get transactions using the collector:
214
        $collector = app(JournalCollectorInterface::class);
215
        $collector->setUser(auth()->user());
216
        $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
217
        // filter on specific journals.
218
        $collector->setJournals(new Collection([$journal]));
219
        $set          = $collector->getJournals();
220
        $transactions = [];
221
        $transformer  = new TransactionTransformer(new ParameterBag);
222
        /** @var Transaction $transaction */
223
        foreach ($set as $transaction) {
224
            $transactions[] = $transformer->transform($transaction);
225
        }
226
227
        $events   = $this->repository->getPiggyBankEvents($journal);
228
        $what     = strtolower($transactionType);
229
        $subTitle = trans('firefly.' . $what) . ' "' . $journal->description . '"';
230
231
        return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'linkTypes', 'links'));
232
    }
233
234
    /**
235
     * @param string $what
236
     *
237
     * @param Carbon $date
238
     *
239
     * @return Collection
240
     */
241
    private function getPeriodOverview(string $what, Carbon $date): Collection
242
    {
243
        $range = Preferences::get('viewRange', '1M')->data;
244
        $first = $this->repository->firstNull();
245
        $start = new Carbon;
246
        $start->subYear();
247
        $types   = config('firefly.transactionTypesByWhat.' . $what);
248
        $entries = new Collection;
249
        if (null !== $first) {
250
            $start = $first->date;
251
        }
252
        if ($date < $start) {
253
            [$start, $date] = [$date, $start]; // @codeCoverageIgnore
254
        }
255
256
        /** @var array $dates */
257
        $dates = app('navigation')->blockPeriods($start, $date, $range);
258
259
        foreach ($dates as $currentDate) {
260
            /** @var JournalCollectorInterface $collector */
261
            $collector = app(JournalCollectorInterface::class);
262
            $collector->setAllAssetAccounts()->setRange($currentDate['start'], $currentDate['end'])->withOpposingAccount()->setTypes($types);
263
            $collector->removeFilter(InternalTransferFilter::class);
264
            $journals = $collector->getJournals();
265
266
            if ($journals->count() > 0) {
267
                $sums     = $this->sumPerCurrency($journals);
268
                $dateName = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
269
                $sum      = $journals->sum('transaction_amount');
270
                $entries->push(
271
                    [
272
                        'name'  => $dateName,
273
                        'sums'  => $sums,
274
                        'sum'   => $sum,
275
                        'start' => $currentDate['start']->format('Y-m-d'),
276
                        'end'   => $currentDate['end']->format('Y-m-d'),
277
                    ]
278
                );
279
            }
280
        }
281
282
        return $entries;
283
    }
284
285
    /**
286
     * @param Collection $collection
287
     *
288
     * @return array
289
     */
290
    private function sumPerCurrency(Collection $collection): array
291
    {
292
        $return = [];
293
        /** @var Transaction $transaction */
294
        foreach ($collection as $transaction) {
295
            $currencyId = (int)$transaction->transaction_currency_id;
296
297
            // save currency information:
298
            if (!isset($return[$currencyId])) {
299
                $currencySymbol      = $transaction->transaction_currency_symbol;
300
                $decimalPlaces       = $transaction->transaction_currency_dp;
301
                $currencyCode        = $transaction->transaction_currency_code;
302
                $return[$currencyId] = [
303
                    'currency' => [
304
                        'id'     => $currencyId,
305
                        'code'   => $currencyCode,
306
                        'symbol' => $currencySymbol,
307
                        'dp'     => $decimalPlaces,
308
                    ],
309
                    'sum'      => '0',
310
                    'count'    => 0,
311
                ];
312
            }
313
            // save amount:
314
            $return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], $transaction->transaction_amount);
315
            ++$return[$currencyId]['count'];
316
        }
317
        asort($return);
318
319
        return $return;
320
    }
321
}
322