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/Models/TransactionJournal.php (1 issue)

Severity
1
<?php
2
/**
3
 * TransactionJournal.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\Models;
24
25
use Carbon\Carbon;
26
use Crypt;
27
use FireflyIII\Support\CacheProperties;
28
use FireflyIII\Support\Models\TransactionJournalTrait;
29
use FireflyIII\User;
30
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
31
use Illuminate\Database\Eloquent\Model;
32
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
33
use Illuminate\Database\Eloquent\Relations\HasMany;
34
use Illuminate\Database\Eloquent\SoftDeletes;
35
use Log;
36
use Preferences;
37
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
38
39
/**
40
 * Class TransactionJournal.
41
 *
42
 * @property User $user
43
 */
44
class TransactionJournal extends Model
45
{
46
    use SoftDeletes, TransactionJournalTrait;
0 ignored issues
show
The trait FireflyIII\Support\Models\TransactionJournalTrait requires some properties which are not provided by FireflyIII\Models\TransactionJournal: $joins, $account, $type
Loading history...
47
48
    /**
49
     * The attributes that should be casted to native types.
50
     *
51
     * @var array
52
     */
53
    protected $casts
54
        = [
55
            'created_at'    => 'datetime',
56
            'updated_at'    => 'datetime',
57
            'deleted_at'    => 'datetime',
58
            'date'          => 'date',
59
            'interest_date' => 'date',
60
            'book_date'     => 'date',
61
            'process_date'  => 'date',
62
            'order'         => 'int',
63
            'tag_count'     => 'int',
64
            'encrypted'     => 'boolean',
65
            'completed'     => 'boolean',
66
        ];
67
68
    /** @var array */
69
    protected $fillable
70
        = ['user_id', 'transaction_type_id', 'bill_id', 'interest_date', 'book_date', 'process_date',
71
           'transaction_currency_id', 'description', 'completed',
72
           'date', 'rent_date', 'encrypted', 'tag_count',];
73
    /** @var array */
74
    protected $hidden = ['encrypted'];
75
76
    /**
77
     * @param string $value
78
     *
79
     * @return TransactionJournal
80
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
81
     */
82
    public static function routeBinder(string $value): TransactionJournal
83
    {
84
        if (auth()->check()) {
85
            $journalId = (int)$value;
86
            $journal   = auth()->user()->transactionJournals()->where('transaction_journals.id', $journalId)
87
                               ->first(['transaction_journals.*']);
88
            if (null !== $journal) {
89
                return $journal;
90
            }
91
        }
92
93
        throw new NotFoundHttpException;
94
    }
95
96
    /**
97
     * @codeCoverageIgnore
98
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
99
     */
100
    public function attachments()
101
    {
102
        return $this->morphMany('FireflyIII\Models\Attachment', 'attachable');
103
    }
104
105
    /**
106
     * @codeCoverageIgnore
107
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
108
     */
109
    public function bill()
110
    {
111
        return $this->belongsTo('FireflyIII\Models\Bill');
112
    }
113
114
    /**
115
     * @codeCoverageIgnore
116
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
117
     */
118
    public function budgets(): BelongsToMany
119
    {
120
        return $this->belongsToMany('FireflyIII\Models\Budget');
121
    }
122
123
    /**
124
     * @codeCoverageIgnore
125
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
126
     */
127
    public function categories(): BelongsToMany
128
    {
129
        return $this->belongsToMany('FireflyIII\Models\Category');
130
    }
131
132
    /**
133
     * @codeCoverageIgnore
134
     * @deprecated
135
     *
136
     * @param string $name
137
     *
138
     * @return bool
139
     */
140
    public function deleteMeta(string $name): bool
141
    {
142
        $this->transactionJournalMeta()->where('name', $name)->delete();
143
144
        return true;
145
    }
146
147
    /**
148
     * @codeCoverageIgnore
149
     *
150
     * @return HasMany
151
     */
152
    public function destinationJournalLinks(): HasMany
153
    {
154
        return $this->hasMany(TransactionJournalLink::class, 'destination_id');
155
    }
156
157
    /**
158
     * @codeCoverageIgnore
159
     *
160
     * @param $value
161
     *
162
     * @return string
163
     * @throws \Illuminate\Contracts\Encryption\DecryptException
164
     */
165
    public function getDescriptionAttribute($value)
166
    {
167
        if ($this->encrypted) {
168
            return Crypt::decrypt($value);
169
        }
170
171
        return $value;
172
    }
173
174
    /**
175
     *
176
     * @param string $name
177
     *
178
     * @deprecated
179
     * @return string
180
     */
181
    public function getMeta(string $name)
182
    {
183
        $value = null;
184
        $cache = new CacheProperties;
185
        $cache->addProperty('journal-meta');
186
        $cache->addProperty($this->id);
187
        $cache->addProperty($name);
188
189
        if ($cache->has()) {
190
            return $cache->get(); // @codeCoverageIgnore
191
        }
192
193
        Log::debug(sprintf('Looking for journal #%d meta field "%s".', $this->id, $name));
194
        $entry = $this->transactionJournalMeta()->where('name', $name)->first();
195
        if (null !== $entry) {
196
            $value = $entry->data;
197
            // cache:
198
            $cache->store($value);
199
        }
200
201
        // convert to Carbon if name is _date
202
        if (null !== $value && '_date' === substr($name, -5)) {
203
            $value = new Carbon($value);
204
            // cache:
205
            $cache->store($value);
206
        }
207
208
        return $value;
209
    }
210
211
    /**
212
     * @codeCoverageIgnore
213
     *
214
     * @param string $name
215
     *
216
     * @deprecated
217
     * @return bool
218
     */
219
    public function hasMeta(string $name): bool
220
    {
221
        return null !== $this->getMeta($name);
222
    }
223
224
    /**
225
     * @codeCoverageIgnore
226
     * @return bool
227
     */
228
    public function isDeposit(): bool
229
    {
230
        if (null !== $this->transaction_type_type) {
231
            return TransactionType::DEPOSIT === $this->transaction_type_type;
232
        }
233
234
        return $this->transactionType->isDeposit();
235
    }
236
237
    /**
238
     * @codeCoverageIgnore
239
     * @return bool
240
     */
241
    public function isOpeningBalance(): bool
242
    {
243
        if (null !== $this->transaction_type_type) {
244
            return TransactionType::OPENING_BALANCE === $this->transaction_type_type;
245
        }
246
247
        return $this->transactionType->isOpeningBalance();
248
    }
249
250
    /**
251
     * @codeCoverageIgnore
252
     * @return bool
253
     */
254
    public function isTransfer(): bool
255
    {
256
        if (null !== $this->transaction_type_type) {
257
            return TransactionType::TRANSFER === $this->transaction_type_type;
258
        }
259
260
        return $this->transactionType->isTransfer();
261
    }
262
263
    /**
264
     * @codeCoverageIgnore
265
     * @return bool
266
     */
267
    public function isWithdrawal(): bool
268
    {
269
        if (null !== $this->transaction_type_type) {
270
            return TransactionType::WITHDRAWAL === $this->transaction_type_type;
271
        }
272
273
        return $this->transactionType->isWithdrawal();
274
    }
275
276
    /**
277
     * @codeCoverageIgnore
278
     * Get all of the notes.
279
     */
280
    public function notes()
281
    {
282
        return $this->morphMany('FireflyIII\Models\Note', 'noteable');
283
    }
284
285
    /**
286
     * @codeCoverageIgnore
287
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
288
     */
289
    public function piggyBankEvents(): HasMany
290
    {
291
        return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
292
    }
293
294
    /**
295
     * @codeCoverageIgnore
296
     *
297
     * @param EloquentBuilder $query
298
     * @param Carbon          $date
299
     *
300
     * @return EloquentBuilder
301
     */
302
    public function scopeAfter(EloquentBuilder $query, Carbon $date)
303
    {
304
        return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
305
    }
306
307
    /**
308
     * @codeCoverageIgnore
309
     *
310
     * @param EloquentBuilder $query
311
     * @param Carbon          $date
312
     *
313
     * @return EloquentBuilder
314
     */
315
    public function scopeBefore(EloquentBuilder $query, Carbon $date)
316
    {
317
        return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
318
    }
319
320
    /**
321
     * @codeCoverageIgnore
322
     *
323
     * @param EloquentBuilder $query
324
     * @param array           $types
325
     */
326
    public function scopeTransactionTypes(EloquentBuilder $query, array $types)
327
    {
328
        if (!self::isJoined($query, 'transaction_types')) {
329
            $query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
330
        }
331
        if (count($types) > 0) {
332
            $query->whereIn('transaction_types.type', $types);
333
        }
334
    }
335
336
    /**
337
     * @codeCoverageIgnore
338
     *
339
     * @param $value
340
     *
341
     * @throws \Illuminate\Contracts\Encryption\EncryptException
342
     */
343
    public function setDescriptionAttribute($value)
344
    {
345
        $encrypt                         = config('firefly.encryption');
346
        $this->attributes['description'] = $encrypt ? Crypt::encrypt($value) : $value;
347
        $this->attributes['encrypted']   = $encrypt;
348
    }
349
350
    /**
351
     * @deprecated
352
     *
353
     * @param string $name
354
     * @param        $value
355
     *
356
     * @return TransactionJournalMeta
357
     */
358
    public function setMeta(string $name, $value): TransactionJournalMeta
359
    {
360
        if (null === $value) {
361
            $this->deleteMeta($name);
362
363
            return new TransactionJournalMeta();
364
        }
365
        if (is_string($value) && 0 === strlen($value)) {
366
            $this->deleteMeta($name);
367
368
            return new TransactionJournalMeta();
369
        }
370
371
        if ($value instanceof Carbon) {
372
            $value = $value->toW3cString();
373
        }
374
375
        Log::debug(sprintf('Going to set "%s" with value "%s"', $name, json_encode($value)));
376
        $entry = $this->transactionJournalMeta()->where('name', $name)->first();
377
        if (null === $entry) {
378
            $entry = new TransactionJournalMeta();
379
            $entry->transactionJournal()->associate($this);
380
            $entry->name = $name;
381
        }
382
        $entry->data = $value;
383
        $entry->save();
384
        Preferences::mark();
385
386
        return $entry;
387
    }
388
389
    /**
390
     * @codeCoverageIgnore
391
     * @return HasMany
392
     */
393
    public function sourceJournalLinks(): HasMany
394
    {
395
        return $this->hasMany(TransactionJournalLink::class, 'source_id');
396
    }
397
398
    /**
399
     * @codeCoverageIgnore
400
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
401
     */
402
    public function tags()
403
    {
404
        return $this->belongsToMany('FireflyIII\Models\Tag');
405
    }
406
407
    /**
408
     * @codeCoverageIgnore
409
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
410
     */
411
    public function transactionCurrency()
412
    {
413
        return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
414
    }
415
416
    /**
417
     * @codeCoverageIgnore
418
     * @return HasMany
419
     */
420
    public function transactionJournalMeta(): HasMany
421
    {
422
        return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
423
    }
424
425
    /**
426
     * @codeCoverageIgnore
427
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
428
     */
429
    public function transactionType()
430
    {
431
        return $this->belongsTo('FireflyIII\Models\TransactionType');
432
    }
433
434
    /**
435
     * @codeCoverageIgnore
436
     * @return HasMany
437
     */
438
    public function transactions(): HasMany
439
    {
440
        return $this->hasMany('FireflyIII\Models\Transaction');
441
    }
442
443
    /**
444
     * @codeCoverageIgnore
445
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
446
     */
447
    public function user()
448
    {
449
        return $this->belongsTo('FireflyIII\User');
450
    }
451
}
452