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 ( 6f8b1f...142a48 )
by James
25:51 queued 11:45
created

AccountForm   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 33
eloc 144
c 1
b 0
f 0
dl 0
loc 291
rs 9.76

7 Methods

Rating   Name   Duplication   Size   Complexity  
B activeDepositDestinations() 0 35 7
A assetAccountCheckList() 0 33 4
B activeWithdrawalDestinations() 0 38 7
A longAccountList() 0 26 4
A assetAccountList() 0 24 3
A activeAssetAccountList() 0 21 3
A activeLongAccountList() 0 32 5
1
<?php
2
/**
3
 * AccountForm.php
4
 * Copyright (c) 2019 [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
namespace FireflyIII\Support\Form;
23
24
25
use FireflyIII\Models\Account;
26
use FireflyIII\Models\AccountType;
27
use Log;
28
use Throwable;
29
30
/**
31
 * Class AccountForm
32
 *
33
 * All form methods that are account related.
34
 *
35
 * TODO describe all methods.
36
 * TODO optimize repositories and methods.
37
 */
38
class AccountForm
39
{
40
    use FormSupport;
41
42
    /**
43
     * Shows a <select> with all active asset accounts.
44
     *
45
     * @param string $name
46
     * @param mixed  $value
47
     * @param array  $options
48
     *
49
     * @return string
50
     */
51
    public function activeAssetAccountList(string $name, $value = null, array $options = null): string
52
    {
53
        $repository      = $this->getAccountRepository();
54
        $accountList     = $repository->getActiveAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
55
        $defaultCurrency = app('amount')->getDefaultCurrency();
56
        $grouped         = [];
57
        $date            = $this->getDate();
58
59
        /** @var Account $account */
60
        foreach ($accountList as $account) {
61
            $balance                     = app('steam')->balance($account, $date);
62
            $role                        = $repository->getMetaValue($account, 'account_role');
63
            $currency                    = $repository->getAccountCurrency($account) ?? $defaultCurrency;
64
            $role                        = '' === $role ? 'no_account_type' : $role;
65
            $key                         = (string)trans(sprintf('firefly.opt_group_%s', $role));
66
            $formatted                   = app('amount')->formatAnything($currency, $balance, false);
67
            $name                        = sprintf('%s (%s)', $account->name, $formatted);
68
            $grouped[$key][$account->id] = $name;
69
        }
70
71
        return $this->select($name, $grouped, $value, $options);
72
    }
73
74
75
    /**
76
     * Return a list that includes liabilities.
77
     *
78
     * @param string $name
79
     * @param mixed  $value
80
     * @param array  $options
81
     *
82
     * @return string
83
     */
84
    public function activeLongAccountList(string $name, $value = null, array $options = null): string
85
    {
86
        $types           = [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,];
87
        $liabilityTypes  = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
88
        $repository      = $this->getAccountRepository();
89
        $accountList     = $repository->getActiveAccountsByType($types);
90
        $defaultCurrency = app('amount')->getDefaultCurrency();
91
        $grouped         = [];
92
        $date            = $this->getDate();
93
94
        /** @var Account $account */
95
        foreach ($accountList as $account) {
96
            $balance  = app('steam')->balance($account, $date);
97
            $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
98
            $role     = $repository->getMetaValue($account, 'account_role');
99
100
            if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
101
                $role = 'no_account_type';
102
            }
103
104
            if (in_array($account->accountType->type, $liabilityTypes, true)) {
105
                $role = sprintf('l_%s', $account->accountType->type);
106
            }
107
108
            $key                         = (string)trans(sprintf('firefly.opt_group_%s', $role));
109
            $formatted                   = app('amount')->formatAnything($currency, $balance, false);
110
            $name                        = sprintf('%s (%s)', $account->name, $formatted);
111
            $grouped[$key][$account->id] = $name;
112
        }
113
114
115
        return $this->select($name, $grouped, $value, $options);
116
    }
