From 512ce159734031b26627518197d1097134bc362f Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 16 May 2015 10:13:41 +0200 Subject: [PATCH] Small tweaks in charts. --- .../Controllers/Chart/CategoryController.php | 76 +++++++++---------- .../Category/CategoryRepository.php | 30 +++++++- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index aa6b781c1e..5aa5ed4592 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -25,6 +25,44 @@ class CategoryController extends Controller { + /** + * Show an overview for a category for all time, per month/week/year. + * + * @param GChart $chart + * @param CategoryRepositoryInterface $repository + * @param Category $category + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function all(GChart $chart, CategoryRepositoryInterface $repository, Category $category) + { + // oldest transaction in category: + $start = $repository->getFirstActivityDate($category); + $range = Preferences::get('viewRange', '1M')->data; + // jump to start of week / month / year / etc + $start = Navigation::startOfPeriod($start, $range); + + $chart->addColumn(trans('firefly.period'), 'date'); + $chart->addColumn(trans('firefly.spent'), 'number'); + + + $end = new Carbon; + while ($start <= $end) { + + $currentEnd = Navigation::endOfPeriod($start, $range); + $spent = $repository->spentInPeriod($category, $start, $currentEnd); + $chart->addRow(clone $start, $spent); + + $start = Navigation::addPeriod($start, $range, 0); + } + + $chart->generate(); + + return Response::json($chart->getData()); + + + } + /** * Show this month's category overview. * @@ -81,44 +119,6 @@ class CategoryController extends Controller return Response::json($chart->getData()); - } - - /** - * Show an overview for a category for all time, per month/week/year. - * - * @param GChart $chart - * @param CategoryRepositoryInterface $repository - * @param Category $category - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function all(GChart $chart, CategoryRepositoryInterface $repository, Category $category) - { - // oldest transaction in category: - $start = $repository->getFirstActivityDate($category); - - $range = Preferences::get('viewRange', '1M')->data; - // jump to start of week / month / year / etc - $start = Navigation::startOfPeriod($start, $range); - - $chart->addColumn(trans('firefly.period'), 'date'); - $chart->addColumn(trans('firefly.spent'), 'number'); - - $end = new Carbon; - while ($start <= $end) { - - $currentEnd = Navigation::endOfPeriod($start, $range); - $spent = $repository->spentInPeriod($category, $start, $currentEnd); - $chart->addRow(clone $start, $spent); - - $start = Navigation::addPeriod($start, $range, 0); - } - - $chart->generate(); - - return Response::json($chart->getData()); - - } /** diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index b81b2b4dd5..55b6f43cb3 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -172,12 +172,34 @@ class CategoryRepository implements CategoryRepositoryInterface */ public function spentInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false) { - if($shared === false) { - // do something else, SEE budgets. - $sum = floatval($category->transactionjournals()->before($end)->after($start)->lessThan(0)->sum('amount')) * -1; + if ($shared === true) { + // shared is true. + // always ignore transfers between accounts! + $sum = floatval( + $category->transactionjournals() + ->transactionTypes(['Withdrawal', 'Deposit']) + ->before($end)->after($start)->lessThan(0)->sum('amount') + ) * -1; + } else { - $sum = floatval($category->transactionjournals()->before($end)->after($start)->lessThan(0)->sum('amount')) * -1; + // do something else, SEE budgets. + // get all journals in this month where the asset account is NOT shared. + $sum = $category->transactionjournals() + ->before($end) + ->after($start) + ->transactionTypes(['Withdrawal', 'Deposit']) + ->lessThan(0) + ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') + ->leftJoin( + 'account_meta', function (JoinClause $join) { + $join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole'); + } + ) + ->where('account_meta.data', '!=', '"sharedAsset"') + ->sum('amount'); + $sum = floatval($sum) * -1; } + return $sum; }