From 80350f84239276a0a6f00117ac5f2112c0bf3cad Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 30 Apr 2016 22:30:11 +0200 Subject: [PATCH] Fix for transactions. --- .../Controllers/Chart/CategoryController.php | 112 +++++++++--------- .../Category/CategoryRepository.php | 3 + 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index 892464b254..1298c0ea0f 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -270,6 +270,62 @@ class CategoryController extends Controller } + /** + * @param Category $category + * @param string $reportType + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return \Illuminate\Http\JsonResponse + */ + public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts) + { + // chart properties for cache: + $cache = new CacheProperties(); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty($reportType); + $cache->addProperty($accounts); + $cache->addProperty($category->id); + $cache->addProperty('category'); + $cache->addProperty('period'); + if ($cache->has()) { + return Response::json($cache->get()); + } + + /** @var SingleCategoryRepositoryInterface $repository */ + $repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface'); + // loop over period, add by users range: + $current = clone $start; + $viewRange = Preferences::get('viewRange', '1M')->data; + $format = strval(trans('config.month')); + $set = new Collection; + while ($current < $end) { + $currentStart = clone $current; + $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange); + + $spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, $accounts))); + $earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, $accounts))); + + $entry = [ + $category->name, + $currentStart->formatLocalized($format), + $spent, + $earned, + + ]; + $set->push($entry); + $currentEnd->addDay(); + $current = clone $currentEnd; + } + $data = $this->generator->period($set); + $cache->store($data); + + return Response::json($data); + + } + /** * @param SCRI $repository * @param Category $category @@ -435,60 +491,4 @@ class CategoryController extends Controller return $data; } - /** - * @param Category $category - * @param string $reportType - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return \Illuminate\Http\JsonResponse - */ - public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts) - { - // chart properties for cache: - $cache = new CacheProperties(); - $cache->addProperty($start); - $cache->addProperty($end); - $cache->addProperty($reportType); - $cache->addProperty($accounts); - $cache->addProperty($category->id); - $cache->addProperty('category'); - $cache->addProperty('period'); - if ($cache->has()) { - return Response::json($cache->get()); - } - - /** @var SingleCategoryRepositoryInterface $repository */ - $repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface'); - // loop over period, add by users range: - $current = clone $start; - $viewRange = Preferences::get('viewRange', '1M')->data; - $format = strval(trans('config.month')); - $set = new Collection; - while ($current < $end) { - $currentStart = clone $current; - $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange); - - $spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, $accounts))); - $earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, $accounts))); - - $entry = [ - $category->name, - $currentStart->formatLocalized($format), - $spent, - $earned, - - ]; - $set->push($entry); - $currentEnd->addDay(); - $current = clone $currentEnd; - } - $data = $this->generator->period($set); - $cache->store($data); - - return Response::json($data); - - } - } diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index 0575a2b665..0b8209b76d 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -284,7 +284,9 @@ class CategoryRepository implements CategoryRepositoryInterface ->before($end) ->after($start) ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->having('transaction_count', '=', 1) ->transactionTypes($types); + if (count($accountIds) > 0) { $query->whereIn('transactions.account_id', $accountIds); } @@ -293,6 +295,7 @@ class CategoryRepository implements CategoryRepositoryInterface $single = $query->first( [ DB::raw('SUM(`transactions`.`amount`) as `sum`'), + DB::raw('COUNT(`transactions`.`id`) as `transaction_count`'), ] ); if (!is_null($single)) {