Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Working but fairly useless budget report
James Colebc11c3fab216
diff --git a/app/Generator/Report/Budget/MonthReportGenerator.php b/app/Generator/Report/Budget/MonthReportGenerator.phpindex 10b5c39afe..8a8e96d4b5 100644--- a/app/Generator/Report/Budget/MonthReportGenerator.php+++ b/app/Generator/Report/Budget/MonthReportGenerator.php@@ -16,6 +16,7 @@ namespace FireflyIII\Generator\Report\Budget;use Carbon\Carbon;use FireflyIII\Generator\Report\ReportGeneratorInterface;+use FireflyIII\Generator\Report\Support;use FireflyIII\Helpers\Collector\JournalCollector;use FireflyIII\Models\Transaction;use FireflyIII\Models\TransactionType;@@ -27,7 +28,7 @@ use Log;** @package FireflyIII\Generator\Report\Budget*/-class MonthReportGenerator implements ReportGeneratorInterface+class MonthReportGenerator extends Support implements ReportGeneratorInterface{/** @var Collection */private $accounts;@@ -56,26 +57,16 @@ class MonthReportGenerator implements ReportGeneratorInterface*/public function generate(): string{- $accountIds = join(',', $this->accounts->pluck('id')->toArray());- $categoryIds = join(',', $this->budgets->pluck('id')->toArray());- $reportType = 'budget';- // $expenses = $this->getExpenses();- // $income = $this->getIncome();- // $accountSummary = $this->getObjectSummary($this->summarizeByAccount($expenses), $this->summarizeByAccount($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 = join(',', $this->accounts->pluck('id')->toArray());+ $categoryIds = join(',', $this->budgets->pluck('id')->toArray());+ $expenses = $this->getExpenses();+ $accountSummary = $this->summarizeByAccount($expenses);+ $budgetSummary = $this->summarizeByBudget($expenses);+ $averageExpenses = $this->getAverages($expenses, SORT_ASC);+ $topExpenses = $this->getTopExpenses();// render!- return view(- 'reports.budget.month',- compact(- 'accountIds', 'categoryIds', 'topIncome', 'reportType', 'accountSummary', 'categorySummary', 'averageExpenses', 'averageIncome', 'topExpenses'- )- )+ return view('reports.budget.month', compact('accountIds', 'categoryIds', 'accountSummary', 'budgetSummary', 'averageExpenses', 'topExpenses'))->with('start', $this->start)->with('end', $this->end)->with('budgets', $this->budgets)->with('accounts', $this->accounts)@@ -113,8 +104,6 @@ class MonthReportGenerator implements ReportGeneratorInterface*/public function setCategories(Collection $categories): ReportGeneratorInterface{- $this->categories = $categories;-return $this;}@@ -197,8 +186,8 @@ class MonthReportGenerator implements ReportGeneratorInterface$collector = new JournalCollector(auth()->user());$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)- ->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])- ->setCategories($this->categories)->withOpposingAccount()->disableFilter();+ ->setTypes([TransactionType::WITHDRAWAL])+ ->setBudgets($this->budgets)->withOpposingAccount()->disableFilter();$accountIds = $this->accounts->pluck('id')->toArray();$transactions = $collector->getJournals();@@ -208,67 +197,6 @@ class MonthReportGenerator implements ReportGeneratorInterfacereturn $transactions;}- /**- * @return Collection- */- 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])- ->setCategories($this->categories)->withOpposingAccount();- $accountIds = $this->accounts->pluck('id')->toArray();- $transactions = $collector->getJournals();- $transactions = self::filterIncome($transactions, $accountIds);- $this->income = $transactions;-- return $transactions;- }-- /**- * @param array $spent- * @param array $earned- *- * @return array- */- private function getObjectSummary(array $spent, array $earned): array- {- $return = [];-- /**- * @var int $accountId- * @var string $entry- */- foreach ($spent as $objectId => $entry) {- if (!isset($return[$objectId])) {- $return[$objectId] = ['spent' => 0, 'earned' => 0];- }-- $return[$objectId]['spent'] = $entry;- }- unset($entry);-- /**- * @var int $accountId- * @var string $entry- */- foreach ($earned as $objectId => $entry) {- if (!isset($return[$objectId])) {- $return[$objectId] = ['spent' => 0, 'earned' => 0];- }-- $return[$objectId]['earned'] = $entry;- }--- return $return;- }--/*** @return Collection*/@@ -279,16 +207,6 @@ class MonthReportGenerator implements ReportGeneratorInterfacereturn $transactions;}- /**- * @return Collection- */- private function getTopIncome(): Collection- {- $transactions = $this->getIncome()->sortByDesc('transaction_amount');-- return $transactions;- }-/*** @param Collection $collection*@@ -312,16 +230,16 @@ class MonthReportGenerator implements ReportGeneratorInterface** @return array*/- private function summarizeByCategory(Collection $collection): array+ private function summarizeByBudget(Collection $collection): array{$result = [];/** @var Transaction $transaction */foreach ($collection as $transaction) {- $jrnlCatId = intval($transaction->transaction_journal_category_id);- $transCatId = intval($transaction->transaction_category_id);- $categoryId = max($jrnlCatId, $transCatId);- $result[$categoryId] = $result[$categoryId] ?? '0';- $result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);+ $jrnlBudId = intval($transaction->transaction_journal_budget_id);+ $transBudId = intval($transaction->transaction_budget_id);+ $budgetId = max($jrnlBudId, $transBudId);+ $result[$budgetId] = $result[$budgetId] ?? '0';+ $result[$budgetId] = bcadd($transaction->transaction_amount, $result[$budgetId]);}return $result;diff --git a/app/Generator/Report/Category/MonthReportGenerator.php b/app/Generator/Report/Category/MonthReportGenerator.phpindex cbac1ee2d9..410fd29346 100644--- a/app/Generator/Report/Category/MonthReportGenerator.php+++ b/app/Generator/Report/Category/MonthReportGenerator.php@@ -16,6 +16,7 @@ namespace FireflyIII\Generator\Report\Category;use Carbon\Carbon;use FireflyIII\Generator\Report\ReportGeneratorInterface;+use FireflyIII\Generator\Report\Support;use FireflyIII\Helpers\Collector\JournalCollector;use FireflyIII\Models\Transaction;use FireflyIII\Models\TransactionType;diff --git a/app/Generator/Report/Category/Support.php b/app/Generator/Report/Support.phpsimilarity index 98%rename from app/Generator/Report/Category/Support.phprename to app/Generator/Report/Support.phpindex f568cd4282..56ee7e981c 100644--- a/app/Generator/Report/Category/Support.php+++ b/app/Generator/Report/Support.php@@ -11,7 +11,7 @@declare(strict_types = 1);-namespace FireflyIII\Generator\Report\Category;+namespace FireflyIII\Generator\Report;use FireflyIII\Models\Transaction;use Illuminate\Support\Collection;diff --git a/public/js/ff/reports/budget/all.js b/public/js/ff/reports/budget/all.jsnew file mode 100644index 0000000000..25a412d1c5--- /dev/null+++ b/public/js/ff/reports/budget/all.js@@ -0,0 +1,10 @@+/*+ * all.js+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */+diff --git a/public/js/ff/reports/budget/month.js b/public/js/ff/reports/budget/month.jsnew file mode 100644index 0000000000..90a4f3a5a2--- /dev/null+++ b/public/js/ff/reports/budget/month.js@@ -0,0 +1,64 @@+/*+ * month.js+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */+++$(function () {+ "use strict";+ drawChart();++ $('#budgets-in-pie-chart-checked').on('change', function () {+ redrawPieChart('budgets-in-pie-chart', categoryIncomeUri);+ });++ $('#budgets-out-pie-chart-checked').on('change', function () {+ redrawPieChart('budgets-out-pie-chart', categoryExpenseUri);+ });++ $('#accounts-in-pie-chart-checked').on('change', function () {+ redrawPieChart('accounts-in-pie-chart', accountIncomeUri);+ });++ $('#accounts-out-pie-chart-checked').on('change', function () {+ redrawPieChart('accounts-out-pie-chart', accountExpenseUri);+ });++});+++function drawChart() {+ "use strict";++ // month view:+ stackedColumnChart(mainUri, 'in-out-chart');++ // draw pie chart of income, depending on "show other transactions too":+ redrawPieChart('budgets-in-pie-chart', categoryIncomeUri);+ redrawPieChart('budgets-out-pie-chart', categoryExpenseUri);+ redrawPieChart('accounts-in-pie-chart', accountIncomeUri);+ redrawPieChart('accounts-out-pie-chart', accountExpenseUri);+++}++function redrawPieChart(container, uri) {+ "use strict";+ var checkbox = $('#' + container + '-checked');++ var others = '0';+ // check if box is checked:+ if (checkbox.prop('checked')) {+ others = '1';+ }+ uri = uri.replace('OTHERS', others);+ console.log('URI for ' + container + ' is ' + uri);++ pieChart(uri, container);++}diff --git a/resources/views/reports/budget/month.twig b/resources/views/reports/budget/month.twigindex 29fe364188..aeccc2dbde 100644--- a/resources/views/reports/budget/month.twig+++ b/resources/views/reports/budget/month.twig@@ -17,7 +17,6 @@<thead><tr><th data-defaultsign="az">{{ 'name'|_ }}</th>- <th data-defaultsign="_19">{{ 'earned'|_ }}</th><th data-defaultsign="_19">{{ 'spent'|_ }}</th></tr></thead>@@ -28,12 +27,7 @@<a href="{{ route('accounts.show', account.id) }}" title="{{ account.name }}">{{ account.name }}</a></td>{% if accountSummary[account.id] %}- <td data-value="{{ accountSummary[account.id].earned }}">{{ accountSummary[account.id].earned|formatAmount }}</td>- {% else %}- <td data-value="0">{{ 0|formatAmount }}</td>- {% endif %}- {% if accountSummary[account.id] %}- <td data-value="{{ accountSummary[account.id].spent }}">{{ accountSummary[account.id].spent|formatAmount }}</td>+ <td data-value="{{ accountSummary[account.id] }}">{{ accountSummary[account.id]|formatAmount }}</td>{% else %}<td data-value="0">{{ 0|formatAmount }}</td>{% endif %}@@ -63,12 +57,7 @@<a href="{{ route('budgets.show', budget.id) }}" title="{{ budget.name }}">{{ budget.name }}</a></td>{% if budgetSummary[budget.id] %}- <td data-value="{{ budgetSummary[budget.id].earned }}">{{ budgetSummary[budget.id].earned|formatAmount }}</td>- {% else %}- <td data-value="0">{{ 0|formatAmount }}</td>- {% endif %}- {% if budgetSummary[budget.id] %}- <td data-value="{{ budgetSummary[budget.id].spent }}">{{ budgetSummary[budget.id].spent|formatAmount }}</td>+ <td data-value="{{ budgetSummary[budget.id] }}">{{ budgetSummary[budget.id]|formatAmount }}</td>{% else %}<td data-value="0">{{ 0|formatAmount }}</td>{% endif %}@@ -120,6 +109,10 @@<h3 class="box-title">{{ 'income_and_expenses'|_ }}</h3></div><div class="box-body">+ Here be a chart with the budget limits as well if relevant.<br>+ amount spent vs budget limit reps<br>+ over the entire period the amount spent would rise and the budget limit rep would be like a heart beat jumping up and down<br>+ needs to be two axes to work<br><canvas id="in-out-chart" style="margin:0 auto;" height="300"></canvas></div></div>@@ -244,11 +237,11 @@var budgetIds = '{{ budgetIds }}';// chart uri's- var categoryIncomeUri = '{{ route('chart.category.category-income', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';- var categoryExpenseUri = '{{ route('chart.category.category-expense', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';- var accountIncomeUri = '{{ route('chart.category.account-income', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';- var accountExpenseUri = '{{ route('chart.category.account-expense', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';- var mainUri = '{{ route('chart.category.main', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}';+ var budgetIncomeUri = '{{ route('chart.budget.budget-income', [accountIds, budgetIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';+ var budgetExpenseUri = '{{ route('chart.budget.budget-expense', [accountIds, budgetIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';+ var accountIncomeUri = '{{ route('chart.budget.account-income', [accountIds, budgetIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';+ var accountExpenseUri = '{{ route('chart.budget.account-expense', [accountIds, budgetIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';+ var mainUri = '{{ route('chart.budget.main', [accountIds, budgetIds, start.format('Ymd'), end.format('Ymd')]) }}';</script>