From 98d6c90e90f55de3f576466c853066a832c07b45 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 12 Nov 2016 19:22:03 +0100 Subject: [PATCH] Removed some duplicate code. --- .../Controllers/Chart/ReportController.php | 83 +++++++++---------- public/js/ff/reports/default/year.js | 4 +- routes/web.php | 36 +++++--- 3 files changed, 68 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 941d66da86..c8fecbdad2 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -92,52 +92,36 @@ class ReportController extends Controller /** - * @param AccountTaskerInterface $accountTasker - * @param string $reportType - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse - * @internal param AccountRepositoryInterface $repository */ - public function yearInOut(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts) + public function yearInOut(Carbon $start, Carbon $end, Collection $accounts) { // chart properties for cache: $cache = new CacheProperties; $cache->addProperty('yearInOut'); $cache->addProperty($start); - $cache->addProperty($reportType); $cache->addProperty($accounts); $cache->addProperty($end); if ($cache->has()) { return Response::json($cache->get()); } - // always per month. - $currentStart = clone $start; - $spentArray = []; - $earnedArray = []; - while ($currentStart <= $end) { - $currentEnd = Navigation::endOfPeriod($currentStart, '1M'); - $date = $currentStart->format('Y-m'); - $spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd); - $earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd); - $spentArray[$date] = bcmul($spent, '-1'); - $earnedArray[$date] = $earned; - $currentStart = Navigation::addPeriod($currentStart, '1M', 0); - } + $chartSource = $this->getYearData($accounts, $start, $end); if ($start->diffInMonths($end) > 12) { // data = method X - $data = $this->multiYearInOut($earnedArray, $spentArray, $start, $end); + $data = $this->multiYearInOut($chartSource['earned'], $chartSource['spent'], $start, $end); $cache->store($data); return Response::json($data); } // data = method Y - $data = $this->singleYearInOut($earnedArray, $spentArray, $start, $end); + $data = $this->singleYearInOut($chartSource['earned'], $chartSource['spent'], $start, $end); $cache->store($data); return Response::json($data); @@ -146,8 +130,6 @@ class ReportController extends Controller } /** - * @param AccountTaskerInterface $accountTasker - * @param string $reportType * @param Carbon $start * @param Carbon $end * @param Collection $accounts @@ -155,7 +137,7 @@ class ReportController extends Controller * @return \Illuminate\Http\JsonResponse * @internal param AccountRepositoryInterface $repository */ - public function yearInOutSummarized(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts) + public function yearInOutSummarized(Carbon $start, Carbon $end, Collection $accounts) { // chart properties for cache: @@ -163,35 +145,21 @@ class ReportController extends Controller $cache->addProperty('yearInOutSummarized'); $cache->addProperty($start); $cache->addProperty($end); - $cache->addProperty($reportType); $cache->addProperty($accounts); if ($cache->has()) { return Response::json($cache->get()); } - - // always per month. - $currentStart = clone $start; - $spentArray = []; - $earnedArray = []; - while ($currentStart <= $end) { - $currentEnd = Navigation::endOfPeriod($currentStart, '1M'); - $date = $currentStart->format('Y-m'); - $spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd); - $earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd); - $spentArray[$date] = bcmul($spent, '-1'); - $earnedArray[$date] = $earned; - $currentStart = Navigation::addPeriod($currentStart, '1M', 0); - } + $chartSource = $this->getYearData($accounts, $start, $end); if ($start->diffInMonths($end) > 12) { // per year - $data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end); + $data = $this->multiYearInOutSummarized($chartSource['earned'], $chartSource['spent'], $start, $end); $cache->store($data); return Response::json($data); } // per month! - $data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end); + $data = $this->singleYearInOutSummarized($chartSource['earned'], $chartSource['spent'], $start, $end); $cache->store($data); return Response::json($data); @@ -342,4 +310,33 @@ class ReportController extends Controller return $sum; } + + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + private function getYearData(Collection $accounts, Carbon $start, Carbon $end): array + { + $tasker = app(AccountTaskerInterface::class); + $currentStart = clone $start; + $spentArray = []; + $earnedArray = []; + while ($currentStart <= $end) { + $currentEnd = Navigation::endOfPeriod($currentStart, '1M'); + $date = $currentStart->format('Y-m'); + $spent = $tasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd); + $earned = $tasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd); + $spentArray[$date] = bcmul($spent, '-1'); + $earnedArray[$date] = $earned; + $currentStart = Navigation::addPeriod($currentStart, '1M', 0); + } + + return [ + 'spent' => $spentArray, + 'earned' => $earnedArray, + ]; + } } diff --git a/public/js/ff/reports/default/year.js b/public/js/ff/reports/default/year.js index 0609105bf6..1e2893f8e5 100644 --- a/public/js/ff/reports/default/year.js +++ b/public/js/ff/reports/default/year.js @@ -14,8 +14,8 @@ function drawChart() { "use strict"; lineChart('chart/report/net-worth/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'net-worth'); - columnChart('chart/report/in-out/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-chart'); - columnChart('chart/report/in-out-sum/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-sum-chart'); + columnChart('chart/report/in-out/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-chart'); + columnChart('chart/report/in-out-sum/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-sum-chart'); } diff --git a/routes/web.php b/routes/web.php index 795b82a17d..bc61e74842 100755 --- a/routes/web.php +++ b/routes/web.php @@ -209,22 +209,38 @@ Route::group( Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']); // these charts are used in reports (category reports): - Route::get('/chart/category/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/income', - ['uses' => 'Chart\CategoryReportController@categoryIncome']); - Route::get('/chart/category/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/expense', - ['uses' => 'Chart\CategoryReportController@categoryExpense']); + Route::get( + '/chart/category/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/income', + ['uses' => 'Chart\CategoryReportController@categoryIncome'] + ); + Route::get( + '/chart/category/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/expense', + ['uses' => 'Chart\CategoryReportController@categoryExpense'] + ); - Route::get('/chart/account/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/income', - ['uses' => 'Chart\CategoryReportController@accountIncome']); - Route::get('/chart/account/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/expense', - ['uses' => 'Chart\CategoryReportController@accountExpense']); + Route::get( + '/chart/account/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/income', + ['uses' => 'Chart\CategoryReportController@accountIncome'] + ); + Route::get( + '/chart/account/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/expense', + ['uses' => 'Chart\CategoryReportController@accountExpense'] + ); + Route::get( + '/chart/category-report-income/{accountList}/{categoryList}/{start_date}/{end_date}', + ['uses' => 'Chart\CategoryReportController@mainIncomeChart'] + ); + Route::get( + '/chart/category-report-expenses/{accountList}/{categoryList}/{start_date}/{end_date}', + ['uses' => 'Chart\CategoryReportController@mainExpenseChart'] + ); // piggy banks: Route::get('/chart/piggy-bank/{piggyBank}', ['uses' => 'Chart\PiggyBankController@history']); // reports: - Route::get('/chart/report/in-out/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOut']); - Route::get('/chart/report/in-out-sum/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOutSummarized']); + Route::get('/chart/report/in-out/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOut']); + Route::get('/chart/report/in-out-sum/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOutSummarized']); Route::get('/chart/report/net-worth/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@netWorth']);