mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Optimise query. [skip ci]
This commit is contained in:
parent
2538b4a885
commit
ce78c8993f
@ -166,7 +166,7 @@ class ReportController extends Controller
|
||||
*/
|
||||
if ($start->between($first, $last) || $end->between($first, $last)) {
|
||||
$exists = true;
|
||||
$journals = $repos->journalsInPeriod($accounts, [], $start, $end);
|
||||
$journals = $repos->journalsInPeriod(new Collection([$account]), [], $start, $end);
|
||||
|
||||
}
|
||||
/*
|
||||
|
@ -82,13 +82,29 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
*/
|
||||
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
{
|
||||
$incomes = $this->incomesInPeriod($accounts, $start, $end);
|
||||
$sum = '0';
|
||||
foreach ($incomes as $entry) {
|
||||
$amount = TransactionJournal::amount($entry);
|
||||
$sum = bcadd($sum, $amount);
|
||||
$query = $this->user->transactionjournals()->expanded()->sortCorrectly()
|
||||
->transactionTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]);
|
||||
|
||||
if ($end >= $start) {
|
||||
$query->before($end)->after($start);
|
||||
}
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$query->leftJoin(
|
||||
'transactions as destination', function (JoinClause $join) {
|
||||
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
||||
}
|
||||
);
|
||||
$query->whereIn('destination.account_id', $accountIds);
|
||||
|
||||
}
|
||||
// remove group by
|
||||
$query->getQuery()->getQuery()->groups = null;
|
||||
|
||||
// that should do it:
|
||||
$sum = strval($query->sum('destination.amount'));
|
||||
|
||||
return $sum;
|
||||
|
||||
}
|
||||
@ -522,13 +538,29 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
*/
|
||||
public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
{
|
||||
$incomes = $this->expensesInPeriod($accounts, $start, $end);
|
||||
$sum = '0';
|
||||
foreach ($incomes as $entry) {
|
||||
$amount = TransactionJournal::amountPositive($entry);
|
||||
$sum = bcadd($sum, $amount);
|
||||
/** @var HasMany $query */
|
||||
$query = $this->user->transactionjournals()->expanded()->sortCorrectly()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]);
|
||||
if ($end >= $start) {
|
||||
$query->before($end)->after($start);
|
||||
}
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
||||
}
|
||||
);
|
||||
$query->whereIn('source.account_id', $accountIds);
|
||||
|
||||
}
|
||||
// remove group by
|
||||
$query->getQuery()->getQuery()->groups = null;
|
||||
|
||||
// that should do it:
|
||||
$sum = strval($query->sum('source.amount'));
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user