Optimise queries.

This commit is contained in:
James Cole 2015-12-26 09:39:35 +01:00
parent 209116e766
commit 8b0f0fb615
3 changed files with 58 additions and 11 deletions

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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.
*