mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Optimise queries.
This commit is contained in:
@@ -59,6 +59,7 @@ class JsonController extends Controller
|
|||||||
$steps[7]['orphan'] = true; // final in the center again.
|
$steps[7]['orphan'] = true; // final in the center again.
|
||||||
$steps[7]['backdrop'] = true;
|
$steps[7]['backdrop'] = true;
|
||||||
$template = view('json.tour')->render();
|
$template = view('json.tour')->render();
|
||||||
|
|
||||||
return Response::json(['steps' => $steps, 'template' => $template]);
|
return Response::json(['steps' => $steps, 'template' => $template]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +74,6 @@ class JsonController extends Controller
|
|||||||
{
|
{
|
||||||
$start = Session::get('start', Carbon::now()->startOfMonth());
|
$start = Session::get('start', Carbon::now()->startOfMonth());
|
||||||
$end = Session::get('end', Carbon::now()->endOfMonth());
|
$end = Session::get('end', Carbon::now()->endOfMonth());
|
||||||
$amount = 0;
|
|
||||||
bcscale(2);
|
bcscale(2);
|
||||||
|
|
||||||
// works for json too!
|
// works for json too!
|
||||||
@@ -84,16 +84,10 @@ class JsonController extends Controller
|
|||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$bills = $repository->getActiveBills(); // these two functions are the same as the chart
|
// get amount from bills
|
||||||
|
$amount = $repository->billsPaidInRange($start, $end)->sum('paid');
|
||||||
/** @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.
|
|
||||||
|
|
||||||
|
// add credit card bill.
|
||||||
$creditCards = $accountRepository->getCreditCards(); // Find credit card accounts and possibly unpaid credit card bills.
|
$creditCards = $accountRepository->getCreditCards(); // Find credit card accounts and possibly unpaid credit card bills.
|
||||||
/** @var Account $creditCard */
|
/** @var Account $creditCard */
|
||||||
foreach ($creditCards as $creditCard) {
|
foreach ($creditCards as $creditCard) {
|
||||||
|
@@ -9,6 +9,7 @@ use FireflyIII\Models\Account;
|
|||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
|
use Illuminate\Database\Query\JoinClause;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
use Navigation;
|
use Navigation;
|
||||||
@@ -84,7 +85,7 @@ class BillRepository implements BillRepositoryInterface
|
|||||||
public function getActiveBills()
|
public function getActiveBills()
|
||||||
{
|
{
|
||||||
/** @var Collection $set */
|
/** @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;
|
return $set;
|
||||||
}
|
}
|
||||||
@@ -524,4 +525,45 @@ class BillRepository implements BillRepositoryInterface
|
|||||||
|
|
||||||
return $set;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -49,6 +49,17 @@ interface BillRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function billPaymentsInRange(Bill $bill, Carbon $start, Carbon $end);
|
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.
|
* Create a fake bill to help the chart controller.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user