2015-02-09 00:23:39 -06:00
|
|
|
<?php
|
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;
|
2016-10-21 14:41:31 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2015-04-03 15:54:21 -05:00
|
|
|
use Log;
|
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
|
|
|
|
{
|
2019-07-01 13:22:35 -05:00
|
|
|
|
|
|
|
|
2016-03-03 01:55:43 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2018-09-03 01:41:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 11:20:15 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-09-03 01:41:03 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-09 04:19:40 -05:00
|
|
|
|
2019-07-01 13:22:35 -05:00
|
|
|
/**
|
|
|
|
* Search in journal descriptions.
|
|
|
|
*
|
|
|
|
* @param string $search
|
2020-07-22 23:58:00 -05:00
|
|
|
* @param int $limit
|
2019-07-01 13:22:35 -05:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2020-07-22 23:58:00 -05:00
|
|
|
public function searchJournalDescriptions(string $search, int $limit): Collection
|
2019-07-01 13:22:35 -05:00
|
|
|
{
|
|
|
|
$query = $this->user->transactionJournals()
|
|
|
|
->orderBy('date', 'DESC');
|
|
|
|
if ('' !== $query) {
|
|
|
|
$query->where('description', 'LIKE', sprintf('%%%s%%', $search));
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:58:00 -05:00
|
|
|
return $query->take($limit)->get();
|
2019-07-01 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
2017-07-04 09:31:16 -05:00
|
|
|
/**
|
2019-03-30 01:09:52 -05:00
|
|
|
* @param TransactionGroup $transactionGroup
|
2017-07-04 09:31:16 -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
|
|
|
}
|
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2018-04-24 12:26:16 -05:00
|
|
|
/**
|
|
|
|
* Find a specific journal.
|
|
|
|
*
|
|
|
|
* @param int $journalId
|
|
|
|
*
|
|
|
|
* @return TransactionJournal|null
|
|
|
|
*/
|
|
|
|
public function findNull(int $journalId): ?TransactionJournal
|
|
|
|
{
|
|
|
|
return $this->user->transactionJournals()->where('id', $journalId)->first();
|
|
|
|
}
|
|
|
|
|
2018-04-02 08:10:40 -05:00
|
|
|
/**
|
|
|
|
* Get users first transaction journal or NULL.
|
|
|
|
*
|
|
|
|
* @return TransactionJournal|null
|
|
|
|
*/
|
|
|
|
public function firstNull(): ?TransactionJournal
|
|
|
|
{
|
|
|
|
/** @var TransactionJournal $entry */
|
|
|
|
$entry = $this->user->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
|
|
|
|
$result = null;
|
|
|
|
if (null !== $entry) {
|
|
|
|
$result = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
/**
|
|
|
|
* Return a list of all destination accounts related to journal.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
2019-05-31 06:35:33 -05:00
|
|
|
* @param bool $useCache
|
2018-02-24 07:31:20 -06:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-02-16 04:43:05 -06:00
|
|
|
public function getJournalDestinationAccounts(TransactionJournal $journal, bool $useCache = true): Collection
|
2018-02-24 07:31:20 -06:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('destination-account-list');
|
2019-02-16 04:43:05 -06:00
|
|
|
if ($useCache && $cache->has()) {
|
2018-02-24 07:31:20 -06:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
$transactions = $journal->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get();
|
|
|
|
$list = new Collection;
|
|
|
|
/** @var Transaction $t */
|
|
|
|
foreach ($transactions as $t) {
|
|
|
|
$list->push($t->account);
|
|
|
|
}
|
|
|
|
$list = $list->unique('id');
|
|
|
|
$cache->store($list);
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of all source accounts related to journal.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
2019-05-31 06:35:33 -05:00
|
|
|
* @param bool $useCache
|
2018-02-24 07:31:20 -06:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2019-02-16 04:43:05 -06:00
|
|
|
public function getJournalSourceAccounts(TransactionJournal $journal, bool $useCache = true): Collection
|
2018-02-24 07:31:20 -06:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('source-account-list');
|
2019-02-16 04:43:05 -06:00
|
|
|
if ($useCache && $cache->has()) {
|
2018-02-24 07:31:20 -06:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
$transactions = $journal->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get();
|
|
|
|
$list = new Collection;
|
|
|
|
/** @var Transaction $t */
|
|
|
|
foreach ($transactions as $t) {
|
|
|
|
$list->push($t->account);
|
|
|
|
}
|
|
|
|
$list = $list->unique('id');
|
|
|
|
$cache->store($list);
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2018-02-25 12:09:05 -06:00
|
|
|
/**
|
|
|
|
* Return total amount of journal. Is always positive.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getJournalTotal(TransactionJournal $journal): string
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('amount-positive');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
// saves on queries:
|
|
|
|
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
|
2018-04-02 08:10:40 -05:00
|
|
|
$amount = (string)$amount;
|
2018-02-25 12:09:05 -06:00
|
|
|
$cache->store($amount);
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
2018-12-20 15:03:34 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionJournalLink $link
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLinkNoteText(TransactionJournalLink $link): string
|
|
|
|
{
|
|
|
|
$notes = null;
|
|
|
|
/** @var Note $note */
|
|
|
|
$note = $link->notes()->first();
|
|
|
|
if (null !== $note) {
|
|
|
|
return $note->text ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:34:02 -06:00
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
|
|
|
|
|
2018-03-11 10:24:07 -05:00
|
|
|
|
|
|
|
|
2019-03-23 12:58:06 -05:00
|
|
|
|
2019-03-18 10:52:49 -05:00
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
|
2018-02-25 12:09:05 -06:00
|
|
|
/**
|
2019-05-31 06:35:33 -05:00
|
|
|
* @param int $transactionId
|
2018-02-25 12:09:05 -06:00
|
|
|
*/
|
2019-06-22 22:53:01 -05:00
|
|
|
public function reconcileById(int $journalId): void
|
2018-02-25 12:09:05 -06:00
|
|
|
{
|
2019-06-22 22:53:01 -05:00
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
$journal = $this->user->transactionJournals()->find($journalId);
|
|
|
|
if (null !== $journal) {
|
|
|
|
$journal->transactions()->update(['reconciled' => true]);
|
2018-02-25 12:09:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 23:42:36 -06:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
2018-07-22 11:50:27 -05:00
|
|
|
public function setUser(User $user): void
|
2017-02-16 23:42:36 -06:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
2019-05-31 06:35:33 -05:00
|
|
|
* @param int $budgetId
|
2018-02-23 08:12:47 -06:00
|
|
|
*
|
2018-02-23 09:21:28 -06:00
|
|
|
* @return TransactionJournal
|
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
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
2019-05-31 06:35:33 -05:00
|
|
|
* @param string $category
|
2018-02-23 08:12:47 -06:00
|
|
|
*
|
2018-02-23 09:21:28 -06:00
|
|
|
* @return TransactionJournal
|
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.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
2019-05-31 06:35:33 -05:00
|
|
|
* @param array $tags
|
2018-02-23 09:21:28 -06:00
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
2019-06-10 13:14:00 -05:00
|
|
|
|
2019-08-01 22:44:51 -05:00
|
|
|
/**
|
|
|
|
* Return Carbon value of a meta field (or NULL).
|
|
|
|
*
|
|
|
|
* @param int $journalId
|
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return null|Carbon
|
|
|
|
*/
|
|
|
|
public function getMetaDateById(int $journalId, string $field): ?Carbon
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('journal-meta-updated');
|
|
|
|
$cache->addProperty($journalId);
|
|
|
|
$cache->addProperty($field);
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
return new Carbon($cache->get()); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
$entry = TransactionJournalMeta::where('transaction_journal_id', $journalId)
|
|
|
|
->where('name', $field)->first();
|
|
|
|
if (null === $entry) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$value = new Carbon($entry->data);
|
|
|
|
$cache->store($entry->data);
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2019-12-20 14:01:27 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getSourceAccount(TransactionJournal $journal): Account
|
|
|
|
{
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
$transaction = $journal->transactions()->with('account')->where('amount', '<', 0)->first();
|
2020-02-28 11:56:27 -06:00
|
|
|
if (null === $transaction) {
|
|
|
|
throw new FireflyException(sprintf('Your administration is broken. Transaction journal #%d has no source transaction.', $journal->id));
|
|
|
|
}
|
2019-12-20 14:01:27 -06:00
|
|
|
|
|
|
|
return $transaction->account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getDestinationAccount(TransactionJournal $journal): Account
|
|
|
|
{
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
$transaction = $journal->transactions()->with('account')->where('amount', '>', 0)->first();
|
2020-02-28 11:56:27 -06:00
|
|
|
if (null === $transaction) {
|
|
|
|
throw new FireflyException(sprintf('Your administration is broken. Transaction journal #%d has no destination transaction.', $journal->id));
|
|
|
|
}
|
2019-12-20 14:01:27 -06:00
|
|
|
|
|
|
|
return $transaction->account;
|
|
|
|
}
|
2020-04-30 23:24:24 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return TransactionJournal|null
|
|
|
|
*/
|
|
|
|
public function getLast(): ?TransactionJournal
|
|
|
|
{
|
|
|
|
/** @var TransactionJournal $entry */
|
|
|
|
$entry = $this->user->transactionJournals()->orderBy('date', 'DESC')->first(['transaction_journals.*']);
|
|
|
|
$result = null;
|
|
|
|
if (null !== $entry) {
|
|
|
|
$result = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2020-07-11 08:13:15 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
->get(['transaction_journals.*']);
|
|
|
|
}
|
2015-04-03 02:30:44 -05:00
|
|
|
}
|