First code for #426

This commit is contained in:
James Cole 2016-12-09 07:07:53 +01:00
parent c73866f47c
commit 1fd028dfb8
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 648 additions and 2 deletions

View File

@ -25,7 +25,7 @@ use Steam;
/**
* Class MonthReportGenerator
*
* @package FireflyIII\Generator\Report\Standard
* @package FireflyIII\Generator\Report\Audit
*/
class MonthReportGenerator implements ReportGeneratorInterface
{

View File

@ -0,0 +1,329 @@
<?php
/**
* MonthReportGenerator.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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.
*/
declare(strict_types = 1);
namespace FireflyIII\Generator\Report\Budget;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollector;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
use Log;
/**
* Class MonthReportGenerator
*
* @package FireflyIII\Generator\Report\Budget
*/
class MonthReportGenerator implements ReportGeneratorInterface
{
/** @var Collection */
private $accounts;
/** @var Collection */
private $budgets;
/** @var Carbon */
private $end;
/** @var Collection */
private $expenses;
/** @var Collection */
private $income;
/** @var Carbon */
private $start;
/**
* MonthReportGenerator constructor.
*/
public function __construct()
{
$this->income = new Collection;
$this->expenses = new Collection;
}
/**
* @return string
*/
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();
// render!
return view(
'reports.budget.month',
compact(
'accountIds', 'categoryIds', 'topIncome', 'reportType', 'accountSummary', 'categorySummary', 'averageExpenses', 'averageIncome', 'topExpenses'
)
)
->with('start', $this->start)->with('end', $this->end)
->with('budgets', $this->budgets)
->with('accounts', $this->accounts)
->render();
}
/**
* @param Collection $accounts
*
* @return ReportGeneratorInterface
*/
public function setAccounts(Collection $accounts): ReportGeneratorInterface
{
$this->accounts = $accounts;
return $this;
}
/**
* @param Collection $budgets
*
* @return ReportGeneratorInterface
*/
public function setBudgets(Collection $budgets): ReportGeneratorInterface
{
$this->budgets = $budgets;
return $this;
}
/**
* @param Collection $categories
*
* @return ReportGeneratorInterface
*/
public function setCategories(Collection $categories): ReportGeneratorInterface
{
$this->categories = $categories;
return $this;
}
/**
* @param Carbon $date
*
* @return ReportGeneratorInterface
*/
public function setEndDate(Carbon $date): ReportGeneratorInterface
{
$this->end = $date;
return $this;
}
/**
* @param Carbon $date
*
* @return ReportGeneratorInterface
*/
public function setStartDate(Carbon $date): ReportGeneratorInterface
{
$this->start = $date;
return $this;
}
/**
* @param Collection $collection
* @param int $sortFlag
*
* @return array
*/
private function getAverages(Collection $collection, int $sortFlag): array
{
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
// opposing name and ID:
$opposingId = $transaction->opposing_account_id;
// is not set?
if (!isset($result[$opposingId])) {
$name = $transaction->opposing_account_name;
$result[$opposingId] = [
'name' => $name,
'count' => 1,
'id' => $opposingId,
'average' => $transaction->transaction_amount,
'sum' => $transaction->transaction_amount,
];
continue;
}
$result[$opposingId]['count']++;
$result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $transaction->transaction_amount);
$result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], strval($result[$opposingId]['count']));
}
// sort result by average:
$average = [];
foreach ($result as $key => $row) {
$average[$key] = floatval($row['average']);
}
array_multisort($average, $sortFlag, $result);
return $result;
}
/**
* @return Collection
*/
private function getExpenses(): Collection
{
if ($this->expenses->count() > 0) {
Log::debug('Return previous set of expenses.');
return $this->expenses;
}
$collector = new JournalCollector(auth()->user());
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setCategories($this->categories)->withOpposingAccount()->disableFilter();
$accountIds = $this->accounts->pluck('id')->toArray();
$transactions = $collector->getJournals();
$transactions = self::filterExpenses($transactions, $accountIds);
$this->expenses = $transactions;
return $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
*/
private function getTopExpenses(): Collection
{
$transactions = $this->getExpenses()->sortBy('transaction_amount');
return $transactions;
}
/**
* @return Collection
*/
private function getTopIncome(): Collection
{
$transactions = $this->getIncome()->sortByDesc('transaction_amount');
return $transactions;
}
/**
* @param Collection $collection
*
* @return array
*/
private function summarizeByAccount(Collection $collection): array
{
$result = [];
/** @var Transaction $transaction */
foreach ($collection as $transaction) {
$accountId = $transaction->account_id;
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
}
return $result;
}
/**
* @param Collection $collection
*
* @return array
*/
private function summarizeByCategory(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]);
}
return $result;
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* MultiYearReportGenerator.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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.
*/
declare(strict_types = 1);
namespace FireflyIII\Generator\Report\Budget;
/**
* Class MultiYearReportGenerator
*
* @package FireflyIII\Generator\Report\Budget
*/
class MultiYearReportGenerator extends MonthReportGenerator
{
/**
* Doesn't do anything different.
*/
}

