firefly-iii/app/Repositories/Journal/JournalRepository.php

792 lines
27 KiB
PHP
Raw Normal View History

2015-02-09 00:23:39 -06:00
<?php
/**
* JournalRepository.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2017-03-24 09:01:53 -05:00
declare(strict_types=1);
2015-02-09 00:23:39 -06:00
namespace FireflyIII\Repositories\Journal;
2015-04-28 03:36:13 -05:00
use DB;
2016-02-17 08:52:46 -06:00
use FireflyIII\Exceptions\FireflyException;
2015-02-24 15:53:38 -06:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
2015-04-28 03:36:13 -05:00
use FireflyIII\Models\Tag;
2015-02-24 15:53:38 -06:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
2016-05-02 13:49:19 -05:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
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
/**
* Class JournalRepository
*
* @package FireflyIII\Repositories\Journal
*/
class JournalRepository implements JournalRepositoryInterface
{
/** @var User */
private $user;
/** @var array */
private $validMetaFields
= ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes', 'original_amount',
'original_currency_id',
];
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
{
// 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'));
if ($source->id === $destination->id || is_null($source->id) || is_null($destination->id)) {
return $messages;
}
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
$destinationTransaction = $journal->transactions()->where('amount', '>', 0)->first();
$sourceTransaction->account_id = $source->id;
$sourceTransaction->save();
$destinationTransaction->account_id = $destination->id;
$destinationTransaction->save();
$journal->transaction_type_id = $type->id;
$journal->save();
// if journal is a transfer now, remove budget:
if ($type->type === TransactionType::TRANSFER) {
$journal->budgets()->detach();
}
2016-10-30 00:14:07 -05:00
Preferences::mark();
return new MessageBag;
2016-10-30 00:14:07 -05:00
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
2016-04-06 02:27:45 -05:00
public function delete(TransactionJournal $journal): bool
{
$journal->delete();
return true;
}
2016-04-23 02:33:54 -05:00
/**
* @param int $journalId
*
* @return TransactionJournal
*/
public function find(int $journalId): TransactionJournal
2016-04-23 02:33:54 -05:00
{
$journal = $this->user->transactionJournals()->where('id', $journalId)->first();
2016-04-23 02:33:54 -05:00
if (is_null($journal)) {
return new TransactionJournal;
}
return $journal;
}
2015-04-03 15:54:21 -05:00
/**
* Get users first transaction journal
*
* @return TransactionJournal
*/
2016-04-06 02:27:45 -05:00
public function first(): TransactionJournal
2015-04-03 15:54:21 -05:00
{
$entry = $this->user->transactionJournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
2016-04-10 14:31:00 -05:00
if (is_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
}
/**
* @return Collection
*/
public function getTransactionTypes(): Collection
{
return TransactionType::orderBy('type', 'ASC')->get();
}
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
*/
2016-04-06 02:27:45 -05:00
public function store(array $data): TransactionJournal
2015-02-24 15:53:38 -06:00
{
// find transaction type.
/** @var TransactionType $transactionType */
2015-02-24 15:53:38 -06:00
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
$accounts = $this->storeAccounts($transactionType, $data);
$currencyId = $data['currency_id'];
$amount = strval($data['amount']);
// switch type to find what account to verify for currency stuff
switch ($transactionType->type) {
case TransactionType::WITHDRAWAL:
/*
* Overrule the currency selection and the amount:
*/
$accountCurrencyId = intval($accounts['source']->getMeta('currency_id'));
if ($accountCurrencyId !== $currencyId) {
$data['original_amount'] = $data['amount'];
$data['original_currency_id'] = $currencyId;
$currencyId = $accountCurrencyId;
2017-04-14 07:48:44 -05:00
$amount = strval($data['exchanged_amount']);
}
break;
case TransactionType::DEPOSIT:
$accountCurrencyId = intval($accounts['destination']->getMeta('currency_id'));
if ($accountCurrencyId !== $currencyId) {
$data['original_amount'] = $data['amount'];
$data['original_currency_id'] = $currencyId;
$currencyId = $accountCurrencyId;
$amount = strval($data['exchanged_amount']);
}
break;
default:
throw new FireflyException(sprintf('Currency exchange routine cannot handle %s', $transactionType->type));
}
$journal = new TransactionJournal(
2015-02-24 15:53:38 -06:00
[
2016-10-23 05:19:32 -05:00
'user_id' => $this->user->id,
'transaction_type_id' => $transactionType->id,
'transaction_currency_id' => $currencyId,
'description' => $data['description'],
'completed' => 0,
'date' => $data['date'],
2015-02-24 15:53:38 -06:00
]
);
$journal->save();
// store stuff:
$this->storeCategoryWithJournal($journal, $data['category']);
$this->storeBudgetWithJournal($journal, $data['budget_id']);
// store two transactions:
$one = [
'journal' => $journal,
'account' => $accounts['source'],
'amount' => bcmul($amount, '-1'),
'description' => null,
'category' => null,
'budget' => null,
'identifier' => 0,
];
$this->storeTransaction($one);
$two = [
'journal' => $journal,
'account' => $accounts['destination'],
'amount' => $amount,
'description' => null,
'category' => null,
'budget' => null,
'identifier' => 0,
];
$this->storeTransaction($two);
2015-02-24 15:53:38 -06:00
2015-04-28 07:08:58 -05:00
// store tags
if (isset($data['tags']) && is_array($data['tags'])) {
$this->saveTags($journal, $data['tags']);
}
foreach ($data as $key => $value) {
if (in_array($key, $this->validMetaFields)) {
$journal->setMeta($key, $value);
continue;
}
Log::debug(sprintf('Could not store meta field "%s" with value "%s" for journal #%d', json_encode($key), json_encode($value), $journal->id));
}
2015-02-24 15:53:38 -06:00
$journal->completed = 1;
$journal->save();
return $journal;
2015-02-24 15:53:38 -06:00
}
2015-02-27 07:27:04 -06:00
/**
* @param TransactionJournal $journal
* @param array $data
*
* @return TransactionJournal
2015-02-27 07:27:04 -06:00
*/
2016-04-06 02:27:45 -05:00
public function update(TransactionJournal $journal, array $data): TransactionJournal
2015-02-27 07:27:04 -06:00
{
// update actual journal:
$journal->description = $data['description'];
$journal->date = $data['date'];
$accounts = $this->storeAccounts($journal->transactionType, $data);
$amount = strval($data['amount']);
if ($data['currency_id'] !== $journal->transaction_currency_id) {
// user has entered amount in foreign currency.
// amount in "our" currency is $data['exchanged_amount']:
$amount = strval($data['exchanged_amount']);
// other values must be stored as well:
$data['original_amount'] = $data['amount'];
$data['original_currency_id'] = $data['currency_id'];
}
2015-02-27 07:27:04 -06:00
// unlink all categories, recreate them:
$journal->categories()->detach();
$journal->budgets()->detach();
$this->storeCategoryWithJournal($journal, $data['category']);
$this->storeBudgetWithJournal($journal, $data['budget_id']);
2015-03-29 05:32:00 -05:00
$this->updateSourceTransaction($journal, $accounts['source'], bcmul($amount, '-1')); // negative because source loses money.
$this->updateDestinationTransaction($journal, $accounts['destination'], $amount); // positive because destination gets money.
2015-03-29 05:32:00 -05:00
$journal->save();
2015-04-28 03:36:13 -05:00
// update tags:
if (isset($data['tags']) && is_array($data['tags'])) {
$this->updateTags($journal, $data['tags']);
}
// update meta fields:
$result = $journal->save();
if ($result) {
foreach ($data as $key => $value) {
if (in_array($key, $this->validMetaFields)) {
$journal->setMeta($key, $value);
continue;
}
Log::debug(sprintf('Could not store meta field "%s" with value "%s" for journal #%d', json_encode($key), json_encode($value), $journal->id));
}
return $journal;
}
2015-03-29 05:32:00 -05:00
return $journal;
}
2016-10-21 14:41:31 -05:00
/**
* Same as above but for transaction journal with multiple transactions.
*
* @param TransactionJournal $journal
* @param array $data
*
* @return TransactionJournal
*/
public function updateSplitJournal(TransactionJournal $journal, array $data): TransactionJournal
{
// update actual journal:
2016-10-22 00:28:31 -05:00
$journal->transaction_currency_id = $data['currency_id'];
2016-10-21 14:41:31 -05:00
$journal->description = $data['journal_description'];
$journal->date = $data['date'];
2016-10-22 00:28:31 -05:00
$journal->save();
2016-10-23 05:37:12 -05:00
Log::debug(sprintf('Updated split journal #%d', $journal->id));
2016-10-22 02:33:03 -05:00
2016-10-21 14:41:31 -05:00
// unlink all categories:
$journal->categories()->detach();
$journal->budgets()->detach();
// update meta fields:
$result = $journal->save();
if ($result) {
foreach ($data as $key => $value) {
if (in_array($key, $this->validMetaFields)) {
$journal->setMeta($key, $value);
continue;
}
Log::debug(sprintf('Could not store meta field "%s" with value "%s" for journal #%d', json_encode($key), json_encode($value), $journal->id));
}
}
// update tags:
if (isset($data['tags']) && is_array($data['tags'])) {
$this->updateTags($journal, $data['tags']);
}
// delete original transactions, and recreate them.
$journal->transactions()->delete();
// store each transaction.
$identifier = 0;
2016-10-23 05:37:12 -05:00
Log::debug(sprintf('Count %d transactions in updateSplitJournal()', count($data['transactions'])));
2016-10-21 14:41:31 -05:00
foreach ($data['transactions'] as $transaction) {
Log::debug(sprintf('Split journal update split transaction %d', $identifier));
$transaction = $this->appendTransactionData($transaction, $data);
$this->storeSplitTransaction($journal, $transaction, $identifier);
$identifier++;
}
$journal->save();
return $journal;
}
/**
* When the user edits a split journal, each line is missing crucial data:
*
* - Withdrawal lines are missing the source account ID
* - Deposit lines are missing the destination account ID
* - Transfers are missing both.
*
* We need to append the array.
*
* @param array $transaction
* @param array $data
*
* @return array
*/
private function appendTransactionData(array $transaction, array $data): array
{
switch ($data['what']) {
case strtolower(TransactionType::TRANSFER):
case strtolower(TransactionType::WITHDRAWAL):
$transaction['source_account_id'] = intval($data['journal_source_account_id']);
break;
2016-10-22 00:28:31 -05:00
}
switch ($data['what']) {
2016-10-21 14:41:31 -05:00
case strtolower(TransactionType::TRANSFER):
case strtolower(TransactionType::DEPOSIT):
$transaction['destination_account_id'] = intval($data['journal_destination_account_id']);
break;
}
return $transaction;
}
2015-04-28 03:36:13 -05:00
/**
2016-05-01 02:42:08 -05:00
*
* * Remember: a balancingAct takes at most one expense and one transfer.
* an advancePayment takes at most one expense, infinite deposits and NO transfers.
*
2015-04-28 03:36:13 -05:00
* @param TransactionJournal $journal
* @param array $array
*
2016-04-06 02:27:45 -05:00
* @return bool
2015-04-28 03:36:13 -05:00
*/
2016-05-01 02:42:08 -05:00
private function saveTags(TransactionJournal $journal, array $array): bool
2015-04-28 03:36:13 -05:00
{
2016-05-02 13:49:19 -05:00
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
2015-04-28 10:10:52 -05:00
2015-04-28 03:36:13 -05:00
foreach ($array as $name) {
2015-04-28 14:01:54 -05:00
if (strlen(trim($name)) > 0) {
2016-05-01 02:42:08 -05:00
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
if (!is_null($tag)) {
2016-11-11 23:34:54 -06:00
Log::debug(sprintf('Will try to connect tag #%d to journal #%d.', $tag->id, $journal->id));
2016-05-01 02:42:08 -05:00
$tagRepository->connect($journal, $tag);
}
2015-04-28 14:01:54 -05:00
}
2015-04-28 03:36:13 -05:00
}
2016-04-06 02:27:45 -05:00
return true;
2015-04-28 03:36:13 -05:00
}
2015-03-29 05:32:00 -05:00
/**
* @param TransactionType $type
* @param array $data
*
* @return array
2016-02-17 14:14:32 -06:00
* @throws FireflyException
2015-03-29 05:32:00 -05:00
*/
2016-05-01 02:42:08 -05:00
private function storeAccounts(TransactionType $type, array $data): array
2015-03-29 05:32:00 -05:00
{
$accounts = [
'source' => null,
'destination' => null,
];
2016-10-22 00:28:31 -05:00
2016-10-21 14:41:31 -05:00
Log::debug(sprintf('Going to store accounts for type %s', $type->type));
2015-03-29 05:32:00 -05:00
switch ($type->type) {
case TransactionType::WITHDRAWAL:
$accounts = $this->storeWithdrawalAccounts($data);
2015-02-27 07:27:04 -06:00
break;
case TransactionType::DEPOSIT:
$accounts = $this->storeDepositAccounts($data);
2015-02-27 07:27:04 -06:00
break;
case TransactionType::TRANSFER:
$accounts['source'] = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first();
$accounts['destination'] = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first();
2015-02-27 07:27:04 -06:00
break;
2016-04-24 00:18:39 -05:00
default:
throw new FireflyException(sprintf('Did not recognise transaction type "%s".', $type->type));
2015-02-27 07:27:04 -06:00
}
2015-05-17 05:49:09 -05:00
if (is_null($accounts['source'])) {
2016-10-21 14:41:31 -05:00
Log::error('"source"-account is null, so we cannot continue!', ['data' => $data]);
throw new FireflyException('"source"-account is null, so we cannot continue!');
2015-04-03 15:54:21 -05:00
}
2015-05-09 11:30:58 -05:00
if (is_null($accounts['destination'])) {
2016-10-21 14:41:31 -05:00
Log::error('"destination"-account is null, so we cannot continue!', ['data' => $data]);
throw new FireflyException('"destination"-account is null, so we cannot continue!');
2015-07-07 12:09:45 -05:00
2015-04-03 15:54:21 -05:00
}
2015-05-14 02:51:54 -05:00
2015-02-27 07:27:04 -06:00
return $accounts;
}
/**
* @param TransactionJournal $journal
* @param int $budgetId
*/
private function storeBudgetWithJournal(TransactionJournal $journal, int $budgetId)
{
2016-11-05 01:43:23 -05:00
if (intval($budgetId) > 0 && $journal->transactionType->type === TransactionType::WITHDRAWAL) {
/** @var \FireflyIII\Models\Budget $budget */
$budget = Budget::find($budgetId);
$journal->budgets()->save($budget);
}
}
2016-10-21 14:41:31 -05:00
/**
* @param Transaction $transaction
* @param int $budgetId
*/
private function storeBudgetWithTransaction(Transaction $transaction, int $budgetId)
{
if (intval($budgetId) > 0 && $transaction->transactionJournal->transactionType->type !== TransactionType::TRANSFER) {
/** @var \FireflyIII\Models\Budget $budget */
$budget = Budget::find($budgetId);
$transaction->budgets()->save($budget);
}
}
/**
* @param TransactionJournal $journal
* @param string $category
*/
private function storeCategoryWithJournal(TransactionJournal $journal, string $category)
{
if (strlen($category) > 0) {
$category = Category::firstOrCreateEncrypted(['name' => $category, 'user_id' => $journal->user_id]);
$journal->categories()->save($category);
}
2015-05-17 05:49:09 -05:00
}
2016-10-21 14:41:31 -05:00
/**
* @param Transaction $transaction
* @param string $category
*/
private function storeCategoryWithTransaction(Transaction $transaction, string $category)
{
if (strlen($category) > 0) {
$category = Category::firstOrCreateEncrypted(['name' => $category, 'user_id' => $transaction->transactionJournal->user_id]);
$transaction->categories()->save($category);
}
}
2015-05-17 05:49:09 -05:00
/**
* @param array $data
*
* @return array
*/
2016-05-01 02:42:08 -05:00
private function storeDepositAccounts(array $data): array
2015-05-17 05:49:09 -05:00
{
2016-11-18 11:58:48 -06:00
Log::debug('Now in storeDepositAccounts().');
$destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first(['accounts.*']);
2015-05-17 05:49:09 -05:00
2016-11-18 11:58:48 -06:00
Log::debug(sprintf('Destination account is #%d ("%s")', $destinationAccount->id, $destinationAccount->name));
2016-04-29 10:29:13 -05:00
if (strlen($data['source_account_name']) > 0) {
$sourceType = AccountType::where('type', 'Revenue account')->first();
$sourceAccount = Account::firstOrCreateEncrypted(
2016-10-23 05:19:32 -05:00
['user_id' => $this->user->id, 'account_type_id' => $sourceType->id, 'name' => $data['source_account_name'], 'active' => 1]
2015-05-17 05:49:09 -05:00
);
2016-11-18 11:58:48 -06:00
Log::debug(sprintf('source account name is "%s", account is %d', $data['source_account_name'], $sourceAccount->id));
return [
'source' => $sourceAccount,
'destination' => $destinationAccount,
];
2015-05-17 05:49:09 -05:00
}
2016-11-18 11:58:48 -06:00
Log::debug('source_account_name is empty, so default to cash account!');
$sourceType = AccountType::where('type', AccountType::CASH)->first();
$sourceAccount = Account::firstOrCreateEncrypted(
2016-10-23 05:19:32 -05:00
['user_id' => $this->user->id, 'account_type_id' => $sourceType->id, 'name' => 'Cash account', 'active' => 1]
);
return [
'source' => $sourceAccount,
'destination' => $destinationAccount,
];
}
2016-10-21 14:41:31 -05:00
/**
* @param TransactionJournal $journal
* @param array $transaction
* @param int $identifier
*
* @return Collection
*/
private function storeSplitTransaction(TransactionJournal $journal, array $transaction, int $identifier): Collection
{
// store source and destination accounts (depends on type)
$accounts = $this->storeAccounts($journal->transactionType, $transaction);
// store transaction one way:
$one = $this->storeTransaction(
[
'journal' => $journal,
'account' => $accounts['source'],
'amount' => bcmul(strval($transaction['amount']), '-1'),
'description' => $transaction['description'],
'category' => null,
'budget' => null,
'identifier' => $identifier,
]
);
$this->storeCategoryWithTransaction($one, $transaction['category']);
$this->storeBudgetWithTransaction($one, $transaction['budget_id']);
// and the other way:
$two = $this->storeTransaction(
[
'journal' => $journal,
'account' => $accounts['destination'],
'amount' => strval($transaction['amount']),
'description' => $transaction['description'],
'category' => null,
'budget' => null,
'identifier' => $identifier,
]
);
$this->storeCategoryWithTransaction($two, $transaction['category']);
$this->storeBudgetWithTransaction($two, $transaction['budget_id']);
return new Collection([$one, $two]);
}
2016-10-21 14:41:31 -05:00
/**
* @param array $data
*
* @return Transaction
*/
private function storeTransaction(array $data): Transaction
{
/** @var Transaction $transaction */
$transaction = Transaction::create(
[
'transaction_journal_id' => $data['journal']->id,
'account_id' => $data['account']->id,
'amount' => $data['amount'],
'description' => $data['description'],
'identifier' => $data['identifier'],
]
);
2016-10-21 14:41:31 -05:00
Log::debug(sprintf('Transaction stored with ID: %s', $transaction->id));
if (!is_null($data['category'])) {
$transaction->categories()->save($data['category']);
}
if (!is_null($data['budget'])) {
$transaction->categories()->save($data['budget']);
}
return $transaction;
2015-05-17 05:49:09 -05:00
}
/**
* @param array $data
*
* @return array
*/
2016-05-01 02:42:08 -05:00
private function storeWithdrawalAccounts(array $data): array
2015-05-17 05:49:09 -05:00
{
2016-11-18 11:58:48 -06:00
Log::debug('Now in storeWithdrawalAccounts().');
2016-04-29 10:29:13 -05:00
$sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first(['accounts.*']);
2016-11-18 11:58:48 -06:00
Log::debug(sprintf('Source account is #%d ("%s")', $sourceAccount->id, $sourceAccount->name));
2016-04-29 10:29:13 -05:00
if (strlen($data['destination_account_name']) > 0) {
2016-10-21 14:41:31 -05:00
$destinationType = AccountType::where('type', AccountType::EXPENSE)->first();
2016-04-29 10:29:13 -05:00
$destinationAccount = Account::firstOrCreateEncrypted(
[
2016-10-23 05:19:32 -05:00
'user_id' => $this->user->id,
2016-04-29 10:29:13 -05:00
'account_type_id' => $destinationType->id,
'name' => $data['destination_account_name'],
'active' => 1,
]
2015-05-17 05:49:09 -05:00
);
2016-04-29 10:29:13 -05:00
2016-11-18 11:58:48 -06:00
Log::debug(sprintf('destination account name is "%s", account is %d', $data['destination_account_name'], $destinationAccount->id));
return [
'source' => $sourceAccount,
'destination' => $destinationAccount,
];
2015-05-17 05:49:09 -05:00
}
2016-11-18 11:58:48 -06:00
Log::debug('destination_account_name is empty, so default to cash account!');
$destinationType = AccountType::where('type', AccountType::CASH)->first();
2016-04-29 10:29:13 -05:00
$destinationAccount = Account::firstOrCreateEncrypted(
2016-10-23 05:19:32 -05:00
['user_id' => $this->user->id, 'account_type_id' => $destinationType->id, 'name' => 'Cash account', 'active' => 1]
2016-04-29 10:29:13 -05:00
);
return [
'source' => $sourceAccount,
'destination' => $destinationAccount,
];
}
/**
* @param TransactionJournal $journal
* @param Account $account
* @param string $amount
*
* @throws FireflyException
*/
private function updateDestinationTransaction(TransactionJournal $journal, Account $account, string $amount)
{
// should be one:
$set = $journal->transactions()->where('amount', '>', 0)->get();
if ($set->count() != 1) {
throw new FireflyException(
sprintf('Journal #%d has an unexpected (%d) amount of transactions with an amount more than zero.', $journal->id, $set->count())
);
}
/** @var Transaction $transaction */
$transaction = $set->first();
$transaction->amount = $amount;
$transaction->account_id = $account->id;
$transaction->save();
}
/**
* @param TransactionJournal $journal
* @param Account $account
* @param string $amount
*
* @throws FireflyException
*/
private function updateSourceTransaction(TransactionJournal $journal, Account $account, string $amount)
{
// should be one:
$set = $journal->transactions()->where('amount', '<', 0)->get();
if ($set->count() != 1) {
throw new FireflyException(
sprintf('Journal #%d has an unexpected (%d) amount of transactions with an amount less than zero.', $journal->id, $set->count())
);
}
/** @var Transaction $transaction */
$transaction = $set->first();
$transaction->amount = $amount;
$transaction->account_id = $account->id;
$transaction->save();
2016-04-29 10:29:13 -05:00
2015-05-17 05:49:09 -05:00
2015-02-27 07:27:04 -06:00
}
2016-05-01 02:42:08 -05:00
/**
* @param TransactionJournal $journal
* @param array $array
*
* @return bool
*/
private function updateTags(TransactionJournal $journal, array $array): bool
{
// create tag repository
2016-05-02 13:49:19 -05:00
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
2016-05-01 02:42:08 -05:00
// find or create all tags:
$tags = [];
$ids = [];
foreach ($array as $name) {
if (strlen(trim($name)) > 0) {
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
$tags[] = $tag;
$ids[] = $tag->id;
}
}
// delete all tags connected to journal not in this array:
if (count($ids) > 0) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
}
// if count is zero, delete them all:
if (count($ids) == 0) {
DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
}
// connect each tag to journal (if not yet connected):
/** @var Tag $tag */
foreach ($tags as $tag) {
2016-11-11 23:34:54 -06:00
Log::debug(sprintf('Will try to connect tag #%d to journal #%d.', $tag->id, $journal->id));
2016-05-01 02:42:08 -05:00
$tagRepository->connect($journal, $tag);
}
return true;
}
}