From b019962f347bcbeb5e13de4022659870d97afa4a Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 27 Jan 2016 20:54:14 +0100 Subject: [PATCH] Moved a method. --- app/Helpers/Report/BudgetReportHelper.php | 133 ++++++++++++++++++ .../Report/BudgetReportHelperInterface.php | 32 +++++ app/Helpers/Report/ReportHelper.php | 79 ----------- app/Helpers/Report/ReportHelperInterface.php | 9 -- 4 files changed, 165 insertions(+), 88 deletions(-) create mode 100644 app/Helpers/Report/BudgetReportHelper.php create mode 100644 app/Helpers/Report/BudgetReportHelperInterface.php diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php new file mode 100644 index 0000000000..3983f3c2aa --- /dev/null +++ b/app/Helpers/Report/BudgetReportHelper.php @@ -0,0 +1,133 @@ +getBudgets(); + $allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end); + $allTotalSpent = $repository->spentAllPerDayForAccounts($accounts, $start, $end); + bcscale(2); + + foreach ($set as $budget) { + + $repetitions = $allRepetitions->filter( + function (LimitRepetition $rep) use ($budget) { + return $rep->budget_id == $budget->id; + } + ); + $totalSpent = isset($allTotalSpent[$budget->id]) ? $allTotalSpent[$budget->id] : []; + + // no repetition(s) for this budget: + if ($repetitions->count() == 0) { + $spent = array_sum($totalSpent); + $budgetLine = new BudgetLine; + $budgetLine->setBudget($budget); + $budgetLine->setOverspent($spent); + $object->addOverspent($spent); + $object->addBudgetLine($budgetLine); + continue; + } + + // one or more repetitions for budget: + /** @var LimitRepetition $repetition */ + foreach ($repetitions as $repetition) { + $budgetLine = new BudgetLine; + $budgetLine->setBudget($budget); + $budgetLine->setRepetition($repetition); + $expenses = $this->getSumOfRange($start, $end, $totalSpent); + + // 200 en -100 is 100, vergeleken met 0 === 1 + // 200 en -200 is 0, vergeleken met 0 === 0 + // 200 en -300 is -100, vergeleken met 0 === -1 + + $left = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : 0; + $spent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0'; + $overspent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount); + + $budgetLine->setLeft($left); + $budgetLine->setSpent($spent); + $budgetLine->setOverspent($overspent); + $budgetLine->setBudgeted($repetition->amount); + + $object->addBudgeted($repetition->amount); + $object->addSpent($spent); + $object->addLeft($left); + $object->addOverspent($overspent); + $object->addBudgetLine($budgetLine); + + } + + } + + // stuff outside of budgets: + $noBudget = $repository->getWithoutBudgetSum($start, $end); + $budgetLine = new BudgetLine; + $budgetLine->setOverspent($noBudget); + $budgetLine->setSpent($noBudget); + $object->addOverspent($noBudget); + $object->addBudgetLine($budgetLine); + + return $object; + } + + /** + * Take the array as returned by SingleCategoryRepositoryInterface::spentPerDay and SingleCategoryRepositoryInterface::earnedByDay + * and sum up everything in the array in the given range. + * + * @param Carbon $start + * @param Carbon $end + * @param array $array + * + * @return string + */ + protected function getSumOfRange(Carbon $start, Carbon $end, array $array) + { + bcscale(2); + $sum = '0'; + $currentStart = clone $start; // to not mess with the original one + $currentEnd = clone $end; // to not mess with the original one + + while ($currentStart <= $currentEnd) { + $date = $currentStart->format('Y-m-d'); + if (isset($array[$date])) { + $sum = bcadd($sum, $array[$date]); + } + $currentStart->addDay(); + } + + return $sum; + } +} \ No newline at end of file diff --git a/app/Helpers/Report/BudgetReportHelperInterface.php b/app/Helpers/Report/BudgetReportHelperInterface.php new file mode 100644 index 0000000000..d2600f1e1b --- /dev/null +++ b/app/Helpers/Report/BudgetReportHelperInterface.php @@ -0,0 +1,32 @@ +getBudgets(); - $allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end); - $allTotalSpent = $repository->spentAllPerDayForAccounts($accounts, $start, $end); - bcscale(2); - - foreach ($set as $budget) { - - $repetitions = $allRepetitions->filter( - function (LimitRepetition $rep) use ($budget) { - return $rep->budget_id == $budget->id; - } - ); - $totalSpent = isset($allTotalSpent[$budget->id]) ? $allTotalSpent[$budget->id] : []; - - // no repetition(s) for this budget: - if ($repetitions->count() == 0) { - $spent = array_sum($totalSpent); - $budgetLine = new BudgetLine; - $budgetLine->setBudget($budget); - $budgetLine->setOverspent($spent); - $object->addOverspent($spent); - $object->addBudgetLine($budgetLine); - continue; - } - - // one or more repetitions for budget: - /** @var LimitRepetition $repetition */ - foreach ($repetitions as $repetition) { - $budgetLine = new BudgetLine; - $budgetLine->setBudget($budget); - $budgetLine->setRepetition($repetition); - $expenses = $this->getSumOfRange($start, $end, $totalSpent); - - // 200 en -100 is 100, vergeleken met 0 === 1 - // 200 en -200 is 0, vergeleken met 0 === 0 - // 200 en -300 is -100, vergeleken met 0 === -1 - - $left = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : 0; - $spent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0'; - $overspent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount); - - $budgetLine->setLeft($left); - $budgetLine->setSpent($spent); - $budgetLine->setOverspent($overspent); - $budgetLine->setBudgeted($repetition->amount); - - $object->addBudgeted($repetition->amount); - $object->addSpent($spent); - $object->addLeft($left); - $object->addOverspent($overspent); - $object->addBudgetLine($budgetLine); - - } - - } - - // stuff outside of budgets: - $noBudget = $repository->getWithoutBudgetSum($start, $end); - $budgetLine = new BudgetLine; - $budgetLine->setOverspent($noBudget); - $budgetLine->setSpent($noBudget); - $object->addOverspent($noBudget); - $object->addBudgetLine($budgetLine); - - return $object; - } - /** * @param Carbon $start * @param Carbon $end diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php index 55d95cde75..b48b34835c 100644 --- a/app/Helpers/Report/ReportHelperInterface.php +++ b/app/Helpers/Report/ReportHelperInterface.php @@ -41,15 +41,6 @@ interface ReportHelperInterface */ public function getBillReport(Carbon $start, Carbon $end, Collection $accounts); - /** - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return BudgetCollection - */ - public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts); - /** * @param Carbon $start * @param Carbon $end