117
118
    /**
119
     * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal.
120
     *
121
     * @param string $name
122
     * @param mixed $value
123
     * @param array $options
124
     *
125
     * @return string
126
     */
127
    public function activeWithdrawalDestinations(string $name, $value = null, array $options = null): string
128
    {
129
        $types           = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::EXPENSE,];
130
        $liabilityTypes  = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
131
        $repository      = $this->getAccountRepository();
132
        $accountList     = $repository->getActiveAccountsByType($types);
133
        $defaultCurrency = app('amount')->getDefaultCurrency();
134
        $grouped         = [];
135
        $date            = $this->getDate();
136
137
        $cash                     = $repository->getCashAccount();
138
        $key                      = (string)trans('firefly.cash_account_type');
139
        $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash'));
140
141
        // group accounts:
142
        /** @var Account $account */
143
        foreach ($accountList as $account) {
144
            $balance  = app('steam')->balance($account, $date);
145
            $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
146
            $role     = (string)$repository->getMetaValue($account, 'account_role');
147
            if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
148
                $role = 'no_account_type';
149
            }
150
151
            if ('no_account_type' === $role && AccountType::EXPENSE === $account->accountType->type) {
152
                $role = 'expense_account';
153
            }
154
155
            if (in_array($account->accountType->type, $liabilityTypes, true)) {
156
                $role = sprintf('l_%s', $account->accountType->type);
157
            }
158
            $key                         = (string)trans('firefly.opt_group_' . $role);
159
            $formatted                   = app('amount')->formatAnything($currency, $balance, false);
160
            $name                        = sprintf('%s (%s)', $account->name, $formatted);
161
            $grouped[$key][$account->id] = $name;
162
        }
163
164
        return $this->select($name, $grouped, $value, $options);
165
    }
166
167
    /**
168
     * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal.
169
     *
170
     * @param string $name
171
     * @param mixed  $value
172
     * @param array  $options
173
     *
174
     * @return string
175
     */
176
    public function activeDepositDestinations(string $name, $value = null, array $options = null): string
177
    {
178
        $types                    = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::REVENUE,];
179
        $liabilityTypes           = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
180
        $repository               = $this->getAccountRepository();
181
        $accountList              = $repository->getActiveAccountsByType($types);
182
        $defaultCurrency          = app('amount')->getDefaultCurrency();
183
        $grouped                  = [];
184
        $date                     = $this->getDate();
185
        $cash                     = $repository->getCashAccount();
186
        $key                      = (string)trans('firefly.cash_account_type');
187
        $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash'));
188
189
        // group accounts:
190
        /** @var Account $account */
191
        foreach ($accountList as $account) {
192
            $balance  = app('steam')->balance($account, $date);
193
            $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
194
            $role     = (string)$repository->getMetaValue($account, 'account_role');
195
            if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) {
196
                $role = 'no_account_type';
197
            }
198
            if ('no_account_type' === $role && AccountType::REVENUE === $account->accountType->type) {
199
                $role = 'revenue_account';
200
            }
201
            if (in_array($account->accountType->type, $liabilityTypes, true)) {
202
                $role = sprintf('l_%s', $account->accountType->type); // @codeCoverageIgnore
203
            }
204
            $key                         = (string)trans(sprintf('firefly.opt_group_%s', $role));
205
            $formatted                   = app('amount')->formatAnything($currency, $balance, false);
206
            $name                        = sprintf('%s (%s)', $account->name, $formatted);
207
            $grouped[$key][$account->id] = $name;
208
        }
209
210
        return $this->select($name, $grouped, $value, $options);
211
    }
212
213
214
    /**
215
     * Check list of asset accounts.
216
     *
217
     * @param string $name
218
     * @param array $options
219
     *
220
     * @return string
221
     *
222
     */
223
    public function assetAccountCheckList(string $name, array $options = null): string
