Changed to budget controller chart.

This commit is contained in:
James Cole 2016-12-29 17:42:46 +01:00
parent fab511cc53
commit 7a3b39886e

View File

@ -31,6 +31,8 @@ use Response;
/** /**
* Class BudgetController * Class BudgetController
* *
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) // can't realy be helped.
*
* @package FireflyIII\Http\Controllers\Chart * @package FireflyIII\Http\Controllers\Chart
*/ */
class BudgetController extends Controller class BudgetController extends Controller
@ -73,7 +75,6 @@ class BudgetController extends Controller
$final = clone $last; $final = clone $last;
$final->addYears(2); $final->addYears(2);
$budgetCollection = new Collection([$budget]); $budgetCollection = new Collection([$budget]);
$last = Navigation::endOfX($last, $range, $final); // not to overshoot. $last = Navigation::endOfX($last, $range, $final); // not to overshoot.
$entries = []; $entries = [];
@ -100,6 +101,7 @@ class BudgetController extends Controller
/** /**
* Shows the amount left in a specific budget limit. * Shows the amount left in a specific budget limit.
* *
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five.
* @param BudgetRepositoryInterface $repository * @param BudgetRepositoryInterface $repository
* @param Budget $budget * @param Budget $budget
* @param LimitRepetition $repetition * @param LimitRepetition $repetition
@ -139,6 +141,8 @@ class BudgetController extends Controller
/** /**
* Shows a budget list with spent/left/overspent. * Shows a budget list with spent/left/overspent.
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five.
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) // 46 lines, I'm fine with this.
* *
* @param BudgetRepositoryInterface $repository * @param BudgetRepositoryInterface $repository
* *
@ -159,55 +163,30 @@ class BudgetController extends Controller
$budgets = $repository->getActiveBudgets(); $budgets = $repository->getActiveBudgets();
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end); $repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$chartData = [ $chartData = [
[ ['label' => strval(trans('firefly.spent_in_budget')), 'entries' => [], 'type' => 'bar',],
'label' => strval(trans('firefly.spent_in_budget')), ['label' => strval(trans('firefly.left_to_spend')), 'entries' => [], 'type' => 'bar',],
'entries' => [], ['label' => strval(trans('firefly.overspent')), 'entries' => [], 'type' => 'bar',],
'type' => 'bar',
],
[
'label' => strval(trans('firefly.left_to_spend')),
'entries' => [],
'type' => 'bar',
],
[
'label' => strval(trans('firefly.overspent')),
'entries' => [],
'type' => 'bar',
],
]; ];
/** @var Budget $budget */ /** @var Budget $budget */
foreach ($budgets as $budget) { foreach ($budgets as $budget) {
// get relevant repetitions: // get relevant repetitions:
$reps = $this->filterRepetitions($repetitions, $budget, $start, $end); $filtered = $this->filterRepetitions($repetitions, $budget, $start, $end);
$expenses = $this->getExpensesForBudget($filtered, $budget, $start, $end);
if ($reps->count() === 0) { foreach ($expenses as $name => $row) {
$row = $this->spentInPeriodSingle($repository, $budget, $start, $end); $chartData[0]['entries'][$name] = $row['spent'];
if (bccomp($row['spent'], '0') !== 0 || bccomp($row['repetition_left'], '0') !== 0) { $chartData[1]['entries'][$name] = $row['left'];
$chartData[0]['entries'][$row['name']] = bcmul($row['spent'], '-1'); $chartData[2]['entries'][$name] = $row['overspent'];
$chartData[1]['entries'][$row['name']] = $row['repetition_left'];
$chartData[2]['entries'][$row['name']] = bcmul($row['repetition_overspent'], '-1');
}
continue;
} }
$rows = $this->spentInPeriodMulti($repository, $budget, $reps);
foreach ($rows as $row) {
if (bccomp($row['spent'], '0') !== 0 || bccomp($row['repetition_left'], '0') !== 0) {
$chartData[0]['entries'][$row['name']] = bcmul($row['spent'], '-1');
$chartData[1]['entries'][$row['name']] = $row['repetition_left'];
$chartData[2]['entries'][$row['name']] = bcmul($row['repetition_overspent'], '-1');
}
}
unset($rows, $row);
} }
// for no budget: // for no budget:
$row = $this->spentInPeriodWithout($start, $end); $spent = $this->spentInPeriodWithout($start, $end);
if (bccomp($row['repetition_overspent'], '0') !== 0) { $name = strval(trans('firefly.no_budget'));
$chartData[0]['entries'][$row['name']] = bcmul($row['spent'], '-1'); if (bccomp($spent, '0') !== 0) {
$chartData[1]['entries'][$row['name']] = $row['repetition_left']; $chartData[0]['entries'][$name] = bcmul($spent, '-1');
$chartData[2]['entries'][$row['name']] = bcmul($row['repetition_overspent'], '-1'); $chartData[1]['entries'][$name] = '0';
$chartData[2]['entries'][$name] = '0';
} }
$data = $this->generator->multiSet($chartData); $data = $this->generator->multiSet($chartData);
@ -252,6 +231,7 @@ class BudgetController extends Controller
while ($current < $end) { while ($current < $end) {
$currentStart = Navigation::startOfPeriod($current, $range); $currentStart = Navigation::startOfPeriod($current, $range);
$currentEnd = Navigation::endOfPeriod($current, $range); $currentEnd = Navigation::endOfPeriod($current, $range);
$reps = $repetitions->filter( $reps = $repetitions->filter(
function (LimitRepetition $repetition) use ($budget, $currentStart, $currentEnd) { function (LimitRepetition $repetition) use ($budget, $currentStart, $currentEnd) {
if ($repetition->budget_id === $budget->id && $repetition->startdate >= $currentStart && $repetition->enddate <= $currentEnd) { if ($repetition->budget_id === $budget->id && $repetition->startdate >= $currentStart && $repetition->enddate <= $currentEnd) {
@ -354,12 +334,53 @@ class BudgetController extends Controller
); );
} }
/**
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's 6 but ok.
*
* @param Collection $repetitions
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
private function getExpensesForBudget(Collection $repetitions, Budget $budget, Carbon $start, Carbon $end): array
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$return = [];
if ($repetitions->count() === 0) {
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
if (bccomp($spent, '0') !== 0) {
$return[$budget->name]['spent'] = $spent;
$return[$budget->name]['left'] = 0;
$return[$budget->name]['overspent'] = 0;
}
return $return;
}
$rows = $this->spentInPeriodMulti($repository, $budget, $repetitions);
foreach ($rows as $name => $row) {
if (bccomp($row['spent'], '0') !== 0 || bccomp($row['left'], '0') !== 0) {
$return[$name]['spent'] = bcmul($row['spent'], '-1');
$return[$name]['left'] = $row['left'];
$return[$name]['overspent'] = bcmul($row['overspent'], '-1');
}
}
unset($rows, $row);
return $return;
}
/** /**
* Returns an array with the following values: * Returns an array with the following values:
* 0 => * 0 =>
* 'name' => name of budget + repetition * 'name' => name of budget + repetition
* 'repetition_left' => left in budget repetition (always zero) * 'left' => left in budget repetition (always zero)
* 'repetition_overspent' => spent more than budget repetition? (always zero) * 'overspent' => spent more than budget repetition? (always zero)
* 'spent' => actually spent in period for budget * 'spent' => actually spent in period for budget
* 1 => (etc) * 1 => (etc)
* *
@ -384,49 +405,20 @@ class BudgetController extends Controller
['start' => $repetition->startdate->formatLocalized($format), 'end' => $repetition->enddate->formatLocalized($format)] ['start' => $repetition->startdate->formatLocalized($format), 'end' => $repetition->enddate->formatLocalized($format)]
); );
} }
$amount = $repetition->amount; $amount = $repetition->amount;
$left = bccomp(bcadd($amount, $expenses), '0') < 1 ? '0' : bcadd($amount, $expenses); $left = bccomp(bcadd($amount, $expenses), '0') < 1 ? '0' : bcadd($amount, $expenses);
$spent = $expenses; $spent = $expenses;
$overspent = bccomp(bcadd($amount, $expenses), '0') < 1 ? bcadd($amount, $expenses) : '0'; $overspent = bccomp(bcadd($amount, $expenses), '0') < 1 ? bcadd($amount, $expenses) : '0';
$return[] = [ $return[$name] = [
'name' => $name, 'left' => $left,
'repetition_left' => $left, 'overspent' => $overspent,
'repetition_overspent' => $overspent, 'spent' => $spent,
'spent' => $spent,
]; ];
} }
return $return; return $return;
} }
/**
* Returns an array with the following values:
* 'name' => name of budget
* 'repetition_left' => left in budget repetition (always zero)
* 'repetition_overspent' => spent more than budget repetition? (always zero)
* 'spent' => actually spent in period for budget
*
*
* @param BudgetRepositoryInterface $repository
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
private function spentInPeriodSingle(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end): array
{
$spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
$array = [
'name' => $budget->name,
'repetition_left' => '0',
'repetition_overspent' => '0',
'spent' => $spent,
];
return $array;
}
/** /**
* Returns an array with the following values: * Returns an array with the following values:
* 'name' => "no budget" in local language * 'name' => "no budget" in local language
@ -437,9 +429,9 @@ class BudgetController extends Controller
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return array * @return string
*/ */
private function spentInPeriodWithout(Carbon $start, Carbon $end): array private function spentInPeriodWithout(Carbon $start, Carbon $end): string
{ {
// collector // collector
/** @var JournalCollectorInterface $collector */ /** @var JournalCollectorInterface $collector */
@ -452,13 +444,7 @@ class BudgetController extends Controller
foreach ($journals as $entry) { foreach ($journals as $entry) {
$sum = bcadd($entry->transaction_amount, $sum); $sum = bcadd($entry->transaction_amount, $sum);
} }
$array = [
'name' => strval(trans('firefly.no_budget')),
'repetition_left' => '0',
'repetition_overspent' => $sum,
'spent' => '0',
];
return $array; return $sum;
} }
} }