diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index b65b400f64..50870a4b6e 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -223,6 +223,21 @@ class TransactionJournal extends Model return $value; } + /** + * @return Account + */ + public function getDestinationAccountAttribute() + { + /** @var Transaction $transaction */ + foreach ($this->transactions()->get() as $transaction) { + if (floatval($transaction->amount) > 0) { + return $transaction->account; + } + } + + return $this->transactions()->first()->account; + } + /** * @codeCoverageIgnore * @return \Illuminate\Database\Eloquent\Relations\HasMany @@ -273,24 +288,6 @@ class TransactionJournal extends Model return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00')); } - /** - * @codeCoverageIgnore - * - * @param EloquentBuilder $query - * @param $amount - */ - public function scopeLessThan(EloquentBuilder $query, $amount) - { - if (is_null($this->joinedTransactions)) { - $query->leftJoin( - 'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id' - ); - $this->joinedTransactions = true; - } - - $query->where('transactions.amount', '<=', $amount); - } - /** * @codeCoverageIgnore * @@ -304,24 +301,6 @@ class TransactionJournal extends Model return $query->where('date', '=', $date->format('Y-m-d')); } - /** - * Returns the account to which the money was moved. - * - * @codeCoverageIgnore - * - * @param EloquentBuilder $query - * @param Account $account - */ - public function scopeToAccountIs(EloquentBuilder $query, Account $account) - { - $query->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '>', 0); - } - ); - $query->where('transactions.account_id', $account->id); - } - /** * @codeCoverageIgnore * diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index c8ff2053eb..f98448c387 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -287,7 +287,7 @@ class AccountRepository implements AccountRepositoryInterface */ public function getTransfersInRange(Account $account, Carbon $start, Carbon $end) { - return TransactionJournal::whereIn( + $set = TransactionJournal::whereIn( 'id', function (Builder $q) use ($account, $start, $end) { $q->select('transaction_journals.id') ->from('transactions') @@ -297,11 +297,19 @@ class AccountRepository implements AccountRepositoryInterface ->where('transaction_journals.user_id', Auth::user()->id) ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - ->where('transactions.amount', '>', 0) ->where('transaction_types.type', 'Transfer'); } )->get(); + $filtered = $set->filter( + function (TransactionJournal $journal) use ($account) { + if ($journal->destination_account->id == $account->id) { + return $journal; + } + } + ); + + return $filtered; } /** diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index d384b8da8a..b41c73b671 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -108,16 +108,10 @@ class BillRepository implements BillRepositoryInterface public function getJournals(Bill $bill) { return $bill->transactionjournals()->withRelevantData() - ->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->where('transactions.amount', '>', 0); - } - ) ->orderBy('transaction_journals.date', 'DESC') ->orderBy('transaction_journals.order', 'ASC') ->orderBy('transaction_journals.id', 'DESC') - ->get(['transaction_journals.*', 'transactions.amount']); + ->get(['transaction_journals.*']); } /** diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index ed4ed70654..f2b40c9747 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -201,7 +201,7 @@ class CategoryRepository implements CategoryRepositoryInterface ->before($end) ->after($start) ->transactionTypes(['Withdrawal']) - ->lessThan(0) + ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') ->leftJoin( 'account_meta', function (JoinClause $join) { diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index c13c79adb7..120e96780e 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -76,15 +76,19 @@ class JournalRepository implements JournalRepositoryInterface */ public function getAmountBefore(TransactionJournal $journal, Transaction $transaction) { - return floatval( - $transaction->account->transactions()->leftJoin( - 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id' - ) - ->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d')) - ->where('transaction_journals.order', '>=', $journal->order) - ->where('transaction_journals.id', '!=', $journal->id) - ->sum('transactions.amount') - ); + $set = $transaction->account->transactions()->leftJoin( + 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id' + ) + ->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d')) + ->where('transaction_journals.order', '>=', $journal->order) + ->where('transaction_journals.id', '!=', $journal->id) + ->get(['transactions.*']); + $sum = 0; + foreach($set as $entry) { + $sum += $entry->amount; + } + return $sum; + } /** diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 753d35f23e..f1b42f628e 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -78,9 +78,13 @@ class TagRepository implements TagRepositoryInterface /** @var Tag $tag */ foreach ($tags as $tag) { - $transfer = $tag->transactionjournals()->after($start)->before($end)->toAccountIs($account)->transactionTypes(['Transfer'])->first(); - if ($transfer) { - $amount += $transfer->amount; + $journals = $tag->transactionjournals()->after($start)->before($end)->transactionTypes(['Transfer'])->get(['transaction_journals.*']); + + /** @var TransactionJournal $journal */ + foreach ($journals as $journal) { + if ($journal->destination_account->id == $account->id) { + $amount += $journal->amount; + } } } diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 512e7bbe93..f7494d61e5 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -34,11 +34,15 @@ class Steam $firstDate = is_null($firstDateObject) ? clone $date : new Carbon($firstDateObject->date); $date = $date < $firstDate ? $firstDate : $date; - $balance = floatval( - $account->transactions()->leftJoin( - 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id' - )->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount') - ); + + $set = $account->transactions()->leftJoin( + 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id' + )->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->get(['transactions.*']); + $balance = 0; + foreach($set as $entry) { + $balance += $entry->amount; + } + if (!$ignoreVirtualBalance) { $balance += floatval($account->virtual_balance); } diff --git a/app/Support/Twig/Budget.php b/app/Support/Twig/Budget.php index 787f14bd39..d19e7f658b 100644 --- a/app/Support/Twig/Budget.php +++ b/app/Support/Twig/Budget.php @@ -20,25 +20,6 @@ class Budget extends Twig_Extension */ public function getFunctions() { - $functions[] = new Twig_SimpleFunction( - 'spentInRepetition', function (LimitRepetition $repetition) { - $sum = DB::table('transactions') - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id') - ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') - ->where('transaction_journals.date', '>=', $repetition->startdate->format('Y-m-d')) - ->where('transaction_journals.date', '<=', $repetition->enddate->format('Y-m-d')) - ->where('transaction_journals.user_id', Auth::user()->id) - ->whereNull('transactions.deleted_at') - ->where('transactions.amount', '>', 0) - ->where('limit_repetitions.id', '=', $repetition->id) - ->sum('transactions.amount'); - - return floatval($sum); - } - ); - $functions[] = new Twig_SimpleFunction( 'spentInRepetitionCorrected', function (LimitRepetition $repetition) { $sum