Better query for no budget. [skip ci]

This commit is contained in:
James Cole 2016-05-16 07:13:54 +02:00
parent 4effc95c5f
commit 5a43e6cb9f
2 changed files with 15 additions and 6 deletions

View File

@ -74,7 +74,8 @@ class BalanceReportHelper implements BalanceReportHelperInterface
/** @var LimitRepetition $repetition */
foreach ($limitRepetitions as $repetition) {
$budget = $this->budgetRepository->find($repetition->budget_id);
$balance->addBalanceLine($this->createBalanceLine($budget, $repetition, $accounts));
$line = $this->createBalanceLine($budget, $repetition, $accounts);
$balance->addBalanceLine($line);
}
$noBudgetLine = $this->createNoBudgetLine($accounts, $start, $end);
$coveredByTagLine = $this->createTagsBalanceLine($accounts, $start, $end);

View File

@ -360,15 +360,22 @@ class BudgetRepository implements BudgetRepositoryInterface
*/
public function spentInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): string
{
$types = [TransactionType::WITHDRAWAL];
$query = $this->user->transactionjournals()
->distinct()
->transactionTypes($types)
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->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('budget_transaction', 't.id', '=', 'budget_transaction.transaction_id')
->leftJoin(
'transactions as destination', function (JoinClause $join) {
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
}
)
->leftJoin('budget_transaction', 'source.id', '=', 'budget_transaction.transaction_id')
->whereNull('budget_transaction_journal.id')
->whereNull('budget_transaction.id')
->before($end)
@ -377,9 +384,10 @@ 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 . '))');
}
$sum = strval($query->sum('t.amount'));
$sum = strval($query->sum('source.amount'));
return $sum;
}