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.

Issues (724)

app/Http/Controllers/Budget/IndexController.php (11 issues)

1
<?php
2
/**
3
 * IndexController.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
22
declare(strict_types=1);
23
24
namespace FireflyIII\Http\Controllers\Budget;
25
26
use Carbon\Carbon;
27
use FireflyIII\Http\Controllers\Controller;
28
use FireflyIII\Models\AvailableBudget;
29
use FireflyIII\Models\Budget;
30
use FireflyIII\Models\BudgetLimit;
31
use FireflyIII\Models\TransactionCurrency;
32
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
33
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
34
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
35
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
36
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
37
use FireflyIII\Support\Http\Controllers\DateCalculation;
38
use Illuminate\Contracts\View\Factory;
39
use Illuminate\Http\JsonResponse;
40
use Illuminate\Http\Request;
41
use Illuminate\Support\Collection;
42
use Illuminate\View\View;
43
use Log;
44
45
/**
46
 *
47
 * Class IndexController
48
 */
49
class IndexController extends Controller
50
{
51
    use DateCalculation;
52
    /** @var AvailableBudgetRepositoryInterface */
53
    private $abRepository;
54
    /** @var BudgetLimitRepositoryInterface */
55
    private $blRepository;
56
    /** @var CurrencyRepositoryInterface */
57
    private $currencyRepository;
58
    /** @var OperationsRepositoryInterface */
59
    private $opsRepository;
60
    /** @var BudgetRepositoryInterface The budget repository */
61
    private $repository;
62
63
    /**
64
     * IndexController constructor.
65
     *
66
     * @codeCoverageIgnore
67
     */
68
    public function __construct()
69
    {
70
        parent::__construct();
71
72
        $this->middleware(
73
            function ($request, $next) {
74
                app('view')->share('title', (string) trans('firefly.budgets'));
75
                app('view')->share('mainTitleIcon', 'fa-tasks');
76
                $this->repository         = app(BudgetRepositoryInterface::class);
77
                $this->opsRepository      = app(OperationsRepositoryInterface::class);
78
                $this->abRepository       = app(AvailableBudgetRepositoryInterface::class);
79
                $this->currencyRepository = app(CurrencyRepositoryInterface::class);
80
                $this->blRepository       = app(BudgetLimitRepositoryInterface::class);
81
                $this->repository->cleanupBudgets();
82
83
                return $next($request);
84
            }
85
        );
86
    }
87
88
    /**
89
     * TODO the "budgeted" progress bar doesn't update.
90
     * Show all budgets.
91
     *
92
     * @param Request     $request
93
     *
94
     * @param Carbon|null $start
95
     * @param Carbon|null $end
96
     *
97
     * @return Factory|View
98
     */
99
    public function index(Request $request, Carbon $start = null, Carbon $end = null)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

99
    public function index(/** @scrutinizer ignore-unused */ Request $request, Carbon $start = null, Carbon $end = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        // collect some basic vars:
102
        $range           = app('preferences')->get('viewRange', '1M')->data;
103
        $start           = $start ?? session('start', Carbon::now()->startOfMonth());
104
        $end             = $end ?? app('navigation')->endOfPeriod($start, $range);
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $end of FireflyIII\Support\Facad...vigation::endOfPeriod() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

104
        $end             = $end ?? app('navigation')->endOfPeriod(/** @scrutinizer ignore-type */ $start, $range);
Loading history...
105
        $defaultCurrency = app('amount')->getDefaultCurrency();
106
        $budgeted        = '0';
107
        $spent           = '0';
108
109
110
        // new period stuff:
111
        $periodTitle = app('navigation')->periodShow($start, $range);
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $theDate of FireflyIII\Support\Facad...avigation::periodShow() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

111
        $periodTitle = app('navigation')->periodShow(/** @scrutinizer ignore-type */ $start, $range);
Loading history...
112
        $prevLoop    = $this->getPreviousPeriods($start, $range);
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $date of FireflyIII\Http\Controll...r::getPreviousPeriods() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

112
        $prevLoop    = $this->getPreviousPeriods(/** @scrutinizer ignore-type */ $start, $range);
Loading history...
113
        $nextLoop    = $this->getNextPeriods($start, $range);
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $date of FireflyIII\Http\Controll...oller::getNextPeriods() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

113
        $nextLoop    = $this->getNextPeriods(/** @scrutinizer ignore-type */ $start, $range);
Loading history...
114
115
        // get all available budgets.
116
        $ab               = $this->abRepository->get($start, $end);
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $start of FireflyIII\Repositories\...ositoryInterface::get() does only seem to accept Carbon\Carbon|null, 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 ignore-type  annotation

116
        $ab               = $this->abRepository->get(/** @scrutinizer ignore-type */ $start, $end);
Loading history...
117
        $availableBudgets = [];
118
        // for each, complement with spent amount:
119
        /** @var AvailableBudget $entry */
120
        foreach ($ab as $entry) {
121
            $array               = $entry->toArray();
122
            $array['start_date'] = $entry->start_date;
123
            $array['end_date']   = $entry->end_date;
124
125
            // spent in period:
126
            $spentArr       = $this->opsRepository->sumExpenses($entry->start_date, $entry->end_date, null, null, $entry->transactionCurrency);
127
            $array['spent'] = $spentArr[$entry->transaction_currency_id]['sum'] ?? '0';
128
129
            // budgeted in period:
130
            $budgeted           = $this->blRepository->budgeted($entry->start_date, $entry->end_date, $entry->transactionCurrency, );
131
            $array['budgeted']  = $budgeted;
132
            $availableBudgets[] = $array;
133
            unset($spentArr);
134
        }
135
136
        if (0 === count($availableBudgets)) {
137
            // get budgeted for default currency:
138
            $budgeted = $this->blRepository->budgeted($start, $end, $defaultCurrency, );
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $start of FireflyIII\Repositories\...ryInterface::budgeted() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

138
            $budgeted = $this->blRepository->budgeted(/** @scrutinizer ignore-type */ $start, $end, $defaultCurrency, );
Loading history...
139
            $spentArr = $this->opsRepository->sumExpenses($start, $end, null, null, $defaultCurrency);
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $start of FireflyIII\Repositories\...nterface::sumExpenses() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

139
            $spentArr = $this->opsRepository->sumExpenses(/** @scrutinizer ignore-type */ $start, $end, null, null, $defaultCurrency);
Loading history...
140
            $spent    = $spentArr[$defaultCurrency->id]['sum'] ?? '0';
141
            unset($spentArr);
142
        }
143
144
        // count the number of enabled currencies. This determines if we display a "+" button.
145
        $currencies      = $this->currencyRepository->getEnabled();
146
        $enableAddButton = $currencies->count() > count($availableBudgets);
147
148
        // number of days for consistent budgeting.
149
        $activeDaysPassed = $this->activeDaysPassed($start, $end); // see method description.
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $start of FireflyIII\Http\Controll...ler::activeDaysPassed() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

149
        $activeDaysPassed = $this->activeDaysPassed(/** @scrutinizer ignore-type */ $start, $end); // see method description.
Loading history...
150
        $activeDaysLeft   = $this->activeDaysLeft($start, $end); // see method description.
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $start of FireflyIII\Http\Controll...oller::activeDaysLeft() does only seem to accept Carbon\Carbon, 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 ignore-type  annotation

150
        $activeDaysLeft   = $this->activeDaysLeft(/** @scrutinizer ignore-type */ $start, $end); // see method description.
Loading history...
151
        Log::debug(sprintf('Start: %s, end: %s', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
152
153
        // get all budgets, and paginate them into $budgets.
154
        $collection = $this->repository->getActiveBudgets();
155
        $budgets    = [];
156
157
        // complement budget with budget limits in range, and expenses in currency X in range.
158
        /** @var Budget $current */
159
        foreach ($collection as $current) {
160
            $array                = $current->toArray();
161
            $array['spent']       = [];
162
            $array['budgeted']    = [];
163
            $array['attachments'] = $this->repository->getAttachments($current);
164
            $array['auto_budget'] = $this->repository->getAutoBudget($current);
165
            $budgetLimits         = $this->blRepository->getBudgetLimits($current, $start, $end);
0 ignored issues
show
It seems like $start can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $start of FireflyIII\Repositories\...face::getBudgetLimits() does only seem to accept Carbon\Carbon|null, 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 ignore-type  annotation

165
            $budgetLimits         = $this->blRepository->getBudgetLimits($current, /** @scrutinizer ignore-type */ $start, $end);
Loading history...
166
167
            /** @var BudgetLimit $limit */
168
            foreach ($budgetLimits as $limit) {
169
                $currency            = $limit->transactionCurrency ?? $defaultCurrency;
170
                $array['budgeted'][] = [
171
                    'id'                      => $limit->id,
172
                    'amount'                  => round($limit->amount, $currency->decimal_places),
173
                    'currency_id'             => $currency->id,
174
                    'currency_symbol'         => $currency->symbol,
175
                    'currency_name'           => $currency->name,
176
                    'currency_decimal_places' => $currency->decimal_places,
177
                ];
178
            }
179
180
            /** @var TransactionCurrency $currency */
181
            foreach ($currencies as $currency) {
182
                $spentArr = $this->opsRepository->sumExpenses($start, $end, null, new Collection([$current]), $currency);
183
                if (isset($spentArr[$currency->id]['sum'])) {
184
                    $array['spent'][$currency->id]['spent']                   = $spentArr[$currency->id]['sum'];
185
                    $array['spent'][$currency->id]['currency_id']             = $currency->id;
186
                    $array['spent'][$currency->id]['currency_symbol']         = $currency->symbol;
187
                    $array['spent'][$currency->id]['currency_decimal_places'] = $currency->decimal_places;
188
                }
189
            }
190
            $budgets[] = $array;
191
        }
192
193
        // get all inactive budgets, and simply list them:
194
        $inactive = $this->repository->getInactiveBudgets();
195
196
197
        return view(
198
            'budgets.index',
199
            compact(
200
                'availableBudgets',
201
                'budgeted',
202
                'spent',
203
                'prevLoop',
204
                'nextLoop',
205
                'budgets',
206
                'currencies',
207
                'enableAddButton',
208
                'periodTitle',
209
                'defaultCurrency',
210
                'activeDaysPassed',
211
                'activeDaysLeft',
212
                'inactive',
213
                'budgets',
214
                'start',
215
                'end'
216
            )
217
        );
218
    }
219
220
    /**
221
     * @param Request                   $request
222
     *
223
     * @param BudgetRepositoryInterface $repository
224
     *
225
     * @return JsonResponse
226
     */
227
    public function reorder(Request $request, BudgetRepositoryInterface $repository): JsonResponse
228
    {
229
        $budgetIds = $request->get('budgetIds');
230
231
        foreach ($budgetIds as $index => $budgetId) {
232
            $budgetId = (int) $budgetId;
233
            $budget   = $repository->findNull($budgetId);
234
            if (null !== $budget) {
235
                Log::debug(sprintf('Set budget #%d ("%s") to position %d', $budget->id, $budget->name, $index + 1));
236
                $repository->setBudgetOrder($budget, $index + 1);
237
            }
238
        }
239
        app('preferences')->mark();
240
241
        return response()->json(['OK']);
242
    }
243
}
244