Skip to content

Firefly III: a personal finances manager

Optimised queries.

James Colebc59f2db0d73
diff --git a/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php b/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php
index 7e3edf4d06..1e7ae33828 100644
--- a/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php
+++ b/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php
@@ -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'][] = [
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index 8d96e141e2..ab722f3786 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -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;
}
/**