1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JournalRepository.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\Repositories\Journal; |
24
|
|
|
|
25
|
|
|
use Carbon\Carbon; |
26
|
|
|
use Exception; |
27
|
|
|
use FireflyIII\Factory\TransactionJournalFactory; |
28
|
|
|
use FireflyIII\Factory\TransactionJournalMetaFactory; |
29
|
|
|
use FireflyIII\Models\Account; |
30
|
|
|
use FireflyIII\Models\AccountType; |
31
|
|
|
use FireflyIII\Models\Note; |
32
|
|
|
use FireflyIII\Models\PiggyBankEvent; |
33
|
|
|
use FireflyIII\Models\Transaction; |
34
|
|
|
use FireflyIII\Models\TransactionJournal; |
35
|
|
|
use FireflyIII\Models\TransactionType; |
36
|
|
|
use FireflyIII\Services\Internal\Destroy\JournalDestroyService; |
37
|
|
|
use FireflyIII\Services\Internal\Update\JournalUpdateService; |
38
|
|
|
use FireflyIII\Support\CacheProperties; |
39
|
|
|
use FireflyIII\User; |
40
|
|
|
use Illuminate\Support\Collection; |
41
|
|
|
use Illuminate\Support\MessageBag; |
42
|
|
|
use Log; |
43
|
|
|
use Preferences; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Class JournalRepository. |
47
|
|
|
*/ |
48
|
|
|
class JournalRepository implements JournalRepositoryInterface |
49
|
|
|
{ |
50
|
|
|
/** @var User */ |
51
|
|
|
private $user; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param TransactionJournal $journal |
55
|
|
|
* @param TransactionType $type |
56
|
|
|
* @param Account $source |
57
|
|
|
* @param Account $destination |
58
|
|
|
* |
59
|
|
|
* @return MessageBag |
60
|
|
|
*/ |
61
|
|
|
public function convert(TransactionJournal $journal, TransactionType $type, Account $source, Account $destination): MessageBag |
62
|
|
|
{ |
63
|
|
|
// default message bag that shows errors for everything. |
64
|
|
|
$messages = new MessageBag; |
65
|
|
|
$messages->add('source_account_revenue', trans('firefly.invalid_convert_selection')); |
|
|
|
|
66
|
|
|
$messages->add('destination_account_asset', trans('firefly.invalid_convert_selection')); |
67
|
|
|
$messages->add('destination_account_expense', trans('firefly.invalid_convert_selection')); |
68
|
|
|
$messages->add('source_account_asset', trans('firefly.invalid_convert_selection')); |
69
|
|
|
|
70
|
|
|
if ($source->id === $destination->id || null === $source->id || null === $destination->id) { |
71
|
|
|
return $messages; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first(); |
75
|
|
|
$destinationTransaction = $journal->transactions()->where('amount', '>', 0)->first(); |
76
|
|
|
$sourceTransaction->account_id = $source->id; |
77
|
|
|
$sourceTransaction->save(); |
78
|
|
|
$destinationTransaction->account_id = $destination->id; |
79
|
|
|
$destinationTransaction->save(); |
80
|
|
|
$journal->transaction_type_id = $type->id; |
|
|
|
|
81
|
|
|
$journal->save(); |
82
|
|
|
|
83
|
|
|
// if journal is a transfer now, remove budget: |
84
|
|
|
if (TransactionType::TRANSFER === $type->type) { |
|
|
|
|
85
|
|
|
$journal->budgets()->detach(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
Preferences::mark(); |
|
|
|
|
89
|
|
|
|
90
|
|
|
return new MessageBag; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param TransactionJournal $journal |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function countTransactions(TransactionJournal $journal): int |
99
|
|
|
{ |
100
|
|
|
return $journal->transactions()->count(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param TransactionJournal $journal |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
* |
108
|
|
|
|
109
|
|
|
*/ |
110
|
|
|
public function destroy(TransactionJournal $journal): bool |
111
|
|
|
{ |
112
|
|
|
/** @var JournalDestroyService $service */ |
113
|
|
|
$service = app(JournalDestroyService::class); |
114
|
|
|
$service->destroy($journal); |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int $journalId |
121
|
|
|
* |
122
|
|
|
* @return TransactionJournal |
123
|
|
|
*/ |
124
|
|
|
public function find(int $journalId): TransactionJournal |
125
|
|
|
{ |
126
|
|
|
/** @var TransactionJournal $journal */ |
127
|
|
|
$journal = $this->user->transactionJournals()->where('id', $journalId)->first(); |
128
|
|
|
if (null === $journal) { |
129
|
|
|
return new TransactionJournal; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $journal; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param Transaction $transaction |
137
|
|
|
* |
138
|
|
|
* @return Transaction|null |
139
|
|
|
*/ |
140
|
|
|
public function findOpposingTransaction(Transaction $transaction): ?Transaction |
141
|
|
|
{ |
142
|
|
|
$opposing = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') |
143
|
|
|
->where('transaction_journals.user_id', $this->user->id) |
144
|
|
|
->where('transactions.transaction_journal_id', $transaction->transaction_journal_id) |
|
|
|
|
145
|
|
|
->where('transactions.identifier', $transaction->identifier) |
146
|
|
|
->where('amount', bcmul($transaction->amount, '-1')) |
147
|
|
|
->first(['transactions.*']); |
148
|
|
|
|
149
|
|
|
return $opposing; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param int $transactionid |
154
|
|
|
* |
155
|
|
|
* @return Transaction|null |
156
|
|
|
*/ |
157
|
|
|
public function findTransaction(int $transactionid): ?Transaction |
158
|
|
|
{ |
159
|
|
|
$transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') |
160
|
|
|
->where('transaction_journals.user_id', $this->user->id) |
161
|
|
|
->where('transactions.id', $transactionid) |
162
|
|
|
->first(['transactions.*']); |
163
|
|
|
|
164
|
|
|
return $transaction; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get users first transaction journal. |
169
|
|
|
* |
170
|
|
|
* @deprecated |
171
|
|
|
* @return TransactionJournal |
172
|
|
|
*/ |
173
|
|
|
public function first(): TransactionJournal |
174
|
|
|
{ |
175
|
|
|
/** @var TransactionJournal $entry */ |
176
|
|
|
$entry = $this->user->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']); |
177
|
|
|
|
178
|
|
|
if (null === $entry) { |
179
|
|
|
return new TransactionJournal; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $entry; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get users first transaction journal or NULL. |
187
|
|
|
* |
188
|
|
|
* @return TransactionJournal|null |
189
|
|
|
*/ |
190
|
|
|
public function firstNull(): ?TransactionJournal |
191
|
|
|
{ |
192
|
|
|
/** @var TransactionJournal $entry */ |
193
|
|
|
$entry = $this->user->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']); |
194
|
|
|
$result = null; |
195
|
|
|
if (null !== $entry) { |
196
|
|
|
$result = $entry; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param TransactionJournal $journal |
204
|
|
|
* |
205
|
|
|
* @return Transaction|null |
206
|
|
|
*/ |
207
|
|
|
public function getAssetTransaction(TransactionJournal $journal): ?Transaction |
208
|
|
|
{ |
209
|
|
|
/** @var Transaction $transaction */ |
210
|
|
|
foreach ($journal->transactions as $transaction) { |
211
|
|
|
if (AccountType::ASSET === $transaction->account->accountType->type) { |
212
|
|
|
return $transaction; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns the first positive transaction for the journal. Useful when editing journals. |
221
|
|
|
* |
222
|
|
|
* @param TransactionJournal $journal |
223
|
|
|
* |
224
|
|
|
* @return Transaction |
225
|
|
|
*/ |
226
|
|
|
public function getFirstPosTransaction(TransactionJournal $journal): Transaction |
227
|
|
|
{ |
228
|
|
|
return $journal->transactions()->where('amount', '>', 0)->first(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return the ID of the budget linked to the journal (if any) or the transactions (if any). |
233
|
|
|
* |
234
|
|
|
* @param TransactionJournal $journal |
235
|
|
|
* |
236
|
|
|
* @return int |
237
|
|
|
*/ |
238
|
|
|
public function getJournalBudgetId(TransactionJournal $journal): int |
239
|
|
|
{ |
240
|
|
|
$budget = $journal->budgets()->first(); |
241
|
|
|
if (null !== $budget) { |
242
|
|
|
return $budget->id; |
243
|
|
|
} |
244
|
|
|
$budget = $journal->transactions()->first()->budgets()->first(); |
245
|
|
|
if (null !== $budget) { |
246
|
|
|
return $budget->id; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return 0; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Return the name of the category linked to the journal (if any) or to the transactions (if any). |
254
|
|
|
* |
255
|
|
|
* @param TransactionJournal $journal |
256
|
|
|
* |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function getJournalCategoryName(TransactionJournal $journal): string |
260
|
|
|
{ |
261
|
|
|
$category = $journal->categories()->first(); |
262
|
|
|
if (null !== $category) { |
263
|
|
|
return $category->name; |
264
|
|
|
} |
265
|
|
|
$category = $journal->transactions()->first()->categories()->first(); |
266
|
|
|
if (null !== $category) { |
267
|
|
|
return $category->name; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return ''; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Return requested date as string. When it's a NULL return the date of journal, |
275
|
|
|
* otherwise look for meta field and return that one. |
276
|
|
|
* |
277
|
|
|
* @param TransactionJournal $journal |
278
|
|
|
* @param null|string $field |
279
|
|
|
* |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
public function getJournalDate(TransactionJournal $journal, ?string $field): string |
283
|
|
|
{ |
284
|
|
|
if (null === $field) { |
285
|
|
|
return $journal->date->format('Y-m-d'); |
286
|
|
|
} |
287
|
|
|
if (null !== $journal->$field && $journal->$field instanceof Carbon) { |
288
|
|
|
// make field NULL |
289
|
|
|
$carbon = clone $journal->$field; |
290
|
|
|
$journal->$field = null; |
291
|
|
|
$journal->save(); |
292
|
|
|
|
293
|
|
|
// create meta entry |
294
|
|
|
$journal->setMeta($field, $carbon); |
|
|
|
|
295
|
|
|
|
296
|
|
|
// return that one instead. |
297
|
|
|
return $carbon->format('Y-m-d'); |
298
|
|
|
} |
299
|
|
|
$metaField = $journal->getMeta($field); |
|
|
|
|
300
|
|
|
if (null !== $metaField) { |
|
|
|
|
301
|
|
|
$carbon = new Carbon($metaField); |
302
|
|
|
|
303
|
|
|
return $carbon->format('Y-m-d'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return ''; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Return a list of all destination accounts related to journal. |
311
|
|
|
* |
312
|
|
|
* @param TransactionJournal $journal |
313
|
|
|
* |
314
|
|
|
* @return Collection |
315
|
|
|
*/ |
316
|
|
|
public function getJournalDestinationAccounts(TransactionJournal $journal): Collection |
317
|
|
|
{ |
318
|
|
|
$cache = new CacheProperties; |
319
|
|
|
$cache->addProperty($journal->id); |
320
|
|
|
$cache->addProperty('destination-account-list'); |
321
|
|
|
if ($cache->has()) { |
322
|
|
|
return $cache->get(); // @codeCoverageIgnore |
323
|
|
|
} |
324
|
|
|
$transactions = $journal->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get(); |
325
|
|
|
$list = new Collection; |
326
|
|
|
/** @var Transaction $t */ |
327
|
|
|
foreach ($transactions as $t) { |
328
|
|
|
$list->push($t->account); |
329
|
|
|
} |
330
|
|
|
$list = $list->unique('id'); |
331
|
|
|
$cache->store($list); |
332
|
|
|
|
333
|
|
|
return $list; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Return a list of all source accounts related to journal. |
338
|
|
|
* |
339
|
|
|
* @param TransactionJournal $journal |
340
|
|
|
* |
341
|
|
|
* @return Collection |
342
|
|
|
*/ |
343
|
|
|
public function getJournalSourceAccounts(TransactionJournal $journal): Collection |
344
|
|
|
{ |
345
|
|
|
$cache = new CacheProperties; |
346
|
|
|
$cache->addProperty($journal->id); |
347
|
|
|
$cache->addProperty('source-account-list'); |
348
|
|
|
if ($cache->has()) { |
349
|
|
|
return $cache->get(); // @codeCoverageIgnore |
350
|
|
|
} |
351
|
|
|
$transactions = $journal->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get(); |
352
|
|
|
$list = new Collection; |
353
|
|
|
/** @var Transaction $t */ |
354
|
|
|
foreach ($transactions as $t) { |
355
|
|
|
$list->push($t->account); |
356
|
|
|
} |
357
|
|
|
$list = $list->unique('id'); |
358
|
|
|
$cache->store($list); |
359
|
|
|
|
360
|
|
|
return $list; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Return total amount of journal. Is always positive. |
365
|
|
|
* |
366
|
|
|
* @param TransactionJournal $journal |
367
|
|
|
* |
368
|
|
|
* @return string |
369
|
|
|
*/ |
370
|
|
|
public function getJournalTotal(TransactionJournal $journal): string |
371
|
|
|
{ |
372
|
|
|
$cache = new CacheProperties; |
373
|
|
|
$cache->addProperty($journal->id); |
374
|
|
|
$cache->addProperty('amount-positive'); |
375
|
|
|
if ($cache->has()) { |
376
|
|
|
return $cache->get(); // @codeCoverageIgnore |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
// saves on queries: |
380
|
|
|
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount'); |
381
|
|
|
$amount = (string)$amount; |
382
|
|
|
$cache->store($amount); |
383
|
|
|
|
384
|
|
|
return $amount; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Return Carbon value of a meta field (or NULL). |
389
|
|
|
* |
390
|
|
|
* @param TransactionJournal $journal |
391
|
|
|
* @param string $field |
392
|
|
|
* |
393
|
|
|
* @return null|Carbon |
394
|
|
|
*/ |
395
|
|
|
public function getMetaDate(TransactionJournal $journal, string $field): ?Carbon |
396
|
|
|
{ |
397
|
|
|
$cache = new CacheProperties; |
398
|
|
|
$cache->addProperty('journal-meta-updated'); |
399
|
|
|
$cache->addProperty($journal->id); |
400
|
|
|
$cache->addProperty($field); |
401
|
|
|
|
402
|
|
|
if ($cache->has()) { |
403
|
|
|
return new Carbon($cache->get()); // @codeCoverageIgnore |
|
|
|
|
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
$entry = $journal->transactionJournalMeta()->where('name', $field)->first(); |
407
|
|
|
if (null === $entry) { |
408
|
|
|
return null; |
409
|
|
|
} |
410
|
|
|
$value = new Carbon($entry->data); |
411
|
|
|
$cache->store($entry->data); |
412
|
|
|
|
413
|
|
|
return $value; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Return value of a meta field (or NULL) as a string. |
418
|
|
|
* |
419
|
|
|
* @param TransactionJournal $journal |
420
|
|
|
* @param string $field |
421
|
|
|
* |
422
|
|
|
* @return null|string |
423
|
|
|
*/ |
424
|
|
|
public function getMetaField(TransactionJournal $journal, string $field): ?string |
425
|
|
|
{ |
426
|
|
|
$cache = new CacheProperties; |
427
|
|
|
$cache->addProperty('journal-meta-updated'); |
428
|
|
|
$cache->addProperty($journal->id); |
429
|
|
|
$cache->addProperty($field); |
430
|
|
|
|
431
|
|
|
if ($cache->has()) { |
432
|
|
|
return $cache->get(); // @codeCoverageIgnore |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
$entry = $journal->transactionJournalMeta()->where('name', $field)->first(); |
436
|
|
|
if (null === $entry) { |
437
|
|
|
return null; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
$value = $entry->data; |
441
|
|
|
|
442
|
|
|
// return when array: |
|
|
|
|
443
|
|
|
if (is_array($value)) { |
444
|
|
|
$return = implode(',', $value); |
445
|
|
|
$cache->store($return); |
446
|
|
|
|
447
|
|
|
return $return; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
// return when something else: |
451
|
|
|
try { |
452
|
|
|
$return = (string)$value; |
453
|
|
|
$cache->store($return); |
454
|
|
|
} catch (Exception $e) { |
455
|
|
|
Log::error($e->getMessage()); |
456
|
|
|
|
457
|
|
|
return ''; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
return $return; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @param TransactionJournal $journal |
465
|
|
|
* |
466
|
|
|
* @return Note|null |
467
|
|
|
*/ |
468
|
|
|
public function getNote(TransactionJournal $journal): ?Note |
469
|
|
|
{ |
470
|
|
|
return $journal->notes()->first(); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Return text of a note attached to journal, or NULL |
475
|
|
|
* |
476
|
|
|
* @param TransactionJournal $journal |
477
|
|
|
* |
478
|
|
|
* @return string|null |
479
|
|
|
*/ |
480
|
|
|
public function getNoteText(TransactionJournal $journal): ?string |
481
|
|
|
{ |
482
|
|
|
$note = $this->getNote($journal); |
483
|
|
|
if (null === $note) { |
484
|
|
|
return null; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
return $note->text; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* @param TransactionJournal $journal |
492
|
|
|
* |
493
|
|
|
* @return Collection |
494
|
|
|
*/ |
495
|
|
|
public function getPiggyBankEvents(TransactionJournal $journal): Collection |
496
|
|
|
{ |
497
|
|
|
/** @var Collection $set */ |
498
|
|
|
$events = $journal->piggyBankEvents()->get(); |
499
|
|
|
$events->each( |
500
|
|
|
function (PiggyBankEvent $event) { |
501
|
|
|
$event->piggyBank = $event->piggyBank()->withTrashed()->first(); |
502
|
|
|
} |
503
|
|
|
); |
504
|
|
|
|
505
|
|
|
return $events; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Return all tags as strings in an array. |
510
|
|
|
* |
511
|
|
|
* @param TransactionJournal $journal |
512
|
|
|
* |
513
|
|
|
* @return array |
514
|
|
|
*/ |
515
|
|
|
public function getTags(TransactionJournal $journal): array |
516
|
|
|
{ |
517
|
|
|
return $journal->tags()->get()->pluck('tag')->toArray(); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Return the transaction type of the journal. |
522
|
|
|
* |
523
|
|
|
* @param TransactionJournal $journal |
524
|
|
|
* |
525
|
|
|
* @return string |
526
|
|
|
*/ |
527
|
|
|
public function getTransactionType(TransactionJournal $journal): string |
528
|
|
|
{ |
529
|
|
|
return $journal->transactionType->type; |
|
|
|
|
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* @return Collection |
534
|
|
|
*/ |
535
|
|
|
public function getTransactionTypes(): Collection |
536
|
|
|
{ |
537
|
|
|
return TransactionType::orderBy('type', 'ASC')->get(); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* @param array $transactionIds |
542
|
|
|
* |
543
|
|
|
* @return Collection |
544
|
|
|
*/ |
545
|
|
|
public function getTransactionsById(array $transactionIds): Collection |
546
|
|
|
{ |
547
|
|
|
$set = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') |
548
|
|
|
->whereIn('transactions.id', $transactionIds) |
549
|
|
|
->where('transaction_journals.user_id', $this->user->id) |
550
|
|
|
->whereNull('transaction_journals.deleted_at') |
551
|
|
|
->whereNull('transactions.deleted_at') |
552
|
|
|
->get(['transactions.*']); |
553
|
|
|
|
554
|
|
|
return $set; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Will tell you if journal is reconciled or not. |
559
|
|
|
* |
560
|
|
|
* @param TransactionJournal $journal |
561
|
|
|
* |
562
|
|
|
* @return bool |
563
|
|
|
*/ |
564
|
|
|
public function isJournalReconciled(TransactionJournal $journal): bool |
565
|
|
|
{ |
566
|
|
|
foreach ($journal->transactions as $transaction) { |
567
|
|
|
if ($transaction->reconciled) { |
568
|
|
|
return true; |
569
|
|
|
} |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
return false; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* @param Transaction $transaction |
577
|
|
|
* |
578
|
|
|
* @return bool |
579
|
|
|
*/ |
580
|
|
|
public function reconcile(Transaction $transaction): bool |
581
|
|
|
{ |
582
|
|
|
Log::debug(sprintf('Going to reconcile transaction #%d', $transaction->id)); |
583
|
|
|
$opposing = $this->findOpposingTransaction($transaction); |
584
|
|
|
|
585
|
|
|
if (null === $opposing) { |
586
|
|
|
Log::debug('Opposing transaction is NULL. Cannot reconcile.'); |
587
|
|
|
|
588
|
|
|
return false; |
589
|
|
|
} |
590
|
|
|
Log::debug(sprintf('Opposing transaction ID is #%d', $opposing->id)); |
591
|
|
|
|
592
|
|
|
$transaction->reconciled = true; |
593
|
|
|
$opposing->reconciled = true; |
594
|
|
|
$transaction->save(); |
595
|
|
|
$opposing->save(); |
596
|
|
|
|
597
|
|
|
return true; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* @param int $transactionId |
602
|
|
|
* |
603
|
|
|
* @return bool |
604
|
|
|
*/ |
605
|
|
|
public function reconcileById(int $transactionId): bool |
606
|
|
|
{ |
607
|
|
|
/** @var Transaction $transaction */ |
608
|
|
|
$transaction = $this->user->transactions()->find($transactionId); |
609
|
|
|
if (null !== $transaction) { |
610
|
|
|
return $this->reconcile($transaction); |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
return false; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Set meta field for journal that contains a date. |
618
|
|
|
* |
619
|
|
|
* @param TransactionJournal $journal |
620
|
|
|
* @param string $name |
621
|
|
|
* @param Carbon $date |
622
|
|
|
* |
623
|
|
|
* @return void |
624
|
|
|
*/ |
625
|
|
|
public function setMetaDate(TransactionJournal $journal, string $name, Carbon $date): void |
626
|
|
|
{ |
627
|
|
|
/** @var TransactionJournalMetaFactory $factory */ |
628
|
|
|
$factory = app(TransactionJournalMetaFactory::class); |
629
|
|
|
$factory->updateOrCreate( |
630
|
|
|
[ |
631
|
|
|
'data' => $date, |
632
|
|
|
'journal' => $journal, |
633
|
|
|
'name' => $name, |
634
|
|
|
] |
635
|
|
|
); |
636
|
|
|
|
637
|
|
|
return; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Set meta field for journal that contains string. |
642
|
|
|
* |
643
|
|
|
* @param TransactionJournal $journal |
644
|
|
|
* @param string $name |
645
|
|
|
* @param string $value |
646
|
|
|
*/ |
647
|
|
|
public function setMetaString(TransactionJournal $journal, string $name, string $value): void |
648
|
|
|
{ |
649
|
|
|
/** @var TransactionJournalMetaFactory $factory */ |
650
|
|
|
$factory = app(TransactionJournalMetaFactory::class); |
651
|
|
|
$factory->updateOrCreate( |
652
|
|
|
[ |
653
|
|
|
'data' => $value, |
654
|
|
|
'journal' => $journal, |
655
|
|
|
'name' => $name, |
656
|
|
|
] |
657
|
|
|
); |
658
|
|
|
|
659
|
|
|
return; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* @param TransactionJournal $journal |
664
|
|
|
* @param int $order |
665
|
|
|
* |
666
|
|
|
* @return bool |
667
|
|
|
*/ |
668
|
|
|
public function setOrder(TransactionJournal $journal, int $order): bool |
669
|
|
|
{ |
670
|
|
|
$journal->order = $order; |
|
|
|
|
671
|
|
|
$journal->save(); |
672
|
|
|
|
673
|
|
|
return true; |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
/** |
677
|
|
|
* @param User $user |
678
|
|
|
*/ |
679
|
|
|
public function setUser(User $user) |
680
|
|
|
{ |
681
|
|
|
$this->user = $user; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* @param array $data |
686
|
|
|
* |
687
|
|
|
* @return TransactionJournal |
688
|
|
|
* |
689
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException |
690
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException |
691
|
|
|
*/ |
692
|
|
|
public function store(array $data): TransactionJournal |
693
|
|
|
{ |
694
|
|
|
/** @var TransactionJournalFactory $factory */ |
695
|
|
|
$factory = app(TransactionJournalFactory::class); |
696
|
|
|
$factory->setUser($this->user); |
697
|
|
|
|
698
|
|
|
return $factory->create($data); |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
/** |
702
|
|
|
* @param TransactionJournal $journal |
703
|
|
|
* @param array $data |
704
|
|
|
* |
705
|
|
|
* @return TransactionJournal |
706
|
|
|
* |
707
|
|
|
*/ |
708
|
|
|
public function update(TransactionJournal $journal, array $data): TransactionJournal |
709
|
|
|
{ |
710
|
|
|
/** @var JournalUpdateService $service */ |
711
|
|
|
$service = app(JournalUpdateService::class); |
712
|
|
|
$journal = $service->update($journal, $data); |
713
|
|
|
|
714
|
|
|
return $journal; |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Update budget for a journal. |
719
|
|
|
* |
720
|
|
|
* @param TransactionJournal $journal |
721
|
|
|
* @param int $budgetId |
722
|
|
|
* |
723
|
|
|
* @return TransactionJournal |
724
|
|
|
*/ |
725
|
|
|
public function updateBudget(TransactionJournal $journal, int $budgetId): TransactionJournal |
726
|
|
|
{ |
727
|
|
|
/** @var JournalUpdateService $service */ |
728
|
|
|
$service = app(JournalUpdateService::class); |
729
|
|
|
|
730
|
|
|
return $service->updateBudget($journal, $budgetId); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Update category for a journal. |
735
|
|
|
* |
736
|
|
|
* @param TransactionJournal $journal |
737
|
|
|
* @param string $category |
738
|
|
|
* |
739
|
|
|
* @return TransactionJournal |
740
|
|
|
*/ |
741
|
|
|
public function updateCategory(TransactionJournal $journal, string $category): TransactionJournal |
742
|
|
|
{ |
743
|
|
|
/** @var JournalUpdateService $service */ |
744
|
|
|
$service = app(JournalUpdateService::class); |
745
|
|
|
|
746
|
|
|
return $service->updateCategory($journal, $category); |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Update tag(s) for a journal. |
751
|
|
|
* |
752
|
|
|
* @param TransactionJournal $journal |
753
|
|
|
* @param array $tags |
754
|
|
|
* |
755
|
|
|
* @return TransactionJournal |
756
|
|
|
*/ |
757
|
|
|
public function updateTags(TransactionJournal $journal, array $tags): TransactionJournal |
758
|
|
|
{ |
759
|
|
|
/** @var JournalUpdateService $service */ |
760
|
|
|
$service = app(JournalUpdateService::class); |
761
|
|
|
$service->connectTags($journal, $tags); |
762
|
|
|
|
763
|
|
|
return $journal; |
764
|
|
|
|
765
|
|
|
} |
766
|
|
|
} |
767
|
|
|
|