Experimental query. [skip ci]

This commit is contained in:
James Cole 2016-05-14 22:11:49 +02:00
parent 4870945af2
commit e64b40d58b

View File

@ -13,6 +13,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Log;
@ -323,8 +324,25 @@ class AccountRepository implements AccountRepositoryInterface
}
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$query->leftJoin('transactions as t', 't.transaction_journal_id', '=', 'transaction_journals.id');
$query->whereIn('t.account_id', $accountIds);
$query->leftJoin(
'transactions as source', function (JoinClause $join) {
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('amount', '<', 0);
}
);
$query->leftJoin(
'transactions as destination', function (JoinClause $join) {
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('amount', '>', 0);
}
);
// XOR. must be either.
$query->where(
function (Builder $query) use ($accountIds) {
$query->whereIn('source.account_id', $accountIds, 'xor')
->whereIn('destination.account_id', $accountIds, 'xor');
}
);
}
// that should do it:
$complete = $query->get(TransactionJournal::queryFields());