Refactoring.

This commit is contained in:
James Cole
2016-05-14 14:02:12 +02:00
parent 863227c55c
commit 6090efe2df

View File

@@ -6,10 +6,11 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface; use FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface;
use FireflyIII\Helpers\Report\ReportQueryInterface;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\CacheProperties; use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Navigation;
use Response; use Response;
use Steam; use Steam;
@@ -81,18 +82,15 @@ class ReportController extends Controller
/** /**
* Summarizes all income and expenses, per month, for a given year. * @param AccountRepositoryInterface $repository
* * @param string $reportType
* @param ReportQueryInterface $query * @param Carbon $start
* @param $reportType * @param Carbon $end
* @param Carbon $start * @param Collection $accounts
* @param Carbon $end
* @param Collection $accounts
*
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function yearInOut(ReportQueryInterface $query, string $reportType, Carbon $start, Carbon $end, Collection $accounts) public function yearInOut(AccountRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{ {
// chart properties for cache: // chart properties for cache:
$cache = new CacheProperties; $cache = new CacheProperties;
@@ -102,13 +100,22 @@ class ReportController extends Controller
$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());
} }
// spent per month, and earned per month. For a specific set of accounts // always per month.
// grouped by month $currentStart = clone $start;
$spentArray = $query->spentPerMonth($accounts, $start, $end); $spentArray = [];
$earnedArray = $query->earnedPerMonth($accounts, $start, $end); $earnedArray = [];
while ($currentStart <= $end) {
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
$date = $currentStart->format('Y-m');
$spent = $repository->spentInPeriod($accounts, $currentStart, $currentEnd);
$earned = $repository->earnedInPeriod($accounts, $currentStart, $currentEnd);
$spentArray[$date] = $spent;
$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
@@ -125,17 +132,15 @@ class ReportController extends Controller
} }
/** /**
* Summarizes all income and expenses for a given year. Gives a total and an average. * @param AccountRepositoryInterface $repository
* * @param string $reportType
* @param ReportQueryInterface $query * @param Carbon $start
* @param $reportType * @param Carbon $end
* @param Carbon $start * @param Collection $accounts
* @param Carbon $end
* @param Collection $accounts
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function yearInOutSummarized(ReportQueryInterface $query, string $reportType, Carbon $start, Carbon $end, Collection $accounts) public function yearInOutSummarized(AccountRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{ {
// chart properties for cache: // chart properties for cache:
@@ -148,10 +153,22 @@ class ReportController extends Controller
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); return Response::json($cache->get());
} }
// spent per month, and earned per month. For a specific set of accounts
// grouped by month // always per month.
$spentArray = $query->spentPerMonth($accounts, $start, $end); $currentStart = clone $start;
$earnedArray = $query->earnedPerMonth($accounts, $start, $end); $spentArray = [];
$earnedArray = [];
while ($currentStart <= $end) {
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
$date = $currentStart->format('Y-m');
$spent = $repository->spentInPeriod($accounts, $currentStart, $currentEnd);
$earned = $repository->earnedInPeriod($accounts, $currentStart, $currentEnd);
$spentArray[$date] = $spent;
$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($earnedArray, $spentArray, $start, $end);
@@ -253,8 +270,8 @@ class ReportController extends Controller
while ($start < $end) { while ($start < $end) {
// total income and total expenses: // total income and total expenses:
$date = $start->format('Y-m'); $date = $start->format('Y-m');
$incomeSum = $earned[$date] ?? 0; $incomeSum = isset($earned[$date]) ? $earned[$date] * -1 : 0;
$expenseSum = isset($spent[$date]) ? ($spent[$date] * -1) : 0; $expenseSum = isset($spent[$date]) ? $spent[$date] * -1 : 0;
$entries->push([clone $start, $incomeSum, $expenseSum]); $entries->push([clone $start, $incomeSum, $expenseSum]);
$start->addMonth(); $start->addMonth();
@@ -281,7 +298,7 @@ class ReportController extends Controller
while ($start < $end) { while ($start < $end) {
$date = $start->format('Y-m'); $date = $start->format('Y-m');
$currentIncome = $earned[$date] ?? '0'; $currentIncome = $earned[$date] ?? '0';
$currentExpense = isset($spent[$date]) ? bcmul($spent[$date], '-1') : '0'; $currentExpense = $spent[$date] ??'0';
$income = bcadd($income, $currentIncome); $income = bcadd($income, $currentIncome);
$expense = bcadd($expense, $currentExpense); $expense = bcadd($expense, $currentExpense);