1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JournalAPIRepository.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\Repositories\Journal; |
23
|
|
|
|
24
|
|
|
use FireflyIII\Models\PiggyBankEvent; |
25
|
|
|
use FireflyIII\Models\Transaction; |
26
|
|
|
use FireflyIII\Models\TransactionJournal; |
27
|
|
|
use FireflyIII\User; |
28
|
|
|
use Illuminate\Support\Collection; |
29
|
|
|
use Log; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class JournalAPIRepository |
33
|
|
|
*/ |
34
|
|
|
class JournalAPIRepository implements JournalAPIRepositoryInterface |
35
|
|
|
{ |
36
|
|
|
/** @var User */ |
37
|
|
|
private $user; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
if ('testing' === config('app.env')) { |
45
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns transaction by ID. Used to validate attachments. |
51
|
|
|
* |
52
|
|
|
* @param int $transactionId |
53
|
|
|
* |
54
|
|
|
* @return Transaction|null |
55
|
|
|
*/ |
56
|
|
|
public function findTransaction(int $transactionId): ?Transaction |
57
|
|
|
{ |
58
|
|
|
$transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') |
59
|
|
|
->where('transaction_journals.user_id', $this->user->id) |
60
|
|
|
->where('transactions.id', $transactionId) |
61
|
|
|
->first(['transactions.*']); |
62
|
|
|
|
63
|
|
|
return $transaction; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return all attachments for journal. |
68
|
|
|
* |
69
|
|
|
* @param TransactionJournal $journal |
70
|
|
|
* |
71
|
|
|
* @return Collection |
72
|
|
|
*/ |
73
|
|
|
public function getAttachments(TransactionJournal $journal): Collection |
74
|
|
|
{ |
75
|
|
|
return $journal->attachments; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get all piggy bank events for a journal. |
80
|
|
|
* |
81
|
|
|
* @param TransactionJournal $journal |
82
|
|
|
* |
83
|
|
|
* @return Collection |
84
|
|
|
*/ |
85
|
|
|
public function getPiggyBankEvents(TransactionJournal $journal): Collection |
86
|
|
|
{ |
87
|
|
|
/** @var Collection $set */ |
88
|
|
|
$events = $journal->piggyBankEvents()->get(); |
89
|
|
|
$events->each( |
90
|
|
|
function (PiggyBankEvent $event) { |
91
|
|
|
$event->piggyBank = $event->piggyBank()->withTrashed()->first(); |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $events; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param User $user |
100
|
|
|
*/ |
101
|
|
|
public function setUser(User $user): void |
102
|
|
|
{ |
103
|
|
|
$this->user = $user; |
104
|
|
|
} |
105
|
|
|
} |