View File

@ -0,0 +1,28 @@
<?php
/**
* YearReportGenerator.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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.
*/
declare(strict_types = 1);
namespace FireflyIII\Generator\Report\Budget;
/**
* Class YearReportGenerator
*
* @package FireflyIII\Generator\Report\Budget
*/
class YearReportGenerator extends MonthReportGenerator
{
/**
* Doesn't do anything different.
*/
}

View File

@ -98,7 +98,7 @@ class ReportController extends Controller
/**
* @param Collection $accounts
* @param Collection $categories
* @param Collection $budgets
* @param Carbon $start
* @param Carbon $end
*

View File

@ -0,0 +1,262 @@
{% extends "./layout/default" %}
{% block breadcrumbs %}
{{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName) }}
{% endblock %}
{% block content %}
<div class="row">
<div class="col-lg-4 col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'accounts'|_ }}</h3>
</div>
<div class="box-body table-responsive no-padding">
<table class="table table-hover sortable">
<thead>
<tr>
<th data-defaultsign="az">{{ 'name'|_ }}</th>
<th data-defaultsign="_19">{{ 'earned'|_ }}</th>
<th data-defaultsign="_19">{{ 'spent'|_ }}</th>
</tr>
</thead>
<tbody>
{% for account in accounts %}
<tr>
<td data-value="{{ account.name }}">
<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>
{% else %}
<td data-value="0">{{ 0|formatAmount }}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'budgets'|_ }}</h3>
</div>
<div class="box-body table-responsive no-padding">
<table class="table table-hover sortable">
<thead>
<tr>
<th data-defaultsign="az">{{ 'name'|_ }}</th>
<th data-defaultsign="_19">{{ 'spent'|_ }}</th>
</tr>
</thead>
<tbody>
{% for budget in budgets %}
<tr>
<td data-value="{{ budget.name }}">
<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>
{% else %}
<td data-value="0">{{ 0|formatAmount }}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% if budgets.count > 1 %}
<div class="col-lg-4 col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'expense_per_budget'|_ }}</h3>
</div>
<div class="box-body">
<canvas id="budgets-out-pie-chart" style="margin:0 auto;" height="150" width="150"></canvas>
<label style="font-weight:normal;">
<input type="checkbox" id="budgets-out-pie-chart-checked">
<small>{{ 'include_not_in_budget'|_ }}</small>
</label>
</div>
</div>
</div>
{% endif %}
{% if accounts.count > 1 %}
<div class="col-lg-4 col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'expense_per_budget'|_ }}</h3>
</div>
<div class="box-body">
<canvas id="accounts-out-pie-chart" style="margin:0 auto;" height="150" width="150"></canvas>
<label style="font-weight:normal;">
<input type="checkbox" id="accounts-out-pie-chart-checked">
<small>{{ 'include_not_in_budget'|_ }}</small>
</label>
</div>
</div>
</div>
{% endif %}
</div>
<div class="row">
<div class="col-lg-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'income_and_expenses'|_ }}</h3>
</div>
<div class="box-body">
<canvas id="in-out-chart" style="margin:0 auto;" height="300"></canvas>
</div>
</div>
</div>
</div>
<div class="row">
{% if averageExpenses|length > 0 %}
<div class="col-lg-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'average_spending_per_account'|_ }}</h3>
</div>
<div class="box-body table-responsive no-padding">
<table class="table table-hover sortable">
<thead>
<tr>
<th data-defaultsign="az">{{ 'account'|_ }}</th>
<th data-defaultsign="_19">{{ 'spent_average'|_ }}</th>
<th data-defaultsign="_19">{{ 'total'|_ }}</th>
<th data-defaultsign="_19">{{ 'transaction_count'|_ }}</th>
</tr>
</thead>
<tbody>
{% for row in averageExpenses %}
<tr>
<td data-value="{{ row.name }}">
<a href="{{ route('accounts.show', row.id) }}">{{ row.name }}</a>
</td>
<td data-value="{{ row.average }}">
{{ row.average|formatAmount }}
</td>
<td data-value="{{ row.sum }}">
{{ row.sum|formatAmount }}
</td>
<td data-value="{{ row.count }}">
{{ row.count }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endif %}
{% if topExpenses.count > 0 %}
<div class="col-lg-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'expenses'|_ }} ({{ trans('firefly.topX', {number: listLength}) }})</h3>
</div>
<div class="box-body">
<table class="table table-hover sortable">
<thead>
<tr>
<th data-defaultsort="disabled">{{ 'description'|_ }}</th>
<th data-defaultsign="month">{{ 'date'|_ }}</th>
<th data-defaultsign="az">{{ 'account'|_ }}</th>
<th data-defaultsign="_19">{{ 'amount'|_ }}</th>
</tr>
</thead>
<tbody>
{% for row in topExpenses %}
{% if loop.index > listLength %}
<tr class="overListLength">
{% else %}
<tr>
{% endif %}
<td data-sortable="false">
<a href="{{ route('transactions.show', row.journal_id) }}">
{% if row.transaction_description|length > 0 %}
{{ row.transaction_description }} ({{ row.description }})
{% else %}
{{ row.description }}
{% endif %}
</a>
</td>
<td data-value="{{ row.date.format('Y-m-d') }}">
{{ row.date.formatLocalized(monthAndDayFormat) }}
</td>
<td data-value="{{ row.opposing_account_name }}">
<a href="{{ route('accounts.show', row.opposing_account_id) }}">
{{ row.opposing_account_name }}
</a>
</td>
<td data-value="{{ row.transaction_amount }}">
{{ row.transaction_amount|formatAmount }}
</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
{% if topExpenses.count > listLength %}
<tr>
<td colspan="3" class="active">
<a href="#" class="listLengthTrigger">{{ trans('firefly.show_full_list',{number:incomeTopLength}) }}</a>
</td>
</tr>
{% endif %}
</tfoot>
</table>
</div>
</div>
</div>
{% endif %}
</div>
{% endblock %}
{% block scripts %}
<script type="text/javascript" src="js/lib/Chart.bundle.min.js"></script>
<script type="text/javascript" src="js/ff/charts.defaults.js"></script>
<script type="text/javascript" src="js/ff/charts.js"></script>
<script type="text/javascript" src="js/lib/bootstrap-sortable.js"></script>
<script type="text/javascript">
// to report another URL:
var startDate = '{{ start.format('Ymd') }}';
var endDate = '{{ end.format('Ymd') }}';
var accountIds = '{{ accountIds }}';
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')]) }}';
</script>
<script type="text/javascript" src="js/ff/reports/budget/all.js"></script>
<script type="text/javascript" src="js/ff/reports/budget/month.js"></script>
{% endblock %}
{% block styles %}
<link rel="stylesheet" href="css/bootstrap-sortable.css" type="text/css" media="all"/>
{% endblock %}