From 57a3f20c13c248c53d8d5746109e72c867c38a0f Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 27 Jan 2016 21:35:59 +0100 Subject: [PATCH] Moved more code around. --- app/Helpers/Report/BalanceReportHelper.php | 242 ++++++++++++++++++ .../Report/BalanceReportHelperInterface.php | 32 +++ app/Helpers/Report/ReportHelper.php | 196 -------------- app/Helpers/Report/ReportHelperInterface.php | 10 - app/Http/Controllers/ReportController.php | 6 +- app/Providers/FireflyServiceProvider.php | 2 + 6 files changed, 279 insertions(+), 209 deletions(-) create mode 100644 app/Helpers/Report/BalanceReportHelper.php create mode 100644 app/Helpers/Report/BalanceReportHelperInterface.php diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php new file mode 100644 index 0000000000..89223d18d4 --- /dev/null +++ b/app/Helpers/Report/BalanceReportHelper.php @@ -0,0 +1,242 @@ +budgetRepository = $budgetRepository; + $this->tagRepository = $tagRepository; + } + + + /** + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Balance + */ + public function getBalanceReport(Carbon $start, Carbon $end, Collection $accounts) + { + $balance = new Balance; + + // build a balance header: + $header = new BalanceHeader; + $budgets = $this->budgetRepository->getBudgetsAndLimitsInRange($start, $end); + $spentData = $this->budgetRepository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end); + foreach ($accounts as $account) { + $header->addAccount($account); + } + + /** @var BudgetModel $budget */ + foreach ($budgets as $budget) { + $balance->addBalanceLine($this->createBalanceLine($budget, $accounts, $spentData)); + } + + $balance->addBalanceLine($this->createEmptyBalanceLine($accounts, $spentData)); + $balance->addBalanceLine($this->createTagsBalanceLine($accounts, $start, $end)); + $balance->addBalanceLine($this->createDifferenceBalanceLine($accounts, $spentData, $start, $end)); + + $balance->setBalanceHeader($header); + + return $balance; + } + + + /** + * @param Budget $budget + * @param Collection $accounts + * @param Collection $spentData + * + * @return BalanceLine + */ + private function createBalanceLine(BudgetModel $budget, Collection $accounts, Collection $spentData) + { + $line = new BalanceLine; + $line->setBudget($budget); + + // loop accounts: + foreach ($accounts as $account) { + $balanceEntry = new BalanceEntry; + $balanceEntry->setAccount($account); + + // get spent: + $entry = $spentData->filter( + function (TransactionJournal $model) use ($budget, $account) { + return $model->account_id == $account->id && $model->budget_id == $budget->id; + } + ); + $spent = 0; + if (!is_null($entry->first())) { + $spent = $entry->first()->spent; + } + $balanceEntry->setSpent($spent); + $line->addBalanceEntry($balanceEntry); + } + + return $line; + } + + /** + * @param Collection $accounts + * @param Collection $spentData + * @param Carbon $start + * @param Carbon $end + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * + * @return BalanceLine + */ + private function createDifferenceBalanceLine(Collection $accounts, Collection $spentData, Carbon $start, Carbon $end) + { + $diff = new BalanceLine; + $tagsLeft = $this->tagRepository->allCoveredByBalancingActs($accounts, $start, $end); + + $diff->setRole(BalanceLine::ROLE_DIFFROLE); + + foreach ($accounts as $account) { + $entry = $spentData->filter( + function (TransactionJournal $model) use ($account) { + return $model->account_id == $account->id && is_null($model->budget_id); + } + ); + $spent = 0; + if (!is_null($entry->first())) { + $spent = $entry->first()->spent; + } + $leftEntry = $tagsLeft->filter( + function (Tag $tag) use ($account) { + return $tag->account_id == $account->id; + } + ); + $left = 0; + if (!is_null($leftEntry->first())) { + $left = $leftEntry->first()->sum; + } + bcscale(2); + $diffValue = bcadd($spent, $left); + + // difference: + $diffEntry = new BalanceEntry; + $diffEntry->setAccount($account); + $diffEntry->setSpent($diffValue); + $diff->addBalanceEntry($diffEntry); + + } + + return $diff; + } + + /** + * @param Collection $accounts + * @param Collection $spentData + * + * @return BalanceLine + */ + private function createEmptyBalanceLine(Collection $accounts, Collection $spentData) + { + $empty = new BalanceLine; + + foreach ($accounts as $account) { + $entry = $spentData->filter( + function (TransactionJournal $model) use ($account) { + return $model->account_id == $account->id && is_null($model->budget_id); + } + ); + $spent = 0; + if (!is_null($entry->first())) { + $spent = $entry->first()->spent; + } + + // budget + $budgetEntry = new BalanceEntry; + $budgetEntry->setAccount($account); + $budgetEntry->setSpent($spent); + $empty->addBalanceEntry($budgetEntry); + + } + + return $empty; + } + + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return BalanceLine + */ + private function createTagsBalanceLine(Collection $accounts, Carbon $start, Carbon $end) + { + $tags = new BalanceLine; + $tagsLeft = $this->tagRepository->allCoveredByBalancingActs($accounts, $start, $end); + + $tags->setRole(BalanceLine::ROLE_TAGROLE); + + foreach ($accounts as $account) { + $leftEntry = $tagsLeft->filter( + function (Tag $tag) use ($account) { + return $tag->account_id == $account->id; + } + ); + $left = 0; + if (!is_null($leftEntry->first())) { + $left = $leftEntry->first()->sum; + } + bcscale(2); + + // balanced by tags + $tagEntry = new BalanceEntry; + $tagEntry->setAccount($account); + $tagEntry->setLeft($left); + $tags->addBalanceEntry($tagEntry); + + } + + return $tags; + } + +} \ No newline at end of file diff --git a/app/Helpers/Report/BalanceReportHelperInterface.php b/app/Helpers/Report/BalanceReportHelperInterface.php new file mode 100644 index 0000000000..b531014bd6 --- /dev/null +++ b/app/Helpers/Report/BalanceReportHelperInterface.php @@ -0,0 +1,32 @@ +tagRepository = $tagRepository; } - /** - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return Balance - */ - public function getBalanceReport(Carbon $start, Carbon $end, Collection $accounts) - { - $balance = new Balance; - - // build a balance header: - $header = new BalanceHeader; - $budgets = $this->budgetRepository->getBudgetsAndLimitsInRange($start, $end); - $spentData = $this->budgetRepository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end); - foreach ($accounts as $account) { - $header->addAccount($account); - } - - /** @var BudgetModel $budget */ - foreach ($budgets as $budget) { - $balance->addBalanceLine($this->createBalanceLine($budget, $accounts, $spentData)); - } - - $balance->addBalanceLine($this->createEmptyBalanceLine($accounts, $spentData)); - $balance->addBalanceLine($this->createTagsBalanceLine($accounts, $start, $end)); - $balance->addBalanceLine($this->createDifferenceBalanceLine($accounts, $spentData, $start, $end)); - - $balance->setBalanceHeader($header); - - return $balance; - } - /** * This method generates a full report for the given period on all * the users bills and their payments. @@ -273,157 +230,4 @@ class ReportHelper implements ReportHelperInterface return $sum; } - - /** - * @param Budget $budget - * @param Collection $accounts - * @param Collection $spentData - * - * @return BalanceLine - */ - private function createBalanceLine(BudgetModel $budget, Collection $accounts, Collection $spentData) - { - $line = new BalanceLine; - $line->setBudget($budget); - - // loop accounts: - foreach ($accounts as $account) { - $balanceEntry = new BalanceEntry; - $balanceEntry->setAccount($account); - - // get spent: - $entry = $spentData->filter( - function (TransactionJournal $model) use ($budget, $account) { - return $model->account_id == $account->id && $model->budget_id == $budget->id; - } - ); - $spent = 0; - if (!is_null($entry->first())) { - $spent = $entry->first()->spent; - } - $balanceEntry->setSpent($spent); - $line->addBalanceEntry($balanceEntry); - } - - return $line; - } - - /** - * @param Collection $accounts - * @param Collection $spentData - * @param Carbon $start - * @param Carbon $end - * - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - * - * @return BalanceLine - */ - private function createDifferenceBalanceLine(Collection $accounts, Collection $spentData, Carbon $start, Carbon $end) - { - $diff = new BalanceLine; - $tagsLeft = $this->tagRepository->allCoveredByBalancingActs($accounts, $start, $end); - - $diff->setRole(BalanceLine::ROLE_DIFFROLE); - - foreach ($accounts as $account) { - $entry = $spentData->filter( - function (TransactionJournal $model) use ($account) { - return $model->account_id == $account->id && is_null($model->budget_id); - } - ); - $spent = 0; - if (!is_null($entry->first())) { - $spent = $entry->first()->spent; - } - $leftEntry = $tagsLeft->filter( - function (Tag $tag) use ($account) { - return $tag->account_id == $account->id; - } - ); - $left = 0; - if (!is_null($leftEntry->first())) { - $left = $leftEntry->first()->sum; - } - bcscale(2); - $diffValue = bcadd($spent, $left); - - // difference: - $diffEntry = new BalanceEntry; - $diffEntry->setAccount($account); - $diffEntry->setSpent($diffValue); - $diff->addBalanceEntry($diffEntry); - - } - - return $diff; - } - - /** - * @param Collection $accounts - * @param Collection $spentData - * - * @return BalanceLine - */ - private function createEmptyBalanceLine(Collection $accounts, Collection $spentData) - { - $empty = new BalanceLine; - - foreach ($accounts as $account) { - $entry = $spentData->filter( - function (TransactionJournal $model) use ($account) { - return $model->account_id == $account->id && is_null($model->budget_id); - } - ); - $spent = 0; - if (!is_null($entry->first())) { - $spent = $entry->first()->spent; - } - - // budget - $budgetEntry = new BalanceEntry; - $budgetEntry->setAccount($account); - $budgetEntry->setSpent($spent); - $empty->addBalanceEntry($budgetEntry); - - } - - return $empty; - } - - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return BalanceLine - */ - private function createTagsBalanceLine(Collection $accounts, Carbon $start, Carbon $end) - { - $tags = new BalanceLine; - $tagsLeft = $this->tagRepository->allCoveredByBalancingActs($accounts, $start, $end); - - $tags->setRole(BalanceLine::ROLE_TAGROLE); - - foreach ($accounts as $account) { - $leftEntry = $tagsLeft->filter( - function (Tag $tag) use ($account) { - return $tag->account_id == $account->id; - } - ); - $left = 0; - if (!is_null($leftEntry->first())) { - $left = $leftEntry->first()->sum; - } - bcscale(2); - - // balanced by tags - $tagEntry = new BalanceEntry; - $tagEntry->setAccount($account); - $tagEntry->setLeft($left); - $tags->addBalanceEntry($tagEntry); - - } - - return $tags; - } } diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php index b48b34835c..7a1833a230 100644 --- a/app/Helpers/Report/ReportHelperInterface.php +++ b/app/Helpers/Report/ReportHelperInterface.php @@ -3,9 +3,7 @@ namespace FireflyIII\Helpers\Report; use Carbon\Carbon; -use FireflyIII\Helpers\Collection\Balance; use FireflyIII\Helpers\Collection\Bill as BillCollection; -use FireflyIII\Helpers\Collection\Budget as BudgetCollection; use FireflyIII\Helpers\Collection\Category as CategoryCollection; use FireflyIII\Helpers\Collection\Expense; use FireflyIII\Helpers\Collection\Income; @@ -18,14 +16,6 @@ use Illuminate\Support\Collection; */ interface ReportHelperInterface { - /** - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return Balance - */ - public function getBalanceReport(Carbon $start, Carbon $end, Collection $accounts); /** * This method generates a full report for the given period on all diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 40b38c94fb..562ae198b7 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -18,9 +18,8 @@ class ReportController extends Controller protected $accountHelper; - + protected $balanceHelper; protected $budgetHelper; - /** @var ReportHelperInterface */ protected $helper; @@ -36,6 +35,7 @@ class ReportController extends Controller $this->helper = $helper; $this->accountHelper = app('FireflyIII\Helpers\Report\AccountReportHelperInterface'); $this->budgetHelper = app('FireflyIII\Helpers\Report\BudgetReportHelperInterface'); + $this->balanceHelper = app('FireflyIII\Helpers\Report\BalanceReportHelperInterface'); View::share('title', trans('firefly.reports')); View::share('mainTitleIcon', 'fa-line-chart'); @@ -61,7 +61,7 @@ class ReportController extends Controller $expenses = $this->helper->getExpenseReport($start, $end, $accounts); // done (+1) $budgets = $this->budgetHelper->getBudgetReport($start, $end, $accounts); // done (+5) $categories = $this->helper->getCategoryReport($start, $end, $accounts); // done (+1) (20) - $balance = $this->helper->getBalanceReport($start, $end, $accounts); // +566 + $balance = $this->balanceHelper->getBalanceReport($start, $end, $accounts); // +566 $bills = $this->helper->getBillReport($start, $end, $accounts); // and some id's, joined: diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 68b41bb4f1..d8225e4d1a 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -117,6 +117,8 @@ class FireflyServiceProvider extends ServiceProvider // better report helper interfaces: $this->app->bind('FireflyIII\Helpers\Report\AccountReportHelperInterface', 'FireflyIII\Helpers\Report\AccountReportHelper'); + $this->app->bind('FireflyIII\Helpers\Report\BalanceReportHelperInterface', 'FireflyIII\Helpers\Report\BalanceReportHelper'); + $this->app->bind('FireflyIII\Helpers\Report\BudgetReportHelperInterface', 'FireflyIII\Helpers\Report\BudgetReportHelper');