From 4872d3e4bc3bfe569638a7849d77dc2ff927bd64 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 3 Sep 2019 11:55:06 +0200 Subject: [PATCH] Clean up and add some todo's --- .../Report/Budget/MonthReportGenerator.php | 1 + .../Report/Category/MonthReportGenerator.php | 140 +++++++----------- 2 files changed, 55 insertions(+), 86 deletions(-) diff --git a/app/Generator/Report/Budget/MonthReportGenerator.php b/app/Generator/Report/Budget/MonthReportGenerator.php index ab9832c1e2..33418683ab 100644 --- a/app/Generator/Report/Budget/MonthReportGenerator.php +++ b/app/Generator/Report/Budget/MonthReportGenerator.php @@ -35,6 +35,7 @@ use Throwable; /** * Class MonthReportGenerator. + * TODO include info about tags. * * @codeCoverageIgnore */ diff --git a/app/Generator/Report/Category/MonthReportGenerator.php b/app/Generator/Report/Category/MonthReportGenerator.php index ca08e03c59..5646ddee3e 100644 --- a/app/Generator/Report/Category/MonthReportGenerator.php +++ b/app/Generator/Report/Category/MonthReportGenerator.php @@ -35,6 +35,7 @@ use Throwable; /** * Class MonthReportGenerator. + * TODO include info about tags. * * @codeCoverageIgnore */ @@ -69,26 +70,13 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface */ public function generate(): string { - $accountIds = implode(',', $this->accounts->pluck('id')->toArray()); - $categoryIds = implode(',', $this->categories->pluck('id')->toArray()); - $reportType = 'category'; - $expenses = $this->getExpenses(); - $income = $this->getIncome(); - $accountSummary = $this->getObjectSummary($this->summarizeByAssetAccount($expenses), $this->summarizeByAssetAccount($income)); - $categorySummary = $this->getObjectSummary($this->summarizeByCategory($expenses), $this->summarizeByCategory($income)); - $averageExpenses = $this->getAverages($expenses, SORT_ASC); - $averageIncome = $this->getAverages($income, SORT_DESC); - $topExpenses = $this->getTopExpenses(); - $topIncome = $this->getTopIncome(); + $accountIds = implode(',', $this->accounts->pluck('id')->toArray()); + $categoryIds = implode(',', $this->categories->pluck('id')->toArray()); + $reportType = 'category'; // render! try { - return view( - 'reports.category.month', compact( - 'accountIds', 'categoryIds', 'topIncome', 'reportType', 'accountSummary', 'categorySummary', 'averageExpenses', - 'averageIncome', 'topExpenses' - ) - ) + return view('reports.category.month', compact('accountIds', 'categoryIds', 'reportType',)) ->with('start', $this->start)->with('end', $this->end) ->with('categories', $this->categories) ->with('accounts', $this->accounts) @@ -101,75 +89,6 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface return $result; } - /** - * Get the expenses for this report. - * - * @return array - */ - protected function getExpenses(): array - { - if (count($this->expenses) > 0) { - Log::debug('Return previous set of expenses.'); - - return $this->expenses; - } - - /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); - $collector->setAccounts($this->accounts)->setRange($this->start, $this->end) - ->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]) - ->setCategories($this->categories)->withAccountInformation(); - - $transactions = $collector->getExtractedJournals(); - $this->expenses = $transactions; - - return $transactions; - } - - /** - * Get the income for this report. - * - * @return array - */ - protected function getIncome(): array - { - if (count($this->income) > 0) { - return $this->income; - } - - /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); - - $collector->setAccounts($this->accounts)->setRange($this->start, $this->end) - ->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]) - ->setCategories($this->categories)->withAccountInformation(); - - $transactions = $collector->getExtractedJournals(); - $this->income = $transactions; - - return $transactions; - } - - /** - * Summarize the category. - * - * @param array $array - * - * @return array - */ - private function summarizeByCategory(array $array): array - { - $result = []; - /** @var array $journal */ - foreach ($array as $journal) { - $categoryId = (int)$journal['category_id']; - $result[$categoryId] = $result[$categoryId] ?? '0'; - $result[$categoryId] = bcadd($journal['amount'], $result[$categoryId]); - } - - return $result; - } - /** * Set the involved accounts. * @@ -261,4 +180,53 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface { return $this; } + + /** + * Get the expenses for this report. + * + * @return array + */ + protected function getExpenses(): array + { + if (count($this->expenses) > 0) { + Log::debug('Return previous set of expenses.'); + + return $this->expenses; + } + + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollectorInterface::class); + $collector->setAccounts($this->accounts)->setRange($this->start, $this->end) + ->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]) + ->setCategories($this->categories)->withAccountInformation(); + + $transactions = $collector->getExtractedJournals(); + $this->expenses = $transactions; + + return $transactions; + } + + /** + * Get the income for this report. + * + * @return array + */ + protected function getIncome(): array + { + if (count($this->income) > 0) { + return $this->income; + } + + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollectorInterface::class); + + $collector->setAccounts($this->accounts)->setRange($this->start, $this->end) + ->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]) + ->setCategories($this->categories)->withAccountInformation(); + + $transactions = $collector->getExtractedJournals(); + $this->income = $transactions; + + return $transactions; + } }