From 0bb07e1eebd8ceda079f3eb3d72907766aaca62f Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 13 Nov 2016 20:18:01 +0100 Subject: [PATCH] Small extension of category report. --- .../Report/Category/MonthReportGenerator.php | 79 +++++++- resources/views/reports/category/month.twig | 173 ++++++++++++------ 2 files changed, 197 insertions(+), 55 deletions(-) diff --git a/app/Generator/Report/Category/MonthReportGenerator.php b/app/Generator/Report/Category/MonthReportGenerator.php index a46802b1e6..a3d2abb4ee 100644 --- a/app/Generator/Report/Category/MonthReportGenerator.php +++ b/app/Generator/Report/Category/MonthReportGenerator.php @@ -15,11 +15,13 @@ namespace FireflyIII\Generator\Report\Category; use Carbon\Carbon; +use Crypt; use FireflyIII\Generator\Report\ReportGeneratorInterface; use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionType; use Illuminate\Support\Collection; +use Log; /** * Class MonthReportGenerator @@ -34,9 +36,22 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface private $categories; /** @var Carbon */ private $end; + /** @var Collection */ + private $expenses; + /** @var Collection */ + private $income; /** @var Carbon */ private $start; + /** + * MonthReportGenerator constructor. + */ + public function __construct() + { + $this->income = new Collection; + $this->expenses = new Collection; + } + /** * @return string */ @@ -47,9 +62,10 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface $reportType = 'category'; $accountSummary = $this->getAccountSummary(); $categorySummary = $this->getCategorySummary(); + $averageExpenses = $this->getAverageExpenses(); // render! - return view('reports.category.month', compact('accountIds', 'categoryIds', 'reportType', 'accountSummary', 'categorySummary')) + return view('reports.category.month', compact('accountIds', 'categoryIds', 'reportType', 'accountSummary', 'categorySummary','averageExpenses')) ->with('start', $this->start)->with('end', $this->end) ->with('categories', $this->categories) ->with('accounts', $this->accounts) @@ -143,6 +159,49 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface } + /** + * @return array + */ + private function getAverageExpenses(): array + { + $expenses = $this->getExpenses(); + $result = []; + /** @var Transaction $transaction */ + foreach ($expenses as $transaction) { + // opposing name and ID: + $opposingId = $transaction->opposing_account_id; + + // is not set? + if (!isset($result[$opposingId])) { + $name = $transaction->opposing_account_name; + $encrypted = intval($transaction->opposing_account_encrypted); + $name = $encrypted === 1 ? Crypt::decrypt($name) : $name; + $result[$opposingId] = [ + 'name' => $name, + 'count' => 1, + 'id' => $opposingId, + 'average' => $transaction->transaction_amount, + 'sum' => $transaction->transaction_amount, + ]; + continue; + } + $result[$opposingId]['count']++; + $result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $transaction->transaction_amount); + $result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], strval($result[$opposingId]['count'])); + } + + // sort result by average: + $average = []; + foreach ($result as $key => $row) { + $average[$key] = floatval($row['average']); + } + + array_multisort($average, SORT_ASC, $result); + + return $result; + + } + /** * @return array */ @@ -222,14 +281,21 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface */ private function getExpenses(): Collection { + if ($this->expenses->count() > 0) { + Log::debug('Return previous set of expenses.'); + + return $this->expenses; + } + $collector = new JournalCollector(auth()->user()); $collector->setAccounts($this->accounts)->setRange($this->start, $this->end) ->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]) ->setCategories($this->categories)->withOpposingAccount()->disableFilter(); - $accountIds = $this->accounts->pluck('id')->toArray(); - $transactions = $collector->getJournals(); - $transactions = self::filterExpenses($transactions, $accountIds); + $accountIds = $this->accounts->pluck('id')->toArray(); + $transactions = $collector->getJournals(); + $transactions = self::filterExpenses($transactions, $accountIds); + $this->expenses = $transactions; return $transactions; } @@ -239,6 +305,10 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface */ private function getIncome(): Collection { + if ($this->income->count() > 0) { + return $this->income; + } + $collector = new JournalCollector(auth()->user()); $collector->setAccounts($this->accounts)->setRange($this->start, $this->end) ->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]) @@ -246,6 +316,7 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface $accountIds = $this->accounts->pluck('id')->toArray(); $transactions = $collector->getJournals(); $transactions = self::filterIncome($transactions, $accountIds); + $this->income = $transactions; return $transactions; } diff --git a/resources/views/reports/category/month.twig b/resources/views/reports/category/month.twig index f4ef4a6f04..46357c569d 100644 --- a/resources/views/reports/category/month.twig +++ b/resources/views/reports/category/month.twig @@ -89,73 +89,77 @@ {% if categories.count > 1 %} -
-
-
-

{{ 'income_per_category'|_ }}

-
-
- - +
+
+
+

{{ 'income_per_category'|_ }}

+
+
+ + +
-
-
-
-
-

{{ 'expense_per_category'|_ }}

-
-
- - +
+
+
+

{{ 'expense_per_category'|_ }}

+
+
+ + +
-
{% endif %} {% if accounts.count > 1 %} -
-
-
-

{{ 'income_per_account'|_ }}

-
-
- - +
+
+
+

{{ 'income_per_account'|_ }}

+
+
+ + +
-
-
-
-
-

{{ 'expense_per_account'|_ }}

-
-
- - +
+
+
+

{{ 'expense_per_account'|_ }}

+
+
+ + +
-
{% endif %}
-
-
-

{{ 'income_and_expenses'|_ }}

-
-
- -
+
+
+

{{ 'income_and_expenses'|_ }}

+
+ +
+
{# @@ -164,6 +168,73 @@ In a bar chart, possibly grouped by expense/revenue account. #} +
+
+
+
+

{{ 'average_spending_per_account'|_ }}

+
+
+ + + + + + + + + + {% for row in averageExpenses %} + + + + + + {% endfor %} + +
{{ 'account'|_ }}{{ 'spent_average'|_ }}{{ 'transaction_count'|_ }}
+ {{ row.name }} + + {{ row.average|formatAmount }} + + {{ row.count }} +
+
+
+
+
+
+
+

{{ 'expenses'|_ }} ({{ trans('firefly.topX', {number: 10}) }})

+
+
+
+
+
+
+
+ +
+
+
+

{{ 'average_income_per_account'|_ }}

+
+
+
+
+
+
+
+
+

{{ 'income'|_ }} ({{ trans('firefly.topX', {number: 10}) }})

+
+
+
+
+
+
+ +
List of spending (withdrawals) by account, if relevant. Grouped: