Optimised queries.

This commit is contained in:
James Cole 2015-12-27 20:07:49 +01:00
parent cd2be8c1a4
commit bc59f2db0d
2 changed files with 30 additions and 6 deletions

View File

@ -3,6 +3,7 @@
namespace FireflyIII\Generator\Chart\Bill;
use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
/**
@ -64,11 +65,15 @@ class ChartJsBillChartGenerator implements BillChartGenerator
$minAmount = [];
$maxAmount = [];
$actualAmount = [];
/** @var TransactionJournal $entry */
foreach ($entries as $entry) {
$data['labels'][] = $entry->date->formatLocalized($format);
$minAmount[] = round($bill->amount_min, 2);
$maxAmount[] = round($bill->amount_max, 2);
$actualAmount[] = round(($entry->amount * -1), 2);
/*
* journalAmount has been collected in BillRepository::getJournals
*/
$actualAmount[] = round(($entry->journalAmount * -1), 2);
}
$data['datasets'][] = [

View File

@ -98,17 +98,36 @@ class BillRepository implements BillRepositoryInterface
}
/**
* This method also returns the amount of the journal in "journalAmount"
* for easy access.
*
* @param Bill $bill
*
* @return Collection
*/
public function getJournals(Bill $bill)
{
return $bill->transactionjournals()->withRelevantData()
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
$cache = new CacheProperties;
$cache->addProperty($bill->id);
$cache->addProperty('journals-for-bill');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$set = $bill->transactionjournals()
->leftJoin(
'transactions', function (JoinClause $join) {
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('amount', '<', 0);
}
)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*', 'transactions.amount as journalAmount']);
$cache->store($set);
return $set;
}
/**