2015-02-09 00:23:39 -06:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* JournalRepository.php
|
2020-02-16 07:00:57 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-03-24 09:01:53 -05:00
|
|
|
declare(strict_types=1);
|
2015-02-09 00:23:39 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Journal;
|
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
use Carbon\Carbon;
|
2020-02-28 11:56:27 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2019-12-20 14:01:27 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2018-12-20 15:03:34 -06:00
|
|
|
use FireflyIII\Models\Note;
|
2017-11-05 12:48:43 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
2019-03-30 01:09:52 -05:00
|
|
|
use FireflyIII\Models\TransactionGroup;
|
2015-02-24 15:53:38 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-12-20 15:03:34 -06:00
|
|
|
use FireflyIII\Models\TransactionJournalLink;
|
2018-05-05 06:53:12 -05:00
|
|
|
use FireflyIII\Models\TransactionJournalMeta;
|
2018-02-22 13:07:14 -06:00
|
|
|
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
|
2019-03-30 01:09:52 -05:00
|
|
|
use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService;
|
2018-02-22 13:07:14 -06:00
|
|
|
use FireflyIII\Services\Internal\Update\JournalUpdateService;
|
2018-02-24 07:31:20 -06:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2016-03-03 01:55:43 -06:00
|
|
|
use FireflyIII\User;
|
2023-02-19 01:43:28 -06:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2016-10-21 14:41:31 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-24 15:53:38 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class JournalRepository.
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
|
|
|
class JournalRepository implements JournalRepositoryInterface
|
|
|
|
{
|
2023-10-22 11:44:30 -05:00
|
|
|
private User $user;
|
2018-09-03 01:41:03 -05:00
|
|
|
|
2019-03-30 01:09:52 -05:00
|
|
|
public function destroyGroup(TransactionGroup $transactionGroup): void
|
2017-07-04 09:31:16 -05:00
|
|
|
{
|
2019-03-30 01:09:52 -05:00
|
|
|
/** @var TransactionGroupDestroyService $service */
|
|
|
|
$service = app(TransactionGroupDestroyService::class);
|
|
|
|
$service->destroy($transactionGroup);
|
2017-07-04 09:31:16 -05:00
|
|
|
}
|
|
|
|
|
2019-03-30 01:09:52 -05:00
|
|
|
public function destroyJournal(TransactionJournal $journal): void
|
2015-05-04 16:46:14 -05:00
|
|
|
{
|
2018-02-22 13:07:14 -06:00
|
|
|
/** @var JournalDestroyService $service */
|
|
|
|
$service = app(JournalDestroyService::class);
|
|
|
|
$service->destroy($journal);
|
2015-05-04 16:46:14 -05:00
|
|
|
}
|
|
|
|
|
2021-09-18 03:26:12 -05:00
|
|
|
public function findByType(array $types): Collection
|
|
|
|
{
|
|
|
|
return $this->user
|
|
|
|
->transactionJournals()
|
|
|
|
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
|
|
|
->whereIn('transaction_types.type', $types)
|
2023-12-20 12:35:52 -06:00
|
|
|
->get(['transaction_journals.*'])
|
|
|
|
;
|
2021-09-18 03:26:12 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 08:10:40 -05:00
|
|
|
/**
|
|
|
|
* Get users first transaction journal or NULL.
|
|
|
|
*/
|
|
|
|
public function firstNull(): ?TransactionJournal
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|TransactionJournal $entry */
|
2018-04-02 08:10:40 -05:00
|
|
|
$entry = $this->user->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
|
|
|
|
$result = null;
|
|
|
|
if (null !== $entry) {
|
|
|
|
$result = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
public function getDestinationAccount(TransactionJournal $journal): Account
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|Transaction $transaction */
|
2021-03-11 23:20:01 -06:00
|
|
|
$transaction = $journal->transactions()->with('account')->where('amount', '>', 0)->first();
|
|
|
|
if (null === $transaction) {
|
|
|
|
throw new FireflyException(sprintf('Your administration is broken. Transaction journal #%d has no destination transaction.', $journal->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $transaction->account;
|
|
|
|
}
|
|
|
|
|
2018-02-25 12:09:05 -06:00
|
|
|
/**
|
|
|
|
* Return total amount of journal. Is always positive.
|
|
|
|
*/
|
|
|
|
public function getJournalTotal(TransactionJournal $journal): string
|
|
|
|
{
|
2024-01-01 07:43:56 -06:00
|
|
|
$cache = new CacheProperties();
|
2018-02-25 12:09:05 -06:00
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('amount-positive');
|
|
|
|
if ($cache->has()) {
|
2021-09-18 03:26:12 -05:00
|
|
|
return $cache->get();
|
2018-02-25 12:09:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// saves on queries:
|
|
|
|
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
|
2022-12-29 12:42:26 -06:00
|
|
|
$amount = (string)$amount;
|
2018-02-25 12:09:05 -06:00
|
|
|
$cache->store($amount);
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
public function getLast(): ?TransactionJournal
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|TransactionJournal $entry */
|
2021-03-11 23:20:01 -06:00
|
|
|
$entry = $this->user->transactionJournals()->orderBy('date', 'DESC')->first(['transaction_journals.*']);
|
|
|
|
$result = null;
|
|
|
|
if (null !== $entry) {
|
|
|
|
$result = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-12-20 15:03:34 -06:00
|
|
|
public function getLinkNoteText(TransactionJournalLink $link): string
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|Note $note */
|
2018-12-20 15:03:34 -06:00
|
|
|
$note = $link->notes()->first();
|
2023-12-20 12:35:52 -06:00
|
|
|
|
2023-11-04 00:52:40 -05:00
|
|
|
return (string)$note?->text;
|
2018-12-20 15:03:34 -06:00
|
|
|
}
|
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
/**
|
|
|
|
* Return Carbon value of a meta field (or NULL).
|
|
|
|
*/
|
|
|
|
public function getMetaDateById(int $journalId, string $field): ?Carbon
|
|
|
|
{
|
2022-10-30 08:24:28 -05:00
|
|
|
$cache = new CacheProperties();
|
2021-03-11 23:20:01 -06:00
|
|
|
$cache->addProperty('journal-meta-updated');
|
|
|
|
$cache->addProperty($journalId);
|
|
|
|
$cache->addProperty($field);
|
2018-03-10 15:34:02 -06:00
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
if ($cache->has()) {
|
2021-09-18 03:26:12 -05:00
|
|
|
return new Carbon($cache->get());
|
2021-03-11 23:20:01 -06:00
|
|
|
}
|
|
|
|
$entry = TransactionJournalMeta::where('transaction_journal_id', $journalId)
|
2023-12-20 12:35:52 -06:00
|
|
|
->where('name', $field)->first()
|
|
|
|
;
|
2021-03-11 23:20:01 -06:00
|
|
|
if (null === $entry) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$value = new Carbon($entry->data);
|
|
|
|
$cache->store($entry->data);
|
2018-02-24 07:31:20 -06:00
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
return $value;
|
|
|
|
}
|
2018-02-24 07:31:20 -06:00
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
public function getSourceAccount(TransactionJournal $journal): Account
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|Transaction $transaction */
|
2021-03-11 23:20:01 -06:00
|
|
|
$transaction = $journal->transactions()->with('account')->where('amount', '<', 0)->first();
|
|
|
|
if (null === $transaction) {
|
|
|
|
throw new FireflyException(sprintf('Your administration is broken. Transaction journal #%d has no source transaction.', $journal->id));
|
|
|
|
}
|
2018-03-11 10:24:07 -05:00
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
return $transaction->account;
|
|
|
|
}
|
2018-02-24 07:31:20 -06:00
|
|
|
|
2019-06-22 22:53:01 -05:00
|
|
|
public function reconcileById(int $journalId): void
|
2018-02-25 12:09:05 -06:00
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|TransactionJournal $journal */
|
2019-06-22 22:53:01 -05:00
|
|
|
$journal = $this->user->transactionJournals()->find($journalId);
|
2021-09-18 03:20:19 -05:00
|
|
|
$journal?->transactions()->update(['reconciled' => true]);
|
2018-02-25 12:09:05 -06:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
/**
|
|
|
|
* Find a specific journal.
|
|
|
|
*/
|
|
|
|
public function find(int $journalId): ?TransactionJournal
|
|
|
|
{
|
|
|
|
return $this->user->transactionJournals()->find($journalId);
|
|
|
|
}
|
|
|
|
|
2021-03-11 23:20:01 -06:00
|
|
|
/**
|
|
|
|
* Search in journal descriptions.
|
|
|
|
*/
|
|
|
|
public function searchJournalDescriptions(string $search, int $limit): Collection
|
|
|
|
{
|
|
|
|
$query = $this->user->transactionJournals()
|
2023-12-20 12:35:52 -06:00
|
|
|
->orderBy('date', 'DESC')
|
|
|
|
;
|
2023-10-08 09:11:04 -05:00
|
|
|
if ('' !== $search) {
|
2021-03-11 23:20:01 -06:00
|
|
|
$query->where('description', 'LIKE', sprintf('%%%s%%', $search));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->take($limit)->get();
|
|
|
|
}
|
|
|
|
|
2023-12-20 12:35:52 -06:00
|
|
|
public function setUser(null|Authenticatable|User $user): void
|
2017-02-16 23:42:36 -06:00
|
|
|
{
|
2023-10-30 13:49:40 -05:00
|
|
|
if ($user instanceof User) {
|
2023-02-19 01:43:28 -06:00
|
|
|
$this->user = $user;
|
|
|
|
}
|
2017-02-16 23:42:36 -06:00
|
|
|
}
|
|
|
|
|
2023-10-28 08:03:33 -05:00
|
|
|
public function unreconcileById(int $journalId): void
|
|
|
|
{
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|TransactionJournal $journal */
|
2023-10-28 08:03:33 -05:00
|
|
|
$journal = $this->user->transactionJournals()->find($journalId);
|
|
|
|
$journal?->transactions()->update(['reconciled' => false]);
|
|
|
|
}
|
|
|
|
|
2018-02-23 08:12:47 -06:00
|
|
|
/**
|
2018-02-23 09:21:28 -06:00
|
|
|
* Update budget for a journal.
|
2018-02-23 08:12:47 -06:00
|
|
|
*/
|
2018-02-23 09:21:28 -06:00
|
|
|
public function updateBudget(TransactionJournal $journal, int $budgetId): TransactionJournal
|
2018-02-23 08:12:47 -06:00
|
|
|
{
|
2018-02-23 09:21:28 -06:00
|
|
|
/** @var JournalUpdateService $service */
|
|
|
|
$service = app(JournalUpdateService::class);
|
|
|
|
|
2019-07-04 10:31:47 -05:00
|
|
|
$service->setTransactionJournal($journal);
|
|
|
|
$service->setData(
|
|
|
|
[
|
|
|
|
'budget_id' => $budgetId,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$service->update();
|
|
|
|
$journal->refresh();
|
|
|
|
|
|
|
|
return $journal;
|
2017-06-07 01:18:42 -05:00
|
|
|
}
|
|
|
|
|
2018-02-23 08:12:47 -06:00
|
|
|
/**
|
2018-02-23 09:21:28 -06:00
|
|
|
* Update category for a journal.
|
2018-02-23 08:12:47 -06:00
|
|
|
*/
|
2018-02-23 09:21:28 -06:00
|
|
|
public function updateCategory(TransactionJournal $journal, string $category): TransactionJournal
|
|
|
|
{
|
|
|
|
/** @var JournalUpdateService $service */
|
|
|
|
$service = app(JournalUpdateService::class);
|
2019-07-04 10:31:47 -05:00
|
|
|
$service->setTransactionJournal($journal);
|
|
|
|
$service->setData(
|
|
|
|
[
|
|
|
|
'category_name' => $category,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$service->update();
|
|
|
|
$journal->refresh();
|
2018-02-23 09:21:28 -06:00
|
|
|
|
2019-07-04 10:31:47 -05:00
|
|
|
return $journal;
|
2018-02-23 09:21:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update tag(s) for a journal.
|
|
|
|
*/
|
|
|
|
public function updateTags(TransactionJournal $journal, array $tags): TransactionJournal
|
2018-02-23 08:12:47 -06:00
|
|
|
{
|
2018-02-23 09:21:28 -06:00
|
|
|
/** @var JournalUpdateService $service */
|
|
|
|
$service = app(JournalUpdateService::class);
|
2019-07-04 10:31:47 -05:00
|
|
|
$service->setTransactionJournal($journal);
|
|
|
|
$service->setData(
|
|
|
|
[
|
|
|
|
'tags' => $tags,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$service->update();
|
|
|
|
$journal->refresh();
|
2018-02-23 09:21:28 -06:00
|
|
|
|
|
|
|
return $journal;
|
2018-02-23 08:12:47 -06:00
|
|
|
}
|
2015-04-03 02:30:44 -05:00
|
|
|
}
|