Skip to content

Firefly III: a personal finances manager

Fixed some issues with expense/income overview.

James Coleb713eae009f6
diff --git a/app/Http/Controllers/Report/InOutController.php b/app/Http/Controllers/Report/InOutController.php
index 83ce241de1..faaf7256d8 100644
--- a/app/Http/Controllers/Report/InOutController.php
+++ b/app/Http/Controllers/Report/InOutController.php
@@ -15,8 +15,11 @@ namespace FireflyIII\Http\Controllers\Report;
use Carbon\Carbon;
+use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Http\Controllers\Controller;
+use FireflyIII\Models\Transaction;
+use FireflyIII\Models\TransactionType;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
@@ -29,14 +32,13 @@ class InOutController extends Controller
{
/**
- * @param ReportHelperInterface $helper
- * @param Carbon $start
- * @param Carbon $end
- * @param Collection $accounts
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
- public function expenseReport(ReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts)
+ public function expenseReport(Carbon $start, Carbon $end, Collection $accounts)
{
// chart properties for cache:
$cache = new CacheProperties;
@@ -48,7 +50,38 @@ class InOutController extends Controller
return $cache->get();
}
- $expenses = $helper->getExpenseReport($start, $end, $accounts);
+ // 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();
$cache->store($result);
@@ -104,16 +137,64 @@ class InOutController extends Controller
$cache->addProperty('income-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
- return $cache->get();
+ //return $cache->get();
}
- $incomes = $helper->getIncomeReport($start, $end, $accounts);
-
- $result = view('reports.partials.income', compact('incomes'))->render();
+ // 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::DEPOSIT, TransactionType::TRANSFER])
+ ->withOpposingAccount()
+ ->enableInternalFilter();
+ $transactions = $collector->getJournals();
+ $transactions = $transactions->filter(
+ function (Transaction $transaction) {
+ // return positive amounts only.
+ if (bccomp($transaction->transaction_amount, '0') === 1) {
+ return $transaction;
+ }
+
+ return false;
+ }
+ );
+ $income = $this->groupByOpposing($transactions);
+
+ $result = view('reports.partials.income', compact('income'))->render();
$cache->store($result);
return $result;
}
+ /**
+ * @param Collection $transactions
+ *
+ * @return array
+ */
+ private function groupByOpposing(Collection $transactions): array
+ {
+ $expenses = [];
+ // join the result together:
+ foreach ($transactions as $transaction) {
+ $opposingId = $transaction->opposing_account_id;
+ $name = $transaction->opposing_account_name;
+ if (!isset($expenses[$opposingId])) {
+ $expenses[$opposingId] = [
+ 'id' => $opposingId,
+ 'name' => $name,
+ 'sum' => '0',
+ 'count' => 0,
+ ];
+ }
+ $expenses[$opposingId]['sum'] = bcadd($expenses[$opposingId]['sum'], $transaction->transaction_amount);
+ $expenses[$opposingId]['count']++;
+ }
+
+
+ return $expenses;
+ }
+
}
\ No newline at end of file
diff --git a/resources/views/reports/partials/expenses.twig b/resources/views/reports/partials/expenses.twig
index f3cf12e4aa..e1d7956087 100644
--- a/resources/views/reports/partials/expenses.twig
+++ b/resources/views/reports/partials/expenses.twig
@@ -1,6 +1,8 @@
<table class="table table-hover">
<tbody>
- {% for expense in expenses.getExpenses %}
+ {% set sum = 0 %}
+ {% for expense in expenses %}
+ {% set sum = sum + expense.sum %}
{% if loop.index > listLength %}
<tr class="overListLength">
{% else %}
@@ -18,13 +20,13 @@
{% endif %}
</td>
<td>
- {{ (expense.amount)|formatAmount }}
+ {{ (expense.sum)|formatAmount }}
</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
- {% if expenses.getExpenses|length > listLength %}
+ {% if expenses|length > listLength %}
<tr>
<td colspan="2" class="active">
<a href="#" class="listLengthTrigger">{{ trans('firefly.show_full_list',{number:incomeTopLength}) }}</a>
@@ -33,7 +35,7 @@
{% endif %}
<tr>
<td><em>{{ 'sum'|_ }}</em></td>
- <td>{{ (expenses.getTotal)|formatAmount }}</td>
+ <td>{{ (sum)|formatAmount }}</td>
</tr>
</tfoot>
</table>
diff --git a/resources/views/reports/partials/income.twig b/resources/views/reports/partials/income.twig
index 3428b0a33a..be2e37dcd0 100644
--- a/resources/views/reports/partials/income.twig
+++ b/resources/views/reports/partials/income.twig
@@ -1,30 +1,32 @@
<table class="table table-hover">
<tbody>
- {% for income in incomes.getIncomes %}
+ {% set sum = 0 %}
+ {% for row in income %}
+ {% set sum = sum + row.sum %}
{% if loop.index > listLength %}
<tr class="overListLength">
{% else %}
<tr>
{% endif %}
<td>
- <a href="{{ route('accounts.show',income.id) }}" title="{{ income.name }}">{{ income.name }}</a>
- {% if income.count > 1 %}
+ <a href="{{ route('accounts.show',row.id) }}" title="{{ row.name }}">{{ row.name }}</a>
+ {% if row.count > 1 %}
<br/>
<small>
- {{ income.count }} {{ 'transactions'|_|lower }}
+ {{ row.count }} {{ 'transactions'|_|lower }}
<i class="fa fa-fw text-muted fa-info-circle firefly-info-button" data-location="income-entry"
- data-account-id="{{ income.id }}"></i>
+ data-account-id="{{ row.id }}"></i>
</small>
{% endif %}
</td>
- <td>{{ income.amount|formatAmount }}</td>
+ <td>{{ row.sum|formatAmount }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
- {% if incomes.getIncomes|length > listLength %}
+ {% if income|length > listLength %}
<tr>
<td colspan="2" class="active">
<a href="#" class="listLengthTrigger">{{ trans('firefly.show_full_list',{ number:listLength } ) }}</a>
@@ -33,7 +35,7 @@
{% endif %}
<tr>
<td><em>{{ 'sum'|_ }}</em></td>
- <td>{{ incomes.getTotal|formatAmount }}</td>
+ <td>{{ sum|formatAmount }}</td>
</tr>
</tfoot>
</table>