Skip to content

Firefly III: a personal finances manager

Some query optimisations.

James Cole67a178591d62
diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php
index 96f42b5662..2b1b24b20b 100644
--- a/app/Http/Controllers/JsonController.php
+++ b/app/Http/Controllers/JsonController.php
@@ -15,7 +15,6 @@ use Illuminate\Support\Collection;
use Preferences;
use Response;
use Session;
-use Steam;
/**
* Class JsonController
@@ -72,8 +71,8 @@ class JsonController extends Controller
*/
public function boxBillsPaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
- $start = Session::get('start', Carbon::now()->startOfMonth());
- $end = Session::get('end', Carbon::now()->endOfMonth());
+ $start = Session::get('start', Carbon::now()->startOfMonth());
+ $end = Session::get('end', Carbon::now()->endOfMonth());
bcscale(2);
// works for json too!
@@ -88,11 +87,10 @@ class JsonController extends Controller
$amount = $repository->billsPaidInRange($start, $end)->sum('paid');
// add credit card bill.
- $creditCards = $accountRepository->getCreditCards(); // Find credit card accounts and possibly unpaid credit card bills.
+ $creditCards = $accountRepository->getCreditCards($end); // Find credit card accounts and possibly unpaid credit card bills.
/** @var Account $creditCard */
foreach ($creditCards as $creditCard) {
- $balance = Steam::balance($creditCard, $end, true); // if the balance is not zero, the monthly payment is still underway.
- if ($balance == 0) {
+ if ($creditCard->balance == 0) {
// find a transfer TO the credit card which should account for
// anything paid. If not, the CC is not yet used.
$amount = bcadd($amount, $accountRepository->getTransfersInRange($creditCard, $start, $end)->sum('amount'));
@@ -142,14 +140,15 @@ class JsonController extends Controller
}
unset($bill, $bills, $range, $ranges);
- $creditCards = $accountRepository->getCreditCards();
+ $creditCards = $accountRepository->getCreditCards($end);
+
+ /** @var Account $creditCard */
foreach ($creditCards as $creditCard) {
- $balance = Steam::balance($creditCard, $end, true);
- $date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate'));
- if ($balance < 0) {
+ $date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate'));
+ if ($creditCard->balance < 0) {
// unpaid! create a fake bill that matches the amount.
$description = $creditCard->name;
- $fakeAmount = $balance * -1;
+ $fakeAmount = $creditCard->balance * -1;
$fakeBill = $repository->createFakeBill($description, $date, $fakeAmount);
$unpaid->push([$fakeBill, $date]);
}
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index 12091470b8..8d6c54af42 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -106,18 +106,32 @@ class AccountRepository implements AccountRepositoryInterface
/**
+ * This method returns the users credit cards, along with some basic information about the
+ * balance they have on their CC. To be used in the JSON boxes on the front page that say
+ * how many bills there are still left to pay. The balance will be saved in field "balance".
+ *
+ * To get the balance, the field "date" is necessary.
+ *
+ * @param Carbon $date
+ *
* @return Collection
*/
- public function getCreditCards()
+ public function getCreditCards(Carbon $date)
{
return Auth::user()->accounts()
->hasMetaValue('accountRole', 'ccAsset')
->hasMetaValue('ccType', 'monthlyFull')
+ ->leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')
+ ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
+ ->whereNull('transactions.deleted_at')
+ ->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
+ ->groupBy('accounts.id')
->get(
[
'accounts.*',
'ccType.data as ccType',
- 'accountRole.data as accountRole'
+ 'accountRole.data as accountRole',
+ DB::Raw('SUM(`transactions`.`amount`) AS `balance`')
]
);
}
@@ -350,13 +364,14 @@ class AccountRepository implements AccountRepositoryInterface
*/
public function getTransfersInRange(Account $account, Carbon $start, Carbon $end)
{
- $set = TransactionJournal::whereIn(
+ $set = TransactionJournal::whereIn(
'id', function (Builder $q) use ($account, $start, $end) {
$q->select('transaction_journals.id')
->from('transactions')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('transactions.account_id', $account->id)
+ ->where('transactions.amount', '>', 0)// this makes the filter unnecessary.
->where('transaction_journals.user_id', Auth::user()->id)
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
@@ -364,17 +379,8 @@ class AccountRepository implements AccountRepositoryInterface
}
)->get();
- $filtered = $set->filter(
- function (TransactionJournal $journal) use ($account) {
- if ($journal->destination_account->id == $account->id) {
- return $journal;
- }
- return null;
- }
- );
-
- return $filtered;
+ return $set;
}
/**
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 8bbcf7c6b2..bb7b5e0c8f 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -26,6 +26,7 @@ interface AccountRepositoryInterface
/**
* @param $accountId
+ *
* @deprecated
*
* @return Account
@@ -56,9 +57,17 @@ interface AccountRepositoryInterface
public function getFirstTransaction(TransactionJournal $journal, Account $account);
/**
+ * This method returns the users credit cards, along with some basic information about the
+ * balance they have on their CC. To be used in the JSON boxes on the front page that say
+ * how many bills there are still left to pay. The balance will be saved in field "balance".
+ *
+ * To get the balance, the field "date" is necessary.
+ *
+ * @param Carbon $date
+ *
* @return Collection
*/
- public function getCreditCards();
+ public function getCreditCards(Carbon $date);
/**
* Get the accounts of a user that have piggy banks connected to them.