1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JournalCLIRepository.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
|
|
|
|
25
|
|
|
use Carbon\Carbon; |
26
|
|
|
use DB; |
27
|
|
|
use Exception; |
28
|
|
|
use FireflyIII\Models\TransactionJournal; |
29
|
|
|
use FireflyIII\Support\CacheProperties; |
30
|
|
|
use FireflyIII\User; |
31
|
|
|
use Illuminate\Support\Collection; |
32
|
|
|
use Log; |
33
|
|
|
use stdClass; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class JournalCLIRepository |
37
|
|
|
*/ |
38
|
|
|
class JournalCLIRepository implements JournalCLIRepositoryInterface |
39
|
|
|
{ |
40
|
|
|
/** @var User */ |
41
|
|
|
private $user; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
if ('testing' === config('app.env')) { |
49
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all transaction journals with a specific type, regardless of user. |
55
|
|
|
* |
56
|
|
|
* @param array $types |
57
|
|
|
* |
58
|
|
|
* @return Collection |
59
|
|
|
*/ |
60
|
|
|
public function getAllJournals(array $types): Collection |
61
|
|
|
{ |
62
|
|
|
return TransactionJournal |
63
|
|
|
::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') |
64
|
|
|
->whereIn('transaction_types.type', $types) |
65
|
|
|
->with(['user', 'transactionType', 'transactionCurrency', 'transactions', 'transactions.account']) |
66
|
|
|
->get(['transaction_journals.*']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the ID of the budget linked to the journal (if any) or the transactions (if any). |
71
|
|
|
* |
72
|
|
|
* @param TransactionJournal $journal |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public function getJournalBudgetId(TransactionJournal $journal): int |
77
|
|
|
{ |
78
|
|
|
$budget = $journal->budgets()->first(); |
79
|
|
|
if (null !== $budget) { |
80
|
|
|
return $budget->id; |
81
|
|
|
} |
82
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
83
|
|
|
$budget = $journal->transactions()->first()->budgets()->first(); |
84
|
|
|
if (null !== $budget) { |
85
|
|
|
return $budget->id; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the ID of the category linked to the journal (if any) or to the transactions (if any). |
93
|
|
|
* |
94
|
|
|
* @param TransactionJournal $journal |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function getJournalCategoryId(TransactionJournal $journal): int |
99
|
|
|
{ |
100
|
|
|
$category = $journal->categories()->first(); |
101
|
|
|
if (null !== $category) { |
102
|
|
|
return $category->id; |
103
|
|
|
} |
104
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
105
|
|
|
$category = $journal->transactions()->first()->categories()->first(); |
106
|
|
|
if (null !== $category) { |
107
|
|
|
return $category->id; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return all journals without a group, used in an upgrade routine. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getJournalsWithoutGroup(): array |
119
|
|
|
{ |
120
|
|
|
return TransactionJournal::whereNull('transaction_group_id')->get(['id', 'user_id'])->toArray(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return Carbon value of a meta field (or NULL). |
125
|
|
|
* |
126
|
|
|
* @param TransactionJournal $journal |
127
|
|
|
* @param string $field |
128
|
|
|
* |
129
|
|
|
* @return null|Carbon |
130
|
|
|
*/ |
131
|
|
|
public function getMetaDate(TransactionJournal $journal, string $field): ?Carbon |
132
|
|
|
{ |
133
|
|
|
$cache = new CacheProperties; |
134
|
|
|
$cache->addProperty('journal-meta-updated'); |
135
|
|
|
$cache->addProperty($journal->id); |
136
|
|
|
$cache->addProperty($field); |
137
|
|
|
|
138
|
|
|
if ($cache->has()) { |
139
|
|
|
$result = null; |
|
|
|
|
140
|
|
|
try { |
141
|
|
|
$result = new Carbon($cache->get()); // @codeCoverageIgnore |
142
|
|
|
} catch (Exception $e) { |
143
|
|
|
$e->getMessage(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $result; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$entry = $journal->transactionJournalMeta()->where('name', $field)->first(); |
150
|
|
|
if (null === $entry) { |
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
$value = null; |
|
|
|
|
154
|
|
|
try { |
155
|
|
|
$value = new Carbon($entry->data); |
156
|
|
|
} catch (Exception $e) { |
157
|
|
|
$e->getMessage(); |
158
|
|
|
|
159
|
|
|
return null; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$cache->store($entry->data); |
163
|
|
|
|
164
|
|
|
return $value; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return value of a meta field (or NULL) as a string. |
169
|
|
|
* |
170
|
|
|
* @param TransactionJournal $journal |
171
|
|
|
* @param string $field |
172
|
|
|
* |
173
|
|
|
* @return null|string |
174
|
|
|
*/ |
175
|
|
|
public function getMetaField(TransactionJournal $journal, string $field): ?string |
176
|
|
|
{ |
177
|
|
|
$cache = new CacheProperties; |
178
|
|
|
$cache->addProperty('journal-meta-updated'); |
179
|
|
|
$cache->addProperty($journal->id); |
180
|
|
|
$cache->addProperty($field); |
181
|
|
|
|
182
|
|
|
if ($cache->has()) { |
183
|
|
|
return $cache->get(); // @codeCoverageIgnore |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$entry = $journal->transactionJournalMeta()->where('name', $field)->first(); |
187
|
|
|
if (null === $entry) { |
188
|
|
|
return null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$value = $entry->data; |
192
|
|
|
|
193
|
|
|
if (is_array($value)) { |
194
|
|
|
$return = implode(',', $value); |
195
|
|
|
$cache->store($return); |
196
|
|
|
|
197
|
|
|
return $return; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// return when something else: |
201
|
|
|
try { |
202
|
|
|
$return = (string)$value; |
203
|
|
|
$cache->store($return); |
204
|
|
|
} catch (Exception $e) { |
205
|
|
|
Log::error($e->getMessage()); |
206
|
|
|
|
207
|
|
|
return ''; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $return; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Return text of a note attached to journal, or NULL |
215
|
|
|
* |
216
|
|
|
* @param TransactionJournal $journal |
217
|
|
|
* |
218
|
|
|
* @return string|null |
219
|
|
|
*/ |
220
|
|
|
public function getNoteText(TransactionJournal $journal): ?string |
221
|
|
|
{ |
222
|
|
|
$note = $journal->notes()->first(); |
223
|
|
|
if (null === $note) { |
224
|
|
|
return null; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $note->text; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns all journals with more than 2 transactions. Should only return empty collections |
232
|
|
|
* in Firefly III > v4.8,0. |
233
|
|
|
* |
234
|
|
|
* @return Collection |
235
|
|
|
*/ |
236
|
|
|
public function getSplitJournals(): Collection |
237
|
|
|
{ |
238
|
|
|
$query = TransactionJournal |
239
|
|
|
::leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') |
240
|
|
|
->groupBy('transaction_journals.id'); |
241
|
|
|
$result = $query->get(['transaction_journals.id as id', DB::raw('count(transactions.id) as transaction_count')]); |
242
|
|
|
$journalIds = []; |
243
|
|
|
/** @var stdClass $row */ |
244
|
|
|
foreach ($result as $row) { |
245
|
|
|
if ((int)$row->transaction_count > 2) { |
246
|
|
|
$journalIds[] = (int)$row->id; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
$journalIds = array_unique($journalIds); |
250
|
|
|
|
251
|
|
|
return TransactionJournal |
252
|
|
|
::with(['transactions']) |
253
|
|
|
->whereIn('id', $journalIds)->get(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Return all tags as strings in an array. |
258
|
|
|
* |
259
|
|
|
* @param TransactionJournal $journal |
260
|
|
|
* |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
public function getTags(TransactionJournal $journal): array |
264
|
|
|
{ |
265
|
|
|
return $journal->tags()->get()->pluck('tag')->toArray(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param User $user |
270
|
|
|
*/ |
271
|
|
|
public function setUser(User $user): void |
272
|
|
|
{ |
273
|
|
|
$this->user = $user; |
274
|
|
|
} |
275
|
|
|
} |