diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php index 8b02c35307..9c5d658f81 100644 --- a/app/Helpers/Report/BudgetReportHelper.php +++ b/app/Helpers/Report/BudgetReportHelper.php @@ -67,8 +67,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface $budgetLine = new BudgetLine; $budgetLine->setBudget($budget); $budgetLine->setRepetition($repetition); - $expenses = $repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate); - + $expenses = $repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate); $left = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : '0'; $spent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0'; $overspent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index f92cf4c9f3..0312f19693 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -304,10 +304,14 @@ class BudgetRepository implements BudgetRepositoryInterface // first collect actual transaction journals (fairly easy) $query = $this->user ->transactionjournals() - ->distinct() ->leftJoin( - 'transactions as t', function (JoinClause $join) { - $join->on('t.transaction_journal_id', '=', 'transaction_journals.id')->where('amount', '<', 0); + 'transactions as source', function (JoinClause $join) { + $join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0); + } + ) + ->leftJoin( + 'transactions as destination', function (JoinClause $join) { + $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0); } ); @@ -316,7 +320,8 @@ class BudgetRepository implements BudgetRepositoryInterface } if ($accounts->count() > 0) { $accountIds = $accounts->pluck('id')->toArray(); - $query->whereIn('t.account_id', $accountIds); + $set = join(', ', $accountIds); + $query->whereRaw('(source.account_id in (' . $set . ') XOR destination.account_id in (' . $set . '))'); } if ($budgets->count() > 0) { $budgetIds = $budgets->pluck('id')->toArray(); @@ -325,7 +330,7 @@ class BudgetRepository implements BudgetRepositoryInterface } // that should do it: - $first = strval($query->sum('t.amount')); + $first = strval($query->sum('source.amount')); // then collection transactions (harder) $query = $this->user->transactions() diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index 64d63d416b..6dacf03f22 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -454,7 +454,6 @@ class CategoryRepository implements CategoryRepositoryInterface // first collect actual transaction journals (fairly easy) $query = $this->user ->transactionjournals() - ->distinct() ->transactionTypes($types) ->leftJoin( 'transactions as source', function (JoinClause $join) {