Passed
Push — master ( 49de82...9f1fc0 )
by James
35:45 queued 23:44
created

IndexController::inactive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 41
rs 9.44
cc 2
nc 2
nop 2
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
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
22
declare(strict_types=1);
23
24
namespace FireflyIII\Http\Controllers\Account;
25
26
use Carbon\Carbon;
27
use FireflyIII\Http\Controllers\Controller;
28
use FireflyIII\Models\Account;
29
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
30
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
31
use Illuminate\Http\Request;
32
use Illuminate\Pagination\LengthAwarePaginator;
33
34
/**
35
 *
36
 * Class IndexController
37
 */
38
class IndexController extends Controller
39
{
40
    use BasicDataSupport;
41
    /** @var AccountRepositoryInterface The account repository */
42
    private $repository;
43
44
    /**
45
     * IndexController constructor.
46
     * @codeCoverageIgnore
47
     */
48
    public function __construct()
49
    {
50
        parent::__construct();
51
52
        // translations:
53
        $this->middleware(
54
            function ($request, $next) {
55
                app('view')->share('mainTitleIcon', 'fa-credit-card');
56
                app('view')->share('title', (string)trans('firefly.accounts'));
57
58
                $this->repository = app(AccountRepositoryInterface::class);
59
60
                return $next($request);
61
            }
62
        );
63
    }
64
65
    /**
66
     * @param Request $request
67
     * @param string  $objectType
68
     *
69
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
70
     */
71
    public function inactive(Request $request, string $objectType)
72
    {
73
        $objectType   = $objectType ?? 'asset';
74
        $inactivePage = true;
75
        $subTitle     = (string)trans(sprintf('firefly.%s_accounts_inactive', $objectType));
76
        $subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
77
        $types        = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType));
78
        $collection   = $this->repository->getInactiveAccountsByType($types);
79
        $total        = $collection->count();
80
        $page         = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
81
        $pageSize     = (int)app('preferences')->get('listPageSize', 50)->data;
82
        $accounts     = $collection->slice(($page - 1) * $pageSize, $pageSize);
83
        unset($collection);
84
        /** @var Carbon $start */
85
        $start = clone session('start', Carbon::now()->startOfMonth());
86
        /** @var Carbon $end */
87
        $end = clone session('end', Carbon::now()->endOfMonth());
88
        $start->subDay();
89
90
        $ids           = $accounts->pluck('id')->toArray();
91
        $startBalances = app('steam')->balancesByAccounts($accounts, $start);
92
        $endBalances   = app('steam')->balancesByAccounts($accounts, $end);
93
        $activities    = app('steam')->getLastActivities($ids);
94
95
        $accounts->each(
96
            function (Account $account) use ($activities, $startBalances, $endBalances) {
97
                $account->lastActivityDate  = $this->isInArray($activities, $account->id);
98
                $account->startBalance      = $this->isInArray($startBalances, $account->id);
99
                $account->endBalance        = $this->isInArray($endBalances, $account->id);
100
                $account->difference        = bcsub($account->endBalance, $account->startBalance);
101
                $account->interest          = round($this->repository->getMetaValue($account, 'interest'), 6);
0 ignored issues
show
Bug introduced by James Cole
It seems like $this->repository->getMe...e($account, 'interest') can also be of type string; however, parameter $val of round() does only seem to accept double, 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

101
                $account->interest          = round(/** @scrutinizer ignore-type */ $this->repository->getMetaValue($account, 'interest'), 6);
Loading history...
102
                $account->interestPeriod    = (string)trans(sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period')));
103
                $account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type));
104
            }
105
        );
106
107
        // make paginator:
108
        $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page);
109
        $accounts->setPath(route('accounts.inactive.index', [$objectType]));
110
111
        return view('accounts.index', compact('objectType','inactivePage', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
112
113
    }
114
115
    /**
116
     * Show list of accounts.
117
     *
118
     * @param Request $request
119
     * @param string $objectType
120
     *
121
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
122
     */
123
    public function index(Request $request, string $objectType)
124
    {
125
        $objectType    = $objectType ?? 'asset';
126
        $subTitle      = (string)trans(sprintf('firefly.%s_accounts', $objectType));
127
        $subTitleIcon  = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
128
        $types         = config(sprintf('firefly.accountTypesByIdentifier.%s', $objectType));
129
        $collection    = $this->repository->getActiveAccountsByType($types);
130
        $total         = $collection->count();
131
        $page          = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
132
        $pageSize      = (int)app('preferences')->get('listPageSize', 50)->data;
133
        $accounts      = $collection->slice(($page - 1) * $pageSize, $pageSize);
134
        $inactiveCount = $this->repository->getInactiveAccountsByType($types)->count();
135
136
137
        unset($collection);
138
        /** @var Carbon $start */
139
        $start = clone session('start', Carbon::now()->startOfMonth());
140
        /** @var Carbon $end */
141
        $end = clone session('end', Carbon::now()->endOfMonth());
142
        $start->subDay();
143
144
        $ids           = $accounts->pluck('id')->toArray();
145
        $startBalances = app('steam')->balancesByAccounts($accounts, $start);
146
        $endBalances   = app('steam')->balancesByAccounts($accounts, $end);
147
        $activities    = app('steam')->getLastActivities($ids);
148
149
        $accounts->each(
150
            function (Account $account) use ($activities, $startBalances, $endBalances) {
151
                $account->lastActivityDate  = $this->isInArray($activities, $account->id);
152
                $account->startBalance      = $this->isInArray($startBalances, $account->id);
153
                $account->endBalance        = $this->isInArray($endBalances, $account->id);
154
                $account->difference        = bcsub($account->endBalance, $account->startBalance);
155
                $account->interest          = round($this->repository->getMetaValue($account, 'interest'), 6);
0 ignored issues
show
Bug introduced by James Cole
It seems like $this->repository->getMe...e($account, 'interest') can also be of type string; however, parameter $val of round() does only seem to accept double, 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

155
                $account->interest          = round(/** @scrutinizer ignore-type */ $this->repository->getMetaValue($account, 'interest'), 6);
Loading history...
156
                $account->interestPeriod    = (string)trans(sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period')));
157
                $account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type));
158
            }
159
        );
160
161
        // make paginator:
162
        $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page);
163
        $accounts->setPath(route('accounts.index', [$objectType]));
164
165
        return view('accounts.index', compact('objectType', 'inactiveCount', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
166
    }
167
168
169
}
170