mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Optimised queries.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace FireflyIII\Generator\Chart\Bill;
|
namespace FireflyIII\Generator\Chart\Bill;
|
||||||
|
|
||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,11 +65,15 @@ class ChartJsBillChartGenerator implements BillChartGenerator
|
|||||||
$minAmount = [];
|
$minAmount = [];
|
||||||
$maxAmount = [];
|
$maxAmount = [];
|
||||||
$actualAmount = [];
|
$actualAmount = [];
|
||||||
|
/** @var TransactionJournal $entry */
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$data['labels'][] = $entry->date->formatLocalized($format);
|
$data['labels'][] = $entry->date->formatLocalized($format);
|
||||||
$minAmount[] = round($bill->amount_min, 2);
|
$minAmount[] = round($bill->amount_min, 2);
|
||||||
$maxAmount[] = round($bill->amount_max, 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'][] = [
|
$data['datasets'][] = [
|
||||||
|
@@ -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
|
* @param Bill $bill
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
public function getJournals(Bill $bill)
|
public function getJournals(Bill $bill)
|
||||||
{
|
{
|
||||||
return $bill->transactionjournals()->withRelevantData()
|
$cache = new CacheProperties;
|
||||||
->orderBy('transaction_journals.date', 'DESC')
|
$cache->addProperty($bill->id);
|
||||||
->orderBy('transaction_journals.order', 'ASC')
|
$cache->addProperty('journals-for-bill');
|
||||||
->orderBy('transaction_journals.id', 'DESC')
|
if ($cache->has()) {
|
||||||
->get(['transaction_journals.*']);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user