Skip to content

Firefly III: a personal finances manager

Add average

James Cole62890d9b7ac4
diff --git a/app/Http/Controllers/Report/BudgetController.php b/app/Http/Controllers/Report/BudgetController.php
index 75afe0734e..3f8e59811e 100644
--- a/app/Http/Controllers/Report/BudgetController.php
+++ b/app/Http/Controllers/Report/BudgetController.php
@@ -481,10 +481,12 @@ class BudgetController extends Controller
$report = [];
foreach ($expenses as $currency) {
foreach ($currency['budgets'] as $budget) {
+ $count = 0;
foreach ($budget['transaction_journals'] as $journal) {
- $key = sprintf('%d-%d', $budget['id'], $currency['currency_id']);
- $dateKey = $journal['date']->format($keyFormat);
- $report[$key] = $report[$key] ?? [
+ $count++;
+ $key = sprintf('%d-%d', $budget['id'], $currency['currency_id']);
+ $dateKey = $journal['date']->format($keyFormat);
+ $report[$key] = $report[$key] ?? [
'id' => $budget['id'],
'name' => sprintf('%s (%s)', $budget['name'], $currency['currency_name']),
'sum' => '0',
@@ -495,9 +497,10 @@ class BudgetController extends Controller
'currency_decimal_places' => $currency['currency_decimal_places'],
'entries' => [],
];
- $report[$key] ['entries'][$dateKey] = $report[$key] ['entries'][$dateKey] ?? '0';
- $report[$key] ['entries'][$dateKey] = bcadd($journal['amount'], $report[$key] ['entries'][$dateKey]);
- $report[$key] ['sum'] = bcadd($report[$key] ['sum'], $journal['amount']);
+ $report[$key]['entries'][$dateKey] = $report[$key] ['entries'][$dateKey] ?? '0';
+ $report[$key]['entries'][$dateKey] = bcadd($journal['amount'], $report[$key] ['entries'][$dateKey]);
+ $report[$key]['sum'] = bcadd($report[$key] ['sum'], $journal['amount']);
+ $report[$key]['avg'] = bcdiv($report[$key]['sum'], (string)$count);
}
}
}
diff --git a/resources/views/v1/reports/partials/budget-period.twig b/resources/views/v1/reports/partials/budget-period.twig
index db3ee7f67e..27cf2cbb13 100644
--- a/resources/views/v1/reports/partials/budget-period.twig
+++ b/resources/views/v1/reports/partials/budget-period.twig
@@ -5,6 +5,7 @@
{% for period in periods %}
<th data-defaultsign="_19" style="text-align: right;">{{ period }}</th>
{% endfor %}
+ <th data-defaultsign="_19" style="text-align: right;">{{ 'average'|_ }}</th>
<th data-defaultsign="_19" style="text-align: right;">{{ 'sum'|_ }}</th>
</tr>
</thead>
@@ -33,6 +34,9 @@
{% endif %}
{% endfor %}
+ <td data-value="{{ info.avg }}" style="text-align: right;">
+ {{ formatAmountBySymbol(info.avg, info.currency_symbol, info.currency_decimal_places) }}
+ </td>
<td data-value="{{ info.sum }}" style="text-align: right;">
{{ formatAmountBySymbol(info.sum, info.currency_symbol, info.currency_decimal_places) }}
</td>