From 8b0f0fb61564d727c13813ae197a47a45f27525e Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 26 Dec 2015 09:39:35 +0100 Subject: [PATCH] Optimise queries. --- app/Http/Controllers/JsonController.php | 14 ++---- app/Repositories/Bill/BillRepository.php | 44 ++++++++++++++++++- .../Bill/BillRepositoryInterface.php | 11 +++++ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 2a0dba184f..96f42b5662 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -59,6 +59,7 @@ class JsonController extends Controller $steps[7]['orphan'] = true; // final in the center again. $steps[7]['backdrop'] = true; $template = view('json.tour')->render(); + return Response::json(['steps' => $steps, 'template' => $template]); } @@ -73,7 +74,6 @@ class JsonController extends Controller { $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); - $amount = 0; bcscale(2); // works for json too! @@ -84,16 +84,10 @@ class JsonController extends Controller if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } - $bills = $repository->getActiveBills(); // these two functions are the same as the chart - - /** @var Bill $bill */ - foreach ($bills as $bill) { - $amount = bcadd($amount, $repository->billPaymentsInRange($bill, $start, $end)); - } - unset($bill, $bills); - - $amount = $amount * -1; // make the amount positive again. + // get amount from bills + $amount = $repository->billsPaidInRange($start, $end)->sum('paid'); + // add credit card bill. $creditCards = $accountRepository->getCreditCards(); // Find credit card accounts and possibly unpaid credit card bills. /** @var Account $creditCard */ foreach ($creditCards as $creditCard) { diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 686267e980..90384f755c 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -9,6 +9,7 @@ use FireflyIII\Models\Account; use FireflyIII\Models\Bill; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; +use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; use Log; use Navigation; @@ -84,7 +85,7 @@ class BillRepository implements BillRepositoryInterface public function getActiveBills() { /** @var Collection $set */ - $set = Auth::user()->bills()->orderBy('name', 'ASC')->where('active', 1)->get()->sortBy('name'); + $set = Auth::user()->bills()->where('active', 1)->get()->sortBy('name'); return $set; } @@ -524,4 +525,45 @@ class BillRepository implements BillRepositoryInterface return $set; } + + /** + * This method returns all active bills which have been paid for in the given range, + * with the field "paid" indicating how much the bill was for. + * + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function billsPaidInRange(Carbon $start, Carbon $end) + { + /* + * select bills.*, SUM(transactions.amount) as `paid` from bills + +left join transaction_journals ON transaction_journals.bill_id = bills.id +left join transactions ON transactions.transaction_journal_id = transaction_journals.id +where + + +transaction_journals.date >= "2015-12-01" +and transaction_journals.date <= "2015-12-31" +and bills.active =1 +and transactions.amount > 0 +group by bills.id + */ + $set = Auth::user()->bills() + ->leftJoin('transaction_journals', 'transaction_journals.bill_id', '=', 'bills.id') + ->leftJoin( + 'transactions', function (JoinClause $join) { + $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '>', 0); + } + ) + ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + ->where('bills.active', 1) + ->groupBy('bills.id')->get( + ['bills.*', DB::Raw('SUM(`transactions`.`amount`) as `paid`')] + ); + return $set; + } } diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php index a886536cd4..6fa4739662 100644 --- a/app/Repositories/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Bill/BillRepositoryInterface.php @@ -49,6 +49,17 @@ interface BillRepositoryInterface */ public function billPaymentsInRange(Bill $bill, Carbon $start, Carbon $end); + /** + * This method returns all active bills which have been paid for in the given range, + * with the field "paid" indicating how much the bill was for. + * + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function billsPaidInRange(Carbon $start, Carbon $end); + /** * Create a fake bill to help the chart controller. *