2015-02-09 00:23:39 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* JournalRepository.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://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;
|
2018-03-10 13:25:11 -06:00
|
|
|
use Exception;
|
2018-02-22 13:07:14 -06:00
|
|
|
use FireflyIII\Factory\TransactionJournalFactory;
|
2018-03-19 13:39:02 -05:00
|
|
|
use FireflyIII\Factory\TransactionJournalMetaFactory;
|
2015-02-24 15:53:38 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2017-11-24 14:51:07 -06:00
|
|
|
use FireflyIII\Models\AccountType;
|
2017-12-23 10:42:07 -06:00
|
|
|
use FireflyIII\Models\Note;
|
2018-03-11 10:24:07 -05:00
|
|
|
use FireflyIII\Models\PiggyBankEvent;
|
2017-11-05 12:48:43 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
2015-02-24 15:53:38 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2018-02-22 13:07:14 -06:00
|
|
|
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
|
|
|
|
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;
|
2016-10-30 00:14:07 -05:00
|
|
|
use Illuminate\Support\MessageBag;
|
2015-04-03 15:54:21 -05:00
|
|
|
use Log;
|
2016-10-30 00:14:07 -05:00
|
|
|
use Preferences;
|
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
|
|
|
|
{
|
2016-03-03 01:55:43 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2016-09-09 04:19:40 -05:00
|
|
|
|
2016-10-30 00:14:07 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param TransactionType $type
|
|
|
|
* @param Account $source
|
|
|
|
* @param Account $destination
|
|
|
|
*
|
|
|
|
* @return MessageBag
|
|
|
|
*/
|
|
|
|
public function convert(TransactionJournal $journal, TransactionType $type, Account $source, Account $destination): MessageBag
|
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
if ($source->id === $destination->id || null === $source->id || null === $destination->id) {
|
2018-04-24 12:48:42 -05:00
|
|
|
// default message bag that shows errors for everything.
|
|
|
|
$messages = new MessageBag;
|
|
|
|
$messages->add('source_account_revenue', trans('firefly.invalid_convert_selection'));
|
|
|
|
$messages->add('destination_account_asset', trans('firefly.invalid_convert_selection'));
|
|
|
|
$messages->add('destination_account_expense', trans('firefly.invalid_convert_selection'));
|
|
|
|
$messages->add('source_account_asset', trans('firefly.invalid_convert_selection'));
|
|
|
|
|
2016-10-30 00:14:07 -05:00
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
2018-04-24 12:48:42 -05:00
|
|
|
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
|
|
|
|
$destinationTransaction = $journal->transactions()->where('amount', '>', 0)->first();
|
|
|
|
if (null === $sourceTransaction || null === $destinationTransaction) {
|
|
|
|
// default message bag that shows errors for everything.
|
|
|
|
|
|
|
|
$messages = new MessageBag;
|
|
|
|
$messages->add('source_account_revenue', trans('firefly.source_or_dest_invalid'));
|
|
|
|
$messages->add('destination_account_asset', trans('firefly.source_or_dest_invalid'));
|
|
|
|
$messages->add('destination_account_expense', trans('firefly.source_or_dest_invalid'));
|
|
|
|
$messages->add('source_account_asset', trans('firefly.source_or_dest_invalid'));
|
|
|
|
|
|
|
|
return $messages;
|
|
|
|
}
|
2016-10-30 00:14:07 -05:00
|
|
|
$sourceTransaction->account_id = $source->id;
|
|
|
|
$sourceTransaction->save();
|
|
|
|
$destinationTransaction->account_id = $destination->id;
|
|
|
|
$destinationTransaction->save();
|
|
|
|
$journal->transaction_type_id = $type->id;
|
|
|
|
$journal->save();
|
2016-12-18 03:37:59 -06:00
|
|
|
|
|
|
|
// if journal is a transfer now, remove budget:
|
2017-11-15 05:25:49 -06:00
|
|
|
if (TransactionType::TRANSFER === $type->type) {
|
2016-12-18 03:37:59 -06:00
|
|
|
$journal->budgets()->detach();
|
2018-04-24 12:48:42 -05:00
|
|
|
// also from transactions:
|
|
|
|
foreach ($journal->transactions as $transaction) {
|
|
|
|
$transaction->budgets()->detach();
|
|
|
|
}
|
2016-12-18 03:37:59 -06:00
|
|
|
}
|
|
|
|
|
2016-10-30 00:14:07 -05:00
|
|
|
Preferences::mark();
|
|
|
|
|
2016-12-18 03:37:59 -06:00
|
|
|
return new MessageBag;
|
2016-10-30 00:14:07 -05:00
|
|
|
}
|
|
|
|
|
2017-07-04 09:31:16 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function countTransactions(TransactionJournal $journal): int
|
|
|
|
{
|
|
|
|
return $journal->transactions()->count();
|
|
|
|
}
|
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return bool
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2018-04-02 08:10:40 -05:00
|
|
|
|
2015-05-04 16:46:14 -05:00
|
|
|
*/
|
2018-02-18 03:31:15 -06:00
|
|
|
public function destroy(TransactionJournal $journal): bool
|
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
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-23 02:33:54 -05:00
|
|
|
/**
|
|
|
|
* @param int $journalId
|
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
2016-12-04 11:02:19 -06:00
|
|
|
public function find(int $journalId): TransactionJournal
|
2016-04-23 02:33:54 -05:00
|
|
|
{
|
2018-02-22 13:13:00 -06:00
|
|
|
/** @var TransactionJournal $journal */
|
2016-08-26 02:30:52 -05:00
|
|
|
$journal = $this->user->transactionJournals()->where('id', $journalId)->first();
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $journal) {
|
2016-04-23 02:33:54 -05:00
|
|
|
return new TransactionJournal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $journal;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-11-05 12:48:43 -06:00
|
|
|
/**
|
|
|
|
* @param Transaction $transaction
|
|
|
|
*
|
|
|
|
* @return Transaction|null
|
|
|
|
*/
|
|
|
|
public function findOpposingTransaction(Transaction $transaction): ?Transaction
|
|
|
|
{
|
|
|
|
$opposing = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
|
|
->where('transaction_journals.user_id', $this->user->id)
|
|
|
|
->where('transactions.transaction_journal_id', $transaction->transaction_journal_id)
|
|
|
|
->where('transactions.identifier', $transaction->identifier)
|
|
|
|
->where('amount', bcmul($transaction->amount, '-1'))
|
|
|
|
->first(['transactions.*']);
|
|
|
|
|
|
|
|
return $opposing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $transactionid
|
|
|
|
*
|
|
|
|
* @return Transaction|null
|
|
|
|
*/
|
|
|
|
public function findTransaction(int $transactionid): ?Transaction
|
|
|
|
{
|
|
|
|
$transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
|
|
->where('transaction_journals.user_id', $this->user->id)
|
|
|
|
->where('transactions.id', $transactionid)
|
|
|
|
->first(['transactions.*']);
|
|
|
|
|
|
|
|
return $transaction;
|
|
|
|
}
|
|
|
|
|
2015-04-03 15:54:21 -05:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Get users first transaction journal.
|
2015-04-03 15:54:21 -05:00
|
|
|
*
|
2018-04-02 08:10:40 -05:00
|
|
|
* @deprecated
|
2015-04-03 15:54:21 -05:00
|
|
|
* @return TransactionJournal
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function first(): TransactionJournal
|
2015-04-03 15:54:21 -05:00
|
|
|
{
|
2018-02-22 13:13:00 -06:00
|
|
|
/** @var TransactionJournal $entry */
|
2016-08-26 02:30:52 -05:00
|
|
|
$entry = $this->user->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
|
2016-05-05 14:25:20 -05:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $entry) {
|
2016-04-10 14:31:00 -05:00
|
|
|
return new TransactionJournal;
|
|
|
|
}
|
2015-12-25 10:11:55 -06:00
|
|
|
|
|
|
|
return $entry;
|
2015-04-03 15:54:21 -05:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-24 14:51:07 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return Transaction|null
|
|
|
|
*/
|
|
|
|
public function getAssetTransaction(TransactionJournal $journal): ?Transaction
|
|
|
|
{
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($journal->transactions as $transaction) {
|
2017-12-22 11:32:43 -06:00
|
|
|
if (AccountType::ASSET === $transaction->account->accountType->type) {
|
2017-11-24 14:51:07 -06:00
|
|
|
return $transaction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
/**
|
|
|
|
* Returns the first positive transaction for the journal. Useful when editing journals.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return Transaction
|
|
|
|
*/
|
|
|
|
public function getFirstPosTransaction(TransactionJournal $journal): Transaction
|
|
|
|
{
|
2018-04-02 08:10:40 -05:00
|
|
|
return $journal->transactions()->where('amount', '>', 0)->first();
|
2018-02-24 07:31:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the ID of the budget linked to the journal (if any) or the transactions (if any).
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getJournalBudgetId(TransactionJournal $journal): int
|
|
|
|
{
|
|
|
|
$budget = $journal->budgets()->first();
|
|
|
|
if (null !== $budget) {
|
|
|
|
return $budget->id;
|
|
|
|
}
|
|
|
|
$budget = $journal->transactions()->first()->budgets()->first();
|
|
|
|
if (null !== $budget) {
|
|
|
|
return $budget->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the name of the category linked to the journal (if any) or to the transactions (if any).
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getJournalCategoryName(TransactionJournal $journal): string
|
|
|
|
{
|
|
|
|
$category = $journal->categories()->first();
|
|
|
|
if (null !== $category) {
|
|
|
|
return $category->name;
|
|
|
|
}
|
|
|
|
$category = $journal->transactions()->first()->categories()->first();
|
|
|
|
if (null !== $category) {
|
|
|
|
return $category->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return requested date as string. When it's a NULL return the date of journal,
|
|
|
|
* otherwise look for meta field and return that one.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param null|string $field
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getJournalDate(TransactionJournal $journal, ?string $field): string
|
|
|
|
{
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null === $field) {
|
2018-02-24 07:31:20 -06:00
|
|
|
return $journal->date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
if (null !== $journal->$field && $journal->$field instanceof Carbon) {
|
|
|
|
// make field NULL
|
|
|
|
$carbon = clone $journal->$field;
|
|
|
|
$journal->$field = null;
|
|
|
|
$journal->save();
|
|
|
|
|
|
|
|
// create meta entry
|
|
|
|
$journal->setMeta($field, $carbon);
|
|
|
|
|
|
|
|
// return that one instead.
|
|
|
|
return $carbon->format('Y-m-d');
|
|
|
|
}
|
|
|
|
$metaField = $journal->getMeta($field);
|
|
|
|
if (null !== $metaField) {
|
|
|
|
$carbon = new Carbon($metaField);
|
|
|
|
|
|
|
|
return $carbon->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of all destination accounts related to journal.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getJournalDestinationAccounts(TransactionJournal $journal): Collection
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('destination-account-list');
|
|
|
|
if ($cache->has()) {
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getJournalSourceAccounts(TransactionJournal $journal): Collection
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('source-account-list');
|
|
|
|
if ($cache->has()) {
|
|
|
|
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-03-10 15:34:02 -06:00
|
|
|
/**
|
|
|
|
* Return Carbon value of a meta field (or NULL).
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return null|Carbon
|
|
|
|
*/
|
|
|
|
public function getMetaDate(TransactionJournal $journal, string $field): ?Carbon
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('journal-meta-updated');
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty($field);
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
2018-03-19 13:39:02 -05:00
|
|
|
return new Carbon($cache->get()); // @codeCoverageIgnore
|
2018-03-10 15:34:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$entry = $journal->transactionJournalMeta()->where('name', $field)->first();
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null === $entry) {
|
2018-03-10 15:34:02 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$value = new Carbon($entry->data);
|
2018-03-19 13:39:02 -05:00
|
|
|
$cache->store($entry->data);
|
2018-03-10 15:34:02 -06:00
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
/**
|
|
|
|
* Return value of a meta field (or NULL) as a string.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getMetaField(TransactionJournal $journal, string $field): ?string
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
2018-03-10 15:34:02 -06:00
|
|
|
$cache->addProperty('journal-meta-updated');
|
2018-02-24 07:31:20 -06:00
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty($field);
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry = $journal->transactionJournalMeta()->where('name', $field)->first();
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null === $entry) {
|
2018-02-24 07:31:20 -06:00
|
|
|
return null;
|
|
|
|
}
|
2018-03-10 15:34:02 -06:00
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
$value = $entry->data;
|
|
|
|
|
2018-03-10 15:34:02 -06:00
|
|
|
// return when array:
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\is_array($value)) {
|
2018-04-02 08:10:40 -05:00
|
|
|
$return = implode(',', $value);
|
2018-03-10 15:34:02 -06:00
|
|
|
$cache->store($return);
|
|
|
|
|
|
|
|
return $return;
|
2018-03-10 13:25:11 -06:00
|
|
|
}
|
2018-03-10 15:34:02 -06:00
|
|
|
|
|
|
|
// return when something else:
|
2018-03-10 13:25:11 -06:00
|
|
|
try {
|
2018-04-02 08:10:40 -05:00
|
|
|
$return = (string)$value;
|
2018-03-10 15:34:02 -06:00
|
|
|
$cache->store($return);
|
2018-03-10 13:25:11 -06:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2018-02-24 07:31:20 -06:00
|
|
|
}
|
|
|
|
|
2017-12-23 10:42:07 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return Note|null
|
|
|
|
*/
|
|
|
|
public function getNote(TransactionJournal $journal): ?Note
|
|
|
|
{
|
|
|
|
return $journal->notes()->first();
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
/**
|
2018-03-25 02:01:43 -05:00
|
|
|
* Return text of a note attached to journal, or NULL
|
2018-02-24 07:31:20 -06:00
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
2018-03-25 02:01:43 -05:00
|
|
|
* @return string|null
|
2018-02-24 07:31:20 -06:00
|
|
|
*/
|
2018-03-25 02:01:43 -05:00
|
|
|
public function getNoteText(TransactionJournal $journal): ?string
|
2018-02-24 07:31:20 -06:00
|
|
|
{
|
|
|
|
$note = $this->getNote($journal);
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null === $note) {
|
2018-03-25 02:01:43 -05:00
|
|
|
return null;
|
2018-02-24 07:31:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $note->text;
|
|
|
|
}
|
|
|
|
|
2018-03-11 10:24:07 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getPiggyBankEvents(TransactionJournal $journal): Collection
|
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
|
|
|
$events = $journal->piggyBankEvents()->get();
|
|
|
|
$events->each(
|
|
|
|
function (PiggyBankEvent $event) {
|
|
|
|
$event->piggyBank = $event->piggyBank()->withTrashed()->first();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $events;
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:31:20 -06:00
|
|
|
/**
|
|
|
|
* Return all tags as strings in an array.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTags(TransactionJournal $journal): array
|
|
|
|
{
|
|
|
|
return $journal->tags()->get()->pluck('tag')->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the transaction type of the journal.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTransactionType(TransactionJournal $journal): string
|
|
|
|
{
|
|
|
|
return $journal->transactionType->type;
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:50:22 -06:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getTransactionTypes(): Collection
|
|
|
|
{
|
|
|
|
return TransactionType::orderBy('type', 'ASC')->get();
|
|
|
|
}
|
|
|
|
|
2017-11-22 09:54:49 -06:00
|
|
|
/**
|
|
|
|
* @param array $transactionIds
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getTransactionsById(array $transactionIds): Collection
|
|
|
|
{
|
|
|
|
$set = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
|
|
->whereIn('transactions.id', $transactionIds)
|
|
|
|
->where('transaction_journals.user_id', $this->user->id)
|
|
|
|
->whereNull('transaction_journals.deleted_at')
|
|
|
|
->whereNull('transactions.deleted_at')
|
2017-11-22 14:12:27 -06:00
|
|
|
->get(['transactions.*']);
|
2017-11-22 09:54:49 -06:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2018-02-25 12:09:05 -06:00
|
|
|
/**
|
|
|
|
* Will tell you if journal is reconciled or not.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isJournalReconciled(TransactionJournal $journal): bool
|
|
|
|
{
|
|
|
|
foreach ($journal->transactions as $transaction) {
|
|
|
|
if ($transaction->reconciled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-05 12:48:43 -06:00
|
|
|
/**
|
|
|
|
* @param Transaction $transaction
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function reconcile(Transaction $transaction): bool
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('Going to reconcile transaction #%d', $transaction->id));
|
|
|
|
$opposing = $this->findOpposingTransaction($transaction);
|
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $opposing) {
|
2017-11-05 12:48:43 -06:00
|
|
|
Log::debug('Opposing transaction is NULL. Cannot reconcile.');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Log::debug(sprintf('Opposing transaction ID is #%d', $opposing->id));
|
|
|
|
|
|
|
|
$transaction->reconciled = true;
|
|
|
|
$opposing->reconciled = true;
|
|
|
|
$transaction->save();
|
|
|
|
$opposing->save();
|
|
|
|
|
|
|
|
return true;
|
2018-02-28 14:32:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $transactionId
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function reconcileById(int $transactionId): bool
|
|
|
|
{
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
$transaction = $this->user->transactions()->find($transactionId);
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null !== $transaction) {
|
2018-02-28 14:32:59 -06:00
|
|
|
return $this->reconcile($transaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-11-05 12:48:43 -06:00
|
|
|
}
|
|
|
|
|
2018-03-19 13:39:02 -05:00
|
|
|
/**
|
|
|
|
* Set meta field for journal that contains a date.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param string $name
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setMetaDate(TransactionJournal $journal, string $name, Carbon $date): void
|
|
|
|
{
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$factory->updateOrCreate(
|
|
|
|
[
|
|
|
|
'data' => $date,
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => $name,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set meta field for journal that contains string.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
public function setMetaString(TransactionJournal $journal, string $name, string $value): void
|
|
|
|
{
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$factory->updateOrCreate(
|
|
|
|
[
|
|
|
|
'data' => $value,
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => $name,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-24 09:01:53 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param int $order
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setOrder(TransactionJournal $journal, int $order): bool
|
|
|
|
{
|
|
|
|
$journal->order = $order;
|
|
|
|
$journal->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-16 23:42:36 -06:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return TransactionJournal
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException
|
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException
|
2015-02-24 15:53:38 -06:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function store(array $data): TransactionJournal
|
2015-02-24 15:53:38 -06:00
|
|
|
{
|
2018-02-22 13:07:14 -06:00
|
|
|
/** @var TransactionJournalFactory $factory */
|
|
|
|
$factory = app(TransactionJournalFactory::class);
|
|
|
|
$factory->setUser($this->user);
|
2017-10-03 03:30:56 -05:00
|
|
|
|
2018-02-22 13:07:14 -06:00
|
|
|
return $factory->create($data);
|
2018-02-16 09:45:03 -06:00
|
|
|
}
|
|
|
|
|
2015-04-28 03:36:13 -05:00
|
|
|
/**
|
2017-06-07 01:18:42 -05:00
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $data
|
2016-05-01 02:42:08 -05:00
|
|
|
*
|
2017-06-07 01:18:42 -05:00
|
|
|
* @return TransactionJournal
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-06-07 01:18:42 -05:00
|
|
|
*/
|
|
|
|
public function update(TransactionJournal $journal, array $data): TransactionJournal
|
|
|
|
{
|
2018-02-22 13:07:14 -06:00
|
|
|
/** @var JournalUpdateService $service */
|
|
|
|
$service = app(JournalUpdateService::class);
|
2018-03-02 09:31:02 -06:00
|
|
|
$journal = $service->update($journal, $data);
|
2018-02-23 08:12:47 -06:00
|
|
|
|
|
|
|
return $journal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-23 09:21:28 -06:00
|
|
|
* Update budget for a journal.
|
2018-02-23 08:12:47 -06:00
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
2018-02-23 09:21:28 -06: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);
|
|
|
|
|
|
|
|
return $service->updateBudget($journal, $budgetId);
|
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
|
2018-02-23 09:21:28 -06: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);
|
|
|
|
|
|
|
|
return $service->updateCategory($journal, $category);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update tag(s) for a journal.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $tags
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
$service->connectTags($journal, $tags);
|
|
|
|
|
|
|
|
return $journal;
|
|
|
|
|
2018-02-23 08:12:47 -06:00
|
|
|
}
|
2015-04-03 02:30:44 -05:00
|
|
|
}
|