224
    {
225
        $options  = $options ?? [];
226
        $label    = $this->label($name, $options);
227
        $options  = $this->expandOptionArray($name, $label, $options);
228
        $classes  = $this->getHolderClasses($name);
229
        $selected = request()->old($name) ?? [];
230
231
        // get all asset accounts:
232
        $repository    = $this->getAccountRepository();
233
        $types         = [AccountType::ASSET, AccountType::DEFAULT];
234
        $assetAccounts = $repository->getAccountsByType($types);
235
        $grouped       = [];
236
        // group accounts:
237
        /** @var Account $account */
238
        foreach ($assetAccounts as $account) {
239
            $role = $repository->getMetaValue($account, 'account_role');
240
            if (null === $role) {
241
                $role = 'no_account_type';
242
            }
243
            $key                         = (string)trans(sprintf('firefly.opt_group_%s', $role));
244
            $grouped[$key][$account->id] = $account->name;
245
        }
246
247
        unset($options['class']);
248
        try {
249
            $html = view('form.assetAccountCheckList', compact('classes', 'selected', 'name', 'label', 'options', 'grouped'))->render();
250
        } catch (Throwable $e) {
251
            Log::debug(sprintf('Could not render assetAccountCheckList(): %s', $e->getMessage()));
252
            $html = 'Could not render assetAccountCheckList.';
253
        }
254
255
        return $html;
256
    }
257
258
    /**
259
     * Basic list of asset accounts.
260
     *
261
     * @param string $name
262
     * @param mixed $value
263
     * @param array $options
264
     *
265
     * @return string
266
     */
267
    public function assetAccountList(string $name, $value = null, array $options = null): string
268
    {
269
        $repository      = $this->getAccountRepository();
270
        $types           = [AccountType::ASSET, AccountType::DEFAULT];
271
        $accountList     = $repository->getAccountsByType($types);
272
        $defaultCurrency = app('amount')->getDefaultCurrency();
273
        $grouped         = [];
274
        $date            = $this->getDate();
275
276
        /** @var Account $account */
277
        foreach ($accountList as $account) {
278
            $balance  = app('steam')->balance($account, $date);
279
            $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
280
            $role     = (string)$repository->getMetaValue($account, 'account_role');
281
            if ('' === $role) {
282
                $role = 'no_account_type';
283
            }
284
285
            $key                         = (string)trans(sprintf('firefly.opt_group_%s', $role));
286
            $formatted                   = app('amount')->formatAnything($currency, $balance, false);
287
            $grouped[$key][$account->id] = sprintf('%s (%s)', $account->name, $formatted);
288
        }
289
290
        return $this->select($name, $grouped, $value, $options);
291
    }
292
293
294
    /**
295
     * Same list but all liabilities as well.
296
     *
297
     * @param string $name
298
     * @param mixed $value
299
     * @param array $options
300
     *
301
     * @return string
302
     */
303
    public function longAccountList(string $name, $value = null, array $options = null): string
304
    {
305
        $types           = [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,];
306
        $liabilityTypes  = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
307
        $repository      = $this->getAccountRepository();
308
        $accountList     = $repository->getAccountsByType($types);
309
        $defaultCurrency = app('amount')->getDefaultCurrency();
310
        $grouped         = [];
311
        $date            = $this->getDate();
312
        /** @var Account $account */
313
        foreach ($accountList as $account) {
314
            $balance  = app('steam')->balance($account, $date);
315
            $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency;
316
            $role     = (string)$repository->getMetaValue($account, 'account_role');
317
            if ('' === $role) {
318
                $role = 'no_account_type';
319
            }
320
            if (in_array($account->accountType->type, $liabilityTypes, true)) {
321
                $role = sprintf('l_%s', $account->accountType->type);
322
            }
323
            $key                         = (string)trans(sprintf('firefly.opt_group_%s', $role));
324
            $formatted                   = app('amount')->formatAnything($currency, $balance, false);
325
            $grouped[$key][$account->id] = sprintf('%s (%s)', $account->name, $formatted);
326
        }
327
328
        return $this->select($name, $grouped, $value, $options);
329
    }
330
}