Skip to content

Firefly III: a personal finances manager

Moved a method.

James Coleb019962f347b
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 @@
+<?php
+/**
+ * BudgetReportHelper.php
+ * Copyright (C) 2016 Sander Dorigo
+ *
+ * This software may be modified and distributed under the terms
+ * of the MIT license. See the LICENSE file for details.
+ */
+
+namespace FireflyIII\Helpers\Report;
+
+
+use Carbon\Carbon;
+use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
+use FireflyIII\Helpers\Collection\BudgetLine;
+use FireflyIII\Models\LimitRepetition;
+use Illuminate\Support\Collection;
+
+/**
+ * Class BudgetReportHelper
+ *
+ * @package FireflyIII\Helpers\Report
+ */
+class BudgetReportHelper implements BudgetReportHelperInterface
+{
+
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return BudgetCollection
+ */
+ public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts)
+ {
+ $object = new BudgetCollection;
+ /** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
+ $set = $repository->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 @@
+<?php
+/**
+ * BudgetReportHelperInterface.php
+ * Copyright (C) 2016 Sander Dorigo
+ *
+ * This software may be modified and distributed under the terms
+ * of the MIT license. See the LICENSE file for details.
+ */
+
+namespace FireflyIII\Helpers\Report;
+
+
+use Carbon\Carbon;
+use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
+use Illuminate\Support\Collection;
+
+/**
+ * Interface BudgetReportHelperInterface
+ *
+ * @package FireflyIII\Helpers\Report
+ */
+interface BudgetReportHelperInterface
+{
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return BudgetCollection
+ */
+ public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts);
+}
\ No newline at end of file
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php
index d66e8daa73..79e9637566 100644
--- a/app/Helpers/Report/ReportHelper.php
+++ b/app/Helpers/Report/ReportHelper.php
@@ -139,85 +139,6 @@ class ReportHelper implements ReportHelperInterface
return $collection;
}
- /**
- * @param Carbon $start
- * @param Carbon $end
- * @param Collection $accounts
- *
- * @return BudgetCollection
- */
- public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts)
- {
- $object = new BudgetCollection;
- /** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
- $repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
- $set = $repository->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