Skip to content

Firefly III: a personal finances manager

New budget table for multi year report.

James Cole124ecb137260
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php
index f9df70c397..cd8520e29d 100644
--- a/app/Helpers/Report/ReportHelper.php
+++ b/app/Helpers/Report/ReportHelper.php
@@ -14,6 +14,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
+use DB;
use FireflyIII\Helpers\Collection\Bill as BillCollection;
use FireflyIII\Helpers\Collection\BillLine;
use FireflyIII\Helpers\Collection\Category as CategoryCollection;
@@ -21,6 +22,7 @@ use FireflyIII\Helpers\Collection\Expense;
use FireflyIII\Helpers\Collection\Income;
use FireflyIII\Helpers\FiscalHelperInterface;
use FireflyIII\Models\Bill;
+use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
@@ -111,6 +113,67 @@ class ReportHelper implements ReportHelperInterface
return $collection;
}
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return array
+ */
+ public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array
+ {
+ $accountIds = $accounts->pluck('id')->toArray();
+ $query = TransactionJournal
+ ::leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
+ ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
+ ->leftJoin(
+ 'transactions', function (JoinClause $join) {
+ $join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
+ }
+ )
+ ->whereNull('transaction_journals.deleted_at')
+ ->whereNull('transactions.deleted_at')
+ ->where('transaction_types.type', 'Withdrawal')
+ ->where('transaction_journals.user_id', auth()->user()->id);
+
+ if (count($accountIds) > 0) {
+ $query->whereIn('transactions.account_id', $accountIds);
+ }
+ $query->groupBy(['budget_transaction_journal.budget_id', 'the_year']);
+ $queryResult = $query->get(
+ [
+ 'budget_transaction_journal.budget_id',
+ DB::raw('DATE_FORMAT(transaction_journals.date,"%Y") AS the_year'),
+ DB::raw('SUM(transactions.amount) as sum_of_period'),
+ ]
+ );
+
+ $data = [];
+ $budgets = $this->budgetRepository->getBudgets();
+ $years = $this->listOfYears($start, $end);
+
+ // do budget "zero"
+ $emptyBudget = new Budget;
+ $emptyBudget->id = 0;
+ $emptyBudget->name = strval(trans('firefly.no_budget'));
+ $budgets->push($emptyBudget);
+
+
+ // get all budgets and years.
+ foreach ($budgets as $budget) {
+ $data[$budget->id] = [
+ 'name' => $budget->name,
+ 'entries' => [],
+ ];
+ foreach ($years as $year) {
+ // filter query result here!
+ $data[$budget->id]['entries'][$year] = $this->filterAmount($queryResult, $budget->id, $year);
+ }
+ }
+
+ return $data;
+ }
+
/**
* @param Carbon $start
* @param Carbon $end
@@ -231,6 +294,24 @@ class ReportHelper implements ReportHelperInterface
return $months;
}
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function listOfYears(Carbon $start, Carbon $end): array
+ {
+ $begin = clone $start;
+ $years = [];
+ while ($begin < $end) {
+ $years[] = $begin->year;
+ $begin->addYear();
+ }
+
+ return $years;
+ }
+
/**
* Returns an array of tags and their comparitive size with amounts bla bla.
*
@@ -305,6 +386,30 @@ class ReportHelper implements ReportHelperInterface
return $collection;
}
+ /**
+ * @param Collection $set
+ * @param int $budgetId
+ * @param int $year
+ *
+ * @return string
+ */
+ protected function filterAmount(Collection $set, int $budgetId, int $year): string
+ {
+ /** @var stdClass $object */
+ $result = $set->filter(
+ function (TransactionJournal $object) use ($budgetId, $year) {
+ return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
+ }
+ );
+ $amount = '0';
+ if (!is_null($result->first())) {
+ $amount = $result->first()->sum_of_period;
+ }
+
+ return $amount;
+
+ }
+
/**
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::earnedByDay
* and sum up everything in the array in the given range.
@@ -331,4 +436,6 @@ class ReportHelper implements ReportHelperInterface
return $sum;
}
+
+
}
diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php
index 34ffcc9e7b..6dec1b9c0e 100644
--- a/app/Helpers/Report/ReportHelperInterface.php
+++ b/app/Helpers/Report/ReportHelperInterface.php
@@ -28,6 +28,16 @@ use Illuminate\Support\Collection;
interface ReportHelperInterface
{
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return array
+ */
+ public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array;
+
+
/**
* This method generates a full report for the given period on all
* the users bills and their payments.
@@ -80,6 +90,14 @@ interface ReportHelperInterface
*/
public function listOfMonths(Carbon $date): array;
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ *
+ * @return array
+ */
+ public function listOfYears(Carbon $start, Carbon $end): array;
+
/**
* Returns an array of tags and their comparitive size with amounts bla bla.
*
diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php
index 9c0d213260..7878830f15 100644
--- a/app/Http/Controllers/ReportController.php
+++ b/app/Http/Controllers/ReportController.php
@@ -21,8 +21,6 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Account\AccountTaskerInterface;
-use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
-use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Support\Collection;
use Preferences;
use Session;
@@ -244,6 +242,11 @@ class ReportController extends Controller
*/
private function defaultMultiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
+ // need all budgets
+ // need all years.
+ $years = $this->helper->listOfYears($start, $end);
+ $budgetMultiYear = $this->helper->getBudgetMultiYear($start, $end, $accounts);
+
// and some id's, joined:
$accountIds = [];
@@ -256,7 +259,8 @@ class ReportController extends Controller
return view(
'reports.default.multi-year',
compact(
- 'accounts', 'start', 'end', 'accountIds', 'reportType'
+ 'accounts', 'start', 'end', 'accountIds', 'reportType',
+ 'years', 'budgetMultiYear'
)
);
}
diff --git a/resources/views/reports/default/multi-year.twig b/resources/views/reports/default/multi-year.twig
index 121784617c..314415d2f8 100644
--- a/resources/views/reports/default/multi-year.twig
+++ b/resources/views/reports/default/multi-year.twig
@@ -69,9 +69,6 @@
</div>
</div>
</div>
- <div class="col-lg-6 col-md-6 col-sm-6">
- {% include 'reports/partials/tags.twig' %}
- </div>
</div>
<div class="row">
@@ -87,6 +84,44 @@
</div>
</div>
+
+ <div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12">
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title">{{ 'budgets'|_ }}</h3>
+ </div>
+ <div class="box-body no-padding table-responsive">
+ <table class="table table-hover sortable">
+ <thead>
+ <tr>
+ <th>Budget</th>
+ {% for year in years %}
+ <th>{{ year }}</th>
+ {% endfor %}
+
+ </tr>
+ </thead>
+ <tbody>
+
+ {% for id, info in budgetMultiYear %}
+ <tr>
+ <td><a href="{{ route('budgets.show', id) }}">{{ info.name }}</a></td>
+ {% for amount in info.entries %}
+ <td>
+ {{ amount|formatAmount }}
+ </td>
+ {% endfor %}
+
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </div>
+ </div>
+ </div>
+ </div>
+
{% endblock %}
{% block styles %}
<link rel="stylesheet" href="css/bootstrap-sortable.css" type="text/css" media="all"/>