From 3833a41acb0a45ba79382cc0a266d9094bce0512 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 27 Apr 2016 06:19:13 +0200 Subject: [PATCH] Fix sorting. --- app/Helpers/Report/ReportHelper.php | 58 ++++++++++++++++------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index 8fbadc3bd4..6d4a660db1 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -102,6 +102,38 @@ class ReportHelper implements ReportHelperInterface return $collection; } + /** + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Collection + */ + public function getCategoriesWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection + { + /** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */ + $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $collection = $repository->earnedForAccountsPerMonth($accounts, $start, $end); + $second = $repository->spentForAccountsPerMonth($accounts, $start, $end); + $collection = $collection->merge($second); + $array = []; + /** @var Category $category */ + foreach ($collection as $category) { + $id = $category->id; + $array[$id] = $category; + + } + $set = new Collection($array); + + $set = $set->sort( + function (Category $category) { + return $category->name; + } + ); + + return $set; + } + /** * @param Carbon $start * @param Carbon $end @@ -312,30 +344,4 @@ class ReportHelper implements ReportHelperInterface return $sum; } - - /** - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return Collection - */ - public function getCategoriesWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection - { - /** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */ - $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); - $collection = $repository->earnedForAccountsPerMonth($accounts, $start, $end); - $second = $repository->spentForAccountsPerMonth($accounts, $start, $end); - $collection = $collection->merge($second); - $array = []; - /** @var Category $category */ - foreach ($collection as $category) { - $id = $category->id; - $array[$id] = $category; - - } - $set = new Collection($array); - - return $set; - } }