mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-29 22:47:42 +00:00 
			
		
		
		
	Optimise query. [skip ci]
This commit is contained in:
		| @@ -166,7 +166,7 @@ class ReportController extends Controller | |||||||
|              */ |              */ | ||||||
|             if ($start->between($first, $last) || $end->between($first, $last)) { |             if ($start->between($first, $last) || $end->between($first, $last)) { | ||||||
|                 $exists   = true; |                 $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 |     public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string | ||||||
|     { |     { | ||||||
|         $incomes = $this->incomesInPeriod($accounts, $start, $end); |         $query = $this->user->transactionjournals()->expanded()->sortCorrectly() | ||||||
|         $sum     = '0'; |                             ->transactionTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]); | ||||||
|         foreach ($incomes as $entry) { |  | ||||||
|             $amount = TransactionJournal::amount($entry); |         if ($end >= $start) { | ||||||
|             $sum    = bcadd($sum, $amount); |             $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; |         return $sum; | ||||||
|  |  | ||||||
|     } |     } | ||||||
| @@ -522,13 +538,29 @@ class AccountRepository implements AccountRepositoryInterface | |||||||
|      */ |      */ | ||||||
|     public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string |     public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string | ||||||
|     { |     { | ||||||
|         $incomes = $this->expensesInPeriod($accounts, $start, $end); |         /** @var HasMany $query */ | ||||||
|         $sum     = '0'; |         $query = $this->user->transactionjournals()->expanded()->sortCorrectly() | ||||||
|         foreach ($incomes as $entry) { |                             ->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]); | ||||||
|             $amount = TransactionJournal::amountPositive($entry); |         if ($end >= $start) { | ||||||
|             $sum    = bcadd($sum, $amount); |             $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; |         return $sum; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user