Skip to content

Firefly III: a personal finances manager

Also fix income/expenses sum

James Coleb1ef225bd095
diff --git a/app/Http/Controllers/Report/InOutController.php b/app/Http/Controllers/Report/InOutController.php
index faaf7256d8..2190e60f87 100644
--- a/app/Http/Controllers/Report/InOutController.php
+++ b/app/Http/Controllers/Report/InOutController.php
@@ -49,41 +49,8 @@ class InOutController extends Controller
if ($cache->has()) {
return $cache->get();
}
-
- // get all expenses for the given accounts in the given period!
- // also transfers!
- // get all transactions:
- /** @var JournalCollectorInterface $collector */
- $collector = app(JournalCollectorInterface::class);
- $collector->setAccounts($accounts)->setRange($start, $end);
- $collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
- ->withOpposingAccount()
- ->enableInternalFilter();
- $transactions = $collector->getJournals();
- $transactions = $transactions->filter(
- function (Transaction $transaction) {
- // return negative amounts only.
- if (bccomp($transaction->transaction_amount, '0') === -1) {
- return $transaction;
- }
-
- return false;
- }
- );
- $expenses = $this->groupByOpposing($transactions);
-
- // sort the result
- // Obtain a list of columns
- $sum = [];
- foreach ($expenses as $accountId => $row) {
- $sum[$accountId] = floatval($row['sum']);
- }
-
- // Sort the data with volume descending, edition ascending
- // Add $data as the last parameter, to sort by the common key
- array_multisort($sum, SORT_ASC, $expenses);
-
- $result = view('reports.partials.expenses', compact('expenses'))->render();
+ $expenses = $this->getExpenseReport($start, $end, $accounts);
+ $result = view('reports.partials.expenses', compact('expenses'))->render();
$cache->store($result);
return $result;
@@ -110,10 +77,25 @@ class InOutController extends Controller
return $cache->get();
}
- $incomes = $helper->getIncomeReport($start, $end, $accounts);
- $expenses = $helper->getExpenseReport($start, $end, $accounts);
+ $incomes = $this->getIncomeReport($start, $end, $accounts);
+ $expenses = $this->getExpenseReport($start, $end, $accounts);
+ $incomeSum = array_sum(
+ array_map(
+ function ($item) {
+ return $item['sum'];
+ }, $incomes
+ )
+ );
+
+ $expensesSum = array_sum(
+ array_map(
+ function ($item) {
+ return $item['sum'];
+ }, $expenses
+ )
+ );
- $result = view('reports.partials.income-vs-expenses', compact('expenses', 'incomes'))->render();
+ $result = view('reports.partials.income-vs-expenses', compact('incomeSum', 'expensesSum'))->render();
$cache->store($result);
return $result;
@@ -139,7 +121,69 @@ class InOutController extends Controller
if ($cache->has()) {
//return $cache->get();
}
+ $income = $this->getIncomeReport($start, $end, $accounts);
+
+ $result = view('reports.partials.income', compact('income'))->render();
+ $cache->store($result);
+
+ return $result;
+
+ }
+
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return array
+ */
+ private function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array
+ {
+ // get all expenses for the given accounts in the given period!
+ // also transfers!
+ // get all transactions:
+ /** @var JournalCollectorInterface $collector */
+ $collector = app(JournalCollectorInterface::class);
+ $collector->setAccounts($accounts)->setRange($start, $end);
+ $collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
+ ->withOpposingAccount()
+ ->enableInternalFilter();
+ $transactions = $collector->getJournals();
+ $transactions = $transactions->filter(
+ function (Transaction $transaction) {
+ // return negative amounts only.
+ if (bccomp($transaction->transaction_amount, '0') === -1) {
+ return $transaction;
+ }
+
+ return false;
+ }
+ );
+ $expenses = $this->groupByOpposing($transactions);
+
+ // sort the result
+ // Obtain a list of columns
+ $sum = [];
+ foreach ($expenses as $accountId => $row) {
+ $sum[$accountId] = floatval($row['sum']);
+ }
+ // Sort the data with volume descending, edition ascending
+ // Add $data as the last parameter, to sort by the common key
+ array_multisort($sum, SORT_ASC, $expenses);
+
+ return $expenses;
+ }
+
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return array
+ */
+ private function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array
+ {
// get all expenses for the given accounts in the given period!
// also transfers!
// get all transactions:
@@ -162,11 +206,18 @@ class InOutController extends Controller
);
$income = $this->groupByOpposing($transactions);
- $result = view('reports.partials.income', compact('income'))->render();
- $cache->store($result);
+ // sort the result
+ // Obtain a list of columns
+ $sum = [];
+ foreach ($income as $accountId => $row) {
+ $sum[$accountId] = floatval($row['sum']);
+ }
- return $result;
+ // Sort the data with volume descending, edition ascending
+ // Add $data as the last parameter, to sort by the common key
+ array_multisort($sum, SORT_ASC, $income);
+ return $income;
}
/**
diff --git a/resources/views/reports/partials/income-vs-expenses.twig b/resources/views/reports/partials/income-vs-expenses.twig
index b9c9d0f5f5..12fb1928f7 100644
--- a/resources/views/reports/partials/income-vs-expenses.twig
+++ b/resources/views/reports/partials/income-vs-expenses.twig
@@ -1,14 +1,14 @@
<table class="table table-hover">
<tr>
<td>{{ 'in'|_ }}</td>
- <td>{{ incomes.getTotal|formatAmount }}</td>
+ <td>{{ incomeSum|formatAmount }}</td>
</tr>
<tr>
<td>{{ 'out'|_ }}</td>
- <td>{{ (expenses.getTotal)|formatAmount }}</td>
+ <td>{{ expensesSum|formatAmount }}</td>
</tr>
<tr>
<td>{{ 'difference'|_ }}</td>
- <td>{{ (incomes.getTotal + expenses.getTotal)|formatAmount }}</td>
+ <td>{{ (incomeSum + expensesSum)|formatAmount }}</td>
</tr>
</table>