Update for balance box in report.

This commit is contained in:
James Cole
2019-08-16 17:54:38 +02:00
parent 070f46c755
commit 02db333d46
9 changed files with 190 additions and 83 deletions

View File

@@ -23,9 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Factory; namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionGroup; use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -60,10 +58,8 @@ class TransactionGroupFactory
$collection = $this->journalFactory->create($data); $collection = $this->journalFactory->create($data);
$title = $data['group_title'] ?? null; $title = $data['group_title'] ?? null;
$title = '' === $title ? null : $title; $title = '' === $title ? null : $title;
/** @var TransactionJournal $first */ $group = new TransactionGroup;
$first = $collection->first(); $group->user()->associate($this->user);
$group = new TransactionGroup;
$group->user()->associate($first->user);
$group->title = $title; $group->title = $title;
$group->save(); $group->save();

View File

@@ -143,6 +143,9 @@ class TransactionJournalFactory
if (null !== $journal) { if (null !== $journal) {
$collection->push($journal); $collection->push($journal);
} }
if(null === $journal) {
Log::error('The createJournal() method returned NULL. This may indicate an error.');
}
} }
return $collection; return $collection;
@@ -247,6 +250,7 @@ class TransactionJournalFactory
$destinationAccount = $this->getAccount($type->type, 'destination', (int)$row['destination_id'], $row['destination_name']); $destinationAccount = $this->getAccount($type->type, 'destination', (int)$row['destination_id'], $row['destination_name']);
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
} catch (FireflyException $e) { } catch (FireflyException $e) {
Log::error('Could not validate source or destination.');
Log::error($e->getMessage()); Log::error($e->getMessage());
return null; return null;

View File

@@ -27,7 +27,11 @@ use FireflyIII\Helpers\Collection\Balance;
use FireflyIII\Helpers\Collection\BalanceEntry; use FireflyIII\Helpers\Collection\BalanceEntry;
use FireflyIII\Helpers\Collection\BalanceHeader; use FireflyIII\Helpers\Collection\BalanceHeader;
use FireflyIII\Helpers\Collection\BalanceLine; use FireflyIII\Helpers\Collection\BalanceLine;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -65,11 +69,91 @@ class BalanceReportHelper implements BalanceReportHelperInterface
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return Balance * @return array
*/ */
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): array
{ {
Log::debug('Start of balance report'); Log::debug('Start of balance report');
$report = [
'budgets' => [],
'accounts' => [],
];
/** @var Account $account */
foreach ($accounts as $account) {
$report['accounts'][$account->id] = [
'id' => $account->id,
'name' => $account->name,
'iban' => $account->iban,
'sum' => '0',
];
}
$budgets = $this->budgetRepository->getBudgets();
// per budget, dan per balance line
// of als het in een balance line valt dan daaronder en anders niet
// kruistabel vullen?
/** @var Budget $budget */
foreach ($budgets as $budget) {
$budgetId = $budget->id;
$report['budgets'][$budgetId] = [
'budget_id' => $budgetId,
'budget_name' => $budget->name,
'spent' => [], // per account
'sums' => [], // per currency
];
$spent = [];
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$journals = $collector->setRange($start, $end)->setSourceAccounts($accounts)->setTypes([TransactionType::WITHDRAWAL])->setBudget($budget)
->getExtractedJournals();
/** @var array $journal */
foreach ($journals as $journal) {
$sourceAccount = $journal['source_account_id'];
$currencyId = $journal['currency_id'];
$spent[$sourceAccount] = $spent[$sourceAccount] ?? [
'source_account_id' => $sourceAccount,
'currency_id' => $journal['currency_id'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'spent' => '0',
];
$spent[$sourceAccount]['spent'] = bcadd($spent[$sourceAccount]['spent'], $journal['amount']);
// also fix sum:
$report['sums'][$budgetId][$currencyId] = $report['sums'][$budgetId][$currencyId] ?? [
'sum' => '0',
'currency_id' => $journal['currency_id'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$report['sums'][$budgetId][$currencyId]['sum'] = bcadd($report['sums'][$budgetId][$currencyId]['sum'], $journal['amount']);
$report['accounts'][$sourceAccount]['sum'] = bcadd($report['accounts'][$sourceAccount]['sum'], $journal['amount']);
// add currency info for account sum
$report['accounts'][$sourceAccount]['currency_id'] = $journal['currency_id'];
$report['accounts'][$sourceAccount]['currency_code'] = $journal['currency_code'];
$report['accounts'][$sourceAccount]['currency_name'] = $journal['currency_name'];
$report['accounts'][$sourceAccount]['currency_symbol'] = $journal['currency_symbol'];
$report['accounts'][$sourceAccount]['currency_decimal_places'] = $journal['currency_decimal_places'];
}
$report['budgets'][$budgetId]['spent'] = $spent;
// get transactions in budget
}
return $report;
// do sums:
echo '<pre>';
print_r($report);
exit;
$balance = new Balance; $balance = new Balance;
$header = new BalanceHeader; $header = new BalanceHeader;
$budgetLimits = $this->budgetRepository->getAllBudgetLimits($start, $end); $budgetLimits = $this->budgetRepository->getAllBudgetLimits($start, $end);

View File

@@ -38,7 +38,7 @@ interface BalanceReportHelperInterface
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return Balance * @return array
*/ */
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance; public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): array;
} }

View File

@@ -47,7 +47,6 @@ class BalanceController extends Controller
*/ */
public function general(Collection $accounts, Carbon $start, Carbon $end) public function general(Collection $accounts, Carbon $start, Carbon $end)
{ {
// chart properties for cache: // chart properties for cache:
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty($start); $cache->addProperty($start);
@@ -55,17 +54,19 @@ class BalanceController extends Controller
$cache->addProperty('balance-report'); $cache->addProperty('balance-report');
$cache->addProperty($accounts->pluck('id')->toArray()); $cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) { if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore //return $cache->get(); // @codeCoverageIgnore
} }
$helper = app(BalanceReportHelperInterface::class); $helper = app(BalanceReportHelperInterface::class);
$balance = $helper->getBalanceReport($accounts, $start, $end); $report = $helper->getBalanceReport($accounts, $start, $end);
try { // TODO no budget.
$result = view('reports.partials.balance', compact('balance'))->render(); // TODO sum over account.
// try {
$result = view('reports.partials.balance', compact('report'))->render();
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
} catch (Throwable $e) { // } catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage())); // Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage()));
$result = 'Could not render view.'; // $result = 'Could not render view.';
} // }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
$cache->store($result); $cache->store($result);

View File

@@ -171,31 +171,60 @@ class CategoryController extends Controller
/** @var CategoryRepositoryInterface $repository */ /** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class); $repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories(); $categories = $repository->getCategories();
$report = []; $report = [
'categories' => [],
'sums' => [],
];
/** @var Category $category */ /** @var Category $category */
foreach ($categories as $category) { foreach ($categories as $category) {
$spent = $repository->spentInPeriod($category, $accounts, $start, $end); $spent = $repository->spentInPeriod($category, $accounts, $start, $end);
$earned = $repository->earnedInPeriod($category, $accounts, $start, $end); $earned = $repository->earnedInPeriod($category, $accounts, $start, $end);
$currencies = array_keys($spent) + array_keys($earned); if (0 === count($spent) && 0 === count($earned)) {
continue;
}
$currencies = array_unique(array_merge(array_keys($spent), array_keys($earned)));
foreach ($currencies as $code) { foreach ($currencies as $code) {
$currencyInfo = $spent[$code] ?? $earned[$code]; $currencyInfo = $spent[$code] ?? $earned[$code];
$report[$category->id] = [ $key = sprintf('%s-%s', $category->id, $code);
'name' => sprintf('%s (%s)', $category->name, $code), $report['categories'][$key] = [
'spent' => round($spent[$code]['spent'] ?? '0', $currencyInfo['currency_decimal_places']), 'name' => $category->name,
'earned' => round($earned[$code]['earned'] ?? '0', $currencyInfo['currency_decimal_places']), 'spent' => $spent[$code]['spent'] ?? '0',
'earned' => $earned[$code]['earned'] ?? '0',
'id' => $category->id, 'id' => $category->id,
'currency_id' => $currencyInfo['currency_id'], 'currency_id' => $currencyInfo['currency_id'],
'currency_code' => $currencyInfo['currency_code'], 'currency_code' => $currencyInfo['currency_code'],
'currency_symbol' => $currencyInfo['currency_symbol'], 'currency_symbol' => $currencyInfo['currency_symbol'],
'cyrrency_decimal_places' => $currencyInfo['currency_decimal_places'], 'currency_name' => $currencyInfo['currency_name'],
'currency_decimal_places' => $currencyInfo['currency_decimal_places'],
]; ];
} }
} }
$sum = []; $sum = [];
foreach ($report as $categoryId => $row) { /**
$sum[$categoryId] = (float)$row['spent']; * @var string $categoryId
* @var array $row
*/
foreach ($report['categories'] as $categoryId => $row) {
$sum[$categoryId] = (float)$row['spent'];
}
array_multisort($sum, SORT_ASC, $report['categories']);
// get sums:
foreach ($report['categories'] as $entry) {
$currencyId = $entry['currency_id'];
$report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [
'spent' => '0',
'earned' => '0',
'currency_id' => $entry['currency_id'],
'currency_code' => $entry['currency_code'],
'currency_symbol' => $entry['currency_symbol'],
'currency_name' => $entry['currency_name'],
'cyrrency_decimal_places' => $entry['currency_decimal_places'],
];
$report['sums'][$currencyId]['spent'] = bcadd($report['sums'][$currencyId]['spent'], $entry['spent']);
$report['sums'][$currencyId]['earned'] = bcadd($report['sums'][$currencyId]['earned'], $entry['earned']);
} }
array_multisort($sum, SORT_ASC, $report);
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
try { try {

View File

@@ -74,7 +74,6 @@ class ExpenseController extends Controller
* @param Carbon $end * @param Carbon $end
* *
* @return string * @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/ */
public function budget(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): string public function budget(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): string
{ {

View File

@@ -1,59 +1,45 @@
<table class="table table-hover"> <table class="table table-hover">
<thead> <thead>
<tr> <tr>
<th colspan="2">{{ 'budgets'|_ }}</th> <th>{{ 'budgets'|_ }}</th>
{% for account in balance.getBalanceHeader.getAccounts %} {% for account in report.accounts %}
<th class="hidden-xs" style="text-align: right;"><a href="{{ route('accounts.show',account.id) }}">{{ account.name }}</a></th> <th class="hidden-xs" style="text-align: right;"><a href="{{ route('accounts.show',account.id) }}" title="{{ account.iban|default(account.name) }}">{{ account.name }}</a></th>
{% endfor %} {% endfor %}
<th style="text-align: right;"> <th style="text-align: right;">{{ 'sum'|_ }}</th>
{{ 'leftInBudget'|_ }}
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for balanceLine in balance.getBalanceLines %} {% for budget in report.budgets %}
<tr> <tr>
<td>
{% if balanceLine.getBudget.id %} <a href="{{ route('budgets.show', [budget.budget_id]) }}">{{ budget.budget_name }}</a>
<td> </td>
<a href="{{ route('budgets.show',balanceLine.getBudget.id) }}">{{ balanceLine.getTitle }}</a> {% for account in report.accounts %}
{% if balanceLine.getStartdate and balanceLine.getEnddate %} {% if budget.spent[account.id] %}
<span class="small" class="hidden-xs"><br>
{{ balanceLine.getStartdate.formatLocalized(monthAndDayFormat) }}
&mdash;
{{ balanceLine.getEnddate.formatLocalized(monthAndDayFormat) }}
</span>
{% endif %}
</td>
<td style="text-align: right;">
{% if(balanceLine.getBudgetLimit.amount) %}
{{ balanceLine.getBudgetLimit.amount|formatAmount }}
{% else %}
{{ '0'|formatAmount }}
{% endif %}
</td>
{% else %}
<td colspan="2">{{ balanceLine.getTitle }}</td>
{% endif %}
{% for balanceEntry in balanceLine.getBalanceEntries %}
<td class="hidden-xs" style="text-align: right;">
{% if balanceEntry.getSpent != 0 %}
<span class="text-danger">{{ (balanceEntry.getSpent)|formatAmountPlain }}</span>
<i class="fa fa-fw text-muted fa-info-circle firefly-info-button" data-location="balance-amount"
data-account-id="{{ balanceEntry.getAccount.id }}"
data-budget-id="{{ balanceLine.getBudget.id }}" data-role="{{ balanceLine.getRole }}"></i>
{% endif %}
{% if balanceEntry.getLeft != 0 %}
<span class="text-success" style="text-align: right;">{{ (balanceEntry.getLeft)|formatAmountPlain }}</span>
{% endif %}
</td>
{% endfor %}
<td style="text-align: right;"> <td style="text-align: right;">
{{ balanceLine.leftOfRepetition|formatAmount }} {{ formatAmountBySymbol(budget.spent[account.id].spent, budget.spent[account.id].currency_symbol, budget.spent[account.id].currency_decimal_places) }}
</td> </td>
</tr> {% else %}
<td>
&nbsp;
</td>
{% endif %}
{% endfor %}
<td style="text-align: right;">
{% for sum in report.sums[budget.budget_id] %}
{{ formatAmountBySymbol(sum.sum, sum.currency_symbol, sum.currency_decimal_places) }}<br />
{% endfor %}
</td>
</tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
<tfoot>
<tr>
<td><em>{{ 'sum'|_ }}</em></td>
{% for account in report.accounts %}
<td style="text-align: right;">{{ formatAmountBySymbol(account.sum, account.currency_symbol, account.currency_decimal_places) }}</td>
{% endfor %}
</tr>
</tfoot>
</table> </table>

View File

@@ -9,11 +9,7 @@
</thead> </thead>
<tbody> <tbody>
{% set sumSpent = 0 %} {% for index, category in report.categories %}
{% set sumEarned = 0 %}
{% for index, category in report %}
{% set sumSpent = sumSpent + category.spent %}
{% set sumEarned = sumEarned + category.earned %}
{% if loop.index > listLength %} {% if loop.index > listLength %}
<tr class="overListLength"> <tr class="overListLength">
{% else %} {% else %}
@@ -35,11 +31,23 @@
<tfoot> <tfoot>
{% if report|length > listLength %} {% if report|length > listLength %}
<tr> <tr>
<td colspan="3" class="active"> <td colspan="4" class="active">
<a href="#" class="listLengthTrigger">{{ trans('firefly.show_full_list',{number:incomeTopLength}) }}</a> <a href="#" class="listLengthTrigger">{{ trans('firefly.show_full_list',{number:incomeTopLength}) }}</a>
</td> </td>
</tr> </tr>
{% endif %} {% endif %}
{% for sum in report.sums %}
<tr>
<td><em>{{ 'sum'|_ }} ({{ sum.currency_name }})</em></td>
<td style="text-align: right;">
{{ formatAmountBySymbol(sum.spent, sum.currency_symbol, sum.currency_decimal_places) }}
</td>
<td style="text-align: right;">
{{ formatAmountBySymbol(sum.earned, sum.currency_symbol, sum.currency_decimal_places) }}
</td>
<td></td>
</tr>
{% endfor %}
{# {#
<tr> <tr>
<td><em>{{ 'sum'|_ }}</em></td> <td><em>{{ 'sum'|_ }}</em></td>