Skip to content

Firefly III: a personal finances manager

Use a lot less queries

James Colea6594358d8e3
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index 11d207773d..d80f2a021b 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -56,8 +56,8 @@ class AccountController extends Controller
}
/**
- * @param ARI $repository
- * @param Account $account
+ * @param ARI $repository
+ * @param Account $account
*
* @return \Illuminate\View\View
*/
@@ -77,8 +77,8 @@ class AccountController extends Controller
}
/**
- * @param ARI $repository
- * @param Account $account
+ * @param ARI $repository
+ * @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/
@@ -98,8 +98,8 @@ class AccountController extends Controller
}
/**
- * @param ARI $repository
- * @param Account $account
+ * @param ARI $repository
+ * @param Account $account
*
* @return \Illuminate\View\View
*/
@@ -143,7 +143,7 @@ class AccountController extends Controller
}
/**
- * @param ARI $repository
+ * @param ARI $repository
* @param $what
*
* @return \Illuminate\View\View
@@ -184,8 +184,8 @@ class AccountController extends Controller
}
/**
- * @param ARI $repository
- * @param Account $account
+ * @param ARI $repository
+ * @param Account $account
*
* @return \Illuminate\View\View
*/
@@ -195,7 +195,7 @@ class AccountController extends Controller
$subTitleIcon = Config::get('firefly.subTitlesByIdentifier.' . $account->accountType->type);
$what = Config::get('firefly.shortNamesByFullName.' . $account->accountType->type);
$journals = $repository->getJournals($account, $page);
- $subTitle = trans('firefly.details_for_' . $what, ['name' => $account->name]);
+ $subTitle = trans('firefly.details_for_' . $what, ['name' => $account->name]);
$journals->setPath('accounts/show/' . $account->id);
@@ -203,8 +203,8 @@ class AccountController extends Controller
}
/**
- * @param AccountFormRequest $request
- * @param ARI $repository
+ * @param AccountFormRequest $request
+ * @param ARI $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
@@ -242,9 +242,9 @@ class AccountController extends Controller
}
/**
- * @param AccountFormRequest $request
- * @param ARI $repository
- * @param Account $account
+ * @param AccountFormRequest $request
+ * @param ARI $repository
+ * @param Account $account
*
* @return \Illuminate\Http\RedirectResponse
*/
diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php
index fe7a1ebeae..d564789c21 100644
--- a/app/Http/Controllers/CategoryController.php
+++ b/app/Http/Controllers/CategoryController.php
@@ -202,6 +202,12 @@ class CategoryController extends Controller
$cache->addProperty($end);
$cache->addProperty('category-show');
$cache->addProperty($category->id);
+
+ // get all spent and earned data:
+ // get amount earned in period, grouped by day.
+ $spentArray = $repository->spentPerDay($category, $start, $end);
+ $earnedArray = $repository->earnedPerDay($category, $start, $end);
+
if ($cache->has()) {
$entries = $cache->get();
} else {
@@ -210,9 +216,9 @@ class CategoryController extends Controller
$end = Navigation::startOfPeriod($end, $range);
$currentEnd = Navigation::endOfPeriod($end, $range);
- // here do something.
- $spent = $repository->spentInPeriod($category, $end, $currentEnd);
- $earned = $repository->earnedInPeriod($category, $end, $currentEnd);
+ // get data from spentArray:
+ $spent = $this->getSumOfRange($end, $currentEnd, $spentArray);
+ $earned = $this->getSumOfRange($end, $currentEnd, $earnedArray);
$dateStr = $end->format('Y-m-d');
$dateName = Navigation::periodShow($end, $range);
$entries->push([$dateStr, $dateName, $spent, $earned]);
diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php
index 2271330061..0b75b0b328 100644
--- a/app/Http/Controllers/Chart/CategoryController.php
+++ b/app/Http/Controllers/Chart/CategoryController.php
@@ -460,32 +460,6 @@ class CategoryController extends Controller
return $data;
}
- /**
- * Take the array as returned by SingleCategoryRepositoryInterface::spentPerDay and SingleCategoryRepositoryInterface::earnedByDay
- * and sum up everything in the array in the given range.
- *
- * @param Carbon $start
- * @param Carbon $end
- * @param array $array
- *
- * @return string
- */
- protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
- {
- bcscale(2);
- $sum = '0';
- $currentStart = clone $start; // to not mess with the original one
- $currentEnd = clone $end; // to not mess with the original one
-
- while ($currentStart <= $currentEnd) {
- $date = $currentStart->format('Y-m-d');
- if (isset($array[$date])) {
- $sum = bcadd($sum, $array[$date]);
- }
- $currentStart->addDay();
- }
- return $sum;
- }
}
diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php
index 465c38c441..55d0bc1765 100644
--- a/app/Http/Controllers/Controller.php
+++ b/app/Http/Controllers/Controller.php
@@ -1,6 +1,7 @@
<?php namespace FireflyIII\Http\Controllers;
use Auth;
+use Carbon\Carbon;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
@@ -44,4 +45,32 @@ abstract class Controller extends BaseController
View::share('localeconv', localeconv());
}
}
+
+ /**
+ * Take the array as returned by SingleCategoryRepositoryInterface::spentPerDay and SingleCategoryRepositoryInterface::earnedByDay
+ * and sum up everything in the array in the given range.
+ *
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param array $array
+ *
+ * @return string
+ */
+ protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
+ {
+ bcscale(2);
+ $sum = '0';
+ $currentStart = clone $start; // to not mess with the original one
+ $currentEnd = clone $end; // to not mess with the original one
+
+ while ($currentStart <= $currentEnd) {
+ $date = $currentStart->format('Y-m-d');
+ if (isset($array[$date])) {
+ $sum = bcadd($sum, $array[$date]);
+ }
+ $currentStart->addDay();
+ }
+
+ return $sum;
+ }
}
diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php
index 50e0faf2b3..25d8f93298 100644
--- a/app/Support/Navigation.php
+++ b/app/Support/Navigation.php
@@ -100,11 +100,11 @@ class Navigation
/**
*
- * @param Carbon $theCurrentEnd
+ * @param \Carbon\Carbon $theCurrentEnd
* @param $repeatFreq
- * @param Carbon $maxDate
+ * @param \Carbon\Carbon $maxDate
*
- * @return Carbon
+ * @return \Carbon\Carbon
*/
public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate = null)
{
@@ -218,11 +218,11 @@ class Navigation
}
/**
- * @param Carbon $theDate
+ * @param \Carbon\Carbon $theDate
* @param $repeatFreq
* @param int $subtract
*
- * @return Carbon
+ * @return \Carbon\Carbon
* @throws FireflyException
*/
public function subtractPeriod(Carbon $theDate, $repeatFreq, $subtract = 1)