Removed some duplicate code.

This commit is contained in:
James Cole
2016-11-12 19:22:03 +01:00
parent da49afa37b
commit 98d6c90e90
3 changed files with 68 additions and 55 deletions

View File

@@ -92,52 +92,36 @@ class ReportController extends Controller
/** /**
* @param AccountTaskerInterface $accountTasker
* @param string $reportType
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param Collection $accounts * @param Collection $accounts
* *
* @return \Illuminate\Http\JsonResponse * @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: // chart properties for cache:
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty('yearInOut'); $cache->addProperty('yearInOut');
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($reportType);
$cache->addProperty($accounts); $cache->addProperty($accounts);
$cache->addProperty($end); $cache->addProperty($end);
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); return Response::json($cache->get());
} }
// always per month. $chartSource = $this->getYearData($accounts, $start, $end);
$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);
}
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end) > 12) {
// data = method X // data = method X
$data = $this->multiYearInOut($earnedArray, $spentArray, $start, $end); $data = $this->multiYearInOut($chartSource['earned'], $chartSource['spent'], $start, $end);
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
} }
// data = method Y // data = method Y
$data = $this->singleYearInOut($earnedArray, $spentArray, $start, $end); $data = $this->singleYearInOut($chartSource['earned'], $chartSource['spent'], $start, $end);
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
@@ -146,8 +130,6 @@ class ReportController extends Controller
} }
/** /**
* @param AccountTaskerInterface $accountTasker
* @param string $reportType
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param Collection $accounts * @param Collection $accounts
@@ -155,7 +137,7 @@ class ReportController extends Controller
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* @internal param AccountRepositoryInterface $repository * @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: // chart properties for cache:
@@ -163,35 +145,21 @@ class ReportController extends Controller
$cache->addProperty('yearInOutSummarized'); $cache->addProperty('yearInOutSummarized');
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($end); $cache->addProperty($end);
$cache->addProperty($reportType);
$cache->addProperty($accounts); $cache->addProperty($accounts);
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); return Response::json($cache->get());
} }
$chartSource = $this->getYearData($accounts, $start, $end);
// 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);
}
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end) > 12) {
// per year // per year
$data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end); $data = $this->multiYearInOutSummarized($chartSource['earned'], $chartSource['spent'], $start, $end);
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
} }
// per month! // per month!
$data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end); $data = $this->singleYearInOutSummarized($chartSource['earned'], $chartSource['spent'], $start, $end);
$cache->store($data); $cache->store($data);
return Response::json($data); return Response::json($data);
@@ -342,4 +310,33 @@ class ReportController extends Controller
return $sum; 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,
];
}
} }

View File

@@ -14,8 +14,8 @@ function drawChart() {
"use strict"; "use strict";
lineChart('chart/report/net-worth/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'net-worth'); 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/' + 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-sum/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-sum-chart');
} }

View File

@@ -209,22 +209,38 @@ Route::group(
Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']); Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']);
// these charts are used in reports (category reports): // these charts are used in reports (category reports):
Route::get('/chart/category/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/income', Route::get(
['uses' => 'Chart\CategoryReportController@categoryIncome']); '/chart/category/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/income',
Route::get('/chart/category/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/expense', ['uses' => 'Chart\CategoryReportController@categoryIncome']
['uses' => 'Chart\CategoryReportController@categoryExpense']); );
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', Route::get(
['uses' => 'Chart\CategoryReportController@accountIncome']); '/chart/account/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/income',
Route::get('/chart/account/{accountList}/{categoryList}/{start_date}/{end_date}/{others}/expense', ['uses' => 'Chart\CategoryReportController@accountIncome']
['uses' => 'Chart\CategoryReportController@accountExpense']); );
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: // piggy banks:
Route::get('/chart/piggy-bank/{piggyBank}', ['uses' => 'Chart\PiggyBankController@history']); Route::get('/chart/piggy-bank/{piggyBank}', ['uses' => 'Chart\PiggyBankController@history']);
// reports: // reports:
Route::get('/chart/report/in-out/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOut']); Route::get('/chart/report/in-out/{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-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']); Route::get('/chart/report/net-worth/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@netWorth']);